<jupyter_start><jupyter_text>ResNet18 on CIFAR10---1) Extracted Resnet18 model and added it to [my API](https://github.com/genigarus/EVA4/tree/master/API/models)2) Used data loader, model loading, train, and test code to train ResNet18 on Cifar103) Best Test Accuracy = 90.12%, Final Test accuracy = 90.12%, number of epochs = 50. 4) Used image augmentation[Link to Google Colab Code File](https://colab.research.google.com/drive/1V86eiHb4kYyAPOXaMpquM9BjGhDDBXtC)[Link to python helper files](https://github.com/genigarus/EVA4/tree/master/API)  Google Drive Connection<jupyter_code>## Connecting google drive to colab

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
!mkdir -p drive
!google-drive-ocamlfuse drive<jupyter_output><empty_output><jupyter_text>Change working directory to where python files are located<jupyter_code>import os
default_path = 'drive/EVA4/API'
os.chdir(default_path)
print("Current working directory: "+os.getcwd())<jupyter_output>Current working directory: /content/drive/EVA4/API<jupyter_text>Imports<jupyter_code>from __future__ import print_function
import math
import numpy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR

%matplotlib inline
import matplotlib.pyplot as plt

!pip install torchsummary
from torchsummary import summary<jupyter_output>Requirement already satisfied: torchsummary in /usr/local/lib/python3.6/dist-packages (1.5.1)<jupyter_text>Model<jupyter_code>from models import *<jupyter_output><empty_output><jupyter_text>Training Code<jupyter_code>from training_testing import train<jupyter_output><empty_output><jupyter_text>Testing Code<jupyter_code>from training_testing import test<jupyter_output><empty_output><jupyter_text>Regularization<jupyter_code>from regularization import *<jupyter_output><empty_output><jupyter_text>Dataloader/Transformations/Image-augmentations<jupyter_code>from data_loader import generate_train_test_loader<jupyter_output><empty_output><jupyter_text>Misc Items like finding misclassified images<jupyter_code>from utility import *
from grad_cam import *<jupyter_output><empty_output><jupyter_text>Execution(Run models for 50 epochs)<jupyter_code># google drive path for saving models, graphs and images
PATH = "../Session9"<jupyter_output><empty_output><jupyter_text>Generating Data Loaders<jupyter_code>SEED = 1
means, stdevs = (0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
DATA_PATH = PATH+'/data'
need_albumentation = True
CIFAR10_CLASS_LABELS = ("airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck")

train_loader, test_loader, test_data = generate_train_test_loader(DATA_PATH, SEED, means, stdevs, need_albumentation)
# get some random training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images[:4]))
# print labels
print(' '.join('%5s' % CIFAR10_CLASS_LABELS[labels[j]] for j in range(4)))
print("*"*50)
print("*"*10,"With Resnet18 model","*"*10)
print("*"*50)
print("\n Initializing...")
aug_l1_l2_reg_train_losses = []
aug_l1_l2_reg_test_losses = []
aug_l1_l2_reg_train_acc = []
aug_l1_l2_reg_test_acc = []
# dropout_value = 0.2
LAMBDA1=1e-5
LR=0.01
MOMENTUM=0.9
WEIGHT_DECAY=5e-4
inp_size = (3, 32, 32)
device = set_device()

print("Creating model...")
aug_l1_l2_model =  ResNet18().to(device)

print("\n Model Summary:")
summary(aug_l1_l2_model, input_size=inp_size)
optimizer = optim.SGD(aug_l1_l2_model.parameters(), lr=LR, momentum=MOMENTUM, weight_decay=WEIGHT_DECAY)
criterion = nn.CrossEntropyLoss()

print("\n Training the model...")
EPOCHS = 50
for epoch in range(EPOCHS):
    print("EPOCH:", epoch+1)
    train(aug_l1_l2_model, device, train_loader, optimizer, criterion, epoch, aug_l1_l2_reg_train_losses, aug_l1_l2_reg_train_acc, l1=True, lambda1=LAMBDA1)
    test(aug_l1_l2_model, device, criterion, test_loader, aug_l1_l2_reg_test_losses, aug_l1_l2_reg_test_acc)

print("\n Saving trained model...")
torch.save(aug_l1_l2_model.state_dict(), PATH+"/model/cifar10_aug_model_l1_l2.pth")

print("\n Visualizing:")
visualize_graph(aug_l1_l2_reg_train_losses, aug_l1_l2_reg_train_acc, aug_l1_l2_reg_test_losses, aug_l1_l2_reg_test_acc)<jupyter_output>0%|          | 0/391 [00:00<?, ?it/s]<jupyter_text>Load Saved Model---<jupyter_code>inp_size = (3, 32, 32)
device = set_device()
model_path = PATH + "/model/cifar10_aug_model_l1_l2.pth"
aug_l1_l2_model =  ResNet18().to(device)
aug_l1_l2_model.load_state_dict(torch.load(model_path))<jupyter_output><empty_output><jupyter_text>View Correctly Classified Images---<jupyter_code>show_save_correctly_classified_images(aug_l1_l2_model, test_loader, device, PATH, name="misclassified_imgs", max_correctly_classified_images_imgs=25)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>View Incorrectly Classified Images---<jupyter_code>show_save_misclassified_images(aug_l1_l2_model, test_loader, device, PATH, name="misclassified_imgs", max_misclassified_imgs=25)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>GradCAM Visualization--- Fetching correctly and incorrectly classified images<jupyter_code>misclassified_imgs, correctly_classified_images = classify_images(aug_l1_l2_model, test_loader, device, 5)
target_layers = ["layer1","layer2","layer3","layer4"]
viz_cam = VisualizeCam(aug_l1_l2_model, CIFAR10_CLASS_LABELS, target_layers)<jupyter_output><empty_output><jupyter_text>For correctly classified images:-<jupyter_code>correct_pred_imgs = []
for i in range(len(correctly_classified_images)):
  correct_pred_imgs.append(torch.as_tensor(correctly_classified_images[i]["img"]))
viz_cam(torch.stack(correct_pred_imgs), target_layers, PATH, metric="correct")<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping i[...]<jupyter_text>For misclassified images:-<jupyter_code>misclassified_pred_images = []
for i in range(len(misclassified_imgs)):
  misclassified_pred_images.append(torch.as_tensor(misclassified_imgs[i]["img"]))
viz_cam(torch.stack(misclassified_pred_images), target_layers, PATH, metric="correct")<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping i[...]
{'fraction_non_alphanumeric': 0.1119608442948914, 'fraction_numerical': 0.03793208932395228, 'mean_word_length': 6.061195104391649, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 67, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '661921', 'license': 'MIT', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3351, 'n_words': 973, 'path': 'S9/EVA4_Session9.ipynb', 'repo_name': 'garima-mahato/EVA4', 'size': '9807'}