前提・実現したいこと
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
あなたの回答
tips
プレビュー