进行预测时获得相同的输出

2024-04-16 14:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我是ML的新手。我正在尝试做一个包含数字的图像分类的基本示例。我创建了自己的数据集,但精确度很差(11%)。我有246个训练项目和62个测试项目。 这是我的密码:

#TRAINING

def load_data(input_path, img_height, img_width):
  data = []
  labels = []
  for imagePath in os.listdir(input_path):  
    labels_path = os.path.join(input_path, imagePath)
    if os.path.isdir(labels_path): 
      for img_path in os.listdir(labels_path):
        labels.append(imagePath)
        img_full_path = os.path.join(labels_path, img_path)
        img = image.load_img(img_full_path, target_size=(img_height, img_width)) 
        img = image.img_to_array(img)
        data.append(img)
  return data, labels



  train_data = []
  train_labels = []
  test_data = []
  test_labels = []
  train_data, train_labels = load_data(train_path, 28, 28)
  test_data, test_labels = load_data(test_path, 28, 28)


  train_data = np.array(train_data)
  train_data = train_data / 255.0
  train_data = tf.reshape(train_data, train_data.shape[:3])
  train_labels = np.array(train_labels)
  train_labels = np.asfarray(train_labels,float)


  test_data = np.array(test_data) 
  test_data = tf.reshape(test_data, test_data.shape[:3])
  test_data = test_data / 255.0
  test_labels = np.array(test_labels)


 model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
  ])


  model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])


  model.fit(train_data, train_labels, batch_size=10, epochs=5, steps_per_epoch=246)

  test_loss, test_acc = model.evaluate(test_data, test_labels, steps=1)
  print('Test accuracy:', test_acc)

#CLASSIFICATION

def classify(input_path):
    if os.path.isdir(input_path):
        images = []
        for file_path in os.listdir(input_path):
            full_path = os.path.join(input_path, file_path)
            img_tensor = preprocess_images(full_path, 28, 28, "L")
            images.append(img_tensor)
        images = np.array(images)
        images = tf.reshape(images,(images.shape[0],images.shape[2],images.shape[3]))
        predictions = model.predict(images, steps = 1)


        for i in range(len(predictions)):
            print("Image", i , "is", np.argmax(predictions[i]))

def preprocess_images(image_path, img_height, img_width, mode):
    img = image.load_img(image_path, target_size=(img_height, img_width))
    #convert 3-channel image to 1-channel
    img = img.convert(mode)
    img_tensor = image.img_to_array(img) 
    img_tensor = np.expand_dims(img_tensor, axis=0)   
    img_tensor /= 255.0
    img_tensor = tf.reshape(img_tensor, img_tensor.shape[:3])
    return tf.keras.backend.eval(img_tensor)

当我做预测时,我总是得到“图像是5”的结果。所以,我有两个问题: -如何将其他类[0-9]作为输出? -我可以通过增加数据的数量来获得更好的准确性吗?你知道吗

谢谢。你知道吗


Tags: pathtestimageimginputdatalabelsos
1条回答
网友
1楼 · 发布于 2024-04-16 14:32:11

TLDR;

您的load_data()函数是罪魁祸首—您需要将数据集的标签作为整数而不是字符串filepath返回

更全面的解释:

Can I get better accuracy by increasing the number of data ?

一般来说,是的。你知道吗

你的模型没有本质上的问题。我显然无权访问您创建的数据集,但我可以在MNIST数据集(您的数据集可能正在尝试镜像)上测试它:

(train_data, train_labels),(test_data, test_labels) = tf.keras.datasets.mnist.load_data()


model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
  ])


model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])


model.fit(train_data, train_labels, batch_size=10, epochs=5)

test_loss, test_acc = model.evaluate(test_data, test_labels)
print('Test accuracy:', test_acc)

这样,我们可以训练到大约93%的准确率:

Test accuracy: 0.9275

然后,您的推理代码也会在测试数据上按预期工作:

predictions = model.predict(test_data)

for i in range(len(predictions)):
    print("Image", i , "is", np.argmax(predictions[i]))

给出输出,你会期望:

Image 0 is 7
Image 1 is 2
Image 2 is 1
Image 3 is 0
Image 4 is 4
...

所以我们知道模型可以工作。那么,与MNIST(60000)相比,性能上的差异仅仅取决于数据集(246)的大小吗?你知道吗

好吧,这是一件很容易测试的事情-我们可以从MNIST数据中提取一个大小相似的片段,然后重复这个练习:

train_data = train_data[:246]
train_labels = train_labels[:246]

test_data = test_data[:62]
test_labels = test_labels[:62]

所以这一次我看到了准确率的大幅下降(这次约为66%),但我可以训练模型,使其达到比你看到的更高的准确度,即使是更小的子集。你知道吗

因此,问题必须与数据预处理(或数据集本身)有关。你知道吗

事实上,通过查看load_data()函数,我可以看出问题出在生成的标签上。你的labels刚刚出现在图像路径上?你有这个:

#  snip 

for img_path in os.listdir(labels_path):
  labels.append(imagePath) ## <  this does not look right!

#  snip 

然而,您需要用图像所属类别的整数值填充labels(对于mnist数字,这是一个介于0和9之间的整数)

相关问题 更多 >