batch_size=config.batch_size, shuffle=False, num_workers=8, pin_memory=True) setattr(dataloader['base'], 'total_item_len', len(base_set)) # ---------------------------- model ------------------------------------ if config.model_name == 'resnet18': model = ResNet18(config.code_length, classes, config.class_mask) elif config.model_name == 'alexnet': model = AlexNet(config.code_length, classes, config.class_mask) else: print('undefined model ! ') model = nn.DataParallel(model) model.cuda() # ---------------------------- loss and opt ------------------------------------ criterion = nn.CrossEntropyLoss() criterion_hash = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=config.lr, momentum=0.9) exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1) # ---------------------------- log and train ------------------------------------ log_file = open( config.model_name + '_' + config.dataset + '_' + str(config.code_length) + '.log', 'a') log_file.write(str(config)) log_file.write('\n') print('training start ...') train_model(model, dataloader, criterion, criterion_hash, optimizer,
# net = net.cuda() # for name, parameters in net.named_parameters(): # print(name, ':', parameters.size()) # exit() print("weight init..") weights_init(net) criterion = nn.CrossEntropyLoss() # weight_path = "/home/zengweijia/.jupyter/cnrpark/parking_lot_occupancy_detection/weights/carnet_60_0.001.pth" # net.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(weight_path, map_location="cpu").items()}) if args.path == '': net = train(args.epochs, args.train_img, args.train_lab, transforms=transforms, net=net, criterion=criterion) # net=train(args.epochs, args.train_img, args.train_lab, transforms, net, criterion) PATH = './weights/' + args.model + '_' + args.shuf_type + '_' + args.train_data + '.pth' torch.save(net.state_dict(), PATH) else: PATH = args.path if args.model == 'mAlexNet': net = mAlexNet() elif args.model == 'AlexNet': net = AlexNet() net.cuda() net.load_state_dict(torch.load(PATH)) # accuracy = test(args.test_img, args.test_lab, transforms, net) # print("\nThe accuracy of training on '{}' and testing on '{}' is {:.3f}.".format(args.train_lab.split('.')[0], args.test_lab.split('.')[0], accuracy))
if __name__ == "__main__": if args.model == 'mAlexNet': net = mAlexNet() elif args.model == 'AlexNet': net = AlexNet() elif args.model == "carnet": net = carNet() elif args.model == 'stn_shuf': net = stn_shufflenet() elif args.model == 'stn_trans_shuf': net = stn_trans_shufflenet() elif args.model == 'shuf': net = torchvision.models.shufflenet_v2_x1_0(pretrained=False, num_classes=2) elif args.model == 'trans_shuf': net = trans_shufflenet() torch.set_default_tensor_type('torch.FloatTensor') print("test net:carNet..") # print({k.replace('module.',''):v for k,v in torch.load(args.path,map_location="cpu").items()}) # exit() net.load_state_dict({ k.replace('module.', ''): v for k, v in torch.load(args.path, map_location="cpu").items() }) if torch.cuda.is_available(): net.cuda(int(args.cuda_device)) # exit() acc = test(args.test_img, args.test_lab, net) print(args.test_lab, acc)