-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
50 lines (40 loc) · 1.36 KB
/
util.py
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
import os
import torch
import matplotlib.pyplot as plt
import numpy as np
from torchvision.utils import save_image
def save_decoded_image(img, epoch):
img = img.view(img.size(0), 1, 28, 28)
save_image(img, './FashionMNIST_Images/image{}.png'.format(epoch))
class add_gaussian_noise(object):
def __init__(self, mean=0., std=1.):
self.std = std
self.mean = mean
def __call__(self, tensor):
chance = (torch.rand(tensor.size())<0.25).int()
return tensor + chance * torch.rand(tensor.size()) + self.mean
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
def matplotlib_imshow(img, one_channel=False):
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
def plotLoss(train_loss, model_name):
title_name = model_name + ' Train Loss'
file_name = model_name + '_fashionmnist_loss.png'
plt.figure()
plt.plot(train_loss)
plt.title(title_name)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.savefig(file_name)
def make_folder(folder_name):
current_directory = os.getcwd()
directory = folder_name
if not os.path.exists(directory):
os.mkdir(os.path.join(current_directory, directory))
def get_device():
if torch.cuda.is_available():
device = 'cuda:0'
else:
device = 'cpu'
return device