1 year ago

#355805

test-img

Francisco Massucci

OpencvDNN cannot read the .onnx file if there is a GAP layer in the original Keras model

I can't read a Tensorflow Keras model converted to .ONNX, using the opencvDnn module if the original model has a GlobalAveragePooling2D instead of a Flatten layer in the fully connected part.

I'm trying to use a Resnet50 (doesn't work with simpler models either), importing imagenet weights, and instead of a flatten layer I'm using a GlobalAveragePooling2D as described below:

pretrained_Model = applications.ResNet50(include_top=False, weights="imagenet", input_shape=(img_rows, img_cols, img_channel))

add_model = Sequential()
add_model.add(GlobalAveragePooling2D(input_shape=pretrained_Model.output_shape[1:])))
#add_model.add(Flatten(input_shape=pretrained_Model.output_shape[1:]))
add_model.add(Dense(256, activation='relu'))
add_model.add(Dense(1, activation='sigmoid'))

model = Model(inputs=pretrained_Model.input, outputs=add_model(pretrained_Model.output))
model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy']) 

When I convert to .onnx there is no noticeable error, but when trying to read it with cv2.dnn.readNetFromONNX() this is the error that comes up:

opencvDnn Error

tensorflow

opencv

keras

dotnetnuke

onnx

0 Answers

Your Answer

Accepted video resources