fradinho commited on
Commit
2a9537a
·
1 Parent(s): 9f12d03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -390
app.py CHANGED
@@ -7,14 +7,7 @@ import tensorflow
7
  import tensorflow as tf
8
  from tensorflow.keras import backend as K
9
  from tensorflow.keras.models import Model
10
- from tensorflow.keras.optimizers import Adam
11
- from tensorflow.keras.metrics import MeanIoU
12
- from tensorflow.keras.utils import normalize, to_categorical
13
- from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda
14
- from tensorflow.keras import layers
15
 
16
- size = 1024
17
- pach_size = 256
18
 
19
  def jacard(y_true, y_pred):
20
  y_true_c = K.flatten(y_true)
@@ -28,388 +21,6 @@ def bce_dice(y_true, y_pred):
28
  return bce(y_true, y_pred) - K.log(jacard(y_true, y_pred))
29
 
30
 
31
- def upsample(X,X_side):
32
- """
33
- Upsampling and concatination with the side path
34
- """
35
-
36
- X = Conv2DTranspose(int(X.shape[1]/2), (3, 3), strides=(2, 2), padding='same')(X)
37
- #X = tf.keras.layers.UpSampling2D((2,2))(X)
38
- concat = tf.keras.layers.Concatenate()([X,X_side])
39
- return concat
40
-
41
- def gating_signal(input, out_size, batch_norm=False):
42
- """
43
- resize the down layer feature map into the same dimension as the up layer feature map
44
- using 1x1 conv
45
- :return: the gating feature map with the same dimension of the up layer feature map
46
- """
47
- x = layers.Conv2D(out_size, (1, 1), padding='same')(input)
48
- if batch_norm:
49
- x = layers.BatchNormalization()(x)
50
- x = layers.Activation('relu')(x)
51
- return x
52
-
53
-
54
-
55
- def attention_block(x, gating, inter_shape):
56
- shape_x = K.int_shape(x)
57
- shape_g = K.int_shape(gating)
58
-
59
- # Getting the x signal to the same shape as the gating signal
60
- theta_x = layers.Conv2D(inter_shape, (2, 2), strides=(2, 2), padding='same')(x) # 16
61
- shape_theta_x = K.int_shape(theta_x)
62
-
63
- # Getting the gating signal to the same number of filters as the inter_shape
64
- phi_g = layers.Conv2D(inter_shape, (1, 1), padding='same')(gating)
65
- upsample_g = layers.Conv2DTranspose(inter_shape, (3, 3),
66
- strides=(shape_theta_x[1] // shape_g[1], shape_theta_x[2] // shape_g[2]),
67
- padding='same')(phi_g) # 16
68
-
69
- concat_xg = layers.add([upsample_g, theta_x])
70
- act_xg = layers.Activation('relu')(concat_xg)
71
- psi = layers.Conv2D(1, (1, 1), padding='same')(act_xg)
72
- sigmoid_xg = layers.Activation('sigmoid')(psi)
73
- shape_sigmoid = K.int_shape(sigmoid_xg)
74
- upsample_psi = layers.UpSampling2D(size=(shape_x[1] // shape_sigmoid[1], shape_x[2] // shape_sigmoid[2]))(sigmoid_xg) # 32
75
-
76
- upsample_psi = repeat_elem(upsample_psi, shape_x[3])
77
-
78
- y = layers.multiply([upsample_psi, x])
79
-
80
- result = layers.Conv2D(shape_x[3], (1, 1), padding='same')(y)
81
- result_bn = layers.BatchNormalization()(result)
82
- return result_bn
83
-
84
-
85
-
86
- def repeat_elem(tensor, rep):
87
- # lambda function to repeat Repeats the elements of a tensor along an axis
88
- #by a factor of rep.
89
- # If tensor has shape (None, 256,256,3), lambda will return a tensor of shape
90
- #(None, 256,256,6), if specified axis=3 and rep=2.
91
-
92
- return layers.Lambda(lambda x, repnum: K.repeat_elements(x, repnum, axis=3),
93
- arguments={'repnum': rep})(tensor)
94
-
95
-
96
- activation_funtion = 'relu'
97
- recurrent_repeats = 2 * 4
98
- FILTER_NUM = 4 * 4
99
- axis = 3
100
-
101
- act_func = 'relu'
102
- filters = 64
103
- def encoder(inputs, input_tensor):
104
-
105
- #Contraction path
106
- conv_1 = Conv2D(filters, (3, 3), activation='relu', padding='same')(inputs)
107
- conv_1 = BatchNormalization()(conv_1)
108
- conv_1 = Dropout(0.1)(conv_1)
109
- conv_1 = Conv2D(filters, (3, 3), activation='relu', padding='same')(conv_1)
110
- conv_1 = BatchNormalization()(conv_1)
111
- pool_1 = MaxPooling2D((2, 2))(conv_1)
112
-
113
- conv_2 = Conv2D(2*filters, (3, 3), activation='relu', padding='same')(pool_1)
114
- conv_2 = BatchNormalization()(conv_2)
115
- conv_2 = Dropout(0.1)(conv_2)
116
- conv_2 = Conv2D(2*filters, (3, 3), activation='relu', padding='same')(conv_2)
117
- conv_2 = BatchNormalization()(conv_2)
118
- pool_2 = MaxPooling2D((2, 2))(conv_2)
119
-
120
- conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same')(pool_2)
121
- conv_3 = BatchNormalization()(conv_3)
122
- conv_3 = Dropout(0.1)(conv_3)
123
- conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same')(conv_3)
124
- conv_3 = BatchNormalization()(conv_3)
125
- pool_3 = MaxPooling2D((2, 2))(conv_3)
126
-
127
- conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same')(pool_3)
128
- conv_4 = BatchNormalization()(conv_4)
129
- conv_4 = Dropout(0.1)(conv_4)
130
- conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same')(conv_4)
131
- conv_4 = BatchNormalization()(conv_4)
132
- pool_4 = MaxPooling2D(pool_size=(2, 2))(conv_4)
133
-
134
- conv_5 = Conv2D(16*filters, (3, 3), activation='relu', padding='same')(pool_4)
135
- conv_5 = BatchNormalization()(conv_5)
136
- conv_5 = Dropout(0.1)(conv_5)
137
-
138
-
139
- model = Model(inputs=[input_tensor], outputs=[conv_5, conv_4, conv_3, conv_2, conv_1])
140
- return model
141
-
142
- def encoder_unet(inputs):
143
- ## Project residual
144
- # residual = layers.Conv2D(filters, 1, strides=2, padding="same")(
145
- # previous_block_activation
146
- # )
147
- #x = layers.add([x, residual]) # Add back residual
148
- #Contraction path
149
- #Contraction path
150
- conv_11 = Conv2D(filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(inputs)
151
- conv_11 = BatchNormalization()(conv_11)
152
- conv_11 = Dropout(0.2)(conv_11)
153
- conv_1 = Conv2D(filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_11)
154
- conv_1 = BatchNormalization()(conv_1)
155
- #conv_1 = concatenate([resblock(conv_11, 64), conv_1], axis=3)
156
- #conv_1 = Dropout(0.2)(conv_1)
157
- #pool_1 = layers.GaussianNoise(0.1+np.random.random()*0.4)(conv_1)
158
- pool_1 = MaxPooling2D((2, 2))(conv_1)
159
-
160
-
161
-
162
- conv_2 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(pool_1)
163
- conv_2 = BatchNormalization()(conv_2)
164
- conv_2 = Dropout(0.2)(conv_2)
165
- conv_2 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_2)
166
- conv_2 = BatchNormalization()(conv_2)
167
- #conv_2 = Dropout(0.2)(conv_2)
168
- #conv_2 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_2)
169
- #conv_2 = concatenate([resblock(pool_1, 128), conv_2], axis=3)
170
- #conv_2 = BatchNormalization()(conv_2)
171
- #conv_2 = Dropout(0.2)(conv_2)
172
- #pool_2 = layers.GaussianNoise(0.1+np.random.random()*0.4)(conv_2)
173
- pool_2 = MaxPooling2D((2, 2))(conv_2)
174
-
175
-
176
-
177
- conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(pool_2)
178
- conv_3 = BatchNormalization()(conv_3)
179
- conv_3 = Dropout(0.2)(conv_3)
180
- conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_3)
181
- conv_3 = BatchNormalization()(conv_3)
182
- #conv_3 = Dropout(0.2)(conv_3)
183
- #conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_3)
184
- #conv_3 = BatchNormalization()(conv_3)
185
- #conv_3 = Dropout(0.2)(conv_3)
186
- conv_3 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_3)
187
- conv_3 = BatchNormalization()(conv_3)
188
- #conv_3 = concatenate([resblock(pool_2, 256), conv_3], axis=3)
189
- #conv_3 = Dropout(0.2)(conv_3)
190
- #pool_3 = layers.GaussianNoise(0.1+np.random.random()*0.4)(conv_3)
191
- pool_3 = MaxPooling2D((2, 2))(conv_3)
192
-
193
-
194
-
195
- conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(pool_3)
196
- conv_4 = BatchNormalization()(conv_4)
197
- conv_4 = Dropout(0.2)(conv_4)
198
- #conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_4)
199
- #conv_4 = BatchNormalization()(conv_4)
200
- #conv_4 = Dropout(0.2)(conv_4)
201
- conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_4)
202
- conv_4 = BatchNormalization()(conv_4)
203
- conv_4 = Dropout(0.2)(conv_4)
204
- #conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_4)
205
- #conv_4 = BatchNormalization()(conv_4)
206
- #conv_4 = Dropout(0.2)(conv_4)
207
- conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_4)
208
- conv_4 = BatchNormalization()(conv_4)
209
- #conv_4 = concatenate([resblock(pool_3, 512), conv_4], axis=3)
210
- #conv_4 = Dropout(0.2)(conv_4)
211
- #pool_4 = layers.GaussianNoise(0.1+np.random.random()*0.4)(conv_4)
212
- pool_4 = MaxPooling2D(pool_size=(2, 2))(conv_4)
213
-
214
-
215
- conv_44 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(pool_4)
216
- conv_44 = BatchNormalization()(conv_44)
217
- conv_44 = Dropout(0.2)(conv_44)
218
- conv_44 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_44)
219
- conv_44 = BatchNormalization()(conv_44)
220
- conv_44 = Dropout(0.2)(conv_44)
221
- #conv_44 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_44)
222
- #conv_44 = BatchNormalization()(conv_44)
223
- #conv_44 = Dropout(0.2)(conv_44)
224
- #conv_4 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_4)
225
- #conv_4 = BatchNormalization()(conv_4)
226
- #conv_4 = Dropout(0.2)(conv_4)
227
- conv_44 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_44)
228
- conv_44 = BatchNormalization()(conv_44)
229
- #conv_4 = concatenate([resblock(pool_3, 512), conv_4], axis=3)
230
- #conv_44 = Dropout(0.2)(conv_44)
231
- #pool_4 = layers.GaussianNoise(0.1+np.random.random()*0.4)(conv_4)
232
- pool_44 = MaxPooling2D(pool_size=(2, 2))(conv_44)
233
-
234
-
235
-
236
- conv_5 = Conv2D(16*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(pool_44)
237
- conv_5 = BatchNormalization()(conv_5)
238
- #conv_5 = Conv2D(16*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_5)
239
- #conv_5 = BatchNormalization()(conv_5)
240
- #conv_5 = concatenate([resblock(pool_4, 1024), conv_5], axis=3)
241
- #conv_5 = Dropout(0.2)(conv_5)
242
- #conv_5 = layers.GaussianNoise(0.1)(conv_5)
243
-
244
-
245
-
246
- model = Model(inputs=[inputs], outputs=[conv_5, conv_44, conv_3, conv_2, conv_1])
247
- return model
248
-
249
- def decoder(inputs, input_tensor):
250
- #Expansive path
251
-
252
- gating_64 = gating_signal(inputs[0], 16*FILTER_NUM, True)
253
- att_64 = attention_block(inputs[1], gating_64, 16*FILTER_NUM)
254
- up_stage_2 = upsample(inputs[0],inputs[1])
255
- #u6 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(inputs[0])
256
- u6 = concatenate([up_stage_2, att_64], axis=3)
257
- #u6 = concatenate([att_5, u6])
258
- #conv_6 = Conv2D(512, (3, 3), activation='relu', padding='same')(u6)
259
- #conv_6 = BatchNormalization()(conv_6)
260
- #conv_6 = Dropout(0.2)(conv_6)
261
- #conv_6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv_6)
262
- #conv_6 = Dropout(0.2)(conv_6)
263
- conv_6 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(u6)
264
- conv_6 = BatchNormalization()(conv_6)
265
- #conv_6 = Dropout(0.2)(conv_6)
266
- conv_6 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_6)
267
- conv_6 = BatchNormalization()(conv_6)
268
- #conv_6 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_6)
269
- #conv_6 = BatchNormalization()(conv_6)
270
- #conv_6 = Dropout(0.2)(conv_6)
271
- #conv_6 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_6)
272
- #conv_6 = BatchNormalization()(conv_6)
273
- #conv_6 = Dropout(0.2)(conv_6)
274
- conv_6 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_6)
275
- conv_6 = BatchNormalization()(conv_6)
276
- conv_6 = Dropout(0.2)(conv_6)
277
-
278
-
279
- up_stage_22 = Conv2DTranspose(int(conv_6.shape[1]/2), (3, 3), strides=(2, 2), padding='same')(conv_6)
280
- conv_66 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(up_stage_22)
281
- conv_66 = BatchNormalization()(conv_66)
282
- #conv_6 = Dropout(0.2)(conv_6)
283
- #conv_66 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_66)
284
- #conv_66 = BatchNormalization()(conv_66)
285
- conv_66 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_66)
286
- conv_66 = BatchNormalization()(conv_66)
287
- #conv_6 = Dropout(0.2)(conv_6)
288
- #conv_66 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_66)
289
- #conv_66 = BatchNormalization()(conv_66)
290
- #conv_6 = Dropout(0.2)(conv_6)
291
- conv_66 = Conv2D(8*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_66)
292
- conv_66 = BatchNormalization()(conv_66)
293
- conv_66 = Dropout(0.2)(conv_66)
294
-
295
-
296
- gating_128 = gating_signal(conv_66, 8*FILTER_NUM, True)
297
- att_128 = attention_block(inputs[2], gating_128, 8*FILTER_NUM)
298
- up_stage_3 = upsample(conv_66,inputs[2])
299
- #u7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv_6)
300
- u7 = concatenate([up_stage_3, att_128], axis=3)
301
- #conv_7 = Conv2D(256, (3, 3), activation='relu', padding='same')(u7)
302
- #conv_7 = BatchNormalization()(conv_7)
303
- #conv_7 = Dropout(0.2)(conv_7)
304
- #conv_7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv_7)
305
- #conv_7 = Dropout(0.2)(conv_7)
306
- conv_7 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(u7)
307
- conv_7 = BatchNormalization()(conv_7)
308
- #conv_7 = Dropout(0.2)(conv_7)
309
- conv_7 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_7)
310
- conv_7 = BatchNormalization()(conv_7)
311
- #conv_7 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_7)
312
- #conv_7 = BatchNormalization()(conv_7)
313
- #conv_7 = Dropout(0.2)(conv_7)
314
- conv_7 = Conv2D(4*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_7)
315
- conv_7 = BatchNormalization()(conv_7)
316
- conv_7 = Dropout(0.2)(conv_7)
317
-
318
- gating_256 = gating_signal(conv_7, 4*FILTER_NUM, True)
319
- att_256 = attention_block(inputs[3], gating_256, 4*FILTER_NUM)
320
- up_stage_4 = upsample(conv_7,inputs[3])
321
- #u8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv_7)
322
- u8 = concatenate([up_stage_4, att_256], axis=3)
323
- #conv_8 = Conv2D(128, (3, 3), activation='relu', padding='same')(u8)
324
- #conv_8 = BatchNormalization()(conv_8)
325
- #conv_8 = Dropout(0.1)(conv_8)
326
- conv_8 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(u8)
327
- conv_8 = BatchNormalization()(conv_8)
328
- #conv_8 = Dropout(0.2)(conv_8)
329
- #conv_8 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(u8)
330
- #conv_8 = BatchNormalization()(conv_8)
331
- #conv_8 = Dropout(0.2)(conv_8)
332
- conv_8 = Conv2D(2*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_8)
333
- conv_8 = BatchNormalization()(conv_8)
334
- conv_8 = Dropout(0.2)(conv_8)
335
-
336
- gating_512 = gating_signal(conv_8, 2*FILTER_NUM, True)
337
- att_512 = attention_block(inputs[4], gating_512, 2*FILTER_NUM)
338
- up_stage_5 = upsample(conv_8,inputs[4])
339
- #u9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv_8)
340
- u9 = concatenate([up_stage_5, att_512], axis=3)
341
-
342
- conv_9 = Conv2D(1*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(u9)
343
- conv_9 = BatchNormalization()(conv_9)
344
- #conv_9 = Dropout(0.2)(conv_9)
345
- conv_9 = Conv2D(1*filters, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal')(conv_9)
346
- conv_9 = BatchNormalization()(conv_9)
347
- conv_9 = Dropout(0.2)(conv_9)
348
-
349
- model = Model(inputs=[input_tensor], outputs=[conv_9])
350
- return model
351
-
352
-
353
-
354
- def unet_2( n_classes=2, height=pach_size, width=pach_size, channels=3, metrics = ['accuracy']):
355
- inputs = Input((height, width, channels))
356
-
357
-
358
- encode = encoder_unet(inputs)
359
- decode = decoder(encode.output, inputs)
360
-
361
- outputs = decode.output
362
- #outputs = Conv2D(n_classes, (1, 1), activation='softmax', padding='same', kernel_initializer='he_normal')(decode.output)
363
- #outputs = tf.reshape(encode_2.output[0], [None, 16, 16, 256])
364
- model = Model(inputs=[inputs], outputs=[outputs])
365
-
366
-
367
-
368
-
369
- if n_classes <= 2:
370
- model.compile(optimizer = Adam(lr = 1e-3), loss = 'binary_crossentropy', metrics = metrics)
371
- elif n_classes > 2:
372
- model.compile(optimizer = Adam(lr = 1e-3), loss = 'categorical_crossentropy', metrics = metrics)
373
-
374
-
375
- #model.summary()
376
-
377
- return model
378
-
379
- def unet_enssemble(n_classes=2, height=64, width=64, channels=3, metrics = ['accuracy']):
380
- x = Input((height, width, channels))
381
-
382
- model10 = unet_2( n_classes=n_classes, height = height, width = width, channels = 3)
383
-
384
-
385
-
386
- out = model10(x)
387
-
388
- outputs = Conv2D(n_classes, (1, 1), activation='softmax', padding='same')(out)
389
-
390
-
391
- #model = Model(inputs=[inputs], outputs=[encode.output])
392
- model = Model(inputs=[x], outputs=[outputs])
393
- #model = Model(inputs=[model7.input, model11.input], outputs=[outputs])
394
-
395
-
396
-
397
-
398
- if n_classes <= 2:
399
- model.compile(optimizer = Adam(lr = 1e-3), loss = 'binary_crossentropy', metrics = metrics)
400
- elif n_classes > 2:
401
- model.compile(optimizer = Adam(lr = 1e-3), loss = 'categorical_crossentropy', metrics = metrics)
402
-
403
- #if summary:
404
- # model.summary()
405
-
406
- return model
407
-
408
- n_classes = 23
409
- n_channels = 3
410
- model = unet_enssemble(n_classes=n_classes, height = pach_size, width = pach_size, channels = n_channels)
411
-
412
-
413
  size = 1024
414
  pach_size = 256
415
 
@@ -517,7 +128,7 @@ def weighted_categorical_crossentropy(weights):
517
  # Load the model
518
  #model = tf.keras.models.load_model("model.h5", custom_objects={"jacard":jacard, "wcce":weighted_categorical_crossentropy})
519
  #model = tf.keras.models.load_model("model_2.h5", custom_objects={"jacard":jacard, "bce_dice":bce_dice})
520
- model = model.load_weights("model_2_A (1).h5")
521
 
522
  # Create a user interface for the model
523
  my_app = gr.Blocks()
 
7
  import tensorflow as tf
8
  from tensorflow.keras import backend as K
9
  from tensorflow.keras.models import Model
 
 
 
 
 
10
 
 
 
11
 
12
  def jacard(y_true, y_pred):
13
  y_true_c = K.flatten(y_true)
 
21
  return bce(y_true, y_pred) - K.log(jacard(y_true, y_pred))
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  size = 1024
25
  pach_size = 256
26
 
 
128
  # Load the model
129
  #model = tf.keras.models.load_model("model.h5", custom_objects={"jacard":jacard, "wcce":weighted_categorical_crossentropy})
130
  #model = tf.keras.models.load_model("model_2.h5", custom_objects={"jacard":jacard, "bce_dice":bce_dice})
131
+ model = tf.keras.models.load_model("model_2 (1).h5", custom_objects={"jacard":jacard, "bce_dice":bce_dice})
132
 
133
  # Create a user interface for the model
134
  my_app = gr.Blocks()