如何与DenseNet121模型使用K-Fold交叉验证



我正在研究使用DensetNet121预训练模型对乳腺癌图像进行分类。我将数据集分为训练、测试和验证。我想用k-fold cross validation。我使用cross_validationsklearn库,但我得到下面的错误,当我运行代码。我试图解决它,但没有解决错误。有人知道怎么解决这个问题吗?

in_model = tf.keras.applications.DenseNet121(input_shape=(224,224,3),
include_top=False,
weights='imagenet',classes = 2)
in_model.trainable = False
inputs = tf.keras.Input(shape=(224,224,3))
x = in_model(inputs)
flat = Flatten()(x)
dense_1 = Dense(1024,activation = 'relu')(flat)
dense_2 = Dense(1024,activation = 'relu')(dense_1)
prediction = Dense(2,activation = 'softmax')(dense_2)
in_pred = Model(inputs = inputs,outputs = prediction)
validation_data=(valid_data,valid_labels)
#16
in_pred.summary()
in_pred.compile(optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.0002), loss=tf.keras.losses.CategoricalCrossentropy(from_logits = False), metrics=['accuracy'])
history=in_pred.fit(train_data,train_labels,epochs = 3,batch_size=32,validation_data=validation_data)
model_result=cross_validation(in_pred, train_data, train_labels, 5)

错误:

TypeError: Cannot clone object '<keras.engine.functional.Functional object at 0x000001F82E17E3A0>'
(type <class 'keras.engine.functional.Functional'>): 
it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' method.

由于您的模型不是scikit-learn估计器,因此您将无法使用sklearn的内置cross_validate方法。

然而,你可以使用k-fold将你的数据分成k个折叠,并获得每个折叠的指标。我们可以使用model.evaluate内置的TF,或者这里也可以使用sklearn的指标。

from sklearn.model_selection import KFold
in_model = tf.keras.applications.DenseNet121(
input_shape=(224, 224, 3), include_top=False, weights="imagenet", classes=2
)
in_model.trainable = False
inputs = tf.keras.Input(shape=(224, 224, 3))
x = in_model(inputs)
flat = Flatten()(x)
dense_1 = Dense(1024, activation="relu")(flat)
dense_2 = Dense(1024, activation="relu")(dense_1)
prediction = Dense(2, activation="softmax")(dense_2)
in_pred = Model(inputs=inputs, outputs=prediction)
validation_data = (valid_data, valid_labels)
# 16
in_pred.summary()
in_pred.compile(
optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.0002),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=["accuracy"],
)

kf = KFold(n_splits=10)
kf.get_n_splits(train_data)
for i, (fold_train_index, fold_test_index) in enumerate(kf.split(train_data)):
print(f"Fold {i}:")
print(f"  Train: index={fold_train_index}")
print(f"  Test:  index={fold_test_index}")
history = in_pred.fit(
train_data[fold_train_index],
train_labels[fold_train_index],
epochs=3,
batch_size=32,
validation_data=validation_data,
)
in_pred.evaluate(train_data[fold_test_index],train_labels[fold_test_index])

相关内容

  • 没有找到相关文章

最新更新