Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow-替换其他数据集上的MNIST_Python_Tensorflow_Conv Neural Network_Tensorflow Datasets - Fatal编程技术网

Python Tensorflow-替换其他数据集上的MNIST

Python Tensorflow-替换其他数据集上的MNIST,python,tensorflow,conv-neural-network,tensorflow-datasets,Python,Tensorflow,Conv Neural Network,Tensorflow Datasets,我在使用tensorflow默认的Different数据集时遇到问题。 我有使用MNIST数据集识别数字的代码。在这个应用程序中有一个生成的图形,稍后由android应用程序导入。 现在我想识别数字和数学运算符(基本运算符:+,-,*,/) 我找到了生成所需数据的脚本。我有两个。pickle文件 但即使使用适合我的数据集,我仍然不知道如何使用tensorflow将此数据集导入我的应用程序 我将非常感谢您的帮助,或者给我其他(可能更简单)的解决方案 编辑 我对代码做了一些修改,这是加布里埃尔建议

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

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

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

我将非常感谢您的帮助,或者给我其他(可能更简单)的解决方案


编辑

我对代码做了一些修改,这是加布里埃尔建议的

现在我有一个错误:

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

ValueError: too many values to unpack (expected 2)
我找到了我使用的数据集的描述:

  • 从inkml文件中提取跟踪组
  • 将提取的跟踪组转换为图像。图像是只有黑色(值0)和白色(值1)像素的方形位图。黑色表示模式(ROI)
  • 标记这些图像(根据inkml文件)
  • 将图像展平为一维向量
  • 将标签转换为一种热格式
  • 将培训和测试集分别转储到outputs文件夹中
  • 下面是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}))
    

    在代码中,实例化
    tf.Session()
    后,行
    batch=mnist\u data.train.next\u batch(50)
    调用一个内置函数,该函数返回类型为
    (输入,标签)
    的元组。为了向网络提供数据,这里需要定义一些函数返回,即具有输入数据和相关标签的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\u dict
    使用
    x\u batch
    中的值为占位符
    x\u input
    和占位符
    y\u input
    提供
    label\u batch
    。然后在会话中,代码将运行
    train\u步骤
    操作

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

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

    这仍然是个问题吗?