如何在Keras CNN模型中预测单个图像? [英] How can I predict single image in Keras CNN model?

查看:658
本文介绍了如何在Keras CNN模型中预测单个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照本指南学习CNN的图像分类,并将此代码实现到我的数据集中:

I am following this guide to learn image classification with CNN and I implemented this code into my data set:

https://www.tensorflow.org/tutorials/images/classification

代码已更新

train_image_generator = ImageDataGenerator(rescale=1. / 255)  # Generator for our training data
validation_image_generator = ImageDataGenerator(rescale=1. / 255)  # Generator for our validation data

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                           directory=train_img_folder,
                                                           shuffle=True,
                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                           class_mode='categorical',
                                                           color_mode='grayscale')

val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
                                                              directory=valid_img_folder,
                                                              target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                              class_mode='categorical',
                                                              color_mode='grayscale'
                                                              )

model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 1)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(3, activation='softmax')
])

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

history = model.fit_generator(
    train_data_gen,
    steps_per_epoch=total_train_value // batch_size,
    epochs=epochs,
    validation_data=val_data_gen,
    validation_steps=total_valid_value // batch_size
)

# Single prediction
img = []
temp = np.array(Image.open('path/to/pic.jpg').resize((256, 256), Image.ANTIALIAS))
temp.shape = temp.shape + (1,) # now its (256, 256, 1)
img.append(temp)
test = np.array(img) # (1, 1024, 1024, 1)
prediction = model.predict(test) 

当我尝试使用Forecast_generator函数时:

When I try predict_generator function:

test_datagen = ImageDataGenerator(rescale=1 / 255.)

test_generator = test_datagen.flow_from_directory('test_images/',
                                                  classes=['0', '1', '2'],
                                                  color_mode='grayscale',
                                                  shuffle=True,
                                                  # use same size as in training
                                                  target_size=(256, 256))

preds = model.predict_generator(test_generator, steps=4) # I dont know what is steps doing. I put there because of error. 

我的第一个问题是:我可以获得训练和验证的准确性,但是我想获得单张图片的预测结果.我怎样才能做到这一点?示例:

My first question is: I can get training and validation accuracy but I want to get single picture's prediction result. How can I do that? Example:

foo = model.predict(path/to/pic.jpg)
# foo returns 0-> 0.70 | 1-> 0.30

添加:当我尝试使用model.predict时,出现此错误:

Added: When I try to use model.predict like that I got this error:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1024, 1024)

或转换为2d(以及3d)np.array仍然相同.

or converting to 2d (and also 3d) np.array still got same.

我的第二个问题是:如果没有完整的%100,还有什么方法可以预测?我的意思是,如果我们有2个班级(猫和狗)并测试月亮图片,我想得到这样的结果:

My second question is: is there any way to predict without complete %100? I mean If we have 2 classes (cat and dog) and test moon picture i want to get results like that:

%15 cat | %10 dog

不是

%50 cat | %50 dog

已添加:我尝试按照以下更改放置垃圾分类.当我在history = model.fit_generator行上运行该命令时,出现以下错误:

Added: I tried to put garbage class as following changing. When I run that onhistory = model.fit_generator line I got following error:

ValueError: Error when checking target: expected dense_2 to have shape (3,) but got array with shape (2,) 

提前谢谢

推荐答案

第一个问题:我可以得到训练和验证准确性,但是我想获得单张图片的预测结果.我该怎么办?

First question :I can get training and validation accuracy but I want to get single picture's prediction result. How can I do that?

您可以在文档中看到,只要您完全使用model.predict(x)因为您的x是:
-Numpy数组(或类似数组的数组)或数组列表(如果模型具有多个输入).
-如果模型已命名输入,则dict将输入名称映射到相应的数组/张量.
-生成器或keras.utils.Sequence返回(输入,目标)或(输入,目标,样本权重).

As you can see in the doc, you can totally use model.predict(x), as long as your x is :
- A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
- A dict mapping input names to the corresponding array/tensors, if the model has named inputs.
- A generator or keras.utils.Sequence returning (inputs, targets) or (inputs, targets, sample weights).

您只需要编写读取.jpg图像并将其输入模型的代码即可.

You just have to write the code that reads the .jpg image and feed it to the model.

第二个问题:如果没有完整的%100,有什么方法可以预测?我的意思是,如果我们有2个班级(猫和狗)并测试月球图片,我想得到这样的结果:

Second question : is there any way to predict without complete %100? I mean If we have 2 classes (cat and dog) and test moon picture i want to get results like that:

您可以创建第三类垃圾",为此,您需要将网络的最后一层更改为:

You could create a third class 'garbage', to do so you'll need to change the last layer of your net to:

Dense(3, activation='softmax')

然后将损失更改为categorical_crossentropy

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

然后将class_mode更改为categorical,而不是binary.

And change class_mode to categorical and not binary.

在这种情况下,您将拥有狗:15%,猫:10%,垃圾:75%

In that case you'll have dog:15%, cat:10%, garbage: 75%

编辑conv2D错误:

Edit on conv2D error :

ValueError:检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(1024,1024)

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1024, 1024)

您有:

Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)),

这表示一个图片是(height, width, channel).
文档所示,因为这是您的input_layer需要提供形状为(samples, rows, cols, channels)的4D格式.如果只想提供一张图像,则需要具有一个形状为(1, rows, cols, channels)的数组.

This means that an image is (height, width, channel).
As seen in the doc, since this is the input_layer you need to provide the format in 4D with the shape : (samples, rows, cols, channels). If you want to give only one image, you need to have an array shaped as (1, rows, cols, channels).

这篇关于如何在Keras CNN模型中预测单个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆