model = None if args['load'] : model = torch.load(StoragePath + args['load']) else : model = ImageNet().half().cuda() PruneList = ['Conv2d', 'Linear'] PrunePair = () for m in model.modules(): if len(list(m.children())) == 0 and m.__class__.__name__ in PruneList: # skip for non-leaf module PrunePair += ((m, 'weight'), ) prune.RandomStructured.apply(module = m, name = 'weight', amount = 0.5) # prune.global_unstructured(PrunePair, pruning_method = prune.RandomStructured, amount = 0.6) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr = args['learning_rate'], momentum = 0.9) print('Training :') lastaccuracy = 0 # model.eval() # with torch.no_grad() : # lastaccuracy = forward('Validation', validationloader, model) if args['save'] : torch.save(model, StoragePath + args['save']) for epoch in range(args['epoch']): # loop over the dataset multiple times print('\n\tEpoch : ' + str(epoch))
loss_print = 0 MODEL_UPDATE_ITER = 0 # get loader train_loader = get_train_loader(opt=opt) # define net imageNet = ImageNet() imageNet.cuda() # text net tokenizer = BertTokenizer.from_pretrained( '/home/poac/code/Multi_modal_Retrieval/experiments/pretrained_models/bert-base-uncased-vocab.txt' ) textNet = TextNet(code_length=opt.hashbits) textNet.cuda() # embedding net embNet = EmbNet(opt) embNet.cuda() optimizer = optim.Adam( list(imageNet.parameters()) + list(textNet.parameters()), lr=opt.lr, weight_decay=opt.weight_decay) #+list(textExtractor.parameters()) # train and test on 10 epoch for epoch in tqdm(range(start_epoch, start_epoch + opt.max_epochs + 1)): train(opt, epoch) if epoch % 1 == 0: test(opt, epoch)
shuffle=False, num_workers=args['thread']) testloader = torch.utils.data.DataLoader(testset, batch_size=args['batch_size'], shuffle=False, num_workers=args['thread']) model = None if args['load']: model = torch.load(args['load']) else: model = ImageNet().cuda() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=args['learning_rate'], momentum=0.9) print('Training :') avgloss = 0.0 avgcorrect = [0.0] * 11 cases = [0.0] * 11 lastaccuracy = 0 model.eval() with torch.no_grad(): for i, (inputs, labels) in enumerate(validationloader): inputs = inputs.cuda() labels = labels.cuda()