Skip to content

Commit 17baf26

Browse files
authored
Upsample (#902)
* update sampling layers * upadte zoom * fix bug zoom * typo
1 parent f2db115 commit 17baf26

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

tensorlayer/prepro.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random=
14511451

14521452

14531453
# zoom
1454-
def zoom(x, zoom_range=(0.9, 1.1), row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1):
1454+
def zoom(x, zoom_range=(0.9, 1.1), flags=None, border_mode='constant'):
14551455
"""Zooming/Scaling a single image that height and width are changed together.
14561456
14571457
Parameters
@@ -1462,14 +1462,9 @@ def zoom(x, zoom_range=(0.9, 1.1), row_index=0, col_index=1, channel_index=2, fi
14621462
The zooming/scaling ratio, greater than 1 means larger.
14631463
- float, a fixed ratio.
14641464
- tuple of 2 floats, randomly sample a value as the ratio between 2 values.
1465-
row_index col_index and channel_index : int
1466-
Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
1467-
fill_mode : str
1468-
Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
1469-
cval : float
1470-
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0.
1471-
order : int
1472-
The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
1465+
border_mode : str
1466+
- `constant`, pad the image with a constant value (i.e. black or 0)
1467+
- `replicate`, the row or column at the very edge of the original is replicated to the extra border.
14731468
14741469
Returns
14751470
-------
@@ -1478,9 +1473,9 @@ def zoom(x, zoom_range=(0.9, 1.1), row_index=0, col_index=1, channel_index=2, fi
14781473
14791474
"""
14801475
zoom_matrix = affine_zoom_matrix(zoom_range=zoom_range)
1481-
h, w = x.shape[row_index], x.shape[col_index]
1476+
h, w = x.shape[0], x.shape[1]
14821477
transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
1483-
x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)
1478+
x = affine_transform_cv2(x, transform_matrix, flags=flags, border_mode=border_mode)
14841479
return x
14851480

14861481

@@ -1524,10 +1519,7 @@ def respective_zoom(
15241519
return x
15251520

15261521

1527-
def zoom_multi(
1528-
x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest',
1529-
cval=0., order=1
1530-
):
1522+
def zoom_multi(x, zoom_range=(0.9, 1.1), flags=None, border_mode='constant'):
15311523
"""Zoom in and out of images with the same arguments, randomly or non-randomly.
15321524
Usually be used for image segmentation which x=[X, Y], X and Y should be matched.
15331525
@@ -1544,28 +1536,14 @@ def zoom_multi(
15441536
A list of processed images.
15451537
15461538
"""
1547-
if len(zoom_range) != 2:
1548-
raise Exception('zoom_range should be a tuple or list of two floats. ' 'Received arg: ', zoom_range)
15491539

1550-
if is_random:
1551-
if zoom_range[0] == 1 and zoom_range[1] == 1:
1552-
zx, zy = 1, 1
1553-
tl.logging.info(" random_zoom : not zoom in/out")
1554-
else:
1555-
zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2)
1556-
else:
1557-
zx, zy = zoom_range
1558-
1559-
zoom_matrix = np.array([[zx, 0, 0], [0, zy, 0], [0, 0, 1]])
1560-
1561-
h, w = x[0].shape[row_index], x[0].shape[col_index]
1562-
transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
1563-
# x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval)
1564-
# return x
1540+
zoom_matrix = affine_zoom_matrix(zoom_range=zoom_range)
15651541
results = []
1566-
for data in x:
1567-
results.append(affine_transform(data, transform_matrix, channel_index, fill_mode, cval, order))
1568-
return np.asarray(results)
1542+
for img in x:
1543+
h, w = x.shape[0], x.shape[1]
1544+
transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
1545+
results.append(affine_transform_cv2(x, transform_matrix, flags=flags, border_mode=border_mode))
1546+
return result
15691547

15701548

15711549
# image = tf.image.random_brightness(image, max_delta=32. / 255.)

0 commit comments

Comments
 (0)