Python 如何使用CSV作为Tensorflow神经网络的输入数据?
我目前正试图通过稍微改变参数来编写一个神经网络。我有一个如下组织的CSV:Python 如何使用CSV作为Tensorflow神经网络的输入数据?,python,csv,tensorflow,Python,Csv,Tensorflow,我目前正试图通过稍微改变参数来编写一个神经网络。我有一个如下组织的CSV: Image_Name|痣|发育不良痣?|黑色素瘤? asdfgjkgdsl.png | 1 | 0 | 0 一个图像名称,这是一个热门结果。每个图像是1022x767,我想使用每个像素的颜色作为输入。因此,我将MNIST代码更改为具有2351622个输入(1022像素宽*767像素高*3个颜色/像素)和3个输出 # from tensorflow.examples.tutorials.mnist import inpu
Image_Name|痣|发育不良痣?|黑色素瘤?
asdfgjkgdsl.png | 1 | 0 | 0
一个图像名称,这是一个热门结果。每个图像是1022x767,我想使用每个像素的颜色作为输入。因此,我将MNIST代码更改为具有2351622个输入(1022像素宽*767像素高*3个颜色/像素)和3个输出
# from tensorflow.examples.tutorials.mnist import input_data
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
def main():
x = tf.placeholder(tf.float32, [None, 2351622])
W = tf.Variable(tf.zeroes([2351622, 3]))
b = tf.Variable(tf.zeroes([3]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 3])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
example, label = sess.run([features, col5])
# batch_xs, batch_ys = mnist.train.next_batch(100)
# sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
注释行是我必须替换的行,以便将数据加载到神经网络中。为每个图像(我发现的)获取230万个输入的最简单方法是:
如何将此数据集加载到tensorflow中以用于训练神经网络?建议的方法可能是准备一系列
TFU记录
文件。MNIST中有一个这样做的例子。然后,创建一个队列。为了节省空间,最好保持输入为png格式,并在运行时使用decode\u png
简而言之,首先转换(您应该编写多个文件):
然后,读它:(把它放在队列中)
您也可以使用逐行读取
from PIL import Image
import numpy as np
list(np.array(Image.open('asdfgjkgdsl.png')).ravel().flatten())
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def convert():
writer = tf.python_io.TFRecordWriter(output_filename)
for filename, nv, dnv, mn in parse_csv(...):
fs = {}
png_data = read_image_as_np_array(filename)
image_name = 'data/image/png'
fs['png_data'] = _bytes_feature(png_data)
fs['label'] = _bytes_feature([nv, dnv, mn])
example = tf.train.Example(features=tf.train.Features(feature=fs))
writer.write(example.SerializeToString())
writer.close()
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_from_queue)
features_def = {
'png_data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([3], tf.uint8)
}
features = tf.parse_single_example(serialized_example, features=feature_def)
image = tf.image.decode_png(features['png_data'])
...