我已经修改了tensorflow MNIST示例,用我自己的照片创建二进制分类。 CNN分类器确实训练,但只有在我尝试运行该脚本几次之后。大多数时候,我只是在每一步都获得50%的准确度。
看起来权重初始化并不起作用。或许还有其他问题。
有什么想法吗?
with tf.Graph().as_default():
with tf.Session() as sess:
# Tensorflow placeholders
input = tf.placeholder(
tf.float32, shape=(None, 32, 32, 1))
label = tf.placeholder(
tf.float32, shape=(None, 2))
def train(output, epochs=1000):
loss = tf.reduce_mean(-tf.reduce_sum(label * tf.log(output), reduction_indices=[1]))
correct_samples = tf.reduce_sum(tf.reduce_sum(label * output))
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
sess.run(tf.initialize_all_variables())
for i in range(epochs):
for j in range(int(math.floor(X_train.shape[0] / batch_size))):
batch_input = X_train[j*batch_size: (j*batch_size) + batch_size]
batch_label = y_train[j*batch_size: (j*batch_size) + batch_size]
sess.run(train_step, feed_dict={input: batch_input, label: batch_label})
if i % 200 == 0:
print i, sess.run(correct_samples, feed_dict={input: X_train, label: y_train}) / X_train.shape[0]
def convolution(flow, kernel_size, in_maps, out_maps):
filter_ = tf.Variable(tf.random_normal([1,1,1,1], stddev=0.1))
return tf.nn.conv2d(flow, filter_, strides=[1, 1, 1, 1], padding='VALID')
def pooling(flow, kernel_size):
return tf.nn.max_pool(flow, ksize=[1, kernel_size, kernel_size, 1], strides=[1, kernel_size, kernel_size, 1], padding='SAME')
conv = convolution(input, 5, 32, 32)
conv = tf.nn.relu(conv)
conv = pooling(conv, 2)
conv = convolution(conv, 5, 32, 32)
conv = tf.nn.relu(conv)
conv = pooling(conv, 2)
conv = tf.reshape(conv, [-1, 8*8])
W1 = tf.Variable(tf.truncated_normal(shape=[8*8, 200], stddev=0.1))
b1 = tf.Variable(tf.constant(0.1, shape=[200]))
W2 = tf.Variable(tf.truncated_normal(shape=[200, 2], stddev=0.1))
b2 = tf.Variable(tf.constant(0.1, shape=[2]))
layer1 = tf.matmul(conv, W1) + b1
activation1 = tf.nn.relu(layer1)
logits = tf.matmul(activation1, W2) + b2
output = tf.nn.softmax(logits)
import datetime
print datetime.datetime.now()
train(output, epochs=5000)
print datetime.datetime.now()