Spaces:
Sleeping
Sleeping
tested complete transforms pipeline
Browse files
tests/test_validation_transforms.py
CHANGED
@@ -65,18 +65,45 @@ class TestTransforms:
|
|
65 |
).all()
|
66 |
|
67 |
def testFlipEqualsCustomGPUCrop(self, im_fastai: PILImage, im_pil: Image):
|
|
|
68 |
tt_fastai = fastai_aug.ToTensor()
|
69 |
i2f_fastai = fastai_aug.IntToFloatTensor()
|
70 |
flip = fastai_aug.Flip(size=(224, 224))
|
71 |
-
tt_torch = tvtfms.ToTensor()
|
72 |
-
|
73 |
-
# apply flip augmentation on validation
|
74 |
result_im_fastai = flip(
|
75 |
i2f_fastai(tt_fastai(im_fastai).unsqueeze(0)), split_idx=1
|
76 |
)
|
77 |
|
78 |
# apply custom gpu crop
|
|
|
79 |
result_im_tv = gpu_crop(tt_torch(im_pil).unsqueeze(0), size=(224, 224))
|
80 |
|
81 |
assert torch.allclose(result_im_fastai, result_im_tv)
|
82 |
assert (result_im_fastai == result_im_tv).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
).all()
|
66 |
|
67 |
def testFlipEqualsCustomGPUCrop(self, im_fastai: PILImage, im_pil: Image):
|
68 |
+
# apply flip augmentation on validation
|
69 |
tt_fastai = fastai_aug.ToTensor()
|
70 |
i2f_fastai = fastai_aug.IntToFloatTensor()
|
71 |
flip = fastai_aug.Flip(size=(224, 224))
|
|
|
|
|
|
|
72 |
result_im_fastai = flip(
|
73 |
i2f_fastai(tt_fastai(im_fastai).unsqueeze(0)), split_idx=1
|
74 |
)
|
75 |
|
76 |
# apply custom gpu crop
|
77 |
+
tt_torch = tvtfms.ToTensor()
|
78 |
result_im_tv = gpu_crop(tt_torch(im_pil).unsqueeze(0), size=(224, 224))
|
79 |
|
80 |
assert torch.allclose(result_im_fastai, result_im_tv)
|
81 |
assert (result_im_fastai == result_im_tv).all()
|
82 |
+
|
83 |
+
def testFastaiTransformsEqualsCustom(self, im_fastai: PILImage, im_pil: Image):
|
84 |
+
# fastai transforms
|
85 |
+
crop_rrc = fastai_aug.RandomResizedCrop((460, 460))
|
86 |
+
tt_fastai = fastai_aug.ToTensor()
|
87 |
+
i2f_fastai = fastai_aug.IntToFloatTensor()
|
88 |
+
flip = fastai_aug.Flip(size=(224, 224))
|
89 |
+
brightness = fastai_aug.Brightness()
|
90 |
+
norm_fastai = fastai_aug.Normalize.from_stats(
|
91 |
+
*fastai_aug.imagenet_stats, cuda=False
|
92 |
+
)
|
93 |
+
|
94 |
+
# custom transforms
|
95 |
+
tt_torch = tvtfms.ToTensor()
|
96 |
+
norm_torch = tvtfms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
97 |
+
|
98 |
+
# apply all fastai augmentations on validation (transformations)
|
99 |
+
batch_im_fastai = tt_fastai(crop_rrc(im_fastai, split_idx=1)).unsqueeze(0)
|
100 |
+
result_im_fastai = norm_fastai(
|
101 |
+
brightness(flip(i2f_fastai(batch_im_fastai), split_idx=1), split_idx=1)
|
102 |
+
)
|
103 |
+
|
104 |
+
# apply all custom transformations
|
105 |
+
batch_im_tv = tt_torch(resized_crop_pad(im_pil, (460, 460))).unsqueeze(0)
|
106 |
+
result_im_tv = norm_torch(gpu_crop(batch_im_tv, size=(224, 224)))
|
107 |
+
|
108 |
+
assert torch.allclose(result_im_fastai, result_im_tv)
|
109 |
+
assert (result_im_fastai == result_im_tv).all()
|