神经网络盲猜

时间:2016-07-16 14:22:37

标签: machine-learning neural-network tensorflow

我正在尝试训练简单的神经网络 包括:

  1. 卷积层滤波器(5x5)x 8,步幅2。
  2. 最大合并25x25(图片的细节数量较少)
  3. 将输出展平为(2x2x8)向量
  4. 具有逻辑回归的分类器
  5. 网络共有< 1000个重量。

    文件:nn.py

    #!/bin/python 
    import tensorflow as tf
    import create_batch
    
    # Prepare data
    batch = create_batch.batch
    
    x = tf.reshape(batch[0], [-1,100,100,3])
    y_ = batch[1]
    
    
    # CONVOLUTION NETWORK
    
    # For initialization  
    def weight_variable(shape):
      initial = tf.truncated_normal(shape, stddev=0.3)
      return tf.Variable(initial)
    
    def bias_variable(shape):
      initial = tf.constant(0.2, shape=shape)
      return tf.Variable(initial)
    
    # Convolution with stride 1
    def conv2d(x, W):
      return tf.nn.conv2d(x, W, strides=[1, 2, 2, 1], padding='SAME')
    
    def max_pool_25x25(x):
      return tf.nn.max_pool(x, ksize=[1, 25, 25, 1],
                        strides=[1, 25, 25, 1], padding='SAME')
    
    # First layer
    W_conv1 = weight_variable([5, 5, 3, 8])
    b_conv1 = bias_variable([8])
    
    x_image = tf.reshape(x, [-1,100,100,3])
    
    # First conv1
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_25x25(h_conv1)
    
    
    # Dense connection layer
    # make data flat
    W_fc1 = weight_variable([2 * 2 * 8, 2])
    b_fc1 = bias_variable([2])
    
    h_pool1_flat = tf.reshape(h_pool1, [-1, 2*2*8])
    y_conv = tf.nn.softmax(tf.matmul(h_pool1_flat, W_fc1) + b_fc1)
    
    #Learning
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),    reduction_indices=[1]))
    train_step =    tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    # Session
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    
    # Start input enqueue threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    
    for i in range(200):
      if i%10 == 0:
        train_accuracy = accuracy.eval(session=sess)
        print("step %d, training accuracy %g"%(i, train_accuracy))
    
      train_step.run(session=sess)
    

    文件:create_batch.py​​

    #!/bin/python
    import tensorflow as tf
    
    PATH1 = "../dane/trening/NK/"
    PATH2 = "../dane/trening/K/"
    
    
    def create_labeled_image_list():
    
        filenames = [(PATH1 + "nk_%d.png" % i) for i in range(300)]
        labels = [[1,0] for i in range(300)]
    
        filenames += [(PATH2 + "kulki_%d.png" % i) for i in range(300)]
        labels += [[0,1] for i in range(300)]
    
        return filenames, labels
    
    def read_images_from_disk(input_queue):
        label = input_queue[1]
        file_contents = tf.read_file(input_queue[0])
        example = tf.image.decode_png(file_contents, channels=3)
        example.set_shape([100, 100, 3])
        example = tf.to_float(example)
        print ("READ, label:")
        print(label)
        return example, label
    
    # Start
    image_list, label_list = create_labeled_image_list()
    
    # Create appropriate tensors for naming
    images = tf.convert_to_tensor(image_list, dtype=tf.string)
    labels = tf.convert_to_tensor(label_list, dtype=tf.float32)
    
    input_queue = tf.train.slice_input_producer([images, labels],
                                            shuffle=True)
    
    image, label = read_images_from_disk(input_queue)
    batch = tf.train.batch([image, label], batch_size=600)
    

    我正在喂100x100张图片我每张有两张300张图片。 在步骤0中基本上随机初始化的网络具有比训练的网络更好的准确性。 网络在达到0.5准确度(基本上是硬币翻转)后停止学习。图像包含蓝色斑点物(第1类)或草(第2类)。

    我正在使用整个图像集一次训练网络(600张图像),丢失函数是交叉熵。

    我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,我发现有两个错误,现在网络正在学习。

  1. 尽管我在tf
  2. 中宣称它们是RGB,但图像仍然是RGBA
  3. 我没有将图像标准化为[-1,1] float32
  4. 在tensorflow中,它应该用这样的东西来完成:

    # i use "im" for image
    tf.image.convert_image_dtype(im, dtype=float32)
    im = tf.sub(im, -0.5)
    im = tf.mul(im, 2.0)
    

    对ML的所有新手 - 谨慎准备您的数据!

    感谢。