卷积神经网络在做图像分类的时候不收敛

发布于 2022-09-05 10:17:45 字数 3573 浏览 30 评论 0

I try to train a CNN model, 2 classes, which is based on tensorflow to do the image classification.

I have tried much modification about epochs, learning rate, batch size and the CNN size, but nothing works.

about data

86(label: 0) + 63(label: 1) images

shape: (128, 128)

about current parameters

learning_rate = 0.00005(I have tried from 0.00000001 to 0.8...)

batch size = 30(I also have tried from 5 to 130)

epoch = 20

about network

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))

about result

The loss is not convergent and also the accuracy.

I don't know if my CNN model is not suitable for my data?
or

The Activate function and loss function of the network is not suitable?

Really thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文