<> Applied to image classification
from torchvision.datasets import ImageFolder from torchvision import transforms
from torch.utils.data import DataLoader # Image target path ROOT_TRAIN = './data/train' #
a set 2 Package images batch_size = 2 # Pretreatment packaging : More preprocessing operations can be added train_transform = transforms.Compose
([ # zoom transforms.Resize((224, 224)), # ------------------ Data enhancement
start --------------------- # Random rotation ,-45 Degree to 45 Random selection between degrees transforms.RandomRotation(45), #
Crop from center transforms.CenterCrop(224), # Random horizontal flip The selected probability value is p=0.5 transforms.
RandomHorizontalFlip(p=0.5), # Random vertical flip transforms.RandomVerticalFlip(p=0.5), #
parameter : brightness , contrast ratio , saturation , Hue transforms.ColorJitter(brightness=0.2, contrast=0.1, saturation
=0.1, hue=0.1), # Turn into 3 Channel gray image R=G=B Probability setting 0.025 transforms.RandomGrayscale(p=0.025),
# ------------------ Data enhancement end --------------------- # Convert data format to rensor Necessary steps transforms
.ToTensor(), # normalization # Normalize the pixel value of the image to 【-1, 1】 between mean value , standard deviation It's calculated , for example VGG:[0.485, 0.456,
0.406], [0.229, 0.224, 0.225] # this 0.5 It's free transforms.Normalize([0.5, 0.5, 0.5],
[0.5, 0.5, 0.5])]) # Data path , Model preprocessing and packaging train_data = ImageFolder(ROOT_TRAIN, transform
=train_transform) # Format packaging # parameter : data ,1 Group several , Is the next round disrupted , Number of processes , Does the last group form a group train_datas =
DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True, num_workers=
4, drop_last=True) # The returned data is Iterative object of the label corresponding to the image # for data in train_datas: # imgs,
targets = data

Technology