-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
163 lines (134 loc) · 4.71 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
import os, sys
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import torch.autograd.variable as Variable
import numpy as np
import scipy.io as sio
from os.path import join as pjoin
#from skimage.transform import resize
#from models import HiFi1Edge
import skimage.io as io
import time
import skimage
import warnings
from PIL import Image
class Logger(object):
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.file = open(fpath, 'w')
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
class Averagvalue(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def save_checkpoint(state, filename='checkpoint.pth'):
torch.save(state, filename)
def load_pretrained(model, fname, optimizer=None):
"""
resume training from previous checkpoint
:param fname: filename(with path) of checkpoint file
:return: model, optimizer, checkpoint epoch
"""
if os.path.isfile(fname):
print("=> loading checkpoint '{}'".format(fname))
checkpoint = torch.load(fname)
model.load_state_dict(checkpoint['state_dict'])
if optimizer is not None:
optimizer.load_state_dict(checkpoint['optimizer'])
return model, optimizer, checkpoint['epoch']
else:
return model, checkpoint['epoch']
else:
print("=> no checkpoint found at '{}'".format(fname))
def load_vgg16pretrain(model, vggmodel='fast-rcnn-vgg16-pascal07-dagnn.mat'):
vgg16 = sio.loadmat(vggmodel)
torch_params = model.state_dict()
for k in vgg16.keys():
name_par = k.split('-')
size = len(name_par)
if size == 2:
name_space = name_par[0] + '.' + name_par[1]
data = np.squeeze(vgg16[k])
torch_params[name_space] = torch.from_numpy(data)
model.load_state_dict(torch_params)
def load_fsds_caffe(model, fsdsmodel='caffe-fsds.mat'):
fsds = sio.loadmat(fsdsmodel)
torch_params = model.state_dict()
for k in fsds.keys():
name_par = k.split('-')
#print (name_par)
size = len(name_par)
data = np.squeeze(fsds[k])
if 'upsample' in name_par:
# print('skip upsample')
continue
if size == 2:
name_space = name_par[0] + '.' + name_par[1]
data = np.squeeze(fsds[k])
if data.ndim==2:
data = np.reshape(data, (data.shape[0], data.shape[1]))
torch_params[name_space] = torch.from_numpy(data)
if size == 3:
# if 'bias' in name_par:
# continue
name_space = name_par[0] + '_' + name_par[1]+ '.' + name_par[2]
data = np.squeeze(fsds[k])
# print(data.shape)
if data.ndim==2:
# print (data.shape[0])
data = np.reshape(data,(data.shape[0], data.shape[1]))
if data.ndim==1 :
data = np.reshape(data, (1, len(data), 1, 1))
if data.ndim==0:
data = np.reshape(data, (1))
torch_params[name_space] = torch.from_numpy(data)
if size == 4:
# if 'bias' in name_par:
# continue
data = np.squeeze(fsds[k])
name_space = name_par[0] + '_' + name_par[1] + name_par[2] + '.' + name_par[3]
if data.ndim==2:
data = np.reshape(data,(data.shape[0], data.shape[1], 1, 1))
torch_params[name_space] = torch.from_numpy(data)
model.load_state_dict(torch_params)
print('loaded')
def weights_init(m):
if isinstance(m, nn.Conv2d):
# xavier(m.weight.data)
m.weight.data.normal_(0, 0.01)
if m.weight.data.shape == torch.Size([1,4,1,1]):
torch.nn.init.constant_(m.weight, 0.25)
if m.bias is not None:
m.bias.data.zero_()