获取使用CNN提取的特征类别

时间:2019-05-26 18:06:28

标签: python image-processing keras deep-learning conv-neural-network

我有一个包含374个类别的图像数据集。我正在尝试使用CNN提取特征向量。我使用最后一个maxpooling层的输出来获取功能。我的问题是我如何知道哪个特征向量对应于特定的图像类。这是我到目前为止所做的事情

model = Sequential()
conv1_2d = model.add(Conv2D(180, (3, 3), padding='same', input_shape=(180, 180, 3), activation="relu")) #180 is the number of filters
conv2_2d = model.add(Conv2D(180, (3, 3), activation="relu"))
max_pool1 = model.add(MaxPooling2D(pool_size=(3, 3))) #maxpooling to scale down the values and keep only the important data
drop_1 = model.add(Dropout(0.25))
conv3_2d =model.add(Conv2D(360, (3, 3), padding='same', activation="relu"))
conv4_2d =model.add(Conv2D(360, (3, 3), activation="relu"))
max_pool2 = model.add(MaxPooling2D(pool_size=(10, 10)))
drop_2 = model.add(Dropout(0.25))
flat = model.add(Flatten())
dense_1 = model.add(Dense(496, activation="relu"))
drop_3 = model.add(Dropout(0.25))                   
dense_2 = dense_layer = model.add(Dense(376, activation="softmax"))
model.compile(
    loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['accuracy']
    )
model.fit(
    train_data,
    train_label,
    batch_size=32,
    epochs=40,
    verbose = 2 ,
    validation_split=0.1,
    shuffle=True)


# Save neural network structure

model_structure = model.to_json()

f = Path("model_structure.json")

f.write_text(model_structure)

# Save neural network's trained weights

model.save_weights("model_weights.h5")

#loading the model
f = Path("model_structure.json")
model_structure = f.read_text()
model_ = model_from_json(model_structure)
model_.load_weights("model_weights.h5")
layer_output = model_.layers[6].output
input_ = model_.layers[0].input
intermediate_layer_model = Model(inputs=input_,
                                 outputs=layer_output)
features_vector_train = intermediate_layer_model.predict(train_data)
features_vector_test = intermediate_layer_model.predict(test_data)

0 个答案:

没有答案