-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.py
40 lines (36 loc) · 1.21 KB
/
functions.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
import torch
import torch.nn as nn
import functools
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm2d') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def get_norm_layer(norm_type='instance'):
if norm_type == 'batch':
norm_layer = functools.partial(nn.BatchNorm2d, affine=True)
elif norm_type == 'instance':
norm_layer = functools.partial(nn.InstanceNorm2d, affine=False)
else:
raise NotImplementedError('normalization layer [%s] is not found' % norm_type)
return norm_layer
def print_network(net):
if isinstance(net, list):
net = net[0]
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
def show_tensor(img_tensor):
with torch.no_grad():
arr = 255*(img_tensor.cpu().permute(1, 2, 0) + 1)/2
arr = arr.numpy().astype(np.uint8)
plt.figure(figsize=(7, 7))
plt.imshow(arr)
plt.show()