使用类似于Lenet5的架构对’101物体类别’数据进行分类时,获得了比预期更高的准确率

我正在尝试构建一个类似于Lenet5的模型,并在Caltech 101数据集上进行训练。这个基准模型的预期准确率应低于60%。但我的模型达到了90+%的准确率,我怀疑对于这个数据集而言,Lenet5是不可能达到这个准确率的。以下代码片段展示了数据的读取方式以及我的模型定义,接下来是我的实验结果。

我使用了tf.data来加载图像,具体方法如Tensorflow教程所示。

# 获得测试集(10%)和训练集大小(90%)test_size = round(0.1 * image_count)train_size = image_count-test_size# 取前10%的数据作为测试数据test_data = labeled_ds.take(test_size)# 跳过前10%的数据,并将剩余的90%数据作为训练数据train_data = labeled_ds.skip(test_size)# 定义np数组来存储图像和标签(将传递给模型)train_images = np.empty((train_size,64,64,3), dtype=np.float32)train_labels = np.empty((train_size,101,), dtype=np.bool_)test_images = np.empty((test_size,64,64,3), dtype=np.float32)test_labels = np.empty((test_size,101,), dtype=np.bool_)# 遍历train_data以分离图像和标签for i,data in enumerate(train_data):    train_images[i] = data[0]    train_labels[i] = data[1]# 遍历test_data以分离图像和标签    for i,data in enumerate(test_data):    test_images[i] = data[0]    test_labels[i] = data[1]# 将numpy数组转换为张量# 对标签进行one-hot编码(False ->0, True -> 1)train_images = tf.convert_to_tensor(train_images)train_labels = tf.convert_to_tensor(train_labels, dtype=tf.int32)test_images = tf.convert_to_tensor(test_images)test_labels = tf.convert_to_tensor(test_labels, dtype=tf.int32)

数据的形状为:

Train images shape:  (7809, 64, 64, 3)Test images shape:   (868, 64, 64, 3)Train labels shape:  (7809, 101)Test labels shape:   (868, 101)

我定义的Lenet5模型如下:

# 根据描述定义Lenet 5架构model = models.Sequential()model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(64, 64, 3)))model.add(layers.MaxPooling2D((4, 4)))model.add(layers.Conv2D(64, (5, 5), activation='relu'))model.add(layers.MaxPooling2D((4, 4)))model.add(layers.Flatten())model.add(layers.Dense(1024, input_shape=(256,), activation='relu'))model.add(layers.Dense(84, activation='relu'))model.add(layers.Dense(101, activation='softmax'))model.compile(optimizer='adam',loss=tf.keras.losses.CategoricalCrossentropy(), metrics=['accuracy'])history = model.fit(train_images, train_labels, epochs=20, validation_data=(test_images, test_labels))

观察到的损失和准确率值如下:

Train on 7809 samples, validate on 868 samplesEpoch 1/207809/7809 [==============================] - 17s 2ms/sample - loss: 3.8387 - accuracy: 0.2018 - val_loss: 3.3969 - val_accuracy: 0.2661...Epoch 19/207809/7809 [==============================] - 15s 2ms/sample - loss: 0.1205 - accuracy: 0.9679 - val_loss: 0.5456 - val_accuracy: 0.9136Epoch 20/207809/7809 [==============================] - 15s 2ms/sample - loss: 0.1672 - accuracy: 0.9522 - val_loss: 0.5295 - val_accuracy: 0.9159

我检查了确保我的测试数据不在训练数据中。

test_images.numpy() in train_images.numpy()# 输出 'False'

我绘制了混淆矩阵以确保模型不是将所有标签都分类为False。

from sklearn.metrics import confusion_matrixy_pred = model.predict_classes(test_images)y_label = []for i in range(len(y_pred)):    a, = np.where(test_labels[i].numpy()==1)    y_label.append(a[0])con_mat = tf.math.confusion_matrix(labels=y_label, predictions=y_pred)'''Output: con_mat = array([[2, 0, 0, ..., 0, 0, 0],       [0, 9, 0, ..., 0, 0, 0],       [0, 0, 4, ..., 0, 0, 0],       ...,       [0, 0, 0, ..., 5, 0, 0],       [0, 0, 0, ..., 0, 3, 0],       [0, 0, 0, ..., 0, 0, 2]], dtype=int32)'''

我明白我可能犯了一些错误或忽略了潜在的缺陷,这可能导致了高准确率。任何建议都将不胜感激。谢谢!


回答:

测试数据和训练数据没有正确分割。使用’take’和’skip’方法并没有将数据分割成两个独特的集合。我认为我对take和skip方法的理解有误。由于测试数据中的’大部分’而不是’全部’数据存在于训练数据中,因此行

test_images.numpy() in train_images.numpy()

输出False。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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