You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/13 23:38:08 UTC

[GitHub] indhub closed pull request #13196: [Example] Fixing Gradcam implementation

indhub closed pull request #13196: [Example] Fixing Gradcam implementation
URL: https://github.com/apache/incubator-mxnet/pull/13196
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/docs/tutorials/vision/cnn_visualization.md b/docs/tutorials/vision/cnn_visualization.md
index 940c261efc8..a350fffaa36 100644
--- a/docs/tutorials/vision/cnn_visualization.md
+++ b/docs/tutorials/vision/cnn_visualization.md
@@ -151,7 +151,8 @@ def show_images(pred_str, images):
     for i in range(num_images):
         fig.add_subplot(rows, cols, i+1)
         plt.xlabel(titles[i])
-        plt.imshow(images[i], cmap='gray' if i==num_images-1 else None)
+        img = images[i].astype(np.uint8)
+        plt.imshow(img, cmap='gray' if i==num_images-1 else None)
     plt.show()
 ```
 
diff --git a/example/cnn_visualization/gradcam.py b/example/cnn_visualization/gradcam.py
index a8708f78758..54cb65eef11 100644
--- a/example/cnn_visualization/gradcam.py
+++ b/example/cnn_visualization/gradcam.py
@@ -249,8 +249,8 @@ def visualize(net, preprocessed_img, orig_img, conv_layer_name):
     imggrad = get_image_grad(net, preprocessed_img)
     conv_out, conv_out_grad = get_conv_out_grad(net, preprocessed_img, conv_layer_name=conv_layer_name)
 
-    cam = get_cam(imggrad, conv_out)
-    
+    cam = get_cam(conv_out_grad, conv_out)
+    cam = cv2.resize(cam, (imggrad.shape[1], imggrad.shape[2]))
     ggcam = get_guided_grad_cam(cam, imggrad)
     img_ggcam = grad_to_image(ggcam)
     
diff --git a/example/cnn_visualization/vgg.py b/example/cnn_visualization/vgg.py
index b6215a334e3..a8a0ef6c8de 100644
--- a/example/cnn_visualization/vgg.py
+++ b/example/cnn_visualization/vgg.py
@@ -72,11 +72,17 @@ def get_vgg(num_layers, pretrained=False, ctx=mx.cpu(),
             root=os.path.join('~', '.mxnet', 'models'), **kwargs):
     layers, filters = vgg_spec[num_layers]
     net = VGG(layers, filters, **kwargs)
-    if pretrained:
-        from mxnet.gluon.model_zoo.model_store import get_model_file
-        batch_norm_suffix = '_bn' if kwargs.get('batch_norm') else ''
-        net.load_params(get_model_file('vgg%d%s'%(num_layers, batch_norm_suffix),
-                                       root=root), ctx=ctx)
+    net.initialize(ctx=ctx)
+    
+    # Get the pretrained model
+    vgg = mx.gluon.model_zoo.vision.get_vgg(num_layers, pretrained=True, ctx=ctx)
+    
+    # Set the parameters in the new network
+    params = vgg.collect_params()
+    for key in params:
+        param = params[key]
+        net.collect_params()[net.prefix+key.replace(vgg.prefix, '')].set_data(param.data())
+   
     return net
 
 def vgg16(**kwargs):


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services