(self, model='lpips', net='alex', colorspace='Lab', pnet_rand=False, pnet_tune=False, model_path=None,
            use_gpu=True, printNet=False, spatial=False, 
            is_train=False, lr=.0001, beta1=0.5, version='0.1', gpu_ids=[0])
INPUTS
            model - ['lpips'] for linearly calibrated network
                    ['baseline'] for off-the-shelf network
                    ['L2'] for L2 distance in Lab colorspace
                    ['SSIM'] for ssim in RGB colorspace
            net - ['squeeze','alex','vgg']
            model_path - if None, will look in weights/[NET_NAME].pth
            colorspace - ['Lab','RGB'] colorspace to use for L2 and SSIM
            use_gpu - bool - whether or not to use a GPU
            printNet - bool - whether or not to print network architecture out
            spatial - bool - whether to output an array containing varying distances across spatial dimensions
            is_train - bool - [True] for training mode
            lr - float - initial learning rate
            beta1 - float - initial momentum term for adam
            version - 0.1 for latest, 0.0 was original (with a bug)
            gpu_ids - int array - [0] by default, gpus to use
INPUTS
            model - ['lpips'] for linearly calibrated network
                    ['baseline'] for off-the-shelf network
                    ['L2'] for L2 distance in Lab colorspace
                    ['SSIM'] for ssim in RGB colorspace
            net - ['squeeze','alex','vgg']
            model_path - if None, will look in weights/[NET_NAME].pth
            colorspace - ['Lab','RGB'] colorspace to use for L2 and SSIM
            use_gpu - bool - whether or not to use a GPU
            printNet - bool - whether or not to print network architecture out
            spatial - bool - whether to output an array containing varying distances across spatial dimensions
            is_train - bool - [True] for training mode
            lr - float - initial learning rate
            beta1 - float - initial momentum term for adam
            version - 0.1 for latest, 0.0 was original (with a bug)
            gpu_ids - int array - [0] by default, gpus to use
INPUTS model - [ lpips ] for linearly calibrated network [ baseline ] for off - the - shelf network [ L2 ] for L2 distance in Lab colorspace [ SSIM ] for ssim in RGB colorspace net - [ squeeze alex vgg ] model_path - if None will look in weights / [ NET_NAME ] . pth colorspace - [ Lab RGB ] colorspace to use for L2 and SSIM use_gpu - bool - whether or not to use a GPU printNet - bool - whether or not to print network architecture out spatial - bool - whether to output an array containing varying distances across spatial dimensions is_train - bool - [ True ] for training mode lr - float - initial learning rate beta1 - float - initial momentum term for adam version - 0 . 1 for latest 0 . 0 was original ( with a bug ) gpu_ids - int array - [ 0 ] by default gpus to use
def initialize(self, model='lpips', net='alex', colorspace='Lab', pnet_rand=False, pnet_tune=False, model_path=None,
            use_gpu=True, printNet=False, spatial=False, 
            is_train=False, lr=.0001, beta1=0.5, version='0.1', gpu_ids=[0]):
        '''
        INPUTS
            model - ['lpips'] for linearly calibrated network
                    ['baseline'] for off-the-shelf network
                    ['L2'] for L2 distance in Lab colorspace
                    ['SSIM'] for ssim in RGB colorspace
            net - ['squeeze','alex','vgg']
            model_path - if None, will look in weights/[NET_NAME].pth
            colorspace - ['Lab','RGB'] colorspace to use for L2 and SSIM
            use_gpu - bool - whether or not to use a GPU
            printNet - bool - whether or not to print network architecture out
            spatial - bool - whether to output an array containing varying distances across spatial dimensions
            is_train - bool - [True] for training mode
            lr - float - initial learning rate
            beta1 - float - initial momentum term for adam
            version - 0.1 for latest, 0.0 was original (with a bug)
            gpu_ids - int array - [0] by default, gpus to use
        '''
        self.use_gpu = use_gpu
        self.gpu_ids = gpu_ids
        self.model = model
        self.net = net
        self.is_train = is_train
        self.spatial = spatial
        self.model_name = '%s [%s]'%(model,net)

        if(self.model == 'lpips'): # pretrained net + linear layer
            self.net = lpips.LPIPS(pretrained=not is_train, net=net, version=version, lpips=True, spatial=spatial, 
                pnet_rand=pnet_rand, pnet_tune=pnet_tune, 
                use_dropout=True, model_path=model_path, eval_mode=False)
        elif(self.model=='baseline'): # pretrained network
            self.net = lpips.LPIPS(pnet_rand=pnet_rand, net=net, lpips=False)
        elif(self.model in ['L2','l2']):
            self.net = lpips.L2(use_gpu=use_gpu,colorspace=colorspace) # not really a network, only for testing
            self.model_name = 'L2'
        elif(self.model in ['DSSIM','dssim','SSIM','ssim']):
            self.net = lpips.DSSIM(use_gpu=use_gpu,colorspace=colorspace)
            self.model_name = 'SSIM'
        else:
            raise ValueError("Model [%s] not recognized." % self.model)

        self.parameters = list(self.net.parameters())

        if self.is_train: # training mode
            # extra network on top to go from distances (d0,d1) => predicted human judgment (h*)
            self.rankLoss = lpips.BCERankingLoss()
            self.parameters += list(self.rankLoss.net.parameters())
            self.lr = lr
            self.old_lr = lr
            self.optimizer_net = torch.optim.Adam(self.parameters, lr=lr, betas=(beta1, 0.999))
        else: # test mode
            self.net.eval()

        if(use_gpu):
            self.net.to(gpu_ids[0])
            self.net = torch.nn.DataParallel(self.net, device_ids=gpu_ids)
            if(self.is_train):
                self.rankLoss = self.rankLoss.to(device=gpu_ids[0]) # just put this on GPU0

        if(printNet):
            print('---------- Networks initialized -------------')
            networks.print_network(self.net)
            print('-----------------------------------------------')
def initialize ( self , model = 'lpips' , net = 'alex' , colorspace = 'Lab' , pnet_rand = False , pnet_tune = False , model_path = None , use_gpu = True , printNet = False , spatial = False , is_train = False , lr = .0001 , beta1 = 0.5 , version = '0.1' , gpu_ids = [ 0 ] ) : self . use_gpu = use_gpu self . gpu_ids = gpu_ids self . model = model self . net = net self . is_train = is_train self . spatial = spatial self . model_name = '%s [%s]' % ( model , net ) if ( self . model == 'lpips' ) : # pretrained net + linear layer self . net = lpips . LPIPS ( pretrained = not is_train , net = net , version = version , lpips = True , spatial = spatial , pnet_rand = pnet_rand , pnet_tune = pnet_tune , use_dropout = True , model_path = model_path , eval_mode = False ) elif ( self . model == 'baseline' ) : # pretrained network self . net = lpips . LPIPS ( pnet_rand = pnet_rand , net = net , lpips = False ) elif ( self . model in [ 'L2' , 'l2' ] ) : self . net = lpips . L2 ( use_gpu = use_gpu , colorspace = colorspace ) # not really a network, only for testing self . model_name = 'L2' elif ( self . model in [ 'DSSIM' , 'dssim' , 'SSIM' , 'ssim' ] ) : self . net = lpips . DSSIM ( use_gpu = use_gpu , colorspace = colorspace ) self . model_name = 'SSIM' else : raise ValueError ( "Model [%s] not recognized." % self . model ) self . parameters = list ( self . net . parameters ( ) ) if self . is_train : # training mode # extra network on top to go from distances (d0,d1) => predicted human judgment (h*) self . rankLoss = lpips . BCERankingLoss ( ) self . parameters += list ( self . rankLoss . net . parameters ( ) ) self . lr = lr self . old_lr = lr self . optimizer_net = torch . optim . Adam ( self . parameters , lr = lr , betas = ( beta1 , 0.999 ) ) else : # test mode self . net . eval ( ) if ( use_gpu ) : self . net . to ( gpu_ids [ 0 ] ) self . net = torch . nn . DataParallel ( self . net , device_ids = gpu_ids ) if ( self . is_train ) : self . rankLoss = self . rankLoss . to ( device = gpu_ids [ 0 ] ) # just put this on GPU0 if ( printNet ) : print ( '---------- Networks initialized -------------' ) networks . print_network ( self . net ) print ( '-----------------------------------------------' )