• Stars
    star
    1,687
  • Rank 27,667 (Top 0.6 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is the code for "YOLO Object Detection" by Siraj Raval on Youtube

Intro

This is the code for this video on Youtube by Siraj Raval

Build Status codecov

Real-time object detection and classification. Paper: version 1, version 2.

Read more about YOLO (in darknet) and download weight files here. In case the weight file cannot be found, I uploaded some of mine here, which include yolo-full and yolo-tiny of v1.0, tiny-yolo-v1.1 of v1.1 and yolo, tiny-yolo-voc of v2.

Click on this image to see demo from yolov2:

img

Dependencies

Python3, tensorflow 1.0, numpy, opencv 3.

Getting started

You can choose one of the following three ways to get started with darkflow.

  1. Just build the Cython extensions in place. NOTE: If installing this way you will have to use ./flow in the cloned darkflow directory instead of flow as darkflow is not installed globally.

    python3 setup.py build_ext --inplace
    
  2. Let pip install darkflow globally in dev mode (still globally accessible, but changes to the code immediately take effect)

    pip install -e .
    
  3. Install with pip globally

    pip install .
    

Update

Android demo on Tensorflow's here

I am looking for help:

  • help wanted labels in issue track

Parsing the annotations

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 classes tvmonitor, person, pottedplant; edit labels.txt as follows

tvmonitor
person
pottedplant

And 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 from labels.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).

Design the net

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 = 1
size = 3
stride = 1
pad = 1
activation = leaky

[maxpool]

[connected]
output = 4096
activation = linear

...

Flowing the graph using flow

# Have a look at its options
flow --h

First, let's take a closer look at one of a very useful option --load

# 1. Load yolo-tiny.weights
flow --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights

# 2. To completely initialize a model, leave the --load option
flow --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/yolo-tiny.weights
# this will print out which layers are reused, which are initialized

All input images from default folder sample_img/ are flowed through the net and predictions are put in sample_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 usage
flow --imgdir sample_img/ --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights --gpu 1.0

json output can be generated with descriptions of the pixel location of each bounding box and the pixel location. Each prediction is stored in the sample_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/yolo-tiny.cfg --load bin/yolo-tiny.weights --json

JSON 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 new model

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/yolo-tiny.weights --train --gpu 1.0

# Completely initialize yolo-new and train it with ADAM optimizer
flow --model cfg/yolo-new.cfg --train --trainer adam

During training, the script will occasionally save intermediate results into Tensorflow checkpoints, stored in ckpt/. To resume to any checkpoint before performing training/testing, use --load [checkpoint_num] option, if checkpoint_num < 0, darkflow will load the most recent save by parsing ckpt/checkpoint.

# Resume the most recent checkpoint for training
flow --train --model cfg/yolo-new.cfg --load -1

# Test with checkpoint at step 1500
flow --model cfg/yolo-new.cfg --load 1500

# Fine tuning yolo-tiny from the original one
flow --train --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights

Example of training on Pascal VOC 2007:

# Download the Pascal VOC dataset:
curl -O https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
tar 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"

Training on your own dataset

The steps below assume we want to use tiny YOLO and our dataset has 3 classes

  1. Create a copy of the configuration file tiny-yolo-voc.cfg and rename it according to your preference tiny-yolo-voc-3c.cfg (It is crucial that you leave the original tiny-yolo-voc.cfg file unchanged, see below for explanation).

  2. 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.52
    bias_match=1
    classes=3
    coords=4
    num=5
    softmax=1
    
    ...
  3. 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=1
    stride=1
    pad=1
    filters=40
    activation=linear
    
    [region]
    anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
    
    ...
  4. Change labels.txt to include the label(s) you want to train on (number of labels should be the same as the number of classes you set in tiny-yolo-voc-3c.cfg file). In our case, labels.txt will contain 3 labels.

    label1
    label2
    label3
    
  5. Reference the tiny-yolo-voc-3c.cfg model 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.cfg file unchanged?

    When darkflow sees you are loading tiny-yolo-voc.weights it will look for tiny-yolo-voc.cfg in 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.

Camera/video file demo

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 replace videofile.avi with keyword camera.

To save a video with predicted bounding box, add --saveVideo option.

Using darkflow from another python application

Please note that return_predict(img) must take an numpy.ndarray. Your image must be loaded beforehand and passed to return_predict(img). Passing the file path won't work.

Result from return_predict(img) will be a list of dictionaries representing each detected object's values in the same format as the JSON output listed above.

from darkflow.net.build import TFNet
import cv2

options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.1}

tfnet = TFNet(options)

imgcv = cv2.imread("./sample_img/dog.jpg")
result = tfnet.return_predict(imgcv)
print(result)

Save the built graph to a protobuf file (.pb)

## Saving the lastest checkpoint to protobuf file
flow --model cfg/yolo-new.cfg --load -1 --savepb

## Saving graph and weights to protobuf file
flow --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 the meta dictionary that contains information nessecary for post-processing such as anchors and labels. 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 of Tensorflow on C++ API here. 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 file
flow --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 using return_predict() you can set the "pbLoad" and "metaLoad" options in place of the "model" and "load" options you would normally set.

That's all.

Credits

Credits for this code go to https://github.com/thtrieu. I've merely created a wrapper to get people started.

More Repositories

1

Learn_Machine_Learning_in_3_Months

This is the code for "Learn Machine Learning in 3 Months" by Siraj Raval on Youtube
7,557
star
2

learn_math_fast

This is the Curriculum for "How to Learn Mathematics Fast" By Siraj Raval on Youtube
Python
3,183
star
3

Learn_Data_Science_in_3_Months

This is the Curriculum for "Learn Data Science in 3 Months" By Siraj Raval on Youtube
2,701
star
4

Learn_Deep_Learning_in_6_Weeks

This is the Curriculum for "Learn Deep Learning in 6 Weeks" by Siraj Raval on Youtube
2,680
star
5

Learn_Computer_Science_in_5_Months

This is the Curriculum for "Learn Computer Science in 5 Months" By Siraj Raval on Youtube
1,856
star
6

Learn_Blockchain_in_2_months

This is the code for "Learn Blockchain in 2 Months" by Siraj Raval on Youtube
1,725
star
7

tensorflow_chatbot

Tensorflow chatbot demo by @Sirajology on Youtube
Python
1,422
star
8

Learn_Computer_Vision

This is the curriculum for "Learn Computer Vision" by Siraj Raval on Youtube
1,078
star
9

Learn-Natural-Language-Processing-Curriculum

This is the curriculum for "Learn Natural Language Processing" by Siraj Raval on Youtube
1,054
star
10

Machine_Learning_Journey

This is the Curriculum for "Machine Learning Journey" By Siraj Raval on Youtube
973
star
11

deepfakes

This is the code for "DeepFakes" by Siraj Raval on Youtube
Python
946
star
12

100_Days_of_ML_Code

These are the instructions for "100 Days of ML Code" By Siraj Raval on Youtube
865
star
13

How-to-Predict-Stock-Prices-Easily-Demo

How to Predict Stock Prices Easily - Intro to Deep Learning #7 by Siraj Raval on Youtube
Jupyter Notebook
764
star
14

Programming_Interview_Study_Plan

This is the Programming Interview Study Plan by Siraj Raval on Youtube
689
star
15

Reinforcement_Learning_for_Stock_Prediction

This is the code for "Reinforcement Learning for Stock Prediction" By Siraj Raval on Youtube
Python
610
star
16

ChatGPT_Trading_Bot

This is the code for the "ChatGPT Trading Bot" Video by Siraj Raval on Youtube
Jupyter Notebook
587
star
17

capsule_networks

This is the code for "Capsule Networks: An Improvement to Convolutional Networks" by Siraj Raval on Youtube
Python
569
star
18

tensorflow_image_classifier

TensorFlow Image Classifier Demo by @Sirajology on Youtube
Python
530
star
19

Make_Money_with_Tensorflow_2.0

This is the code for "Make Money with Tensorflow 2.0" by Siraj Raval
Jupyter Notebook
527
star
20

LearnML

This is the Study Guide for Learn Machine Learning in 3 Months (PyTorch Curriculum) by Siraj Raval on Youtube
520
star
21

Mathematics_for_Beginners

This is the formula sheet for "Mathematics for Beginners" by Siraj Raval on Youtube
484
star
22

How_to_make_a_text_summarizer

This is the code for "How to Make a Text Summarizer - Intro to Deep Learning #10" by Siraj Raval on Youtube
Jupyter Notebook
481
star
23

Pokemon_GAN

This is the code for "Generating Pokemon with a Generative Adversarial Network" by Siraj Raval on Youtube
Python
432
star
24

Watch-Me-Build-a-Trading-Bot

This is the code for "Watch Me Build a Trading Bot" by Siraj Raval on Youtube
JavaScript
419
star
25

Learn_Physics_in_2_Months

This is the curriculum for "Learn Physics in 2 Months" by Siraj Raval on Youtube
414
star
26

How_to_simulate_a_self_driving_car

This is the code for "How to Simulate a Self-Driving Car" by Siraj Raval on Youtube
Python
413
star
27

Your_First_Decentralized_Application

This is the code for "A Guide to Building Your First Decentralized Application" by Siraj Raval on Youtube
Jupyter Notebook
413
star
28

AI_in_Finance

This is the code for "AI in Finance" By Siraj Raval on Youtube
JavaScript
410
star
29

The_Math_of_Intelligence

This is the Syllabus for Siraj Raval's new course "The Math of Intelligence"
394
star
30

tensorflow_speech_recognition_demo

This is the code for 'How to Make a Simple Tensorflow Speech Recognizer' by @Sirajology on Youtube
Python
383
star
31

Convolutional_neural_network

This is the code for "Convolutional Neural Networks - The Math of Intelligence (Week 4)" By Siraj Raval on Youtube
Jupyter Notebook
368
star
32

AI_Startup_Prototype

This is the code for "Watch Me Build an AI Startup" By Siraj Raval on Youtube
Python
364
star
33

Neural_Network_Voices

This is the code for "Neural Network Voices" by Siraj Raval on Youtube
Python
358
star
34

A_Guide_to_Running_Tensorflow_Models_on_Android

This is the code for"A Guide to Running Tensorflow Models on Android" By SIraj Raval on Youtube
Java
344
star
35

pose_estimation

This is the code for "Webcam Tracking with Tensorflow.js" By Siraj Raval on Youtube
TypeScript
318
star
36

Everybody_Dance_Now

This is the code for "Everybody Dance Now!" By Siraj Raval on Youtube
Python
313
star
37

Q-Learning-for-Trading

Python
311
star
38

ethereum_future

This is the Code for "Ethereum Future Prices" by Siraj Raval on Youtube
Jupyter Notebook
299
star
39

AI_Composer

AI Composer for Machine Learning for Hackers #2
Python
294
star
40

How-to-Use-GitHub

This is the supplementary material for "How to Use GitHub" By Siraj Raval on Youtube
294
star
41

how_to_deploy_a_keras_model_to_production

This is the code for the "How to Deploy a Keras Model to Production" by Siraj Raval on Youtube
Python
288
star
42

predicting_stock_prices

This is the coding challenge for "Predicting Stock Prices" by @Sirajology on Youtube
Python
282
star
43

How-to-Deploy-a-Tensorflow-Model-in-Production

This is the code for the "How to Deploy a Tensorflow Model in Production" by Siraj Raval on YouTube
Python
280
star
44

linear_regression_live

This is the code for the "How to Do Linear Regression the Right Way" live session by Siraj Raval on Youtube
Python
269
star
45

Chatbot-AI

Chatbot AI for Machine Learning for Hackers #6
Lua
261
star
46

Predicting_Winning_Teams

This is the code for "Predicting the Winning Team with Machine Learning" by Siraj Raval on Youtube
Jupyter Notebook
249
star
47

Music_Generator_Demo

Music Generator Demo by @Sirajology on Youtube
Python
246
star
48

Data_Science_Interview_Guide

These are the tips for "5 Steps to Pass Data Science Interviews" By Siraj Raval on Youtube
245
star
49

quantum_machine_learning

This is the code for "Quantum Machine Learning" By Siraj Raval on Youtube
HTML
245
star
50

AI_Freelancing

This is the code for "How to Do Freelance AI Programming" By Siraj Raval on Youtube
245
star
51

AI_Artist

AI Artist for Machine Learning for Hackers #5
Python
237
star
52

bitcoin_prediction

This is the code for "Bitcoin Prediction" by Siraj Raval on Youtube
Jupyter Notebook
234
star
53

tensorflow_demo

Tensorflow Demo for my TF in 5 Min Video on Youtube
Python
227
star
54

Neural_Differential_Equations

This is the code for "Neural DIfferential Equations" By Siraj Raval on Youtube
Jupyter Notebook
225
star
55

Stock_Market_Prediction

This is the code for "Stock Market Prediction" by Siraj Raval on Youtube
Jupyter Notebook
218
star
56

Build-an-AI-Startup-with-PyTorch

This is the code for 'Build an AI Startup with Pytorch" by Siraj Raval
Java
217
star
57

ChatGPT_Sports_Betting_Bot

This is the code for "I Built a Sports Betting Bot with ChatGPT" by Siraj Raval on Youtube
Jupyter Notebook
216
star
58

Move_37_Syllabus

This is the syllabus for "Move 37", Siraj Raval's new course at School of AI
215
star
59

Classifying_Data_Using_a_Support_Vector_Machine

This is the code for the "Classifying Data using Gradient Descent" by Siraj Raval on Youtube
Jupyter Notebook
212
star
60

A-Guide-to-DeepMinds-StarCraft-AI-Environment

This is the code for "A Guide to DeepMind's StarCraft AI Environment" by Siraj Raval on Youtube
Python
210
star
61

Landing-a-SpaceX-Falcon-Heavy-Rocket

This is the code for "Landing a SpaceX Falcon Heavy Rocket" By Siraj Raval on Youtube
Python
209
star
62

How_to_Build_a_healthcare_startup

This is the code for "How to Build a Healthcare Startup" by Siraj Raval on Youtube
Dart
203
star
63

AI_For_Music_Composition

This is the code for "AI for Music Composition" by Siraj Raval on Youtube
Python
202
star
64

How_to_make_a_chatbot

This is the code for "How to Make a Chatbot - Intro to Deep Learning #12' by Siraj Raval on YouTube
Python
199
star
65

LSTM_Networks

This is the code for "LSTM Networks - The Math of Intelligence (Week 8)" By Siraj Raval on Youtube
Jupyter Notebook
194
star
66

Make_a_neural_network

This is the code for the "Make a Neural Network" - Intro to Deep Learning #2 by Siraj Raval on Youtube
Python
193
star
67

AI_For_Business_Curriculum

This is the curriculum for the "AI for Business" Course By Siraj Raval on Youtube
189
star
68

Time_Series_Prediction

This is the code for "Time Series Prediction" By Siraj Raval on Youtube
Jupyter Notebook
187
star
69

3D_Pose_Estimation

This is the code for "Machine Vision" By Siraj Raval on Youtube
Python
185
star
70

Financial_Forecasting_with_TensorflowJS

This is the code for "Financial Forecasting with Tensorflow.js" By Siraj Raval on Youtube
JavaScript
184
star
71

linear_regression_demo

This is the code for "How to Make a Prediction - Intro to Deep Learning #1' by Siraj Raval on YouTube
Python
182
star
72

ethereum_demo

This is the code for "Ethereum Explained" by Siraj Raval on Youtube
Jupyter Notebook
178
star
73

chatbot_tutorial

This is the code for "Chatbot Tutorial" by Siraj Raval on Youtube
Python
176
star
74

Watch-Me-Build-a-Finance-Startup

This is the code for "Watch Me Build a Finance Startup" by Siraj Raval on Youtube
Java
172
star
75

word_vectors_game_of_thrones-LIVE

This is the code for the "How to Make Word Vectors from Game of Thrones (LIVE) " Siraj Raval on Youtube
Jupyter Notebook
170
star
76

AI_in_Medicine_Clinical_Imaging_Classification

This is the code for "AI in Medicine " By Siraj Raval on Youtube
Python
165
star
77

deep_q_learning

This is the Code for "Deep Q Learning - The Math of Intelligence #9" By Siraj Raval on Youtube
Jupyter Notebook
163
star
78

AI_Writer

AI Writer for Machine Learning for Hackers #8
Python
163
star
79

Bitcoin_Trading_Bot

This is the code for "Bitcoin Trading Bot" By Siraj Raval on Youtube
Jupyter Notebook
163
star
80

Unity_ML_Agents

This is the code for "Unity AI" by Siraj Raval on Youtube
Python
161
star
81

how_to_convert_text_to_images

This is the code for "How to Convert Text to Images - Intro to Deep Learning #16' by Siraj Raval on YouTube
Python
155
star
82

Game-AI

Game AI for Machine Learning for Hackers #3
Python
154
star
83

Intro_to_the_Math_of_intelligence

This is the code for "Intro - The Math of Intelligence" by Siraj Raval on Youtube
Python
152
star
84

Sentiment_Analysis

This is the code for "Sentiment Analysis - Data Lit #1" by Siraj Raval on Youtube
Jupyter Notebook
151
star
85

recommender_live

Jupyter Notebook
149
star
86

recurrent_neural_network

This is the code for "Recurrent Neural Networks - The Math of Intelligence (Week 5)" By Siraj Raval on Youtube
Jupyter Notebook
147
star
87

How-to-Learn-from-Little-Data

This is the code for "How to Learn from Little Data - Intro to Deep Learning #17' by Siraj Raval on YouTube
Python
144
star
88

How_to_generate_music_in_tensorflow_LIVE

Python
140
star
89

Kaggle_Earthquake_challenge

This is the code for the Kaggle Earthquake Challenge by Siraj Raval on Youtube
Jupyter Notebook
139
star
90

OpenAI_Five_vs_Dota2_Explained

This is the code for "OpenAI Five vs DOTA 2 Explained" By Siraj Raval on Youtube
Python
138
star
91

Learn_Synthetic_Biology

137
star
92

How-to-Build-a-Biomedical-Startup

This is the code for "How to Build a Biomedical Startup" by Siraj Raval on Youtube
Dart
134
star
93

Gaussian_Mixture_Models

This is the code for "Gaussian Mixture Models - The Math of Intelligence (Week 7)" By Siraj Raval on Youtube
Jupyter Notebook
134
star
94

Make_Money_with_Tensorflow

This is the code for "Make Money with Tensorflow" by Siraj Raval on Youtube
Python
129
star
95

Machine-Learning-API-Tutorial

This is the code for "Machine Learning API tutorial" By Siraj Raval on Youtube
Python
128
star
96

AI_Supply_Chain

This is the code for "AI for Supply Chain" by Siraj Raval on Youtube
Jupyter Notebook
128
star
97

how_to_build_a_bitcoin_startup

This is the code for "How to Build a Bitcoin Startup" by Siraj Raval on Youtube
JavaScript
126
star
98

machine_learning_and_neuroscience

This is the code for "Machine Learning & Neuroscience" By Siraj Raval on Youtube
JavaScript
123
star
99

k_means_clustering

This is the code for "K-Means Clustering - The Math of Intelligence (Week 3)" By SIraj Raval on Youtube
Jupyter Notebook
122
star
100

alphago_demo

This is the code for "How Does DeepMind's AlphaGo Zero Work?" Siraj Raval on Youtube
Python
120
star