Skip to content

Commit a61f81f

Browse files
committed
fix nan loss while training
1 parent 4756f39 commit a61f81f

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

core/common.py

+3-24
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,11 @@ def convolutional(input_layer, filters_shape, downsample=False, activate=True, b
3636
conv = tf.nn.leaky_relu(conv, alpha=0.1)
3737
elif activate_type == "mish":
3838
conv = mish(conv)
39-
# conv = softplus(conv)
40-
# conv = conv * tf.math.tanh(tf.math.softplus(conv))
41-
# conv = conv * tf.tanh(softplus(conv))
42-
# conv = tf.nn.leaky_relu(conv, alpha=0.1)
43-
# conv = tfa.activations.mish(conv)
44-
# conv = conv * tf.nn.tanh(tf.keras.activations.relu(tf.nn.softplus(conv), max_value=20))
45-
# conv = tf.nn.softplus(conv)
46-
# conv = tf.keras.activations.relu(tf.nn.softplus(conv), max_value=20)
47-
4839
return conv
49-
def softplus(x, threshold = 20.):
50-
def f1():
51-
return x
52-
def f2():
53-
return tf.exp(x)
54-
def f3():
55-
return tf.math.log(1 + tf.exp(x))
56-
# mask = tf.greater(x, threshold)
57-
# x = tf.exp(x[mask])
58-
# return tf.exp(x)
59-
return tf.case([(tf.greater(x, tf.constant(threshold)), lambda:f1()), (tf.less(x, tf.constant(-threshold)), lambda:f2())], default=lambda:f3())
60-
# return tf.case([(tf.greater(x, threshold), lambda:f1())])
40+
6141
def mish(x):
62-
return tf.keras.layers.Lambda(lambda x: x*tf.tanh(tf.math.log(1+tf.exp(x))))(x)
63-
# return tf.keras.layers.Lambda(lambda x: softplus(x))(x)
64-
# return tf.keras.layers.Lambda(lambda x: x * tf.tanh(softplus(x)))(x)
42+
return x * tf.math.tanh(tf.math.softplus(x))
43+
# return tf.keras.layers.Lambda(lambda x: x*tf.tanh(tf.math.log(1+tf.exp(x))))(x)
6544

6645
def residual_block(input_layer, input_channel, filter_num1, filter_num2, activate_type='leaky'):
6746
short_cut = input_layer

train.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from core.utils import freeze_all, unfreeze_all
1212

1313
flags.DEFINE_string('model', 'yolov4', 'yolov4, yolov3')
14-
flags.DEFINE_string('weights', './data/yolov4.weights', 'pretrained weights')
14+
flags.DEFINE_string('weights', './scripts/yolov4.weights', 'pretrained weights')
1515
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
1616

1717
def main(_argv):
@@ -60,6 +60,7 @@ def main(_argv):
6060
bbox_tensors.append(bbox_tensor)
6161

6262
model = tf.keras.Model(input_layer, bbox_tensors)
63+
model.summary()
6364

6465
if FLAGS.weights == None:
6566
print("Training from scratch")
@@ -75,6 +76,8 @@ def main(_argv):
7576
if os.path.exists(logdir): shutil.rmtree(logdir)
7677
writer = tf.summary.create_file_writer(logdir)
7778

79+
# define training step function
80+
# @tf.function
7881
def train_step(image_data, target):
7982
with tf.GradientTape() as tape:
8083
pred_result = model(image_data, training=True)
@@ -92,8 +95,8 @@ def train_step(image_data, target):
9295

9396
gradients = tape.gradient(total_loss, model.trainable_variables)
9497
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
95-
tf.print("=> STEP %4d lr: %.6f giou_loss: %4.2f conf_loss: %4.2f "
96-
"prob_loss: %4.2f total_loss: %4.2f" % (global_steps, optimizer.lr.numpy(),
98+
tf.print("=> STEP %4d/%4d lr: %.6f giou_loss: %4.2f conf_loss: %4.2f "
99+
"prob_loss: %4.2f total_loss: %4.2f" % (global_steps, total_steps, optimizer.lr.numpy(),
97100
giou_loss, conf_loss,
98101
prob_loss, total_loss))
99102
# update learning rate

0 commit comments

Comments
 (0)