Skip to content

Commit 5dd8c5c

Browse files
committed
Padding style fixes.
1 parent 169c089 commit 5dd8c5c

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

keras/layers/convolutional.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,14 +1412,15 @@ class ZeroPadding1D(Layer):
14121412
'''Zero-padding layer for 1D input (e.g. temporal sequence).
14131413
14141414
# Arguments
1415-
padding: int or tuple of int (length 2) or dictionary
1416-
For symmetric padding: int
1415+
padding: int, or tuple of int (length 2), or dictionary.
1416+
- If int:
14171417
How many zeros to add at the beginning and end of
14181418
the padding dimension (axis 1).
1419-
For asymmetric padding: tuple of int (length 2)
1419+
- If tuple of int (length 2)
14201420
How many zeros to add at the beginning and at the end of
1421-
the padding dimension '(left_pad, right_pad)' or
1422-
'{'left_pad': left_pad, 'right_pad': right_pad}'.
1421+
the padding dimension, in order '(left_pad, right_pad)'.
1422+
- If dictionary: should contain the keys
1423+
{'left_pad', 'right_pad'}.
14231424
If any key is missing, default value of 0 will be used for the missing key.
14241425
14251426
# Input shape
@@ -1436,15 +1437,21 @@ def __init__(self, padding=1, **kwargs):
14361437
if isinstance(padding, int):
14371438
self.left_pad = padding
14381439
self.right_pad = padding
1440+
14391441
elif isinstance(padding, dict):
14401442
if set(padding.keys()) <= {'left_pad', 'right_pad'}:
14411443
self.left_pad = padding.get('left_pad', 0)
14421444
self.right_pad = padding.get('right_pad', 0)
14431445
else:
1444-
raise ValueError('Unexpected key is found in the padding argument. '
1445-
'Keys have to be in {"left_pad", "right_pad"}')
1446+
raise ValueError('Unexpected key found in `padding` dictionary. '
1447+
'Keys have to be in {"left_pad", "right_pad"}. '
1448+
'Found: ' + str(padding.keys()))
14461449
else:
14471450
padding = tuple(padding)
1451+
if len(padding) != 2:
1452+
raise ValueError('`padding` should be int, or dict with keys '
1453+
'{"left_pad", "right_pad"}, or tuple of length 2. '
1454+
'Found: ' + str(padding))
14481455
self.left_pad = padding[0]
14491456
self.right_pad = padding[1]
14501457
self.input_spec = [InputSpec(ndim=3)]
@@ -1468,15 +1475,16 @@ class ZeroPadding2D(Layer):
14681475
'''Zero-padding layer for 2D input (e.g. picture).
14691476
14701477
# Arguments
1471-
padding: tuple of int (length 2) or tuple of int (length 4) or dictionary
1472-
For symmetric padding tuple of int (length 2)
1478+
padding: tuple of int (length 2), or tuple of int (length 4), or dictionary.
1479+
- If tuple of int (length 2):
14731480
How many zeros to add at the beginning and end of
14741481
the 2 padding dimensions (rows and cols).
1475-
For asymmetric padding tuple of int (length 4)
1482+
- If tuple of int (length 4):
14761483
How many zeros to add at the beginning and at the end of
1477-
the 2 padding dimensions (rows and cols).
1478-
'(top_pad, bottom_pad, left_pad, right_pad)' or
1479-
'{'top_pad': top_pad, 'bottom_pad': bottom_pad, 'left_pad': left_pad, 'right_pad': right_pad}'
1484+
the 2 padding dimensions (rows and cols), in the order
1485+
'(top_pad, bottom_pad, left_pad, right_pad)'.
1486+
- If dictionary: should contain the keys
1487+
{'top_pad', 'bottom_pad', 'left_pad', 'right_pad'}.
14801488
If any key is missing, default value of 0 will be used for the missing key.
14811489
dim_ordering: 'th' or 'tf'.
14821490
In 'th' mode, the channels dimension (the depth)
@@ -1507,16 +1515,18 @@ def __init__(self,
15071515
dim_ordering = K.image_dim_ordering()
15081516

15091517
self.padding = padding
1510-
try:
1518+
if isinstance(padding, dict):
15111519
if set(padding.keys()) <= {'top_pad', 'bottom_pad', 'left_pad', 'right_pad'}:
15121520
self.top_pad = padding.get('top_pad', 0)
15131521
self.bottom_pad = padding.get('bottom_pad', 0)
15141522
self.left_pad = padding.get('left_pad', 0)
15151523
self.right_pad = padding.get('right_pad', 0)
15161524
else:
1517-
raise ValueError('Unexpected key is found in the padding argument. '
1518-
'Keys have to be in {"top_pad", "bottom_pad", "left_pad", "right_pad"}')
1519-
except AttributeError:
1525+
raise ValueError('Unexpected key found in `padding` dictionary. '
1526+
'Keys have to be in {"top_pad", "bottom_pad", '
1527+
'"left_pad", "right_pad"}.'
1528+
'Found: ' + str(padding.keys()))
1529+
else:
15201530
padding = tuple(padding)
15211531
if len(padding) == 2:
15221532
self.top_pad = padding[0]
@@ -1529,9 +1539,11 @@ def __init__(self,
15291539
self.left_pad = padding[2]
15301540
self.right_pad = padding[3]
15311541
else:
1532-
raise TypeError('padding should be tuple of int of length 2 or 4, or dict')
1542+
raise TypeError('`padding` should be tuple of int '
1543+
'of length 2 or 4, or dict. '
1544+
'Found: ' + str(padding))
15331545

1534-
assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'
1546+
assert dim_ordering in {'tf', 'th'}, '`dim_ordering` must be in {"tf", "th"}.'
15351547
self.dim_ordering = dim_ordering
15361548
self.input_spec = [InputSpec(ndim=4)]
15371549

@@ -1574,6 +1586,7 @@ class ZeroPadding3D(Layer):
15741586
padding: tuple of int (length 3)
15751587
How many zeros to add at the beginning and end of
15761588
the 3 padding dimensions (axis 3, 4 and 5).
1589+
Currentl only symmetric padding is supported.
15771590
dim_ordering: 'th' or 'tf'.
15781591
In 'th' mode, the channels dimension (the depth)
15791592
is at index 1, in 'tf' mode is it at index 4.

tests/keras/layers/test_convolutional.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def test_averagepooling_3d():
381381
def test_zero_padding_1d():
382382
nb_samples = 2
383383
input_dim = 2
384-
nb_steps = 11
384+
nb_steps = 5
385385
input = np.ones((nb_samples, nb_steps, input_dim))
386386

387387
# basic test
@@ -420,8 +420,8 @@ def test_zero_padding_1d():
420420
def test_zero_padding_2d():
421421
nb_samples = 2
422422
stack_size = 2
423-
input_nb_row = 11
424-
input_nb_col = 12
423+
input_nb_row = 4
424+
input_nb_col = 5
425425
dim_ordering = K.image_dim_ordering()
426426
assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'
427427

@@ -487,9 +487,9 @@ def test_zero_padding_2d():
487487
def test_zero_padding_3d():
488488
nb_samples = 2
489489
stack_size = 2
490-
input_len_dim1 = 10
491-
input_len_dim2 = 11
492-
input_len_dim3 = 12
490+
input_len_dim1 = 4
491+
input_len_dim2 = 5
492+
input_len_dim3 = 3
493493

494494
input = np.ones((nb_samples,
495495
input_len_dim1, input_len_dim2, input_len_dim3,
@@ -608,7 +608,7 @@ def test_upsampling_3d():
608608
@keras_test
609609
def test_cropping_1d():
610610
nb_samples = 2
611-
time_length = 10
611+
time_length = 4
612612
input_len_dim1 = 2
613613
input = np.random.rand(nb_samples, time_length, input_len_dim1)
614614

0 commit comments

Comments
 (0)