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

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

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

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

2130閲覧

出力値をmatplotlibとnumpyよって3Dグラフを作成方法

satoUmino

総合スコア19

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/07/01 20:31

編集2020/07/15 07:46

プログラミ内で計算される224×224の行列式を3Dグラフ化したい

現在、Grad-CAMというものが画像を生成する時に変え合わせる数値を確認していたのですがこれを3Dグラフ化して確認したいと考えています。数値の出力はうまくいったのですが、その値(行列式)を用いてグラフ化する方法が調べてもよくわかりません。
下記コードの”heatmap”という関数をグラフ化し、x軸、z軸を1から224、y軸を計算される値にしたいです。
参考にしているコードの説明
コードDLリンク
grad-cam_5category.pyというプログラムです。
3Dマップで出したいのは下記のコードのheatmap部分です
一番下に全体のコードを載せておきます。
printした際、普通にheatmapを出力した時は1列224行でしか出なかったのでfor文で224回繰り返すことで224列分出力しています。
この224行224列の行列式を3Dプロットしたいです。
1列分の出力結果
###該当コード

python

1def grad_cam(input_model, image, category_index, layer_name): 2 nb_classes = 1000 3 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) 4 x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output) 5 model = Model(inputs=input_model.input, outputs=x) 6 #model.summary() 7 loss = K.sum(model.output) 8 conv_output = [l for l in model.layers if l.name == layer_name][0].output #is 9 grads = normalize(_compute_gradients(loss, [conv_output])[0]) 10 gradient_function = K.function([model.input], [conv_output, grads]) 11 12 output, grads_val = gradient_function([image]) 13 output, grads_val = output[0, :], grads_val[0, :, :, :] 14 15 weights = np.mean(grads_val, axis = (0, 1)) 16 cam = np.ones(output.shape[0 : 2], dtype = np.float32) 17 18 for i, w in enumerate(weights): 19 cam += w * output[:, :, i] 20 21 cam = cv2.resize(cam, (224,224)) #299,299)) #224, 224)) 22 cam = np.maximum(cam, 0) 23 heatmap = cam / np.max(cam) 24 for x in range(224): 25 print (heatmap[x]) #3マップで出力したい部分 26 27 #Return to BGR [0..255] from the preprocessed image 28 image = image[0, :] 29 image -= np.min(image) 30 image = np.minimum(image, 255) 31 32 cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) 33 cam = np.float32(cam) + np.float32(image) 34 cam = 255 * cam / np.max(cam) 35 return np.uint8(cam), heatmap

###全体のコード

python

1from keras.applications.vgg16 import (VGG16, preprocess_input, decode_predictions) 2from keras.models import Model 3from keras.preprocessing import image 4from keras.layers.core import Lambda 5from keras.models import Sequential 6from tensorflow.python.framework import ops 7import keras.backend as K 8import tensorflow as tf 9import numpy as np 10import keras 11import sys 12import cv2 13#from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions 14#from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions 15#from keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions 16 17def target_category_loss(x, category_index, nb_classes): 18 return tf.multiply(x, K.one_hot([category_index], nb_classes)) 19 20def target_category_loss_output_shape(input_shape): 21 return input_shape 22 23def normalize(x): 24 # utility function to normalize a tensor by its L2 norm 25 return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) 26 27def load_image(path): 28 img_path = sys.argv[1] 29 img = image.load_img(img_path, target_size=(224,224)) #299,299)) #224, 224)) 30 x = image.img_to_array(img) 31 x = np.expand_dims(x, axis=0) 32 x = preprocess_input(x) 33 return x 34 35def register_gradient(): 36 if "GuidedBackProp" not in ops._gradient_registry._registry: 37 @ops.RegisterGradient("GuidedBackProp") 38 def _GuidedBackProp(op, grad): 39 dtype = op.inputs[0].dtype 40 return grad * tf.cast(grad > 0., dtype) * \ 41 tf.cast(op.inputs[0] > 0., dtype) 42 43def compile_saliency_function(model, activation_layer='block5_conv3'): #mixed10 'activation_49' add_16 add_32 activation_98 44 input_img = model.input 45 layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) 46 #print(layer_dict) 47 layer_output = layer_dict[activation_layer].output 48 max_output = K.max(layer_output, axis=3) 49 saliency = K.gradients(K.sum(max_output), input_img)[0] 50 return K.function([input_img, K.learning_phase()], [saliency]) 51 52def modify_backprop(model, name): 53 g = tf.get_default_graph() 54 with g.gradient_override_map({'Relu': name}): 55 56 # get layers that have an activation 57 layer_dict = [layer for layer in model.layers[1:] 58 if hasattr(layer, 'activation')] 59 60 # replace relu activation 61 for layer in layer_dict: 62 if layer.activation == keras.activations.relu: 63 layer.activation = tf.nn.relu 64 65 # re-instanciate a new model 66 new_model = VGG16(weights='imagenet') 67 #new_model = ResNet50(weights='imagenet') 68 new_model.summary() 69 return new_model 70 71def deprocess_image(x): 72 ''' 73 Same normalization as in: 74 https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py 75 ''' 76 if np.ndim(x) > 3: 77 x = np.squeeze(x) 78 # normalize tensor: center on 0., ensure std is 0.1 79 x -= x.mean() 80 x /= (x.std() + 1e-5) 81 x *= 0.1 82 83 # clip to [0, 1] 84 x += 0.5 85 x = np.clip(x, 0, 1) 86 87 # convert to RGB array 88 x *= 255 89 if K.image_dim_ordering() == 'th': 90 x = x.transpose((1, 2, 0)) 91 x = np.clip(x, 0, 255).astype('uint8') 92 return x 93 94def _compute_gradients(tensor, var_list): 95 grads = tf.gradients(tensor, var_list) 96 return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)] 97 98def grad_cam(input_model, image, category_index, layer_name): 99 nb_classes = 1000 100 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) 101 x = Lambda(target_layer, output_shape = target_category_loss_output_shape)(input_model.output) 102 model = Model(inputs=input_model.input, outputs=x) 103 #model.summary() 104 loss = K.sum(model.output) 105 conv_output = [l for l in model.layers if l.name == layer_name][0].output #is 106 grads = normalize(_compute_gradients(loss, [conv_output])[0]) 107 gradient_function = K.function([model.input], [conv_output, grads]) 108 109 output, grads_val = gradient_function([image]) 110 output, grads_val = output[0, :], grads_val[0, :, :, :] 111 112 weights = np.mean(grads_val, axis = (0, 1)) 113 cam = np.ones(output.shape[0 : 2], dtype = np.float32) 114 115 for i, w in enumerate(weights): 116 cam += w * output[:, :, i] 117 118 cam = cv2.resize(cam, (224,224)) #299,299)) #224, 224)) 119 cam = np.maximum(cam, 0) 120 heatmap = cam / np.max(cam) 121 for x in range(224): 122 print (heatmap[x]) 123 124 #Return to BGR [0..255] from the preprocessed image 125 image = image[0, :] 126 image -= np.min(image) 127 image = np.minimum(image, 255) 128 129 cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) 130 cam = np.float32(cam) + np.float32(image) 131 cam = 255 * cam / np.max(cam) 132 return np.uint8(cam), heatmap 133 134preprocessed_input = load_image(sys.argv[1]) 135model = VGG16(weights='imagenet') 136#model = VGG19(weights='imagenet') 137#model = InceptionV3(weights='imagenet') 138#model = ResNet50(weights = 'imagenet') 139#model.summary() 140target_layer = 'block5_conv3' #'activation_49' add_16 "block5_conv3" 141 142predictions = model.predict(preprocessed_input) 143register_gradient() 144guided_model = modify_backprop(model, 'GuidedBackProp') 145guided_model.summary() 146for i in range(5): 147 top_1 = decode_predictions(predictions)[0][i] 148 print(predictions.argsort()[0][::-1][i]) 149 print('Predicted class:') 150 print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2])) 151 predicted_class = predictions.argsort()[0][::-1][i] #np.argmax(predictions) 152 cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, target_layer) 153 cv2.imwrite("gradcam"+str(top_1[1])+".jpg", cam) 154 saliency_fn = compile_saliency_function(guided_model) 155 saliency = saliency_fn([preprocessed_input, 0]) 156 gradcam = saliency[0] * heatmap[..., np.newaxis] 157 cv2.imwrite("guided_gradcam"+str(top_1[1])+".jpg", deprocess_image(gradcam))

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

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

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

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

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

can110

2020/07/03 07:24

第三者が実行して現状の図を再現できる完全なコードを提示すると、質問の意図も把握しやすく回答得られやすくなると思います。(cam, heatmapをCSV出力して提示する、コードに直埋めするなど)
guest

回答2

0

ベストアンサー

この224行224列の行列式を3Dプロットしたいです。

yymmtさんの回答とほぼ同じですが、以下のように(224,224)のarrayを与えるとグラフが描画できます。

Python

1import numpy as np 2import matplotlib.pyplot as plt 3from matplotlib import cm 4from mpl_toolkits.mplot3d import Axes3D 5 6np.random.seed(110) 7M, N = 224, 224 8 9X, Y = np.arange(M), np.arange(N) 10Z = np.random.normal(1, 0.1, (N, N)) # Zに(224,224)のarrayを与える 11print(type(Z), Z.shape) # <class 'numpy.ndarray'> (224, 224) 12 13fig = plt.figure() 14ax = Axes3D(fig) 15 16XX, YY = np.meshgrid(X, Y, indexing='ij') 17surf = ax.scatter3D(XX, YY, Z, c=Z.ravel(), cmap=cm.Spectral) 18fig.colorbar(surf) 19plt.show()

イメージ説明

投稿2020/07/15 08:19

編集2020/07/15 12:59
can110

総合スコア38266

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

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

satoUmino

2020/07/15 08:49

無事3Dグラフにすることができました。 このグラフを3Dの散布図に変更するにはどのようにしたら良いのでしょうか
can110

2020/07/15 08:54

回答修正しました。
satoUmino

2020/07/15 09:42

すぐに修正していただきありがとうございます。 'c' argument has 224 elements, which is not acceptable for use with 'x' with size 50176, 'y' with size 50176. このようなエラーが出てしまったのですが、どのように修正すれば良いでしょうか 何度もすみません
can110

2020/07/15 11:06

Zの型とカタチはどうなっていますか? print( type(Z), Z.shape)の結果はどうなりますか?
satoUmino

2020/07/15 12:33

<class 'numpy.ndarray'> (224, 224) となってました
can110

2020/07/15 13:01

それならOKのはずですが。ならばc=Z.ravel()してみてはどうしょうか。回答修正しました。
satoUmino

2020/07/15 13:09

変更してみたら動作しました 何度もありがとうございます
guest

0

ほとんどチュートリアルそのままですが、以下のようにするのはどうでしょう?
X,Yを1~224にして、Zを計算値としています。

python

1import numpy as np 2import matplotlib.pyplot as plt 3from matplotlib import cm 4from mpl_toolkits.mplot3d import Axes3D 5 6# ダミーデータを作成 7X = np.arange(start=1, stop=225) 8Y = np.arange(start=1, stop=225) 9Z = np.empty(shape=(224, 224)) 10for i in range(224): 11 for j in range(224): 12 Z[i, j] = np.sin(2.0 * np.pi * X[i] / 224.0 * Y[j] / 224.0) 13 14# グラフの枠を作成 15fig = plt.figure(figsize=(12, 6)) 16ax = Axes3D(fig) 17 18# X,Y,Z軸にラベルを設定 19ax.set_xlabel("X") 20ax.set_ylabel("Y") 21ax.set_zlabel("Z") 22 23# .plotで描画 24XX, YY = np.meshgrid(X, Y, indexing='ij') 25surf = ax.plot_surface(XX, YY, Z, cmap=cm.Spectral) 26fig.colorbar(surf, shrink=0.5) 27plt.show()

3Dプロット

参考URL

投稿2020/07/02 03:43

yymmt

総合スコア1615

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

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

satoUmino

2020/07/03 07:13

プログラム内の計算値であるheatmapを指定してグラフを作成する際どこを書き換えるのかがわからなかったので質問したのでそこを教えてもらえると助かります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問