-
Notifications
You must be signed in to change notification settings - Fork 1
/
6-model_training.py
46 lines (45 loc) · 2.44 KB
/
6-model_training.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
'''Arguements-> 6 models: 2 discriminator,2 generator, 2 composite models and dataset.Model would be saved for every 5 epochs'''
def train(d_model,d_model_B,g_model_AtoB,g_model_BtoA,c_model_AtoB,c_model_btoA,dataset):
n_epochs ,n_batch = 100,1
#output square shape fo the discriminator
n_patch = d_model_A.output_shape[1]
#unpack the dataset
trainA,trainB = dataset
#image pool for fakes
poolA,poolB = list(),list()
#calculate number od batches per training epoch
bat_per_epo = int(len(trainA)/n_batch)
#no. of training iterations
n_steps = bat_per_epo*n_epochs
#enumerate the epochs
for i in range(n_steps):
#selecting the batch of real samples
X_realA, y_realA = generate_real_samples(trainA,n_batch,n_patch)
X_realB, y_realB = generate_real_samples(trainB, n_batch, n_patch)
# generate a batch of fake samples
X_fakeA, y_fakeA = generate_fake_samples(g_model_BtoA, X_realB, n_patch)
X_fakeB, y_fakeB = generate_fake_samples(g_model_AtoB, X_realA, n_patch)
#update the fakes from pool
X_fakeA = update_image_pool(poolA, X_fakeA)
X_fakeB = update_image_pool(poolB, X_fakeB)
# update generator B->A via adversarial and cycle loss
g_loss2, _, _, _, _ = c_model_BtoA.train_on_batch([X_realB, X_realA], [y_realA, X_realA, X_realB, X_realA])
# update discriminator for A -> [real/fake]
dA_loss1 = d_model_A.train_on_batch(X_realA, y_realA)
dA_loss2 = d_model_A.train_on_batch(X_fakeA, y_fakeA)
# update generator A->B via adversarial and cycle loss
g_loss1, _, _, _, _ = c_model_AtoB.train_on_batch([X_realA, X_realB], [y_realB, X_realB, X_realA, X_realB])
# update discriminator for B -> [real/fake]
dB_loss1 = d_model_B.train_on_batch(X_realB, y_realB)
dB_loss2 = d_model_B.train_on_batch(X_fakeB, y_fakeB)
# summarize performance
print('>%d, dA[%.3f,%.3f] dB[%.3f,%.3f] g[%.3f,%.3f]' % (i+1, dA_loss1,dA_loss2, dB_loss1,dB_loss2, g_loss1,g_loss2))
# evaluate the model performance every so often
if (i+1) % (bat_per_epo * 1) == 0:
# plot A->B translation
summarize_performance(i, g_model_AtoB, trainA, 'AtoB')
# plot B->A translation
summarize_performance(i, g_model_BtoA, trainB, 'BtoA')
if (i+1) % (bat_per_epo * 5) == 0:
# save the models
save_models(i, g_model_AtoB, g_model_BtoA)