如何使用RGB图像训练CNN

时间:2017-02-20 00:30:12

标签: machine-learning tensorflow neural-network conv-neural-network

我目前正在建立一个CNN来区分腐烂的苹果和普通的苹果。我觉得如果我能用rgb图像喂CNN会有很大的好处。但是,我究竟需要更改为以下网络?

 x = tf.placeholder('float', [None, 784])
#y = tf.placeholder(tf.float32, shape=(), name="init")
y = tf.placeholder('int32')

keep_rate = 0.8
keep_prob = tf.placeholder(tf.float32)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')

def maxpool2d(x):
    #                        size of window         movement of window
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')



def convolutional_neural_network(x):
    weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,1,32])),
               'W_conv2':tf.Variable(tf.random_normal([5,5,32,64])),
               'W_fc':tf.Variable(tf.random_normal([7*7*64,1024])),
               'out':tf.Variable(tf.random_normal([1024, n_classes]))}

    biases = {'b_conv1':tf.Variable(tf.random_normal([32])),
               'b_conv2':tf.Variable(tf.random_normal([64])),
               'b_fc':tf.Variable(tf.random_normal([1024])),
               'out':tf.Variable(tf.random_normal([n_classes]))}

    x = tf.reshape(x, shape=[-1, 28, 28, 1])

    print("test")
    print(x)
    conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1'])
    conv1 = maxpool2d(conv1)

    conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + biases['b_conv2'])
    conv2 = maxpool2d(conv2)

    fc = tf.reshape(conv2,[-1, 7*7*64])
    fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
    fc = tf.nn.dropout(fc, keep_rate)

    output = tf.matmul(fc, weights['out'])+biases['out']
    return output

我试图改变某些值但是我不断地接到一个错误。该网络目前意味着采用28乘28通道1灰度图像。

1 个答案:

答案 0 :(得分:4)

灰度和RGB图像之间的唯一区别是波段数,分别为1和3。

因此,您的CNN必须将3个频段作为输入,而不是1.其余的将被处理。

无需运行代码,至少需要更改:

weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,3,32]))
x = tf.reshape(x, shape=[-1, 28, 28, 3])