2

我正在训练一个自动编码器模型,并希望在运行时从编码器部分保存每个图像的特征,并稍后将其用于特征匹配。

我的模型结构是-

train_data_dir = '/train'
test_data_dir = '/test'
nb_train_samples = 100
nb_validation_samples = 25
nb_epoch = 2
batch_size = 5
input_img = Input(shape=( img_width, img_height,3))

x = Conv2D(128, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu',padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

def fixed_generator(generator):
    for batch in generator:
        yield (batch, batch)

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None)
#print(type(train_generator))
test_generator = test_datagen.flow_from_directory(
        test_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None)

autoencoder.fit_generator(
        fixed_generator(train_generator),
        epochs=nb_epoch,
        samples_per_epoch=nb_train_samples,
        validation_data=fixed_generator(test_generator),
        validation_steps=nb_validation_samples
        )

如何在模型拟合期间保存编码器部分的特征。有什么办法吗,求推荐

4

4 回答 4

1

要从自动编码器模型中保存特征,首先需要加载模型并从模型中提取最后一个编码器层。

Here is the code to extract the layers of an encoder and save the features for an image:

autoencoder= load_model('model_weights.h5')
encoder = Model(autoencoder.input, autoencoder.layers[-1].output)

# Read your input image for which you need to extract features

img = cv2.imread(img)
newimg = cv2.resize(img,(512,512))
encoded_imgs = encoder.predict(newimg[None])[0]

# Save your features as an numpy array

np.save('features.npy', encoded_imgs)

After saving your feature for an input image you need to find the euclidean distance for feature matching.

file = 'sample.npy'
file = np.load(file)
file1 = "features.npy"
file1 = np.load(file1)
distance = np.linalg.norm(file-file1)
print(distance)

This code will extract the features from an image and calculate the distance between two numpy arrays.

于 2019-11-16T11:04:33.023 回答
0

Keras Functional API 是关键词,甚至还有一篇关于自动编码器的非常有见地的博客文章

以下是取自博文的示例的要点:

input_img = Input(shape=(784,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)

现在您可以将该autoencoder.fit(...,epochs=1)方法放入 for 循环并查看编码表示encoder.predict(your_data)以保存特征。

于 2019-11-16T10:44:56.027 回答
0

In a similar task with text data, I am extracting the encoder from a trained model and saving it out. I load the encoder and do encoder.predict(input_sample) to generate encoded vector (for retrieval purpose). However every time I load and predict the output varies (keeping the same input). Is this behavior normal?

于 2020-07-01T09:03:41.383 回答
0

I think you should use

encoder = Model(input_img , encoded)

instead of

encoder = Model(autoencoder.input, autoencoder.layers[-1].output)

because autoencoder.layers[-1].output means the decoder layer which is the reconstructed original image

于 2021-12-13T16:27:02.627 回答