source/utils.py
def visualize_filters(dict_vis):
    """ Visualize and save filters of all the convolutional layers

    Plot filters of the conv layers using matplotlib. Weights are loaded, after which the function extracts the weights to 'model_weights'. Filters visuals are plotted for each layer and saved in data/visualize_filters. Command line arg is (-f, --filter-vis). Terminates execution after plotting and saving.

    :param dict_vis: dictionary containing scale value and path to weights file
    :return: None

    """

    weights_file= dict_vis['weights file']
    scale= dict_vis['scale']
    
    device = torch.device('cpu')
    model = ESPCN(scale_factor=scale)

    state_dict = model.state_dict()
    for n, p in torch.load(weights_file, map_location=lambda storage, loc: storage).items():
        if n in state_dict.keys():
            state_dict[n].copy_(p)
        else:
            raise KeyError(n)

    model_weights= []   # To store weights
    conv_layers= []     # To store the conv2d layers

    model_children= list(model.children())
    counter = 0 
    for i in range(len(model_children)):
        for j in range(len(model_children[i])):
            child= model_children[i][j]
            if type(child) == nn.Conv2d:
                counter += 1
                model_weights.append(child.weight)
                conv_layers.append(child)

    out_path= 'data/visualize_filters'
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    
    sizes= [(8,8), (4,8), (3,3)]
    k_sizes= [5,3,3]
    plt.figure(figsize=(20, 17))
    for n in range(len(model_weights)):
        for i, filter in enumerate(model_weights[n]):
            plt.subplot(sizes[n][0], sizes[n][1], i+1)
            plt.imshow(filter[0, :, :].detach(), cmap='gray')
            plt.axis('off')
        plt.suptitle('Convolutional Layer ' + str(n+1) + ': Filter visualization', fontsize=40)
        plt.savefig('data/visualize_filters/filter'+str(n+1)+'.png')
        plt.clf()
    print('Filter images saved to data/visualize_filters')
    sys.exit()
 Visualize and save filters of all the convolutional layers

    Plot filters of the conv layers using matplotlib. Weights are loaded, after which the function extracts the weights to 'model_weights'. Filters visuals are plotted for each layer and saved in data/visualize_filters. Command line arg is (-f, --filter-vis). Terminates execution after plotting and saving.

    :param dict_vis: dictionary containing scale value and path to weights file
    :return: None

    
Visualize and save filters of all the convolutional layers
Plot filters of the conv layers using matplotlib. Weights are loaded, after which the function extracts the weights to 'model_weights'. Filters visuals are plotted for each layer and saved in data/visualize_filters. Command line arg is (-f, --filter-vis). Terminates execution after plotting and saving.
Visualize and save filters of all the convolutional layers Plot filters of the conv layers using matplotlib . Weights are loaded after which the function extracts the weights to ' model_weights ' . Filters visuals are plotted for each layer and saved in data / visualize_filters . Command line arg is ( - f -- filter - vis ) . Terminates execution after plotting and saving .
def visualize_filters(dict_vis):
    weights_file= dict_vis['weights file']
    scale= dict_vis['scale']
    device = torch.device('cpu')
    model = ESPCN(scale_factor=scale)
    state_dict = model.state_dict()
    for n, p in torch.load(weights_file, map_location=lambda storage, loc: storage).items():
        if n in state_dict.keys():
            state_dict[n].copy_(p)
        else:
            raise KeyError(n)
    model_weights= []   
    conv_layers= []     
    model_children= list(model.children())
    counter = 0 
    for i in range(len(model_children)):
        for j in range(len(model_children[i])):
            child= model_children[i][j]
            if type(child) == nn.Conv2d:
                counter += 1
                model_weights.append(child.weight)
                conv_layers.append(child)
    out_path= 'data/visualize_filters'
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    sizes= [(8,8), (4,8), (3,3)]
    k_sizes= [5,3,3]
    plt.figure(figsize=(20, 17))
    for n in range(len(model_weights)):
        for i, filter in enumerate(model_weights[n]):
            plt.subplot(sizes[n][0], sizes[n][1], i+1)
            plt.imshow(filter[0, :, :].detach(), cmap='gray')
            plt.axis('off')
        plt.suptitle('Convolutional Layer ' + str(n+1) + ': Filter visualization', fontsize=40)
        plt.savefig('data/visualize_filters/filter'+str(n+1)+'.png')
        plt.clf()
    print('Filter images saved to data/visualize_filters')
    sys.exit()
def visualize_filters ( dict_vis ) : weights_file = dict_vis [ 'weights file' ] scale = dict_vis [ 'scale' ] device = torch . device ( 'cpu' ) model = ESPCN ( scale_factor = scale ) state_dict = model . state_dict ( ) for n , p in torch . load ( weights_file , map_location = lambda storage , loc : storage ) . items ( ) : if n in state_dict . keys ( ) : state_dict [ n ] . copy_ ( p ) else : raise KeyError ( n ) model_weights = [ ] conv_layers = [ ] model_children = list ( model . children ( ) ) counter = 0 for i in range ( len ( model_children ) ) : for j in range ( len ( model_children [ i ] ) ) : child = model_children [ i ] [ j ] if type ( child ) == nn . Conv2d : counter += 1 model_weights . append ( child . weight ) conv_layers . append ( child ) out_path = 'data/visualize_filters' if not os . path . exists ( out_path ) : os . makedirs ( out_path ) sizes = [ ( 8 , 8 ) , ( 4 , 8 ) , ( 3 , 3 ) ] k_sizes = [ 5 , 3 , 3 ] plt . figure ( figsize = ( 20 , 17 ) ) for n in range ( len ( model_weights ) ) : for i , filter in enumerate ( model_weights [ n ] ) : plt . subplot ( sizes [ n ] [ 0 ] , sizes [ n ] [ 1 ] , i + 1 ) plt . imshow ( filter [ 0 , : , : ] . detach ( ) , cmap = 'gray' ) plt . axis ( 'off' ) plt . suptitle ( 'Convolutional Layer ' + str ( n + 1 ) + ': Filter visualization' , fontsize = 40 ) plt . savefig ( 'data/visualize_filters/filter' + str ( n + 1 ) + '.png' ) plt . clf ( ) print ( 'Filter images saved to data/visualize_filters' ) sys . exit ( )
Visualize and save filters of all the convolutional layers
Plot filters of the conv layers using matplotlib.
Visualize and save filters of all the convolutional layers Plot filters of the conv layers using matplotlib .
""" Visualize and save filters of all the convolutional layers

    Plot filters of the conv layers using matplotlib. Weights are loaded, after which the function extracts the weights to 'model_weights'. Filters visuals are plotted for each layer and saved in data/visualize_filters. Command line arg is (-f, --filter-vis). Terminates execution after plotting and saving.

    :param dict_vis: dictionary containing scale value and path to weights file
    :return: None

    """ # To store weights # To store the conv2d layers
None
dict_vis
dictionary containing scale value and path to weights file
dictionary containing scale value and path to weights file