前提・実現したいこと
pythonで処理中にエラーメッセージなしで強制終了してしまう原因を突き止めたい
numpyの処理付近が原因かもしれません。
ご教示いただけると幸いです。
該当のソースコード
python
1#おそらく強制終了が出る原因があるコード 2img_path = 'dataset6/test/train' 3img_list = os.listdir(img_path) 4 5x_train_img = [] 6 7for i in img_list: 8 abs_path = os.path.join(img_path, i) 9 temp = load_img(abs_path) 10 temp = img_to_array(temp) 11 x_train_img.append(temp) 12 13x_train_img = np.array(x_train_img) 14 15 16batch_size = 1 17 18step = int(x_train_img.shape[0] / batch_size) 19print(step) 20 21 22y_train_img_dummy = [] 23 24for i in range(step): 25 26 x_temp = x_train_img[batch_size*i:batch_size*(i+1)] 27 28 x_temp = x_temp / 255. 29 30 temp = teacher_model.predict(x_temp) 31 32 y_train_img_dummy.extend(temp) 33 34 35y_train_img_dummy = np.array(y_train_img_dummy) 36print(y_train_img_dummy) 37 38threhold = 0.75 39y_train_img_dummy_th = y_train_img_dummy[np.max(y_train_img_dummy, axis=1) > threhold] 40x_train_img_th = x_train_img[np.max(y_train_img_dummy, axis=1) > threhold] 41 42#コード全文 43from keras import layers 44from keras import models 45from keras import optimizers 46from keras.layers import GlobalAveragePooling2D 47import numpy as np 48import pandas as pd 49import os 50import glob 51import matplotlib.pyplot as plt 52import seaborn as sns 53from PIL import Image 54from efficientnet.keras import EfficientNetB7 55#from efficientnet.keras import EfficientNetL2 56from keras.utils import to_categorical 57from keras.layers import Input, Dense, Reshape 58from keras.models import Model 59from keras.callbacks import ModelCheckpoint 60from keras.models import load_model 61from keras.preprocessing.image import ImageDataGenerator 62from keras.preprocessing.image import load_img, save_img, img_to_array, array_to_img 63 64inputs1 = Input(shape=(200, 200, 3)) 65efficient_netB7 = EfficientNetB7(weights='noisy-student',include_top=False,input_tensor = inputs1,pooling='max') 66for layer in efficient_netB7.layers: 67 layer.trainable = True 68x = efficient_netB7.output 69x = layers.Dense(32, activation='relu')(x) 70output1 = layers.Dense(1, activation='sigmoid')(x) 71 72teacher_model = Model(inputs=inputs1, outputs=output1) 73 74teacher_model.compile(loss='categorical_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) 75 76teacher_model.summary() 77 78inputs2 = Input(shape=(200, 200, 3)) 79efficient_netL2 = EfficientNetB7(weights='noisy-student',include_top=False,input_tensor = inputs2,pooling='max') 80for layer in efficient_netL2.layers: 81 layer.trainable = True 82x = efficient_netL2.output 83x = layers.Dense(32, activation='relu')(x) 84output2 = layers.Dense(1, activation='sigmoid')(x) 85 86student_model = Model(inputs=inputs2, outputs=output2) 87 88student_model.compile(loss='categorical_crossentropy',optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) 89 90student_model.summary() 91 92train_dir = 'dataset6/train' 93validation_dir = 'dataset6/validation' 94 95# rescaled by 1./255 96train_datagen = ImageDataGenerator(rescale=1./255) 97test_datagen = ImageDataGenerator(rescale=1./255) 98 99train_generator = train_datagen.flow_from_directory(train_dir,target_size=(200, 200),batch_size=10,class_mode='categorical') 100 101validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(200, 200),batch_size=10,class_mode='categorical') 102 103#history1 = teacher_model.fit_generator(train_generator,steps_per_epoch=100,epochs=1,validation_data=validation_generator,validation_steps=30) 104 105teacher_model.save('teacher_model.h5') 106 107#history2 = student_model.fit_generator(train_generator,steps_per_epoch=100,epochs=1,validation_data=validation_generator,validation_steps=30) 108 109student_model.save('teacher_model.h5') 110 111img_path = 'dataset6/test/train' 112img_list = os.listdir(img_path) 113 114x_train_img = [] 115 116for i in img_list: 117 abs_path = os.path.join(img_path, i) 118 temp = load_img(abs_path) 119 temp = img_to_array(temp) 120 x_train_img.append(temp) 121 122x_train_img = np.array(x_train_img) 123 124#バッチサイズの設定 125batch_size = 1 126#何ステップfor文を回すか 127step = int(x_train_img.shape[0] / batch_size) 128print(step) 129 130#疑似ラベルのための空リスト 131y_train_img_dummy = [] 132 133for i in range(step): 134 #バッチサイズ分画像データを取り出す 135 x_temp = x_train_img[batch_size*i:batch_size*(i+1)] 136 #正規化 137 x_temp = x_temp / 255. 138 #推論 139 temp = teacher_model.predict(x_temp) 140 #空リストへ追加 141 y_train_img_dummy.extend(temp) 142 143#リストをnumpy配列へ 144y_train_img_dummy = np.array(y_train_img_dummy) 145print(y_train_img_dummy) 146#閾値決め 147threhold = 0.75 148y_train_img_dummy_th = y_train_img_dummy[np.max(y_train_img_dummy, axis=1) > threhold] 149x_train_img_th = x_train_img[np.max(y_train_img_dummy, axis=1) > threhold] 150 151#onehot vectorから分類のインデックスにする 152y_student_all_dummy_label = np.argmax(y_train_img_dummy_th, axis=1) 153 154#疑似ラベルの各クラスの数をカウント 155u, counts = np.unique(y_student_all_dummy_label, return_counts=True) 156print(u, counts) 157 158#カウント数の最大値を計算 159student_label_max = max(counts) 160 161#numpy配列を各ラベル毎に分ける 162y_student_per_label = [] 163y_student_per_img_path = [] 164 165for i in range(10): 166 temp_l = y_train_img_dummy_th[y_student_all_dummy_label == i] 167 print(i, ":", temp_l.shape) 168 y_student_per_label.append(temp_l) 169 temp_i = x_train_img_th[y_student_all_dummy_label == i] 170 print(i, ":", temp_i.shape) 171 y_student_per_img_path.append(temp_i) 172 173#それぞれのラベルで最大のカウント数になるようにデータをコピー 174y_student_per_label_add = [] 175y_student_per_img_add = [] 176 177for i in range(10): 178 num = y_student_per_label[i].shape[0] 179 temp_l = y_student_per_label[i] 180 temp_i = y_student_per_img_path[i] 181 add_num = student_label_max - num 182 q, mod = divmod(add_num, num) 183 print(q, mod) 184 temp_l_tile = np.tile(temp_l, (q+1, 1)) 185 temp_i_tile = np.tile(temp_i, (q+1, 1, 1, 1)) 186 temp_l_add = temp_l[:mod] 187 temp_i_add = temp_i[:mod] 188 y_student_per_label_add.append(np.concatenate([temp_l_tile, temp_l_add], axis=0)) 189 y_student_per_img_add.append(np.concatenate([temp_i_tile, temp_i_add], axis=0)) 190 191#それぞれラベルのカウント数を確認 192print([len(i) for i in y_student_per_label_add]) 193 194#ラベルごとのデータを合体させる 195student_train_img = np.concatenate(y_student_per_img_add, axis=0) 196student_train_label = np.concatenate(y_student_per_label_add, axis=0) 197 198#元のcifar10のnumpy配列と合体 199x_train_student = np.concatenate([x_train_10, student_train_img], axis=0) 200y_train_student = np.concatenate([y_train_10, student_train_label], axis=0) 201
気になること
メモリ不足の可能性あるかもしれません。