Convert DarkNet weights to .pb file
Create frozen graph from DarkNet weights
DarkFlow is a very useful and handy tool to train YOLO implemented with TensorFlow. By finishing the training it is possible to write out the graph and its weights into a protobuff file, called frozen graph. Later this frozen graph can be read and executed by the TensorFlow framework. Unfortunately, beside the advantages the DarkFlow has many disatvantages too, for example sometimes it is lately adapted for the YOLO architecture changes and for this reason different incompatibility issues can appear. Probably, you are already familiar with the assertion error message, when you try to retrain your model: "AssertionError: expect 64701556 bytes, found 180357512". A possible solution is to train your model with the DarkNet which is the reference implementation of this neural network framework, then convert the weights by using DarkFlow. The Google Colaboratory tutorial is available here.
In this article we present you a possible solution to convert the DarkNet weights to .pb file. At this point we suppose that you are already familiar with these two frameworks. If you aren't please read the following two articles:
At the end of the "Train YOLO with DarkNet" tutorial we obtained the trained weights (it is accessible here). We will use this file to convert it to .pb format.
Download this file or use your own and place it into a sub-directory like ./weights, in the DarkFlow root directory.
Here we will use the same configuration file what we used in the previous tutorial. You can get it by using the wget application:
- $ wget https://oc.codespring.ro/s/KcMNWpdnsEzkrtL/download - rename it properly, and place it into the ./cfg directory.
The command to convert it to .pb format is:
- $ ./flow --model cfg/yolov2-tiny-voc.cfg --load weights/yolov2-tiny-voc_10000.weights --savepb
When you execute this command the following outputs appear:
Parsing cfg/yolov2-tiny-voc.cfg
Loading weights/yolov2-tiny-voc_10000.weights ...
Traceback (most recent call last):
File "./flow", line 6, in <module>
cliHandler(sys.argv)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/cli.py", line 26, in cliHandler
tfnet = TFNet(FLAGS)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/net/build.py", line 58, in __init__
darknet = Darknet(FLAGS)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/dark/darknet.py", line 27, in __init__
self.load_weights()
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/dark/darknet.py", line 82, in load_weights
wgts_loader = loader.create_loader(*args)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/utils/loader.py", line 105, in create_loader
return load_type(path, cfg)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/utils/loader.py", line 19, in __init__
self.load(*args)
File "/home/ubbcluj/Documents/Egyetem/FinalExam/Darknet/Darknet/darkflow/darkflow/utils/loader.py", line 77, in load
walker.offset, walker.size)
AssertionError: expect 63102554 bytes, found 63102560
It is already familiar to us. Isn't it?
The solution is very simple, let's modify the line self.offset = 16 in the ./darkflow/utils/loader.py file and replace with self.offset = 20. Execute again the previous command. After running the command two files will appear in the ./built_graph directory:
- yolov2-tiny-voc.meta;
- yolov2-tiny-voc.pb;
The converted graph executed with the tensorflow java example application.
At this point you are ready to use the newly trained graph, for example you can try it out by using our TensorFlow Java API Spring service tutorial. Just copy the graph and the labels file into the right place and follow the instructions described in the article.