def eval_models(models_paths: list, path_to_data: str): if len(models_paths) == 0: return 0.0 # getting test loader ds = FashionMnistHandler(path_to_data, False) ds.download() ds.load() # noise parameters are not relevant since test loader shouldn't have noise _, _, test_loader = ds.get_noisy_loaders(0, '1', 0.2, 128, 128, 128) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') test_acc = [] for model_file in models_paths: # creating model checkpoint = torch.load(model_file, map_location=device) model_name = model_file.split("/")[-1] # loading from checkpoint model = CNNModel() model.load_state_dict(checkpoint['model_state_dict']) model.to(device) loss_fn = torch.nn.CrossEntropyLoss() # evaluating _, acc = Solver.eval(model, device, loss_fn=loss_fn, data_loader=test_loader) test_acc.append(acc) print(f"Model {model_name} has {acc:.4f} acc in test dataset") return test_acc
num_epochs = 100 #################################################################################### ####################### Dataloading ################################################ train_dataset = Fashion_MNIST_Data("./data/train.csv") trainloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4) test_dataset = Fashion_MNIST_Data("./data/test.csv") testloader = DataLoader(test_dataset, batch_size=batch_size,shuffle=False, num_workers=4) classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot') #################################################################################### ####################### Network #################################################### net = CNNModel() net.to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(),lr=learning_rate) #################################################################################### ####################### Training ################################################### for epoch in range(num_epochs): running_loss = 0.0 for i, (images, labels) in enumerate(trainloader): images, labels = images.to(device), labels.to(device) optimizer.zero_grad() outputs = net(images) loss = criterion(outputs, torch.max(labels,1)[1]) loss.backward() optimizer.step()