-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViTGAN.py
53 lines (42 loc) · 2.57 KB
/
ViTGAN.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
from Core.Discriminator import Discriminator
from Core.Generator import Generator
from Core.PytorchGAN import PytorchGAN
class ViTGAN(PytorchGAN):
def __init__(self, img_size, n_channels, lattent_space_size, generator_params=None, discriminator_params=None, criterion='bce', logger=None, opt='adam', device='cpu', ckpt_save_path=None, tag='', **kwargs):
"""
Main VitGAN class for this project
:param img_size: images size, the image must be square sized
:param n_channels: number of channel of the images
:param lattent_space_size: umber of features in the lattent space
:param generator_params: kwargs for optional parameters of the Generator, mandatory args will be filled automatically
:param discriminator_params: kwargs for optional parameters of the Discriminator, mandatory args will be filled automatically
:param criterion: loss used for training, BCE or MSE
:param logger: tensorboard logger
:param opt: optimizer to use for training
:param device: cpu or cuda
:param ckpt_save_path: save path for training checkpoints
:param tag: model tag for saved file names
"""
super().__init__(criterion=criterion, logger=logger, opt=opt, device=device, ckpt_save_path=ckpt_save_path, tag=tag)
self.img_size = img_size
self.n_channels = n_channels
self.lattent_space_size = lattent_space_size
self.generator_params = {} if generator_params is None else generator_params
self.discriminator_params = {} if discriminator_params is None else discriminator_params
self.generator_params = generator_params
self.discriminator_params = discriminator_params
self.generator_params['img_size'], self.generator_params['n_channels'], self.generator_params['lattent_size'] = self.img_size, self.n_channels, self.lattent_space_size
self.discriminator_params['img_size'], self.discriminator_params['n_channels'], self.discriminator_params['output_size'] = self.img_size, self.n_channels, 1
# Necessary attributes for PytorchGAN
self.generator = Generator(**self.generator_params)
self.discriminator = Discriminator(**self.discriminator_params)
self.generator_input_shape = (self.lattent_space_size,)
self.generator.to(self.device)
self.discriminator.to(self.device)
self.to(self.device)
def forward(self, x):
pass
def generate(self, z):
return self.generator(z)
def discriminate(self, imgs):
return self.discriminator(imgs)