forked from facebookresearch/pytorch_GAN_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubconf.py
117 lines (86 loc) · 3.71 KB
/
hubconf.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
'''
hubconf.py for pytorch_gan_zoo repo
## Users can get the diverse models of pytorch_gan_zoo by calling
hub_model = hub.load(
'??/pytorch_gan_zoo:master',
$MODEL_NAME, #
config = None,
useGPU = True,
pretrained=False) # (Not pretrained models online yet)
Available model'names are [DCGAN, PGAN].
The config option should be a dictionnary defining the training parameters of
the model. See ??/pytorch_gan_zoo/models/trainer/standard_configurations to see
all possible options
## How can I use my model ?
### Build a random vector
inputRandom, randomLabels = model.buildNoiseData((int) $BATCH_SIZE)
### Feed a random vector to the model
model.test(inputRandom,
getAvG=True,
toCPU=True)
Arguments:
- getAvG (bool) get the smoothed version of the generator (advised)
- toCPU (bool) if set to False the output tensor will be a torch.cuda tensor
### Acces the generator
model.netG()
### Acces the discriminator
model.netD()
## Can I train my model ?
Of course. You can set all training parameters in the constructor (losses to use,
learning rate, number of iterations etc...) and use the optimizeParameters()
method to make a training steps.
Typically here will be a sample code:
for input_real in dataset:
allLosses = model.optimizeParameters(inputs_real)
# Do something with the losses
Please have a look at
models/trainer/standard_configurations to see all the
training parameters you can use.
'''
import torch.utils.model_zoo as model_zoo
# Optional list of dependencies required by the package
dependencies = ['torch', 'torchvision', 'visdom', 'numpy', 'h5py', 'scipy']
def PGAN(pretrained=False, *args, **kwargs):
"""
Progressive growing model
pretrained (bool): load a pretrained model ?
model_name (string): if pretrained, load one of the following models
celebaHQ-256, celebaHQ-512, DTD, celeba, cifar10. Default is celebaHQ.
"""
from models.progressive_gan import ProgressiveGAN as PGAN
if 'config' not in kwargs or kwargs['config'] is None:
kwargs['config'] = {}
model = PGAN(useGPU=kwargs.get('useGPU', True),
storeAVG=True,
**kwargs['config'])
checkpoint = {"celebAHQ-256": 'https://dl.fbaipublicfiles.com/gan_zoo/PGAN/celebaHQ_s6_i80000-6196db68.pth',
"celebAHQ-512": 'https://dl.fbaipublicfiles.com/gan_zoo/PGAN/celebaHQ16_december_s7_i96000-9c72988c.pth',
"DTD": 'https://dl.fbaipublicfiles.com/gan_zoo/PGAN/testDTD_s5_i96000-04efa39f.pth',
"celeba": "https://dl.fbaipublicfiles.com/gan_zoo/PGAN/celebaCropped_s5_i83000-2b0acc76.pth"}
if pretrained:
if "model_name" in kwargs:
if kwargs["model_name"] not in checkpoint.keys():
raise ValueError("model_name should be in "
+ str(checkpoint.keys()))
else:
print("Loading default model : celebaHQ-256")
kwargs["model_name"] = "celebAHQ-256"
model.load_state_dict(model_zoo.load_url(
checkpoint[kwargs["model_name"]]))
return model
def DCGAN(pretrained=False, *args, **kwargs):
"""
DCGAN basic model
pretrained (bool): load a pretrained model ? In this case load a model
trained on fashionGen cloth
"""
from models.DCGAN import DCGAN
if 'config' not in kwargs or kwargs['config'] is None:
kwargs['config'] = {}
model = DCGAN(useGPU=kwargs.get('useGPU', True),
storeAVG=True,
**kwargs['config'])
checkpoint = 'https://dl.fbaipublicfiles.com/gan_zoo/DCGAN_fashionGen-1d67302.pth'
if pretrained:
model.load_state_dict(model_zoo.load_url(checkpoint))
return model