Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit d7d4241

Browse files
authored
Dev (#19)
* refactored loaders, moved augmentations end point to config * changed paths in notebooks, updated configs
1 parent bcd666f commit d7d4241

9 files changed

+1430
-169
lines changed

notebooks/augmentations.ipynb

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"metadata": {},
2727
"outputs": [],
2828
"source": [
29-
"IMG_DIR = '/mnt/ml-team/minerva/open-solutions/salt/files/train/images'\n",
29+
"IMG_DIR = 'YOUR/DIR'\n",
3030
"IMG_IDX = 18\n",
3131
"img_filepath = sorted(glob.glob('{}/*'.format(IMG_DIR)))[IMG_IDX]\n",
3232
"img = np.array(Image.open(img_filepath))\n",
@@ -47,15 +47,15 @@
4747
"outputs": [],
4848
"source": [
4949
"affine_seq = iaa.Sequential([\n",
50-
" # General\n",
50+
"# General\n",
5151
" iaa.SomeOf((1, 2),\n",
5252
" [iaa.Fliplr(0.5),\n",
53-
" iaa.Affine(rotate=(-20, 20),\n",
54-
" translate_percent={\"x\": (-0.1, 0.1), \"y\": (-0.1, 0.1)}, mode='symmetric'),\n",
53+
" iaa.Affine(rotate=(-10, 10),\n",
54+
" translate_percent={\"x\": (-0.25, 0.25)}, mode='symmetric'),\n",
5555
" ]),\n",
56-
" # Deformations\n",
57-
" iaa.Sometimes(0.3, iaa.PiecewiseAffine(scale=(0.02, 0.04))),\n",
58-
" iaa.Sometimes(0.3, iaa.PerspectiveTransform(scale=(0.05, 0.10))),\n",
56+
"# Deformations\n",
57+
" iaa.Sometimes(0.3, iaa.PiecewiseAffine(scale=(0.04, 0.08))),\n",
58+
" iaa.Sometimes(0.3, iaa.PerspectiveTransform(scale=(0.05, 0.1))),\n",
5959
"], random_order=True)\n"
6060
]
6161
},
@@ -93,16 +93,16 @@
9393
" iaa.Noop(),\n",
9494
" iaa.Sequential([\n",
9595
" iaa.OneOf([\n",
96-
" iaa.Add((-30, 30)),\n",
97-
" iaa.AddElementwise((-20, 20)),\n",
98-
" iaa.Multiply((0.9, 1.1)),\n",
99-
" iaa.MultiplyElementwise((0.9, 1.1)),\n",
96+
" iaa.Add((-10, 10)),\n",
97+
" iaa.AddElementwise((-10, 10)),\n",
98+
" iaa.Multiply((0.95, 1.05)),\n",
99+
" iaa.MultiplyElementwise((0.95, 1.05)),\n",
100100
" ]),\n",
101101
" ]),\n",
102102
" iaa.OneOf([\n",
103-
" iaa.GaussianBlur(sigma=(0.0, 2.0)),\n",
104-
" iaa.AverageBlur(k=(2, 7)),\n",
105-
" iaa.MedianBlur(k=(3, 7))\n",
103+
" iaa.GaussianBlur(sigma=(0.0, 1.0)),\n",
104+
" iaa.AverageBlur(k=(2, 5)),\n",
105+
" iaa.MedianBlur(k=(3, 5))\n",
106106
" ])\n",
107107
" ])\n",
108108
"], random_order=False)"

notebooks/data_exploration.ipynb

+1,157-12
Large diffs are not rendered by default.

notebooks/result_exploration.ipynb

+17-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"from src.utils import plot_list, read_images\n",
2222
"from src.metrics import compute_ious, compute_eval_metric\n",
2323
"\n",
24-
"VALIDATION_RESULTS_FILEPATH = '/mnt/ml-team/minerva/open-solutions/salt/kuba/experiments/sal_491_cv_796_lb_783/validation_results.pkl'"
24+
"VALIDATION_RESULTS_FILEPATH = 'YOUR/validation_results.pkl'"
2525
]
2626
},
2727
{
@@ -145,7 +145,7 @@
145145
" results_filtered.append(tup)\n",
146146
" return results_filtered\n",
147147
"\n",
148-
"results_filtered = filter_results(results, iout_range=(0.0,0.1))\n",
148+
"results_filtered = filter_results(results, iout_range=(0.0,0.5))\n",
149149
"len(results_filtered)"
150150
]
151151
},
@@ -166,13 +166,27 @@
166166
},
167167
"outputs": [],
168168
"source": [
169-
"IMG_NR = 56\n",
169+
"IMG_NR = 77\n",
170170
"\n",
171171
"for iou, iout, z, img, gt, pred in results_filtered[:IMG_NR]:\n",
172172
" print('IOU {}, IOUT {}, depth {}'.format(iou, iout, z))\n",
173173
" plot_list(images=[img],labels=[gt, pred])"
174174
]
175175
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": []
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": []
189+
},
176190
{
177191
"cell_type": "code",
178192
"execution_count": null,

src/augmentation.py

+59-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import imgaug as ia
44
from imgaug import augmenters as iaa
55

6-
from .utils import get_crop_pad_sequence
6+
from .utils import get_crop_pad_sequence, reseed
77

88

99
def _perspective_transform_augment_images(self, images, random_state, parents, hooks):
@@ -35,12 +35,12 @@ def _perspective_transform_augment_images(self, images, random_state, parents, h
3535
# General
3636
iaa.SomeOf((1, 2),
3737
[iaa.Fliplr(0.5),
38-
iaa.Affine(rotate=(-20, 20),
39-
translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, mode='symmetric'),
38+
iaa.Affine(rotate=(-10, 10),
39+
translate_percent={"x": (-0.25, 0.25)}, mode='symmetric'),
4040
]),
4141
# Deformations
42-
iaa.Sometimes(0.3, iaa.PiecewiseAffine(scale=(0.02, 0.04))),
43-
iaa.Sometimes(0.3, iaa.PerspectiveTransform(scale=(0.05, 0.10))),
42+
iaa.Sometimes(0.3, iaa.PiecewiseAffine(scale=(0.04, 0.08))),
43+
iaa.Sometimes(0.3, iaa.PerspectiveTransform(scale=(0.05, 0.1))),
4444
], random_order=True)
4545

4646
intensity_seq = iaa.Sequential([
@@ -50,16 +50,16 @@ def _perspective_transform_augment_images(self, images, random_state, parents, h
5050
iaa.Noop(),
5151
iaa.Sequential([
5252
iaa.OneOf([
53-
iaa.Add((-30, 30)),
54-
iaa.AddElementwise((-30, 30)),
55-
iaa.Multiply((0.9, 1.1)),
56-
iaa.MultiplyElementwise((0.9, 1.1)),
53+
iaa.Add((-10, 10)),
54+
iaa.AddElementwise((-10, 10)),
55+
iaa.Multiply((0.95, 1.05)),
56+
iaa.MultiplyElementwise((0.95, 1.05)),
5757
]),
5858
]),
5959
iaa.OneOf([
60-
iaa.GaussianBlur(sigma=(0.0, 2.0)),
61-
iaa.AverageBlur(k=(2, 7)),
62-
iaa.MedianBlur(k=(3, 7))
60+
iaa.GaussianBlur(sigma=(0.0, 1.0)),
61+
iaa.AverageBlur(k=(2, 5)),
62+
iaa.MedianBlur(k=(3, 5))
6363
])
6464
])
6565
], random_order=False)
@@ -126,6 +126,53 @@ def _is_expanded_grey_format(self, img):
126126
return False
127127

128128

129+
def test_time_augmentation_transform(image, tta_parameters):
130+
if tta_parameters['ud_flip']:
131+
image = np.flipud(image)
132+
if tta_parameters['lr_flip']:
133+
image = np.fliplr(image)
134+
if tta_parameters['color_shift']:
135+
random_color_shift = reseed(intensity_seq, deterministic=False)
136+
image = random_color_shift.augment_image(image)
137+
image = rotate(image, tta_parameters['rotation'])
138+
return image
139+
140+
141+
def test_time_augmentation_inverse_transform(image, tta_parameters):
142+
image = per_channel_rotation(image.copy(), -1 * tta_parameters['rotation'])
143+
144+
if tta_parameters['lr_flip']:
145+
image = per_channel_fliplr(image.copy())
146+
if tta_parameters['ud_flip']:
147+
image = per_channel_flipud(image.copy())
148+
return image
149+
150+
151+
def per_channel_flipud(x):
152+
x_ = x.copy()
153+
for i, channel in enumerate(x):
154+
x_[i, :, :] = np.flipud(channel)
155+
return x_
156+
157+
158+
def per_channel_fliplr(x):
159+
x_ = x.copy()
160+
for i, channel in enumerate(x):
161+
x_[i, :, :] = np.fliplr(channel)
162+
return x_
163+
164+
165+
def per_channel_rotation(x, angle):
166+
return rotate(x, angle, axes=(1, 2))
167+
168+
169+
def rotate(image, angle, axes=(0, 1)):
170+
if angle % 90 != 0:
171+
raise Exception('Angle must be a multiple of 90.')
172+
k = angle // 90
173+
return np.rot90(image, k, axes=axes)
174+
175+
129176
class RandomCropFixedSize(iaa.Augmenter):
130177
def __init__(self, px=None, name=None, deterministic=False, random_state=None):
131178
super(RandomCropFixedSize, self).__init__(name=name, deterministic=deterministic, random_state=random_state)

0 commit comments

Comments
 (0)