目前,我正在使用 tensorflow 和来自 keras 应用程序的 ImageNet 预训练 EfficientNetB0 研究图像错误分类器。作为指标,我使用的是假阳性 (fp)、真阳性 (tp)、假阴性 (fn)、真阴性 (tn)……我对 fp、tp、fn 和 tn 等指标的问题是它们在训练期间不是真正的整数值(即 tp = 4883.6257),只有在验证期间它们才是整数值。据我所知,这些指标应该始终是整数,因为它们只是假阳性预测样本的数量。有什么我错过了 keras 在训练期间计算这些值的功能吗?
作为输入管道,我使用 tensorflow ImageDataGenerator 和 .flow_from_dataframe() 函数:
# create data generators in order to load the images
datagen_train = ImageDataGenerator(horizontal_flip = True, vertical_flip = True, brightness_range = (0.9, 1.1),
rescale=1. / 255, fill_mode = "constant", zoom_range = 0.3, channel_shift_range=100.0)
datagen_val = ImageDataGenerator(rescale = 1. / 255)
train_generator = datagen_train.flow_from_dataframe(
dataframe = balanced_df[:N_Train],
directory = bitmap_folder_path,
x_col = "filename",
y_col = "particle",
batch_size = batch_size,
shuffle = True,
class_mode = "binary",
target_size = (250,250),
color_mode = "rgb",
seed = 42)
valid_generator = datagen_val.flow_from_dataframe(
dataframe = balanced_df[N_Train:],
directory = bitmap_folder_path,
x_col = "filename",
y_col = "particle",
batch_size = batch_size,
shuffle = True,
class_mode = "binary",
target_size = (250,250),
color_mode = "rgb",
seed = 42)
设置模型:
from tensorflow.keras.applications import EfficientNetB0
input_shape = (img_height, img_width, 3) # use depth=3 because imagenet is trained on RGB images
model = EfficientNetB0(weights='imagenet', include_top = False, input_shape = input_shape)
# add a global spatial average pooling layer
x = model.output
x = keras.layers.GlobalAveragePooling2D()(x)
# and a fully connected output/classification layer
predictions = keras.layers.Dense(1, activation='sigmoid')(x)
# create the full network so we can train on it
model_B0 = keras.models.Model(inputs=model.input, outputs=predictions)
batch_size = 16
num_epochs = 30
# setup optimizer similar to used one in original paper
# they used: RMSProp with decay of 0.9 and momentum of 0.9, batch norm momentum of 0.99, a initial learning rate of
# 0.256 that decays by 0.97 every 2.4 epochs
initial_learning_rate = 1e-5
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps= int(2.4 * steps_per_epoch_train),
decay_rate=0.97,
staircase=True)
opt_efficientNet = tf.keras.optimizers.RMSprop(learning_rate=lr_schedule,
rho=0.9, momentum=0.9, name="RMSprop")
为了更好地分析,我添加了以下指标:
METRICS = [
keras.metrics.TruePositives(name='tp'),
keras.metrics.FalsePositives(name='fp'),
keras.metrics.TrueNegatives(name='tn'),
keras.metrics.FalseNegatives(name='fn'),
keras.metrics.BinaryAccuracy(name='accuracy'),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall'),
keras.metrics.AUC(name='auc'),
]
model_B0.compile(
loss="binary_crossentropy",
optimizer=opt_efficientNet,
metrics=METRICS)