我正在遵循此link : Weakly-supervised-object-localization,以在CNN用来识别类别的图像中创建该区域的热图。
根据上述存储库,完成以下步骤:
第1步:在我的3类自定义数据集上训练模型
def creat_model():
inputs = Input(shape=(299,299,3))
#Notic : preprocess is different in each Model
resize = Lambda(Resize,(299,299,3))(inputs)
#normal = Lambda(preprocess_input,(256,256,3))(resize)
base_model = InceptionV3(weights='imagenet', include_top=False)
conv = base_model(resize) #conv = base_model(resize)
GAV = GlobalAveragePooling2D()(conv)
outputs = Dense(3,activation='softmax')(GAV)
model = Model(inputs,outputs)
model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
def training(model):
earlystopping = EarlyStopping()
modelchenkpoint = ModelCheckpoint('model_best_only',save_best_only=True)
model.fit(trainX,trainY,
batch_size=32,
nb_epoch=1,
validation_data=(testX,testY),
callbacks=[earlystopping,modelchenkpoint])
model.save('model_save_file.h5')
第2步:使用
获取卷积特征图def get_conv(model,test_imgs):
#Using InceptionV3's output
#base_model = Model(model.input,model.get_output_at.output)
inputs = Input(shape=(299,299,3))
resize = Lambda(Resize,(299,299,3))(inputs)
inception_v3 = model.get_layer('inception_v3')
outputs = inception_v3(resize)
new_model = Model(inputs,outputs)
#new_model.save('conv.npy')
print('Loading the conv_features of test_images .......')
conv_features = new_model.predict(test_imgs)
print('Loading the conv_features done!!!')
print(conv_features)
return conv_features
第3步:使用
将weights.npy,conv_features.npy和predict_label.npy保存为numpy数组。conv_features = get_conv(model,testX)
np.save('conv_features', conv_features)
print('Predict the labels of test_images .......')
predict_label = model.predict(testX)
np.save('predict_label', predict_label)
print('Extraction the weight between GAV and dense(2048x10) .......')
w = model.get_weights()[-2]
np.save('weight', w)
现在,我要使用此信息并在随机输入的图像上对其进行测试,并获得结果,如 above github page所示。
在github页面上显示的执行此操作的代码(我假设是):
import numpy as np import cv2 import tensorflow as tf
W = np.zeros((10000,2048),dtype='float32')
w = np.load('weight.npy') conv = np.load('conv_features.npy') predict
= np.load('predict_label.npy')
predict_label = (np.argmax(predict,axis=1)).astype('uint8')
#this W meas the coefficients of linear combination:10000x2048 W = np.transpose(w)[predict_label,:] W = np.expand_dims(W,axis=2)
#W and conv are too large temp1 = W[0:10] temp2 = conv[0:10]
del W del conv
sess = tf.Session()
weight = tf.placeholder(dtype='float32',shape=temp1.shape,name='weight') features = tf.placeholder(dtype='float32',shape=temp2.shape,name='features')
resize_features = tf.image.resize_bicubic(features,size=(299,299)) resize_features = tf.reshape(resize_features,[-1,299*299,2048])
#Hotmap = tf.batch_matmul(resize_features,weight) Hotmap = tf.matmul(resize_features,weight) Hotmap = tf.reshape(Hotmap,[-1,299,299])
result_map = sess.run(Hotmap,feed_dict={weight:temp1,features:temp2})
任何人都可以帮助我使用此代码并将结果显示在github存储库中所示的输入图像上吗?或与此有关的任何相关信息?