CoolFace
Apppublic

SV12/ERA_Session13

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
common.py186 linesDownload Raw Back to utils
1import numpy as np2import random3import matplotlib.pyplot as plt4 5import torch6import torchvision7from torchinfo import summary8from torch_lr_finder import LRFinder9 10 11def find_lr(model, optimizer, criterion, device, trainloader, numiter, startlr, endlr):12    lr_finder = LRFinder(13        model=model, optimizer=optimizer, criterion=criterion, device=device14    )15 16    lr_finder.range_test(17        train_loader=trainloader,18        start_lr=startlr,19        end_lr=endlr,20        num_iter=numiter,21        step_mode="exp",22    )23 24    lr_finder.plot()25 26    lr_finder.reset()27 28 29def one_cycle_lr(optimizer, maxlr, steps, epochs):30    scheduler = torch.optim.lr_scheduler.OneCycleLR(31        optimizer=optimizer,32        max_lr=maxlr,33        steps_per_epoch=steps,34        epochs=epochs,35        pct_start=5 / epochs,36        div_factor=100,37        three_phase=False,38        final_div_factor=100,39        anneal_strategy="linear",40    )41    return scheduler42 43 44def show_random_images_for_each_class(train_data, num_images_per_class=16):45    for c, cls in enumerate(train_data.classes):46        rand_targets = random.sample(47            [n for n, x in enumerate(train_data.targets) if x == c],48            k=num_images_per_class,49        )50        show_img_grid(np.transpose(train_data.data[rand_targets], axes=(0, 3, 1, 2)))51        plt.title(cls)52 53 54def show_img_grid(data):55    try:56        grid_img = torchvision.utils.make_grid(data.cpu().detach())57    except:58        data = torch.from_numpy(data)59        grid_img = torchvision.utils.make_grid(data)60 61    plt.figure(figsize=(10, 10))62    plt.imshow(grid_img.permute(1, 2, 0))63 64 65def show_random_images(data_loader):66    data, target = next(iter(data_loader))67    show_img_grid(data)68 69 70def show_model_summary(model, batch_size):71    summary(72        model=model,73        input_size=(batch_size, 3, 32, 32),74        col_names=["input_size", "output_size", "num_params", "kernel_size"],75        verbose=1,76    )77 78 79def lossacc_plots(results):80    plt.plot(results["epoch"], results["trainloss"])81    plt.plot(results["epoch"], results["testloss"])82    plt.legend(["Train Loss", "Validation Loss"])83    plt.xlabel("Epochs")84    plt.ylabel("Loss")85    plt.title("Loss vs Epochs")86    plt.show()87 88    plt.plot(results["epoch"], results["trainacc"])89    plt.plot(results["epoch"], results["testacc"])90    plt.legend(["Train Acc", "Validation Acc"])91    plt.xlabel("Epochs")92    plt.ylabel("Accuracy")93    plt.title("Accuracy vs Epochs")94    plt.show()95 96 97def lr_plots(results, length):98    plt.plot(range(length), results["lr"])99    plt.xlabel("Epochs")100    plt.ylabel("Learning Rate")101    plt.title("Learning Rate vs Epochs")102    plt.show()103 104 105def get_misclassified(model, testloader, device, mis_count=10):106    misimgs, mistgts, mispreds = [], [], []107    with torch.no_grad():108        for data, target in testloader:109            data, target = data.to(device), target.to(device)110            output = model(data)111            pred = output.argmax(dim=1, keepdim=True)112            misclassified = torch.argwhere(pred.squeeze() != target).squeeze()113            for idx in misclassified:114                if len(misimgs) >= mis_count:115                    break116                misimgs.append(data[idx])117                mistgts.append(target[idx])118                mispreds.append(pred[idx].squeeze())119    return misimgs, mistgts, mispreds120 121 122# def plot_misclassified(misimgs, mistgts, mispreds, classes):123#     fig, axes = plt.subplots(len(misimgs) // 2, 2)124#     fig.tight_layout()125#     for ax, img, tgt, pred in zip(axes.ravel(), misimgs, mistgts, mispreds):126#         ax.imshow((img / img.max()).permute(1, 2, 0).cpu())127#         ax.set_title(f"{classes[tgt]} | {classes[pred]}")128#         ax.grid(False)129#         ax.set_axis_off()130#     plt.show()131 132def get_misclassified_data(model, device, test_loader, count):133    """134    Function to run the model on test set and return misclassified images135    :param model: Network Architecture136    :param device: CPU/GPU137    :param test_loader: DataLoader for test set138    """139    # Prepare the model for evaluation i.e. drop the dropout layer140    model.eval()141 142    # List to store misclassified Images143    misclassified_data = []144 145    # Reset the gradients146    with torch.no_grad():147        # Extract images, labels in a batch148        for data, target in test_loader:149 150            # Migrate the data to the device151            data, target = data.to(device), target.to(device)152 153            # Extract single image, label from the batch154            for image, label in zip(data, target):155 156                # Add batch dimension to the image157                image = image.unsqueeze(0)158 159                # Get the model prediction on the image160                output = model(image)161 162                # Convert the output from one-hot encoding to a value163                pred = output.argmax(dim=1, keepdim=True)164 165                # If prediction is incorrect, append the data166                if pred != label:167                    misclassified_data.append((image, label, pred))168            if len(misclassified_data) >= count:169                        break170            171    return misclassified_data[:count]172 173def plot_misclassified(data, classes, size=(10, 10), rows=2, cols=5, inv_normalize=None):174    fig = plt.figure(figsize=size)175    number_of_samples = len(data)176    for i in range(number_of_samples):177        plt.subplot(rows, cols, i + 1)178        img = data[i][0].squeeze().to('cpu')179        if inv_normalize is not None:180            img = inv_normalize(img)181        plt.imshow(np.transpose(img, (1, 2, 0)))182        plt.title(f"Label: {classes[data[i][1].item()]} \n Prediction: {classes[data[i][2].item()]}")183        plt.xticks([])184        plt.yticks([])185 186