-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
52 lines (43 loc) · 1.27 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
47
48
49
50
import torchvision
from torchvision import models,transforms,datasets
import torch
import os
import bcolz
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from torch.autograd import Variable
def save_array(fname, arr):
c=bcolz.carray(arr, rootdir=fname, mode='w')
c.flush()
def load_array(fname):
return bcolz.open(fname)[:]
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
prep1 = transforms.Compose([
transforms.CenterCrop(224),
#transforms.RandomSizedCrop(224),
#transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
def imshow(inp, title=None):
# Imshow for Tensor.
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(0.001) # pause a bit so that plots are updated
# shuffle only to get nice pictures
def shuffle_valtrain(x):
if x == 'train':
return True
else:
return False
def var_cgpu(x,use_gpu):
if use_gpu:
return Variable(x.cuda())
else:
return Variable(x)