- Notifications
You must be signed in to change notification settings - Fork2.1k
Translate darknet to tensorflow. Load trained weights, retrain/fine-tune using tensorflow, export constant graph def to mobile devices
License
thtrieu/darkflow
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Real-time object detection and classification. Paper:version 1,version 2.
Read more about YOLO (in darknet) and download weight fileshere. In case the weight file cannot be found, I uploaded some of minehere, which includeyolo-full andyolo-tiny of v1.0,tiny-yolo-v1.1 of v1.1 andyolo,tiny-yolo-voc of v2.
See demo below or see onthis imgur
Python3, tensorflow 1.0, numpy, opencv 3.
@article{trieu2018darkflow, title={Darkflow}, author={Trieu, Trinh Hoang}, journal={GitHub Repository. Available online: https://github. com/thtrieu/darkflow (accessed on 14 February 2019)}, year={2018}}You can chooseone of the following three ways to get started with darkflow.
Just build the Cython extensions in place. NOTE: If installing this way you will have to use
./flowin the cloned darkflow directory instead offlowas darkflow is not installed globally.python3 setup.py build_ext --inplaceLet pip install darkflow globally in dev mode (still globally accessible, but changes to the code immediately take effect)
pip install -e .Install with pip globally
pip install .
Android demo on Tensorflow'shere
I am looking for help:
help wantedlabels in issue track
Skip this if you are not training or fine-tuning anything (you simply want to forward flow a trained net)
For example, if you want to work with only 3 classestvmonitor,person,pottedplant; editlabels.txt as follows
tvmonitorpersonpottedplantAnd that's it.darkflow will take care of the rest. You can also set darkflow to load from a custom labels file with the--labels flag (i.e.--labels myOtherLabelsFile.txt). This can be helpful when working with multiple models with different sets of output labels. When this flag is not set, darkflow will load fromlabels.txt by default (unless you are using one of the recognized.cfg files designed for the COCO or VOC dataset - then the labels file will be ignored and the COCO or VOC labels will be loaded).
Skip this if you are working with one of the original configurations since they are already there. Otherwise, see the following example:
...[convolutional]batch_normalize=1size=3stride=1pad=1activation=leaky[maxpool][connected]output=4096activation=linear...
# Have a look at its optionsflow --hFirst, let's take a closer look at one of a very useful option--load
# 1. Load tiny-yolo.weightsflow --model cfg/tiny-yolo.cfg --load bin/tiny-yolo.weights# 2. To completely initialize a model, leave the --load optionflow --model cfg/yolo-new.cfg# 3. It is useful to reuse the first identical layers of tiny for `yolo-new`flow --model cfg/yolo-new.cfg --load bin/tiny-yolo.weights# this will print out which layers are reused, which are initialized
All input images from default foldersample_img/ are flowed through the net and predictions are put insample_img/out/. We can always specify more parameters for such forward passes, such as detection threshold, batch size, images folder, etc.
# Forward all images in sample_img/ using tiny yolo and 100% GPU usageflow --imgdir sample_img/ --model cfg/tiny-yolo.cfg --load bin/tiny-yolo.weights --gpu 1.0json output can be generated with descriptions of the pixel location of each bounding box and the pixel location. Each prediction is stored in thesample_img/out folder by default. An example json array is shown below.
# Forward all images in sample_img/ using tiny yolo and JSON output.flow --imgdir sample_img/ --model cfg/tiny-yolo.cfg --load bin/tiny-yolo.weights --jsonJSON output:
[{"label":"person","confidence":0.56,"topleft": {"x":184,"y":101},"bottomright": {"x":274,"y":382}},{"label":"dog","confidence":0.32,"topleft": {"x":71,"y":263},"bottomright": {"x":193,"y":353}},{"label":"horse","confidence":0.76,"topleft": {"x":412,"y":109},"bottomright": {"x":592,"y":337}}]- label: self explanatory
- confidence: somewhere between 0 and 1 (how confident yolo is about that detection)
- topleft: pixel coordinate of top left corner of box.
- bottomright: pixel coordinate of bottom right corner of box.
Training is simple as you only have to add option--train. Training set and annotation will be parsed if this is the first time a new configuration is trained. To point to training set and annotations, use option--dataset and--annotation. A few examples:
# Initialize yolo-new from yolo-tiny, then train the net on 100% GPU:flow --model cfg/yolo-new.cfg --load bin/tiny-yolo.weights --train --gpu 1.0# Completely initialize yolo-new and train it with ADAM optimizerflow --model cfg/yolo-new.cfg --train --trainer adam
During training, the script will occasionally save intermediate results into Tensorflow checkpoints, stored inckpt/. To resume to any checkpoint before performing training/testing, use--load [checkpoint_num] option, ifcheckpoint_num < 0,darkflow will load the most recent save by parsingckpt/checkpoint.
# Resume the most recent checkpoint for trainingflow --train --model cfg/yolo-new.cfg --load -1# Test with checkpoint at step 1500flow --model cfg/yolo-new.cfg --load 1500# Fine tuning yolo-tiny from the original oneflow --train --model cfg/tiny-yolo.cfg --load bin/tiny-yolo.weights
Example of training on Pascal VOC 2007:
# Download the Pascal VOC dataset:curl -O https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tartar xf VOCtest_06-Nov-2007.tar# An example of the Pascal VOC annotation format:vim VOCdevkit/VOC2007/Annotations/000001.xml# Train the net on the Pascal dataset:flow --model cfg/yolo-new.cfg --train --dataset"~/VOCdevkit/VOC2007/JPEGImages" --annotation"~/VOCdevkit/VOC2007/Annotations"
The steps below assume we want to use tiny YOLO and our dataset has 3 classes
Create a copy of the configuration file
tiny-yolo-voc.cfgand rename it according to your preferencetiny-yolo-voc-3c.cfg(It is crucial that you leave the originaltiny-yolo-voc.cfgfile unchanged, see below for explanation).In
tiny-yolo-voc-3c.cfg, change classes in the [region] layer (the last layer) to the number of classes you are going to train for. In our case, classes are set to 3....[region]anchors=1.08,1.19,3.42,4.41,6.63,11.38,9.42,5.11,16.62,10.52bias_match=1classes=3coords=4num=5softmax=1...
In
tiny-yolo-voc-3c.cfg, change filters in the [convolutional] layer (the second to last layer) to num * (classes + 5). In our case, num is 5 and classes are 3 so 5 * (3 + 5) = 40 therefore filters are set to 40....[convolutional]size=1stride=1pad=1filters=40activation=linear[region]anchors=1.08,1.19,3.42,4.41,6.63,11.38,9.42,5.11,16.62,10.52...
Change
labels.txtto include the label(s) you want to train on (number of labels should be the same as the number of classes you set intiny-yolo-voc-3c.cfgfile). In our case,labels.txtwill contain 3 labels.label1label2label3Reference the
tiny-yolo-voc-3c.cfgmodel when you train.flow --model cfg/tiny-yolo-voc-3c.cfg --load bin/tiny-yolo-voc.weights --train --annotation train/Annotations --dataset train/Images
Why should I leave the original
tiny-yolo-voc.cfgfile unchanged?When darkflow sees you are loading
tiny-yolo-voc.weightsit will look fortiny-yolo-voc.cfgin your cfg/ folder and compare that configuration file to the new one you have set with--model cfg/tiny-yolo-voc-3c.cfg. In this case, every layer will have the same exact number of weights except for the last two, so it will load the weights into all layers up to the last two because they now contain different number of weights.
For a demo that entirely runs on the CPU:
flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi
For a demo that runs 100% on the GPU:
flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi --gpu 1.0
To use your webcam/camera, simply replacevideofile.avi with keywordcamera.
To save a video with predicted bounding box, add--saveVideo option.
Please note thatreturn_predict(img) must take annumpy.ndarray. Your image must be loaded beforehand and passed toreturn_predict(img). Passing the file path won't work.
Result fromreturn_predict(img) will be a list of dictionaries representing each detected object's values in the same format as the JSON output listed above.
fromdarkflow.net.buildimportTFNetimportcv2options= {"model":"cfg/yolo.cfg","load":"bin/yolo.weights","threshold":0.1}tfnet=TFNet(options)imgcv=cv2.imread("./sample_img/sample_dog.jpg")result=tfnet.return_predict(imgcv)print(result)
## Saving the lastest checkpoint to protobuf fileflow --model cfg/yolo-new.cfg --load -1 --savepb## Saving graph and weights to protobuf fileflow --model cfg/yolo.cfg --load bin/yolo.weights --savepb
When saving the.pb file, a.meta file will also be generated alongside it. This.meta file is a JSON dump of everything in themeta dictionary that contains information nessecary for post-processing such asanchors andlabels. This way, everything you need to make predictions from the graph and do post processing is contained in those two files - no need to have the.cfg or any labels file tagging along.
The created.pb file can be used to migrate the graph to mobile devices (JAVA / C++ / Objective-C++). The name of input tensor and output tensor are respectively'input' and'output'. For further usage of this protobuf file, please refer to the official documentation ofTensorflow on C++ APIhere. To run it on, say, iOS application, simply add the file to Bundle Resources and update the path to this file inside source code.
Also, darkflow supports loading from a.pb and.meta file for generating predictions (instead of loading from a.cfg and checkpoint or.weights).
## Forward images in sample_img for predictions based on protobuf fileflow --pbLoad built_graph/yolo.pb --metaLoad built_graph/yolo.meta --imgdir sample_img/If you'd like to load a.pb and.meta file when usingreturn_predict() you can set the"pbLoad" and"metaLoad" options in place of the"model" and"load" options you would normally set.
That's all.
About
Translate darknet to tensorflow. Load trained weights, retrain/fine-tune using tensorflow, export constant graph def to mobile devices
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
