2. 特征提取_InceptionResNetV2

导入包


In [1]:
import h5py
import os
import time
import numpy as np

from keras.layers import *
from keras.models import *
from keras.applications import *
from keras.optimizers import *
from keras.regularizers import *
from keras.preprocessing.image import *
from keras.applications.inception_v3 import preprocess_input


Using TensorFlow backend.

使用预训练权重的InceptionResNetV2模型提取特征


In [2]:
from keras.models import load_model
def get_features(h5_file_path, image_size, date_str, lambda_func=None, batch_size=1):
#     print('{0} start.'.format(MODEL.__name__))
    start_time = time.time()
    
    width = image_size[0]
    height = image_size[1]
    input_tensor = Input((height, width, 3))
    x = input_tensor
    if lambda_func:
        print(lambda_func.__name__)
        x = Lambda(lambda_func)(x)
        
    print(h5_file_path)
    base_model = load_model(h5_file_path)
    
    model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))

    cwd = os.getcwd()
    data_train_path = os.path.join(cwd, 'input', 'data_train')
    data_test_path  = os.path.join(cwd, 'input', 'data_test')
    
#     gen = ImageDataGenerator()
    gen = ImageDataGenerator(zoom_range = 0.1,
                            height_shift_range = 0.1,
                            width_shift_range = 0.1,
                            rotation_range = 10)
    train_generator = gen.flow_from_directory(data_train_path, image_size, shuffle=False, 
                                              batch_size=batch_size)
    test_generator  = gen.flow_from_directory(data_test_path,  image_size, shuffle=False, 
                                              batch_size=batch_size)
    
    print(len(train_generator.filenames))
    print(len(test_generator.filenames))
    print('train_generator')
    train = model.predict_generator(train_generator, verbose=1, steps=len(train_generator.filenames))
    print('test_generator')
    test = model.predict_generator(test_generator, verbose=1, steps=len(test_generator.filenames))
    
#     print('train_generator')
#     train = model.predict_generator(train_generator, verbose=1, steps=10)
#     print('test_generator')
#     test = model.predict_generator(test_generator, verbose=1, steps=10)

    folder_path = os.path.join(cwd, 'model')
    if not os.path.exists(folder_path):
        os.mkdir(folder_path)
    file_name = os.path.join(cwd, 'model', 'feature_{0}_{1}.h5'.format('InceptionResNetV2', date_str))
    print(file_name)
    if os.path.exists(file_name):
        os.remove(file_name)
        
    with h5py.File(file_name) as h:
        h.create_dataset("train", data=train)
        h.create_dataset("train_labels", data=train_generator.classes)
        h.create_dataset("test", data=test)
    
    print(train.shape)
    print(train_generator.classes)
    print(test.shape)
    
    end_time = time.time()
    print('Spend time: {0} s'.format(end_time-start_time))

In [3]:
# Get date str
date_str = time.strftime("%Y%m%d", time.localtime())
print(date_str)


20171028

In [4]:
cwd = os.getcwd()
path = os.path.join(cwd, 'model', 'InceptionResNetV2_notop.h5')
get_features(path, (299, 299), date_str, inception_v3.preprocess_input)


preprocess_input
E:\Udacity\MachineLearning(Advanced)\p6_graduation_project\model\InceptionResNetV2_notop.h5
C:\ProgramData\Anaconda3\envs\gpu\lib\site-packages\keras\models.py:251: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.
  warnings.warn('No training configuration found in save file: '
Found 10222 images belonging to 120 classes.
Found 10357 images belonging to 1 classes.
10222
10357
train_generator
10222/10222 [==============================] - 1171s  
test_generator
10357/10357 [==============================] - 1172s  
E:\Udacity\MachineLearning(Advanced)\p6_graduation_project\model\feature_InceptionResNetV2_20171028.h5
(10222, 1536)
[  0   0   0 ..., 119 119 119]
(10357, 1536)
Spend time: 2402.112186193466 s

In [5]:
print('Done !')


Done !

In [ ]: