【问题标题】:How to check "test data" accuracy and plot them如何检查“测试数据”的准确性并绘制它们
【发布时间】:2021-03-14 03:47:40
【问题描述】:

我想测试 11000 多张图像的准确性。 我将数据分为两类“是”和“否”。然后,分别在训练集和测试集中将它们分成80/20。

然后我将训练数据再次拆分为 80/20 用于验证集。

我分别为验证集、测试集、训练集创建“是”和“否”文件夹。并将数据保存在各自的文件夹中。

现在,我想在 Google Colab 中训练模型。

训练模型后,我想测试“测试数据集”的“准确性”,并用特异性、敏感性、准确性、损失函数和召回率绘制它们。

帮助需要这个。详细的帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 您必须发布您的代码示例,以便我们提供帮助
  • @Yefet 谢谢。我在答案中给出代码。实际上,我从代码中获得了训练/验证的准确性,但我想要测试集的准确性。我应该使用哪种方法?我应该使用K折吗?那么,如何使用呢?抱歉,问题太多了。

标签: python testing keras model google-colaboratory


【解决方案1】:

@叶菲特

代码在这里:

import tensorflow as tf
import matplotlib.pyplot as plt
import zipfile
import cv2
import os 
import numpy as np
import matplotlib.image as mpimg
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image

from sklearn.model_selection import KFold, StratifiedKFold

img = image.load_img("drive/MyDrive/data03/train/no/1 no.jpeg" , target_size=(200, 200))
plt.imshow(img)

class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs={}):
        if (logs.get('accuracy') > .98) & (logs.get('val_accuracy') > .9):
            print("Reached 98% accuracy so cancelling training!")
            self.model.stop_training = True
callbacks = myCallback()

train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)


train_generator = train_datagen.flow_from_directory(
        'drive/MyDrive/data03/train',  # This is the source directory for training images
        target_size=(200, 200),  # All images will be resized to 150x150
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

validation_generator = train_datagen.flow_from_directory(
        'drive/MyDrive/data03/validation',  # This is the source directory for training images
        target_size=(200, 200),  # All images will be resized to 150x150
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

from tensorflow.keras.optimizers import RMSprop

model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    # This is the first convolution
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(200, 200, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fourth convolution
    #tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    #tf.keras.layers.MaxPooling2D(2,2),
    # The fifth convolution
    #tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    #tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('horses') and 1 for the other ('humans')
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=0.001),
              metrics=['accuracy'])

history = model.fit(
      train_generator,
      steps_per_epoch=1,  
      epochs=29,
      validation_data = validation_generator,callbacks=[callbacks])
dir_path = "drive/MyDrive/data03/test"
for i in os.listdir(dir_path):
    img = image.load_img(dir_path+ '//' + i, target_size=(200, 200) )
    plt.imshow(img)
    plt.show()
   
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])

    classes =model.predict(images)
    if classes[0]==0:
        print("NO TUMOR")
    else:
        print("YES ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2021-12-12
    • 2019-12-04
    • 2017-06-13
    • 2021-04-09
    • 1970-01-01
    • 2021-09-24
    • 2017-05-03
    相关资源
    最近更新 更多