-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
91 lines (80 loc) · 3.93 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def show_image(image, ax=None, title=''):
if ax is None:
plt.figure(figsize=(8,4))
plt.imshow(image[:,:,::-1].astype('uint8'))
plt.title(title)
class CroppingLike2D(Layer):
def __init__(self, target, offset=None, data_format=None,
**kwargs):
"""Crop to target.
If only one `offset` is set, then all dimensions are offset by this amount.
"""
super(CroppingLike2D, self).__init__(**kwargs)
self.data_format = conv_utils.normalize_data_format(data_format)
self.target_shape = K.int_shape(target)
if offset is None or offset == 'centered':
self.offset = 'centered'
elif isinstance(offset, int):
self.offset = (offset, offset)
elif hasattr(offset, '__len__'):
if len(offset) != 2:
raise ValueError('`offset` should have two elements. '
'Found: ' + str(offset))
self.offset = offset
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
if self.data_format == 'channels_first':
return (input_shape[0],
input_shape[1],
self.target_shape[2],
self.target_shape[3])
else:
return (input_shape[0],
self.target_shape[1],
self.target_shape[2],
input_shape[3])
def call(self, inputs):
input_shape = K.int_shape(inputs)
if self.data_format == 'channels_first':
input_height = input_shape[2]
input_width = input_shape[3]
target_height = self.target_shape[2]
target_width = self.target_shape[3]
if target_height > input_height or target_width > input_width:
raise ValueError('The Tensor to be cropped need to be larger '
'or equal to the target Tensor.')
if self.offset == 'centered':
self.offset = [int((input_height - target_height) / 2),
int((input_width - target_width) / 2)]
if self.offset[0] + target_height > input_height:
raise ValueError('Height index out of range: '
+ str(self.offset[0] + target_height))
if self.offset[1] + target_width > input_width:
raise ValueError('Width index out of range:'
+ str(self.offset[1] + target_width))
return inputs[:,
:,
self.offset[0]:self.offset[0] + target_height,
self.offset[1]:self.offset[1] + target_width]
elif self.data_format == 'channels_last':
input_height = input_shape[1]
input_width = input_shape[2]
target_height = self.target_shape[1]
target_width = self.target_shape[2]
if target_height > input_height or target_width > input_width:
raise ValueError('The Tensor to be cropped need to be larger '
'or equal to the target Tensor.')
if self.offset == 'centered':
self.offset = [int((input_height - target_height) / 2),
int((input_width - target_width) / 2)]
if self.offset[0] + target_height > input_height:
raise ValueError('Height index out of range: '
+ str(self.offset[0] + target_height))
if self.offset[1] + target_width > input_width:
raise ValueError('Width index out of range:'
+ str(self.offset[1] + target_width))
output = inputs[:,
self.offset[0]:self.offset[0] + target_height,
self.offset[1]:self.offset[1] + target_width,
:]
return output