-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
58 lines (54 loc) · 2.3 KB
/
util.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
import torch
import torch.nn as nn
from chainer import serializers
import chainer_models
import torch_models
import gen_models.resnet_32
def load_model(model_type, net_type, model_path, config):
tM = None
if model_type == 'gen':
if net_type == 'c_dcgan':
cG = chainer_models.DCGANGenerator(
bottom_width=config['bottom_width'])
serializers.load_npz(model_path, cG)
cG = cG.to_gpu()
tM = torch_models.DCGANGenerator(cG, bw=config['bottom_width'])
elif net_type == 'c_resnet':
cG = gen_models.resnet_32.ResNetGenerator()
serializers.load_npz(model_path, cG)
cG = cG.to_gpu()
tM = torch_models.ResNetGenerator32(cG)
elif net_type == 't_cnn':
tM = torch_models.CNNDecoder(
isize=32, nc=3, k=config['z_dim'],
act=nn.Tanh(), scale_image=config['image_size'])
tM.load_state_dict(
torch.load(model_path, map_location=torch.device('cpu')))
elif net_type == 't_cnn_vae':
tM = torch_models.CNNDecoder(
isize=32, nc=3, k=config['z_dim'],
act=torch.nn.Sigmoid(), scale_image=config['image_size'])
tM.load_state_dict(
torch.load(model_path, map_location=torch.device('cpu')))
elif model_type == 'disc':
if net_type == 'c_sndcgan':
cD = chainer_models.SNDCGANDiscriminator(
bottom_width=config['bottom_width'])
serializers.load_npz(model_path, cD)
cD = cD.to_gpu()
tM = torch_models.SNDCGANDiscriminator(
cD, bw=config['bottom_width'])
elif net_type == 'c_wgan':
cD = chainer_models.WGANDiscriminator(
bottom_width=config['bottom_width'])
serializers.load_npz(model_path, cD)
cD = cD.to_gpu()
tM = torch_models.WGANDiscriminator(cD, bw=config['bottom_width'])
elif model_type == 'corr':
if net_type == 't_sndcgan':
tM = torch_models.SNDCGANDiscriminator(bw=config['bottom_width'])
tM.load_state_dict(
torch.load(model_path, map_location=torch.device('cpu')))
else:
raise ValueError('model_type can only be one of [gen, disc, corr]')
return tM