in src/transformers/data/datasets/img_cls_datasets.py [0:0]
def build_transform(is_train, cfg):
resize_im = cfg.CONFIG.DATA.IMG_SIZE > 32
if is_train:
# this should always dispatch to transforms_imagenet_train
transform = create_transform(
input_size=cfg.CONFIG.DATA.IMG_SIZE,
is_training=True,
color_jitter=cfg.CONFIG.AUG.COLOR_JITTER,
auto_augment=cfg.CONFIG.AUG.AUTO_AUGMENT,
interpolation=cfg.CONFIG.DATA.INTERPOLATION,
re_prob=cfg.CONFIG.AUG.REPROB,
re_mode=cfg.CONFIG.AUG.REMODE,
re_count=cfg.CONFIG.AUG.RECOUNT,
)
if not resize_im:
# replace RandomResizedCropAndInterpolation with
# RandomCrop
transform.transforms[0] = transforms.RandomCrop(
cfg.CONFIG.DATA.IMG_SIZE, padding=4)
return transform
t = []
if cfg.CONFIG.DATA.TEST_NUM_CROP == 1:
# center crop
if resize_im:
size = int((1.0 / cfg.CONFIG.DATA.CROP_PCT) * cfg.CONFIG.DATA.IMG_SIZE)
t.append(
transforms.Resize(size, interpolation=3), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.CenterCrop(cfg.CONFIG.DATA.IMG_SIZE))
t.append(transforms.ToTensor())
if cfg.CONFIG.DATA.IMAGENET_DEFAULT_NORMALIZE:
t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))
else:
t.append(transforms.Normalize(0.5, 0.5))
elif cfg.CONFIG.DATA.TEST_NUM_CROP == 5:
# five crop
if resize_im:
size = int((1.0 / cfg.CONFIG.DATA.CROP_PCT) * cfg.CONFIG.DATA.IMG_SIZE)
t.append(
transforms.Resize(size, interpolation=3), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.FiveCrop(cfg.CONFIG.DATA.IMG_SIZE))
t.append(Stack2Tensor())
else:
t.append(transforms.ToTensor())
if cfg.CONFIG.DATA.IMAGENET_DEFAULT_NORMALIZE:
t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))
else:
t.append(transforms.Normalize(0.5, 0.5))
elif cfg.CONFIG.DATA.TEST_NUM_CROP == 10:
# ten crop
if resize_im:
size = int((1.0 / cfg.CONFIG.DATA.CROP_PCT) * cfg.CONFIG.DATA.IMG_SIZE)
t.append(
transforms.Resize(size, interpolation=3), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.TenCrop(cfg.CONFIG.DATA.IMG_SIZE))
t.append(Stack2Tensor())
else:
t.append(transforms.ToTensor())
if cfg.CONFIG.DATA.IMAGENET_DEFAULT_NORMALIZE:
t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))
else:
t.append(transforms.Normalize(0.5, 0.5))
else:
raise RuntimeError("Evaluation only supports center crop, five crop or ten crop mode. \
Please set TEST_NUM_CROP in configuration to 1, 5 or 10 accordingly.")
return transforms.Compose(t)