def setup(self, flags): torch.backends.cudnn.deterministic = flags.deterministic print('torch.backends.cudnn.deterministic:', torch.backends.cudnn.deterministic) fix_all_seed(flags.seed) if flags.dataset == 'cifar10': num_classes = 10 else: num_classes = 100 if flags.model == 'densenet': self.network = densenet(num_classes=num_classes) elif flags.model == 'wrn': self.network = WideResNet(flags.layers, num_classes, flags.widen_factor, flags.droprate) elif flags.model == 'allconv': self.network = AllConvNet(num_classes) elif flags.model == 'resnext': self.network = resnext29(num_classes=num_classes) else: raise Exception('Unknown model.') self.network = self.network.cuda() print(self.network) print('flags:', flags) if not os.path.exists(flags.logs): os.makedirs(flags.logs) flags_log = os.path.join(flags.logs, 'flags_log.txt') write_log(flags, flags_log)
num_classes = 100 train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=args.prefetch, pin_memory=True) test_loader = torch.utils.data.DataLoader(test_data, batch_size=args.test_bs, shuffle=False, num_workers=args.prefetch, pin_memory=True) # Create model if args.model == 'allconv': net = AllConvNet(num_classes).cuda() # else: # net = WideResNet(args.layers, num_classes, args.widen_factor, dropRate=args.droprate) start_epoch = 0 cudnn.benchmark = True # fire on all cylinders optimizer = torch.optim.SGD(net.parameters(), state['learning_rate'], momentum=state['momentum'], weight_decay=state['decay'], nesterov=True) def cosine_annealing(step, total_steps, lr_max, lr_min):
num_classes = 1000 train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=args.prefetch, pin_memory=True) test_loader = torch.utils.data.DataLoader(test_data, batch_size=args.test_bs, shuffle=False, num_workers=args.prefetch, pin_memory=True) # Create model if args.model == 'allconv': net = AllConvNet(num_classes) else: net = WideResNet(args.layers, num_classes, args.widen_factor, dropRate=args.droprate) start_epoch = 0 # Restore model if desired if args.load != '': for i in range(1000 - 1, -1, -1): model_name = os.path.join( args.load, args.dataset + '_' + args.model + '_baseline_epoch_' + str(i) + '.pt') if os.path.isfile(model_name):
calib_indicator = '_calib' train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True, num_workers=args.prefetch, pin_memory=True) test_loader = torch.utils.data.DataLoader(test_data, batch_size=args.test_bs, shuffle=False, num_workers=args.prefetch, pin_memory=True) # Create model if args.model == 'allconv': net = AllConvNet(1000) else: net = WideResNet(args.layers, 1000, args.widen_factor, dropRate=args.droprate) start_epoch = 0 # Restore model if desired if args.load != '': for i in range(1000 - 1, -1, -1): model_name = os.path.join( args.load, 'imagenet_' + args.model + '_baseline_epoch_' + str(i) + '.pt') if os.path.isfile(model_name):
# train_indices = np.array(list(set(np.arange(50000)) - set(test_indices))) # test_data.test_data = np.copy(train_data.train_data[test_indices]) # train_data.train_data = np.copy(train_data.train_data[train_indices]) # # train_loader = torch.utils.data.DataLoader( train_data, batch_size=args.batch_size, shuffle=True, num_workers=args.prefetch, pin_memory=True) test_loader = torch.utils.data.DataLoader( test_data, batch_size=args.test_bs, shuffle=False, num_workers=args.prefetch, pin_memory=True) # Create model if args.model == 'allconv': net = AllConvNet(args.num_classes_pretrained_net) else: net = WideResNet(args.layers, args.num_classes_pretrained_net, args.widen_factor, dropRate=args.droprate) net = nn.DataParallel(net) model_found = False # Restore model if desired if args.load != '': for i in range(1000 - 1, -1, -1): model_name = os.path.join(args.load, 'imagenet_' + args.model + '_baseline_epoch_' + str(i) + '.pt') if os.path.isfile(model_name): net.load_state_dict(torch.load(model_name)) print('Model restored! Epoch:', i) start_epoch = i + 1