python - 进行预测时得到相同的输出

标签 python tensorflow machine-learning

我是机器学习新手。我正在尝试制作包含数字的图像分类的基本示例。我创建了自己的数据集,但准确率很差 (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)

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

谢谢。

最佳答案

TLDR;

你的load_data()函数是罪魁祸首 - 你需要以整数而不是字符串文件路径返回数据集的标签

更全面的解释:

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% 的准确率:

测试准确度: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() 函数,我可以发现问题出在您生成的标签上。您的标签只是出现在图像路径中?你有这个:

# --snip--

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

# --snip--

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

关于python - 进行预测时得到相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56433089/

相关文章:

python - 生成数据集的多个独立分割

python - Docopt:重复元素后的选项被解释为重复元素

python - 逗号分隔时 Python 枚举中的 ValueError

python - 如何在自己的线程中扭曲运行并正确停止?

python - 运行时错误: as_numpy_iterator() is not supported while tracing functions

python - ImportError:无法从 'context'(未知位置)导入名称 'tensorflow.python.eager'

tensorflow_core._api.v2.random 没有属性 Generator

image-processing - 解析文本格式 caffe.NetParameter : 54:17: Message type "caffe.ConvolutionParameter" has no field named "sparse_ratio" 时出错

python - 在 Keras 中将顺序转换为函数式

python - 列表和集合的成员资格测试有何不同?