13
13
14
14
class HiCGAN ():
15
15
def __init__ (self , log_dir : str ,
16
+ number_factors : int ,
16
17
loss_weight_pixel : float = 100 , #factor for L1/L2 loss, like Isola et al. 2017
17
18
loss_weight_adversarial : float = 1.0 , #factor for adversarial loss in generator
18
19
loss_weight_discriminator : float = 0.5 , #factor for disc loss, like Isola et al. 2017
@@ -30,10 +31,10 @@ def __init__(self, log_dir: str,
30
31
31
32
self .OUTPUT_CHANNELS = 1
32
33
self .INPUT_CHANNELS = 1
33
- self .INPUT_SIZE = 256
34
+ self .input_size = 256
34
35
if input_size in [64 ,128 ,256 ]:
35
- self .INPUT_SIZE = input_size
36
- self .NR_FACTORS = 14
36
+ self .input_size = input_size
37
+ self .number_factors = number_factors
37
38
self .loss_weight_pixel = loss_weight_pixel
38
39
self .loss_weight_discriminator = loss_weight_discriminator
39
40
self .loss_weight_adversarial = loss_weight_adversarial
@@ -90,7 +91,7 @@ def __init__(self, log_dir: str,
90
91
self .__batch_counter = 0
91
92
92
93
def cnn_embedding (self , nr_filters_list = [1024 ,512 ,512 ,256 ,256 ,128 ,128 ,64 ], kernel_width_list = [4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ], apply_dropout : bool = False ):
93
- inputs = tf .keras .layers .Input (shape = (3 * self .INPUT_SIZE , self .NR_FACTORS ))
94
+ inputs = tf .keras .layers .Input (shape = (3 * self .input_size , self .number_factors ))
94
95
#add 1D convolutions
95
96
x = inputs
96
97
for i , (nr_filters , kernelWidth ) in enumerate (zip (nr_filters_list , kernel_width_list )):
@@ -108,7 +109,7 @@ def cnn_embedding(self, nr_filters_list=[1024,512,512,256,256,128,128,64], kerne
108
109
x = Dropout (0.5 )(x )
109
110
x = tf .keras .layers .LeakyReLU (alpha = 0.2 )(x )
110
111
#make the shape of a square matrix
111
- x = Conv1D (filters = self .INPUT_SIZE ,
112
+ x = Conv1D (filters = self .input_size ,
112
113
strides = 3 ,
113
114
kernel_size = 4 ,
114
115
data_format = "channels_last" ,
@@ -119,14 +120,14 @@ def cnn_embedding(self, nr_filters_list=[1024,512,512,256,256,128,128,64], kerne
119
120
x = tf .keras .layers .Add ()([x , x_T ])
120
121
x = tf .keras .layers .Lambda (lambda z : 0.5 * z )(x ) #add transpose and divide by 2
121
122
#reshape the matrix into a 2D grayscale image
122
- x = tf .keras .layers .Reshape ((self .INPUT_SIZE ,self .INPUT_SIZE ,self .INPUT_CHANNELS ))(x )
123
+ x = tf .keras .layers .Reshape ((self .input_size ,self .input_size ,self .INPUT_CHANNELS ))(x )
123
124
model = tf .keras .Model (inputs = inputs , outputs = x , name = "CNN-embedding" )
124
125
#model.build(input_shape=(3*self.INPUT_SIZE, self.NR_FACTORS))
125
126
#model.summary()
126
127
return model
127
128
128
129
def dnn_embedding (self , pretrained_model_path : str = "" ):
129
- inputs = tf .keras .layers .Input (shape = (3 * self .INPUT_SIZE , self .NR_FACTORS ))
130
+ inputs = tf .keras .layers .Input (shape = (3 * self .input_size , self .number_factors ))
130
131
x = Conv1D (filters = 1 ,
131
132
kernel_size = 1 ,
132
133
strides = 1 ,
@@ -139,7 +140,7 @@ def dnn_embedding(self, pretrained_model_path : str = ""):
139
140
x = Dense (nr_neurons , activation = "relu" , kernel_regularizer = "l2" , name = layerName )(x )
140
141
layerName = "dropout_" + str (i + 1 )
141
142
x = Dropout (0.1 , name = layerName )(x )
142
- nr_output_neurons = (self .INPUT_SIZE * (self .INPUT_SIZE + 1 )) // 2
143
+ nr_output_neurons = (self .input_size * (self .input_size + 1 )) // 2
143
144
x = Dense (nr_output_neurons , activation = "relu" ,kernel_regularizer = "l2" , name = "dense_out" )(x )
144
145
dnn_model = tf .keras .Model (inputs = inputs , outputs = x )
145
146
if pretrained_model_path != "" :
@@ -150,15 +151,15 @@ def dnn_embedding(self, pretrained_model_path : str = ""):
150
151
msg = str (e )
151
152
msg += "\n Could not load the weights of pre-trained model"
152
153
print (msg )
153
- inputs2 = tf .keras .layers .Input (shape = (3 * self .INPUT_SIZE , self .NR_FACTORS ))
154
+ inputs2 = tf .keras .layers .Input (shape = (3 * self .input_size , self .number_factors ))
154
155
x = dnn_model (inputs2 )
155
156
#place the upper triangular part from dnn model into full matrix
156
- x = CustomReshapeLayer (self .INPUT_SIZE )(x )
157
+ x = CustomReshapeLayer (self .input_size )(x )
157
158
#symmetrize the output
158
159
x_T = tf .keras .layers .Permute ((2 ,1 ))(x )
159
160
diag = tf .keras .layers .Lambda (lambda z : - 1 * tf .linalg .band_part (z , 0 , 0 ))(x )
160
161
x = tf .keras .layers .Add ()([x , x_T , diag ])
161
- out = tf .keras .layers .Reshape ((self .INPUT_SIZE , self .INPUT_SIZE , self .INPUT_CHANNELS ))(x )
162
+ out = tf .keras .layers .Reshape ((self .input_size , self .input_size , self .INPUT_CHANNELS ))(x )
162
163
dnn_embedding = tf .keras .Model (inputs = inputs2 , outputs = out , name = "DNN-embedding" )
163
164
return dnn_embedding
164
165
@@ -189,7 +190,7 @@ def upsample(filters, size, apply_dropout=False):
189
190
190
191
191
192
def Generator (self ):
192
- inputs = tf .keras .layers .Input (shape = [3 * self .INPUT_SIZE ,self .NR_FACTORS ], name = "factorData" )
193
+ inputs = tf .keras .layers .Input (shape = [3 * self .input_size ,self .number_factors ], name = "factorData" )
193
194
194
195
twoD_conversion = self .generator_embedding
195
196
#the downsampling part of the network, defined for 256x256 images
@@ -204,9 +205,9 @@ def Generator(self):
204
205
HiCGAN .downsample (512 , 4 , apply_batchnorm = False ), # (bs, 1, 1, 512)
205
206
]
206
207
#if the input images are smaller, leave out some layers accordingly
207
- if self .INPUT_SIZE < 256 :
208
+ if self .input_size < 256 :
208
209
down_stack = down_stack [:- 2 ] + down_stack [- 1 :]
209
- if self .INPUT_SIZE < 128 :
210
+ if self .input_size < 128 :
210
211
down_stack = down_stack [:- 2 ] + down_stack [- 1 :]
211
212
212
213
#the upsampling portion of the generator, designed for 256x256 images
@@ -220,9 +221,9 @@ def Generator(self):
220
221
HiCGAN .upsample (64 , 4 ), # (bs, 128, 128, 128)
221
222
]
222
223
#for smaller images, take layers away, otherwise downsampling won't work
223
- if self .INPUT_SIZE < 256 :
224
+ if self .input_size < 256 :
224
225
up_stack = up_stack [:2 ] + up_stack [3 :]
225
- if self .INPUT_SIZE < 128 :
226
+ if self .input_size < 128 :
226
227
up_stack = up_stack [:2 ] + up_stack [3 :]
227
228
228
229
initializer = tf .random_normal_initializer (0. , 0.02 )
@@ -272,13 +273,13 @@ def generator_loss(self, disc_generated_output, gen_output, target):
272
273
def Discriminator (self ):
273
274
initializer = tf .random_normal_initializer (0. , 0.02 )
274
275
275
- inp = tf .keras .layers .Input (shape = [3 * self .INPUT_SIZE , self .NR_FACTORS ], name = 'input_image' )
276
- tar = tf .keras .layers .Input (shape = [self .INPUT_SIZE , self .INPUT_SIZE , self .OUTPUT_CHANNELS ], name = 'target_image' )
276
+ inp = tf .keras .layers .Input (shape = [3 * self .input_size , self .number_factors ], name = 'input_image' )
277
+ tar = tf .keras .layers .Input (shape = [self .input_size , self .input_size , self .OUTPUT_CHANNELS ], name = 'target_image' )
277
278
embedding = self .discriminator_embedding
278
279
#Patch-GAN (Isola et al.)
279
280
d = embedding (inp )
280
281
d = tf .keras .layers .Concatenate ()([d , tar ])
281
- if self .INPUT_SIZE > 64 :
282
+ if self .input_size > 64 :
282
283
#downsample and symmetrize 1
283
284
d = HiCGAN .downsample (64 , 4 , False )(d ) # (bs, inp.size/2, inp.size/2, 64)
284
285
d_T = tf .keras .layers .Permute ((2 ,1 ,3 ))(d )
@@ -485,7 +486,7 @@ def loadGenerator(self, trainedModelPath: str):
485
486
'''
486
487
try :
487
488
trainedModel = tf .keras .models .load_model (filepath = trainedModelPath ,
488
- custom_objects = {"CustomReshapeLayer" : CustomReshapeLayer (self .INPUT_SIZE )})
489
+ custom_objects = {"CustomReshapeLayer" : CustomReshapeLayer (self .input_size )})
489
490
self .generator = trainedModel
490
491
except Exception as e :
491
492
msg = str (e )
0 commit comments