使用CNN进行图像分类时出现过拟合,显然没有使用下一批数据

我正在尝试使用TensorFlow创建一个CNN来对Google关于CNN的教程中的图像进行分类。我创建了一个函数来加载图像数据集,并有一个函数来提取用于训练的批次。但即使我移动到下一批次,网络仍然总是在同一个批次上进行训练。数据集有10000张图像。我认为模型没有使用下一批次的原因是我在不到10次迭代中就达到了100%的准确率。这里是代码:

#Training Parameterslearning_rate = 0.001batch_size = 128epochs = 10MODE = 'TRAIN'# Function that loads the entire dataset of images (X) with their respective labels (Y). X and Y are two np.arraylen_X, X, Y = get_images(    files_path=dataset_path,    img_size_h=1000,    img_size_w=48,    mode='TRAIN',    randomize=True)# Function that load the batch from X and YX_batch, Y_X_batch = next_batch(    total=len_X,    images=X,    labels=Y,    batch_size=batch_size,    index=0)logits = cnn_model_fn(X_batch, MODE)prediction = tf.nn.softmax(logits)loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=Y_X_batch))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)train_op = optimizer.minimize(loss)correct_predict = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y_X_batch, 1))accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))init = tf.global_variables_initializer()best_acc=0with tf.Session() as sess:    sess.run(init)    saver = tf.train.Saver()    if MODE == 'TRAIN':        print("TRAINING MODE")        for step in range(1,epochs+1):            for i in range(0, int(len_X/batch_size)+1):                if i > 0:                    X_batch, Y_X_batch = next_batch(                        total=len_X,                        images=X,                        labels=Y,                        batch_size=batch_size,                        index=i                    )                sess.run(train_op)                los, acc= sess.run([loss, accuracy])                if acc >= best_acc:                    best_acc = acc        writer = tf.summary.FileWriter(TensorBoard_path, sess.graph)    elif MODE=='TEST':        # TEST MODE #sess.close()

这是神经网络模型的结构:

def cnn_model_fn(X, MODE):    # INPUT LAYER    input_layer = tf.reshape(X, [-1, 1000, 48, 1])    # CONVOLUTIONAL LAYER #1    conv1 = tf.layers.conv2d(        inputs=input_layer,        filters=4,        kernel_size=[10, 10],        strides=(2, 2),        padding="valid",    )    conv1_relu = tf.nn.relu(conv1)    # POOLING LAYER #1    pool1 = tf.layers.max_pooling2d(        inputs=conv1_relu,        pool_size=[2, 2],        strides=2    )    # CONVOLUTIONAL LAYER #2    conv2 = tf.layers.conv2d(        inputs=pool1,        filters=64,        kernel_size=[5, 5],        padding="same",    )    conv2_relu = tf.nn.relu(conv2)    # POOLING LAYER #2    pool2 = tf.layers.max_pooling2d(        inputs=conv2_relu,        pool_size=[2, 2],        strides=2    )    x = tf.TensorShape.as_list(pool2.shape)    pool2_flat = tf.reshape(pool2, [-1, x[1] * x[2] * x[3]])    # DENSE LAYER    dense = tf.layers.dense(        inputs=pool2_flat,        units=1024,    )    dense_relu = tf.nn.relu(dense)    # AGGIUNGO L'OPERAZIONE DI DROPOUT    dropout = tf.layers.dropout(        inputs=dense_relu,        rate=0.4,        training=MODE == tf.estimator.ModeKeys.TRAIN    )    # LOGIT LAYER    logits = tf.layers.dense(        inputs=dropout,        units=2    )    return logits

非常感谢您的时间。


回答:

您没有使用TensorFlow的占位符。请确保定义并在损失函数中使用X_batch_placeholder和Y_batch_placeholder,如下所示:

Y_batch_placeholder = tf.placeholder(tf.float32 ,[None, 2] )X_batch_placeholder = tf.placeholder(tf.float32 ,[None, 1000, 48])logits = cnn_model_fn(X_batch_placeholder, MODE)prediction = tf.nn.softmax(logits)loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=Y_batch_placeholder))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)train_op = optimizer.minimize(loss)correct_predict = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y_batch_placeholder, 1))accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))

然后使用feed_dict运行您的会话。有些TensorFlow变量是占位符,您需要在运行会话时使用feed_dict来为它们提供相关信息:

_, los, acc = sess.run([train_op, loss, accuracy],                                       feed_dict={X_batch_placeholder: X_batch, Y_batch_placeholder: Y_batch})

请参考这里: Tensorflow: When should I use or not use `feed_dict`? 和这里: https://hanxiao.github.io/2017/07/07/Get-10x-Speedup-in-Tensorflow-Multi-Task-Learning-using-Python-Multiprocessing/

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注