2016-11-13 208 views
1

我目前正在尝试通过稍微更改MNIST for ML Beginners code来编写神经网络。我的影片举办这样一个CSV:如何使用CSV作为Tensorflow神经网络的输入数据?

Image_Name |Nevus? |Dysplastic Nevus?| Melanoma? asdfgjkgdsl.png |1 |0 |0

图像名,这是一个炎热的结果。每个图像都是1022 x 767,我也想用每个像素的颜色作为输入。因此,我将MNIST代码更改为2,351,622个输入(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})) 

注释行是我必须替换才能将我的数据加载到神经网络中的行。以获得每个图像(我发现)的2.3M输入的最简单方法是:

from PIL import Image 
import numpy as np 

list(np.array(Image.open('asdfgjkgdsl.png')).ravel().flatten()) 

如何我可以加载这个数据集到tensorflow用于训练神经网络?

回答

1

可能推荐的方法是准备一系列tf_records文件。 MNIST有这样一个例子。然后,你创建一个队列。为节省空间,最好将输入保持为png格式,并在运行时使用decode_png

总之,第一转换(你应该写多个文件):

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']) 
... 

您还可以使用tf.TextLineReader阅读一行一行地代替。