训练期间残差不为零的Tensorflow度量

训练期间残差不为零的Tensorflow度量,tensorflow,keras,keras-metrics,Tensorflow,Keras,Keras Metrics,目前,我正在使用tensorflow和来自keras应用程序的ImageNet预训练的EfficientNetB0开发一个图像错误分类器。作为衡量标准,我使用假阳性(fp)、真阳性(tp)、假阴性(fn)、真阴性(tn)等。。。 我对fp、tp、fn和tn等指标的问题是,它们在培训期间不是真正的整数值(即tp=4883.6257),只有在验证期间才是整数值。据我所知,这些指标应始终为整数,因为它们仅为假阳性预测样本数。我是否遗漏了keras在训练中运用这些价值观所做的工作 作为输入管道,我使用t

目前,我正在使用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)

我认为您应该在这些指标中定义参数阈值。默认情况下,BinaryAccurance度量的阈值为0.5,您可以根据精度进行调整

例如:

keras.metrics.TruePositives(name='tp', thresholds=0.5)