Learn practical skills, build real-world projects, and advance your career
import cv2 as cv
import os
import torch.nn as nn
import torch
import torchvision
import tarfile
import torch.functional as f
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.optim
from torchvision.datasets.utils import download_url
from torch.utils.data import DataLoader as DataLoader
from google.colab.patches import cv2_imshow
from zipfile import ZipFile
import glob
from google.colab import files
import imageio
from albumentations import HorizontalFlip, VerticalFlip, Rotate
import matplotlib.pyplot as plt
batch_size=64
input_size  = 1
output_classes  = 10
lr  = 0.001
num_epochs=5

class   cnn(nn.Module):
  def __init__(self,input_size, output_classes):
    super().__init__()
    self.conv1  = nn.Sequential(
       nn.Conv2d(input_size,4,kernel_size=(3,3), padding=1, stride=1),
       nn.ReLU(),
       nn.MaxPool2d(2),
       nn.Conv2d(4,8,kernel_size=(3,3), padding=1, stride=1),
       nn.ReLU(),
       nn.MaxPool2d(2))
    self.linear2  = nn.Linear(392,output_classes)   
  def forward(self, input):    
    x   = self.conv1(input)
    x = x.reshape(x.shape[0],-1)
    x=self.linear2(x)
    return x 
device  = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)
model = cnn(1,10).to(device)
cuda
train_ds  = datasets.MNIST(root='/dataset', train=True, transform = transforms.ToTensor(), download = True)
test_ds  = datasets.MNIST(root='/dataset', train=False, transform = transforms.ToTensor(), download = True)
train_dl  = DataLoader(train_ds,batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
test_dl   = DataLoader(test_ds, batch_size=batch_size, shuffle = False, num_workers=4,pin_memory=True)
  0%|          | 0/9912422 [00:00<?, ?it/s]
Extracting /dataset/MNIST/raw/train-images-idx3-ubyte.gz to /dataset/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to /dataset/MNIST/raw/train-labels-idx1-ubyte.gz
  0%|          | 0/28881 [00:00<?, ?it/s]
Extracting /dataset/MNIST/raw/train-labels-idx1-ubyte.gz to /dataset/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to /dataset/MNIST/raw/t10k-images-idx3-ubyte.gz
  0%|          | 0/1648877 [00:00<?, ?it/s]
Extracting /dataset/MNIST/raw/t10k-images-idx3-ubyte.gz to /dataset/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to /dataset/MNIST/raw/t10k-labels-idx1-ubyte.gz
  0%|          | 0/4542 [00:00<?, ?it/s]
Extracting /dataset/MNIST/raw/t10k-labels-idx1-ubyte.gz to /dataset/MNIST/raw
/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py:481: UserWarning: This DataLoader will create 4 worker processes in total. Our suggested max number of worker in current system is 2, which is smaller than what this DataLoader is going to create. Please be aware that excessive worker creation might get DataLoader running slow or even freeze, lower the worker number to avoid potential slowness/freeze if necessary. cpuset_checked))