-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
42 lines (32 loc) · 1.05 KB
/
utils.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
import torch
import random
from PIL import Image, ImageFilter
# Hyperparameters and device configuration
NUM_CLASSES = 20
EPOCHS = 1000
BATCH_SIZE = 32
learning_rate = 0.01
momentum = 0.9
weight_decay = 0.0005
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class GaussianBlur(object):
def __init__(self, sigma=[0.1, 2.0]):
self.sigma = sigma
def __call__(self, x):
sigma = random.uniform(self.sigma[0], self.sigma[1])
x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
return x
class NumpyImageDataset:
def __init__(self, images, labels, transform=None):
self.images = images
self.labels = labels
self.transform = transform
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = self.images[idx].transpose((1, 2, 0))
label = self.labels[idx]
image = Image.fromarray((image * 255).astype('uint8'), 'RGB')
if self.transform:
image = self.transform(image)
return image, label