def main(): use_cuda = torch.cuda.is_available() and not args.no_cuda device = torch.device('cuda' if use_cuda else 'cpu') print(device) random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if use_cuda: torch.cuda.manual_seed(args.seed) torch.backends.cudnn.deterministic = True rgb = False if args.mode == 'rgb': rgb = True if args.gray_scale: rgb = False if args.tracking_data_mod is True: args.input_size = 192 # DATALOADER train_dataset = GesturesDataset(model=args.model, csv_path='csv_dataset', train=True, mode=args.mode, rgb=rgb, normalization_type=1, n_frames=args.n_frames, resize_dim=args.input_size, transform_train=args.train_transforms, tracking_data_mod=args.tracking_data_mod) train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.n_workers) validation_dataset = GesturesDataset(model=args.model, csv_path='csv_dataset', train=False, mode=args.mode, rgb=rgb, normalization_type=1, n_frames=args.n_frames, resize_dim=args.input_size, tracking_data_mod=args.tracking_data_mod) validation_loader = DataLoader(validation_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.n_workers) # paramteri per la rete in_channels = args.n_frames if not rgb else args.n_frames * 3 n_classes = args.n_classes if args.model == 'LeNet': model = LeNet(input_channels=in_channels, input_size=args.input_size, n_classes=n_classes).to(device) elif args.model == 'AlexNet': model = AlexNet(input_channels=in_channels, input_size=args.input_size, n_classes=n_classes).to(device) elif args.model == 'AlexNetBN': model = AlexNetBN(input_channels=in_channels, input_size=args.input_size, n_classes=n_classes).to(device) elif args.model == "Vgg16": model = Vgg16(input_channels=in_channels, input_size=args.input_size, n_classes=n_classes).to(device) elif args.model == "Vgg16P": model = models.vgg16(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model.features._modules['0'] = nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=(3, 3), stride=1, padding=1) model.classifier._modules['6'] = nn.Linear(4096, n_classes) # model.fc = torch.nn.Linear(model.fc.in_features, n_classes) model = model.to(device) elif args.model == "ResNet18P": model = models.resnet18(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model._modules['conv1'] = nn.Conv2d(in_channels, 64, 7, stride=2, padding=3) model.fc = torch.nn.Linear(model.fc.in_features, n_classes) model = model.to(device) elif args.model == "ResNet34P": model = models.resnet34(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model._modules['conv1'] = nn.Conv2d(in_channels, 64, 7, stride=2, padding=3) model.fc = torch.nn.Linear(model.fc.in_features, n_classes) model = model.to(device) elif args.model == "DenseNet121P": model = models.densenet121(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model.features._modules['conv0'] = nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3)) model.classifier = nn.Linear(in_features=1024, out_features=n_classes, bias=True) model = model.to(device) elif args.model == "DenseNet161P": model = models.densenet161(pretrained=args.pretrained) # for params in model.parameters(): # params.requires_grad = False model.features._modules['conv0'] = nn.Conv2d(in_channels=in_channels, out_channels=96, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3)) model.classifier = nn.Linear(in_features=2208, out_features=n_classes, bias=True) model = model.to(device) elif args.model == "DenseNet169P": model = models.densenet169(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model.features._modules['conv0'] = nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3)) model.classifier = nn.Linear(in_features=1664, out_features=n_classes, bias=True) model = model.to(device) elif args.model == "DenseNet201P": model = models.densenet201(pretrained=args.pretrained) for params in model.parameters(): params.requires_grad = False model.features._modules['conv0'] = nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3)) model.classifier = nn.Linear(in_features=1920, out_features=n_classes, bias=True) model = model.to(device) # RNN elif args.model == 'LSTM' or args.model == 'GRU': model = Rnn(rnn_type=args.model, input_size=args.input_size, hidden_size=args.hidden_size, batch_size=args.batch_size, num_classes=args.n_classes, num_layers=args.n_layers, final_layer=args.final_layer).to(device) # C3D elif args.model == 'C3D': if args.pretrained: model = C3D(rgb=rgb, num_classes=args.n_classes) # modifico parametri print('ok') model.load_state_dict(torch.load('c3d_weights/c3d.pickle', map_location=device), strict=False) # # for params in model.parameters(): # # params.requires_grad = False model.conv1 = nn.Conv3d(1 if not rgb else 3, 64, kernel_size=(3, 3, 3), padding=(1, 1, 1)) # tolgo fc6 perchè 30 frames model.fc6 = nn.Linear(16384, 4096) # num classes 28672 (112*200) model.fc7 = nn.Linear(4096, 4096) # num classes model.fc8 = nn.Linear(4096, n_classes) # num classes model = model.to(device) # Conv-lstm elif args.model == 'Conv-lstm': model = ConvLSTM(input_size=(args.input_size, args.input_size), input_dim=1 if not rgb else 3, hidden_dim=[64, 64, 128], kernel_size=(3, 3), num_layers=args.n_layers, batch_first=True, ).to(device) elif args.model == 'DeepConvLstm': model = DeepConvLstm(input_channels_conv=1 if not rgb else 3, input_size_conv=args.input_size, n_classes=12, n_frames=args.n_frames, batch_size=args.batch_size).to(device) elif args.model == 'ConvGRU': model = ConvGRU(input_size=40, hidden_sizes=[64, 128], kernel_sizes=[3, 3], n_layers=2).to(device) else: raise NotImplementedError if args.opt == 'SGD': optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) # optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, momentum=args.momentum) elif args.opt == 'Adam': optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) loss_function = nn.CrossEntropyLoss().to(device) start_epoch = 0 if args.resume: checkpoint = torch.load("/projects/fabio/weights/gesture_recog_weights/checkpoint{}.pth.tar".format(args.model)) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) start_epoch = checkpoint['epoch'] print("Resuming state:\n-epoch: {}\n{}".format(start_epoch, model)) #name experiment personal_name = "{}_{}_{}".format(args.model, args.mode, args.exp_name) info_experiment = "{}".format(personal_name) log_dir = "/projects/fabio/logs/gesture_recog_logs/exps" weight_dir = personal_name log_file = open("{}/{}.txt".format("/projects/fabio/logs/gesture_recog_logs/txt_logs", personal_name), 'w') log_file.write(personal_name + "\n\n") if personal_name: exp_name = (("exp_{}_{}".format(time.strftime("%c"), personal_name)).replace(" ", "_")).replace(":", "-") else: exp_name = (("exp_{}".format(time.strftime("%c"), personal_name)).replace(" ", "_")).replace(":", "-") writer = SummaryWriter("{}".format(os.path.join(log_dir, exp_name))) # add info experiment writer.add_text('Info experiment', "model:{}" "\n\npretrained:{}" "\n\nbatch_size:{}" "\n\nepochs:{}" "\n\noptimizer:{}" "\n\nlr:{}" "\n\ndn_lr:{}" "\n\nmomentum:{}" "\n\nweight_decay:{}" "\n\nn_frames:{}" "\n\ninput_size:{}" "\n\nhidden_size:{}" "\n\ntracking_data_mode:{}" "\n\nn_classes:{}" "\n\nmode:{}" "\n\nn_workers:{}" "\n\nseed:{}" "\n\ninfo:{}" "".format(args.model, args.pretrained, args.batch_size, args.epochs, args.opt, args.lr, args.dn_lr, args.momentum, args.weight_decay, args.n_frames, args.input_size, args.hidden_size, args.tracking_data_mod, args.n_classes, args.mode, args.n_workers, args.seed, info_experiment)) trainer = Trainer(model=model, loss_function=loss_function, optimizer=optimizer, train_loader=train_loader, validation_loader=validation_loader, batch_size=args.batch_size, initial_lr=args.lr, device=device, writer=writer, personal_name=personal_name, log_file=log_file, weight_dir=weight_dir, dynamic_lr=args.dn_lr) print("experiment: {}".format(personal_name)) start = time.time() for ep in range(start_epoch, args.epochs): trainer.train(ep) trainer.val(ep) # display classes results classes = ['g0', 'g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8', 'g9', 'g10', 'g11'] for i in range(args.n_classes): print('Accuracy of {} : {:.3f}%%'.format( classes[i], 100 * trainer.class_correct[i] / trainer.class_total[i])) end = time.time() h, rem = divmod(end - start, 3600) m, s, = divmod(rem, 60) print("\nelapsed time (ep.{}):{:0>2}:{:0>2}:{:05.2f}".format(args.epochs, int(h), int(m), s)) # writing accuracy on file log_file.write("\n\n") for i in range(args.n_classes): log_file.write('Accuracy of {} : {:.3f}%\n'.format( classes[i], 100 * trainer.class_correct[i] / trainer.class_total[i])) log_file.close()
test_loader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=4, pin_memory=True) net = LeNet() criterion = nn.CrossEntropyLoss() if use_cuda: print('start move to cuda') torch.cuda.manual_seed_all(seed) cudnn.benchmark = True device = torch.device("cuda:0") net.to(device=device) criterion.to(device=device, dtype=dtype) optimizer = optim.Adam( net.parameters(), lr=0.001, ) def train(epoch): # global monitor print('\nEpoch: %d' % epoch) net.train() train_loss = 0 correct = 0 a = time.time()
def main(): opt = Parser(train=False).get() # dataset and data loader _, val_loader, adv_val_loader, _, num_classes = \ load_dataset(opt.dataset, opt.batch_size, opt.data_root, False, 0.0, opt.num_val_samples, workers=4) # model if opt.arch == 'lenet': model = LeNet(num_classes) elif opt.arch == 'resnet': model = ResNetv2_20(num_classes) else: raise NotImplementedError # move model to device model.to(opt.device) # load trained weight try: model.load_state_dict(torch.load(opt.weight_path)) except: model_weight = convert_model_from_parallel(opt.weight_path) model.load_state_dict(model_weight) # criterion criterion = nn.CrossEntropyLoss() # advertorch attacker if opt.attack == 'pgd': attacker = PGDAttack(model, loss_fn=criterion, eps=opt.eps / 255, nb_iter=opt.num_steps, eps_iter=opt.eps_iter / 255, rand_init=True, clip_min=opt.clip_min, clip_max=opt.clip_max, ord=np.inf, targeted=False) else: raise NotImplementedError # trainer trainer = Trainer(opt, model, criterion, attacker) trainer.print_freq = -1 # validation val_losses, val_acc1s, val_acc5s = \ trainer.validate(val_loader) aval_losses, aval_acc1s, aval_acc5s = \ trainer.adv_validate(adv_val_loader) print('[model] {}'.format(opt.weight_path)) print('[standard]\n' 'loss: {:.4f} | acc1: {:.2f}% | acc5: {:.2f}%' '\n[adversarial]\n' 'loss: {:.4f} | acc1: {:.2f}% | acc5: {:.2f}%'.format( val_losses['val'].avg, val_acc1s['val'].avg, val_acc5s['val'].avg, aval_losses['aval'].avg, aval_acc1s['aval'].avg, aval_acc5s['aval'].avg))
def main(): opt = Parser().get() experiment = None if opt.comet: experiment = Experiment() experiment.set_name(opt.exp_name) experiment.log_parameters(opt.__dict__) experiment.add_tags(opt.add_tags) # dataset and data loader train_loader, val_loader, adv_val_loader, _, num_classes = \ load_dataset(opt.dataset, opt.batch_size, opt.data_root, opt.noise, opt.noise_std, opt.num_val_samples, workers=4) # model if opt.arch == 'lenet': model = LeNet(num_classes) elif opt.arch == 'resnet': model = ResNetv2_20(num_classes) else: raise NotImplementedError # weight init if opt.weight_init == 'he': model.apply(init_he) # move model to device model.to(opt.device) if opt.gpu_ids: model = nn.DataParallel(model, device_ids=opt.gpu_ids) # criterion criterion = nn.CrossEntropyLoss() # advertorch attacker if opt.attack == 'pgd': attacker = LinfPGDAttack(model, loss_fn=criterion, eps=opt.eps / 255, nb_iter=opt.num_steps, eps_iter=opt.eps_iter / 255, rand_init=True, clip_min=opt.clip_min, clip_max=opt.clip_max, targeted=False) else: raise NotImplementedError # optimizer if opt.optim == 'Adam': optimizer = optim.Adam(model.parameters(), opt.lr, eps=1e-6, weight_decay=opt.wd) elif opt.optim == 'SGD': optimizer = optim.SGD(model.parameters(), opt.lr, weight_decay=opt.wd) else: raise NotImplementedError # scheduler if opt.scheduler_step: scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=opt.scheduler_step, gamma=opt.scheduler_gamma) else: scheduler = None # timer timer = Timer(opt.num_epochs, 0) # trainer trainer = Trainer(opt, model, criterion, attacker, optimizer) # epoch iteration for epoch in range(1, opt.num_epochs + 1): trainer.epoch = epoch if scheduler: scheduler.step(epoch - 1) # scheduler's epoch is 0-indexed. # training train_losses, train_acc1s, train_acc5s = \ trainer.train(train_loader) # validation val_losses, val_acc1s, val_acc5s = \ trainer.validate(val_loader) if opt.adv_val_freq != -1 and epoch % opt.adv_val_freq == 0: aval_losses, aval_acc1s, aval_acc5s = \ trainer.adv_validate(adv_val_loader) else: aval_losses, aval_acc1s, aval_acc5s = \ dict(), dict(), dict() losses = dict(**train_losses, **val_losses, **aval_losses) acc1s = dict(**train_acc1s, **val_acc1s, **aval_acc1s) acc5s = dict(**train_acc5s, **val_acc5s, **aval_acc5s) report_epoch_status(losses, acc1s, acc5s, trainer.num_loss, epoch, opt, timer, experiment) save_path = os.path.join('ckpt', opt.dataset, 'models', opt.exp_name + '.pth') trainer.save_model(save_path)
loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=param['batch_size'], shuffle=True) test_dataset = datasets.MNIST(root='../data/', train=False, download=True, transform=transforms.ToTensor()) loader_test = torch.utils.data.DataLoader(test_dataset, batch_size=param['test_batch_size'], shuffle=True) model = LeNet() model.load_state_dict( torch.load('models/lenet_pretrained.pkl', map_location='cpu')) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) print("--- Accuracy of Pretrained Model ---") test(model, loader_test) # pruning masks = lenet_prune() model.set_masks(masks) print("--- Accuracy After Pruning ---") test(model, loader_test) # Retraining criterion = nn.CrossEntropyLoss() optimizer = torch.optim.RMSprop(model.parameters(), lr=param['learning_rate'], weight_decay=param['weight_decay']) train(model, criterion, optimizer, param, loader_train)
batch_size=128) v = np.random.randn(1, 32, 32) v = v / np.linalg.norm(v) trainloader, testloader, trainset, testset = generate_synthetic_data( v, num_train=10000, num_test=10000, sigma=3, epsilon=1, shape=[1, 32, 32], batch_size=128) # net = LogReg(input_dim=32 * 32, num_classes=2) # net = VGG11_bn(num_channels=1, num_classes=2) # net = ResNet18(num_channels=1, num_classes=2) # net = DenseNet121(num_channels=1, num_classes=2) net = LeNet(num_channels=1, num_classes=2) net = net.to(DEVICE) trained_model = train(model=net, trans=TransformLayer( mean=torch.tensor(0., device=DEVICE), std=torch.tensor(1., device=DEVICE)), trainloader=trainloader, testloader=testloader, epochs=20, max_lr=0.5, momentum=0, weight_decay=0)