def main(): global args, best_prec1 args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") # datasets num_class, train_loader, test_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) model_test = CapsNet_report(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters).to(device) model_test.load_state_dict( torch.load(args.snapshot_folder + "/model_10.pth")) model_test.eval() test_acc = test(test_loader, model_test, criterion, device)
def load_model(model_name, device_ids, lr, routing_iters): num_class = 5 if model_name == 'matrix-capsules': A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 model = MatrixCapsules(A=A, B=B, C=C, D=D, E=num_class, iters=routing_iters, _lambda=[[1e-4, 1e-2], [1e-4, 1e-2], [1e-4, 1e-2]]) elif model_name == 'cnn': model = CNN(num_class) elif model_name == 'vector-capsules': model = VectorCapsules(routing_iters, num_classes=num_class) model = torch.nn.DataParallel(model, device_ids=device_ids) print(f'Network has {count_parameters(model):d} parameters') criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) optimizer = torch.optim.Adam(model.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', patience=1) return model, criterion, optimizer, scheduler
def main(): global args, best_prec1 args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") # datasets num_class, train_loader, val_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 model = capsules(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters).to(device) print(model) if args.load_weights: model.load_state_dict( torch.load(os.path.join(args.snapshot_folder, args.load_weights))) print_number_parameters(model) criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9) # scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', patience=3) best_acc = 0.0 for epoch in range(1, args.epochs + 1): acc, loss = train(train_loader, model, criterion, optimizer, epoch, device) # scheduler.step(acc) if epoch % args.test_intvl == 0: val_acc, val_loss = test(val_loader, model, criterion, device) print("Train - Average loss: {:.6f} Average acc: {:.6f}".format( loss, acc)) if val_acc > best_acc: best_acc = val_acc snapshot(model, args.snapshot_folder, epoch) print("Current Best: {:.6f}".format(best_acc)) print('[EPOCH {}] TRAIN_LOSS: {:.6f} TRAIN_ACC: {:.6f}'.format( epoch, loss, acc)) print('[EPOCH {}] VAL_LOSS: {:.6f} VAL_ACC: {:.6f}'.format( epoch, val_loss, val_acc)) print('Best val accuracy: {:.6f}'.format(best_acc))
def main(): global args, best_prec1 args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") # datasets num_class, train_loader, test_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 model = capsules(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters).to(device) model2 = RECONSTRUCT(num_class=num_class).to(device) #model.load_state_dict(torch.load('snapshots/model_1.pth')) #model2.load_state_dict(torch.load('snapshots/model2_1.pth')) criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) #print(model.parameters()) #print(model2.parameters()) params = list(model.parameters()) + list(model2.parameters()) optimizer = optim.Adam(params, lr=args.lr, weight_decay=args.weight_decay) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', patience=1) best_acc = test(test_loader, model, criterion, device) for epoch in range(1, args.epochs + 1): acc = train(train_loader, model, model2, criterion, optimizer, epoch, device) acc /= len(train_loader) scheduler.step(acc) if epoch % args.test_intvl == 0: best_acc = max(best_acc, test(test_loader, model, criterion, device)) snapshot(model, model2, args.snapshot_folder, epoch) best_acc = max(best_acc, test(test_loader, model, criterion, device)) print('best test accuracy: {:.6f}'.format(best_acc)) snapshot(model, model2, args.snapshot_folder, args.epochs)
def main(): global args, best_prec1 args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") # datasets num_class, train_loader, test_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 modelRAVEN = CapsNetRAVEN(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters, cycle=args.cycle, intwidth=args.intwidth_max, fracwidth=args.fracwidth_max, bitwidth_reduce=args.bitwidth_reduce, rounding="round").to(device) criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) optimizer = optim.Adam(modelRAVEN.parameters(), lr=args.lr, weight_decay=args.weight_decay) best_acc = test(test_loader, modelRAVEN, criterion, device) for epoch in range(1, args.epochs + 1): acc = train(train_loader, modelRAVEN, criterion, optimizer, epoch, device) acc /= len(train_loader) if epoch % args.test_intvl == 0: best_acc = max(best_acc, test(test_loader, modelRAVEN, criterion, device)) best_acc = max(best_acc, test(test_loader, modelRAVEN, criterion, device)) print('best test accuracy: {:.6f}'.format(best_acc)) snapshot(modelRAVEN, args.snapshot_folder, args.epochs, args)
def main(): global args, best_prec1 args = parser.parse_args() print("Current setting: cycle-" + str(args.cycle) + \ "\tintwidth-max-" + str(args.intwidth_max) + \ "\tfracwidth-max-" + str(args.fracwidth_max) + \ "\tbitwidth-reduce-" + str(args.bitwidth_reduce) + \ "\trounding-" + args.rounding + "\n") args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") # datasets num_class, train_loader, test_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) print("Floating-point model:") model = capsules(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters).to(device) model.load_state_dict(torch.load(args.snapshot_folder+"/model_10.pth")) model_test_acc = test(test_loader, model, criterion, device) print("RAVEN model:") modelRAVEN = CapsNetRAVEN(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters, cycle=args.cycle, intwidth=args.intwidth_max, fracwidth=args.fracwidth_max, bitwidth_reduce=args.bitwidth_reduce, rounding="round").to(device) modelRAVEN.eval() modelRAVEN_state_dict = modelRAVEN.state_dict() modelRAVEN_state_dict.update(torch.load(args.snapshot_folder+"/model_10.pth")) modelRAVEN.load_state_dict(modelRAVEN_state_dict) modelRAVEN_test_acc = test(test_loader, modelRAVEN, criterion, device)
def train(cfg): print(cfg.pretty()) ds = ModelNetDataset(cfg) dataloader = torch.utils.data.DataLoader(ds, batch_size=cfg.batch_size, num_workers=0, pin_memory=True, shuffle=True, drop_last=True) model = Model(cfg).cuda() model.train() crit = SpreadLoss(cfg.min_margin) opt = torch.optim.Adam(model.parameters(), lr=1e-3) for e in range(cfg.max_epoch): td = tqdm(dataloader) for i, batch in enumerate(td): pts, lrfs, labels = batch pts = pts.cuda() lrfs = lrfs.cuda() labels = labels.cuda() opt.zero_grad() # with torch.autograd.detect_anomaly(): output_cap, output_a = model(pts, lrfs) loss = crit(output_a, labels) loss.backward() # torch.nn.utils.clip_grad_norm_(model.parameters(), 0.01) for p in model.parameters(): assert not torch.any(torch.isnan(p.grad)) opt.step() td.set_description('iter {}/{}'.format(i, len(td))) td.set_postfix({'loss': loss.item()})
def main(): global args, best_prec1 args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) # datasets num_class, train_loader, test_loader = get_setting(args) # model A, B, C, D = 64, 8, 16, 16 # A, B, C, D = 32, 32, 32, 32 model = capsules(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters) print("Architecture construction finished...") if args.cuda: model.cuda() criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', patience=1) best_acc = test(test_loader, model, criterion) for epoch in range(1, args.epochs + 1): print("Starting epoch: [%d/%d]" % (epoch, args.epochs)) acc = train(train_loader, model, criterion, optimizer, epoch) acc /= len(train_loader) scheduler.step(acc) if epoch % args.test_intvl == 0: best_acc = max(best_acc, test(test_loader, model, criterion)) best_acc = max(best_acc, test(test_loader, model, criterion)) print('best test accuracy: {:.6f}'.format(best_acc)) snapshot(model, args.snapshot_folder, args.epochs)
T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ]) imgs, labels, name_list = get_processed_datas(list_root) imgs, labels = upsample(imgs, labels) dataset = Dataset2(imgs, labels, transforms=transform) dataloader = DataLoader(dataset, batch_size=batch_sizes, shuffle=True, num_workers=0, drop_last=True) train_len = len(dataloader) print('train_len:', train_len) # loss # weights = torch.FloatTensor([0.75,0.25]).cuda() net_loss1 = torch.nn.CrossEntropyLoss(weight=None, size_average=True) net_loss = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) lr = 0.01 optimizer1 = optim.SGD([{ 'params': model1.parameters(), 'lr': lr }, { 'params': model2.parameters(), 'lr': lr }], lr=lr, momentum=0.9, weight_decay=0.0005) # optimizer1 = optim.Adam([ # {'params': model1.parameters(), 'lr': lr}, # {'params': model2.parameters(), 'lr': lr}
def main(): A, B, C, D = 256, 22, 22, 22 global args args = SETTINGS() args.cuda = args.cuda and torch.cuda.is_available() # torch.manual_seed(args.seed) # if args.cuda: # torch.cuda.manual_seed(args.seed) device = torch.device("cuda" if args.cuda else "cpu") num_class, train_loader, test_loader = load_database(args) model = capsules(A=A, B=B, C=C, D=D, E=num_class, iters=args.em_iters).to(device) criterion = SpreadLoss(num_class=num_class, m_min=0.2, m_max=0.9) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) #optimizer = optim.RMSprop(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) #optimizer = optim.Adadelta(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) #optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=args.weight_decay, nesterov=True) #scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', patience=1, verbose=True) scheduler = optim.lr_scheduler.StepLR(optimizer, 1, gamma= 0.81, last_epoch=-1) epoch = 1 _accs = [] _accs_test = [] _loss = [] _loss_test = [] model, epoch, optimizer, criterion, scheduler, _accs, _accs_test, _loss, _loss_test = load(model, epoch, optimizer, criterion, scheduler, _accs, _accs_test, _loss, _loss_test, args.save_folder, A, B, C, D) #optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=args.weight_decay) #plot_graph(_accs, _accs_test, "Accuracy") #plot_graph(_loss, _loss_test, "Loss") #quit() #best_acc = test(test_loader, model, criterion, device) #quit() print(str(model)) print(str(optimizer)) try: for epoch in range(epoch, args.epochs + 1): _s = time.time() acc, loss = train(train_loader, model, criterion, optimizer, epoch, device) acc /= len(train_loader) _accs.append(acc) loss /= len(train_loader) _loss.append(loss) print("Average acc: " + str(acc)) print("Average loss: " + str(loss)) print("Epoch time: " + str(round(time.time() - _s, 3))) test_acc, test_loss = test(test_loader, model, criterion, device) _accs_test.append(test_acc) _loss_test.append(test_loss) save(model, epoch+1, optimizer, criterion, scheduler, _accs, _accs_test, _loss, _loss_test, args.save_folder, A, B, C, D) #scheduler.step(acc) #print(optimizer) plot_graph(_accs, _accs_test, "Accuracy") plot_graph(_loss, _loss_test, "Loss") except KeyboardInterrupt: if len(_accs_test) < len(_accs): test_acc, test_loss = test(test_loader, model, criterion, device) _accs_test.append(test_acc) _loss_test.append(test_loss) # epoch += 1 #save(model, epoch, optimizer, criterion, scheduler, _accs, _accs_test, _loss, _loss_test, args.save_folder, A, B, C, D) plot_graph(_accs, _accs_test, "Accuracy") plot_graph(_loss, _loss_test, "Loss") quit()