Tensorflow-keras:CNN的预测非常接近1或0

时间:2019-01-07 10:22:03

标签: tensorflow keras deep-learning

我在96x96细菌图像上训练了CNN。我有3类:“细菌”,“絮凝物”和“什么都没有”。

然后,要检测1920x1080图像上的细菌,我用96x96窗口扫描图像,然后为所有扫描的窗口运行CNN。

但是我的预测始终是[0,1,0]。我从来没有。 [0.1,0.8,0.1]

这是我的模特:

batch_size = 32
nb_epochs = 10
taille_image = (96,96)

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=(96, 96, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(3))
model.add(Activation('softmax'))




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


def Entrainer():

        train_datagen = ImageDataGenerator(
                rescale=1./255,
                horizontal_flip=True,
                vertical_flip=True,
                validation_split=0.2)


        train_generator = train_datagen.flow_from_directory(
                "Images_traitees/Vignettes_squared",
                target_size = taille_image,
                batch_size=batch_size,
                class_mode="categorical",
                shuffle=True,
                save_to_dir="augmented_data",
                save_prefix="augmented_",
                save_format="jpeg",
                subset="training"
        )
        validation_generator = train_datagen.flow_from_directory(
                "Images_traitees/Vignettes_squared",
                target_size = taille_image,
                batch_size=batch_size,
                class_mode="categorical",
                subset="validation"
        )

        tbCallback = keras.callbacks.TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
        history = model.fit_generator(
                train_generator,
                steps_per_epoch=train_generator.samples // batch_size,
                validation_data = validation_generator,
                validation_steps = validation_generator.samples // batch_size,
                epochs=nb_epochs,
                callbacks = [tbCallback]
        )

        plt.plot(history.history['acc'])
        plt.title('model accuracy')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.show()

        plt.plot(history.history['loss'])
        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.show()

        model.save("my_model.h5")


if __name__ == "__main__":
        model.summary()
        Entrainer()

tensorboard results

这是调用我的模型并为检测到的类着色的代码。

model = load_model("my_model.h5")

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


def Predire(img, vignettes, pos):
    image = Image.open(img)
    draw = ImageDraw.Draw(image, mode='RGBA')

    for vignette in vignettes:
        x = img_to_array(vignette)
        x = np.expand_dims(x, axis=0)
        y = model.predict(x)

        if y[0][1] > max (y[0][0],y[0][2]):
            draw.rectangle(pos[nb], outline='red', fill=(255,0,0,125))
            proto +=1
        if y[0][0] > max (y[0][1],y[0][2]):
            draw.rectangle(pos[nb], outline='blue', fill=(0,0,255,125))
            floc +=1
        if y[0][2] > max(y[0][1],y[0][0]):
            draw.rectangle(pos[nb], outline='black')

        print (y)



    return image

当我打印y时,它返回:

[[0. 0. 1.]]
[[0. 0. 1.]]
[[0. 0. 1.]]
[[0. 0. 1.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1. 0. 0.]]
[[1.0000000e+00 1.7819082e-33 0.0000000e+00]]
[[1. 0. 0.]]
...

每行代表整个图像96x96滑动窗口的CNN预测。

我认为这太合适了,但是我尝试了仅735个可训练参数,而且我的pb仍然相同。

1 个答案:

答案 0 :(得分:0)

您的模型可能严重过度拟合了数据。在训练和测试集上检查模型的准确性,看看它们是否合理。

更新:原来是图像数据的预处理是问题所在。始终确保对训练,验证和测试集应用相同的预处理。