【问题标题】:Tensorflow and keras get same output prediction errorTensorFlow 和 keras 得到相同的输出预测误差
【发布时间】:2020-05-25 07:51:47
【问题描述】:

所以我学会了为石头、纸、剪刀创建图像识别。所以模型的类型将是分类的。 当我试图预测输出时程序出错总是相同的。所以这里的输出和代码:

import numpy as np
from google.colab import files
from keras.preprocessing import image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

uploaded = files.upload()

for fn in uploaded.keys():

  # predicting images
  path = fn
  img = image.load_img(path, target_size=(150,150))
  imgplot = plt.imshow(img)
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=30)
  model.summary()
  print(classes[0:10])

输出:

Saving p4.jpg to p4 (4).jpg
Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_12 (Conv2D)           (None, 148, 148, 32)      896       
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 72, 72, 64)        18496     
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 36, 36, 64)        0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 34, 34, 128)       73856     
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 17, 17, 128)       0         
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 15, 15, 128)       147584    
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 7, 7, 128)         0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 6272)              0         
_________________________________________________________________
dense_8 (Dense)              (None, 256)               1605888   
_________________________________________________________________
dropout (Dropout)            (None, 256)               0         
_________________________________________________________________
dense_9 (Dense)              (None, 3)                 771       
=================================================================
Total params: 1,847,491
Trainable params: 1,847,491
Non-trainable params: 0
_________________________________________________________________
[[1. 0. 0.]]

我不太了解机器学习。所以我希望能解决这个程序的一些问题。此外,我从我的程序中添加了一些完整的代码。这里是:

train_datagen = ImageDataGenerator(
                    rescale=1./255,
                    rotation_range=20,
                    horizontal_flip=True,
                    shear_range = 0.2,
                    fill_mode = 'nearest')

test_datagen = ImageDataGenerator(
                    rescale=1./255,
                    rotation_range=20,
                    horizontal_flip=True,
                    shear_range = 0.2,
                    fill_mode = 'nearest')

train_generator = train_datagen.flow_from_directory(
        train_dir,  
        target_size=(150, 150),  
        batch_size=32,
        class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
        validation_dir, 
        target_size=(150, 150), 
        batch_size=32, 
        class_mode='categorical')


model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(3, activation = 'softmax')
])


model.compile(loss='categorical_crossentropy',
              optimizer=tf.optimizers.Adam(),
              metrics=['accuracy'])

model.fit(
      train_generator,
      # steps_per_epoch=25,
      epochs=20,
      batch_size=32,
      validation_data=validation_generator, 
      # validation_steps=5,
      verbose=2)

对于输出拟合: output.fit

【问题讨论】:

  • 你遇到了什么错误?
  • 您遇到了什么问题?
  • 问题是预测总是在每个图像中得到这样的输出[[1. 0. 0.]]

标签: python tensorflow machine-learning image-processing keras


【解决方案1】:

您没有对输入图像进行重新缩放。在运行 predict() 之前尝试将输入图像除以 255

classes = model.predict(images/255.0, batch_size=30)

【讨论】:

  • 以下代码:img = image.load_img(path, target_size=(150,150)) 是否重新缩放输入图像?如果不是,代码是什么
  • 不,train_datagen 的参数 rescale=1./255 这意味着它在训练之前将每个图像除以 255。但是 predict 函数在没有这种修改的情况下获取图像
  • okee 在我尝试添加您的代码之后,现在输出预测再次不同,这是输出:[[9.9998391e-01 1.6114020e-05 7.1954855e-09]] 抱歉,我是 ml 先生
猜你喜欢
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2021-01-07
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多