avatarNaina Chaturvedi

Summary

The website content provides a guide on using PyTorch and ResNet for a Data Science and Machine Learning project, with a focus on a COVID-19 radiography dataset, and announces related educational resources and a new YouTube channel.

Abstract

The web content outlines the 54th day of a comprehensive series on Data Science and Machine Learning, with a particular focus on PyTorch and Residual Networks (ResNet). It offers insights into the basics of PyTorch, an open-source machine learning library, and its application in deep learning using the example of a COVID-19 radiography dataset. The article guides readers through creating a custom dataset, transforming images, setting up data loaders, visualizing data, and training a ResNet-18 model. Additionally, it announces the launch of the Ignito YouTube channel, which will feature project and coding exercise videos, and promotes a tech newsletter for further learning in tech interviews, coding patterns, and various tech projects. The content also provides links to other educational series and projects, emphasizing the practical implementation of machine learning concepts.

Opinions

  • The author values the importance of hands-on learning, as evidenced by the detailed project walkthrough and the emphasis on practical implementation.
  • The author is enthusiastic about sharing knowledge, shown by the launch of the Ignito YouTube channel and the invitation to subscribe to the tech newsletter.
  • There is an appreciation for the PyTorch library, highlighting its ease of use and its effectiveness in deep learning applications.
  • The author acknowledges the challenges of training deep neural networks and suggests that ResNet helps mitigate issues like vanishing gradients.
  • The author believes in the power of community and shared resources, as indicated by the promotion of various educational series and the encouragement to follow the ongoing learning journey.
  • A positive outlook on continuous learning is conveyed, with the mention of "Day 55: Coming soon!" and the encouragement to "Keep coding :)."

Day 54: 60 days of Data Science and Machine Learning Series

Pytorch and ResNet with a project…

Pic credits : Synced

Welcome back peeps! Hope you have been learning a lot from this 60 days of Data Science and Machine Learning Series. In this post we will learn about the basics of PyTorch ( one of my favorite library) and ResNet.

Some of the other best Series —

30 Days of Natural Language Processing ( NLP) Series

30 days of Data Engineering with projects Series

60 days of Data Science and ML Series with projects

100 days : Your Data Science and Machine Learning Degree Series with projects

23 Data Science Techniques You Should Know

Tech Interview Series — Curated List of coding questions

Complete System Design with most popular Questions Series

Complete Data Visualization and Pre-processing Series with projects

Complete Python Series with Projects

Complete Advanced Python Series with Projects

Kaggle Best Notebooks that will teach you the most

Complete Developers Guide to Git

All the Data Science and Machine Learning Resources

210 Machine Learning Projects

30 days of Machine Learning Ops

Projects Videos —

All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).

Subscribe today!

Tech Newsletter —

If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :

PyTorch, developed by Facebook AI and built based on Python is an open source machine learning — optimized tensor library used for Deep Learning applications and allows computations of the tensors on Graphical Processing Units ( GPU’s). In simple words, it’s used to process tensors which are nothing but the fundamental unit of data which can be a number, vector or matrix/n-dimensional array.

Pic credits : nvidia

A good reference to understand the vastness of PyTorch —

You can install PyTorch using —

pip3 install torch torchvision

The residual neural network (ResNet) is basically artificial neural network (ANN) which builds on pyramidal cells by utilizing skip connections to jump over the different layers. It’s frequently used in computer vision tasks, easier to optimize and solves the vanishing gradient problem.

A good reference to understand Resnet-18 ( at the link below):

Let’s dive in!

Importing Libraries

%matplotlib inline
import os
import shutil
import random
import torch
import torchvision
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
torch.manual_seed(0)

Creating Custom Dataset

class_names = ['normal', 'viral', 'covid']
root_dir = 'COVID-19 Radiography Database'
source_dirs = ['NORMAL', 'Viral Pneumonia', 'COVID-19']
if os.path.isdir(os.path.join(root_dir, source_dirs[1])):
    os.mkdir(os.path.join(root_dir, 'test'))
for i, d in enumerate(source_dirs):
        os.rename(os.path.join(root_dir, d), os.path.join(root_dir, class_names[i]))
for c in class_names:
        os.mkdir(os.path.join(root_dir, 'test', c))
for c in class_names:
        images = [x for x in os.listdir(os.path.join(root_dir, c)) if x.lower().endswith('png')]
        selected_images = random.sample(images, 30)
        for image in selected_images:
            source_path = os.path.join(root_dir, c, image)
            target_path = os.path.join(root_dir, 'test', c, image)
            shutil.move(source_path, target_path)
class cds(torch.utils.data.Dataset):
    def __init__(self,image_dirs,transform):
        def gi(class_name):
            images = [x for x in os.listdir(image_dirs[class_name]) if x.lower()]
            return images
        
        self.images ={}
        self.class_names = ['normal','viral','covid']
        
        for c in self.class_names:
            self.images[c] = gi(c)
            
        self.image_dirs = image_dirs
        self.transform = transform
        
    def __len__(self):
        return sum([len(self.images[c]) for c in self.class_names])
    
    def __getitem__(self,index):
        class_name = random.choice(self.class_names)
        index = index % len( self.images[class_name])
        image_name = self.images[class_name][index]
        image_path = os.path.join(self.image_dirs[class_name], image_name)
        image = Image.open(image_path).convert('RGB')
        return self.transform(image),self.class_names.index(class_name)

Transform Images

train_transform = torchvision.transforms.Compose([
    torchvision.transforms.Resize(size=(224,224)),
    torchvision.transforms.RandomHorizontalFlip(),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize(mean=[0.485,0.456,0.406],
                                    std= [0.229,0.224,0.225])
    
])
test_transform = torchvision.transforms.Compose([
    torchvision.transforms.Resize(size=(224,224)),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize(mean=[0.485,0.456,0.406],
                                    std= [0.229,0.224,0.225])
    
])

DataLoader

train_dirs = {
    'normal': 'COVID-19 Radiography Database/normal',
    'viral': 'COVID-19 Radiography Database/viral',
    'covid': 'COVID-19 Radiography Database/covid'
}
train_dataset = cds(train_dirs,train_transform)
test_dirs = {
    'normal': 'COVID-19 Radiography Database/test/normal',
    'viral': 'COVID-19 Radiography Database/test/viral',
    'covid': 'COVID-19 Radiography Database/test/covid'
}
test_dataset = cds(test_dirs,test_transform)
batch_size = 6
dl_train = torch.utils.data.DataLoader(train_dataset,batch_size=batch_size,shuffle=True)
dl_test = torch.utils.data.DataLoader(test_dataset,batch_size=batch_size,shuffle=True)

Data Visualization

class_names = train_dataset.class_names
def show_images(images, labels, preds):
    plt.figure(figsize=(8, 4))
    for i, image in enumerate(images):
        plt.subplot(1, 6, i + 1, xticks=[], yticks=[])
        image = image.numpy().transpose((1, 2, 0))
        mean = np.array([0.485, 0.456, 0.406])
        std = np.array([0.229, 0.224, 0.225])
        image = image * std + mean
        image = np.clip(image, 0., 1.)
        plt.imshow(image)
        col = 'green'
        if preds[i] != labels[i]:
            col = 'red'
            
        plt.xlabel(f'{class_names[int(labels[i].numpy())]}')
        plt.ylabel(f'{class_names[int(preds[i].numpy())]}', color=col)
images, labels = next(iter(dl_train))
show_images(images, labels, labels)
images, labels = next(iter(dl_test))
show_images(images, labels, labels)

Create the Model

resnet18 = torchvision.models.resnet18(pretrained=True)
resnet18.fc = torch.nn.Linear(in_features=512, out_features=3)
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(resnet18.parameters(), lr=3e-5)

Train the Model

def train(epochs):
    print('Starting training..')
    for e in range(0, epochs):
        print('='*20)
        print(f'Starting epoch {e + 1}/{epochs}')
        print('='*20)
train_loss = 0.
        val_loss = 0.
resnet18.train() # set model to training phase
for train_step, (images, labels) in enumerate(dl_train):
            optimizer.zero_grad()
            outputs = resnet18(images)
            loss = loss_fn(outputs, labels)
            loss.backward()
            optimizer.step()
            train_loss += loss.item()
            if train_step % 20 == 0:
                print('Evaluating at step', train_step)
            accuracy = 0
            resnet18.eval() # set model to eval phase
            for val_step, (images, labels) in enumerate(dl_test):
                    outputs = resnet18(images)
                    loss = loss_fn(outputs, labels)
                    val_loss += loss.item()
             _, preds = torch.max(outputs, 1)
                     accuracy += sum((preds == labels).numpy())
                    val_loss /= (val_step + 1)
                    accuracy = accuracy/len(test_dataset)
                print(f'Validation Loss: {val_loss:.4f}, Accuracy: {accuracy:.4f}')
resnet18.train()

Learnings —

How to create dataloader , custom data and train ResNet-18 model in PyTorch.

Day 55: Coming soon!

Follow and Stay tuned. Keep coding :)

For other projects, tune to —

Build Machine Learning Pipelines( With Code)

Recurrent Neural Network with Keras

Clustering Geolocation Data in Python using DBSCAN and K-Means

Facial Expression Recognition using Keras

Hyperparameter Tuning with Keras Tuner

Custom Layers in Keras

That’s it fellas. Peace out and keep coding :)

Stay Tuned and of-course let me end this post with a quote by Steve Jobs ;)

“You have to be burning with an idea, or a problem, or a wrong that you want to right. If you’re not passionate enough from the start, you’ll never stick it out.”

Machine Learning
Programming
Tech
Artificial Intelligence
Data Science
Recommended from ReadMedium