带有图像数据生成器的keras中的单层网络,但损失始终为负

single layer net in keras with imagedatagenerator, but loss is always negative

我试过很多种网络,但即使在基本网络(单层)中,设置为binary_crossentropy的损失总是负数

这是代码

from __future__ import print_function
import numpy as np
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
import cv2
from PIL import ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True
train_path = 'D:/rectangle'
val_path = 'D:/rectang'

model = Sequential()
model.add(Conv2D(32, 1, 1, input_shape=(230, 230, 3)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Dense(1))

model.compile(loss='binary_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])

train_datagen = ImageDataGenerator(
            samplewise_center=True,
            samplewise_std_normalization=True)
test_datagen = ImageDataGenerator()

train_generator = train_datagen.flow_from_directory(
    train_path,
    target_size=(230, 230),
    batch_size=32,
    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
    val_path,
    target_size=(230, 230),
    batch_size=32,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=200,
    epochs=50,
    validation_data=validation_generator,
    nb_val_samples=800
)

处理过程如下: 1/200 [................................] - 预计到达时间:20:17 - 损失:12.9030 - 累积:0.1250 2/200 [................................] - 预计到达时间:10:22 - 损失:-2.0179 - 累积:0.0625 3/200 [................................] - 预计到达时间:7:03 - 损失:-6.3273 - 累积:0.0417 4/200 [................................] - 预计到达时间:5:23 - 损失:-7.8592 - 累积:0.0312 5/200 [................................] - 预计到达时间:4:24 - 损失:-8.6776 - 累积:0.0250 6/200 [................................] - 预计到达时间:3:44 - 损失:-9.5563 - 累积:0.0208 7/200 [>................................] - 预计到达时间:3:15 - 损失:-9.3298 - 累积:0.0179 8/200 [>................................] - 预计到达时间:2:54 - 损失:-9.3455 - 累积:0.0156 9/200 [>................................] - 预计到达时间:2:37 - 损失:-10.2439 - 累积:0.0139 10/200 [>................................] - 预计到达时间:2:24 - 损失:-10.5647 - 累积:0.0125 11/200 [>................................] - 预计到达时间:2:13 - 损失:-10.8719 - 累积:0.0114 12/200 [>................................] - 预计到达时间:2:04 - 损失:-11.3775 - 累积:0.0104 13/200 [>................................] - 预计到达时间:1:56 - 损失:-11.3066 - 累积:0.0096 14/200 [=>......................] - 预计到达时间:1:49 - 损失:-11.4598 - 累积:0.0089 15/200 [=>................................] - 预计到达时间:1:48 - 损失:-11.4930 - 累积:0.0083 16/200 [=>......................] - 预计到达时间:1:47 - 损失:-11.6465 - 累积:0.0078 17/200 [=>................................] - 预计到达时间:1:51 - 损失:-11.6061 - 累积:0.0074

输入图片为乳腺癌组织学图片,460*460大小,20000张PNG格式图片。 如能解决不胜感激!

由于您正在进行二元分类(基于您的损失),因此您的最后一个激活函数应该是 sigmoid。所以 而不是

model.add(Dense(1))

你的最后一层应该是这样的:

model.add(Dense(1,activation='sigmoid'))

如果不指定,默认情况下您的激活将只是线性的,这适合回归场景而不是分类。