@@ -96,3 +96,99 @@ def parse_zeropadding2d_layer(keras_layer, input_names, input_shapes, data_reade
9696 layer ['in_width' ] = input_shapes [0 ][2 ]
9797
9898 return layer , output_shape
99+
100+
101+ @keras_handler ('Cropping1D' )
102+ def parse_cropping1d_layer (keras_layer , input_names , input_shapes , data_reader ):
103+ assert keras_layer ['class_name' ] == 'Cropping1D'
104+
105+ layer = parse_default_keras_layer (keras_layer , input_names )
106+
107+ cropping = keras_layer ['config' ]['cropping' ]
108+ if isinstance (cropping , int ):
109+ layer ['crop_left' ] = cropping
110+ layer ['crop_right' ] = cropping
111+ elif isinstance (cropping , collections .abc .Sequence ):
112+ layer ['crop_left' ] = cropping [0 ]
113+ layer ['crop_right' ] = cropping [1 ]
114+
115+ # No data_format attribute for Cropping1D (always cl), but keeping it consistent with Cropping2D
116+ if layer ['data_format' ] == 'channels_first' :
117+ output_shape = [
118+ input_shapes [0 ][0 ], # Batch
119+ input_shapes [0 ][1 ], # Channels
120+ input_shapes [0 ][2 ] - layer ['crop_left' ] - layer ['crop_right' ], # Width
121+ ]
122+ layer ['out_width' ] = output_shape [2 ]
123+ layer ['n_chan' ] = output_shape [1 ]
124+
125+ layer ['in_width' ] = input_shapes [0 ][2 ]
126+ else :
127+ output_shape = [
128+ input_shapes [0 ][0 ], # Batch
129+ input_shapes [0 ][1 ] - layer ['crop_left' ] - layer ['crop_right' ], # Width
130+ input_shapes [0 ][2 ], # Channels
131+ ]
132+ layer ['out_width' ] = output_shape [1 ]
133+ layer ['n_chan' ] = output_shape [2 ]
134+
135+ layer ['in_width' ] = input_shapes [0 ][1 ]
136+
137+ return layer , output_shape
138+
139+
140+ @keras_handler ('Cropping2D' )
141+ def parse_cropping2d_layer (keras_layer , input_names , input_shapes , data_reader ):
142+ assert keras_layer ['class_name' ] == 'Cropping2D'
143+
144+ layer = parse_default_keras_layer (keras_layer , input_names )
145+
146+ cropping = keras_layer ['config' ]['cropping' ]
147+ if isinstance (cropping , int ):
148+ layer ['crop_top' ] = cropping
149+ layer ['crop_bottom' ] = cropping
150+ layer ['crop_left' ] = cropping
151+ layer ['crop_right' ] = cropping
152+ elif isinstance (cropping , collections .abc .Sequence ):
153+ height_crop , width_crop = cropping
154+ if isinstance (height_crop , collections .abc .Sequence ):
155+ layer ['crop_top' ] = height_crop [0 ]
156+ layer ['crop_bottom' ] = height_crop [1 ]
157+ else :
158+ layer ['crop_top' ] = height_crop
159+ layer ['crop_bottom' ] = height_crop
160+ if isinstance (width_crop , collections .abc .Sequence ):
161+ layer ['crop_left' ] = width_crop [0 ]
162+ layer ['crop_right' ] = width_crop [1 ]
163+ else :
164+ layer ['crop_left' ] = width_crop
165+ layer ['crop_right' ] = width_crop
166+
167+ if layer ['data_format' ] == 'channels_first' :
168+ output_shape = [
169+ input_shapes [0 ][0 ], # Batch
170+ input_shapes [0 ][1 ], # Channels
171+ input_shapes [0 ][2 ] - layer ['crop_top' ] - layer ['crop_bottom' ], # Height
172+ input_shapes [0 ][3 ] - layer ['crop_left' ] - layer ['crop_right' ], # Width
173+ ]
174+ layer ['out_height' ] = output_shape [2 ]
175+ layer ['out_width' ] = output_shape [3 ]
176+ layer ['n_chan' ] = output_shape [1 ]
177+
178+ layer ['in_height' ] = input_shapes [0 ][2 ]
179+ layer ['in_width' ] = input_shapes [0 ][3 ]
180+ else :
181+ output_shape = [
182+ input_shapes [0 ][0 ], # Batch
183+ input_shapes [0 ][1 ] - layer ['crop_top' ] - layer ['crop_bottom' ], # Height
184+ input_shapes [0 ][2 ] - layer ['crop_left' ] - layer ['crop_right' ], # Width
185+ input_shapes [0 ][3 ], # Channels
186+ ]
187+ layer ['out_height' ] = output_shape [1 ]
188+ layer ['out_width' ] = output_shape [2 ]
189+ layer ['n_chan' ] = output_shape [3 ]
190+
191+ layer ['in_height' ] = input_shapes [0 ][1 ]
192+ layer ['in_width' ] = input_shapes [0 ][2 ]
193+
194+ return layer , output_shape
0 commit comments