Learn practical skills, build real-world projects, and advance your career
import os
import torch
import torchvision
import tarfile
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F
import torchvision.transforms as tt
import torchvision.models as models

from torchvision.datasets.utils import download_url
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
from torch.utils.data import random_split
from torchvision.utils import make_grid
from tqdm.notebook import tqdm


%matplotlib inline
project_name = 'pneumonia-detection-from-chest-xray'

Preparing the Data

# transformations

# stats = ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) # fill this using mean and SD of the 2 channels
train_transf = tt.Compose([
    tt.RandomResizedCrop(224),
    tt.RandomHorizontalFlip(),
    tt.ToTensor(),
#     tt.Normalize(*stats,inplace=True)
])

# No need to flip and apply other transformations as we will evaluate the model on this set
test_transf = tt.Compose([
    tt.RandomResizedCrop(224),
    tt.ToTensor()
])

val_transf = tt.Compose([
    tt.RandomResizedCrop(224), 
    tt.ToTensor(),
])
train_data = ImageFolder('../input/chest-xray-pneumonia/chest_xray/train', transform=train_transf)

test_data = ImageFolder('../input/chest-xray-pneumonia/chest_xray/test', transform=test_transf)

val_data = ImageFolder('../input/chest-xray-pneumonia/chest_xray/val', transform=val_transf)