← Back to Pull requests

fix nan problem for yolov2 #482

Open
chewwt opened this pull request with 14 lines diff across 1 files
View on GitHub
chewwt chewwt commented on Dec 21, 2017 09:43 UTC
Author

resolves #381

gradients of expit_tensor, tf.pow, and tf.sqrt are unstable when the value is 0.
expit_tensor is replaced with tf.nn.sigmoid
tf.pow is replaced with tf.square
tf.sqrt is fixed according to https://github.com/tensorflow/tensorflow/issues/4914

Values going into tf.exp is also clipped to min -1e3 and max 10

Diff: 14 lines (+6 / -8) across 1 files Reviewer: thtrieu Review duration: 4709495m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.26) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.26 · 14 lines diff across 1 files
0.26
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.162
Real PR Conversation Timeline
9 items
codecov-io codecov-io commented on Dec 21, 2017 09:47 UTC
Contributor

Codecov Report


Merging #482 into master will decrease coverage by 0.01%.

The diff coverage is 100%.

![Impacted file tree graph](https://codecov.io/gh/thtrieu/darkflow/pull/482?src=pr&el=tree)

@@            Coverage Diff             @@
##           master     #482      +/-   ##
==========================================
- Coverage   67.27%   67.25%   -0.02%     
==========================================
  Files          33       33              
  Lines        2096     2095       -1     
==========================================
- Hits         1410     1409       -1     
  Misses        686      686

| Impacted Files | Coverage Δ | |
|---|---|---|
| darkflow/net/yolov2/train.py | 100% <100%> (ø) | :arrow_up: |

------

Continue to review full report at Codecov.

Legend - Click here to learn more

Δ = absolute <relative> (impact), ø = not affected, ? = missing data

Powered by Codecov. Last update a539083...7998c13. Read the comment docs.

thtrieu thtrieu commented on code on Dec 21, 2017 11:34 UTC
Code Review
darkflow/net/yolov2/train.py:1
@@ -56,19 +53,20 @@ def loss(self, net_out):
     net_out_reshape = tf.reshape(net_out, [-1, H, W, B, (4 + 1 + C)])
     coords = net_out_reshape[:, :, :, :, :4]
     coords = tf.reshape(coords, [-1, H*W, B, 4])
-    adjusted_coords_xy = expit_tensor(coords[:,:,:,0:2])
-    adjusted_coords_wh = tf.sqrt(tf.exp(coords[:,:,:,2:4]) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2]))
+    adjusted_coords_xy = tf.nn.sigmoid(coords[:,:,:,0:2])
+    adjusted_coords_wh = tf.exp(tf.clip_by_value(coords[:,:,:,2:4], -1e3, 10)) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2])
+    adjusted_coords_wh = tf.sqrt(tf.clip_by_value(adjusted_coords_wh, 1e-10, 1e5))
     coords = tf.concat([adjusted_coords_xy, adjusted_coords_wh], 3)
 
-    adjusted_c = expit_tensor(net_out_reshape[:, :, :, :, 4])
+    adjusted_c = tf.nn.sigmoid(net_out_reshape[:, :, :, :, 4])

tf.sigmoid is enough

thtrieu thtrieu commented on code on Dec 21, 2017 11:36 UTC
Code Review
darkflow/net/yolov2/train.py:1
@@ -56,19 +53,20 @@ def loss(self, net_out):
     net_out_reshape = tf.reshape(net_out, [-1, H, W, B, (4 + 1 + C)])
     coords = net_out_reshape[:, :, :, :, :4]
     coords = tf.reshape(coords, [-1, H*W, B, 4])
-    adjusted_coords_xy = expit_tensor(coords[:,:,:,0:2])
-    adjusted_coords_wh = tf.sqrt(tf.exp(coords[:,:,:,2:4]) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2]))
+    adjusted_coords_xy = tf.nn.sigmoid(coords[:,:,:,0:2])
+    adjusted_coords_wh = tf.exp(tf.clip_by_value(coords[:,:,:,2:4], -1e3, 10)) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2])

what are the ranges of coords? why do we clip to, specifically, -1e3 and 10? Source?

chewwt chewwt commented on code on Dec 21, 2017 17:39 UTC
Code Review
darkflow/net/yolov2/train.py:1
@@ -56,19 +53,20 @@ def loss(self, net_out):
     net_out_reshape = tf.reshape(net_out, [-1, H, W, B, (4 + 1 + C)])
     coords = net_out_reshape[:, :, :, :, :4]
     coords = tf.reshape(coords, [-1, H*W, B, 4])
-    adjusted_coords_xy = expit_tensor(coords[:,:,:,0:2])
-    adjusted_coords_wh = tf.sqrt(tf.exp(coords[:,:,:,2:4]) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2]))
+    adjusted_coords_xy = tf.nn.sigmoid(coords[:,:,:,0:2])
+    adjusted_coords_wh = tf.exp(tf.clip_by_value(coords[:,:,:,2:4], -1e3, 10)) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2])

They were arbitrarily chosen. I had the problem where coords went to inf, so the main purpose is to clip the maximum. I'm actually not too sure how to choose the numbers.

sheerun sheerun commented on Dec 29, 2017 12:23 UTC
Contributor

merge please? :)

thtrieu thtrieu commented on Dec 29, 2017 18:58 UTC
Contributor

Are you sure this one solve #381 ?

sheerun sheerun commented on Dec 29, 2017 19:18 UTC
Contributor

I didn't experience this issue since I applied this patch, no issues with training as well

onurbarut onurbarut commented on Dec 30, 2017 07:56 UTC
Contributor

I tried this and it works! Thanks.

Reviewers
Assignees
chewwt chewwt
Labels
risk: low confidence: 89.3% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 26.2% (LOW) · Higher = larger blast radius
26.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.3% · Lower = rushed or shallow review
89.3%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2.8% · Safe to merge
2.8%