def createModel(config): model = None arch = config['arch'] lossType = config['loss'] if (arch == 'lenet5'): model = LeNet5() if (lossType == 'crossEntropy'): criterion = torch.nn.CrossEntropyLoss().to(config['device']) if (arch == 'rnn'): model = models.RNNModel(config["model"], config["ntokens"], config["emsize"], config["nhid"], config["nlayers"], config["dropout"], config["tied"]) if (lossType == 'crossEntropy'): criterion = torch.nn.NLLLoss().to(config['device']) if (arch == 'textBC'): model = TextBinaryClassificationModel(config["modelParams"]) criterion = None if (arch == 'hateSpeech'): model = HateSpeechModel(config["modelParams"]) criterion = None model.to(config['device']) return model, criterion
def calculate_kid_given_paths(paths, batch_size, cuda, dims, model_type='inception'): """Calculates the KID of two paths""" pths = [] for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) if os.path.isdir(p): pths.append(p) elif p.endswith('.npy'): np_imgs = np.load(p) if np_imgs.shape[0] > 50000: np_imgs = np_imgs[np.random.permutation(np.arange(np_imgs.shape[0]))][:50000] pths.append(np_imgs) if model_type == 'inception': block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]) elif model_type == 'lenet': model = LeNet5() model.load_state_dict(torch.load('./models/lenet.pth')) if cuda: model.cuda() act_true = _compute_activations(pths[0], model, batch_size, dims, cuda, model_type) pths = pths[1:] results = [] for j, pth in enumerate(pths): print(paths[j+1]) actj = _compute_activations(pth, model, batch_size, dims, cuda, model_type) kid_values = polynomial_mmd_averages(act_true, actj, n_subsets=100) results.append((paths[j+1], kid_values[0].mean(), kid_values[0].std())) return results
def init_erm_phase(self): if self.args.ctr_model_name == 'lenet': from models.lenet import LeNet5 ctr_phi = LeNet5().to(self.cuda) if self.args.ctr_model_name == 'alexnet': from models.alexnet import alexnet ctr_phi = alexnet(self.args.out_classes, self.args.pre_trained, 'matchdg_ctr').to(self.cuda) if self.args.ctr_model_name == 'fc': from models.fc import FC fc_layer = 0 ctr_phi = FC(self.args.out_classes, fc_layer).to(self.cuda) if 'resnet' in self.args.ctr_model_name: from models.resnet import get_resnet fc_layer = 0 ctr_phi = get_resnet(self.args.ctr_model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env).to(self.cuda) if 'densenet' in self.args.ctr_model_name: from models.densenet import get_densenet fc_layer = 0 ctr_phi = get_densenet(self.args.ctr_model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env).to(self.cuda) # Load MatchDG CTR phase model from the saved weights if self.args.os_env: base_res_dir = os.getenv( 'PT_DATA_DIR' ) + '/' + self.args.dataset_name + '/' + 'matchdg_ctr' + '/' + self.args.ctr_match_layer + '/' + 'train_' + str( self.args.train_domains) else: base_res_dir = "results/" + self.args.dataset_name + '/' + 'matchdg_ctr' + '/' + self.args.ctr_match_layer + '/' + 'train_' + str( self.args.train_domains) save_path = base_res_dir + '/Model_' + self.ctr_load_post_string + '.pth' ctr_phi.load_state_dict(torch.load(save_path)) ctr_phi.eval() #Inferred Match Case if self.args.match_case == -1: inferred_match = 1 data_match_tensor, label_match_tensor, indices_matched, perfect_match_rank = get_matched_pairs( self.args, self.cuda, self.train_dataset, self.domain_size, self.total_domains, self.training_list_size, ctr_phi, self.args.match_case, self.args.perfect_match, inferred_match) # x% percentage match initial strategy else: inferred_match = 0 data_match_tensor, label_match_tensor, indices_matched, perfect_match_rank = get_matched_pairs( self.args, self.cuda, self.train_dataset, self.domain_size, self.total_domains, self.training_list_size, ctr_phi, self.args.match_case, self.args.perfect_match, inferred_match) return data_match_tensor, label_match_tensor
def calculate_fid_given_paths(paths, batch_size, cuda, dims, bootstrap=True, n_bootstraps=10, model_type='inception'): """Calculates the FID of two paths""" pths = [] for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) if os.path.isdir(p): pths.append(p) elif p.endswith('.npy'): np_imgs = np.load(p) if np_imgs.shape[0] > 25000: np_imgs = np_imgs[:50000] pths.append(np_imgs) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] if model_type == 'inception': model = InceptionV3([block_idx]) elif model_type == 'lenet': model = LeNet5() model.load_state_dict( torch.load('/content/gan-metrics-pytorch/models/lenet.pth')) if cuda: model.cuda() act_true = _compute_activations(pths[0], model, batch_size, dims, cuda, model_type) n_bootstraps = n_bootstraps if bootstrap else 1 pths = pths[1:] results = [] for j, pth in enumerate(pths): print(paths[j + 1]) actj = _compute_activations(pth, model, batch_size, dims, cuda, model_type) fid_values = np.zeros((n_bootstraps)) with tqdm(range(n_bootstraps), desc='FID') as bar: for i in bar: act1_bs = act_true[np.random.choice(act_true.shape[0], act_true.shape[0], replace=True)] act2_bs = actj[np.random.choice(actj.shape[0], actj.shape[0], replace=True)] m1, s1 = calculate_activation_statistics(act1_bs) m2, s2 = calculate_activation_statistics(act2_bs) fid_values[i] = calculate_frechet_distance(m1, s1, m2, s2) bar.set_postfix({'mean': fid_values[:i + 1].mean()}) results.append((paths[j + 1], fid_values.mean(), fid_values.std())) return results
def createModel(config): model = None arch = config['modelConf']['arch'] if (arch == 'lenet'): model = LeNet5() if (arch == 'textBC'): model = TextBinaryClassificationModel(config["modelConf"]) model.to(config['device']) return model
def get_model(self, run_matchdg_erm=0): if self.args.model_name == 'lenet': from models.lenet import LeNet5 phi = LeNet5() if self.args.model_name == 'fc': from models.fc import FC if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = FC(self.args.out_classes, fc_layer) if self.args.model_name == 'domain_bed_mnist': from models.domain_bed_mnist import DomainBed phi = DomainBed(self.args.img_c) if self.args.model_name == 'alexnet': from models.alexnet import alexnet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = alexnet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) if 'resnet' in self.args.model_name: from models.resnet import get_resnet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = get_resnet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) if 'densenet' in self.args.model_name: from models.densenet import get_densenet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = get_densenet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) print('Model Architecture: ', self.args.model_name) self.phi = phi.to(self.cuda) self.load_model(run_matchdg_erm) return
def setup(self, flags): torch.backends.cudnn.deterministic = flags.deterministic print('torch.backends.cudnn.deterministic:', torch.backends.cudnn.deterministic) fix_all_seed(flags.seed) self.network = LeNet5(num_classes=flags.num_classes) self.network = self.network.cuda() print(self.network) print('flags:', flags) if not os.path.exists(flags.logs): os.makedirs(flags.logs) flags_log = os.path.join(flags.logs, 'flags_log.txt') write_log(flags, flags_log)
def get_model(self): if self.args.model_name == 'lenet': from models.lenet import LeNet5 phi= LeNet5() if self.args.model_name == 'alexnet': from models.alexnet import alexnet phi= alexnet(self.args.out_classes, self.args.pre_trained, self.args.method_name) if self.args.model_name == 'resnet18': from models.resnet import get_resnet phi= get_resnet('resnet18', self.args.out_classes, self.args.method_name, self.args.img_c, self.args.pre_trained) print('Model Architecture: ', self.args.model_name) phi= phi.to(self.cuda) return phi
def get_model(self, run_matchdg_erm=0): if self.args.model_name == 'lenet': from models.lenet import LeNet5 phi = LeNet5() if self.args.model_name == 'alexnet': from models.alexnet import alexnet phi = alexnet(self.args.out_classes, self.args.pre_trained, self.args.method_name) if self.args.model_name == 'domain_bed_mnist': from models.domain_bed_mnist import DomainBed phi = DomainBed(self.args.img_c) if 'resnet' in self.args.model_name: from models.resnet import get_resnet phi = get_resnet(self.args.model_name, self.args.out_classes, self.args.method_name, self.args.img_c, self.args.pre_trained) print('Model Architecture: ', self.args.model_name) self.phi = phi.to(self.cuda) self.load_model(run_matchdg_erm) return
def get_model(self): if self.args.model_name == 'lenet': from models.lenet import LeNet5 phi = LeNet5() if self.args.model_name == 'alexnet': from models.alexnet import alexnet phi = alexnet(self.args.out_classes, self.args.pre_trained, self.args.method_name) if self.args.model_name == 'domain_bed_mnist': from models.domain_bed_mnist import DomainBed phi = DomainBed(self.args.img_c) if 'resnet' in self.args.model_name: from models.resnet import get_resnet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = get_resnet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained) print('Model Architecture: ', self.args.model_name) phi = phi.to(self.cuda) return phi