Friday, April 8, 2022

[SOLVED] Darknet detector not initializing with output redirect

Issue

When I run my script from the CLI without output redirect everything works.

This is how myscript.py looks like:

# Load darknet 
sys.path.insert(0, '/var/AI/darknet/')
import darknet
darknet.performDetect(initOnly=True)
print('Beginning API query loop')
while True:
    # Business logic

By running my script with py myscript.py, this is the tail output I get in the console:

[yolo] params: iou loss: giou (1), iou_norm: 0.50, cls_norm: 1.00, scale_x_y: 1.00
Total BFLOPS 139.496
avg_outputs = 1103769
 Try to load weights: /var/AI/darknet/weights/yolov3-papersheet_600.weights
Loading weights from /var/AI/darknet/weights/yolov3-papersheet_600.weights...
 seen 64, trained: 76 K-images (1 Kilo-batches_64)
Done! Loaded 107 layers from weights-file
Loaded - names_list: /var/AI/darknet/data/papersheet.names, classes = 1
Initialized detector
Beginning API query loop

So this works fine, but if I run this script with output redirect:

nohup python3 myscript.py > /var/log/python/myscript.log 2>&1 & echo $!

It stops at darknet.performDetect(initOnly=True) line of the script, with the following tail output:

[yolo] params: iou loss: giou (1), iou_norm: 0.50, cls_norm: 1.00, scale_x_y: 1.00
  95 route  91                                     ->   38 x  38 x 256
  96 conv    128       1 x 1/ 1     38 x  38 x 256 ->   38 x  38 x 128 0.095 BF
  97 upsample                 2x    38 x  38 x 128 ->   76 x  76 x 128
  98 route  97 36                                  ->   76 x  76 x 384
  99 conv    128       1 x 1/ 1     76 x  76 x 384 ->   76 x  76 x 128 0.568 BF
 100 conv    256       3 x 3/ 1     76 x  76 x 128 ->   76 x  76 x 256 3.407 BF
 101 conv    128       1 x 1/ 1     76 x  76 x 256 ->   76 x  76 x 128 0.379 BF
 102 conv    256       3 x 3/ 1     76 x  76 x 128 ->   76 x  76 x 256 3.407 BF
 103 conv    128       1 x 1/ 1     76 x  76 x 256 ->   76 x  76 x 128 0.379 BF
 104 conv    256       3 x 3/ 1     76 x  76 x 128 ->   76 x  76 x 256 3.407 BF
 105 conv     18       1 x 1/ 1     76 x  76 x 256 ->   76 x  76 x  18 0.053 BF
 106 yolo
[yolo] params: iou loss: giou (1), iou_norm: 0.50, cls_norm: 1.00, scale_x_y: 1.00
Total BFLOPS 139.496
avg_outputs = 1103769
Loading weights from /var/AI/darknet/weights/yolov3-papersheet_600.weights... Try to load cfg: /var/AI/darknet/cfg/yolov3-papersheet.cfg, weights: /var/AI/darknet/weights/yolov3-papersheet_600.weights, clear = 0
mini_batch = 1, batch = 1, time_steps = 1, train = 0
 Try to load weights: /var/AI/darknet/weights/yolov3-papersheet_600.weights
Done! Loaded 107 layers from weights-file

This issue happens with any output redirect, such as:

py script.py > log.py

I am using the following fork of the darknet library and I have opened an issue there as well, if you wanna give it a check. Although I think this might not be a library issue.

Any ideas?


Solution

The issue was caused by output buffering.

I solved by calling python with -u option:

nohup python3 -u myscript.py

See this question



Answered By - Constantin
Answer Checked By - Mary Flores (WPSolving Volunteer)