【发布时间】:2019-04-11 00:21:09
【问题描述】:
首先抱歉,我对这个问题并不准确,但我正在研究 tensorflow-serving 以及如何将我的 cnn 投入生产。真诚的文档让我很困惑。我希望你能帮助更好地理解保存模型架构。所以请以老师的身份回复我,我想了解更多关于整个流程的信息。
我正在开发一个简单的 cnn 来将图像分类为 4 个输出。 我需要 tensorflow-serving 将其投入生产。 输入中的图像可以是 watherver 大小,CNN 应该首先调整它的大小并进行预测。 代码在这里
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot as plt
from scipy.misc import toimage
from keras.models import Sequential
from keras.layers import *
from keras.optimizers import *
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants, signature_def_utils_impl
import cv2
#train_path='Garage/train'
#train_datagen = ImageDataGenerator(rescale=1./255)
#train_batch = train_datagen.flow_from_directory(train_path, target_size=(64,64), class_mode='categorical', batch_size=10, color_mode='grayscale')
#validation_datagen = ImageDataGenerator(rescale=1./255)
#validation_batch = validation_datagen.flow_from_directory(
# './Garage/validation',
# target_size=(64, 64),
# batch_size=3,
# class_mode='categorical', color_mode='grayscale')
model = Sequential()
model.add(InputLayer(input_shape=[64,64,1]))
model.add(Conv2D(filters=32,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))
model.add(Conv2D(filters=50,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))
model.add(Conv2D(filters=80,kernel_size=5,strides=1,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=5,padding='same'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(4,activation='softmax'))
optimizer=Adam(lr=1e-3)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
#model.fit_generator(
# train_batch,
# epochs=50,
# steps_per_epoch=6,
# validation_data=validation_batch,
# validation_steps=5)
model.load_weights('model.h5')
#score = model.evaluate_generator(validation_batch,steps=3)
#print('Test loss:', score[0])
#print('Test accuracy:', score[1])
#model.save('model.h5')
from PIL import Image
import requests
from io import BytesIO
response = requests.get('http://192.168.3.21:7451/shot.jpg')
image_pil = Image.open(BytesIO(response.content))
image = np.asarray(image_pil)
img2 = cv2.resize(image,(64,64))
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
img = np.reshape(img2,[1,64,64,1])
classes = model.predict_classes(img)
print(classes)
model_version="1"
sess = tf.Session()
#setting values for the sake of saving the model in the proper format
x = model.input
y = model.output
prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def({"inputs":x}, {"prediction":y})
valid_prediction_signature = tf.saved_model.signature_def_utils.is_valid_signature(prediction_signature)
if(valid_prediction_signature == False):
raise ValueError("Error: Prediction signature not valid!")
builder = saved_model_builder.SavedModelBuilder('./'+model_version)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
# Add the meta_graph and the variables to the builder
builder.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:prediction_signature, },
legacy_init_op=legacy_init_op)
# save the graph
builder.save()
代码将从摄像头拍摄照片http://192.168.3.21:7451/shot.jpg 然后它会预测它
当我编译代码时,它会在尝试保存模型时返回很多错误。请您检查一下并告诉我保存模型说明是否正确?
我使用 x = model.input 作为服务的输入,但我希望它将图片作为服务器的输入。 实际上我很困惑,对不起。 范围是当我通过 gRPC 请求预测图像时,模型可以给我预测结果 谢谢
【问题讨论】: