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
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)
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)
In [5]:
print('Done !')
In [ ]: