@@ -41,6 +41,13 @@ def hook_cnn_layer(self):
41
41
"""
42
42
def hook_fn (module , grad_in , grad_out ):
43
43
self .conv_output = grad_out [0 , self .cnn_filter ]
44
+ print ('---- conv out -----' )
45
+ print (type (self .conv_output ))
46
+ print (self .conv_output .shape )
47
+ print (grad_out .shape )
48
+ print (grad_out [0 , self .cnn_filter ])
49
+ print ('\n ----- grad in ------' )
50
+ print (grad_in [0 ][0 ][0 ])
44
51
# saving the number of filters in that layer
45
52
self .num_filters = grad_out .shape [1 ]
46
53
self .model [self .cnn_layer ].register_forward_hook (hook_fn )
@@ -72,13 +79,15 @@ def vis_cnn_layer(self):
72
79
# self.conv_output = x[0, self.cnn_filter]
73
80
# loss function according to Erhan et al. (2009)
74
81
loss = - torch .mean (self .conv_output )
82
+ print (loss .shape )
83
+ print (loss .data )
75
84
loss .backward () # calculate gradients
76
85
optimizer .step () # update weights
77
86
self .layer_img = rebuild_image (processed_image ) # reconstruct image
78
87
79
88
print ('Epoch {}/{} --> Loss {:.3f}' .format (e + 1 , self .epochs , loss .data .numpy ()))
80
89
81
- if e % 10 == 0 :
90
+ if e % 5 == 0 :
82
91
img_path = 'activ_max_imgs/am_vis_l' + str (self .cnn_layer ) + \
83
92
'_f' + str (self .cnn_filter ) + '_iter' + str (e + 1 ) + '.jpg'
84
93
save_image (self .layer_img , img_path )
0 commit comments