Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow 2.0:在保存的模型中添加图像预处理步骤_Python_Tensorflow_Keras_Tensorflow Serving_Tensorflow2.0 - Fatal编程技术网

Python Tensorflow 2.0:在保存的模型中添加图像预处理步骤

Python Tensorflow 2.0:在保存的模型中添加图像预处理步骤,python,tensorflow,keras,tensorflow-serving,tensorflow2.0,Python,Tensorflow,Keras,Tensorflow Serving,Tensorflow2.0,我是一个新手,TF和GCP部署。因此,提前非常感谢您的帮助 目前,我正在尝试使用TensorFlow服务在Google云平台(GCP)上部署我的Mnist应用程序。我已经在TF服务上部署了我的模型,并使用一个自定义的MySimpleScaler类在将图像输入到我的模型之前对其进行预处理和调整大小。我的问题是,是否有办法在我的保存模型中添加预处理和调整类大小,以便我的flask应用程序没有任何tensorflow依赖项。原因是TF库对于应用程序引擎来说太大了 我的应用程序流程如下: 1) 我的烧瓶

我是一个新手,TF和GCP部署。因此,提前非常感谢您的帮助

目前,我正在尝试使用TensorFlow服务在Google云平台(GCP)上部署我的Mnist应用程序。我已经在TF服务上部署了我的模型,并使用一个自定义的MySimpleScaler类在将图像输入到我的模型之前对其进行预处理和调整大小。我的问题是,是否有办法在我的保存模型中添加预处理和调整类大小,以便我的flask应用程序没有任何tensorflow依赖项。原因是TF库对于应用程序引擎来说太大了

我的应用程序流程如下:

1) 我的烧瓶应用程序部署在应用程序引擎上。它有一个MySimpleScaler类来调整画布中输入的图像大小。我允许用户从前端的画布上绘制-->使用jquery-->获取数据,使用parse_image函数将数据写入output.jpg-->从本地驱动器读取output.jpg,并将其提供给MySimpleScaler进行预处理

2) 我的模型是使用TF服务部署在AI平台上。在步骤1中,我使用MysimpleScaler的输出发送预测请求。然后将预测值推送到Flask后端,然后使用Jinja将其推送到前端

以下是我用来获取和预处理数据的两个函数:

def parse_image(imgData):
    # imgData fetch img from canvas using request.get_data()
    imgstr = re.search(b"base64,(.*)", imgData).group(1)
    img_decode = base64.decodebytes(imgstr)
    with open("output.jpg", "wb") as file:
        file.write(img_decode)
    return img_decode
TL;DR:在将其部署到TF服务之前,我想在我的保存模型中添加preprocess_img函数作为层之一。
非常感谢您抽出时间

如果您对
batch_size=1
没有意见,那么在图中添加预处理函数应该是直截了当的,下面是我的做法

代码:

输出:

TensorFlow: 2.0.0
WARNING:tensorflow:From /tensorflow-2.0.0/python3.6/tensorflow_core/python/ops/resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
INFO:tensorflow:Assets written to: export/1/assets

Model Input Shape: (1,)
[('n02123045', 'tabby', 0.5762127), ('n02123159', 'tiger_cat', 0.24783427), ('n02124075', 'Egyptian_cat', 0.09435685)]
PS:如果您想扩展它以支持
批量大小>1

非常感谢您的帮助!我还有一个关于tf.keras.Input()的愚蠢问题。为什么使用tf.expand_dims()来读取图像,而不是直接将其传递给tf.io.read_file()来获取字符串字节?tf.keras.Input()是否需要特定的形状?我尝试了无扩展dims输入,但它产生了以下错误:
形状必须是秩0,但对于输入形状为[1]的“DecodeJpeg”(op:“DecodeJpeg”)来说是秩1。
keras模型需要一个
batch
轴,这迫使您使用
batch
轴发送图像字节。但是
tf.io.read\u文件
不支持批处理。所以你需要相应地重塑你的张量!
import tensorflow as tf
import numpy as np

print('TensorFlow:',tf.__version__)

def preprocess_single_image(image_bytes, h=299, w=299):
    image = tf.image.decode_jpeg(image_bytes[0], channels=3)
    image = tf.image.resize(image, size=[h, w])
    image = (image - 127.5) / 127.5
    image = tf.expand_dims(image, axis=0)
    return image

image_bytes = tf.keras.Input(shape=[], batch_size=1, name='image_bytes', dtype=tf.string)
preprocessed_image = preprocess_single_image(image_bytes)
model = tf.keras.applications.Xception(weights='imagenet')
predictions = model(preprocessed_image)
new_model = tf.keras.Model(image_bytes, predictions)
new_model.save('export/1', save_format='tf')
print('Model Input Shape:', new_model.input_shape)

### !wget -q -O "cat.jpg" "https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg?cs=srgb&dl=adorable-animal-blur-cat-617278.jpg&fm=jpg"
loaded_model = tf.saved_model.load('export/1')
cat_bytes = tf.expand_dims(tf.io.read_file('cat.jpg'), axis=0)
preds = loaded_model(cat_bytes).numpy()
print(tf.keras.applications.xception.decode_predictions(preds, top=3)[0])
TensorFlow: 2.0.0
WARNING:tensorflow:From /tensorflow-2.0.0/python3.6/tensorflow_core/python/ops/resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
INFO:tensorflow:Assets written to: export/1/assets

Model Input Shape: (1,)
[('n02123045', 'tabby', 0.5762127), ('n02123159', 'tiger_cat', 0.24783427), ('n02124075', 'Egyptian_cat', 0.09435685)]