|
1 |
| -from torch import nn, Tensor |
2 |
| -from torchvision.transforms import autoaugment, transforms |
| 1 | +from torchvision.transforms import * |
3 | 2 |
|
4 | 3 |
|
5 |
| -class ClassificationPresetTrain(nn.Module): |
| 4 | +class ClassificationPresetTrain: |
6 | 5 | def __init__(self, crop_size, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), auto_augment_policy=None,
|
7 | 6 | random_erase_prob=0.0):
|
8 |
| - super().__init__() |
9 |
| - |
10 | 7 | trans = [
|
11 |
| - transforms.RandomResizedCrop(crop_size), |
12 |
| - transforms.RandomHorizontalFlip(), |
| 8 | + RandomResizedCrop(crop_size), |
| 9 | + RandomHorizontalFlip(), |
13 | 10 | ]
|
14 | 11 | if auto_augment_policy is not None:
|
15 |
| - aa_policy = autoaugment.AutoAugmentPolicy(auto_augment_policy) |
16 |
| - trans.append(autoaugment.AutoAugment(policy=aa_policy)) |
| 12 | + aa_policy = AutoAugmentPolicy(auto_augment_policy) |
| 13 | + trans.append(AutoAugment(policy=aa_policy)) |
17 | 14 | trans.extend([
|
18 |
| - transforms.ToTensor(), |
19 |
| - transforms.Normalize(mean=mean, std=std), |
| 15 | + ToTensor(), |
| 16 | + Normalize(mean=mean, std=std), |
20 | 17 | ])
|
21 | 18 | if random_erase_prob > 0:
|
22 |
| - trans.append(transforms.RandomErasing(p=random_erase_prob)) |
| 19 | + trans.append(RandomErasing(p=random_erase_prob)) |
23 | 20 |
|
24 |
| - self.transforms = nn.Sequential(*trans) |
| 21 | + self.transforms = Compose(trans) |
25 | 22 |
|
26 |
| - def forward(self, img: Tensor) -> Tensor: |
| 23 | + def __call__(self, img): |
27 | 24 | return self.transforms(img)
|
28 | 25 |
|
29 | 26 |
|
30 |
| -class ClassificationPresetEval(nn.Module): |
| 27 | +class ClassificationPresetEval: |
31 | 28 | def __init__(self, crop_size, resize_size=256, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
|
32 |
| - super().__init__() |
33 |
| - |
34 |
| - trans = [ |
35 |
| - transforms.Resize(resize_size), |
36 |
| - transforms.CenterCrop(crop_size), |
37 |
| - transforms.ToTensor(), |
38 |
| - transforms.Normalize(mean=mean, std=std), |
39 |
| - ] |
40 |
| - self.transforms = nn.Sequential(*trans) |
| 29 | + self.transforms = Compose([ |
| 30 | + Resize(resize_size), |
| 31 | + CenterCrop(crop_size), |
| 32 | + ToTensor(), |
| 33 | + Normalize(mean=mean, std=std), |
| 34 | + ]) |
41 | 35 |
|
42 |
| - def forward(self, img: Tensor) -> Tensor: |
| 36 | + def __call__(self, img): |
43 | 37 | return self.transforms(img)
|
0 commit comments