我尝试使用 U-net 来做语义分割问题。掩码图像是二进制的。但是在训练时,我发现我的损失是负数。这里的损失我使用 binary_crossentropy。这是我的代码:
X_train = X_train /255
y_train = y_train /255
X_val = X_val/255
y_val = y_val/255
他们都有类型np.float32
然后我使用 imageDataGenerator 来增强图像,代码如下:
def image_augmentation(X_train,y_train):
# Set hyper parameters for the model.
data_gen_args = dict(featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=90.,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True)
image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)
seed = 42
image_datagen.fit(X_train, augment=True, seed=seed)
mask_datagen.fit(y_train, augment=True, seed=seed)
image_generator = image_datagen.flow(
X_train,batch_size=8,
seed=seed)
mask_generator = mask_datagen.flow(
y_train, batch_size=8,
seed=seed)
while True:
yield(image_generator.next(),mask_generator.next())
train_generator = image_augmentation(X_train,y_train)
pat_init = 50
pat = pat_init
learning_rate = 1e-4
##change the model weight you want
file_path = "./model_v1/improvement-{epoch:02d}-{val_my_iou_metric:.5f}.hdf5"
checkpoint = ModelCheckpoint(file_path,monitor = 'val_my_iou_metric',verbose=1,save_best_only=True,mode='max')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', mode = 'auto',factor=0.5, patience=5, min_lr=1e-9, verbose=1)
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=learning_rate), metrics=[my_iou_metric])
# Use the image data Augment below to achieve better result
model.fit_generator(
train_generator,steps_per_epoch=2000,epochs=300,
validation_data=(X_val, y_val), verbose=1,
callbacks=[checkpoint,reduce_lr]
)
My net 的最后一层定义如下:
output = Conv2D(1,activation='sigmoid',
kernel_size=(1,1),
padding='same',
data_format='channels_last')(x)
我真的很好奇为什么会发生这种情况?'sigmoid' 函数的输出是否介于 0 和 1 之间?
如果您有什么想法,请与我讨论。非常感谢!