pix2pixHD code --- data set processing

In the train file: where dataset is the method of dataloader, and dataloader is equal to CreateDataLoader.
insert image description here
So we jump to CreateDataLoader:
insert image description here
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:
insert image description here
first we define the name method, and when we call it, we return: 'CustomDatasetDataLoader'. Then initialize is defined:
first, the dataset dataset is defined:
insert image description here

dataset comes from AlignedDataset, and the initialize method is called:
insert image description here
AlignedDataset inherits BaseDataset, BaseDataset is an abstract class, and the method needs to be rewritten by AlignedDataset:
insert image description here
in AlignedDataset, we load the dataset, which is len, getitem, ini three elements.
insert image description here
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.
insert image description here
Put the image of train_label in a list according to the make_dataset function:
insert image description here
train_img, train_instance is the same.
insert image description here
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.
insert image description here
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):
insert image description here
Then the original picture is 2048x1024, (x,y)=rand((2048-512), (1024- 512)), returns a dictionary, 'flip' corresponds to True or False.
insert image description here
Perform transfromer transformation according to the label channel:
insert image description here

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
insert image description here
insert image description here
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.
insert image description here
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:
insert image description here
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.
insert image description here
Finally back to train:
insert image description here

Guess you like

Origin blog.csdn.net/qq_43733107/article/details/130849970