(
img_size=(224, 224),
scale=(0.08, 1.0),
ratio=(3./4., 4./3.),
hflip=0.5,
vflip=0.5,
auto_augment='rand-m9-mstd0.5-inc1',
interpolation='random',
mean=(0.485, 0.456, 0.406),
jpeg_compression = 0,
)
| 24 | |
| 25 | # 原始数据增强 |
| 26 | def transforms_imagenet_train( |
| 27 | img_size=(224, 224), |
| 28 | scale=(0.08, 1.0), |
| 29 | ratio=(3./4., 4./3.), |
| 30 | hflip=0.5, |
| 31 | vflip=0.5, |
| 32 | auto_augment='rand-m9-mstd0.5-inc1', |
| 33 | interpolation='random', |
| 34 | mean=(0.485, 0.456, 0.406), |
| 35 | jpeg_compression = 0, |
| 36 | ): |
| 37 | scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range |
| 38 | ratio = tuple(ratio or (3./4., 4./3.)) # default imagenet ratio range |
| 39 | |
| 40 | primary_tfl = [ |
| 41 | RandomResizedCropAndInterpolation(img_size, scale=scale, ratio=ratio, interpolation=interpolation)] |
| 42 | if hflip > 0.: |
| 43 | primary_tfl += [transforms.RandomHorizontalFlip(p=hflip)] |
| 44 | if vflip > 0.: |
| 45 | primary_tfl += [transforms.RandomVerticalFlip(p=vflip)] |
| 46 | |
| 47 | secondary_tfl = [] |
| 48 | if auto_augment: |
| 49 | assert isinstance(auto_augment, str) |
| 50 | |
| 51 | if isinstance(img_size, (tuple, list)): |
| 52 | img_size_min = min(img_size) |
| 53 | else: |
| 54 | img_size_min = img_size |
| 55 | |
| 56 | aa_params = dict( |
| 57 | translate_const=int(img_size_min * 0.45), |
| 58 | img_mean=tuple([min(255, round(255 * x)) for x in mean]), |
| 59 | ) |
| 60 | if auto_augment.startswith('rand'): |
| 61 | secondary_tfl += [rand_augment_transform(auto_augment, aa_params)] |
| 62 | elif auto_augment.startswith('augmix'): |
| 63 | aa_params['translate_pct'] = 0.3 |
| 64 | secondary_tfl += [augment_and_mix_transform(auto_augment, aa_params)] |
| 65 | else: |
| 66 | secondary_tfl += [auto_augment_transform(auto_augment, aa_params)] |
| 67 | |
| 68 | if jpeg_compression == 1: |
| 69 | secondary_tfl += [JPEGCompression(quality=10, p=0.3)] |
| 70 | |
| 71 | final_tfl = [transforms.ToTensor()] |
| 72 | |
| 73 | return transforms.Compose(primary_tfl + secondary_tfl + final_tfl) |
| 74 | |
| 75 | |
| 76 | # 推理(测试)使用 |
no test coverage detected