Example #1
0
def loader_from_list(root,
                     file_list,
                     batch_size,
                     new_size=None,
                     height=64,
                     width=128,
                     crop=True,
                     num_workers=4,
                     shuffle=True,
                     center_crop=True,
                     return_paths=False,
                     drop_last=True):
    # transform_list = [transforms.ToTensor(),
    #                   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
    # if center_crop:
    #     transform_list = [transforms.CenterCrop((height, width))] + \
    #                      transform_list if crop else transform_list
    # else:
    #     transform_list = [transforms.RandomCrop((height, width))] + \
    #                      transform_list if crop else transform_list
    # transform_list = [transforms.Resize(new_size)] + transform_list \
    #     if new_size is not None else transform_list
    # if not center_crop:
    #     transform_list = [transforms.RandomHorizontalFlip()] + transform_list

    transform_list = []
    transform_list += [
        transforms.Resize(new_size),
        transforms.CenterCrop(height),
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
    ]

    transform = transforms.Compose(transform_list)
    dataset = ImageLabelFilelist(root,
                                 file_list,
                                 transform,
                                 return_paths=return_paths)
    loader = DataLoader(dataset,
                        batch_size,
                        shuffle=shuffle,
                        drop_last=drop_last,
                        num_workers=num_workers)
    return loader
Example #2
0
def loader_from_list(
        root,
        file_list,
        batch_size,
        new_size=None,
        height=128,
        width=128,
        crop=True,
        num_workers=4,
        shuffle=True,
        center_crop=False,
        return_paths=False,
        drop_last=True):
    transform_list = [transforms.ToTensor(),
                      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
    if center_crop:
        transform_list = [transforms.CenterCrop((height, width))] + \
                         transform_list if crop else transform_list
    else:
        transform_list = [transforms.RandomCrop((height, width))] + \
                         transform_list if crop else transform_list
    # 先将图片转换为140*140,再随机挑出128*128
    transform_list = [transforms.Resize((new_size,new_size))] + transform_list \
        if new_size is not None else transform_list
    if not center_crop: # 以一定概率水平翻转
        transform_list = [transforms.RandomHorizontalFlip()] + transform_list
    transform = transforms.Compose(transform_list) # 得到transform组合体
    dataset = ImageLabelFilelist(root,
                                 file_list,
                                 transform,
                                 return_paths=return_paths)
    loader = DataLoader(dataset,
                        batch_size,
                        shuffle=shuffle,
                        drop_last=drop_last,
                        num_workers=num_workers)
    return loader