5 python machine-learning deep-learning tensorflow
我正在Mac OS上安装python 3.6。我有存储图像名称和每个图像上的类编号的文本文件。
#label.txt:
img0001.jpg 1
img0002.jpg 3
img0003.jpg 5
img0004.jpg 10
img0005.jpg 6
img0006.jpg 8
img0007.jpg 10
.....
Run Code Online (Sandbox Code Playgroud)
我想给他们我的神经网络tensorflow作为输入数据的标签,并给图像网络中同时这样的
xs = tf.placeholder(tf.float32,[None,#size of my photo])
ys = tf.placeholder(tf.float32,[None,#size of my label if it is an array])
Run Code Online (Sandbox Code Playgroud)
我找不到任何相关文档。有人可以高兴地告诉我该怎么办吗?
您可以直接使用 python i/o 实用程序(https://docs.python.org/3/tutorial/inputoutput.html),例如:
names = []
labels = []
with open('label.txt', 'r') as f:
for line in f.readlines():
tokens = line.split(' ')
names.append(tokens[0])
labels.append(int(tokens[1]))
Run Code Online (Sandbox Code Playgroud)
然后您可以使用名称数组加载图像和标签作为您的 y 数组。
假设您想知道,如何将图像及其各自的标签输入神经网络。
有两件事:
正如Thomas Pinetz 所说,一旦你计算了名称和标签。创建一种标签的热编码。
from PIL import Image
number_of_batches = len(names)/ batch_size
for i in range(number_of_batches):
batch_x = names[i*batch_size:i*batch_size+batch_size]
batch_y = labels[i*batch_size:i*batch_size+batch_size]
batch_image_data = np.empty([batch_size, image_height, image_width, image_depth], dtype=np.int)
for ix in range(len(batch_x)):
f = batch_x[ix]
batch_image_data[ix] = np.array(Image.open(data_dir+f))
sess.run(train_op, feed_dict={xs:batch_image_data, ys:batch_y})
Run Code Online (Sandbox Code Playgroud)