def __init__(self, path): super().__init__('ann') self.pot_rewards = { 11: 300, # Insure the agent values the goal suit. Handle seeing 11 cards elsewhere 10: 10 * 10 + 100, # [cards] * 10 + [10 pot] 9: 9 * 10 + 100, # [cards] * 10 + [10 pot] 8: 8 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] 7: 7 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] 6: 6 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] # [other hands 10] = C(n+r-1,r-1)) where n = 5 and r = 3 so [other hands 10] = 21 # [cards] * 10 + [prob 8 goal] * [8 pot] + [prob 10 goal] * (3/[other hands 10] * [10 pot]/2 + ([other hands 10] - 3)/[other hands 10] * [10 pot]]) 5: 5 * 10 + .33 * 120 + .67 * (3/21 * 100/2 + 18/21 * 100), # [other hands 10] = C(n+r-1,r-1)) where n = 6 and r = 3 so [other hands 10] = 28 # [other hands 8] = C(n+r-1,r-1)) where n = 4 and r = 3 so [other hands 8] = 15 # [cards] * 10 + [prob 8 goal] * (3/[other hands 8] * [8 pot] / 2 + ([other hands 8] - 3)/[other hands 8]) + [prob 10 goal] * (3/[other hands] * [10 pot]/2 * ([other hands 10] - 6)/[other hands 10] * [10 pot]) 4: 4 * 10 + .33 * (3/15 * 120/2 + 12/15 * 120) + .67 * (3/28 * 100/2 + 22/28 * 100), # [other hands 10] = C(n+r-1,r-1)) where n = 7 and r = 3 so [other hands 10] = 36 # [other hands 8] = C(n+r-1,r-1)) where n = 5 and r = 3 so [other hands 8] = 21 # [cards] * 10 + [prob 8 goal] * (3/[other hands 8] * [8 pot] / 3) + [prob 10 goal] * (3/[other hands 10] * [10 pot] / 2) 3: 3 * 10 + .33 * (3/21 * 120/3) + .67 * (3/36 * 100 / 2), # [other hands 8] = C(n+r-1,r-1)) where n = 6 and r = 3 so [other hands 8] = 28 # [cards] * 10 + [prob 8 goal] * (1/[other hands 8] * [8 pot] / 4) 2: 2 * 10 + .33 * (1/28 * 120/4), 1: 10, # 1 * 10 0: 0, # 0 * 10 } self.model = Net(16, 32, 64) # TODO don't hard code these self.model.load_state_dict(torch.load(path)) self.model.eval()
images, labels = data # if labels.data == 4: # continue # uncomment to test the adversarial patch images = images.to(device) labels = labels.to(device) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the %s test images: %10.5f %%' % (total,100 * correct / total)) if __name__ == "__main__": parser = argparse.ArgumentParser(description='test') parser.add_argument("model", type=str, help="test_model") args = parser.parse_args() model = Net() model.load_state_dict(torch.load('../donemodel/'+args.model)) print("test model is ", args.model) dataloaders,dataset_sizes =data_process_lisa(batch_size =1) test(model,dataloaders,dataset_sizes)
time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) print('Best val Acc: {:4f}'.format(best_acc)) # load best model weights model.load_state_dict(best_model_wts) return model if __name__ == "__main__": torch.manual_seed(123456) dataloaders, dataset_sizes = data_process_lisa(batch_size=128) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model_ft = Net() model_ft.apply(weights_init) #model_ft.load_state_dict(torch.load('../donemodel/'+args.model)) model_ft.to(device) # model_ft = nn.DataParallel(model,device_ids=[0,1]) # use multiple gpus criterion = nn.CrossEntropyLoss() optimizer_ft = optim.Adam(model_ft.parameters(), lr=0.01) # Decay LR by a factor of 0.1 every 7 epochs exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=10, gamma=0.1)
patch_type = opt.patch_type #patch_size = opt.patch_size image_size = opt.image_size plot_all = opt.plot_all print("=> creating model ") netClassifier_par = { 'input_size': [3, 32, 32], 'input_range': [0, 1], 'mean': [0, 0, 0], 'std': [1, 1, 1], 'num_classes': 16, 'input_space': "RGB" } netClassifier = Net() netClassifier.load_state_dict(torch.load('../donemodel/' + opt.model)) if torch.cuda.is_available(): netClassifier.cuda() print('==> Preparing data..') data_dir = '../LISA' train_loader = torch.utils.data.DataLoader(dset.ImageFolder( os.path.join(data_dir, 'Train'), transforms.Compose([ transforms.Resize( round(max(netClassifier_par["input_size"]) * 1.050)), transforms.CenterCrop(max(netClassifier_par["input_size"])), transforms.ToTensor()
delta.data *= epsilon / norms(delta.detach()).clamp(min=epsilon) delta.grad.zero_() all_loss = nn.CrossEntropyLoss(reduction='none')(model(X + delta), y) max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss] max_loss = torch.max(max_loss, all_loss) return max_delta if __name__ == "__main__": parser = argparse.ArgumentParser(description='test') parser.add_argument("model", type=str, help="test_model") args = parser.parse_args() model = Net() model.load_state_dict(torch.load('../donemodel/' + args.model)) print("test model is ", args.model) model.eval() batch_size = 1 dataloaders, dataset_sizes = data_process_lisa(batch_size) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(device) eps = [0.5, 1, 1.5, 2, 2.5, 3] # eps is epsilon of the l_2 bound alpha = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3] # alpha is learning rate itera = [20, 20, 20, 20, 20, 20] # iterations to find optimal restart = [ 1, 1, 1, 1, 1, 1 ] # restart times, since we just do some standard check of our model,
for x in ['train', 'val', 'test'] } class_names = image_datasets['train'].classes print(class_names) print(dataset_sizes) return dataloaders, dataset_sizes if __name__ == "__main__": parser = argparse.ArgumentParser(description='test') parser.add_argument("model", type=str, help="test_model") args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = Net() model.load_state_dict(torch.load('../donemodel/' + args.model)) print("test model is ", args.model) model.eval() batch_size = 8 dataloaders, dataset_sizes = data_process(batch_size) # model.to(device) preprocessing = dict(mean=[0, 0, 0], std=[1, 1, 1], axis=-3) fmodel = foolbox.models.PyTorchModel(model.eval(), bounds=(0, 1), num_classes=16, preprocessing=preprocessing) correct = 0
class AnnModel(UtilityModel): def __init__(self, path): super().__init__('ann') self.pot_rewards = { 11: 300, # Insure the agent values the goal suit. Handle seeing 11 cards elsewhere 10: 10 * 10 + 100, # [cards] * 10 + [10 pot] 9: 9 * 10 + 100, # [cards] * 10 + [10 pot] 8: 8 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] 7: 7 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] 6: 6 * 10 + .33 * 120 + .67 * 100, # [cards] * 10 + [prob. 8 goal] * [8 pot] + [prob. 10 goal] * [10 pot] # [other hands 10] = C(n+r-1,r-1)) where n = 5 and r = 3 so [other hands 10] = 21 # [cards] * 10 + [prob 8 goal] * [8 pot] + [prob 10 goal] * (3/[other hands 10] * [10 pot]/2 + ([other hands 10] - 3)/[other hands 10] * [10 pot]]) 5: 5 * 10 + .33 * 120 + .67 * (3/21 * 100/2 + 18/21 * 100), # [other hands 10] = C(n+r-1,r-1)) where n = 6 and r = 3 so [other hands 10] = 28 # [other hands 8] = C(n+r-1,r-1)) where n = 4 and r = 3 so [other hands 8] = 15 # [cards] * 10 + [prob 8 goal] * (3/[other hands 8] * [8 pot] / 2 + ([other hands 8] - 3)/[other hands 8]) + [prob 10 goal] * (3/[other hands] * [10 pot]/2 * ([other hands 10] - 6)/[other hands 10] * [10 pot]) 4: 4 * 10 + .33 * (3/15 * 120/2 + 12/15 * 120) + .67 * (3/28 * 100/2 + 22/28 * 100), # [other hands 10] = C(n+r-1,r-1)) where n = 7 and r = 3 so [other hands 10] = 36 # [other hands 8] = C(n+r-1,r-1)) where n = 5 and r = 3 so [other hands 8] = 21 # [cards] * 10 + [prob 8 goal] * (3/[other hands 8] * [8 pot] / 3) + [prob 10 goal] * (3/[other hands 10] * [10 pot] / 2) 3: 3 * 10 + .33 * (3/21 * 120/3) + .67 * (3/36 * 100 / 2), # [other hands 8] = C(n+r-1,r-1)) where n = 6 and r = 3 so [other hands 8] = 28 # [cards] * 10 + [prob 8 goal] * (1/[other hands 8] * [8 pot] / 4) 2: 2 * 10 + .33 * (1/28 * 120/4), 1: 10, # 1 * 10 0: 0, # 0 * 10 } self.model = Net(16, 32, 64) # TODO don't hard code these self.model.load_state_dict(torch.load(path)) self.model.eval() def get_card_values(self, figgie: Figgie, index: int) -> np.ndarray: hand = figgie.cards[index] result = np.zeros(4, dtype=int) for s in SUITS: if hand[s.value] > 10: goal_suit = s.opposite() result[goal_suit.value] = 100 return result for s in SUITS: result[s.value] = (self.pot_rewards[hand[s.value] + 1] - self.pot_rewards[hand[s.value]]) input = np.array([ [cards for cards in figgie.cards[figgie.active_player]], [market.buying_price if market.buying_price is not None else 0 for market in figgie.markets], [market.selling_price if market.selling_price is not None else 0 for market in figgie.markets], [market.last_price if market.last_price is not None else 0 for market in figgie.markets], [market.operations for market in figgie.markets], [market.transactions for market in figgie.markets], ], dtype=np.float32).flatten() input = torch.from_numpy(input).view(-1, 24) percents = self.model(input) percents = torch.exp(percents) percents = percents.detach().numpy().flatten() return (percents * result).astype(int)
parser.add_argument("--nums_choose", type=int, default=5, help="number of potential positons for final search") args = parser.parse_args() for i in [5, 6, 7]: seed = i torch.manual_seed(seed) torch.cuda.empty_cache() print('Outout model name is ../donemodel/new_sticker_model0' + str(args.out) + str(seed) + '.pt') dataloaders, dataset_sizes = data_process_lisa(batch_size=256) #256 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model_ft = Net() model_ft.load_state_dict(torch.load('../donemodel/' + args.model)) #model_ft.load_weights() model_ft.to(device) # model_ft = nn.DataParallel(model,device_ids=[0,1]) criterion = nn.CrossEntropyLoss() #optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9) optimizer_ft = optim.Adam(model_ft.parameters(), lr=0.01) #optimizer_ft = optim.Adadelta(model_ft.parameters(), lr=0.1, rho=0.9, eps=1e-06, weight_decay=0.01) # Decay LR by a factor of 0.1 every 7 epochs exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=100,