简体   繁体   English

如何配置Keras模型来预测图像?

[英]How do I need to configure Keras model to predict an image?

The main task is to predict a mask for the input image. 主要任务是预测输入图像的遮罩。 So I have the following data for training: 因此,我有以下训练数据:

  • lot's of 768x768 original pics like this: 768x768的原始图片是这样的:

在此处输入图片说明

  • and output mask pics(also 768x768) like this: 并输出掩码图片(也为768x768),如下所示:

在此处输入图片说明

Also I have validation original pics. 另外我有验证原始照片。

I prepare some kind of neural model that should predict the output mask. 我准备了一些可以预测输出掩码的神经模型。 I prepared keras model configuaration that should have a topology which looks like below: 我准备了keras模型配置 ,它应该具有如下所示的拓扑:

在此处输入图片说明

The code I prepared for training is there. 我准备培训的代码在那里。

import keras
epochs=100

image_datagen = keras.preprocessing.image.ImageDataGenerator()
mask_datagen = keras.preprocessing.image.ImageDataGenerator()
seed = 1
image_generator = image_datagen.flow_from_directory(
    'H:/LABS/ship_detection/test_train/',
    color_mode='rgb',batch_size=32,target_size=(768,768),
    seed=seed)

mask_generator = mask_datagen.flow_from_directory(
    'H:/LABS/ship_detection/test_mask/',
    class_mode="categorical",batch_size=32,target_size=(768,768),
    seed=seed)

train_generator = zip(image_generator, mask_generator)

model.fit_generator(generator=train_generator,
                    epochs=epochs,
                    callbacks=callbacks,steps_per_epoch=1)

But when I try to fit generator for prediction I have an issue: 但是,当我尝试使用生成器进行预测时,我遇到了一个问题:

c:\users\harwister\appdata\local\programs\python\python36\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    208                     batch_size = list(x.values())[0].shape[0]
    209                 else:
--> 210                     batch_size = x.shape[0]
    211                 batch_logs['batch'] = batch_index
    212                 batch_logs['size'] = batch_size

AttributeError: 'tuple' object has no attribute 'shape'

I do something wrong for sure, but I can't understand anything from these kind of errors. 我肯定会做错事,但是从这些错误中我什么也听不懂。 The simple question I can't find a response in Google is: How can I push into Keras two images (input and output images) for training and after training get an output image providing an input image? 我在Google中找不到响应的简单问题是:如何将两张图像(输入和输出图像)推入Keras进行训练,训练后如何获得提供输入图像的输出图像?

Since you have separate generators for the images and the labels (ie masks), you need to set the class_mode argument to None to prevent the generators from producing any labels arrays: 由于您有图像和标签(即蒙版)的单独生成器,因此需要将class_mode参数设置为None以防止生成器生成任何标签数组:

image_generator = image_datagen.flow_from_directory(class_mode=None, ...)
mask_generator = mask_datagen.flow_from_directory(class_mode=None, ...)

This way, image_generator would only generate the input images and the mask_generator would only generate the mask (ie true label) images. 这样, image_generator将仅生成输入图像,而mask_generator将仅生成蒙版(即真实标签)图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM