質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Q&A

0回答

734閲覧

エラーの意味が分からない

pan_p

総合スコア3

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

0グッド

0クリップ

投稿2021/12/08 02:43

前提・実現したいこと

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.")

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aokikenichi

2021/12/08 11:16

yにNullが含まれてませんか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問