-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainConditionalGAN.py
65 lines (48 loc) · 1.53 KB
/
TrainConditionalGAN.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
import sys
import os
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import Training
import Constants
import torch
import torch.nn as nn
import GANUtils
import ConditionalDiscriminator
import ConditionalGenerator
import os
from torch.utils.data import DataLoader
load_folder = None
gen_load = None
disc_load = None
load = False
if len(sys.argv) != 1 and len(sys.argv) != 5:
print("Usage: python trainingStyleGan.py trainingDir <load> <folder> <generator_load> <discriminator_load>")
if len(sys.argv) == 5 and sys.argv[2] == 'load' :
load = True
load_folder = sys.argv[3]
gen_load = sys.argv[4]
disc_load = sys.argv[5]
else:
load = False
# Modulos
disc = ConditionalDiscriminator.DiscriminadorCondicional('cuda',18,64)
gen = ConditionalGenerator.GeneradorCondicional(64,15)
gen.train()
disc.train()
if not load :
disc.apply(GANUtils.weights_init)
gen.apply(GANUtils.weights_init)
# Carpeta para resultados
training_dir = sys.argv[1]
os.mkdir(training_dir)
# Dataset
ds = torch.load('PreprocessDatasets/preprocessed15ClassesDataset.pt')
dataLoader = DataLoader(ds, batch_size=Constants.BATCH_SIZE, shuffle=True)
# Entrenamiento
criterion = nn.BCEWithLogitsLoss()
n_epochs = 100
display_step = int(7230/Constants.BATCH_SIZE)
checkpoint_step = int(7230/Constants.BATCH_SIZE)
trainer = Training.Cond_Trainer(dataLoader, gen, disc, criterion,display_step, training_dir, 15, 'cuda', True, True, checkpoint_step)
trainer.train_for_epochs(n_epochs)