Extracting last layers of keras model as a submodel

前端 未结 3 1393
不知归路
不知归路 2021-02-06 08:37

Say we have a convolutional neural network M. I can extract features from images by using

extractor = Model(M.inputs, M.get_layer(\'last_conv\').output)
feat         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 09:23

    It depends on what you want to do.

    • If you are going to throw away the feature extractor afterwards
    • If you plan on training the feature extractor later

    If you are going to use the extracted features but you don't intend on training the model used to generate them, you could use the predict method to get the features as you did:

    features = extractor.predict(X)
    

    then save its output to a file (np.save or cPickle or whatever). After that you could use that new dataset as the input to a new model.

    If you plan on training the feature extractor later you'll need to stack the two networks as seen here with vgg as feature extractor https://github.com/fchollet/keras/issues/4576:

    img_width, img_height = 150, 150
    vgg16_model = VGG16(include_top=False, weights='imagenet')
    
    input = Input(batch_shape=vgg16_model.output_shape)
    x = GlobalAveragePooling2D()(input)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.5)(x)
    predict = Dense(1, activation='sigmoid')(x)
    top_model = Model(input, predict)
    
    top_model.load_weights(os.path.join(data_path, 'VGG16Classifier.hdf5'))  
    
    input = Input(shape=(3, img_width, img_height))
    x = vgg16_model(input)
    predict = top_model(x)
    model = Model(input, predict)
    

    PS: This example uses channels first ordering. If you are using tensorflow you should change the shape to shape=(img_width, img_height,3 )

提交回复
热议问题