-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
46 lines (34 loc) · 1.08 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
43
44
45
46
import pickle
import torch
from torch.utils import data
from torchvision import datasets, transforms
def get_dataset_mean_and_std(directory):
dataset = datasets.ImageFolder(
directory,
transform=transforms.Compose([
transforms.ToTensor()
])
)
data_loader = data.DataLoader(dataset,num_workers=4)
mean = [0, 0, 0]
std = [0, 0, 0]
for channel in range(3):
_mean = 0
_std = 0
for _, (xs, _) in enumerate(data_loader):
img = xs[0][channel].numpy()
_mean += img.mean()
_std += img.std()
mean[channel] = _mean/len(dataset)
std[channel] = _std/len(dataset)
return mean, std
def save(obj, path):
with open(path, 'wb') as f:
pickle.dump(obj, f)
print('[INFO] Object saved to {}'.format(path))
def save_net(model, path):
torch.save(model.state_dict(), path)
print('[INFO] Checkpoint saved to {}'.format(path))
def load_net(model, path):
model.load_state_dict(torch.load(path))
print('[INFO] Checkpoint {} loaded'.format(path))