【发布时间】:2018-01-15 15:26:00
【问题描述】:
简介
我有一个基于 tensorflow 的普通 CNN 网络,我的目标是训练它,然后用它来将图像分类为 2 类。
关于训练数据集
X:图片(健康,不健康),128*128
标签:[1, 0](不健康)或 [0, 1](健康)
我使用 TFrecords 来制作数据集。
关于 CNN 模型
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1, dtype = tf.float32)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape = shape, dtype = tf.float32)
return tf.Variable(initial)
def conv2d(x, W):
#(input, filter, strides, padding)
#[batch, height, width, in_channels]
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
#(value, ksize, strides, padding)
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def cnn_model():
epochs = 1
batch_size = 200
learning_rate = 0.001
hidden = 1024
cap_c = 498
cap_h = 478
num = cap_c + cap_h # the sum number of the training x
image_size = 128
label_size = 2
ex = 2
#train_loss = np.empty((num//(batch_size * ex)) * epochs)
#train_acc = np.empty((num//(batch_size * ex)) * epochs)
x = tf.placeholder(tf.float32, shape = [None, image_size * image_size])
y = tf.placeholder(tf.float32, shape = [None, label_size])
X_train_ = tf.reshape(x, [-1, image_size, image_size, 1])
#First layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(X_train_, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#Second layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#Third layer
#W_conv3 = weight_variable([5, 5, 64, 128])
#b_conv3 = bias_variable([128])
#h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
#h_pool3 = max_pool_2x2(h_conv3)
#Full connect layer
W_fc1 = weight_variable([64 * 64 * 32, hidden])
b_fc1 = bias_variable([hidden])
h_pool2_flat = tf.reshape(h_pool2, [-1, 64 * 64 * 32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#Output_Softmax
W_fc2 = weight_variable([hidden, label_size])
b_fc2 = bias_variable([label_size])
y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = y_conv))
optimize = tf.train.AdamOptimizer(learning_rate).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
然后是数据读取或会话部分。
关于形状
作为占位符的形状,如果batch size为200
X 形状:[200, 128 * 128]
标签形状:[200, 2]
输出形状:[200, 2]
关于输出结果
我认为预测值应该被训练为[1, 0]或者[0, 1],但是大约5步之后,预测值都是[1, 0]或者[0, 1]。例如,如果批量大小为 5,则结果将是
[[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 0]]
或完全相反。 但是,有时结果会有所不同,像这样
[[1, 0],
[0, 1],
[1, 0],
[0, 1],
[1, 0]]
但这只会持续大约 5 个步骤,然后结果将是一样的。
关于损失和准确率
由于预测结果不正确,所以损失不收敛。换句话说,损失和准确率完全取决于训练数据集的X,这是没有意义的。
我的想法
我认为数据集TFrecords没有问题,因为我已经打印了图像矩阵和标签,它们都很好。所以我认为问题出在模型上。
我没有得到可以从 Google 搜索和 SO 中的其他问题中解决我的问题和问题的答案,如果你能帮我解决这个问题,真的非常感谢。如果您需要更多结果或参考代码,请告诉我。
【问题讨论】:
标签: python image tensorflow neural-network classification