def _set_models(self): if not self.options.f_config: self.options.f_config = {'kernel_size': 7, 'feature_pos': 'post'} self.options.g_config = {'kernel_size': 1, 'feature_pos': 'post'} f_net = SimpleConvNet(**self.options.f_config) g_nets = [ SimpleConvNet(**self.options.g_config) for _ in range(self.options.n_g_nets) ] self.model = ReBiasModels(f_net, g_nets) self.evaluator = MNISTEvaluator(device=self.device)
def create_model(config): model_type = config["model_type"] if model_type == "SimpleConvNet": if model_type not in config: config[model_type] = { "conv1_size": 32, "conv2_size": 64, "fc_size": 128 } model = SimpleConvNet(**config[model_type]) elif model_type == "MiniVGG": if model_type not in config: config[model_type] = { "conv1_size": 128, "conv2_size": 256, "classifier_size": 1024 } model = MiniVGG(**config[model_type]) elif model_type == "WideResNet": if model_type not in config: config[model_type] = { "depth": 34, "num_classes": 10, "widen_factor": 10 } model = WideResNet(**config[model_type]) # elif model_type == "ShuffleNetv2": # if model_type not in config: # config[model_type] = {} # model = shufflenet_v2_x0_5() elif model_type == "MobileNetv2": if model_type not in config: config[model_type] = {"pretrained": False} model = mobilenet_v2(num_classes=10, pretrained=config[model_type]["pretrained"]) else: print(f"Error: MODEL_TYPE {model_type} unknown.") exit() config["num_parameters"] = sum(p.numel() for p in model.parameters()) config["num_trainable_parameters"] = sum(p.numel() for p in model.parameters() if p.requires_grad) return model
print("Using Cuda: %s" % use_cuda) # Set Random Seed torch.manual_seed(42) # Load the dataset, 'mnist' or 'cifar10' dataset = 'mnist' data = Dataset(dataset) trainloader = data.get_train_loader(batch_size=128) testloader = data.get_train_loader(batch_size=128) N = data.get_train_size() # Save the size of the train set input_channels = 1 dims = 28 # Initilaize the model, criterion and optimizer model = SimpleConvNet(input_channels=input_channels, dims=dims) if use_cuda: model = model.cuda() criterion = nn.CrossEntropyLoss() optimizer = VOGN(model, train_set_size=N, initial_prec=1e2, num_samples=10) #optimizer = optim.Adam(model.parameters()) model, train_loss, train_accuracy, test_loss, test_accuracy = train_model( model, [trainloader, testloader], criterion, optimizer, num_epochs=2) # Plot the results fig, ax = plt.subplots() ax.plot(train_loss, 'b') ax.plot(test_loss, 'g') ax.legend(["Train Loss", "Test Loss"]) plt.ylabel("Log Loss") plt.xlabel("Epochs")
parser.add_argument('--root_dir', type=str, default="./dataset/mnist_cifar", help='path of the dataset') parser.add_argument('--load_dir', type=str, default="./checkpoints", help="path of the checkpoints") parser.add_argument('--rho', type=float, required=True, help='bias ratio: 0.999 | 0.997 | 0.995 | 0.99 | 0.9 | 0.0') parser.add_argument('--load_epoch', type=int, required=True, help='epoch number of the model loading') args = parser.parse_args() return args if __name__ == '__main__': args = get_args() device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") _, biased_test_loader, unbiased_test_loader = get_dataloader(args) model_f = SimpleConvNet().to(device) model_v = SimpleConvNet().to(device) model_b = SimpleConvNet(kernel_size=1).to(device) model_f.load_state_dict(torch.load(os.path.join(args.load_dir, args.name, "model_f_{}.pth".format(args.load_epoch)), map_location=device)) model_v.load_state_dict(torch.load(os.path.join(args.load_dir, args.name, "model_v_{}.pth".format(args.load_epoch)), map_location=device)) model_b.load_state_dict(torch.load(os.path.join(args.load_dir, args.name, "model_b_{}.pth".format(args.load_epoch)), map_location=device)) with torch.no_grad(): model_f.eval() model_v.eval() model_b.eval() f_acc_b, v_acc_b, b_acc_b = 0., 0., 0. for batch in biased_test_loader: image, mnist_label, _ = batch[0].to(device), batch[1].to(device), batch[2].to(device) f_logits, _ = model_f(image)
def train(): # Load dataset sequences from training data print("[task1_run]: Loading dataset...") data, labels = load_mfcc_training_dataset(args.dataset) sequence_length = data[0].shape[0] print(f"[task1_run]: Finished loading. Sequence length: {sequence_length}") # Construct PyTorch data loaders for training and validation print( "[task1_run]: Performing train/val split and constructing PyTorch data" "loader") train_loader, val_loader, class_weights = construct_pytorch_dataset( data, labels, test_size=0.1, batch_size=16) n_train_samples = len(train_loader) n_test_samples = len(val_loader) print( f"[task1_run]: Loaded total of {n_train_samples} batches of 16 for training" f"and {n_test_samples} batches of 16 for testing.") # Instantiate model model = SimpleConvNet().cuda() # SGD optimzier with small learning rate optimizer = optim.SGD(model.parameters(), lr=0.00025, momentum=0.9) # Cross entropy loss with class weights criterion = nn.CrossEntropyLoss( weight=torch.FloatTensor(class_weights).cuda()) PRINT_INTERVAL = 35 # Get names of the filling types from the filling type dict target_names = list(fi_dict.keys()) # Train for 500 epochs for epoch in range(500): print(f"Epoch {epoch+1}") running_loss = 0.0 model.train() # Train mode for i_batch, batch in enumerate(train_loader): # Load data batch x, y = batch[0].cuda(), batch[1].cuda() x = x.unsqueeze(1) # Add channel dimension # Reset gradients optimizer.zero_grad() # Get model predictions pred_y = model(x) # Compute loss loss = criterion(pred_y, y) running_loss += loss.item() # Backpropagate loss.backward() # Update weights optimizer.step() if i_batch % PRINT_INTERVAL == PRINT_INTERVAL - 1: running_loss += running_loss print( f"[{epoch+1}, {i_batch+1}]: Loss: {running_loss/PRINT_INTERVAL:.4f}" ) running_loss = 0.0 if epoch % 10 == 9: correct = 0 total = 0 y_true = [] y_pred = [] with torch.no_grad(): model.eval() # Eval mode for batch in val_loader: x, y = batch[0].cuda(), batch[1].cuda() x = x.unsqueeze(1) y_true += list(y.cpu().numpy()) pred_y = model(x) _, predicted = torch.max(pred_y.data, 1) y_pred += list(predicted.cpu().numpy()) total += y.size(0) correct += (predicted == y).sum().item() print( classification_report(y_true, y_pred, target_names=target_names)) acc = 100 * correct / total print(f"Test Acc: {acc:.3f}%") print("[task1_run]: Finished training.") torch.save(model.state_dict(), 'weights/task1.weights') print("[task1_run]: Saved model state to `weights/task1.weights`.") return model
prediction_list.append( getModelPrediction(model, tl, sequence_length=1501)) # Open reference csv df = pd.read_csv(args.input_csv, index_col=0) # Write predictions to 'Filling type' column' df['Filling type'] = prediction_list df.to_csv(args.output_csv) print(f"[task1_run]: Saving predictions to `{args.output_csv}`") print("Finished.") if __name__ == "__main__": args = parser.parse_args() if not args.pred: # Train and generate test predictions trained_model = train() generate_test_preds(trained_model) else: if args.model_weights == None: print("Please specify `--model-weights` when using `--pred`.") sys.exit(1) # Load model weights and generate predictions model = SimpleConvNet() print(f"Loading `{args.model_weights}`.") model.load_state_dict(torch.load(args.model_weights)) model.cuda() generate_test_preds(model)
###For Sanity Check### # root = "./dataset/MNIST" # train_loader = get_biased_mnist_dataloader(root, batch_size=args.batch_size, # data_label_correlation=args.rho, # n_confusing_labels=9, # train=True) # biased_test_loader = get_biased_mnist_dataloader(root, batch_size=args.batch_size, # data_label_correlation=1, # n_confusing_labels=9, # train=False) # unbiased_test_loader = get_biased_mnist_dataloader(root, batch_size=args.batch_size, # data_label_correlation=0.1, # n_confusing_labels=9, # train=False) ###### model_f = SimpleConvNet().to(device) model_g = SimpleConvNet(kernel_size=1).to(device) model_v = SimpleConvNet().to(device) model_b = SimpleConvNet(kernel_size=1).to(device) criterionCE = nn.CrossEntropyLoss().to(device) criterionHSIC = HSIC_measure(args.kernel_type, train_loader, device, args.sample_rate) criterionHSIC.update_sigma(model_f, model_g) optimizer_f = AdamP(model_f.parameters(), lr=args.lr, betas=(0.9, 0.999), weight_decay=1e-2) optimizer_g = AdamP(model_g.parameters(),
transform=data_transform) train_loader = DataLoader(train_dataset, shuffle=True, batch_size=train_bs, pin_memory=True, num_workers=train_nworkers) test_dataset = torchvision.datasets.MNIST(root=data_root, train=False, download=True, transform=data_transform) test_loader = DataLoader(test_dataset, shuffle=True, batch_size=test_bs, pin_memory=True, num_workers=test_nworkers) device = utils.select_device(force_cpu=False) # instantiate network net = SimpleConvNet(1, 10).to(device) # criterion loss_fn = CustomLoss() # optimer optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate, momentum=0.5) # load checkpoint if needed start_epoch = 0 start_niter = 0 if resume: ckpt = utils.load_checkpoint(ckpt_path) # custom method for loading last checkpoint net.load_state_dict(ckpt['model_state']) start_epoch = ckpt['epoch'] start_niter = ckpt['niter'] optimizer.load_state_dict(ckpt['optim_state'])