'Visualize layer'

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
    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]
        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)
        model.fprop(img_batch, inference=True)
        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)
        for (i, l) in enumerate(layers[layer_ind::(-1)], 0):
            if isinstance(l, Convolution):
                activation[:] = be.maximum(activation, 0)
                (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
                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])
