-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgan.py
50 lines (35 loc) · 1.18 KB
/
gan.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
import torch
from utils import EpochTracker, weights_init_normal
class GAN:
def __init__(self, device, file_prefix):
self.device = device
self.file_prefix = file_prefix
self.epoch_tracker = EpochTracker(file_prefix + "epoch.txt")
def set_input(self, real_A, real_B):
self.real_A = real_A.to(self.device)
self.real_B = real_B.to(self.device)
def forward(self):
pass
def train(self):
pass
def test(self):
with torch.no_grad():
self.forward()
@staticmethod
def set_requires_grad(nets, requires_grad=False):
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad
@staticmethod
def init_net(net, file=None):
gpu_ids = list(range(torch.cuda.device_count()))
if file is not None:
net.load_state_dict(torch.load(file))
else:
net.apply(weights_init_normal)
if len(gpu_ids) > 0:
assert(torch.cuda.is_available())
net.to(gpu_ids[0])
net = torch.nn.DataParallel(net, gpu_ids)
return net