import numpy as np
import pandas as pd
import tensorflow as tf
from PIL import Image
from tensorflow import keras
from tensorflow.keras import layers
from matplotlib import pyplot as plt
Stage-I
def generator_c(x):
mean = x[:,:128]
log_sigma = x[:,128:]
stddev = tf.exp(log_sigma)
epsilon = tf.random.normal((mean.shape[1],),dtype=tf.int32)
c = stddev * epsilon + mean
return c
class CA(keras.Model):
“”"
Get conditioning augmentation model.
Takes an embedding of shape (1024,) and returns a tensor of shape (256,)
“”"
def init(self):
super(CA,self).init()
self.fc = layers.Dense(256)
self.activation = layers.LeakyReLU(alpha=0.2)
def call(self,inputs,training=False):
x = self.activation(self.fc(inputs))
return x
class Embedding_Compressor(keras.Model):
“”"
Build embedding compressor model
“”"
def init(self):
super(Embedding_Compressor,self).init()
self.fc = layers.Dense(128)
self.activation = layers.ReLU()
def call(self,inputs,training=False):
x = self.activation(self.fc(inputs))
return x
class Generator_stage1(keras.Model):
“”"
Builds a generator model used in Stage-I
“”"
def init(self):
super(Generator_stage1,self).init()
self.ca_fc = layers.Dense(256)
self.ca_activation = layers.LeakyReLU(alpha=0.2)
#self.lambda1 = layers.Lambda(generator_c)
#self.mean1 = layers.Dense(128)
#self.log_sigma1 = layers.Dense(128)
self.fc1 = layers.Dense(128 * 8 * 4 * 4,use_bias=False)
self.activation = layers.ReLU()
self.upsampling1 = layers.UpSampling2D(size=(2,2))
self.conv1 = layers.Conv2D(512,kernel_size=3,strides=1,padding=‘same’,use_bias=False)
self.bn1 = layers.BatchNormalization()
self.ac1 = layers.ReLU()
self.upsampling2 = layers.UpSampling2D(size=(2,2))
self.conv2 = layers.Conv2D(256,kernel_size=3,strides=1,padding=‘same’,use_bias=False)
self.bn2 = layers.BatchNormalization()
self.ac2 = layers.ReLU()
self.upsampling3 = layers.UpSampling2D(size=(2,2))
self.conv3 = layers.Conv2D(128,kernel_size=3,strides=1,padding=‘same’,use_bias=False)
self.bn3 = layers.BatchNormalization()
self.ac3 = layers.ReLU()
self.upsampling4 = layers.UpSampling2D(size=(2,2))
self.conv4 = layers.Conv2D(64,kernel_size=3,strides=1,padding=‘same’,use_bias=False)
self.bn4 = layers.BatchNormalization()
self.ac4 = layers.ReLU()
self.conv5 = layers.Conv2D(3,kernel_size=3,strides=1,padding=‘same’,use_bias=False)
def call(self,inputs,training=False):
mean_logsigma = tf.split(self.ca_activation(self.ca_fc(inputs[0])),num_or_size_splits=2,axis=-1)
#print(mean_logsigma.shape)
#c = self.lambda1(mean_logsigma)
#mean_logsigma_split = tf.split(mean_logsigma,num_or_size_splits=2,axis=-1)
mean = mean_logsigma[0]
log_sigma = mean_logsigma[1]
stddev = tf.exp(log_sigma)
c = stddev * inputs[2] + mean
#print(c.shape)
gen_inputs = tf.concat([c,inputs[1]],axis=1)
#print(gen_inputs.shape)
x = self.activation(self.fc1(gen_inputs))
#print(x.shape)
x = tf.reshape(x,shape=(-1,4,4,128*8))
x = self.ac1(self.bn1(self.conv1(self.upsampling1(x)),training=training))
x = self.ac2(self.bn2(self.conv2(self.upsampling2(x)),training=training))
x = self.ac3(self.bn3(self.conv3(self.upsampling3(x)),training=training))
x = self.ac4(self.bn4(self.conv4(self.upsampling4(x)),training=training))
x = self.conv5(x)
x = tf.tanh(x)
#print(x.shape)
return x,mean_logsigma
class Discriminator_stage1(keras.Model):
def init(self):
super(Discriminator_stage1,self).init()
self.e_fc = layers.Dense(128)
self.e_ac = layers.LeakyReLU(alpha=0.2)
self.conv1 = layers.Conv2D(64,kernel_size=(4,4),padding=‘same’,strides=2,use_bias=False)
self.ac1 =