In the train file: where dataset is the method of dataloader, and dataloader is equal to CreateDataLoader.
So we jump to CreateDataLoader:
the dataset_loader returned in CreateDataLoader is from CustomDatasetDataLoader. Everything calls initialize. Because CustomDatasetDataLoader is a class, initialize calls the method in the class.
We go to CustomDatasetDataLoader:
first we define the name method, and when we call it, we return: 'CustomDatasetDataLoader'. Then initialize is defined:
first, the dataset dataset is defined:
dataset comes from AlignedDataset, and the initialize method is called:
AlignedDataset inherits BaseDataset, BaseDataset is an abstract class, and the method needs to be rewritten by AlignedDataset:
in AlignedDataset, we load the dataset, which is len, getitem, ini three elements.
First, judge whether the input is a label according to the channel of the label. If not label, self.opt.label_nc == 0, dir_A='_A', otherwise dir_A='_label'. Then get the path of dir_A.
Put the image of train_label in a list according to the make_dataset function:
train_img, train_instance is the same.
Then randomly enter an index in getitem, go to the list A to get the value according to the index, and open the extracted path through image, which is in Image format after opening. Then enter it into the get_params function.
First get the size of the picture, and then determine the cropping method. The default is no cropping, zoom to 1024x512 (read me has a talk):
Then the original picture is 2048x1024, (x,y)=rand((2048-512), (1024- 512)), returns a dictionary, 'flip' corresponds to True or False.
Perform transfromer transformation according to the label channel:
def get_transform(opt, params, method=Image.BICUBIC, normalize=True):
transform_list = []
if 'resize' in opt.resize_or_crop:
osize = [opt.loadSize, opt.loadSize]
transform_list.append(transforms.Scale(osize, method))
elif 'scale_width' in opt.resize_or_crop:
transform_list.append(transforms.Lambda(lambda img: __scale_width(img, opt.loadSize, method)))
if 'crop' in opt.resize_or_crop:
transform_list.append(transforms.Lambda(lambda img: __crop(img, params['crop_pos'], opt.fineSize)))
if opt.resize_or_crop == 'none':
base = float(2 ** opt.n_downsample_global)
if opt.netG == 'local':
base *= (2 ** opt.n_local_enhancers)
transform_list.append(transforms.Lambda(lambda img: __make_power_2(img, base, method)))
if opt.isTrain and not opt.no_flip:
transform_list.append(transforms.Lambda(lambda img: __flip(img, params['flip'])))
transform_list += [transforms.ToTensor()]
if normalize:
transform_list += [transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]
return transforms.Compose(transform_list)
First define an empty list and add it according to the conditions:
first add the __scale_width operation: transforms.Lambda encapsulates __scale_width into transform
Three parameters, image size, target_w=1024, method=bicubic.
First ow=2048, oh=1024, w=1024, h=(1024*1024/2048)=512, then interpolate img to (1024,512). The remaining transformations can be added as appropriate.
Finally, convert to tensor and normalize and string together with compose.
After a series of transformations, it is multiplied by 255, and the label is multiplied by 255 to convert it into a grayscale image.
Do the same for image, but without multiplying by 255. If using instance images, do the same for labels.
Finally, a dictionary is used to store it:
the CreateDataset ends, and the output dataset is passed to the dataloader. The value after entering the dataloader can be obtained by calling CustomDatasetDataLoader.load_data.
Finally back to train: