【问题标题】:Tensorflow - replace MNIST on other datasetTensorflow - 在其他数据集上替换 MNIST
【发布时间】:2018-06-26 08:33:16
【问题描述】:

我在使用不同的数据集时遇到问题,然后默认使用 tensorflow。 我有使用 MNIST 数据集识别数字的代码。在此应用程序中生成了图形,稍后由 android 应用程序导入。 现在我想识别数字和数学运算符(基本的一个:+、-、*、/)。

我找到了生成所需数据的脚本。我有两个 .pickle 文件。

但即使有适合我的数据集,我仍然不知道如何使用 tensorflow 将此数据集导入我的应用程序。

我会很感激这方面的帮助,或者给我其他(也许更容易)的解决方案。


编辑

我根据 gabriele 的建议对代码进行了一些更改。

现在我有错误:

(x, label) = train_pickle_reader('train.pickle')

ValueError: too many values to unpack (expected 2)

我找到了我使用的数据集的描述:

  1. 从 inkml 文件中提取跟踪组。
  2. 将提取的跟踪组转换为图像。图像是只有黑色(值 0)和白色(值 1)像素的方形位图。黑色表示图案 (ROI)。
  3. 标记这些图像(根据 inkml 文件)。
  4. 将图像扁平化为一维向量。
  5. 将标签转换为 one-hot 格式。
  6. 将训练集和测试集分别转储到输出文件夹中。

下面是python中的代码:

import tensorflow as tf
import pickle

def train_pickle_reader(filename):
    with open(filename, 'rb') as f:
        x = pickle.load(f)
    # assuming x is already of the form (all_train_input, all_train_labels):
    return x

def test_pickle_reader(filename):
    with open(filename, 'rb') as f:
        x = pickle.load(f)
    # assuming x is already of the form (all_train_input, all_train_labels):
    return x

# Function to create a weight neuron using a random number. Training will assign a real weight later
def weight_variable(shape, name):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, name=name)


# Function to create a bias neuron. Bias of 0.1 will help to prevent any 1 neuron from being chosen too often
def biases_variable(shape, name):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial, name=name)


# Function to create a convolutional neuron. Convolutes input from 4d to 2d. This helps streamline inputs
def conv_2d(x, W, name):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME', name=name)


# Function to create a neuron to represent the max input. Helps to make the best prediction for what comes next
def max_pool(x, name):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)


# A way to input images (as 784 element arrays of pixel values 0 - 1)
x_input = tf.placeholder(dtype=tf.float32, shape=[None, 784], name='x_input')
# A way to input labels to show model what the correct answer is during training
y_input = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y_input')

# First convolutional layer - reshape/resize images
# A weight variable that examines batches of 5x5 pixels, returns 32 features (1 feature per bit value in 32 bit float)
W_conv1 = weight_variable([5, 5, 1, 32], 'W_conv1')
# Bias variable to add to each of the 32 features
b_conv1 = biases_variable([32], 'b_conv1')
# Reshape each input image into a 28 x 28 x 1 pixel matrix
x_image = tf.reshape(x_input, [-1, 28, 28, 1], name='x_image')
# Flattens filter (W_conv1) to [5 * 5 * 1, 32], multiplies by [None, 28, 28, 1] to associate each 5x5 batch with the
# 32 features, and adds biases
h_conv1 = tf.nn.relu(conv_2d(x_image, W_conv1, name='conv1') + b_conv1, name='h_conv1')
# Takes windows of size 2x2 and computes a reduction on the output of h_conv1 (computes max, used for better prediction)
# Images are reduced to size 14 x 14 for analysis
h_pool1 = max_pool(h_conv1, name='h_pool1')

# Second convolutional layer, reshape/resize images
# Does mostly the same as above but converts each 32 unit output tensor from layer 1 to a 64 feature tensor
W_conv2 = weight_variable([5, 5, 32, 64], 'W_conv2')
b_conv2 = biases_variable([64], 'b_conv2')
h_conv2 = tf.nn.relu(conv_2d(h_pool1, W_conv2, name='conv2') + b_conv2, name='h_conv2')
# Images at this point are reduced to size 7 x 7 for analysis
h_pool2 = max_pool(h_conv2, name='h_pool2')

# First dense layer, performing calculation based on previous layer output
# Each image is 7 x 7 at the end of the previous section and outputs 64 features, we want 32 x 32 neurons = 1024
W_dense1 = weight_variable([7 * 7 * 64, 1024], name='W_dense1')
# bias variable added to each output feature
b_dense1 = biases_variable([1024], name='b_dense1')
# Flatten each of the images into size [None, 7 x 7 x 64]
h_pool_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64], name='h_pool_flat')
# Multiply weights by the outputs of the flatten neuron and add biases
h_dense1 = tf.nn.relu(tf.matmul(h_pool_flat, W_dense1, name='matmul_dense1') + b_dense1, name='h_dense1')

# Dropout layer prevents overfitting or recognizing patterns where none exist
# Depending on what value we enter into keep_prob, it will apply or not apply dropout layer
keep_prob = tf.placeholder(dtype=tf.float32, name='keep_prob')
# Dropout layer will be applied during training but not testing or predicting
h_drop1 = tf.nn.dropout(h_dense1, keep_prob, name='h_drop1')

# Readout layer used to format output
# Weight variable takes inputs from each of the 1024 neurons from before and outputs an array of 10 elements
W_readout1 = weight_variable([1024, 10], name='W_readout1')
# Apply bias to each of the 10 outputs
b_readout1 = biases_variable([10], name='b_readout1')
# Perform final calculation by multiplying each of the neurons from dropout layer by weights and adding biases
y_readout1 = tf.add(tf.matmul(h_drop1, W_readout1, name='matmul_readout1'), b_readout1, name='y_readout1')

# Softmax cross entropy loss function compares expected answers (labels) vs actual answers (logits)
cross_entropy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_input, logits=y_readout1))
# Adam optimizer aims to minimize loss
train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy_loss)
# Compare actual vs expected outputs to see if highest number is at the same index, true if they match and false if not
correct_prediction = tf.equal(tf.argmax(y_input, 1), tf.argmax(y_readout1, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# Used to save the graph and weights
saver = tf.train.Saver()

# Run in with statement so session only exists within it
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    # Save the graph shape and node names to pbtxt file
    tf.train.write_graph(sess.graph_def, '.', 'advanced_mnist.pbtxt', False)

    (x, label) = train_pickle_reader('train.pickle')

    batch_size = 64 # the batch size you want to use
    num_batches = len(x)//batch_size

    # Train the model, running through data 20000 times in batches of 50
    # Print out step # and accuracy every 100 steps and final accuracy at the end of training
    # Train by running train_step and apply dropout by setting keep_prob to 0.5
    for i in range(20000):
       for j in range(num_batches):
           x_batch = x[j * batch_size: (j + 1) * batch_size]
           label_batch = label[j * batch_size: (j + 1)*batch_size]
           train_step.run(feed_dict={x_input: x_batch, y_input: label_batch, keep_prob: 0.5})

    # Save the session with graph shape and node weights
    saver.save(sess, 'advanced_mnist.ckpt')

    # Make a prediction
    (x, labels) = test_pickle_reader('test.pickle')
    print(sess.run(y_readout1, feed_dict={x_input: x, keep_prob: 1.0}))

【问题讨论】:

  • 这还有问题吗?

标签: python tensorflow conv-neural-network tensorflow-datasets


【解决方案1】:

在您的代码中,在实例化 tf.Session() 之后,batch = mnist_data.train.next_batch(50) 行调用了一个内置函数,该函数返回一个类型为 (input, label) 的元组。为了向网络提供您的数据,您需要在此处定义一些返回函数,即具有输入数据和相关标签的 numpy 数组。例如,假设您有一个包含训练数据的 pickle 文件,您的代码应如下所示:

def pikle_reader(filename):
    with open(filename, 'r') as f:
        x = pickle.load(f)
    # assuming x is already of the form (all_train_input, all_train_labels):
    return x

[...]

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    [...]
    # get your data:
    (x, label) = pikle_reader(filename) 

    batch_size = 64 # the batch size you want to use
    num_batches = len(x)//batch_size

    for i in range(20000):  # number of epochs
        for j in range(num_batches):
            x_batch = x[j*batch_size: (j+1)*batch_size] 
            label_batch = label[j* batch_size: (j+1)batch_size] 
            train_step.run(feed_dict={x_input: x_batch, y_input: label_batch, keep_prob: 0.5})

在这里,feed_dictx_batch 中的值提供给占位符x_input,将label_batch 提供给占位符y_input。然后在会话中代码将运行train_step 操作。

相反,当您要进行预测时,代码基本相同:

(x, label) = pikle_reader(test_data_filename)
print(sess.run(y_readout1, feed_dict={x_input: x, keep_prob: 1.0}))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多