def __init__(self, args): self.criterion = nn.CrossEntropyLoss().cuda() self.lr = args.lr self.epochs = args.epochs self.save_dir = './' + args.save_dir #later change if (os.path.exists(self.save_dir) == False): os.mkdir(self.save_dir) if (args.model == 'vgg16'): self.model = VGG('VGG16', 0) self.optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, self.model.parameters()), lr=self.lr, momentum=args.momentum, weight_decay=args.weight_decay) self.model = torch.nn.DataParallel(self.model) self.model.cuda() elif (args.model == 'dpp_vgg16'): self.model = integrated_kernel(args) self.optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, self.model.parameters()), lr=self.lr, momentum=args.momentum, weight_decay=args.weight_decay) #Parallel num_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad) print('The number of parametrs of models is', num_params) if (args.save_load): location = args.save_location print("locaton", location) checkpoint = torch.load(location) self.model.load_state_dict(checkpoint['state_dict'])
def train_tlfs(dataset_path, model_path, epochs, ortho=False, ortho_factor=0.1): dataset = DataSet() dataset.load(path=dataset_path, blocks=["velocity"], shuffle=False) normalization_shift = np.mean(dataset.train.velocity.data, axis=(0, 1, 2)) normalization_factor = np.std(dataset.train.velocity.data, axis=(0, 1, 2)) normalization_factor[-1] = 1.0 normalization_factor *= 1.0 / 255.0 dataset.train.velocity.normalize(shift=normalization_shift, factor=normalization_factor) dataset.val.velocity.normalize(shift=normalization_shift, factor=normalization_factor) hist = {} ae = VGG(input_shape=(None, None, 3), ortho_regularizer=ortho, ortho_strength=ortho_factor) # if os.path.isfile(model_path): # ae.load_model(path=model_path) hist = ae.train(epochs, dataset=dataset, batch_size=32, augment=False) ae.save_model(path=model_path) return hist
def get_model(self): # Get modeling model_name = self.argument.model if model_name == 'AlexNet': from models.alexnet import AlexNet return AlexNet() elif model_name == 'vgg11': from models.vgg import VGG return VGG(vgg_type='VGG11') elif model_name == 'vgg13': from models.vgg import VGG return VGG(vgg_type='VGG13') elif model_name == 'vgg16': from models.vgg import VGG return VGG(vgg_type='VGG16') elif model_name == 'vgg19': from models.vgg import VGG return VGG(vgg_type='VGG19') elif model_name == 'GoogleNet_inception_v1': from models.googlenet import GoogleNetInceptionV1 return GoogleNetInceptionV1() elif model_name == 'GoogleNet_inception_v2': from models.googlenet import GoogleNetInceptionV2 return GoogleNetInceptionV2() else: raise NotImplemented()
def __init__(self, model='VGG19', main_dir=main_dir_path, face_detector='undefined', use_cuda=False, reliability=0.8): self.main_dir = main_dir self.face_detector = face_detector self.use_cuda = use_cuda self.reliability = reliability self.cut_size = 44 self.transform_test = transforms.Compose([ transforms.TenCrop(self.cut_size), transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops])), ]) self.class_names = [ 'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral' ] if model == 'VGG19': self.net = VGG('VGG19') elif model == 'Resnet18': self.net = ResNet18() self.checkpoint = torch.load(os.path.join( self.main_dir + 'pretrained_model/' + model, 'PrivateTest_model.t7'), map_location='cpu') self.net.load_state_dict(self.checkpoint['net']) if self.use_cuda: self.net.cuda() self.net.eval()
def pre_train(epoch,batch_size,learning_rate,weight_decay,momentum): start_epoch=0 use_cuda=False # Device configuration device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # read data # transform lets you apply transform function to each image and create a pipeline of sequence of operations print('==> Preparing data..') transform_train = transforms.Compose([ # transforms.RandomCrop(64, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), # transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) transform_test = transforms.Compose([ # transforms.RandomCrop(64, padding=4), transforms.ToTensor(), # transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) # calls the TinyImageNet init function that reads data from the training or val directory of dataset train_set = TinyImageNet(root='./data', train=True, transform=transform_train,download=False) #Data loader. Combines a dataset and a sampler, and provides single- or multi-process iterators over the dataset. train_loader = torch.utils.data.DataLoader(train_set, batch_size, shuffle=True, num_workers=2) # calls the TinyImageNet init function that reads data from the training or val directory of dataset test_set = TinyImageNet(root='./data', train=False, transform=transform_test,download=False) #Data loader. Combines a dataset and a sampler, and provides single- or multi-process iterators over the dataset. test_loader = torch.utils.data.DataLoader(test_set, batch_size=50, shuffle=True, num_workers=2) print('==> creating model..') net = VGG('VGG3') criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(),learning_rate, momentum, weight_decay) print( "trainset num = {}".format(len(train_set)) ) print( "trainset num = {}".format(len(test_set)) ) for epoch in range(0, epoch): train(epoch,net,criterion,optimizer,train_loader)
def train_model_opt(parameters): print("Training model with hyper-parameters: {}\n\n\n".format(parameters)) args = parse_args() # Load the config file cfg = load_config(args) # Make the results reproducible fix_seed(cfg.SEED) cfg.TRAIN.LR = parameters[0] cfg.TRAIN.BATCH_SIZE = int(parameters[1]) # Preparing data (train_loader, valid_loader) = prepare_dataloaders(cfg) # Define model architecture vgg19 = VGG("VGG19", num_classes_length=7, num_classes_digits=10) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("Device used: ", device) # We return negative accuracy since we have minimization function (gp_minimize) return -train_model(vgg19, cfg=cfg, train_loader=train_loader, valid_loader=valid_loader, device=device)
def main(): global args args = parser.parse_args() print(args) # create model print("=> creating model '{}'".format(args.arch)) if args.arch == 'vgg16': from models.vgg import VGG model = nn.DataParallel(VGG('VGG16', nclass=args.num_classes), device_ids=range(1)) if args.arch == 'resnet18': from models.resnet import ResNet18 model = nn.DataParallel(ResNet18().cuda()) checkpoint = torch.load('./checkpoint/cifar10_vgg16_teacher.pth') model.load_state_dict(checkpoint) cudnn.benchmark = True # Data loading code mix_img_val_loader = torch.utils.data.DataLoader( active_query_loader(transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) ## image extraction logits_extract(mix_img_val_loader, model)
def model(key): key = key.lower() if key[:3] == 'vgg': return VGG("VGG" + key[3:]) if key[:3] == "dpn": return DPN(key) if key[:8] == 'densenet': return ChooseDenseNet(key[8:])
def predict_test_data(dataset_path, model_path): dataset = DataSet() dataset.load(path=dataset_path, blocks=["velocity"], shuffle=False) normalization_shift = np.mean(dataset.train.velocity.data, axis=(0, 1, 2)) normalization_factor = np.std(dataset.train.velocity.data, axis=(0, 1, 2)) normalization_factor[-1] = 1.0 normalization_factor *= 1.0 / 255.0 dataset.test.velocity.normalize(shift=normalization_shift, factor=normalization_factor) ae = VGG(input_shape=(None, None, 3)) ae.load_model(path=model_path) orig = dataset.test.velocity pred = ae.predict(orig.data, batch_size=8) orig.denormalize() pred *= normalization_factor pred -= normalization_factor return orig.data, pred
def get_model(model='ResNet18', **kwargs): if model == 'ResNet18': return ResNet18(n_classes=kwargs.get('n_classes', 10)) elif 'VGG' in model: return VGG( vgg_name=model, k=kwargs['k'], dropout=kwargs.get('dropout', None), n_classes=kwargs.get('n_classes', 10), ) elif 'LeNet' == model: return LeNet(dropout=kwargs.get('dropout', None)) else: raise NotImplementedError('unknown {} model'.format(model))
def __init__(self, net_12_param_path=None, net_48_param_path=None, net_vgg_param_path=None, use_cuda=True, pthreshold=0.7, rthershold=0.9): if use_cuda == False: self.device = torch.device('cpu') else: self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') if net_12_param_path is not None: self.net_12 = Net12() self.net_12.load_state_dict( torch.load(net_12_param_path, map_location=lambda storage, loc: storage)) self.net_12.to(self.device) self.net_12.eval() if net_48_param_path is not None: self.net_48 = Net48() self.net_48.load_state_dict( torch.load(net_48_param_path, map_location=lambda storage, loc: storage)) self.net_48.to(self.device) self.net_48.eval() if net_vgg_param_path is not None: self.net_vgg = VGG('VGG19') self.net_vgg.load_state_dict( torch.load(net_vgg_param_path, map_location=lambda storage, loc: storage)) self.net_vgg.to(self.device) self.net_vgg.eval() self.pthreshold = pthreshold self.rthershold = rthershold
def get_net(network: str, num_classes) -> torch.nn.Module: return VGG('VGG16', num_classes=num_classes) if network == 'VGG16' else \ ResNet34(num_classes=num_classes) if network == 'ResNet34' else \ PreActResNet18(num_classes=num_classes) if network == 'PreActResNet18' else \ GoogLeNet(num_classes=num_classes) if network == 'GoogLeNet' else \ densenet_cifar(num_classes=num_classes) if network == 'densenet_cifar' else \ ResNeXt29_2x64d(num_classes=num_classes) if network == 'ResNeXt29_2x64d' else \ MobileNet(num_classes=num_classes) if network == 'MobileNet' else \ MobileNetV2(num_classes=num_classes) if network == 'MobileNetV2' else \ DPN92(num_classes=num_classes) if network == 'DPN92' else \ ShuffleNetG2(num_classes=num_classes) if network == 'ShuffleNetG2' else \ SENet18(num_classes=num_classes) if network == 'SENet18' else \ ShuffleNetV2(1, num_classes=num_classes) if network == 'ShuffleNetV2' else \ EfficientNetB0( num_classes=num_classes) if network == 'EfficientNetB0' else None
def make (cf): # Create the optimizer print('\n > Creating optimizer...') optimizer = Optimizer_Factory().make(cf) # Build model print('\n > Building model...') if cf.model_name == 'vgg16': model = VGG(cf.num_classes, optimizer).build_vgg(cf, img_rows=224, img_cols=224, input_channels=3, n_layers=16) elif cf.model_name == 'resnet50': model = myResNet50(cf.num_classes, optimizer).build_resnet50(cf, img_rows=224, img_cols=224, input_channels=3, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) else: raise ValueError('Unknow model') return model, optimizer
def get_model(args, parallel=True, ckpt_path=False): if args.clf == 'fcn': print('Initializing FCN...') model = FCN(args.input_size, args.output_size) elif args.clf == 'mlp': print('Initializing MLP...') model = MLP(args.input_size, args.output_size) elif args.clf == 'svm': print('Initializing SVM...') model = SVM(args.input_size, args.output_size) elif args.clf == 'cnn': print('Initializing CNN...') model = CNN(nc=args.num_channels, fs=args.cnn_view) elif args.clf == 'resnet18': print('Initializing ResNet18...') model = resnet.resnet18(num_channels=args.num_channels, num_classes=args.output_size) elif args.clf == 'vgg19': print('Initializing VGG19...') model = VGG(vgg_name=args.clf, num_channels=args.num_channels, num_classes=args.output_size) elif args.clf == 'unet': print('Initializing UNet...') model = UNet(in_channels=args.num_channels, out_channels=args.output_size) num_params, num_layers = get_model_size(model) print("# params: {}\n# layers: {}".format(num_params, num_layers)) if ckpt_path: model.load_state_dict(torch.load(ckpt_path)) print('Load init: {}'.format(ckpt_path)) if parallel: model = nn.DataParallel(model.to(get_device(args)), device_ids=args.device_id) else: model = model.to(get_device(args)) loss_type = 'hinge' if args.clf == 'svm' else args.loss_type print("Loss: {}".format(loss_type)) return model, loss_type
def integrated_kernel(args): models = args.total_models.split(',') k_number_list = args.k_number.split(',') for i in range(len(k_number_list)): k_number_list[i] = int(k_number_list[i]) #models is number of [1,2,3] total_models = [] for model in models: temp = get_model(args, model) total_models.append(temp) #Get Kernel weight and Bias weights, bias = get_models_kernel(total_models) #Concat total kernel weight and Bias all_weights = [] for i in range(len(weights[0])): allModel = weights[0][i] for j in range(1, len(weights)): allModel = np.concatenate([allModel, weights[j][i]], 0) all_weights.append(allModel) concat_weights = cpu_to_tensor(all_weights) #k_dpp_kernel, k_number_list k_weights = [] k_bias = [] len_number = len(concat_weights) - 1 for i in range(len(k_number_list)): t_weight = k_dpp_kernel(concat_weights[len_number], k_number_list[i]) len_number -= 1 k_weights.append(t_weight) k_weights = tensor_to_parameter(k_weights) #model push if (args.model == 'dpp_vgg16'): model = VGG('VGG16_2', k_number_list[0]) model = nn.DataParallel(model).cuda() #best_model b_model = get_model(args, args.best_model_num) model = push_weight(model, b_model, k_weights, len(k_number_list)) return model
def get_model(args, number): model = None if (args.model == 'dpp_vgg16'): model = VGG('VGG16', 0) model = nn.DataParallel(model) model.cuda() save_name = 'checkpoint_model_' + str(number) + '.tar' model_load = torch.load('save_vgg16_cifar10_best/' + save_name) model.load_state_dict(model_load['state_dict']) if model is None: print("Model is None") raise TypeError return model
def main(): global args args = parser.parse_args() print(args) # create model print("=> creating model '{}'".format(args.arch)) print(torch.cuda.device_count()) if args.arch == 'vgg16': from models.vgg import VGG model = nn.DataParallel(VGG('VGG16', nclass=args.num_classes)) elif args.arch == 'resnet18': from models.resnet import ResNet18 model = nn.DataParallel(ResNet18().cuda()) else: raise NotImplementedError('Invalid model') ############################################################ ############# Modify It To Current Student Model ################# checkpoint = torch.load( './active_student_models/cifar10_vgg_student_model_1000.pth') model.load_state_dict(checkpoint) ############################################################ cudnn.benchmark = True model.cuda() # Data loading code mix_img_val_loader = torch.utils.data.DataLoader( mix_img_query_loader(transforms.Compose([ transforms.ToTensor(), ])), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) ## image extraction logits_extract(mix_img_val_loader, model)
def build_SFDetVGG(mode, new_size, anchors, class_count): base = VGG(config=base_config[str(new_size)], in_channels=3) extras = get_extras(in_channels=1024) fusion_module = get_fusion_module(config=fusion_config[str(new_size)], base=base.layers, extras=extras) pyramid_module = get_pyramid_module(config=pyramid_config[str(new_size)]) head = multibox(config=mbox_config[str(new_size)], class_count=class_count) return SFDetVGG(mode=mode, base=base.layers, extras=extras, fusion_module=fusion_module, pyramid_module=pyramid_module, head=head, anchors=anchors, class_count=class_count)
def main(): parser = argparse.ArgumentParser(description='DeepModels') parser.add_argument('-l', '--learning', type=float, default=0.0000001, help='Learning Rate') parser.add_argument('-e', '--epochs', type=int, default=1, help='Epochs') parser.add_argument('-b', '--batch', type=int, default=2, help='Batch Size') args = parser.parse_args() learning_rate = args.learning epochs = args.epochs batch_size = args.batch print(learning_rate, epochs, batch_size) dataset = Cifar10() # dataset = Cifar100() # dataset = Mnist() model_name = "VGG" # model = AlexNet() model = VGG() # model = GoogLeNet() # model = ResNet(model_type="101") # model = InceptionV3() # model = DenseNet(model_type="201") # training trainer = ClfTrainer(model, dataset) trainer.run_training(epochs, batch_size, learning_rate, './cifar10-ckpt', model_name)
def main(): parser = argparse.ArgumentParser(description='Attack CIFAR10 models') parser.add_argument('-a', '--attack', required=True, type=str, help='name of attacker: {NES, CW, L2, Linf, OPT_lf}') parser.add_argument('-m', '--model', required=True, type=str, help='CIFAR10 models: {rse, adv_vi, vi, adv, plain}') parser.add_argument('-s', '--steps', required=True, type=int, help='number of steps for attacker') parser.add_argument('-e', '--eps', required=True, type=str, help='list of epsilon values separated by comma, i.e. 0.2 or 0.05,0.01') opt = parser.parse_args() max_norm = [float(s) for s in opt.eps.split(',')] batch_size = 1 # choose attack method if opt.attack == 'CW': attack_f = cw elif opt.attack == 'L2': attack_f = L2_PGD elif opt.attack == 'Linf': attack_f = Linf_PGD elif opt.attack == 'NES': attack_f = nes elif opt.attack == 'OPT_lf': attack_f = opt_lf batch_size = 1 else: raise ValueError(f'invalid attack function:{opt.attack}') # load cifar10 dataset root = "~/datasets" nclass = 10 img_width = 32 transform_test = transforms.Compose([ transforms.ToTensor(), ]) testset = torchvision.datasets.CIFAR10(root, train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2) # set the model model_out = "../cifar10-checkpoint/" if opt.model == 'rse': noise_init = 0.2 noise_inner = 0.1 model_out += "cifar10_vgg_rse.pth" from models.vgg_rse import VGG net = nn.DataParallel(VGG('VGG16', nclass, noise_init, noise_inner, img_width=img_width).cuda()) elif opt.model == 'adv_vi': sigma_0 = 0.08 N = 50000 init_s = 0.08 model_out += "cifar10_vgg_adv_vi.pth" from models.vgg_vi import VGG net = nn.DataParallel(VGG(sigma_0, N, init_s, 'VGG16', nclass, img_width=img_width).cuda()) elif opt.model == 'vi': sigma_0 = 0.15 N = 50000 init_s = 0.15 model_out += "cifar10_vgg_vi.pth" from models.vgg_vi import VGG net = nn.DataParallel(VGG(sigma_0, N, init_s, 'VGG16', nclass, img_width=img_width).cuda()) elif opt.model == 'adv': model_out += "cifar10_vgg_adv.pth" from models.vgg import VGG net = nn.DataParallel(VGG('VGG16', nclass).cuda()) elif opt.model == 'plain': model_out += "cifar10_vgg_plain.pth" from models.vgg import VGG net = nn.DataParallel(VGG('VGG16', nclass).cuda()) else: raise ValueError(f'invalid cifar10 model: {opt.model}') net.load_state_dict(torch.load(model_out)) net.cuda() net.eval() loss_f = nn.CrossEntropyLoss() cudnn.benchmark = True softmax = nn.Softmax(dim=1) for eps in max_norm: print(f'Using attack {opt.attack} on CIFAR10 vgg_{opt.model} for eps = {eps}:') success = 0 count = 0 total = 0 max_iter = 100 distortion = 0.0 for it, (x, y) in enumerate(testloader): if it+1 > max_iter: continue #if it<75: # continue print(f'batch {it+1}/{max_iter}') #print(x,y) #print(torch.max(x),torch.min(x)) x, y = x.cuda(), y.cuda() #y_adv = (y+1)%10 y_out = torch.max(softmax(net(x)[0]), dim=1)[1] #filter the wrong predictions x = x[y_out.eq(y)] y = y[y_out.eq(y)] #print(x,y) if x.size()[0]==0: continue #print(x[y_out.eq(y)].size()) #pdb.set_trace() with torch.no_grad(): x_adv = attack_f(x, y, net, opt.steps, eps) pred = torch.max(softmax(net(x_adv)[0]), dim=1)[1] is_adv = pred.eq(y) == 0 #y_out_str = ''.join(map(str, y_out.cpu().numpy())) pred_str = ''.join(map(str, pred.cpu().numpy())) is_adv_str = ''.join(map(str, is_adv.cpu().numpy())) y_true_str = ''.join(map(str, y.cpu().numpy())) print(f'\ttrue image labels (y_true) : {y_true_str}') #print(f'\toriginal images predicted : {y_out_str}') #print(y_adv.item()) print(f'\tadversarial examples predicted: {pred_str}') print(f'\tis adversarial? {is_adv_str}') new_success = torch.sum(is_adv).item() new_distortion = distance2(x_adv, x, is_adv.cpu().numpy(), opt.attack) print(f'\tsuccess = {new_success}\n\ttotal={x.size()[0]}\n\tdistortion = {new_distortion}') if new_distortion and new_distortion[0]<=0.2: success += new_success total += x.size()[0] if len(new_distortion) > 0: count += 1 distortion += np.mean(new_distortion) #if count == 0: # print('Found no adversarial examples!!!') #else: # print(f'Average distortion: {distortion/count}, average success rate = {success/(max_iter * batch_size)}') print(success,total)
'Atlantean': 34, 'Sylheti': 22, 'Mongolian': 33, 'Aurek': 34, 'Angelic': 23, 'ULOG': 33, 'Oriya': 33, 'Avesta': 31, 'Tibetan': 43, 'Tengwar': 28, 'Keble': 25, 'Ge_ez': 31, 'Glagolitic': 46 } model = VGG(n_layer='4+2', in_channels=1).to(device) model.load_state_dict(torch.load(args.pretrain_dir), strict=False) model.last = Identity() init_feat_extractor = model acc = {} nmi = {} ari = {} for key, alphabetStr in omniglot_evaluation_alphabets_mapping.items(): runner_name = os.path.basename(__file__).split(".")[0] model_dir = args.exp_root + '{}/{}'.format(runner_name, args.subfolder_name) if not os.path.exists(model_dir): os.makedirs(model_dir) args.model_dir = model_dir + '/' + 'vgg6_{}.pth'.format(alphabetStr) args.save_txt_path = args.exp_root + '{}/{}'.format( runner_name, args.save_txt_name)
transform_test = transforms.Compose([ transforms.Resize(img_width), transforms.ToTensor(), ]) trainset = torchvision.datasets.ImageFolder(opt.root+'/sngan_dog_cat', transform=transform_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2) testset = torchvision.datasets.ImageFolder(opt.root+'/sngan_dog_cat_val', transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2) else: raise NotImplementedError('Invalid dataset') # Model if opt.model == 'vgg': from models.vgg import VGG net = nn.DataParallel(VGG('VGG16', nclass, img_width=img_width).cuda()) elif opt.model == 'aaron': from models.aaron import Aaron net = nn.DataParallel(Aaron(nclass).cuda()) else: raise NotImplementedError('Invalid model') if opt.resume: logging.info(f'==> Resume from {opt.model_out}') net.load_state_dict(torch.load(opt.model_out)) cudnn.benchmark = True # Loss function criterion = nn.CrossEntropyLoss()
class CNNDetector(object): def __init__(self, net_12_param_path=None, net_48_param_path=None, net_vgg_param_path=None, use_cuda=True, pthreshold=0.7, rthershold=0.9): if use_cuda == False: self.device = torch.device('cpu') else: self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') if net_12_param_path is not None: self.net_12 = Net12() self.net_12.load_state_dict( torch.load(net_12_param_path, map_location=lambda storage, loc: storage)) self.net_12.to(self.device) self.net_12.eval() if net_48_param_path is not None: self.net_48 = Net48() self.net_48.load_state_dict( torch.load(net_48_param_path, map_location=lambda storage, loc: storage)) self.net_48.to(self.device) self.net_48.eval() if net_vgg_param_path is not None: self.net_vgg = VGG('VGG19') self.net_vgg.load_state_dict( torch.load(net_vgg_param_path, map_location=lambda storage, loc: storage)) self.net_vgg.to(self.device) self.net_vgg.eval() self.pthreshold = pthreshold self.rthershold = rthershold def generate_stage(self, img): """ Args: img: source image Rets: bounding boxes, numpy array, n x 5 Generate face bounding box proposals using net-12. """ proposals = list() downscaling_factor = 0.7 current_height, current_width, _ = img.shape current_scale = 1.0 # limit maximum height to 500 if current_height > 500: current_scale = 500.0 / current_height receptive_field = 12 stride = 2 while True: # get the resized image at current scale im_resized = imageproc.resize_image(img, current_scale) current_height, current_width, _ = im_resized.shape if min(current_height, current_width ) <= receptive_field: # receptive field of the net-12 break # transpose hwc (Numpy) to chw (Tensor) feed_imgs = ( transforms.ToTensor()(im_resized)).unsqueeze(0).float() # feed to net-12 with torch.no_grad(): feed_imgs = feed_imgs.to(self.device) bbox_class, bbox_regress = self.net_12(feed_imgs) bbox_class = bbox_class.cpu().squeeze(0).detach().numpy() bbox_regress = bbox_regress.cpu().squeeze(0).detach().numpy() # FILTER classes with threshold up_thresh_masked_index = np.where( bbox_class > self.pthreshold) # threshold up_thresh_masked_index = up_thresh_masked_index[1:3] filtered_results = np.vstack([ # pixel coordinate for receptive window np.round((stride * up_thresh_masked_index[1]) / current_scale), np.round((stride * up_thresh_masked_index[0]) / current_scale), np.round( (stride * up_thresh_masked_index[1] + receptive_field) / current_scale), np.round( (stride * up_thresh_masked_index[0] + receptive_field) / current_scale), # original bbox output form network bbox_class[0, up_thresh_masked_index[0], up_thresh_masked_index[1]], bbox_regress[:, up_thresh_masked_index[0], up_thresh_masked_index[1]], ]).T keep_mask = imageproc.neighbour_supression(filtered_results[:, :5], 0.7, 'Union') filtered_results = filtered_results[keep_mask] current_scale *= downscaling_factor proposals.append(filtered_results) # aggregate proposals from list proposals = np.vstack(proposals) keep_mask = imageproc.neighbour_supression(proposals[:, 0:5], 0.5, 'Union') proposals = proposals[keep_mask] if len(proposals) == 0: # no proposal generated return None # convert multi-sacle bbox to unified bbox at original img scale receptive_window_width_pixels = proposals[:, 2] - proposals[:, 0] + 1 receptive_window_height_pixels = proposals[:, 3] - proposals[:, 1] + 1 bbox_aligned = np.vstack([ proposals[:, 0] + proposals[:, 5] * receptive_window_width_pixels, # upleft_x proposals[:, 1] + proposals[:, 6] * \ receptive_window_height_pixels, # upleft_y proposals[:, 2] + proposals[:, 7] * \ receptive_window_width_pixels, # downright_x proposals[:, 3] + proposals[:, 8] * \ receptive_window_height_pixels, # downright_y proposals[:, 4], # classes ]) bbox_aligned = bbox_aligned.T return bbox_aligned def refine_stage(self, img, proposal_bbox): """ Args: img: source image proposal_bbox: bounding box proposals from generate stage Rets: bounding boxes, numpy array, n x 5 Apply delta corrdinate to bboxes using net-48. """ if proposal_bbox is None: return None, None proposal_bbox = imageproc.convert_to_square(proposal_bbox) cropped_tmp_tensors = imageproc.bbox_crop(img, proposal_bbox) # feed to net-48 with torch.no_grad(): feed_imgs = Variable(torch.stack(cropped_tmp_tensors)) feed_imgs = feed_imgs.to(self.device) bbox_class, bbox_regress, landmark = self.net_48(feed_imgs) bbox_class = bbox_class.cpu().detach().numpy() bbox_regress = bbox_regress.cpu().detach().numpy() landmark = landmark.cpu().detach().numpy() # threshold up_thresh_masked_index = np.where(bbox_class > self.rthershold)[0] boxes = proposal_bbox[up_thresh_masked_index] bbox_class = bbox_class[up_thresh_masked_index] bbox_regress = bbox_regress[up_thresh_masked_index] landmark = landmark[up_thresh_masked_index] # aggregate keep_mask = imageproc.neighbour_supression(boxes, 0.5, mode="Minimum") if len(keep_mask) == 0: return None, None proposals = boxes[keep_mask] bbox_class = bbox_class[keep_mask] bbox_regress = bbox_regress[keep_mask] landmark = landmark[keep_mask] receptive_window_width_pixels = proposals[:, 2] - proposals[:, 0] + 1 receptive_window_height_pixels = proposals[:, 3] - proposals[:, 1] + 1 # get new bounding boxes boxes_align = np.vstack([ proposals[:, 0] + bbox_regress[:, 0] * receptive_window_width_pixels, # upleft_x proposals[:, 1] + bbox_regress[:, 1] * receptive_window_height_pixels, # upleft_y proposals[:, 2] + bbox_regress[:, 2] * receptive_window_width_pixels, # downright_x proposals[:, 3] + bbox_regress[:, 3] * receptive_window_height_pixels, # downright_y bbox_class[:, 0], ]).T # get facial landmarks align_landmark_topx = proposals[:, 0] align_landmark_topy = proposals[:, 1] landmark_align = np.vstack([ align_landmark_topx + landmark[:, 0] * receptive_window_width_pixels, # lefteye_x align_landmark_topy + landmark[:, 1] * receptive_window_height_pixels, # lefteye_y align_landmark_topx + landmark[:, 2] * receptive_window_width_pixels, # righteye_x align_landmark_topy + landmark[:, 3] * receptive_window_height_pixels, # righteye_y align_landmark_topx + landmark[:, 4] * receptive_window_width_pixels, # nose_x align_landmark_topy + landmark[:, 5] * receptive_window_height_pixels, # nose_y align_landmark_topx + landmark[:, 6] * receptive_window_width_pixels, # leftmouth_x align_landmark_topy + landmark[:, 7] * receptive_window_height_pixels, # leftmouth_y align_landmark_topx + landmark[:, 8] * receptive_window_width_pixels, # rightmouth_x align_landmark_topy + landmark[:, 9] * receptive_window_height_pixels, # rightmouth_y ]).T return boxes_align, landmark_align def detect_face(self, img, atleastone=True): """ Args: img: source image atleastone: whether the size of image should be retured when no face is found Rets: bounding boxes, numpy array landmark, numpy array Detect faces in the image. """ if self.net_12: boxes_align = self.generate_stage(img) if self.net_48: boxes_align, landmark_align = self.refine_stage(img, boxes_align) if boxes_align is None: if atleastone: boxes_align = np.array([[0, 0, img.shape[1], img.shape[0]]]) else: boxes_align = np.array([]) if landmark_align is None: landmark_align = np.array([]) return boxes_align, landmark_align def crop_faces(self, img, bbox=None): """ see imageproc.bbox_crop """ return imageproc.bbox_crop(img, bbox, totensor=False) def vgg_net(self, img): """ Args: img: source image Rets: prob of each expression: in order of ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] Detect facial expression in the image. """ grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) grey_img = cv2.resize(grey_img, (48, 48)).astype(np.uint8) grey_img = grey_img[:, :, np.newaxis] grey_img = np.concatenate((grey_img, grey_img, grey_img), axis=2) receptive_field = 44 # get ten crops at the corners and center tencrops = transforms.Compose([ transforms.ToPILImage(), transforms.TenCrop(receptive_field), transforms.Lambda(lambda crops: torch.stack( [transforms.ToTensor()(crop) for crop in crops])), ]) inputs = tencrops(grey_img) ncrops, c, h, w = np.shape(inputs) # feed to VGG net with torch.no_grad(): inputs = inputs.view(-1, c, h, w) inputs = inputs.to(self.device) outputs = self.net_vgg(inputs) # get mean value across all the crops outputs_avg = outputs.view(ncrops, -1).mean(0) probabilities = F.softmax(outputs_avg, dim=0) # max prob as the detection resutlt _, predicted_class = torch.max(outputs_avg.data, 0) probabilities = probabilities.cpu().numpy() predicted_class = int(predicted_class.cpu().numpy()) return probabilities, predicted_class
singleloader = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=0) classes = ('plane', 'car', 'bird', 'cat', 'deeer', 'dog', 'frog', 'horse', 'ship', 'truck') # setup device for training device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Configure the Network # You can swap out any kind of architectire from /models in here # model_fn = ResNet18() # model_fn = VGG('VGG11') model_fn = VGG('VGG11') model_fn = model_fn.to(device) # Load the model model_fn.load_state_dict(torch.load(weights_path)) # Setup the loss function criterion = nn.CrossEntropyLoss() eval(model=model_fn, loss_fn=criterion, dataloader=testloader) compute_seperate_losses(model=model_fn, loss_fn=criterion, dataloader=singleloader)
##################### # params ##################### args = parser.parse_args() test_batch_size = 256 batch_size = 64 pretrain_weight = "YOUR_SR_MODEL_PATH" prune_model_save_path = "YOUR_PRUNE_MODEL_SAVE_PATH" data_root = "YOUR_CIFAR_PATH" cpu_device = "cpu" gpu_device = "cuda" ##################### # model ##################### model = VGG(dataset='cifar10', depth=args.depth) # model.to(gpu_device) if pretrain_weight: if os.path.isfile(pretrain_weight): print("=> loading checkpoint '{}'".format(pretrain_weight)) checkpoint = torch.load(pretrain_weight) args.start_epoch = checkpoint['epoch'] best_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {}) Prec1: {:f}" .format(pretrain_weight, checkpoint['epoch'], best_prec1)) else: print("=> no checkpoint found at '{}'".format(args.model))
def run_magnet_loss(): ''' Test function for the magnet loss ''' m = 8 d = 8 k = 8 alpha = 1.0 batch_size = m * d global plotter plotter = VisdomLinePlotter(env_name=args.name) trainloader, testloader, trainset, testset, n_train = load_dataset(args) emb_dim = 2 n_epochs = 15 epoch_steps = len(trainloader) n_steps = epoch_steps * 15 cluster_refresh_interval = epoch_steps if args.mnist: model = torch.nn.DataParallel(LeNet(emb_dim)).cuda() if args.cifar10: model = torch.nn.DataParallel(VGG(depth=16, num_classes=emb_dim)) print(model) optimizer = optim.Adam(model.parameters(), lr=args.lr) minibatch_magnet_loss = MagnetLoss() images = getattr(trainset, 'train_data') labels = getattr(trainset, 'train_labels') # Get initial embedding initial_reps = compute_reps(model, trainset, 400) if args.cifar10: labels = np.array(labels, dtype=np.float32) # Create batcher batch_builder = ClusterBatchBuilder(labels, k, m, d) batch_builder.update_clusters(initial_reps) batch_losses = [] batch_example_inds, batch_class_inds = batch_builder.gen_batch() trainloader.sampler.batch_indices = batch_example_inds _ = model.train() losses = AverageMeter() for i in tqdm(range(n_steps)): for batch_idx, (img, target) in enumerate(trainloader): img = Variable(img).cuda() target = Variable(target).cuda() optimizer.zero_grad() output, features = model(img) batch_loss, batch_example_losses = minibatch_magnet_loss( output, batch_class_inds, m, d, alpha) batch_loss.backward() optimizer.step() # Update loss index batch_builder.update_losses(batch_example_inds, batch_example_losses) batch_losses.append(batch_loss.data[0]) if not i % 1000: print(i, batch_loss) if not i % cluster_refresh_interval: print("Refreshing clusters") reps = compute_reps(model, trainset, 400) batch_builder.update_clusters(reps) if not i % 2000: n_plot = 10000 plot_embedding(compute_reps(model, trainset, 400)[:n_plot], labels[:n_plot], name=i) batch_example_inds, batch_class_inds = batch_builder.gen_batch() trainloader.sampler.batch_indices = batch_example_inds losses.update(batch_loss, 1) # Log the training loss if args.visdom: plotter.plot('loss', 'train', i, losses.avg.data[0]) # Plot loss curve plot_smooth(batch_losses, "batch-losses")
callbacks = [ # Write TensorBoard logs to `./logs` directory tf.keras.callbacks.TensorBoard(log_dir=logdir, update_freq='batch', profile_batch=0), tf.keras.callbacks.ModelCheckpoint('checkpoints/cifar10/', monitor='loss', verbose=2, save_best_only=True, save_weights_only=True, mode='min') ] # model = SimpleNet(num_classes=10, num_channels=64) model = VGG(vgg_name='VGG16', num_classes=10) epochs = 1 # should be 1, 20 or 100 # SGDW Optimizer total_steps = math.ceil(len(x_train) / float(128)) * max(1, epochs) lr = CosineDecay(0.1, decay_steps=total_steps, alpha=1e-6) optimizer = SGDW(lr, momentum=0.9, nesterov=True, weight_decay=0.001) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['acc']) model.fit(train_dataset, epochs=epochs, validation_data=test_dataset,
from tools.common_tools import * from config.config import cfg device = torch.device("cuda" if torch.cuda.is_available() else "cpu") BASE_DIR = os.path.dirname(os.path.abspath(__file__)) if __name__ == '__main__': test_dir = os.path.join(BASE_DIR, "..", "data", "cifar10_test") path_checkpoint = os.path.join(BASE_DIR, "..", "..", "results/03-02_20-22/checkpoint_best.pkl") # resnet-image path_checkpoint = os.path.join(BASE_DIR, "..", "..", "results/03-06_16-54/checkpoint_best.pkl") # vgg-cifar valid_data = CifarDataset(data_dir=test_dir, transform=cfg.transforms_valid) valid_loader = DataLoader(dataset=valid_data, batch_size=cfg.valid_bs, num_workers=cfg.workers) log_dir = "../../results" model = resnet56() model = VGG("VGG16") check_p = torch.load(path_checkpoint, map_location="cpu", encoding='iso-8859-1') pretrain_dict = check_p["model_state_dict"] print("best acc: {} in epoch:{}".format(check_p["best_acc"], check_p["epoch"])) state_dict_cpu = state_dict_to_cpu(pretrain_dict) model.load_state_dict(state_dict_cpu) # resnet --> ghost-resnet model = replace_conv(model, GhostModule, arc="vgg16", pretrain=False) # model = replace_conv(model, GhostModule, pretrain=True) Isparallel = False if Isparallel and torch.cuda.is_available(): model = torch.nn.DataParallel(model)
import torch import torch.nn as nn from models.vgg import VGG from models.vgg import VGGStudent if __name__ == "__main__": teacher = VGG('VGG16') student = VGGStudent('VGG16') block_st = 0 block_end = -1 for idx, f in enumerate(student.features): if isinstance(f, nn.MaxPool2d): block_end = idx print(block_st, block_end) print("==================================") print(student.features[block_st:block_end]) print("==================================") block_st = idx + 1
testset = torchvision.datasets.CIFAR100(root=opt.root, train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2) else: raise ValueError(f'invlid dataset: {opt.data}') # load model if opt.model == 'vgg': if opt.defense in ('plain', 'adv'): from models.vgg import VGG net = nn.DataParallel(VGG('VGG16', nclass, img_width=img_width), device_ids=range(1)) net.load_state_dict( torch.load( f'./checkpoint/{opt.data}_{opt.model}_{opt.defense}.pth')) elif opt.defense in ('vi', 'adv_vi', 'vi_SEBR') or 'vi' in opt.defense: from models.vgg_vi import VGG net = nn.DataParallel(VGG(1.0, 1.0, 1.0, 'VGG16', nclass, img_width=img_width), device_ids=range(1)) elif opt.defense in ('rse'): from models.vgg_rse import VGG