text: import numpy as np
  import pandas as pd 
  import time 
  import matplotlib.pyplot as plt
  
  from collections import OrderedDict
  
  import torchvision 
  from torchvision import datasets, models, transforms
  
  import torch
  from torch import nn, optim
  import torch.nn.functional as F
  
  from PIL import Image
  
  import json
  
  
  def save_checkpoint(path, model, optimizer, classifier):
      model.class_to_idx = train_data.class_to_idx
      
      checkpoint_part_2 = {'input_size': 1024,
                        'output_size': 102,
                        'arch': 'densenet121',
                        'classifier' : classifier,
                        'learning_rate': learning_rate,
                        'epochs': epochs,
                        'class_to_idx': model.class_to_idx,
                        'state_dict': model.state_dict(),
                        'optimizer': optimizer.state_dict(),
                       }
      torch.save(checkpoint_part_2, path)
      
  
      
  def checkpoint_load(path):
      checkpoint = torch.load(path)
  #     model = getattr(torchvision.models, checkpoint['arch'])(pretrained=True)
      model = checkpoint['model']
      model.classifier = checkpoint['classifier']
      learning_rate = checkpoint['learning_rate']
      model.load_state_dict(checkpoint['state_dict'])
      model.class_to_idx = checkpoint['class_to_idx']
      model.epochs = checkpoint['epochs']
      model.optimizer = checkpoint['optimizer']
      
      return model
  
  
  def load_category_names(file):
      with open(file) as f:
          cat_to_name = json.load(f)
      return cat_to_name
  
  
  def device_type(gpu=True):
      return torch.device("cuda" if gpu and torch.cuda.is_available() else "cpu")
  
  def load_datasets(data_dir):
      data_dir = 'flowers'
      train_dir = data_dir + '/train'
      valid_dir = data_dir + '/valid'
      test_dir = data_dir + '/test'
      
      # Data transforming
      train_transforms = transforms.Compose([transforms.RandomRotation(30),
                                                transforms.RandomResizedCrop(224),
                                                transforms.RandomHorizontalFlip(),
                                                transforms.ToTensor(),
                                                transforms.Normalize([0.485, 0.456, 0.406],
                                                                     [0.229, 0.224, 0.225])])
  
      valid_transforms = transforms.Compose([transforms.Resize(256),
                                                  transforms.CenterCrop(224),
                                                  transforms.ToTensor(),
                                                  transforms.Normalize([0.485, 0.456, 0.406],
                                                                       [0.229, 0.224, 0.225])])
  
      test_transforms = transforms.Compose([transforms.Resize(256),
                                                  transforms.CenterCrop(224),
                                                  transforms.ToTensor(),
                                                  transforms.Normalize([0.485, 0.456, 0.406],
                                                                       [0.229, 0.224, 0.225])])    
      
      #Data loading
      train_data = datasets.ImageFolder(train_dir, transform=train_transforms)
      valid_data = datasets.ImageFolder(valid_dir, transform=valid_transforms)
      test_data = datasets.ImageFolder(test_dir, transform=test_transforms)
      
      
      return train_data, valid_data, test_data
  
  
  
  