@@ -81,7 +81,7 @@ class CSPNetBackbone(FeaturePyramidBackbone):
81
81
82
82
# Pretrained backbone
83
83
model = keras_hub.models.CSPNetBackbone.from_preset(
84
- "cspdarknet53_ra_imagenet "
84
+ "csp_darknet_53_ra_imagenet "
85
85
)
86
86
model(input_data)
87
87
@@ -357,18 +357,6 @@ def apply(x):
357
357
dtype = dtype ,
358
358
name = f"{ name } _bottleneck_block_bn_3" ,
359
359
)(x )
360
- if activation == "leaky_relu" :
361
- x = layers .LeakyReLU (
362
- negative_slope = 0.01 ,
363
- dtype = dtype ,
364
- name = f"{ name } _bottleneck_block_activation_3" ,
365
- )(x )
366
- else :
367
- x = layers .Activation (
368
- activation ,
369
- dtype = dtype ,
370
- name = f"{ name } _bottleneck_block_activation_3" ,
371
- )(x )
372
360
373
361
x = layers .add (
374
362
[x , shortcut ], dtype = dtype , name = f"{ name } _bottleneck_block_add"
@@ -673,6 +661,13 @@ def apply(x):
673
661
name = f"{ name } _csp_activation_1" ,
674
662
)(x )
675
663
else :
664
+ if strides > 1 :
665
+ x = layers .ZeroPadding2D (
666
+ 1 ,
667
+ data_format = data_format ,
668
+ dtype = dtype ,
669
+ name = f"{ name } _csp_conv_pad_1" ,
670
+ )(x )
676
671
x = layers .Conv2D (
677
672
filters = down_chs ,
678
673
kernel_size = 3 ,
@@ -882,6 +877,13 @@ def apply(x):
882
877
name = f"{ name } _cs3_activation_1" ,
883
878
)(x )
884
879
else :
880
+ if strides > 1 :
881
+ x = layers .ZeroPadding2D (
882
+ 1 ,
883
+ data_format = data_format ,
884
+ dtype = dtype ,
885
+ name = f"{ name } _cs3_conv_pad_1" ,
886
+ )(x )
885
887
x = layers .Conv2D (
886
888
filters = down_chs ,
887
889
kernel_size = 3 ,
@@ -1062,6 +1064,13 @@ def apply(x):
1062
1064
name = f"{ name } _dark_activation_1" ,
1063
1065
)(x )
1064
1066
else :
1067
+ if strides > 1 :
1068
+ x = layers .ZeroPadding2D (
1069
+ 1 ,
1070
+ data_format = data_format ,
1071
+ dtype = dtype ,
1072
+ name = f"{ name } _dark_conv_pad_1" ,
1073
+ )(x )
1065
1074
x = layers .Conv2D (
1066
1075
filters = filters ,
1067
1076
kernel_size = 3 ,
@@ -1091,18 +1100,18 @@ def apply(x):
1091
1100
dtype = dtype ,
1092
1101
name = f"{ name } _dark_activation_1" ,
1093
1102
)(x )
1094
- for i in range (depth ):
1095
- x = block_fn (
1096
- filters = block_channels ,
1097
- dilation = dilation ,
1098
- bottle_ratio = bottle_ratio ,
1099
- groups = groups ,
1100
- activation = activation ,
1101
- data_format = data_format ,
1102
- channel_axis = channel_axis ,
1103
- dtype = dtype ,
1104
- name = f"{ name } _block_{ i } " ,
1105
- )(x )
1103
+ for i in range (depth ):
1104
+ x = block_fn (
1105
+ filters = block_channels ,
1106
+ dilation = dilation ,
1107
+ bottle_ratio = bottle_ratio ,
1108
+ groups = groups ,
1109
+ activation = activation ,
1110
+ data_format = data_format ,
1111
+ channel_axis = channel_axis ,
1112
+ dtype = dtype ,
1113
+ name = f"{ name } _block_{ i } " ,
1114
+ )(x )
1106
1115
return x
1107
1116
1108
1117
return apply
@@ -1135,6 +1144,13 @@ def apply(x):
1135
1144
or (i == last_idx and strides > 2 and not pooling )
1136
1145
else 1
1137
1146
)
1147
+ if conv_strides > 1 :
1148
+ x = layers .ZeroPadding2D (
1149
+ (kernel_size - 1 ) // 2 ,
1150
+ data_format = data_format ,
1151
+ dtype = dtype ,
1152
+ name = f"csp_stem_pad_{ i } " ,
1153
+ )(x )
1138
1154
x = layers .Conv2D (
1139
1155
filters = chs ,
1140
1156
kernel_size = kernel_size ,
@@ -1167,10 +1183,19 @@ def apply(x):
1167
1183
1168
1184
if pooling == "max" :
1169
1185
assert strides > 2
1186
+ # Use manual padding to handle edge case scenario to ignore zero's
1187
+ # as max value instead consider negative values from Leaky Relu type
1188
+ # of activations.
1189
+ pad_width = [[1 , 1 ], [1 , 1 ]]
1190
+ if data_format == "channels_last" :
1191
+ pad_width += [[0 , 0 ]]
1192
+ else :
1193
+ pad_width = [[0 , 0 ]] + pad_width
1194
+ pad_width = [[0 , 0 ]] + pad_width
1195
+ x = ops .pad (x , pad_width = pad_width , constant_values = float ("-inf" ))
1170
1196
x = layers .MaxPooling2D (
1171
1197
pool_size = 3 ,
1172
1198
strides = 2 ,
1173
- padding = "same" ,
1174
1199
data_format = data_format ,
1175
1200
dtype = dtype ,
1176
1201
name = "csp_stem_pool" ,
0 commit comments