Feature extraction for actual combat in pre-trained networks

        Recently, I am doing a graduation project, which involves the use of pre-training network content. There are many online resources in this area, but there are some code problems. After some attempts, this white person has successfully completed this function. The main use of keras Library, for keras library installation, just use pip inatall keras in the command line window. Send out my code here, and hope to make progress together with fellow students! Xiaobai's code is more scum ...

# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 15:30:38 2018

@author: 13260
"""

"""
功能:提取VGG19卷积层特征并保存
"""
#from keras.applications.resnet50 import ResNet50
from keras.applications.densenet import DenseNet201,preprocess_input
from keras.preprocessing import image
#from keras.applications.resnet50 import preprocess_input
from keras.models import Model
from sklearn.cluster import MiniBatchKMeans
from keras.utils import plot_model
from sklearn import preprocessing
import numpy as np
import os

"""
""
Output: All picture paths
Input: Database path
Function: Read all picture paths
def read_all_imgs (data_dir):
    catalogs = os.listdir (data_dir) 
    imgSet = [] 
    for i in range (len (catalogs)): 
        catalog_path = data_dir + catalogs [i] 
        for img in os.listdir (catalog_path): 
            #imgs = [catalog_path + "/" + img for img in catalog_path] 
            img_path = catalog_path + "/" + img 
            imgSet.append (img_path) 
        #print (len (imgSet)) 
    print ("[INFO] There are" + str (len (imgSet)) + "images ...... ") 
    return imgSet 
    
" "" 
function: convolution layer corresponding to the image feature extraction model 
inputs: 1, CNN model 2, image path 
outputs: an image feature corresponding to 
"" " 
DEF extract_conv_feature (model, img_path):img_to_array(img)
    x = np.expand_dims(x, axis=0)
    img = image.load_img(img_path)
    x = image.img_to_array (img) 
    x = preprocess_input (x) 
    conv_features = model.predict (x) 
    #print ("[INFO] Feature has been extracted!") 
    return conv_features 
"" " 
Function: read convolution layer feature files 
Input: feature file path 
Output: feature 
"" " 
def load_conv_feature (feature_path): 
    conv_feature = np.load (feature_path) 
    return conv_feature 
" "" 
Function: extract local features in a convolution feature map 
Input: Convolutional layer features of an image, Form (samples, rows, cols, filters) 
output: collection of image convolution feature vectors, form (rows * cols, filters) 
"" " 
def flatten_conv_feature (conv_feature): 
    num_c = conv_feature.shape [2] 
    num_r = conv_feature.shape[1]
    tmp_feature = []
    for i in range (num_c):
        flatten_feature = flatten_conv_feature(conv_feature)
        for j in range(num_r):
            tmp_feature.append (conv_feature [0, i, j,:]. flatten ()) 
    tmp_feature = np.array (tmp_feature) 
    return tmp_feature 
"" " 
Function: extract the convolutional layer features of all images (expand) 
input: image path 
output : All image convolution layer features 
"" " 
def get_all_local_feature (model, imgSet): 
    all_feature = [] 
    max_num = len (imgSet) 
    for i in range (max_num): 
        conv_feature = extract_conv_feature (model, imgSet [i]) 
        if i % 500 == 0: 
            print ("[INFO]" + str (i) + "images'conv_feature has been extracted!") 
        #Flatten_feature = flatten_conv_feature (conv_feature) 
        all_feature.append (flatten_feature) 
    all_feature = np.array(all_feature)
    print("[INFO] All conv_feature has been extracted !")
    return all_feature
"""
功能:提取所有图像的卷积层特征(未展开)
"""
def get_no_flatten_feature(model,imgSet):
    all_feature = []
    max_num = len(imgSet)
    for i in range(max_num):
        conv_feature = extract_conv_feature(model,imgSet[i])
        if i % 500 == 0:
            print("[INFO]" + str(i) + " images'conv_feature has been extracted !")
        all_feature.append(conv_feature)
    all_feature = np.array(all_feature)
    print("[INFO] All conv_feature has been extracted !")
    return all_feature

def save_feature (feature, path): 
    np.save(path,feature)
    print ("[INFO] File has been saved!") 

def main (): 
    img_dir = "F: / shiyan / TensorFlow / retrain / data / train /" 
    #Image data path imgSet = read_all_imgs (img_dir) 
    # If extracting convolutional layer features, the model include_top attribute is set to False, if extracting fully connected layer features, include_top attribute is set to True 
    # Load DenseNet201 pre-trained model, if you need to use another model, you can directly replace the model , No other code needs to move 
    base_model = DenseNet201 (weights = 'imagenet', include_top = False) 
    #base_model = ResNet50 (weights = 'imagenet', include_top = False) 
    # According to the name of each layer of the network in the keras network structure, Use get_layer (network layer name) .output directly to get the output result of the corresponding layer 
    # If you don't know the network layer name, you can use model.summery () to print each layer of the 
    model. Model = Model (inputs = base_model. input, outputs = base_model.get_layer ('conv5_block32_concat'). output) 
    = Model (inputs = base_model.input, outputs = base_model.get_layer ('block5_conv3'). output) 
    print ("[INFO] Model has been loaded!") 
    "" "Unexpanded" ""
    all_conv_feature = get_no_flatten_feature(model,imgSet)
    print(all_conv_feature.shape)
    path = "F:/python/features/DenseNet/DenseNet201_conv_feature.npy"
    save_feature(all_conv_feature,path)
    
if __name__ == "__main__":
    main()
    
    
Published 50 original articles · Likes5 · Visits 20,000+

Guess you like

Origin blog.csdn.net/qq_31207499/article/details/88635079