Spaces:
Sleeping
Sleeping
fix croppad to mimic RandomResizedCrop in validation
Browse files- .vscode/launch.json +17 -0
- deployment/transforms.py +22 -2
- tests/test_transforms.py +13 -2
- training/notebooks/transforms-lab.ipynb +0 -0
.vscode/launch.json
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
// Use IntelliSense to learn about possible attributes.
|
3 |
+
// Hover to view descriptions of existing attributes.
|
4 |
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
5 |
+
"version": "0.2.0",
|
6 |
+
"configurations": [
|
7 |
+
{
|
8 |
+
"name": "Python: Debug Tests",
|
9 |
+
"type": "python",
|
10 |
+
"request": "launch",
|
11 |
+
"program": "${file}",
|
12 |
+
"purpose": ["debug-test"],
|
13 |
+
"console": "integratedTerminal",
|
14 |
+
"justMyCode": false
|
15 |
+
}
|
16 |
+
]
|
17 |
+
}
|
deployment/transforms.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from typing import Literal, Union, Tuple
|
2 |
|
3 |
import torch
|
@@ -70,9 +71,28 @@ def pad(image, size: Tuple[int, int]) -> Image:
|
|
70 |
return tvf.pad(image, [pad_top, pad_left, height, width], padding_mode="constant")
|
71 |
|
72 |
|
73 |
-
def CenterCropPad(size: tuple[Literal[460], Literal[460]]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
# return tvtfms.CenterCrop(size)
|
75 |
def _crop_pad(img):
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
return _crop_pad
|
|
|
1 |
+
import math
|
2 |
from typing import Literal, Union, Tuple
|
3 |
|
4 |
import torch
|
|
|
71 |
return tvf.pad(image, [pad_top, pad_left, height, width], padding_mode="constant")
|
72 |
|
73 |
|
74 |
+
def CenterCropPad(size: tuple[Literal[460], Literal[460]], val_xtra: float = 0.14):
|
75 |
+
"""
|
76 |
+
Args:
|
77 |
+
image (`PIL.Image`):
|
78 |
+
An image to perform padding on
|
79 |
+
size (`tuple` of integers):
|
80 |
+
A size to pad to, should be in the form
|
81 |
+
of (width, height)
|
82 |
+
val_xtra: The ratio of size at the edge cropped out in the validation set
|
83 |
+
"""
|
84 |
# return tvtfms.CenterCrop(size)
|
85 |
def _crop_pad(img):
|
86 |
+
orig_sz = img.shape
|
87 |
+
xtra = math.ceil(max(*size[:2]) * val_xtra / 8) * 8
|
88 |
+
final_size = (size[0] + xtra, size[1] + xtra)
|
89 |
+
|
90 |
+
res = pad(center_crop(img, orig_sz), orig_sz).resize(
|
91 |
+
final_size, resample=Image.Resampling.BILINEAR
|
92 |
+
)
|
93 |
+
if final_size != size:
|
94 |
+
res = pad(center_crop(res, size), size)
|
95 |
+
|
96 |
+
return res
|
97 |
|
98 |
return _crop_pad
|
tests/test_transforms.py
CHANGED
@@ -46,8 +46,8 @@ class TestTransforms:
|
|
46 |
crop_fastai = fastai_aug.CropPad((460, 460))
|
47 |
crop_rrc = fastai_aug.RandomResizedCrop((460, 460))
|
48 |
|
49 |
-
|
50 |
-
|
51 |
|
52 |
assert (np.array(cropped_rrc) == np.array(cropped_fastai)).all()
|
53 |
|
@@ -56,3 +56,14 @@ class TestTransforms:
|
|
56 |
crop_torch = CenterCropPad((460, 460))
|
57 |
|
58 |
assert (np.array(crop_fastai(im_fastai)) == np.array(crop_torch(im_pil))).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
crop_fastai = fastai_aug.CropPad((460, 460))
|
47 |
crop_rrc = fastai_aug.RandomResizedCrop((460, 460))
|
48 |
|
49 |
+
cropped_rrc = crop_rrc(im_fastai, split_idx=1)
|
50 |
+
cropped_fastai = crop_fastai(im_fastai, split_idx=1)
|
51 |
|
52 |
assert (np.array(cropped_rrc) == np.array(cropped_fastai)).all()
|
53 |
|
|
|
56 |
crop_torch = CenterCropPad((460, 460))
|
57 |
|
58 |
assert (np.array(crop_fastai(im_fastai)) == np.array(crop_torch(im_pil))).all()
|
59 |
+
|
60 |
+
def testRandomResizedCropInValidationEqualsCustomCenterCropPad(
|
61 |
+
self, im_fastai: PILImage, im_pil: Image
|
62 |
+
):
|
63 |
+
crop_rrc = fastai_aug.RandomResizedCrop((460, 460))
|
64 |
+
crop_custom = CenterCropPad((460, 460))
|
65 |
+
|
66 |
+
cropped_rrc = crop_rrc(im_fastai, split_idx=1)
|
67 |
+
cropped_custom = crop_custom(im_fastai)
|
68 |
+
|
69 |
+
assert (np.array(cropped_rrc) == np.array(cropped_custom)).all()
|
training/notebooks/transforms-lab.ipynb
CHANGED
The diff for this file is too large to render.
See raw diff
|
|