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

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

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

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

OpenCV

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

Python

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

Q&A

0回答

534閲覧

guied-grad-camでのopencvのエラー文

koukimaru22

総合スコア6

Keras

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

OpenCV

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

Python

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

0グッド

0クリップ

投稿2019/10/10 05:40

編集2019/10/10 06:11

前提・実現したいこと

guied-grad-camを使用したいのですが以下のエラー文が出ました。
調べたのですがユニコードが含まれていると出るなどがあったのですが
解決策がいまいちわかりません。
初心者なのでいまいちわかっていないので教えてください。

発生している問題・エラーメッセージ

OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:667: error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function 'cv::imwrite_'

該当のソースコード

python3

1 def target_category_loss(x, category_index, nb_classes): 2 return tf.multiply(x, K.one_hot([category_index], nb_classes)) 3 4def target_category_loss_output_shape(input_shape): 5 return input_shape 6 7def normalize(x): 8 return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) 9 10def load_image(path): 11 img_path = path 12 img = image.load_img(img_path, target_size=(224, 224)) 13 x = image.img_to_array(img) 14 x = np.expand_dims(x, axis=0) 15 x = preprocess_input(x) 16 return x 17 18 19def register_gradient(): 20 if "GuidedBackProp" not in ops._gradient_registry._registry: 21 @ops.RegisterGradient("GuidedBackProp") 22 def _GuidedBackProp(op, grad): 23 dtype = op.inputs[0].dtype 24 return grad * tf.cast(grad > 0., dtype) * \ 25 tf.cast(op.inputs[0] > 0., dtype) 26 27 28def compile_saliency_function(model, activation_layer='block5_conv3'): 29 input_img = model.input 30 layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) 31 layer_output = layer_dict[activation_layer].output 32 max_output = K.max(layer_output, axis=3) 33 saliency = K.gradients(K.sum(max_output), input_img)[0] 34 return K.function([input_img, K.learning_phase()], [saliency]) 35 36 37def modify_backprop(model, name): 38 g = tf.get_default_graph() 39 with g.gradient_override_map({'Relu': name}): 40 new_model = VGG16(weights='imagenet') 41 return new_model 42 43 44def deprocess_image(x): 45 if np.ndim(x) > 3: 46 x = np.squeeze(x) 47 x -= x.mean() 48 x /= (x.std() + 1e-5) 49 x *= 0.1 50 51 x += 0.5 52 x = np.clip(x, 0, 1) 53 54 x *= 255 55 if K.image_dim_ordering() == 'tf': 56 x = x.transpose((1, 2, 0)) 57 x = np.clip(x, 0, 255).astype('uint8') 58 return x 59 60 61def grad_cam(input_model, image, category_index, layer_name): 62 nb_classes = 1000 63 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) 64 65 x = input_model.layers[-1].output 66 x = Lambda(target_layer, output_shape=target_category_loss_output_shape)(x) 67 model = keras.models.Model(input_model.layers[0].input, x) 68 69 loss = K.sum(model.layers[-1].output) 70 conv_output = [l for l in model.layers if l.name is layer_name][0].output 71 72 grads = normalize(K.gradients(loss, conv_output)[0]) 73 gradient_function = K.function([model.layers[0].input], [conv_output, grads]) 74 75 output, grads_val = gradient_function([image]) 76 output, grads_val = output[0, :], grads_val[0, :, :, :] 77 78 weights = np.mean(grads_val, axis = (0, 1)) 79 cam = np.ones(output.shape[0 : 2], dtype = np.float32) 80 81 for i, w in enumerate(weights): 82 cam += w * output[:, :, i] 83 84 cam = cv2.resize(cam, (224, 224)) 85 cam = np.maximum(cam, 0) 86 heatmap = cam / np.max(cam) 87 88 image = image[0, :] 89 image -= np.min(image) 90 image = np.minimum(image, 255) 91 92 cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) 93 cam = np.float32(cam) + np.float32(image) 94 cam = 255 * cam / np.max(cam) 95 return np.uint8(cam), heatmap 96 97preprocessed_input = load_image("./dog_cat.jpg") 98 99model = VGG16(weights='imagenet') 100 101predictions = model.predict(preprocessed_input) 102top_1 = decode_predictions(predictions)[0][0] 103print('Predicted class:') 104print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2])) 105 106predicted_class = np.argmax(predictions) 107print(predicted_class) 108 109cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "block5_conv3") 110 111cv2.imwrite("gradcam.jpg", cam) 112 113register_gradient() 114guided_model = modify_backprop(model, 'GuidedBackProp') 115saliency_fn = compile_saliency_function(guided_model) 116saliency = saliency_fn([preprocessed_input, 0]) 117gradcam = saliency[0] * heatmap[..., np.newaxis] 118cv2.imwrite("guided_gradcam.jpg", deprocess_image(gradcam)) 119

補足情報(FW/ツールのバージョンなど)

OpenCV:4.1.0
keras:2.2.4
tensorflow:1.14.0

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

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

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

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

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

tiitoi

2019/10/10 05:55

deprocess_image(gradcam) の返り値の numpy 配列の形状と型はどうなっていますか?
koukimaru22

2019/10/10 06:14

今自分でしているコードをすべて質問のところに記載しました。 そこから形状や型っていうのはわかりますか。 それともこちらでどこかの箇所でprint(,shape)と打った方がいいのか教えてください。 初歩的なことですみませんがお願いします。
tiitoi

2019/10/10 06:16

ret = deprocess_image(gradcam) print(ret.shape, ret.dtype) で形状と型を確認できます。
koukimaru22

2019/10/10 06:18

実行した結果 (224, 3, 224) uint8 と表示されました。
tiitoi

2019/10/10 06:23

(224, 3, 224) は画像として解釈するならおかしいですね。 RGB画像であれば、(高さ, 幅, 3) の numpy 配列になると思います。
koukimaru22

2019/10/10 06:27

ありがとうございます。 理由がわかりdeprocessの中の配列の順番を変えたら出力の方ができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問