(self, callback_data, model, num_fm, act_size, layer_ind)
Visualize layer
Visualize layer
Visualize layer
def visualize_layer(self, callback_data, model, num_fm, act_size, layer_ind):
        """
        Visualize layer
        """
        be = model.be
        act_data = callback_data["deconv/max_act/{0:04}".format(layer_ind)]
        layers = model.layers.layers

        # Loop to visualize every feature map
        num_fm_vis = min(num_fm, self.max_fm)
        for fm in range(num_fm_vis):
            batch_ind, img_ind = act_data['batch_img'][fm]

            # Prepare a fake minibatch with just the max activation image for this fm
            img_batch = np.zeros((self.raw_img_cache[batch_ind].shape[0], be.bsz))
            img_cache_offs = self.raw_img_key[batch_ind][img_ind]
            img_batch[:, 0] = self.raw_img_cache[batch_ind][:, img_cache_offs]
            img_batch = be.array(img_batch)

            # Prep model internal state by fprop-ing img
            model.fprop(img_batch, inference=True)

            # Set the max activation at the correct feature map location
            fm_loc = act_data['fm_loc'][fm]
            activation = np.zeros((num_fm, act_size, be.bsz))
            activation[fm, fm_loc, :] = float(act_data['activation'][fm])
            activation = activation.reshape((num_fm * act_size, be.bsz))
            activation = be.array(activation)

            # Loop over the previous layers to perform deconv
            for i, l in enumerate(layers[layer_ind::-1], 0):
                if isinstance(l, Convolution):

                    # zero out w.r.t. current layer activations
                    activation[:] = be.maximum(activation, 0)

                    # output shape of deconv is the input shape of conv
                    C, H, W = [l.convparams[x] for x in ['C', 'H', 'W']]
                    out = be.empty((C * H * W, be.bsz))
                    l.be.bprop_conv(layer=l.nglayer, F=l.W, E=activation, grad_I=out)
                    activation = out

                    # zero out w.r.t to input from lower layer
                    layer_below_acts = layers[layer_ind - i].inputs
                    layer_below_acts[:] = be.greater(layer_below_acts, 0)
                    activation[:] = be.multiply(layer_below_acts, activation)

            C, H, W = layers[0].in_shape
            activation = activation.get().reshape((C, H, W, be.bsz))
            activation = np.transpose(activation, (1, 2, 0, 3))
            act_data['vis'][fm] = self.scale_to_rgb(activation[:, :, :, 0])
def visualize_layer ( self , callback_data , model , num_fm , act_size , layer_ind ) : be = model . be act_data = callback_data [ "deconv/max_act/{0:04}" . format ( layer_ind ) ] layers = model . layers . layers # Loop to visualize every feature map num_fm_vis = min ( num_fm , self . max_fm ) for fm in range ( num_fm_vis ) : batch_ind , img_ind = act_data [ 'batch_img' ] [ fm ] # Prepare a fake minibatch with just the max activation image for this fm img_batch = np . zeros ( ( self . raw_img_cache [ batch_ind ] . shape [ 0 ] , be . bsz ) ) img_cache_offs = self . raw_img_key [ batch_ind ] [ img_ind ] img_batch [ : , 0 ] = self . raw_img_cache [ batch_ind ] [ : , img_cache_offs ] img_batch = be . array ( img_batch ) # Prep model internal state by fprop-ing img model . fprop ( img_batch , inference = True ) # Set the max activation at the correct feature map location fm_loc = act_data [ 'fm_loc' ] [ fm ] activation = np . zeros ( ( num_fm , act_size , be . bsz ) ) activation [ fm , fm_loc , : ] = float ( act_data [ 'activation' ] [ fm ] ) activation = activation . reshape ( ( num_fm * act_size , be . bsz ) ) activation = be . array ( activation ) # Loop over the previous layers to perform deconv for i , l in enumerate ( layers [ layer_ind : : - 1 ] , 0 ) : if isinstance ( l , Convolution ) : # zero out w.r.t. current layer activations activation [ : ] = be . maximum ( activation , 0 ) # output shape of deconv is the input shape of conv C , H , W = [ l . convparams [ x ] for x in [ 'C' , 'H' , 'W' ] ] out = be . empty ( ( C * H * W , be . bsz ) ) l . be . bprop_conv ( layer = l . nglayer , F = l . W , E = activation , grad_I = out ) activation = out # zero out w.r.t to input from lower layer layer_below_acts = layers [ layer_ind - i ] . inputs layer_below_acts [ : ] = be . greater ( layer_below_acts , 0 ) activation [ : ] = be . multiply ( layer_below_acts , activation ) C , H , W = layers [ 0 ] . in_shape activation = activation . get ( ) . reshape ( ( C , H , W , be . bsz ) ) activation = np . transpose ( activation , ( 1 , 2 , 0 , 3 ) ) act_data [ 'vis' ] [ fm ] = self . scale_to_rgb ( activation [ : , : , : , 0 ] )