<jupyter_start><jupyter_text>Mounting the Google Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import os
os.chdir("/content/drive/My Drive/EVA5/Week11/")
!pip install git+https://github.com/waleedka/hiddenlayer.git
!pip install -U git+https://github.com/albumentations-team/albumentations
!pip install torch-lr-finder
!pip install torchsummary<jupyter_output>Requirement already satisfied: torchsummary in /usr/local/lib/python3.6/dist-packages (1.5.1)<jupyter_text>Importing Standard, Third-party and User-defined Libraries<jupyter_code>%matplotlib inline
from utils.CyclicLearningRate import CyclicLR
import time
import torch
from torchsummary import summary
from torchvision.utils import make_grid, save_image
import torch.nn as nn
import torch.optim as optim

import hiddenlayer as hl

from configs import basic_config

from data.data_loaders.base_data_loader import BaseDataLoader
from data.base_data_utils import get_data_loaders
from data.base_data_utils import sample_data
from data.base_data_utils import show_train_data
from data.data_transforms.base_data_transforms import UnNormalize

from utils.misc_utils import get_device_type, current_config
from utils.visualization_utils import plot_multigraph, plot_misclassified_images, plot_LR_graph, visualize_clr
from utils import logger_utils

from models.networks.resnet_davidnet import ResNetDavidNet
from models.model_builder import build_model
from models.gradcam.utils import visualize_cam
from models.gradcam.gradcam import GradCAM
from models.gradcam.gen_gradcam import generate_grad_cam_grid,generate_missclassified_gradcam_grid,upgraded_missclassified_gradcam_grid
from models.learning_rates.lr_range_test import LRRangeFinder
from models.build_graph import draw_graph


from orchestrators.base_orchestrator import start_training
from orchestrators.base_orchestrator import display_classwise_accuracy<jupyter_output><empty_output><jupyter_text>1. Triangular Scheduler<jupyter_code>max_lr = 0.10004247448979592
min_lr = (max_lr/10)
step_size = 100
iteration = step_size*10
triangular_sch = CyclicLR(max_lr,min_lr,step_size,iteration)
triangular_sch.cyclic_lr(plot=True)<jupyter_output><empty_output><jupyter_text>Configuration for the project<jupyter_code>current_config()
logger = logger_utils.get_logger(__name__)<jupyter_output><empty_output><jupyter_text>Creating Train and Test Data Loaders<jupyter_code>train_loader, test_loader = get_data_loaders(dataset_name='cifar10')<jupyter_output>**** Started Loading data ****

Files already downloaded and verified
CUDA Available? True
Files already downloaded and verified
CUDA Available? True
CUDA Available? True

**** Ended Loading data ****<jupyter_text>Peek at Sample Data<jupyter_code>classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
show_train_data(train_loader, classes)<jupyter_output><empty_output><jupyter_text>Creating Model<jupyter_code>def get_model():
  device = get_device_type()
  return ResNetDavidNet().to(device)
model = get_model()<jupyter_output><empty_output><jupyter_text>Network Summary<jupyter_code>def print_summary(network,title):
  print(title)
  summary(network,input_size=(3,32,32))
print_summary(model,'Network summary')
device = get_device_type()
input = torch.zeros([1, 3, 32, 32]).to(device)
draw_graph(model, input, view='TD')
def lr_range_test(model,start_lr, end_lr, step_mode, epochs, sgd_lr=5.34E-02, sgd_momentum=0.9):
  print(f'\n\n***** Started LR Test with step_mode={step_mode}, start_lr={start_lr}, end_lr={end_lr} *****\n\n')
  criterion = nn.NLLLoss()
  optimizer = optim.SGD(model.parameters(), lr=sgd_lr, momentum=sgd_momentum)
  lr_finder = LRRangeFinder(model, optimizer, criterion, device=device)
  lr_finder.range_test(train_loader, val_loader=test_loader, start_lr=start_lr, end_lr=end_lr,
                      epochs=epochs, step_mode=step_mode)
  lr_finder.plot(metric='acc', skip_start=0, skip_end=0)
  lr_finder.reset()
  best_acc = lr_finder.best_acc
  best_lr = lr_finder.history["lr"][lr_finder.history["acc"].index(best_acc)]
  print("Best Accuracy: %s\nBest Learning Rate: %s" % (best_acc, best_lr))
  print(f'\n\n***** Ended LR Test with step_mode={step_mode}, start_lr={start_lr}, end_lr={end_lr} *****\n\n')
linear_lr_pairs = [(1E-4, 0.1),]  ## Add pairs for lr range test in linear mode
exp_lr_pairs = [(1E-7,1E-1),] ## Add pairs for lr range test in exponential mode
## Linear Step Mode
for start_lr, end_lr in linear_lr_pairs:
  model = get_model()
  lr_range_test(model, start_lr, end_lr, 'linear', 24, sgd_lr=5.34E-02, sgd_momentum=0.9)<jupyter_output>***** Started LR Test with step_mode=linear, start_lr=0.0001, end_lr=0.1 *****<jupyter_text>Preparing for Training<jupyter_code>device = get_device_type()
model = get_model()

EPOCHS = basic_config.EPOCHS
device = get_device_type()
results = {}

models_dict = {'resnet_davidnet': {'model': model}
          }<jupyter_output><empty_output><jupyter_text>Start Training<jupyter_code>results = start_training(EPOCHS, device, train_loader, test_loader, **models_dict)
torch.save(model, f'check_points/Session11_assignment_epochs_{EPOCHS}.h5')
lst_plottingJobs_acc = []
lst_plottingLegends_acc = []
lst_plotting_loss = []
lst_plottingLegends_loss = []
lst_plotting_lr = []
lst_plottingLegends_lr = []

for model_type in results:
  lst_plottingJobs_acc.append(results[model_type][0])
  lst_plottingJobs_acc.append(results[model_type][2])
  
  lst_plottingLegends_acc.append('Training accuracy')
  lst_plottingLegends_acc.append('Validation accuracy')

  lst_plotting_loss.append(results[model_type][1])
  lst_plotting_loss.append(results[model_type][3])

  lst_plottingLegends_loss.append('Training loss')
  lst_plottingLegends_loss.append('Validation loss')

  lst_plotting_lr.append(results[model_type][5])
  lst_plottingLegends_lr.append('Learning Rate')<jupyter_output><empty_output><jupyter_text>Plotting model evaluation results<jupyter_code>plot_multigraph(lst_plottingJobs_acc,lst_plottingLegends_acc,title="Accuracy")
plot_multigraph(lst_plotting_loss,lst_plottingLegends_loss,title="Loss curves")<jupyter_output>**** Started Plotting multigraph ****<jupyter_text>Plotting Change in Learning rate during model training<jupyter_code>plot_multigraph(lst_plotting_lr,lst_plottingLegends_lr,title="Learning Rate vs Iterations")<jupyter_output>**** Started Plotting multigraph ****<jupyter_text>Plotting Loss curve at different learning rates during the model Training<jupyter_code>plot_LR_graph(results[model_type][3],results[model_type][5],'Learning Rate vs Loss')<jupyter_output>**** Started Plotting Graph ****<jupyter_text>Classwise accuracies<jupyter_code>display_classwise_accuracy(test_loader,device,model,classes)<jupyter_output>/content/drive/My Drive/EVA5/Week11/models/networks/ResenetEzhirko.py:82: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
  return F.log_softmax(out)<jupyter_text>GradCam Visualization of Miss classified images<jupyter_code>#model =torch.load(f'check_points/Session11_assignment_epochs_24.h5')
configs = [dict(model_type='resnet', arch=model, layer_name='layer3')]
generate_missclassified_gradcam_grid(configs, classes, test_loader, device, model, matched=True, num_of_images=25)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10779910167415271, 'fraction_numerical': 0.014019327616714304, 'mean_word_length': 8.655716162943495, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '544545', 'license': 'MIT', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2521, 'n_words': 464, 'path': 'orchestrators/Session11_Assignment.ipynb', 'repo_name': 'eva5covergence/EVA5_AI_Projects', 'size': '7347'}