(self, pretrained=True, net='alex', version='0.1', lpips=True, spatial=False, 
        pnet_rand=False, pnet_tune=False, use_dropout=True, model_path=None, eval_mode=True, verbose=True)
Initializes a perceptual loss torch.nn.Module

        Parameters (default listed first)
        ---------------------------------
        lpips : bool
            [True] use linear layers on top of base/trunk network
            [False] means no linear layers; each layer is averaged together
        pretrained : bool
            This flag controls the linear layers, which are only in effect when lpips=True above
            [True] means linear layers are calibrated with human perceptual judgments
            [False] means linear layers are randomly initialized
        pnet_rand : bool
            [False] means trunk loaded with ImageNet classification weights
            [True] means randomly initialized trunk
        net : str
            ['alex','vgg','squeeze'] are the base/trunk networks available
        version : str
            ['v0.1'] is the default and latest
            ['v0.0'] contained a normalization bug; corresponds to old arxiv v1 (https://arxiv.org/abs/1801.03924v1)
        model_path : 'str'
            [None] is default and loads the pretrained weights from paper https://arxiv.org/abs/1801.03924v1

        The following parameters should only be changed if training the network

        eval_mode : bool
            [True] is for test mode (default)
            [False] is for training mode
        pnet_tune
            [False] keep base/trunk frozen
            [True] tune the base/trunk network
        use_dropout : bool
            [True] to use dropout when training linear layers
            [False] for no dropout when training linear layers
Initializes a perceptual loss torch.nn.Module
Initializes a perceptual loss torch . nn . Module
def __init__(self, pretrained=True, net='alex', version='0.1', lpips=True, spatial=False, 
        pnet_rand=False, pnet_tune=False, use_dropout=True, model_path=None, eval_mode=True, verbose=True):
        """ Initializes a perceptual loss torch.nn.Module

        Parameters (default listed first)
        ---------------------------------
        lpips : bool
            [True] use linear layers on top of base/trunk network
            [False] means no linear layers; each layer is averaged together
        pretrained : bool
            This flag controls the linear layers, which are only in effect when lpips=True above
            [True] means linear layers are calibrated with human perceptual judgments
            [False] means linear layers are randomly initialized
        pnet_rand : bool
            [False] means trunk loaded with ImageNet classification weights
            [True] means randomly initialized trunk
        net : str
            ['alex','vgg','squeeze'] are the base/trunk networks available
        version : str
            ['v0.1'] is the default and latest
            ['v0.0'] contained a normalization bug; corresponds to old arxiv v1 (https://arxiv.org/abs/1801.03924v1)
        model_path : 'str'
            [None] is default and loads the pretrained weights from paper https://arxiv.org/abs/1801.03924v1

        The following parameters should only be changed if training the network

        eval_mode : bool
            [True] is for test mode (default)
            [False] is for training mode
        pnet_tune
            [False] keep base/trunk frozen
            [True] tune the base/trunk network
        use_dropout : bool
            [True] to use dropout when training linear layers
            [False] for no dropout when training linear layers
        """

        super(LPIPS, self).__init__()
        if(verbose):
            print('Setting up [%s] perceptual loss: trunk [%s], v[%s], spatial [%s]'%
                ('LPIPS' if lpips else 'baseline', net, version, 'on' if spatial else 'off'))

        self.pnet_type = net
        self.pnet_tune = pnet_tune
        self.pnet_rand = pnet_rand
        self.spatial = spatial
        self.lpips = lpips # false means baseline of just averaging all layers
        self.version = version
        self.scaling_layer = ScalingLayer()

        if(self.pnet_type in ['vgg','vgg16']):
            net_type = pn.vgg16
            self.chns = [64,128,256,512,512]
        elif(self.pnet_type=='alex'):
            net_type = pn.alexnet
            self.chns = [64,192,384,256,256]
        elif(self.pnet_type=='squeeze'):
            net_type = pn.squeezenet
            self.chns = [64,128,256,384,384,512,512]
        self.L = len(self.chns)

        self.net = net_type(pretrained=not self.pnet_rand, requires_grad=self.pnet_tune)

        if(lpips):
            self.lin0 = NetLinLayer(self.chns[0], use_dropout=use_dropout)
            self.lin1 = NetLinLayer(self.chns[1], use_dropout=use_dropout)
            self.lin2 = NetLinLayer(self.chns[2], use_dropout=use_dropout)
            self.lin3 = NetLinLayer(self.chns[3], use_dropout=use_dropout)
            self.lin4 = NetLinLayer(self.chns[4], use_dropout=use_dropout)
            self.lins = [self.lin0,self.lin1,self.lin2,self.lin3,self.lin4]
            if(self.pnet_type=='squeeze'): # 7 layers for squeezenet
                self.lin5 = NetLinLayer(self.chns[5], use_dropout=use_dropout)
                self.lin6 = NetLinLayer(self.chns[6], use_dropout=use_dropout)
                self.lins+=[self.lin5,self.lin6]
            self.lins = nn.ModuleList(self.lins)

            if(pretrained):
                if(model_path is None):
                    import inspect
                    import os
                    model_path = os.path.abspath(os.path.join(inspect.getfile(self.__init__), '..', 'weights/v%s/%s.pth'%(version,net)))

                if(verbose):
                    print('Loading model from: %s'%model_path)
                self.load_state_dict(torch.load(model_path, map_location='cpu'), strict=False)          

        if(eval_mode):
            self.eval()
def __init__ ( self , pretrained = True , net = 'alex' , version = '0.1' , lpips = True , spatial = False , pnet_rand = False , pnet_tune = False , use_dropout = True , model_path = None , eval_mode = True , verbose = True ) : super ( LPIPS , self ) . __init__ ( ) if ( verbose ) : print ( 'Setting up [%s] perceptual loss: trunk [%s], v[%s], spatial [%s]' % ( 'LPIPS' if lpips else 'baseline' , net , version , 'on' if spatial else 'off' ) ) self . pnet_type = net self . pnet_tune = pnet_tune self . pnet_rand = pnet_rand self . spatial = spatial self . lpips = lpips # false means baseline of just averaging all layers self . version = version self . scaling_layer = ScalingLayer ( ) if ( self . pnet_type in [ 'vgg' , 'vgg16' ] ) : net_type = pn . vgg16 self . chns = [ 64 , 128 , 256 , 512 , 512 ] elif ( self . pnet_type == 'alex' ) : net_type = pn . alexnet self . chns = [ 64 , 192 , 384 , 256 , 256 ] elif ( self . pnet_type == 'squeeze' ) : net_type = pn . squeezenet self . chns = [ 64 , 128 , 256 , 384 , 384 , 512 , 512 ] self . L = len ( self . chns ) self . net = net_type ( pretrained = not self . pnet_rand , requires_grad = self . pnet_tune ) if ( lpips ) : self . lin0 = NetLinLayer ( self . chns [ 0 ] , use_dropout = use_dropout ) self . lin1 = NetLinLayer ( self . chns [ 1 ] , use_dropout = use_dropout ) self . lin2 = NetLinLayer ( self . chns [ 2 ] , use_dropout = use_dropout ) self . lin3 = NetLinLayer ( self . chns [ 3 ] , use_dropout = use_dropout ) self . lin4 = NetLinLayer ( self . chns [ 4 ] , use_dropout = use_dropout ) self . lins = [ self . lin0 , self . lin1 , self . lin2 , self . lin3 , self . lin4 ] if ( self . pnet_type == 'squeeze' ) : # 7 layers for squeezenet self . lin5 = NetLinLayer ( self . chns [ 5 ] , use_dropout = use_dropout ) self . lin6 = NetLinLayer ( self . chns [ 6 ] , use_dropout = use_dropout ) self . lins += [ self . lin5 , self . lin6 ] self . lins = nn . ModuleList ( self . lins ) if ( pretrained ) : if ( model_path is None ) : import inspect import os model_path = os . path . abspath ( os . path . join ( inspect . getfile ( self . __init__ ) , '..' , 'weights/v%s/%s.pth' % ( version , net ) ) ) if ( verbose ) : print ( 'Loading model from: %s' % model_path ) self . load_state_dict ( torch . load ( model_path , map_location = 'cpu' ) , strict = False ) if ( eval_mode ) : self . eval ( )