Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
import os
from random import randrange
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torchvision
import torchvision.models as models
from torchvision.datasets.utils import download_url
from torchvision.datasets import ImageFolder
import torchvision.transforms as tt
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
%matplotlib inline
data_dir = '../input/kermany2018/oct2017/OCT2017'
classes= os.listdir(data_dir+" /train")
num_classes = len(classes)
print("number of diseases:", num_classes)
print({cls: len(os.listdir(data_dir + f" /train/{cls}/")) for cls in sorted(classes)})
number of diseases: 4
{'CNV': 37205, 'DME': 11348, 'DRUSEN': 8616, 'NORMAL': 26315}
train_tfms =tt.Compose([
tt.Resize(256),
tt.CenterCrop(224),
tt.Resize(32),
tt.Grayscale(num_output_channels=1),
tt.ToTensor(),
tt.Normalize(mean=0.1817, std=0.1797)
])
valid_tfms=tt.Compose([
tt.Resize(256),
tt.CenterCrop(224),
tt.Grayscale(num_output_channels=1),
tt.Resize(32),
tt.ToTensor(),
tt.Normalize(mean=0.1817, std=0.1797)
])
# Create datasets
train_ds = ImageFolder(data_dir+' /train',train_tfms)
valid_ds = ImageFolder(data_dir+' /test', valid_tfms)
# set the batch size
batch_size = 100
# PyTorch data loaders
train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=0, pin_memory=True)
valid_dl = DataLoader(valid_ds, batch_size*2, num_workers=0, pin_memory=True)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu')
print(device)
cuda:0
class LeNet5(torch.nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
# Convolution (In LeNet-5, 32x32 images are given as input. Hence padding of 2 is done below)
self.conv1 = torch.nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2, bias=True)
# Max-pooling
self.max_pool_1 = torch.nn.MaxPool2d(kernel_size=2)
# Convolution
self.conv2 = torch.nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0, bias=True)
# Max-pooling
self.max_pool_2 = torch.nn.MaxPool2d(kernel_size=2)
# Fully connected layer
self.fc1 = torch.nn.Linear(16*7*7, 120) # convert matrix with 16*5*5 (= 400) features to a matrix of 120 features (columns)
self.fc2 = torch.nn.Linear(120, 84) # convert matrix with 120 features to a matrix of 84 features (columns)
self.fc3 = torch.nn.Linear(84, 4) # convert matrix with 84 features to a matrix of 10 features (columns)
def forward(self, x):
# convolve, then perform ReLU non-linearity
x = torch.nn.functional.relu(self.conv1(x))
# convolve, then perform ReLU non-linearity
x = torch.nn.functional.relu(self.conv2(x))
# max-pooling with 2x2 grid
x = self.max_pool_1(x)
# max-pooling with 2x2 grid
x = self.max_pool_2(x)
# first flatten 'max_pool_2_out' to contain 16*5*5 columns
# read through https://stackoverflow.com/a/42482819/7551231
#print(x.shape)
#x = x.view(-1, 16*5*5)
x = x.reshape(x.size(0), -1)
#x = x.view(x.size(0), -1)
# FC-1, then perform ReLU non-linearity
x = torch.nn.functional.relu(self.fc1(x))
# FC-2, then perform ReLU non-linearity
x = torch.nn.functional.relu(self.fc2(x))
# FC-3
x = self.fc3(x)
return x
net = LeNet5()
net.cuda()
LeNet5(
(conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(max_pool_1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(max_pool_2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(fc1): Linear(in_features=784, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=4, bias=True)
)