【发布时间】:2019-02-25 21:21:40
【问题描述】:
我想要一个图像嵌入,以了解网络看到哪些图像更接近,哪些图像对他来说似乎非常不同。 首先,我想在 Keras 中使用 Tensorboard 回调,但文档对我来说不够清晰,我找不到任何有用的示例来重现它。因此,为了确保理解我在做什么,我更喜欢自己进行嵌入。
为此,我计划下载已经在我的数据上训练过的模型,删除最后一层(最后一个 dropout 和密集层),并预测验证图像以获得与每个图像相关的特征。然后我会简单地对这些特征进行 PCA 并根据它们的前三个主成分值绘制图像。
但我想我误解了一些东西,因为当我删除最后一层时,模型预测仍然是类数的大小,但对我来说它应该是最后一层的大小,在我的模型中是 128案例。
以下是澄清代码(我只是在其中放置了似乎对回答问题有用的行,但请不要犹豫询问更多详细信息):
#model creation
base_model = applications.inception_v3.InceptionV3(include_top=False,
weights='imagenet',
pooling='avg',
input_shape=(img_rows, img_cols, img_channel))
#Adding custom Layers
add_model = Sequential()
add_model.add(Dense(128, activation='relu',input_shape=base_model.output_shape[1:],
kernel_regularizer=regularizers.l2(0.001)))
add_model.add(Dropout(0.60))
add_model.add(Dense(2, activation='sigmoid'))
# creating the final model
model = Model(inputs=base_model.input, outputs=add_model(base_model.output))
然后我在具有两个类的数据集上训练模型,并加载模型及其权重以生成特征:
model = load_model(os.path.join(ROOT_DIR,'model_1','model_cervigrams_all.h5'))
#remove the last two layers
#remove dense_2
model.layers[-1].pop()
#remove dropout_1
model.layers[-1].pop()
model.summary() # last alyer output shape is : (None, 128), so the removal worked
#predict
model.predict(np.reshape(image,[1,image.shape[0],image.shape[1],3])) #output only two values
我哪里错了?你有什么建议吗?
【问题讨论】:
标签: python machine-learning keras embedding keras-layer