OpenCV dnn load keras model

asked 2019-10-01 07:35:15 -0500

VCAS gravatar image

updated 2019-10-01 08:18:40 -0500

Hi,

I'm trying to load a model that I trained in Keras with OpenCV Dnn model. I converted the model into .pb and .pbtxt files following this post. However the final model outputs doesn't make any sense. So after browsing other forums I'm still lost/confused about the steps that it is needed to follow to do this conversion. I think that I'm not converting it properly to .pb and .pbtxt files.

So here is my model, it has 5 classes and :

num_classes = 5
epochs = 50

img_x, img_y = 32, 15
input_shape = (img_x, img_y, 3)

X_train, X_test, y_train, y_test = train_test_split(data_x, labels, test_size=0.20, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_val = X_val.astype('float32')
X_train /= 255
X_test /= 255
X_val /= 255

print('x_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),activation='relu',input_shape=input_shape, padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax', name = "output_node"))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

Can someone tell me step buy step how do I properly convert it to the tensorflow files?

edit retag flag offensive close merge delete

Comments

please be so nice and replace the screenshot of your code with a TEXT version, so we can quote you, or even try it - thank you.

berak gravatar imageberak ( 2019-10-01 07:39:33 -0500 )edit
1

Hey! Sorry, I already update it.

VCAS gravatar imageVCAS ( 2019-10-01 08:19:18 -0500 )edit