Python
1import numpy as np 2import pandas as pd 3import matplotlib.pyplot as plt 4%config InlineBackend.figure_formats = {'png', 'retina'} 5import os, zipfile, io, re 6from PIL import Image 7from sklearn.model_selection import train_test_split 8from keras.applications.xception import Xception 9from keras.models import Model, load_model 10from keras.layers.core import Dense 11from keras.layers.pooling import GlobalAveragePooling2D 12from keras.optimizers import Adam, RMSprop, SGD 13from keras.utils.np_utils import to_categorical 14from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, ReduceLROnPlateau 15from keras.preprocessing.image import ImageDataGenerator 16 17# 画像入力サイズ 18image_size=100 19# ZIP読み込み 20z = zipfile.ZipFile('./UTKFace_train.zip') 21# 画像ファイルパスのみ取得S 22imgfiles = [ x for x in z.namelist() if re.search(r"^UTKFace_train.*jpg$", x) ] 23 24X=[] 25Y=[] 26for imgfile in imgfiles: 27 # ZIPから画像読み込み 28 image = Image.open(io.BytesIO(z.read(imgfile))) 29 # RGB変換 30 image = image.convert('RGB') 31 # リサイズ 32 image = image.resize((image_size, image_size)) 33 # 画像から配列に変換 34 data = np.asarray(image) 35 file = os.path.basename(imgfile) 36 file_split = [i for i in file.split('_')] 37 X.append(data) 38 Y.append(int(file_split[0])) 39z.close() 40X = np.array(X) 41Y = np.array(Y) 42del z, imgfiles 43 44print(X.shape, Y.shape) 45>>>>(24107, 100, 100, 3) (24107,) 46 47X_train, X_test, y_train, y_test = train_test_split( 48 X, 49 Y, 50 random_state = 0, 51 test_size = 0.2 52) 53del X,Y 54print(X_train.shape, y_train.shape, X_test.shape, y_test.shape) 55>>>>(19285, 100, 100, 3) (19285,) (4822, 100, 100, 3) (4822,) 56 57# データ型の変換&正規化 58X_train = X_train.astype('float32') / 255 59X_test = X_test.astype('float32') / 255 60# trainデータからvalidデータを分割 61X_train, X_valid, y_train, y_valid = train_test_split( 62 X_train, 63 y_train, 64 random_state = 0, 65 test_size = 0.2 66) 67print(X_train.shape, y_train.shape, X_valid.shape, y_valid.shape) 68>>>>(15428, 100, 100, 3) (15428,) (3857, 100, 100, 3) (3857,) 69 70# model building 71base_model = Xception( 72 include_top = False, 73 weights = "imagenet", 74 input_shape = None 75) 76x = base_model.output 77x = GlobalAveragePooling2D()(x) 78x = Dense(1024, activation='relu')(x) 79predictions = Dense(1)(x) 80datagen = ImageDataGenerator( 81 featurewise_center = False, 82 samplewise_center = False, 83 featurewise_std_normalization = False, 84 samplewise_std_normalization = False, 85 zca_whitening = False, 86 rotation_range = 0, 87 width_shift_range = 0.1, 88 height_shift_range = 0.1, 89 horizontal_flip = True, 90 vertical_flip = False 91) 92datagen = ImageDataGenerator( 93 featurewise_center = False, 94 samplewise_center = False, 95 featurewise_std_normalization = False, 96 samplewise_std_normalization = False, 97 zca_whitening = False, 98 rotation_range = 0, 99 width_shift_range = 0.1, 100 height_shift_range = 0.1, 101 horizontal_flip = True, 102 vertical_flip = False 103) 104# RMSE 105from keras import backend as K 106def root_mean_squared_error(y_true, y_pred): 107 return K.sqrt(K.mean(K.square(y_pred - y_true), axis = -1)) 108# ネットワーク定義 109model = Model(inputs = base_model.input, outputs = predictions) 110 111#108層までfreeze 112for layer in model.layers[:108]: 113 layer.trainable = False 114 115 # Batch Normalizationのfreeze解除 116 if layer.name.startswith('batch_normalization'): 117 layer.trainable = True 118 if layer.name.endswith('bn'): 119 layer.trainable = True 120 121#109層以降、学習させる 122for layer in model.layers[108:]: 123 layer.trainable = True 124 125# layer.trainableの設定後にcompile 126model.compile( 127 optimizer = Adam(), 128 loss = root_mean_squared_error, 129) 130 131%%time 132hist = model.fit_generator( 133 datagen.flow(X_train, y_train, batch_size = 32), 134 steps_per_epoch = X_train.shape[0] // 32, 135 epochs = 50, 136 validation_data = (X_valid, y_valid), 137 callbacks = [early_stopping, reduce_lr], 138 shuffle = True, 139 verbose = 1 140) 141>>>> #Epochが 1からずら~っと並びます 142>>>>Epoch 00037: ReduceLROnPlateau reducing learning rate to 1.000000082740371e-09. 143>>>>Epoch 38/50 144>>>>482/482 [==============================] - 3221s 7s/step - loss: nan - val_loss: >>>>nan 145>>>>Epoch 00038: early stopping 146>>>>Wall time: 1d 5h 59min 46s 147 148score = model.evaluate(X_test, y_test, verbose=1) 149print("evaluate loss: {}".format(score)) 150>>>>4822/4822 [==============================] - 605s 125ms/step 151>>>>evaluate loss: nan 152
最後の出力がnanになってしまいます。途中のX_trainやX_testのshapeやloss, val_lossの値を見てもちゃんとデータはとってこれていそうなのにnanが出てしまいます。さすがにもう一度計算を回すのは30時間かかって骨が折れるのでやりたくはないです。
どうぞ、改善方法がお分かりになる方はお願いいたします(o_ _)o))
あなたの回答
tips
プレビュー