我在一个文件夹中有一个来自this dataset的35,000多张图像的数据集。如何将这些图像转换成train_images
的python数组,可以输入张量流深度学习模型?
答案 0 :(得分:1)
使用numpy,PIL或opencv加载数据,并使用占位符将其提供给网络。这意味着您的数据足够小以适合内存。示例代码如下
import glob
import cv2
import numpy as np
import tensorflow as tf
data = []
for i in glob.glob('path/to/my/data/**/*.png', recursive=True):
data.append(cv2.imread(i))
data = np.stack(data) # array of shape [num_images, height, width, channel]
def get_batch(data, batch_size):
data_size = data.shape[0]
indexes = list(range(data_size))
np.random.shuffle(indexes)
for i in range(0, data_size, batch_size):
yield data[indexes[i:i+batch_size]]
images = tf.placeholder(tf.float32, [None, height, width, channel])
my_net = build_network(images)
...
for epoch in range(max_epochs):
for batch_images in get_batch(data, batch_size):
sess.run(train_op, feed_dict={images: batch_images})
您应该根据数据创建TF记录,并使用TensorFlow的排队机制和数据集API代替占位符。
答案 1 :(得分:0)
要从您的数据集文件夹中获取所有文件/图像名称,请执行以下操作
import os
# train_images list of name of files or images in data set folder
train_images = list()
image_path = ' path to the data set (image) folder '
for image in os.walk(image_path):
train_images.append(image[2])
# os.walk('path') traverse recursively so used index 2 to give file name in same folder only
trian_images
是必需的数组,您可以将其传递/馈送到张量流。
遵循@Olivier Moindrot的here 解决方案,并将train_images传递到文件名,并根据需要标记数据。