【问题标题】:How to extract feature vectors in a fine-tuned network in keras如何在 keras 的微调网络中提取特征向量
【发布时间】:2018-03-25 14:22:02
【问题描述】:

在使用新数据对 keras 上的 Inception v3 CNN 进行微调后,我正在尝试从添加的 Dense 层中提取特征向量。基本上,我加载网络结构及其权重,添加两个密集层(我的数据是针对两类问题的)并仅从网络的某些部分更新权重,如下面的代码所示:

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

# let's add a fully-connected layer
x = Dense(64, activation='relu')(x)

# and a logistic layer -- I have 2 classes only
predictions = Dense(2, activation='softmax')(x)

# this is the model to train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers

for layer in base_model.layers:
        layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

#load new training data
x_train, x_test, y_train, y_test =load_data(train_data, test_data, train_labels, test_labels)

datagen = ImageDataGenerator()      
datagen.fit(x_train)

epochs=1
batch_size=32

# train the model on the new data for a few epochs
model.fit_generator(datagen.flow(x_train, y_train,
                                 batch_size=batch_size),
                                 steps_per_epoch=x_train.shape[0] // 
                                 batch_size,
                                 epochs=epochs,
                                 validation_data=(x_test, y_test))

# at this point, the top layers are well trained and 
#I can start fine-tuning convolutional layers from inception V3. 
#I will freeze the bottom N layers and train the remaining top layers. 
#I chose to train the top 2 inception blocks, i.e. I will freeze the 
#first 249 layers and unfreeze the rest:

for layer in model.layers[:249]:
    layer.trainable = False
for layer in model.layers[249:]:
    layer.trainable = True

# I need to recompile the model for these modifications to take effect
# I use SGD with a low learning rate
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['binary_accuracy'])

# I train our model again (this time fine-tuning the top 2 inception blocks alongside the top Dense layers
model.fit_generator(datagen.flow(x_train, y_train,
                                 batch_size=batch_size),
                                 steps_per_epoch=x_train.shape[0] // 
                                 batch_size,
                                 epochs=epochs,
                                 validation_data=(x_test, y_test))

这段代码运行得很好,不是我的问题。

我的问题是,在微调了这个网络之后,我想要训练和测试数据上最后一层的输出,因为我想使用这个新网络作为特征提取器。我想要你可以在上面的代码中看到的这部分网络的输出:

x = Dense(64, activation='relu')(x)

我尝试了以下代码,但它不起作用:

 from keras import backend as K
 inputs = [K.learning_phase()] + model.inputs
 _convout1_f = K.function(inputs, model.get_layer(dense_1).output)

错误如下

 _convout1_f = K.function(inputs, model.get_layer(dense_1).output)
 NameError: global name 'dense_1' is not defined

在对新数据中的预训练网络进行微调后,如何从添加的新层中提取特征?我在这里做错了什么?

【问题讨论】:

    标签: python tensorflow deep-learning keras


    【解决方案1】:

    我用这个解决了我自己的问题。希望它也适合你。

    首先,提取特征的K.function就是这个

    _convout1_f = K.function([model.layers[0].input, K.learning_phase()],[model.layers[312].output])
    

    其中 312 是我要提取特征的第 312 层

    然后我将这个 _convout1_f 参数传递给这样的函数

        features_train, features_test=feature_vectors_generator(x_train,x_test,_convout1_f)
    

    提取这些特征的函数是这样的

    def feature_vectors_generator(x_train,x_test, _convout1_f):
    
        print('Generating Training Feature Vectors...')
    
        batch_size=100
        index=0
        if x_train.shape[0]%batch_size==0:
                max_iterations=x_train.shape[0]/batch_size
        else:
                max_iterations=(x_train.shape[0]/batch_size)+1
    
    
        for i in xrange(0, max_iterations):
    
                if(i==0):
    
                      features=_convout1_f([x_train[index:batch_size], 1])[0]
                      index=index+batch_size
                      features = numpy.squeeze(features)
                      features_train = features
    
                else:
                         if(i==max_iterations-1):
                  features=_convout1_f([x_train[index:x_train.shape[0],:], 1])[0]
                                features = numpy.squeeze(features)
                                features_train =numpy.append(features_train,features, axis=0)
    
                         else:
    
                features=_convout1_f([x_train[index:index+batch_size,:], 1])[0]
                                index=index+batch_size
                                features = numpy.squeeze(features)          
                                features_train=numpy.append(features_train,features, axis=0)
    
    
    
    print('Generating Testing Feature Vectors...')
    
    batch_size=100
        index=0
        if x_test.shape[0]%batch_size==0:
                max_iterations=x_test.shape[0]/batch_size
        else:
                max_iterations=(x_test.shape[0]/batch_size)+1
    
    
        for i in xrange(0, max_iterations):
    
                if(i==0):
            features=_convout1_f([x_test[index:batch_size], 0])[0]
                        index=index+batch_size
                        features = numpy.squeeze(features)
                        features_test = features
    
                else:
                         if(i==max_iterations-1):
                features=_convout1_f([x_test[index:x_test.shape[0],:], 0])[0]
                                features = numpy.squeeze(features)
                                features_test = numpy.append(features_test,features, axis=0)
    
                         else:
                features=_convout1_f([x_test[index:index+batch_size,:], 0])[0]
                                index=index+batch_size
                                features = numpy.squeeze(features)
                                features_test=numpy.append(features_test,features, axis=0)
    
    return(features_train, features_test)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多