CNNで学習したモデルの中間層を用いて特徴量を得ようと考えています
そこで特徴量を得るためのコードを組んだのですが、実行はできるのですが、特徴量を得る際長文のWARNINGが出てしまします。WEBで調べても解決法が見つからなったのでわかる方がいらしたら教えてください
以下のdef create_imageのところで異常が起きているんだと思います。
以下が実際のコードです。
# -*- coding: utf-8 -* import glob import cv2 import numpy as np import time from tqdm import tqdm from skimage import feature,exposure from keras.models import model_from_json from keras.utils.vis_utils import plot_model from keras.models import Model import pandas as pd from matplotlib import pylab as plt from sklearn.manifold import TSNE from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import MinMaxScaler # 特徴量抽出 def create_images_array(load_img_paths,model,layer_name): imgs=[] for load_img_path in tqdm(load_img_paths): middle_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) img = cv2.imread(load_img_path) #型を合わせる。255は正規化のため。 target = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2])).astype('float') / 255.0 middle_output = middle_layer_model.predict(target) imgs.append(middle_output) return np.array(imgs, np.float32) def main(): t1 = time.time() json_string = open('./preprocess_images_keras/case3/model.json').read() model = model_from_json(json_string) model.load_weights('./preprocess_images_keras/case3/weight.hdf5') layer_name = 'conv5_block3_out' # 学習用の画像ファイルの格納先 LOAD_TRAIN_IMG1S_PATH = './preprocess_images_kears/case3/train/T1-FE/*' LOAD_TRAIN_IMG2S_PATH = './preprocess_images_keras/case3/train/T2-FE/*' LOAD_TRAIN_IMG3S_PATH = './preprocess_images_keras/case3/train/T3-FE/*' LOAD_TRAIN_IMG4S_PATH = './preprocess_images_keras/case3/train/T4-FE/*' LOAD_TRAIN_IMG5S_PATH = './preprocess_images_keras/case3/train/T5-FE/*' LOAD_TRAIN_IMG6S_PATH = './preprocess_images_keras/case3/train/T6-FE/*' LOAD_TRAIN_IMG7S_PATH = './preprocess_images_keras/case3/train/T7-FE/*' LOAD_TRAIN_IMG8S_PATH = './preprocess_images_keras/case3/train/T8-FE/*' LOAD_TRAIN_IMG9S_PATH = './preprocess_images_keras/case3/train/T1-W/*' LOAD_TRAIN_IMG10S_PATH = './preprocess_images_keras/case3/train/T2-W/*' LOAD_TRAIN_IMG11S_PATH = './preprocess_images_keras/case3/train/T3-W/*' LOAD_TRAIN_IMG12S_PATH = './preprocess_images_keras/case3/train/T4-W/*' LOAD_TRAIN_IMG13S_PATH = './preprocess_images_keras/case3/train/T5-W/*' LOAD_TRAIN_IMG14S_PATH = './preprocess_images_keras/case3/train/T6-W/*' LOAD_TRAIN_IMG15S_PATH = './preprocess_images_keras/case3/train/T7-W/*' LOAD_TRAIN_IMG16S_PATH = './preprocess_images_keras/case3/train/T8-W/*' # 学習用の画像ファイルのパスを取得 load_img1_paths = glob.glob(LOAD_TRAIN_IMG1S_PATH) load_img2_paths = glob.glob(LOAD_TRAIN_IMG2S_PATH) load_img3_paths = glob.glob(LOAD_TRAIN_IMG3S_PATH) load_img4_paths = glob.glob(LOAD_TRAIN_IMG4S_PATH) load_img5_paths = glob.glob(LOAD_TRAIN_IMG5S_PATH) load_img6_paths = glob.glob(LOAD_TRAIN_IMG6S_PATH) load_img7_paths = glob.glob(LOAD_TRAIN_IMG7S_PATH) load_img8_paths = glob.glob(LOAD_TRAIN_IMG8S_PATH) load_img9_paths = glob.glob(LOAD_TRAIN_IMG9S_PATH) load_img10_paths = glob.glob(LOAD_TRAIN_IMG10S_PATH) load_img11_paths = glob.glob(LOAD_TRAIN_IMG11S_PATH) load_img12_paths = glob.glob(LOAD_TRAIN_IMG12S_PATH) load_img13_paths = glob.glob(LOAD_TRAIN_IMG13S_PATH) load_img14_paths = glob.glob(LOAD_TRAIN_IMG14S_PATH) load_img15_paths = glob.glob(LOAD_TRAIN_IMG15S_PATH) load_img16_paths = glob.glob(LOAD_TRAIN_IMG16S_PATH) # 学習用の画像ファイルをロードし特徴量抽出 imgs1 = create_images_array(load_img1_paths,model,layer_name) imgs2 = create_images_array(load_img2_paths,model,layer_name) imgs3 = create_images_array(load_img3_paths,model,layer_name) imgs4 = create_images_array(load_img4_paths,model,layer_name) imgs5 = create_images_array(load_img5_paths,model,layer_name) imgs6 = create_images_array(load_img6_paths,model,layer_name) imgs7 = create_images_array(load_img7_paths,model,layer_name) imgs8 = create_images_array(load_img8_paths,model,layer_name) imgs9 = create_images_array(load_img9_paths,model,layer_name) imgs10 = create_images_array(load_img10_paths,model,layer_name) imgs11 = create_images_array(load_img11_paths,model,layer_name) imgs12 = create_images_array(load_img12_paths,model,layer_name) imgs13 = create_images_array(load_img13_paths,model,layer_name) imgs14 = create_images_array(load_img14_paths,model,layer_name) imgs15 = create_images_array(load_img15_paths,model,layer_name) imgs16 = create_images_array(load_img16_paths,model,layer_name) X = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8, imgs9, imgs10, imgs11, imgs12, imgs13, imgs14, imgs15, imgs16] # 正解ラベルを生成imgs.番号でラベルを決める labels1 = np.full(len(load_img1_paths), 1, np.int32) labels2 = np.full(len(load_img2_paths), 2, np.int32) labels3 = np.full(len(load_img3_paths), 3, np.int32) labels4 = np.full(len(load_img4_paths), 4, np.int32) labels5 = np.full(len(load_img5_paths), 5, np.int32) labels6 = np.full(len(load_img6_paths), 6, np.int32) labels7 = np.full(len(load_img7_paths), 7, np.int32) labels8 = np.full(len(load_img8_paths), 8, np.int32) labels9 = np.full(len(load_img9_paths), 1, np.int32) labels10 = np.full(len(load_img10_paths), 2, np.int32) labels11 = np.full(len(load_img11_paths), 3, np.int32) labels12 = np.full(len(load_img12_paths), 4, np.int32) labels13 = np.full(len(load_img13_paths), 5, np.int32) labels14 = np.full(len(load_img14_paths), 6, np.int32) labels15 = np.full(len(load_img15_paths), 7, np.int32) labels16 = np.full(len(load_img16_paths), 8, np.int32) label = np.r_[labels1, labels2, labels3, labels4, labels5, labels6, labels7, labels8, labels9, labels10, labels11, labels12, labels13, labels14, labels15, labels16] t2 = time.time() elapsed_time = (t2-t1)/3600 print(f"経過時間:{elapsed_time}") if __name__ == '__main__': main()
以下のようなWARNINGがでます。以下の%が進むたびにこのWARNINGが表示されます
79%|███████▊ | 264/336 [16:26<02:48, 2.34s/it]WARNING:tensorflow:11 out of the last 11 calls to <function Model.make_predict_function.<locals>.predict_function at 0x0000029486376B88> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. Fo r (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.
上記のエラーが起きないようにする解決方法と、それが出てしまうならこのままでも問題ないのかを知りたいです。
宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。