-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample_pytorch.py
More file actions
150 lines (126 loc) · 5.12 KB
/
sample_pytorch.py
File metadata and controls
150 lines (126 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import torch
import torchvision
import torch.nn as nn
import matplotlib
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
import torch.nn.functional as F
import torch.optim as optim
import os
import time
import numpy as np
import argparse
from tqdm import tqdm
from torchvision import datasets
from torch.utils.data import DataLoader
from torchvision.utils import save_image
matplotlib.style.use('ggplot')
# image transformations
transform = transforms.Compose([ transforms.ToTensor() ])
# load datasets
trainset = datasets.FashionMNIST(root='../../../_data', train=True, download=True, transform=transform)
trainloader = DataLoader(trainset, batch_size=32, shuffle=True)
testset = datasets.FashionMNIST(root='../../../_data', train=False, download=True, transform=transform)
testloader = DataLoader(testset, batch_size=32, shuffle=False)
# define the autoencoder model
class SparseAutoencoder(nn.Module):
def __init__(self):
super(SparseAutoencoder, self).__init__()
# encoder
self.enc1 = nn.Linear(in_features=784, out_features=256)
self.enc2 = nn.Linear(in_features=256, out_features=128)
self.enc3 = nn.Linear(in_features=128, out_features=64)
self.enc4 = nn.Linear(in_features=64, out_features=32)
self.enc5 = nn.Linear(in_features=32, out_features=16)
# decoder
self.dec1 = nn.Linear(in_features=16, out_features=32)
self.dec2 = nn.Linear(in_features=32, out_features=64)
self.dec3 = nn.Linear(in_features=64, out_features=128)
self.dec4 = nn.Linear(in_features=128, out_features=256)
self.dec5 = nn.Linear(in_features=256, out_features=784)
def forward(self, x):
# encoding
x = F.relu(self.enc1(x))
x = F.relu(self.enc2(x))
x = F.relu(self.enc3(x))
x = F.relu(self.enc4(x))
x = F.relu(self.enc5(x))
# decoding
x = F.relu(self.dec1(x))
x = F.relu(self.dec2(x))
x = F.relu(self.dec3(x))
x = F.relu(self.dec4(x))
x = F.relu(self.dec5(x))
return x
class Network:
def __init__(self):
# layer models
self.model = SparseAutoencoder()
# loss function
self.loss_function = nn.MSELoss()
# ptimizer function
self.optimizer = optim.Adam(self.model.parameters(), lr=1e-3)
def train(self, train_loader, test_loader, n_epochs, batch_size, save_interval=400):
# train and validate the autoencoder neural network
train_loss = []
val_loss = []
for epoch in range(n_epochs):
train_epoch_loss = self.fit(train_loader, epoch, save_interval)
val_epoch_loss = self.validate(test_loader, epoch, save_interval)
# network results
print(f"\r[{epoch+1}/{n_epochs}] loss_train:{train_epoch_loss}, loss_val:{val_epoch_loss}", end="")
train_loss.append(train_epoch_loss)
val_loss.append(val_epoch_loss)
def fit(self, train_loader, epoch, save_interval):
self.model.train()
running_loss = 0.0
counter = 0
for i, data in tqdm(enumerate(train_loader), total=int(len(trainset)/train_loader.batch_size)):
counter += 1
img, _ = data
img = img.view(img.size(0), -1)
self.optimizer.zero_grad()
outputs = self.model(img)
mse_loss = self.loss_function(outputs, img)
l1_loss = self.loss_function_sparse(img)
loss = mse_loss #+ 0.001 * l1_loss
loss.backward()
self.optimizer.step()
running_loss += loss.item()
epoch_loss = running_loss / counter
# save image
if epoch % save_interval == 0:
img = outputs.view(img.size(0), 1, 28, 28).cpu().data
save_image(img, f"./sample_pytorch_output/image_fit_{epoch}.png")
return epoch_loss
def validate(self, test_loader, epoch, save_interval):
self.model.eval()
running_loss = 0.0
counter = 0
# with torch.no_grad():
for i, data in tqdm(enumerate(test_loader), total=int(len(testset)/test_loader.batch_size)):
counter += 1
img, _ = data
img = img.view(img.size(0), -1)
outputs = self.model(img)
loss = self.loss_function(outputs, img)
running_loss += loss.item()
epoch_loss = running_loss / counter
# save image
if epoch % save_interval == 0:
outputs = outputs.view(outputs.size(0), 1, 28, 28).cpu().data
save_image(outputs, f"./sample_pytorch_output/image_val_{epoch}.png")
return epoch_loss
def loss_function_sparse(self, image):
model_children = list(self.model.children())
values = image
loss = 0
for child in model_children:
values = F.relu((child(values)))
loss += torch.mean(torch.abs(values))
return loss
# start network
if __name__ == "__main__":
os.makedirs("sample_pytorch_output", exist_ok=True)
network = Network()
network.train(trainloader, testloader, n_epochs=10, batch_size=32, save_interval=2)