Keras中自定义回调中每个时期的Kappa得分

时间:2019-07-29 07:45:18

标签: image keras callback data-augmentation kappa

我正在使用Keras构建图像分类模型,并希望计算每个时期的kappa得分,并将其用于患者= 4的回调中。 我

# data augmentation
train_datagen=ImageDataGenerator(rescale=1./255,
                                rotation_range=30,
                                width_shift_range=0.2,
                                height_shift_range=0.2,
                                validation_split=0.2,
                                zoom_range=0.25)

train_generator=train_datagen.flow_from_dataframe(
    dataframe=train,
    directory="../input/aptos2019-blindness-detection/train_images/",
    x_col="id_code",
    y_col="diagnosis",
    batch_size=BATCH_SIZE,
    class_mode="categorical",
    target_size=(HEIGHT, WIDTH),
    subset='training')

valid_generator=train_datagen.flow_from_dataframe(
    dataframe=train,
    directory="../input/aptos2019-blindness-detection/train_images/",
    x_col="id_code",
    y_col="diagnosis",
    batch_size=BATCH_SIZE,
    class_mode="categorical",    
    target_size=(HEIGHT, WIDTH),
    subset='validation')

def create_model(input_shape, n_out):
    input_tensor = Input(shape=input_shape)
    base_model = applications.DenseNet201(weights=None,include_top=False,input_tensor=input_tensor)
    base_model.load_weights('../input/models-pretrained-weights/densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5')
    x = GlobalAveragePooling2D()(base_model.output)
    x = Dropout(0.6)(x)
    x = Dense(2048, activation='relu')(x)
    x = Dropout(0.6)(x)
    final_output = Dense(n_out, activation='softmax', name='final_output')(x)
    model = Model(input_tensor, final_output)
    return model

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_dataframe(  
        dataframe=test,
        directory = "../input/aptos2019-blindness-detection/test_images/",
        x_col="id_code",
        target_size=(HEIGHT, WIDTH),
        batch_size=1,
        shuffle=False,
        class_mode=None)


# class for calculating the kappa score
class clsvalidation_kappa(Callback):  #inherits from Callback

    def __init__(self, validation_data=(), patience=5):
        super(Callback, self).__init__()

        self.patience = patience
        self.X_val, self.y_val = validation_data  #tuple of validation X and y
        self.best = 0.0
        self.wait = 0  #counter for patience

    def on_epoch_end(self, epoch, logs={}):
        p = self.model.predict(self.X_val.values, verbose=0)
        current = ml_metrics.quadratic_weighted_kappa(self.y_val.values.ravel(),np.clip(np.round(p.astype(int).ravel()), 1, 8))

        if current > self.best:
            self.best = current
            self.wait = 0
        else:
            if self.wait >= self.patience:
                self.model.stop_training = True
                print('Epoch %05d: early stopping' % (epoch))

            self.wait += 1 #incremental the number of times without improvement
        print('Epoch %d Kappa: %f | Best Kappa: %f \n' % (epoch,current,self.best))



#compiling the model
val_call = clsvalidation_kappa(validation_data=(X_test, y_test), patience=3) #instantiate object

model = create_model(input_shape=(HEIGHT, WIDTH, CANAL), n_out=N_CLASSES)

for layer in model.layers:
    layer.trainable = False

for i in range(-10, 0):
    model.layers[i].trainable = True

metric_list = ['accuracy',f1_m,skmetrics]
optimizer = optimizers.Adam(lr=WARMUP_LEARNING_RATE)
model.compile(optimizer=optimizer, loss="categorical_crossentropy",  metrics=metric_list)
model.summary()




#fitting a fit_generator 
STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size
STEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size

history_warmup = model.fit_generator(generator=train_generator,
                              steps_per_epoch=STEP_SIZE_TRAIN,
                              validation_data=valid_generator,
                              validation_steps=STEP_SIZE_VALID,
                              epochs=WARMUP_EPOCHS,                             verbose=1,class_weight=class_weights,callbacks=[skmetrics]).history

  

NameError跟踪(最近的呼叫   最后)   ----> 1 val_call = clsvalidation_kappa(validation_data = {X_test,y_test),耐心= 3)#实例化对象

     

NameError:名称'X_test'未定义

我正在使用数据扩充来生成更多数据,并使用.fit_generator拟合模型。 这里的问题是,用于计算kappa分数的类是X_train和X_test,而我直接使用增强数据。 必须对类进行哪些更改,以便我们可以使用train_generator和valid_generator。

我在代码上苦苦挣扎了一段时间。 任何帮助将不胜感激

0 个答案:

没有答案