tensorflow - 如何使张量具有四个维度?
问题描述
以下代码:
def decode_img(img):
# convert the compressed string to a 3D uint8 tensor
img = tf.image.decode_jpeg(img, channels=3)
# Use `convert_image_dtype` to convert to floats in the [0,1] range.
img = tf.image.convert_image_dtype(img, tf.float32)
# resize the image to the desired size.
return tf.image.resize(img, [200, 200])
def process_path(file_path):
#label = get_label(file_path)
# load the raw data from the file as a string
img = tf.io.read_file(file_path)
img = decode_img(img)
return img
model.predict(process_path('data/train/nonfood/0_808.jpg'))
给出以下错误
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (200, 200, 3)
我希望我需要将图像格式化为大小
(1,200,200,3)
但是进行格式化的正确语法是什么?
解决方案
您需要模拟 batch_size 索引,因为在 Keras 和 TensorFlow 中,您只能对批次进行预测。
您可以使用np.expand_dims(photo,axis=0)
或 tf.expand_dims(photo, axis=0)
翻译成您的情况,这意味着在您的decode_img
, return tf.expand_dims(img,0
)
推荐阅读
- bash - 用另一个文件头替换文件头
- javascript - 基本偶数或奇数问题。完成下面的isLastEvenOdd函数
- python - Firestore 云侦听器,不适用于集合内的集合
- r - 根据 PCA 中的名称对行进行子集化
- local - 尝试在本地加载时出现 google-font 问题
- python - 需要从列表中的列表中添加多个值(IndexError:列表索引超出范围)
- python-3.x - Python(New)为什么这个函数不打印分配给它的新变量?
- c++ - 模板类友元函数的内部类
- excel - 我无法使用按字母排序的公式?
- azure-service-fabric - 使用 VS 2019 针对远程本地集群调试 Service Fabric 应用程序