【发布时间】:2020-08-21 15:23:09
【问题描述】:
我正在尝试使用带有 Tensorflow 的 CNN 对 3D 图像进行分类,但我认为有问题。在训练和验证之后,我使用保存的模型对单个图像进行分类。当我对这张图片进行分类时,每次尝试都会得到不同的结果。
例子:
1st try:
input: Picture of a 3D brain
result of the classification: 1
2nd try:
input: Same picture of 3D brain
result of the classification: 2
我想知道这是正常的还是我做错了什么。我使用的模型的准确率为 67%。
代码如下:
def conv3d(x, W):
return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='SAME')
def maxpool3d(x):
return tf.nn.max_pool3d(x, ksize=[1, 2, 2, 2, 1], strides=[1, 2, 2, 2, 1], padding='SAME')
def convolutional_neural_network(x):
number = calc()
weights = {'W_conv1': tf.Variable(tf.random_normal([3, 3, 3, 1, 32])),
'W_conv2': tf.Variable(tf.random_normal([3, 3, 3, 32, 64])),
'W_fc': tf.Variable(tf.random_normal([number, 1024])),
'out': tf.Variable(tf.random_normal([1024, 3]))}
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([3]))}
x = tf.reshape(x, shape=[-1, 50, 50, 30, 1])
conv1 = tf.nn.relu(conv3d(x, weights['W_conv1']) + biases['b_conv1'])
conv1 = maxpool3d(conv1)
conv2 = tf.nn.relu(conv3d(conv1, weights['W_conv2']) + biases['b_conv2'])
conv2 = maxpool3d(conv2)
fc = tf.reshape(conv2, [-1, number])
fc = tf.nn.relu(tf.matmul(fc, weights['W_fc']) + biases['b_fc'])
fc = tf.nn.dropout(fc, 0.8)
output = tf.matmul(fc, weights['out']) + biases['out']
return output
这是主要功能:
def classification(path):
x = tf.placeholder('float')
new_path = extract(path + '.gz')
X_new = process_data(path=new_path, apply_hist=True)
pred = convolutional_neural_network(x)
res = 0
with tf.Session() as sess:
saver = tf.train.import_meta_graph('modelo.meta')
saver.restore(sess, 'modelo')
sess.run(tf.initialize_all_variables())
probabilities = tf.nn.softmax(pred)
c = sess.run(probabilities, feed_dict={x: X_new})
res = np.argmax(c)
return res
我正在对输入图像进行相同的处理。
非常感谢!
编辑:
我尝试交换这些行,但结果仍然不同。
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver = tf.train.import_meta_graph('modelo.meta')
saver.restore(sess, 'modelo')
【问题讨论】:
标签: python tensorflow neural-network classification conv-neural-network