如何将 python 中的列表转换为 numpy 数组以放入张量流
How to convert a list in python to a numpy array to put into tensor flow
我正在尝试为 this dataset 创建一个深度学习模型。因为我使用的是台式机,所以我只使用了第一个训练集中的 3,000 张图像,这些图像位于外部驱动器的文件夹中。我正在使用以下 python 代码从文件夹中获取图像列表:
from PIL import Image
import glob
image_list = []
for filename in glob.glob('/Volumes/G-DRIVE mobile USB-C/traan/*.jpeg'): #assuming jpeg
im=Image.open(filename)
image_list.append(im)
print(image_list)
列表打印时如下所示:
[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3888x2592 at 0x1087337D0>, <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3888x2592 at 0x108733850>, ...
如何获取此列表并将其转换为可以放入张量流模型的形式,也许就像 this tutorial.
中的那样
您首先需要将列表转换为 numpy
数组,
import numpy as np
images_list = np.stack(image_list) # assuming all the images have similar shape
# i.e. (height, width, 3), images_list has now
# shape (num_images, height, width, channel)
然后您可以使用此张量或此张量的一部分来使用占位符训练您的模型
images = tf.placeholder(tf.float32, [None, height, width, channel])
...
sess.run(train_op, feed_dict={images:images_list})
我正在尝试为 this dataset 创建一个深度学习模型。因为我使用的是台式机,所以我只使用了第一个训练集中的 3,000 张图像,这些图像位于外部驱动器的文件夹中。我正在使用以下 python 代码从文件夹中获取图像列表:
from PIL import Image
import glob
image_list = []
for filename in glob.glob('/Volumes/G-DRIVE mobile USB-C/traan/*.jpeg'): #assuming jpeg
im=Image.open(filename)
image_list.append(im)
print(image_list)
列表打印时如下所示:
[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3888x2592 at 0x1087337D0>, <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3888x2592 at 0x108733850>, ...
如何获取此列表并将其转换为可以放入张量流模型的形式,也许就像 this tutorial.
中的那样您首先需要将列表转换为 numpy
数组,
import numpy as np
images_list = np.stack(image_list) # assuming all the images have similar shape
# i.e. (height, width, 3), images_list has now
# shape (num_images, height, width, channel)
然后您可以使用此张量或此张量的一部分来使用占位符训练您的模型
images = tf.placeholder(tf.float32, [None, height, width, channel])
...
sess.run(train_op, feed_dict={images:images_list})