【问题标题】:CNN model on image classification is not convergent, which is based on Tensorflow图像分类上的CNN模型不收敛,基于Tensorflow
【发布时间】:2018-01-26 05:51:40
【问题描述】:

我尝试训练一个 CNN 模型,2 个类,它基于 tensorflow 来做图像分类。

我尝试了很多关于时期、学习率、批量大小和 CNN 大小的修改,但没有任何效果。

关于数据

86(标签:0)+ 63(标签:1)图像

形状:(128, 128)

关于当前参数

learning_rate = 0.00005(我试过从 0.00000001 到 0.8...)

batch size = 30(我也试过从5到130)

纪元 = 20

关于网络

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 = 20
    batch_size = 30
    learning_rate = 0.00005
    hidden = 2
    cap_c = 86
    cap_h = 63
    num = cap_c + cap_h
    image_size = 128
    label_size = 2

    print ((num//(batch_size)) * epochs)
    train_loss = np.empty((num//(batch_size)) * epochs)
    train_acc = np.empty((num//(batch_size)) * epochs)

    x = tf.placeholder(tf.float32, shape = [None, image_size, image_size])
    y = tf.placeholder(tf.float32, shape = [None, label_size])

    weight_balance = tf.constant([0.1])

    X_train_ = tf.reshape(x, [-1, image_size, image_size, 1])

    #First layer
    W_conv1 = weight_variable([5, 5, 1, 4])
    b_conv1 = bias_variable([4])

    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, 4, 8])
#    b_conv2 = bias_variable([8])
#    
#    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, 8, 16])
#    b_conv3 = bias_variable([16])
#    
#    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 * 4, hidden])
    b_fc1 = bias_variable([hidden])

    h_pool2_flat = tf.reshape(h_pool1, [-1, 64 * 64 * 4])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    #Output_Softmax

    W_fc2 = weight_variable([hidden, label_size])
    b_fc2 = bias_variable([label_size])

    y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    print y_conv.shape



    #Train
    loss = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(y, y_conv, weight_balance))
    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))

关于结果

loss不收敛,准确率也不高。

不知道是不是我的CNN模型不适合我的数据? 或

网络的Activate函数和损失函数不合适?

真的谢谢你

【问题讨论】:

  • 你尝试过非加权版本的损失吗?
  • 另外,您将 softmax 应用于您的输出,然后您的损失函数再次应用它。不要那样做。将未激活的输出输入损失函数,并仅将 softmax 应用于预测。
  • @MadWombat 感谢您的帮助。你的意思是当我将输出输入损失函数时我应该使用out_put = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2)?然后我仍然对我的损失函数使用tf.nn.softmax_cross_entropy_with_logits?谢谢
  • 是的,这将是一个好的开始。您可以使用pred = tf.nn.softmax(out_put) 并使用它来生成您的实际预测。
  • @MadWombat 是的,我已经修改了它。但结果仍然不收敛。你能帮我看看我的网络,如果我有一些不适合我的数据集的东西或者我有其他问题吗?

标签: image tensorflow classification conv-neural-network convergence


【解决方案1】:

代码有几个问题:

  1. 您在最后一层应用softmax,然后调用tf.nn.weighted_cross_entropy_with_logits,后者又应用sigmoid 激活,因此您应用了两次激活。
  2. 对于权重的初始化,使用XavierVariance_scaling 以加快收敛速度​​。最好在实现模型时使用tf.layers API,因为它的默认设置遵循最佳实践。

【讨论】:

  • 感谢您的回答 :-) 我已更改:loss = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(y, out_feed, weight_balance))optimize = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)。但是现在的问题是损失收敛很好但是准确率波动剧烈......
猜你喜欢
  • 1970-01-01
  • 2018-08-03
  • 2022-12-03
  • 2020-10-09
  • 2016-04-19
  • 2017-12-22
  • 1970-01-01
  • 2019-09-22
  • 2017-05-14
相关资源
最近更新 更多