Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
%matplotlib inline
import copy
import time
from tqdm.notebook import tqdm
import torch
import numpy as np
import torchvision
from PIL import Image
import torch.nn as nn
import matplotlib.pyplot as plt
import torch.nn.functional as F
import torch.optim as optim
import torchvision.transforms as transforms
print(torch.__version__)
1.6.0+cu101
transform = transforms.Compose([transforms.ToTensor()])
BatchSize = 200
trainset = torchvision.datasets.CIFAR10(root='./CIFAR10', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BatchSize,
shuffle=True, num_workers=4) # Creating dataloader
testset = torchvision.datasets.CIFAR10(root='./CIFAR10', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=BatchSize,
shuffle=False, num_workers=4) # Creating dataloader
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./CIFAR10/cifar-10-python.tar.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting ./CIFAR10/cifar-10-python.tar.gz to ./CIFAR10
Files already downloaded and verified
# Check availability of GPU
use_gpu = torch.cuda.is_available()
if use_gpu:
print('GPU is available!')
device = "cuda"
else:
print('GPU is not available!')
device = "cpu"
GPU is available!
Convolutional Autoencoder
class autoencoder(nn.Module):
def __init__(self):
super(autoencoder, self).__init__()
self.conv_encoder = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(0.1))
self.fc_encoder = nn.Sequential(
nn.Linear(128*4*4,1024),
nn.LeakyReLU(0.1))
self.fc_decoder = nn.Sequential(
nn.Linear(1024,128*4*4),
nn.LeakyReLU(0.1))
self.conv_decoder = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
nn.Upsample(scale_factor=2, mode='bilinear'),
nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
nn.Upsample(scale_factor=2, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
nn.Upsample(scale_factor=2, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
nn.ReLU())
def forward(self, x):
x = self.conv_encoder(x)
x = x.view(-1, 128*4*4)
x = self.fc_encoder(x)
x = self.fc_decoder(x)
x = x.view(-1, 128,4,4)
x = self.conv_decoder(x)
return x
net = autoencoder()
net = net.to(device)
init_weights = copy.deepcopy(net.conv_encoder[0].weight.data)