Neural netwrok does not train with binary cross entropy - tensorflow

I am using the unet for an image segmentation problem, the network trains very well when I use the Dice loss, but it does not optimize for any order of magnitude of the learning rate when I use the binary cross entropy or the weighted cross entropy with logits. This is the model I am using
def unet_no_dropout(pretrained_weights=None, input_size=(512, 512, 1), act = 'elu'):
inputs = tf.keras.layers.Input(input_size)
conv1 = tf.keras.layers.Conv2D(64, 3 , activation=act,padding='same', kernel_initializer='he_normal')(inputs)
#conv1 = tf.keras.layers.Activation(swish)(conv1)
conv1 = tf.keras.layers.Conv2D(64, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv1)
pool1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = tf.keras.layers.Conv2D(128, 3, activation=act, padding='same', kernel_initializer='he_normal')(pool1)
conv2 = tf.keras.layers.Conv2D(128, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv2)
pool2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = tf.keras.layers.Conv2D(256, 3, activation=act, padding='same', kernel_initializer='he_normal')(pool2)
conv3 = tf.keras.layers.Conv2D(256, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv3)
pool3 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = tf.keras.layers.Conv2D(512, 3, activation=act, padding='same', kernel_initializer='he_normal')(pool3)
conv4 = tf.keras.layers.Conv2D(512, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv4)
#drop4 = tf.keras.layers.Dropout(0.5)(conv4)
pool4 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = tf.keras.layers.Conv2D(1024, 3, activation=act, padding='same', kernel_initializer='he_normal')(pool4)
conv5 = tf.keras.layers.Conv2D(1024, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv5)
#drop5 = tf.keras.layers.Dropout(0.5)(conv5)
up6 = tf.keras.layers.Conv2D(512, 2, activation=act, padding='same', kernel_initializer='he_normal')(
tf.keras.layers.UpSampling2D(size=(2, 2))(conv5))
merge6 = tf.keras.layers.concatenate([conv4, up6], axis=3)
#merge6 = tf.keras.layers.concatenate([conv4, up6], axis=3)
conv6 = tf.keras.layers.Conv2D(512, 3, activation=act, padding='same', kernel_initializer='he_normal')(merge6)
conv6 = tf.keras.layers.Conv2D(512, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv6)
up7 = tf.keras.layers.Conv2D(256, 2, activation=act, padding='same', kernel_initializer='he_normal')(
tf.keras.layers.UpSampling2D(size=(2, 2))(conv6))
merge7 = tf.keras.layers.concatenate([conv3, up7], axis=3)
conv7 = tf.keras.layers.Conv2D(256, 3, activation=act, padding='same', kernel_initializer='he_normal')(merge7)
conv7 = tf.keras.layers.Conv2D(256, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv7)
up8 = tf.keras.layers.Conv2D(128, 2, activation=act, padding='same', kernel_initializer='he_normal')(
tf.keras.layers.UpSampling2D(size=(2, 2))(conv7))
merge8 = tf.keras.layers.concatenate([conv2, up8], axis=3)
conv8 = tf.keras.layers.Conv2D(128, 3, activation=act, padding='same', kernel_initializer='he_normal')(merge8)
conv8 = tf.keras.layers.Conv2D(128, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv8)
up9 = tf.keras.layers.Conv2D(64, 2, activation=act, padding='same', kernel_initializer='he_normal')(
tf.keras.layers.UpSampling2D(size=(2, 2))(conv8))
merge9 = tf.keras.layers.concatenate([conv1, up9], axis=3)
conv9 = tf.keras.layers.Conv2D(64, 3, activation=act, padding='same', kernel_initializer='he_normal')(merge9)
conv9 = tf.keras.layers.Conv2D(64, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv9)
conv9 = tf.keras.layers.Conv2D(2, 3, activation=act, padding='same', kernel_initializer='he_normal')(conv9)
conv10 = tf.keras.layers.Conv2D(1, 1, activation='sigmoid')(conv9)
model = tf.keras.Model(inputs=inputs, outputs=conv10)
#model.compile(optimizer = tf.keras.optimizers.Adam(lr = 2e-4), loss = 'binary_crossentropy', metrics = [tf.keras.metrics.Accuracy()])
#model.compile(optimizer=tf.keras.optimizers.Adam(lr = 5e-6), loss=combo_loss(alpha=1, beta=0.4), metrics=[dice_accuracy])
model.compile(optimizer=tf.keras.optimizers.Adam(lr = 8e-3), loss = 'binary_crossentropy',metrics=[dice_accuracy])
#model.compile(optimizer=RMSprop(lr=0.00001), loss=combo_loss, metrics=[dice_accuracy])
if (pretrained_weights):
model.load_weights(pretrained_weights)
return model
I have tried also the relu activation function and it is the same.
What could be a potential cause?

Related

Why a tensorflow model with Spatial Pyramid Pooling layers is slower than a sequential, vanilla model?

I'm training a U-net type model with a minor variation in the architecture which is the Atrous Spatial Pyramid pooling (ASPP) layer at the bottleneck after the encoder. I profiled the model during one forward pass and used tensorboard to check the tracer_view to see which part of the model has the highest latency.
Profiler Tracer View with ASPP layer
This revealed that there's a lot of idle GPU time at ASPP computation. I double checked it by removing the ASPP layer and the just connected the encoder to the decoder. In this experiment, the idle time that was previously there disappeared.
Profiler Tracer View without ASPP layer
I understand that the second model example would be a bit smaller than the former.
This is how my model looks like with ASPP layer. And to I just commented those ASPP layers out to profile the model without ASPP layers.
With ASPP
def get_custom_deeplab(image_size: tuple, num_classes: int):
"""
This model uses a vanilla CNN backbone. This model also uses upsampling2d in place of conv2d transpose
"""
input_layer = keras.Input(shape=(image_size[0], image_size[1], 3))
conv1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(input_layer)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool1)
conv2 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
#######ASPP layers
out_1 = Conv2D(256, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=1, padding='same')(pool4)
out_6 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=6, padding='same')(pool4)
out_12 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=10, padding='same')(pool4)
out_14 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=14, padding='same')(pool4)
x = layers.Concatenate(axis=-1)([out_1, out_6, out_12, out_14])
########ASPP's output
x = Conv2D(256, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=1, padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
skip_connection_1 = pool3
x = layers.Concatenate(axis=-1)([x,skip_connection_1])
x = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
skip_connection_2 = pool2
x = layers.Concatenate(axis=-1)([x,skip_connection_2])
x = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
x = Conv2D(64, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
x = Conv2D(
num_classes,
kernel_size=1,
padding="same",
use_bias=True,
kernel_initializer=keras.initializers.HeNormal(),
)(x)
return tf.keras.Model(inputs=input_layer,outputs=x)
But, I would like to know if there's any workaround to mitigate the problem of GPU idle time when the model has layers like ASPP?

Using Prelu in Tensorflow

I am building a reinforcement learning model.
I am trying to use PRelu in my 2D Conv model using tensorflow.
Here is the code for Actor Model.
code:
from tensorflow.keras.layers import Conv2D, Input, MaxPool1D, concatenate, Lambda, Dense, Flatten
import tensorflow as tf
# activation = tf.keras.layers.LeakyReLU(alpha=0.5)
activation = tf.keras.layers.PReLU(alpha_initializer=tf.initializers.constant(0.25))
def ActorNetwork(input_shape_A,input_shape_B, n_actions):
input_layer_A = Input(shape=input_shape_A[1:], name="input_layer_A")
input_layer_B = Input(shape=input_shape_B[1:], name="input_layer_B")
Rescale = Lambda(lambda x: tf.divide(tf.subtract(x, tf.reduce_max(x)), tf.subtract(tf.reduce_max(x), tf.reduce_min(x))))(input_layer_A)
Conv1 = Conv2D(32, 3, activation= activation, padding='same', name="Conv1")(Rescale)
Conv2 = Conv2D(32, 3, activation=activation, padding='same', name="Conv2")(Conv1)
Conv_pool_1 = Conv2D(32, 2, strides=2, activation='relu', padding='same', name="Conv_pool_1")(Conv2)
Batchnorm_1 = tf.keras.layers.BatchNormalization(name='Batchnorm_1')(Conv_pool_1)
Conv3 = Conv2D(32, 3, activation= activation, padding='same', name="Conv3")(Batchnorm_1)
Conv4 = Conv2D(32, 3, activation=activation, padding='same', name="Conv4")(Conv3)
Conv_pool_2 = Conv2D(32, 2, strides=2, activation='relu', padding='same', name="Conv_pool_2")(Conv4)
Batchnorm_2 = tf.keras.layers.BatchNormalization(name='Batchnorm_2')(Conv_pool_2)
Conv5 = Conv2D(64, 3, activation= activation, padding='same', name="Conv5")(Batchnorm_2)
Conv6 = Conv2D(64, 3, activation=activation, padding='same', name="Conv6")(Conv5)
Conv_pool_3 = Conv2D(64, 2, strides=2, activation='relu', padding='same', name="Conv_pool_3")(Conv6)
Batchnorm_3 = tf.keras.layers.BatchNormalization(name='Batchnorm_3')(Conv_pool_3)
Conv7 = Conv2D(64, 3, activation= activation, padding='same', name="Conv7")(Batchnorm_3)
Conv8 = Conv2D(64, 3, activation=activation, padding='same', name="Conv8")(Conv7)
Conv_pool_4 = Conv2D(64, 2, strides=2, activation='relu', padding='same', name="Conv_pool_4")(Conv8)
Batchnorm_4 = tf.keras.layers.BatchNormalization(name='Batchnorm_4')(Conv_pool_4)
Conv9 = Conv2D(128, 3, activation= activation, padding='same', name="Conv9")(Batchnorm_4)
Conv10 = Conv2D(128, 3, activation=activation, padding='same', name="Conv10")(Conv9)
Conv_pool_5 = Conv2D(128, 2, strides=2, activation='relu', padding='same', name="Conv_pool_5")(Conv10)
Batchnorm_5 = tf.keras.layers.BatchNormalization(name='Batchnorm_5')(Conv_pool_5)
Conv11 = Conv2D(128, 3, activation= activation, padding='same', name="Conv11")(Batchnorm_5)
Conv12 = Conv2D(128, 3, activation=activation, padding='same', name="Conv12")(Conv11)
Conv_pool_6 = Conv2D(128, 2, strides=2, activation='relu', padding='same', name="Conv_pool_6")(Conv12)
Batchnorm_6 = tf.keras.layers.BatchNormalization(name='Batchnorm_6')(Conv_pool_6)
Conv_pool_7 = Conv2D(128, 1, strides=1, activation='relu', padding='same', name="Conv_pool_7")(Batchnorm_6)
Conv_pool_8 = Conv2D(64, 1, strides=1, activation='relu', padding='same', name="Conv_pool_8")(Conv_pool_7)
Conv_pool_9 = Conv2D(32, 1, strides=1, activation='relu', padding='same', name="Conv_pool_9")(Conv_pool_8)
flatten = Flatten()(Conv_pool_9)
Concat_2 = tf.keras.layers.concatenate([flatten, input_layer_B], axis=-1,name='Concat_2')
fc1 = Dense(8194, activation='relu', name="fc1")(Concat_2)
fc2 = Dense(4096, activation='relu', name="fc2")(fc1)
fc3 = Dense(n_actions, activation='softmax', name="fc3")(fc2)
return tf.keras.models.Model(inputs=[input_layer_A,input_layer_B], outputs = fc3, name="actor_model")
model=ActorNetwork((1,1000,4000,1),(1,2),3)
model.compile()
model.summary()
print(model([tf.random.uniform((1,1000,4000,1)),tf.random.uniform((1,2))]))
tf.keras.utils.plot_model(model, show_shapes=True)
I works fine with LeakyRelu but when i use Prelu i throws error related to dimensions. I dont understand it
Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-0a596da4bc68> in <module>
131
132
--> 133 model=ActorNetwork((1,1000,4000,1),(1,2),3)
134 model.compile()
135 model.summary()
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
2011 except errors.InvalidArgumentError as e:
2012 # Convert to ValueError for backwards compatibility.
-> 2013 raise ValueError(e.message)
2014
2015 return c_op
ValueError: Exception encountered when calling layer "p_re_lu_10" (type PReLU).
Dimensions must be equal, but are 1000 and 500 for '{{node Conv3/p_re_lu_10/mul}} = Mul[T=DT_FLOAT](Conv3/p_re_lu_10/Neg, Conv3/p_re_lu_10/Relu_1)' with input shapes: [1000,4000,32], [?,500,2000,32].
Call arguments received:
• inputs=tf.Tensor(shape=(None, 500, 2000, 32), dtype=float32)
What am i doing wrong here?
The PReLu activation function maintains a learnable parameter alpha that has the same shape as the input of the function. You can read more in the documentation.
You need to define a new layer each time you want to use that activation function.
i.e
Conv1 = Conv2D(32, 3, activation=None, padding='same', name="Conv1")(Rescale)
Conv1_p_relu = tf.keras.layers.PReLU(alpha_initializer=tf.initializers.constant(0.25))(Conv1)
Conv2 = Conv2D(32, 3, activation=None, padding='same', name="Conv2")(Conv1_p_relu)
Conv2_p_relu = tf.keras.layers.PReLU(alpha_initializer=tf.initializers.constant(0.25))(Conv2)
Conv_pool_1 = Conv2D(32, 2, strides=2, activation='relu', padding='same', name="Conv_pool_1")(Conv2_p_relu)

MutableGraphView::SortTopologically error: detected edge(s) creating cycle(s)

2022󈚩󈚶 13:53:37.492047: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:828] layout failed: INVALID_ARGUMENT: MutableGraphView::SortTopologically error: detected edge(s) creating cycle(s) {'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_6' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/add_5', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/clip_by_value' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_7', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/convolution_6' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/add_4', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/clip_by_value_2' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_9', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_6' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/add_5', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/clip_by_value_2' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_9', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/clip_by_value' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_7', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/convolution_6' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/add_4'}.
model.add(ConvLSTM2D(filters = 4, kernel_size = (3, 3), activation = 'tanh',data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True, input_shape = (SEQUENCE_LENGTH,
IMAGE_HEIGHT, IMAGE_WIDTH, 3)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 8, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 14, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 16, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
#model.add(TimeDistributed(Dropout(0.2)))
model.add(Flatten())
model.add(Dense(len(CLASSES_LIST), activation = "softmax"))$

having a very large loss when I am training a regression loss

I want to predict the center of the pupil from an image. so I used a CNN with 3 Dence layer.
so the input is an image and the output is a coordinate (X,Y).
my model is :
from keras.layers import Layer, Conv2D, MaxPooling2D, UpSampling2D, Dropout,Input ,concatenate, Dense
from keras.models import Model
tf.keras.layers.GlobalAveragePooling2D(
data_format=None, keepdims=False
)
def get_model():
img = Input(shape=(None, None, 3 ))
conv1_1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(img)
conv1_2 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv1_1)
pool1 = MaxPooling2D((2, 2))(conv1_2)
conv2_1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool1)
conv2_2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2_1)
conv3_1 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2_2)
conv3_2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv3_1)
pool3 = MaxPooling2D((2, 2))(conv3_2)
conv4_1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool3)
conv4_2 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv4_1)
pool4 = MaxPooling2D((2, 2))(conv4_2)
conv5_1 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool4)
conv5_2 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5_1)
conv5_2 = Dropout(0.5)(conv5_2)
conv5_3 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5_2)
pool5 = MaxPooling2D((2, 2))(conv5_3)
conv6_1 = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool5)
conv6_1 = Dropout(0.5)(conv6_1)
conv6_2 = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv6_1)
pool6 = MaxPooling2D((2, 2))(conv6_2)
conv7_1 = Conv2D(1024, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool6)
pool7 = MaxPooling2D((2, 2))(conv7_1)
conv8_1 = Conv2D(1024, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool7)
Global_pooling = tf.keras.layers.GlobalAveragePooling2D()(conv8_1)
x = Dense(500, activation='relu')(Global_pooling)
x = Dense(256, activation='relu')(x)
x = Dense(128, activation='relu')(x)
prediction = Dense(2, activation='linear')(x)
model = Model(inputs=[img], outputs=[prediction])
#model.summary()
return model
and I got a very large error with "MSE" in training. what is the problem?
Is the problem with my data?
it's my link in colab: https://colab.research.google.com/drive/12hjlT6JG8IlEXYISKw5zFJE6qBDuuVi1?usp=sharing
than you for your help
(Thanks #amina for the update)
Adding the solution here in the Answer Section though it is present in the comment section for the benefit of the community.
I used " tf.keras.losses.MeanSquaredLogarithmicError() " loss
function. It makes the amount of error smaller (because of Log )and
you can understand whether training is doing well or not.

Unet Runtime Execution stop with warning message

I am trying to run a u-net to mask the image data with its labels. Data management part in code went well. Here i am struggling with this code when i run unet.py. Code execute and throw warning and further process stop without getting into results. wanted to know if its library version issue? Didnt find anything similar to it. In the later line i have debugged, Still not giving out any clue. The code execution stops at "Using TensorFlow backend". without giving any error and does proceed to training the model and saving model phase. All the directory structure has been checked and in correct order.
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.preprocessing.image import array_to_img
import cv2
from data import *
class myUnet(object):
def __init__(self, img_rows=512, img_cols=512):
self.img_rows = img_rows
self.img_cols = img_cols
def load_data(self):
mydata = dataProcess(self.img_rows, self.img_cols)
imgs_train, imgs_mask_train = mydata.load_train_data()
imgs_test = mydata.load_test_data()
return imgs_train, imgs_mask_train, imgs_test
def get_unet(self):
inputs = Input((self.img_rows, self.img_cols, 3))
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(inputs)
# print(conv1)
conv1 = BatchNormalization()(conv1)
print ("conv1 shape:", conv1.shape)
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1)
conv1 = BatchNormalization()(conv1)
print ("conv1 shape:", conv1.shape)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
print ("pool1 shape:", pool1.shape)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool1)
print ("conv2 shape:", conv2.shape)
conv2 = BatchNormalization()(conv2)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv2)
print ("conv2 shape:", conv2.shape)
conv2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
print ("pool2 shape:", pool2.shape)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool2)
print ("conv3 shape:", conv3.shape)
conv3 = BatchNormalization()(conv3)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv3)
print ("conv3 shape:", conv3.shape)
conv3 = BatchNormalization()(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
print ("pool3 shape:", pool3.shape)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool3)
conv4 = BatchNormalization()(conv4)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv4)
conv4 = BatchNormalization()(conv4)
drop4 = Dropout(0.5)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool4)
conv5 = BatchNormalization()(conv5)
conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv5)
conv5 = BatchNormalization()(conv5)
drop5 = Dropout(0.5)(conv5)
up6 = Conv2D(512, 2, activation='relu', padding='same', kernel_initializer='he_normal')(UpSampling2D(size=(2, 2))(drop5))
up6 = BatchNormalization()(up6)
merge6 = concatenate([drop4, up6], axis=3)
print(up6)
print(merge6)
conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6)
print(conv6)
conv6 = BatchNormalization()(conv6)
conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv6)
print(conv6)
conv6 = BatchNormalization()(conv6)
up7 = Conv2D(256, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv6))
up7 = BatchNormalization()(up7)
merge7 = concatenate([conv3, up7], axis=3)
print(up7)
print(merge7)
conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge7)
conv7 = BatchNormalization()(conv7)
print(conv7)
conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7)
print(conv7)
conv7 = BatchNormalization()(conv7)
up8 = Conv2D(128, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv7))
up8 = BatchNormalization()(up8)
merge8 = concatenate([conv2, up8], axis=3)
conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge8)
conv8 = BatchNormalization()(conv8)
conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv8)
conv8 = BatchNormalization()(conv8)
up9 = Conv2D(64, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv8))
up9 = BatchNormalization()(up9)
merge9 = concatenate([conv1, up9], axis=3)
print(up9)
print(merge9)
conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge9)
conv9 = BatchNormalization()(conv9)
print(conv9)
conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9)
conv9 = BatchNormalization()(conv9)
print(conv9)
conv9 = Conv2D(2, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9)
conv9 = BatchNormalization()(conv9)
print ("conv9 shape:", conv9.shape)
conv10 = Conv2D(1, 1, activation='sigmoid')(conv9)
print(conv10)
model = Model(inputs=inputs, outputs=conv10)
model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])
return model
def train(self):
print("loading data")
imgs_train, imgs_mask_train, imgs_test = self.load_data()
print("loading data done")
model = self.get_unet()
print("got unet")
model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss', verbose=1, save_best_only=True)
print('Fitting model...')
model.fit(imgs_train, imgs_mask_train, batch_size=4, epochs=100, verbose=1,
validation_split=0.2, shuffle=True, callbacks=[model_checkpoint])
print('predict test data')
imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)
np.save('./data/results/imgs_mask_test.npy', imgs_mask_test)
def save_img(self):
print("array to image")
imgs = np.load('./data/results/imgs_mask_test.npy')
piclist = []
for line in open("./data/results/pic.txt"):
line = line.strip()
picname = line.split('/')[-1]
piclist.append(picname)
print(len(piclist))
for i in range(imgs.shape[0]):
path = "./data/results/" + piclist[i]
img = imgs[i]
img = array_to_img(img)
img.save(path)
cv_pic = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
cv_pic = cv2.resize(cv_pic,(1918,1280),interpolation=cv2.INTER_CUBIC)
binary, cv_save = cv2.threshold(cv_pic, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite(path, cv_save)
def load_model_weights(self, model):
model.load_weights('./data/unet.hdf5')
if __name__ == '__main__':
print("going to create model")
myunet = myUnet()
print("model created.. going to retreive model")
model = myunet.get_unet()
# model.summary()
# plot_model(model, to_file='model.png')
# myunet.load_model_weights(model)
print("train model")
myunet.train()
print("save model")
myunet.save_img()
WARNING:tensorflow:From unet.py:4: The name tf.keras.layers.CuDNNGRU is deprecated. Please use tf.compat.v1.keras.layers.CuDNNGRU instead.
WARNING:tensorflow:From unet.py:4: The name tf.keras.layers.CuDNNLSTM is deprecated. Please use tf.compat.v1.keras.layers.CuDNNLSTM instead.
Using TensorFlow backend.