How to save keras model in WML repository in Watson Studio?

Viewed 1245

I am trying to deploy Keras model by training on MNIST dataset on Watson studio but unable to save and successfully deploy it.

When I am trying to save the model object, it says it can't save Sequential Object. When I am trying to convert hd5 to tgz and save it, it gets saved but on deployment I get error

"{"code":"load_model_failure","message":"SavedModel file does not exist at: /opt/ibm/s..."

When I am trying to deploy hd5 file, it says its not in compressed format.

Can any help me how exactly to save keras model and deploy it on watson studio?

# 

convert class vectors to binary class matrices
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=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

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

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

model_result_path = "keras_model.h5"
model.save(model_result_path)

published_model = client.repository.store_model(model='keras_model.h5', meta_props=model_props,training_data=x_train, training_target=y_train)
4 Answers

Step 1: Save your model to .h5 file.

model_result_path = "keras_model.h5"
model.save(model_result_path)

Step 2: compress .h5 file to tgz.

!tar -zcvf keras_model.tgz keras_model.h5

Step 3: Important > For deploying a keras model, it is mandatory to pass the FRAMEWORK_LIBRARIES along with other metaprops. store_model WML documentation.

metadata = {
client.repository.ModelMetaNames.NAME: 'Image-classifier',
client.repository.ModelMetaNames.FRAMEWORK_NAME: 'tensorflow',
client.repository.ModelMetaNames.FRAMEWORK_VERSION: '1.5',
client.repository.ModelMetaNames.FRAMEWORK_LIBRARIES:[{'name':'keras', 'version': '2.1.3'}]
}

Step 4: Store the model.

published_model = client.repository.store_model(model= 'keras_model.tgz', meta_props=metadata, training_data= X_train,training_target= y_train)

step 5: Deploy the model.

model_id = published_model["metadata"]["guid"]

model_deployment_details = client.deployments.create(artifact_uid=model_id, name="deployment_name" )

can you try compressing the h5 file(i.e forming a tar.gz) and then try giving it to the client.repository.store_model instead of directly giving .h5 file.

You will have to provide the path to the compressed keras file. E.g.:

keras_file_path = "/Users/jsmith/keras/ker_seq_mnist_model.tar.gz"
published_model = client.repository.store_model(model=keras_file_path, meta_props=model_props,training_data=x_train, training_target=y_train)
Related