kerasでの画像認識が終了しない
https://employment.en-japan.com/engineerhub/entry/2017/04/28/110000
を拝見し,実際にソース,使用した画像をそのまま流用して実行しました.
しかし,1エポックが終了する時になっても次のエポックに移行ない問題が発生しました.
CPUなどを見ても動作してるようには思えるのですが一向に移行しません.
考えられる問題などは何がありますか?
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D, AveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler, ReduceLROnPlateau
from keras.optimizers import SGD
from keras.regularizers import l2
import matplotlib.image as mpimg
from scipy.misc import imresize
import numpy as np
import keras.backend as K
import math
K.clear_session()
img_size=299
#訓練データ拡張
train_datagen = ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=False,
zoom_range=[.8, 1],
channel_shift_range=30,
fill_mode='reflect')
test_datagen = ImageDataGenerator()
#画像の読み込み
def load_images(root,nb_img):
all_imgs = []
all_classes = []
for i in range(nb_img):
img_name = "%s/dog.%d.jpg" % (root, i + 1)
img_arr = mpimg.imread(img_name)
resize_img_ar = imresize(img_arr, (img_size, img_size))
all_imgs.append(resize_img_ar)
all_classes.append(0)
for i in range(nb_img):
img_name = "%s/cat.%d.jpg" % (root, i + 1)
img_arr = mpimg.imread(img_name)
resize_img_ar = imresize(img_arr, (img_size, img_size))
all_imgs.append(resize_img_ar)
all_classes.append(1)
return np.array(all_imgs), np.array(all_classes)
X_train, y_train = load_images('./train', 1000)
X_test, y_test = load_images('./train', 400)
train_generator = train_datagen.flow(X_train, y_train, batch_size=64, seed = 13)
test_generator = test_datagen.flow(X_test, y_test, batch_size=64, seed = 13)
#Inception v3モデルの読み込み。最終層は読み込まない
base_model = InceptionV3(weights='imagenet', include_top=False)
#最終層の設定
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(1, kernel_initializer="glorot_uniform", activation="sigmoid", kernel_regularizer=l2(.0005))(x)
model = Model(inputs=base_model.input, outputs=predictions)
#base_modelはweightsを更新しない
for layer in base_model.layers:
layer.trainable = False
opt = SGD(lr=.01, momentum=.9)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics= ['accuracy'])
checkpointer = ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.hdf5', verbose=1, save_best_only=True)
csv_logger = CSVLogger('model.log')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=5, min_lr=0.001)
history = model.fit_generator(train_generator,
steps_per_epoch=2000,
epochs=10,
validation_data=test_generator,
validation_steps=800,
verbose=1,
callbacks=[reduce_lr, csv_logger, checkpointer])
追記
1エポックが終了する時と判断したのは,次のように出力された状態で止まるためです.
ここから一向に処理が進まないにも関わらず(1エポック終了時に作成するhdf5ファイルなども作成されない),CPUなどはしっかり動いています.
Epoch 1/2
1/5 [=====>........................] - ETA: 264s - loss: 0.7831 - acc: 0.6094
2/5 [===========>..................] - ETA: 154s - loss: 0.7622 - acc: 0.5859
3/5 [=================>............] - ETA: 85s - loss: 0.7396 - acc: 0.5729
4/5 [=======================>......] - ETA: 38s - loss: 0.7270 - acc: 0.5703