Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV dnn load keras model

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 : model

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

OpenCV dnn load keras model

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 : model

:

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?

OpenCV dnn load keras model

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 5
epochs = 50

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

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

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

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

'test samples') y_train = keras.utils.to_categorical(y_train, num_classes) = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes)

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

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

metrics=['accuracy'])

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