前提・実現したいこと
Grad-CAM++で特徴量の可視化を行いたいと考えています。
以下のエラーの意味が分からないので、教えていただきたいです
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-17-42f01163d8b3> in <module>() 7 8 img = img_to_array(load_img(image_path,target_size=(row,col,3))) ----> 9 img_GCAMplusplus = Grad_Cam_plus_plus(model, target_layer, img, row, col) 10 time = time.ctime() 11 img_Gplusplusname = image_path+time+"_GCAM++_%s.jpg" 2 frames <ipython-input-16-5889d0d9b241> in Grad_Cam_plus_plus(input_model, layer_name, x, row, col) 18 grads =K.gradients(class_output, conv_output)[0] 19 #first_derivative:1階微分 ---> 20 first_derivative = K.exp(class_output)[0][class_idx] * grads 21 #second_derivative:2階微分 22 second_derivative = K.exp(class_output)[0][class_idx] * grads * grads /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.__traceback__) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords) 532 except ValueError as err: 533 raise ValueError( --> 534 f"Tried to convert '{input_name}' to a tensor and failed. " 535 f"Error: {err}") 536 prefix = ("Input '%s' of '%s' Op has type %s that does not match" % ValueError: Tried to convert 'y' to a tensor and failed. Error: None values not supported.
ソースコード
import pandas as pd import numpy as np import cv2 import argparse import keras import time import sys from keras import backend as K from keras.preprocessing.image import array_to_img, img_to_array, load_img from keras.applications.resnet import ResNet50 import tensorflow as tf tf.compat.v1.disable_eager_execution() K.set_learning_phase(1) def Grad_Cam_plus_plus(input_model, layer_name, x, row, col): model = input_model # 前処理 X = np.expand_dims(x, axis=0) X = X.astype('float32') preprocessed_input = X / 255.0 # 予測クラスの算出 predictions = model.predict(preprocessed_input) class_idx = np.argmax(predictions[0]) # 使用する重みの抽出、高階微分の計算 class_output = model.layers[-1].output conv_output = model.get_layer(layer_name).output grads =K.gradients(class_output, conv_output)[0] #first_derivative:1階微分 first_derivative = K.exp(class_output)[0][class_idx] * grads #second_derivative:2階微分 second_derivative = K.exp(class_output)[0][class_idx] * grads * grads #third_derivative:3階微分 third_derivative = K.exp(class_output)[0][class_idx] * grads * grads * grads #関数の定義 gradient_function = K.function([model.input], [conv_output, first_derivative, second_derivative, third_derivative]) # model.inputを入力すると、conv_outputとgradsを出力する関数 conv_output, conv_first_grad, conv_second_grad, conv_third_grad = gradient_function([preprocessed_input]) conv_output, conv_first_grad, conv_second_grad, conv_third_grad = conv_output[0], conv_first_grad[0], conv_second_grad[0], conv_third_grad[0] #alphaを求める global_sum = np.sum(conv_output.reshape((-1, conv_first_grad.shape[2])), axis=0) alpha_num = conv_second_grad alpha_denom = conv_second_grad*2.0 + conv_third_grad*global_sum.reshape((1,1,conv_first_grad.shape[2])) alpha_denom = np.where(alpha_denom!=0.0, alpha_denom, np.ones(alpha_denom.shape)) alphas = alpha_num / alpha_denom #alphaの正規化 alpha_normalization_constant = np.sum(np.sum(alphas, axis = 0), axis = 0) alpha_normalization_constant_processed = np.where(alpha_normalization_constant != 0.0, alpha_normalization_constant, np.ones(alpha_normalization_constant.shape)) alphas /= alpha_normalization_constant_processed.reshape((1,1,conv_first_grad.shape[2])) #wの計算 weights = np.maximum(conv_first_grad, 0.0) deep_linearization_weights = np.sum((weights * alphas).reshape((-1, conv_first_grad.shape[2]))) #Lの計算 grad_CAM_map = np.sum(deep_linearization_weights * conv_output, axis=2) grad_CAM_map = np.maximum(grad_CAM_map, 0) grad_CAM_map = grad_CAM_map / np.max(grad_CAM_map) #ヒートマップを描く grad_CAM_map = cv2.resize(grad_CAM_map, (row, col), cv2.INTER_LINEAR) jetcam = cv2.applyColorMap(np.uint8(255 * grad_CAM_map), cv2.COLORMAP_JET) # モノクロ画像に疑似的に色をつける jetcam = (np.float32(jetcam) + x / 2) # もとの画像に合成 return jetcam if __name__ == '__main__': model = keras.models.load_model("/content/drive/MyDrive/ji/saved_model/my_model") target_layer = 'inception_v3' image_path = '/content/drive/MyDrive/Cs-N013.jpg' row = 224 col = 224 img = img_to_array(load_img(image_path,target_size=(row,col,3))) img_GCAMplusplus = Grad_Cam_plus_plus(model, target_layer, img, row, col) time = time.ctime() img_Gplusplusname = image_path+time+"_GCAM++_%s.jpg" cv2.imwrite(img_Gplusplusname, img_GCAMplusplus) print("Completed.")
あなたの回答
tips
プレビュー