1

我遵循了使用 MNIST 数据集创建 CNN 的教程,并且了解其中的大部分内容。然后我尝试将其转换为我自己的具有 RGB 值的自定义图像。但是在代码的某些部分有问题,因为我不完全理解会发生什么以及接下来如何进行。我知道我必须将频道更改为 3,但不知道其余的辅助功能是否正确?当我初始化所有内容时,我也不明白如何训练它。因为batch_x, batch_y = iterator.get_next() 我无法使用feed_dict,并且不怎么训练这个?在 MNIST 数据上可以设置 dropout,但是我现在如何指定呢?据我了解,我没有在真实数据上训练它知道吗?当我创建和测试验证数据时,如何以与 MNIST 数据相同的方式计算结果?

代码如下所示:

import tensorflow as tf
import process_images as image_util
from tensorflow.contrib.data import Dataset, Iterator

# With MNIST
#from tensorflow.examples.tutorials.mnist import input_data
#mnist = input_data.read_data_sets("MNISt_data/", one_hot=True)

filenames_dummy, labels_dummy = image_util.run_it()

#The filenames_dummy and labels_dummy are two lists looking like this, respectively:
#["data/image_1.png", "data/image_2.png", ..., "data/image_n.png"]
# The values of the labels are 0-3, since I have 4 classes. 
#[0, 1, ..., 3]   

filenames = tf.constant(filenames_dummy)
labels = tf.constant(labels_dummy)


def _parse_function(filename, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_png(image_string, channels=3)
  # The image size is 425x425.
  image_resized = tf.image.resize_images(image_decoded, [425,425])
  return image_resized, label

dataset = tf.contrib.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.map(_parse_function)

dataset = dataset.batch(30)
dataset = dataset.repeat()

iterator = dataset.make_one_shot_iterator()

# Helper functions

# INIT weights
def init_weights(shape):
    init_random_dist = tf.truncated_normal(shape, stddev=0.1)
    return(tf.Variable(init_random_dist))

# INIT Bias
def init_bias(shape):
    init_bias_vals = tf.constant(0.1, shape=shape)
    return tf.Variable(init_bias_vals)

# CONV2D
def conv2d(x, W):
    # x --> input tensor [batch, H, W, Channels]
    # W --> [filter H, filter W, Channels IN, Channels OUT] 
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')

# Pooling
def max_pooling_2by2(x):
    # x --> [batch, h, w, c]
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1],    padding='SAME')

#Convolutional layer 

def convolutional_layer(input_x, shape):
    W =init_weights(shape)
    b = init_bias([shape[3]])

    return tf.nn.relu(conv2d(input_x, W)+b)

# Normal (FULLY CONNTCTED)

def normal_full_layer(input_layer, size):
    input_size = int(input_layer.get_shape()[1])
    W = init_weights([input_size, size])
    b = init_bias([size])
    return tf.matmul(input_layer, W) + b

# PLACEHOLDERS

x = tf.placeholder(tf.float32, shape=[None, 180625])
y_true = tf.placeholder(tf.float32, shape=[None, 4])

# With MNIST
#x = tf.placeholder(tf.float32, shape=[None, 784])
#y_true = tf.placeholder(tf.float32, shape=[None, 10])

# Layers
x_image = tf.reshape(x, [-1, 425,425, 1]) 
# With MNIST
#x_image = tf.reshape(x, [-1, 28,28, 1]) 

convo_1 = convolutional_layer(x_image, shape=[5,5,1,32]) 
convo_1_pooling = max_pooling_2by2(convo_1)

convo_2 = convolutional_layer(convo_1_pooling, shape=[5,5,32, 64])
convo_2_pooling = max_pooling_2by2(convo_2)
convo_2_flat = tf.reshape(convo_2_pooling, [-1, 7*7*64])

full_layer_one = tf.nn.relu(normal_full_layer(convo_2_flat, 1024))

# Dropout
hold_prob = tf.placeholder(tf.float32)
full_one_dropout = tf.nn.dropout(full_layer_one, keep_prob=hold_prob)

y_pred = normal_full_layer(full_one_dropout, 4)
# With MNIST
#y_pred = normal_full_layer(full_one_dropout, 10)

# LOSS function
cross_entropy =         
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred))

# Optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train = optimizer.minimize(cross_entropy)

init = tf.global_variables_initializer()

steps = 5000

with tf.Session() as sess:
    sess.run(init)

    for i in range(steps):

        batch_x, batch_y = iterator.get_next()
        test1, test2 = sess.run([batch_x, batch_y])

        # With MNIST
        #sess.run(train, feed_dict={x:batch_x, y_true:batch_y, hold_prob:0.5})

        if i%100 == 0:
            print("ON STEP {}".format(i))
            print("Accuracy: ")
            matches = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))
            accuracy = tf.reduce_mean(tf.cast(matches, tf.float32))

            # With MNIST
            #print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_true:mnist.test.labels, hold_prob:1.0}))
4

0 回答 0