python - Tensorflow:调整不同大小的图像列表
问题描述
我创建了一个L
包含不同大小图像的图像列表:
L =
(1, 333, 500, 3)
(1, 500, 333, 3)
(1, 257, 296, 3)
这些图像的数据类型是np.uint8
.
我的目标是将这些图像传递给一个函数process_images()
,该函数将图像重塑为预定义的大小size = [100,100]
并返回以下列表
L_new =
(1, 100, 100, 3)
(1, 100, 100, 3)
(1, 100, 100, 3)
或大小为 numpy 的数组[3,100,100,3]
。
处理图像的函数定义如下:
def process_images(X):
X = tf.convert_to_tensor(X, dtype=tf.uint8)
X = tf.image.resize_images(X, size=[100,100])
return X
到目前为止,如果我打电话,我会得到一个错误img=sess.run(process_images(L))
:
TypeError: Expected uint8, got array([[[[ 0, 91, 127],
[ 17, 94, 122],
[ 39, 90, 85],
...,
[ 67, 128, 87],
[ 71, 129, 88],
[ 71, 130, 86]]]], dtype=uint8) of type 'ndarray' instead.'
我做错了什么?
解决方案
我改变了答案,因为我错过了问题描述中的一些要点。以下是正确答案:
def process_image(x):
x = tf.convert_to_tensor(x, dtype=tf.uint8)
return tf.image.resize_images(x, size=[100,100])
def process_list(X):
L = [process_image(x) for x in X]
return tf.stack(L, axis=0)
import matplotlib.image as mpimg
imgs=[mpimg.imread('nadine.jpg'), mpimg.imread('andy.jpg')]
shapes= [img.shape for img in imgs]
with tf.Session() as sess:
imgs1=sess.run(process_list(imgs))
print(shapes)
print(imgs1.shape)
这是初始数组的形状和最终结果
[(1077, 1280, 3), (2946, 3930, 3)]
(2, 100, 100, 3)
关键是您不能将不同形状的数组列表转换为张量 by tf.convert_to_tensor
。您需要对每个数组逐一进行转换,然后将生成的张量堆叠在一起。
推荐阅读
- javascript - javascript node.js 在这种情况下如何避免 While(true)?
- entity-framework - 如何更快地从 Linq 中的行中获取计数
- json - 有没有办法编辑 SharePoint 列表标题?
- javascript - Videojs和clappr livestream总是在livestream上打开,如何禁用?
- styled-components - React:通过样式组件将 box-sizing 应用于所有元素
- docker - Visual Studio Code 远程容器和 Go GOPATH
- laravel - 通过分页页面持久化查询数据
- file - 如何将 Google Sheets API 响应存储到文件
- java - android.os.BadParcelableException ClassNotFoundException 时解组:内部类
- javafx - 使用 Jfoenix 9.0.9 在 JavaFX 中开发的正确设置