I recently created a custom neural network with the following code for basic architecture:
def gen_base_model(n_class):
cnn_model = InceptionResNetV2(include_top=False, input_shape=(width, width, 3), weights='imagenet')
inputs = Input((width, width, 3))
x = inputs
x = Lambda(preprocess_input, name='preprocessing')(x)
x = cnn_model(x)
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(n_class, activation='softmax', name='softmax')(x)
model = Model(inputs, x)
return model
I've trained the model and now I want to deploy the model on Cloud ML Engine / AI Platform.
I used the following code to convert and save the keras model as a Saved Model :
def convert_and_save(model, path):
full_path = './savedmodels/' + path
signature = tf.saved_model.signature_def_utils.predict_signature_def(
inputs={'image': model.input}, outputs={'scores': model.output})
builder = tf.saved_model.builder.SavedModelBuilder(full_path)
builder.add_meta_graph_and_variables(
sess=K.get_session(),
tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature
})
builder.save()
However, now I am having trouble creating an instance in order to predict the outputs. The Google cloud website specifies the following format :
{"instances": [<instance>, <instance>, ...]}
I have tried using the following code in order to convert the image :
width = 299
image_pre = cv2.resize(cv2.imread('image.jpg'), (width, width))
img_pred = np.expand_dims(image_pre, axis=0)
print(img_pred.shape)
img_pred_1 = img_pred.tolist()
instances = {'image': img_pred_1}
But it failed with the following error :
RuntimeError: Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,1,299,299,3]
[[{{node inception_resnet_v2/conv2d_1/convolution}}]]")
Also, I use the following code (keras) for predictions when using the model locally :
width = 299
image_pre = cv2.resize(cv2.imread('image.jpg'), (width, width))
img_pred = np.expand_dims(image_pre, axis=0)
prediction = model.predict(img_pred)
ind = np.argmax(prediction[0])
predicted_class = model_classes[ind]