前提・実現したいこと
EfficientNetをファインチューニングしてモデルを作り、Grad-CAMによって可視化しようとしています。grad_cam.pyを実行時、ファインチューニングの際自作のoptimizerを用いていて、モデルの読み込み時にエラーが発生しています。エラー箇所に対してエラー文の意味が理解できない状態です。SGD等既存のものを使っている場合は正常に動作します。以下サイトを参考にしています。よろしくお願いします。
リンク内容
リンク内容
ファインチューニングの際のソースコードtrain.pyも変更箇所を掲載します。
発生している問題・エラーメッセージ
Using TensorFlow backend. 2020-09-07 09:10:43.135048: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.135084: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2020-09-07 09:10:43.754234: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1 2020-09-07 09:10:43.775738: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:982] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-09-07 09:10:43.776617: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: pciBusID: 0000:01:00.0 name: TITAN RTX computeCapability: 7.5 coreClock: 1.77GHz coreCount: 72 deviceMemorySize: 23.62GiB deviceMemoryBandwidth: 625.94GiB/s 2020-09-07 09:10:43.776682: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.776723: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcublas.so.10'; dlerror: libcublas.so.10: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.776762: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcufft.so.10'; dlerror: libcufft.so.10: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.776800: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcurand.so.10'; dlerror: libcurand.so.10: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.776838: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.776874: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcusparse.so.10'; dlerror: libcusparse.so.10: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.0/lib64::/usr/local/lib 2020-09-07 09:10:43.778640: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7 2020-09-07 09:10:43.778648: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2020-09-07 09:10:43.778873: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2020-09-07 09:10:43.801388: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3600000000 Hz 2020-09-07 09:10:43.802700: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5593f39cfe30 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-09-07 09:10:43.802711: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2020-09-07 09:10:43.803688: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix: 2020-09-07 09:10:43.803695: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263] Traceback (most recent call last): File "/home/コンピュータ名/デスクトップ/sakagawa/grad_cam_0831.py", line 19, in <module> from adam_lr_mult import Adam_lr_mult File "/home/コンピュータ名/デスクトップ/sakagawa/adam_lr_mult.py", line 125, in <module> adam_with_lr_multipliers = Adam_lr_mult(multipliers=learning_rate_multipliers) File "/home/コンピュータ名/デスクトップ/sakagawa/adam_lr_mult.py", line 38, in __init__ self.lr = K.variable(lr, name='lr') AttributeError: can't set attribute
該当のソースコード
python
1#grad_cam.py 2 3import efficientnet.keras 4import numpy as np 5import cv2 6 7# 画像用 8from keras.preprocessing.image import array_to_img, img_to_array, load_img 9# モデル読み込み用 10from keras.models import load_model 11# Grad−CAM計算用 12from tensorflow.keras import models 13import tensorflow as tf 14from adam_lr_mult import Adam_lr_mult 15 16IMAGE_SIZE = (224, 224) 17 18def grad_cam(input_model, x, layer_name): 19 """ 20 Args: 21 input_model(object): モデルオブジェクト 22 x(ndarray): 画像 23 layer_name(string): 畳み込み層の名前 24 Returns: 25 output_image(ndarray): 元の画像に色付けした画像 26 """ 27 28 # 画像の前処理 29 # 読み込む画像が1枚なため、次元を増やしておかないとmode.predictが出来ない 30 X = np.expand_dims(x, axis=0) 31 preprocessed_input = X.astype('float32') / 255.0 32 33 grad_model = models.Model([input_model.inputs], [input_model.get_layer(layer_name).output, input_model.output]) 34 35 with tf.GradientTape() as tape: 36 conv_outputs, predictions = grad_model(preprocessed_input) 37 class_idx = np.argmax(predictions[0]) 38 loss = predictions[:, class_idx] 39 40 # 勾配を計算 41 output = conv_outputs[0] 42 grads = tape.gradient(loss, conv_outputs)[0] 43 44 gate_f = tf.cast(output > 0, 'float32') 45 gate_r = tf.cast(grads > 0, 'float32') 46 47 guided_grads = gate_f * gate_r * grads 48 49 # 重みを平均化して、レイヤーの出力に乗じる 50 weights = np.mean(guided_grads, axis=(0, 1)) 51 cam = np.dot(output, weights) 52 53 # 画像を元画像と同じ大きさにスケーリング 54 cam = cv2.resize(cam, IMAGE_SIZE, cv2.INTER_LINEAR) 55 # ReLUの代わり 56 cam = np.maximum(cam, 0) 57 # ヒートマップを計算 58 heatmap = cam / cam.max() 59 60 # モノクロ画像に疑似的に色をつける 61 jet_cam = cv2.applyColorMap(np.uint8(255.0*heatmap), cv2.COLORMAP_JET) 62 # RGBに変換 63 rgb_cam = cv2.cvtColor(jet_cam, cv2.COLOR_BGR2RGB) 64 # もとの画像に合成 65 output_image = (np.float32(rgb_cam) + x / 2) 66 67 return output_image 68 69model_path = '/home/ho2869/デスクトップ/sakagawa/effB0_auto.h5' 70image_path = '/home/ho2869/デスクトップ/sakagawa/1.jpg' 71 72model = load_model(model_path, custom_objects={'Adam_lr_mult': Adam_lr_mult}) 73x = img_to_array(load_img(image_path, target_size=IMAGE_SIZE)) 74 75array_to_img(x) 76 77target_layer = 'top_conv' 78cam = grad_cam(model, x, target_layer) 79 80array_to_img(cam) 81cam = cv2.resize(cam, (640, 480)) 82cv2.imwrite("grad_cam1.jpg",cam)
python
1#train.py 2 3import efficientnet.keras as efn #記事のままではefficientnetがインポートできなかったため書き方を変えた 4from keras.layers import GlobalAveragePooling2D, Dense, Dropout 5from keras.models import Model 6from data_loader import DataLoader 7from adam_lr_mult import Adam_lr_mult 8 9def prepare_new_model(input_shape, class_count): 10 # 学習済みモデルの取り出し 11 feature_extractor = efn.EfficientNetB0(input_shape=input_shape, weights='imagenet', include_top=False) #インポートに合わせて 12 # 犬猫分類器を引っ付ける 13 x = feature_extractor.output 14 x = GlobalAveragePooling2D()(x) 15 x = Dense(512, activation='relu')(x) 16 x = Dropout(rate=0.25)(x) 17 x = Dense(class_count, activation='sigmoid')(x) 18 # 新たなモデルの定義 19 model = Model(inputs=feature_extractor.input, outputs=x) 20 print(model.summary()) 21 return model
補足情報(FW/ツールのバージョンなど)
grad_cam.pyを実行した環境は
ubuntu16.04LTS
TensorFlow2.3.0
Keras2.3.1
ファインチューニングのためのtrain.pyを実行した環境は
ubuntu16.04LTS
TensorFlow1.14.0
Keras2.2.4
になります。
あなたの回答
tips
プレビュー