Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy cnn file

I have trained a model using this code...

https://github.com/shantanuo/pandas_examples/blob/master/tensorflow/simages_train_waiting.ipynb

My file is ready, but how do I deploy it?

https://s3.ap-south-1.amazonaws.com/studentimages162a/cnn.h5

I tried to use hosted solution panini.ai but it does not accept h5 files. I tried to convert it to csv but that did not work. I also tried to use flask

https://github.com/mtobeiyf/keras-flask-deploy-webapp

I got this error while trying to run the docker container...

# docker run -v /tmp/:/tmp/ -p 5000:5000 keras_flask_app
Using TensorFlow backend.
Traceback (most recent call last):
  File "app.py", line 26, in <module>
    model = load_model(MODEL_PATH)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/saving.py", line 221, in _deserialize_model
    model_config = f['model_config']
  File "/usr/local/lib/python2.7/site-packages/keras/utils/io_utils.py", line 302, in __getitem__
    raise ValueError('Cannot create group in read only mode.')
ValueError: Cannot create group in read only mode.

In other words how to use cnn.h5 file?


I am trying to use this code...

from keras.models import Sequential
from keras.layers import Dense, Activation

def build_model():
    model = Sequential()

    model.add(Dense(output_dim=64, input_dim=100))
    model.add(Activation("relu"))
    model.add(Dense(output_dim=10))
    model.add(Activation("softmax"))
    model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    return model

model2 = build_model()
model2.load_weights('cnn.h5')

And got the error:

ValueError: You are trying to load a weight file containing 4 layers into a model with 2 layers.
like image 815
shantanuo Avatar asked May 14 '19 06:05

shantanuo


1 Answers

Concerning the first error, I the problem is that the flask app tries to load the complete model (i.e. with configuration):

 model = load_model(MODEL_PATH)

whereas after the training you save only weights:

cnn.save_weights('cnn.h5')

Try to use cnn.save('cnn.h5') instead.

In the second case, your model definition does not match the trained model. Indeed, it is a completely different model with no Convolution layers at all. The corresponding model definition would be:

def build_model():
    model = Sequential()

    model.add(Conv2D(filters=32, 
           kernel_size=(2,2), 
           strides=(1,1),
           padding='same',
           input_shape=(IMG_SIZE,IMG_SIZE,NB_CHANNELS),
           data_format='channels_last'))


    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2),
                           strides=2))

    model.add(Dropout(0.4))

    model.add(Conv2D(filters=64,
                     kernel_size=(2,2),
                     strides=(1,1),
                     padding='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2),
                           strides=2))

    model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    return model
like image 133
Dmytro Prylipko Avatar answered Sep 20 '22 05:09

Dmytro Prylipko