首页 > 解决方案 > 通过线性评估进行迁移学习:SimCLR,高损失值(损失:28494.7827)

问题描述

我使用 SimCLR,所以在使用 SimCLR 对我的图像数据集进行预训练并保存预训练的权重后,我通过线性评估传递给迁移学习,所以我尝试了这个线性评估笔记本:

path = "./train"
pathv = "./test"
img_gen = tf.keras.preprocessing.image.ImageDataGenerator()
train_set = img_gen.flow_from_directory(path,(224, 224),'rgb', batch_size = 8)

img_genv = tf.keras.preprocessing.image.ImageDataGenerator()
test_set = img_genv.flow_from_directory(pathv,(224, 224),'rgb', batch_size=8)

le = LabelEncoder()
y_train_enc2 = le.fit_transform(train_set.classes)
y_test_enc2 = le.transform(test_set.classes)

def get_resnet_simclr(hidden_1, hidden_2, hidden_3):
    base_model = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3))
    base_model.trainable = True
    inputs = tf.keras.Input((224, 224, 3))
    h = base_model(inputs, training=False)
    h = tf.keras.layers.GlobalAveragePooling2D()(h)
    projection_1 = tf.keras.layers.Dense(hidden_1)(h)
    projection_1 = tf.keras.layers.Activation("relu")(projection_1)
    projection_2 = tf.keras.layers.Dense(hidden_2)(projection_1)
    projection_2 = tf.keras.layers.Activation("relu")(projection_2)
    projection_3 = tf.keras.layers.Dense(hidden_3)(projection_2)
    resnet_simclr = tf.keras.Model(inputs, projection_3)
    return resnet_simclr

resnet_simclr = get_resnet_simclr(256, 128, 50)
resnet_simclr.load_weights('weight.h5')
resnet_simclr.summary()

def get_linear_model(features):
    linear_model = tf.keras.Sequential([tf.keras.layers.Dense(3, input_shape=(features, ),activation="softmax")])
    return linear_model

resnet_simclr.layers[1].trainable = False
resnet_simclr.summary()

projection = tf.keras.Model(resnet_simclr.input, resnet_simclr.layers[-2].output)

projection.summary()
# Extract train and test features
train_features = projection.predict(train_set) #problem is here when I load totality of train_set
test_features = projection.predict(test_set)

linear_modell = get_linear_model(128)
linear_modell.summary()

linear_modell.compile(loss="sparse_categorical_crossentropy", metrics=["accuracy"], optimizer=tf.keras.optimizers.SGD(learning_rate=0.0001))

es = tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=2, verbose=2, restore_best_weights=True)
history = linear_modell.fit(train_features, y_train_enc2,
                 validation_data=(test_features,y_test_enc2),
                batch_size=8,
                 epochs=100,
                 callbacks=[es])

但我发现那些有线结果:

loss: 28494.7827 - accuracy: 0.4161 - val_loss: 7616.1938 - val_accuracy: 0.4698 我猜这行的问题是: train_features = projection.predict(train_set) 我必须只预测 x_train 可能

标签: pythontensorflowmachine-learningunsupervised-learning

解决方案


推荐阅读