Serving: Keras model to Tensoflow Server Model

Created on 21 Jan 2019  路  8Comments  路  Source: tensorflow/serving

I get an error when accessing

{ "error": "Generic conv implementation does not support grouped convolutions for now.\n\t [[{{node model_1/conv2d_1/Conv2D}}]]" }

I translate the model to TF server

import tensorflow as tf


tf.keras.backend.set_learning_phase(0)   

model = tf.keras.models.load_model(r'model.h5')
export_path = 'my_image_classifier/1'

with tf.keras.backend.get_session() as sess:
    tf.saved_model.simple_save(
        sess,
        export_path,
        inputs={'input_image': model.input},
        outputs={t.name: t for t in model.outputs})

What do i do? I need a model on the server

System Ubuntu 18.04

TF server 1.12(Docker)

Keras 1.2.4

awaiting tensorflower support

Most helpful comment

The problem was that the picture was not translated into black and white.

img = img.resize((w, h), Image.LANCZOS).convert('L')
image_np = np.array(img )
image_np = np.expand_dims(image_np, -1)

All 8 comments

Hi @sonfiree -- can you try with the new Saved Model API for keras? https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/saving/saved_model.py#L46

Hi @sonfiree -- can you try with the new Saved Model API for keras? https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/saving/saved_model.py#L46

Error save

Traceback (most recent call last):
  File "C:/Users/userson/PycharmProjects/Tensor/save_model_keras.py", line 12, in <module>
    tf.contrib.saved_model.save_keras_model(loaded_model, r'R:\model')
  File "C:\Anaconda3\lib\site-packages\tensorflow\contrib\saved_model\python\saved_model\keras_saved_model.py", line 104, in save_keras_model
    checkpoint_path = _export_model_json_and_variables(model, temp_export_dir)
  File "C:\Anaconda3\lib\site-packages\tensorflow\contrib\saved_model\python\saved_model\keras_saved_model.py", line 148, in _export_model_json_and_variables
    model.save_weights(checkpoint_prefix, save_format='tf', overwrite=True)
TypeError: save_weights() got an unexpected keyword argument 'save_format'
from keras.models import model_from_json
import tensorflow as tf

json_file = open(r'ex_model.json', "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights(r'model.21-0.64.h5' )
loaded_model.summary()

tf.contrib.saved_model.save_keras_model(loaded_model, r'R:\model')

Even after modifying the model error

{ "error": "Fused conv implementation does not support grouped convolutions for now.\n\t [[{{node conv2d/BiasAdd}}]]" }

Model

import tensorflow as tf
import os
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten, Input
from tensorflow.keras.layers import Dropout, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.preprocessing import image

input_layer = Input(shape=(192, 64, 1), dtype=tf.float32, name='Input')
x = BatchNormalization()(input_layer)
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = BatchNormalization()(x)
x = Conv2D(64, (3, 3), padding='same', activation='relu')(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
output_layer = Dense(2, activation='softmax')(x)
model = Model(inputs=[input_layer], outputs=[output_layer])
model.summary()

model.compile(loss=tf.keras.losses.binary_crossentropy,
              optimizer=tf.train.AdamOptimizer(0.001),
              metrics=['accuracy'])
history = model.fit_generator(
    train_generator,
    steps_per_epoch = 2,
    epochs = 1,
    validation_data = val_generator,
    validation_steps = 2)

export

import tensorflow as tf

json_file = open(r'ex_model.json', "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = tf.keras.models.model_from_json(loaded_model_json)
loaded_model.load_weights(r"model.h5" )
loaded_model.summary()

I just realized that Conv2D is not supported input_shape 1colors channel?

Here is a good example to export a Keras model to Tensorflow-Serving. Section 4 in this explains exporting a model with tensorflow-serving.

I have problems finding out with a certain model

Not problem

import tensorflow as tf 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Flatten, Conv2D,MaxPooling2D

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(192,64,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

saved_to_path = tf.keras.experimental.export(
        model, '/tensorflow-serving/test/my_simple_tf_keras_saved_model')

Problem

import tensorflow as tf 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Flatten, Conv2D,MaxPooling2D

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(192,64,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

saved_to_path = tf.keras.experimental.export(
        model, '/tensorflow-serving/test/my_simple_tf_keras_saved_model')

Error
When I run a non server model
{ "error": "Fused conv implementation does not support grouped convolutions for now.\n\t [[{{node conv2d/BiasAdd}}]]" }

Conv2D not work with one channel. But work with three channel.

I need a single channel input signal. How to be?
Need
Conv2D(32, (3, 3), input_shape=(192,64,1))

It's bag or feature?

I think part of the problem here is that the input shape to Conv2d should be a 4D tensor-- https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D , which implies that the model architecture is not quite lining up with your input data. For further help debugging, you might try StackOverflow since there is also a larger community that reads questions there and can help with the model architecture.

The problem was that the picture was not translated into black and white.

img = img.resize((w, h), Image.LANCZOS).convert('L')
image_np = np.array(img )
image_np = np.expand_dims(image_np, -1)
Was this page helpful?
0 / 5 - 0 ratings