Image Detection with YOLO-v2 (pt.8) Custom Object Detection (Train our Model!)

  • Published on Jan 13, 2018
  • In this series we will explore the capabilities of YOLO for image detection in python!
    This video will look at
    - how to modify our the tiny-yolo-voc.cfg model file
    - how to modify the labels.txt file
    - how to train our model
    - how to load our trained weights to test our model

    Github example webcam code:
    Darkflow Repo:
    YOLO weights:
    Discord Server:

Comments • 739

  • Pooja Singh
    Pooja Singh 7 days ago

    Thanks for the detailed explanation in previous videos. Could you please share what are the anchors coordinates in the model. Are they the priors? How one select those values? Thanks

  • arkan 6040
    arkan 6040 11 days ago

    how if we train the model more than one?

  • Devara madhu
    Devara madhu 25 days ago

    i did only for one class but me showing multiple classes eg: i did arduino object while on webcam it giving 5-6 type of color label and showing arduino label

  • harsh mehta
    harsh mehta 27 days ago

    this is a very good video sir
    sir is there any way i can add any beep sound when it detects the object ?

  • Bilal Ahmed
    Bilal Ahmed Month ago

    ValueError: operands could not be broadcast together with shapes (360,800,4) (3,)
    Need help here..

  • harsh mehta
    harsh mehta Month ago

    i have some images how can i run training on it

  • Himansh Mulchandani

    How do i train for multiple classes of objects? I need to train one for traffic sign detection?

  • Yasmine Guemouria
    Yasmine Guemouria 2 months ago

    Somebody please help me, I get this error :
    FileNotFoundError: [Errno 2] No such file or directory: 'cfg/tiny-yolo-voc-1c.cfg'

  • Ashu Gulati
    Ashu Gulati 2 months ago

    How can I test it with mobile camera?

  • Sonam Chaudhari
    Sonam Chaudhari 2 months ago

    Can please just tell me exactly where is your new_model folder located?.......I am getting a bit confused because of that......also please tell me if i use labelimg tool to create bounding boxes how should i procc
    eed ?

  • Amit Kharel
    Amit Kharel 2 months ago

    Watch it's final result by me:

  • Mahesh m
    Mahesh m 2 months ago

    i have an error , bin/tiny-yolo-voc.weights not found

    I downloaded this from the YOLO website as shown but i'm confused as to where it should be placed in my PC. Please help.

  • Александр Седышев

    I got AssertionError: expect 63102556 bytes, found 63471556

    • Edyuto
      Edyuto 12 days ago

      edit "darkflow/utils/" line 121 to
      "self.offset = 16 + 63471556 - 63102556"

  • Agronomistapolo
    Agronomistapolo 2 months ago

    How can I use the final script only for images not for vide?

    • HAL 9000
      HAL 9000 2 months ago

      change: ret, frame = --> frame = cv2.imread('your_picture.jpg') + some modifications

  • Deonis
    Deonis 3 months ago

    nice video. How do I create weight file or use .pb file with meta file from darkflow? If I use pbLoad and metaLoad options in your opencv code nothing gets detects and result is empty list. And it does not look like there is any way to export pb to weight file. Any help appreciated.

  • Aditya Sharma
    Aditya Sharma 3 months ago

    i let the training go upto step 2750, still its not detecting the object even once also

    • Deonis
      Deonis 3 months ago

      I had to do about 150k of steps and around 1000 epochs for my images to get something reasonable.

  • Aditya Sharma
    Aditya Sharma 3 months ago

    hey mark, i am getting this error mentioned below, please help me out

    File "flow", line 6, in
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\", line 26, in cliHandler
    tfnet = TFNet(FLAGS)
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\net\", line 58, in __init__
    darknet = Darknet(FLAGS)
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\dark\", line 27, in __init__
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\dark\", line 82, in load_weights
    wgts_loader = loader.create_loader(*args)
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\utils\", line 105, in create_loader
    return load_type(path, cfg)
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\utils\", line 19, in __init__
    File "C:\Users\Aditya Sharma\PycharmProjects\untitled1\yolo_object_detection\darkflow-master\darkflow\utils\", line 77, in load
    walker.offset, walker.size)
    AssertionError: expect 63082056 bytes, found 63471556

  • Tarık Ünler
    Tarık Ünler 3 months ago

    Hello thanks for this helpful video but i have 2 error
    first one is no module named 'darkflow.python utils.cy_yolo_findboxes'
    and second is no module named 'tensorflow.contrib'
    could you please help . using Python 3.6.1
    tanks regards.

  • Mohammad Zulqurnain
    Mohammad Zulqurnain 3 months ago

    Hi Mark, nice tutorial. I want to train a YOLO neural network on macbook pro. Can you tell if whether I can train it via my current macbook pro 2017 which has radeon 555 or I need the older macbook with 750M geforce if I want to use GPU for faster training?

  • 楊少溥
    楊少溥 3 months ago

    Thank you three thousand

  • Daniel LaMontagne
    Daniel LaMontagne 4 months ago +2

    Hi Mark Jay

    When I type this flow --model cfg/yolov2-tiny.cfg --load bin/yolov2-tiny.weights --train --annotation new_model_data/annotations_dm --dataset new_model_data/images_dm --gpu 1.0 --epoch 300

    I get this error AssertionError: Over-read bin/yolov2-tiny.weights

    When I type this daniellamontagne$ python flow --model cfg/tiny-yolo-voc-1c.cfg --load bin/yolov2-tiny-voc.weights --train --annotation new_model_data/annotations_dm --dataset new_model_data/images_dm --gpu 1.0 --epoch 300

    I get this error AssertionError: expect 63082060 bytes, found 63471556

    I've been working on this for so long...not sure what I'm doing wrong... =(

    • Brent Labasan
      Brent Labasan 4 months ago

      I've been getting this error too. I'm starting to investigate now.

  • BadCode official
    BadCode official 4 months ago

    How do i check the accuracy of the model using this method? U haven't divided your data into tranning and test set

  • Shivam Singh
    Shivam Singh 4 months ago

    my loss value is not decreasing. It rather fluctuates about around 150. What should I do to make it decrease?

  • Bei Zhou
    Bei Zhou 4 months ago

    Is this custom model only able to detect fidget spinner?

  • Yanzee Nongsiej
    Yanzee Nongsiej 4 months ago

    Greetings Mark! I have a question: What is the purpose of weights file? P.S: I would like the answer to be more theory-oriented.Thank You :)

    EK MIN WONG 4 months ago

    Hello Jay,

    Can I train a custom model for face recognition instead of object detection?
    Which mean I want to train my face as the training data, later on, will appear my name with the webcam, other people will display person. Thank you.

    SATYAM SAREEN 4 months ago

    I am getting an error when I am importing darkflow:

    "from import TFnet
    ModuleNotFoundError: No module named 'darkflow' "
    I have downloaded the repository, run the setup file, I have tensorflow , so why is this error.
    Please tell

    • Atharva Choudhary
      Atharva Choudhary 4 months ago

      Did you install it globally or in a virtual environment? Also, which IDE are you using ?

  • Wahyu Hidayat
    Wahyu Hidayat 4 months ago

    i always got division by zero even my images path was correct

  • oornicha Sinthop
    oornicha Sinthop 5 months ago

    Thank you very much .
    Would you pleas tell and demo about object detection with point cloud data

  • nitish hs
    nitish hs 5 months ago

    is training possible in windows ?

    ABHISHEK CHAUDHURI 5 months ago

    Hi i am getting an empty array in the result. can someone please let me know where i am going wrong. @markjay

    • Just Cats
      Just Cats 4 months ago

      ABHISHEK CHAUDHURI did u fimd the solution?

  • apoorve kalot
    apoorve kalot 5 months ago

    hey bro, i was detecting mobile charger by yolo and had used 125 images of it, i don't know why but it always creaing box much farther away from object eant to be detected, and always include (0,0) coordinate while making box, can you suggest where i may be getting error

    AJAY RAJ SINGH 6 months ago

    what is flow here i got an error that there is no such file or directory

  • Aadit Narendar
    Aadit Narendar 6 months ago

    I get an error while loading the weights which says, AssertionError: expect (a 9 digit value) bytes, found (another 9 digit value) bytes.
    I did edit the .cfg as per my detection and also the labels.
    This occurs when I run, after it parses the yolo.cfg file and exactly when it loads the .weights file.

    • Armin Moghadam
      Armin Moghadam 5 months ago

      Do not use "python" in your command line. Start with ./flow and the rest of the command

  • Archie Jack
    Archie Jack 6 months ago

    I trained on my dataset(840 images of 3 class objects, bottle/ mouse/ pen), modify model tiny-yolo-3c.cfg , batch 8, use the default lr, trainer adam, gpu model(GTX 950M 2GB), about 10k steps(about 300 epoches more than 10 hours), loss about 3 or 2, move ave loss the same, but the bounding box is not correct on train image( threshold is 0.0045), they are so many boxes or no boxes on high threshold, how to solve it?

  • duy trần
    duy trần 6 months ago

    thanks so much, but i have one question. If i trained object and want to add new class to tiny-yolo-voc-1c.cfg. How can i do???

  • Just Cats
    Just Cats 7 months ago

    im trying mine and working but to many boxes while detecting object i change the range from 10 to 1 but the box stuck on the left top conner, anyone help

    • Just Cats
      Just Cats 6 months ago

      @Ananay Gupta thank you for the reply! 😊 I'll give it a try and im running it fully on the cpu as my laptop doesn't have support gpu. I have only train 40 images. It might be the cause why

    • Ananay Gupta
      Ananay Gupta 6 months ago +1

      The range only changes the number of colours that your boxes will have, not the actual boxes themselves. If you want to change the number of boxes, then increase the threshold. If that does not work, then I don't see any reason to be worried, it means that your model can detect many objects with a great accuracy :)

  • A way to Brain
    A way to Brain 7 months ago

    sir can i get your facebook link or any other source of communication >>> i want to discuss my project with you , given to me in the university.

  • saif hamza
    saif hamza 7 months ago

    Hi Mark!

    with this model, I just want it to detect all the labels already present and including that, count the number of persons present in the frame. Can We do it? If yes, how?
    Need help, should present it coming week.

    • saif hamza
      saif hamza 6 months ago

      @Ananay Gupta thanks a Lot :)

    • saif hamza
      saif hamza 6 months ago

      @Ananay Gupta Hi!
      Thanks for your reply.
      But I think with this code, it will count all the persons it detects and keeps incrementing. I only need it to count the person present in the frame.

    • Ananay Gupta
      Ananay Gupta 6 months ago

      Best of luck for that presentation!

    • Ananay Gupta
      Ananay Gupta 6 months ago

      Yes, this is infact very simple. In the code, when you iterate through the for loop, you should check if label == 'person', and if it is, increment a counter variable. Then print this variable at the end of the while loop, and reset it. For more help, you could drop another comment

  • Muhamad Arif Lutfi
    Muhamad Arif Lutfi 7 months ago

    I have followed your instruction, and it is very clear and I rarely stuck anywhere. Except on this training part. I stuck when I test, there is no bounding box, and no results.
    I have train my model with only 2 classes. I also edited the number of classes in CFG. I also changed the filter to 35 since (5x(5+2 classes)) on the last convolutional laye, in CFG file.
    I also change the labels.txt into my own labels(Real, Fake)
    I only have 94 datasets (images, .png), and I set the epoch to 1700. The moving average lost came down from 109 to 0.007 (after 4 or 5 hours of training). But when I tried to do some test, JSON output becomes NULL. Or [ ]
    I also tried on the webcam, instead of showing FPS, I make it print out the results.
    When I use the pretrained model, I got a result of :
    [{'label': 'truck', 'confidence': 0.62592417, 'topleft': {'x': 80, 'y': 32}, 'bottomright': {'x': 998, 'y': 400}}]
    But when I use my model, it became :
    [ ]
    I also test it on my training and testing images, but still no result.
    Did I do something wrong somewhere?
    EDIT : After 4 months, I finally solved it. I got bounding box. See my comments (under replies) below to see what I did. I am not sure if it helps anyone, but I promised to share once I solved it. Good luck~

    • Muhamad Arif Lutfi
      Muhamad Arif Lutfi 2 months ago

      @Iryna Shevchenko
      Hey, it's been a while but I finally did it a few weeks ago. I get about 70%-90% detection or confidence rate (occasionally, but not always since there are still times where it won't detect at all). I changed how I approach about this. I am not sure if it helps you, but I did promised to share once I managed to solve my problem.
      First of all, I changed and increased my datasets. Since YOLO use class probability map and combine with the bounding box confidence, it just occurred to me to use a better and clearer datasets (to avoid noises). So I created my own datasets that I took from my own web camera and phone.
      I took about 5,000 images and annotate them all (one by one, it takes a few days). Then, I divided them into 10 parts, with each parts containing around 500 images.
      So I trained my model with the first part (only 500 images) and let the average loss reduced to the smallest amount possible. Then I increased the learning rate a bit (from 1x10-6 to 2x10-6, then end at 1x10-5), then let it train again to make sure it is the average loss is the smallest amount it can reached. The number of epochs really depends on how many datasets are there.
      Then, I continued the training with the second part of the datasets (another 500 images). The average loss will suddenly increase a bit (maybe from 0.xx to 3.xx or 2.xx or just 1.xx, but it will increase a little bit). Trained it until I get the smallest amount possible for the average loss.
      I repeated these steps until I finished training all 5,000 images. When you increase the learning rate, make sure it is not too far of a jump and sometimes it will increase for a while, before it started to decrease again. But if it keeps on increasing after a very long period, then you know that it has diverged and you need to reduce the learning rate again. So make sure you documented all the necessary checkpoint (so that you know which is good and which is isn't)
      The command that I used are on Windows Powershell are:
      (To continue training from the latest checkpoint available)
      python flow --model cfg/tiny-yolo-voc-myC.cfg --load -1 --train --annotation training/annotations --dataset training/cube_data --gpu 0.75 --epoch 1000
      (To continue training from the checkpoint I wanted, which in this case, checkpoint 15300)
      python flow --model cfg/tiny-yolo-voc-myC.cfg --load 15300 --train --annotation training/annotations --dataset training/cube_data --gpu 0.75 --epoch 1000

      (To increase the learning rate to 1x10-5, and train from the latest checkpoint)
      python flow --model cfg/tiny-yolo-voc-myC.cfg --load -1 --train --annotation training/annotations --dataset training/cube_data --gpu 0.75 --epoch 1000 --lr 0.00001
      (To increase the learning to 1x10-5, and train from the checkpoint that I wanted, which in this case 20425)
      python flow --model cfg/tiny-yolo-voc-myC.cfg --load 20425 --train --annotation training/annotations --dataset training/cube_data --gpu 0.75 --epoch 1000 --lr 0.00001
      lr = learning rate (I only increased it from 1x10-6 to 2x10-6 and never go beyond 1x10-5)
      load = loading the checkpoint or the weights
      Note : When you try your webcam inferencing, try to reduce the threshold to 0.05 just to test it first, to see if there is any bounding box available even at that low of a thresold. Increase the threshold as your train your model further. Right now, I increased my threshold bit by bit just to see the improvements when I tested it as I trained my model further and set my threshold highest at 0.6 and never more.
      Hope I helped~ Good luck~

    • Iryna Shevchenko
      Iryna Shevchenko 7 months ago +1

      @Muhamad Arif Lutfi many thx. im too still searching the solution. Of course if I'll find, I will share it

    • Muhamad Arif Lutfi
      Muhamad Arif Lutfi 7 months ago

      @Iryna Shevchenko I'm still searching for the right solution, and still haven't found yet.
      If somehow I managed to solve the problem in the future, I'll share it in this comment.

    • Iryna Shevchenko
      Iryna Shevchenko 7 months ago +1

      Hi Muhamad Arif Lutfi. I have the same problem: no bounding box. My dataset 1215 images, loss - from 115 to 5.4 (after 3 days training) and 500 epoch. I still have not solved this 😢

  • TrailerPlots
    TrailerPlots 7 months ago +1

    i get an error in cmd saying.....'NoneType' object has no attribute 'shape'

    • Ananay Gupta
      Ananay Gupta 6 months ago

      It seems to me that somewhere a part of you code has a variable null. Can you give any more details?

  • Ben M
    Ben M 7 months ago

    Hi Mark Jay, great tutorials, they have helped me a lot! I am interested in performing transfer learning on just the last couple of layers of the CNN. I've tried messing around in the .cfg file but I can't seem to find how to turn training from "Yep!" to "No" in the model.

    Do you know how to do this?

    Thanks in advance, Ben

  • bob BUILDER
    bob BUILDER 7 months ago

    hi mark so i want to train my dataset i have downloaded the weight but there are some errors which u may know how to solve

    C:\Users\eee\Desktop\darkflow-master>python flow --model cfg/yolov2-tiny.cfg --load bin/yolov2-tiny.weights --train --annotation new_model_data/annotations --dataset new_model_data/images --gpu 0.7 --epoch 300

    Parsing ./cfg/yolov2-tiny.cfg
    Parsing cfg/yolov2-tiny.cfg
    Loading bin/yolov2-tiny.weights ...
    Traceback (most recent call last):
    File "flow", line 6, in
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\", line 26, in cliHandler
    tfnet = TFNet(FLAGS)
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\net\", line 58, in __init__
    darknet = Darknet(FLAGS)
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\dark\", line 27, in __init__
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\dark\", line 82, in load_weights
    wgts_loader = loader.create_loader(*args)
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\utils\", line 105, in create_loader
    return load_type(path, cfg)
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\utils\", line 19, in __init__
    File "C:\Users\eee\Desktop\darkflow-master\darkflow\utils\", line 77, in load
    walker.offset, walker.size)
    AssertionError: expect 44138056 bytes, found 44948600

    • bob BUILDER
      bob BUILDER 7 months ago

      i read online and it stated it might be due to the mismatching cfg and weights files. Can you please advise how can I fix this or get the correct files? thanks :)

  • Siddharth Joshi
    Siddharth Joshi 7 months ago

    Hey Mark Awesome Tutorial. So i trained the model with loss around 3 (It wasnt changing much). Then tried to test the model . I am getting multiple annotations all over the image with the label that i have trained for. Any Advice ? What might have went wrong. (I used Yolo not tiny yolo .) and for trainer i used adam optimizer.

    • Siddharth Joshi
      Siddharth Joshi 7 months ago

      an update. Allowed the code to run for more epochs even when it was hovering at 3. and guess what it worked.

  • Tutor alive
    Tutor alive 7 months ago

    What GPU have you used?

  • Fieuws
    Fieuws 7 months ago

    I have multiple predictions on the same object. Anyway I can drop all these? (putting a higher threshold doesn't help as my bad model doesn't have much detection with bigger percentages)

  • Anoop S
    Anoop S 7 months ago

    thank you so much ❤️
    how to have our weight detect only our trained object and not every objects it was previously detecting. like for example only fidget spinner, not other objects the weight was previously detecting.

  • Matheus Melo
    Matheus Melo 7 months ago

    I've been requested by a professor to implement a project similar to your Yolo tutorial( great btw ), but using C++, and, for that, I am having trouble getting instructios and info on it. Is there a way you could help me out?

  • orange222000
    orange222000 8 months ago

    Thank you for your tutorial
    In training i have big problem i have CUDA8.0/cuDNN5.1/tensorflow1.4.0/ gtx1060/

    2019-04-05 13:04:57.116335: E C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\35\tensorflow\stream_executor\cuda\]
    could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED

    i can't find the solution So, i'm looking forward your advice Thank you

    • 크림Kream
      크림Kream 8 months ago

      for me i have tensorflow 1.13.1, cuda 10.0 cudnn7.3.1 running them on anaconda (make sure u install tensorflow on anaconda using conda install tensorflow-gpu because it will pull a few more dependencies that made it work).

  • Nishad Islam
    Nishad Islam 9 months ago

    UserWarning: ./cfg/yolov2-tiny.cfg not found, use cfg/tiny-yolo-voc-1c.cfg instead
    'Over-read {}'.format(self.path)
    AssertionError: Over-read bin/yolov2-tiny.weights I found this error. How to solve this

  • Jason Chiang
    Jason Chiang 9 months ago

    Hi I am just wondering if you could do similar things here but with yolov3?

  • Don Fung
    Don Fung 9 months ago +1

    Hey Mark I'm curious to know if you know the theory/reason behind why the bounding box jitters when the object in the video is static? 15:50-16:00

  • Omkar Naik
    Omkar Naik 9 months ago

    Can I do this with CPU

    • 크림Kream
      크림Kream 8 months ago

      yes just don't add the -gpu part

  • Dhinza Aizawa
    Dhinza Aizawa 9 months ago

    Hi Mark, How to show yolo object detection output on browser not from from pop-up window? I want to make it can be accessed from different IP.. I thought about using flask but I have no idea to do that.. really need guidance.. thanks

  • D Dopfer
    D Dopfer 9 months ago

    Hi Mark, great tutorials. Can I or how can I transfer your codes to run on Linux or Mac? Thanks

  • Anil kumar
    Anil kumar 9 months ago

    Hi Mark, What if the Image size is big? like 1500*1300. Can we annotate on those ? or Should we resize them first and then annotate? If I resize I'm losing information, what is the right way to do? Thanks.

  • Bit Zer0
    Bit Zer0 9 months ago

    bro i am getting OSError: File ./ckpt/tiny-yolo-voc-10c-750.meta does not exist. Even though it exists there, is the file assuming different root folder even though its in main directory?

  • Iryna Shevchenko
    Iryna Shevchenko 9 months ago

    Hi Mark. Thank you very much for such awesome tutorials!!!! maybe you can help me. I trained 1 class but even after 37000 steps, i can't detect any boxes. Loss is about 4.6e-05. Dataset - 1217 images, epoch - 500. where is my mistake??? help me, please :)

  • Naveen Kumar
    Naveen Kumar 10 months ago

    hello mark I have got some error when i run the ,
    sre_constants.error: bad character range
    please help me in solving this issue

  • Samkit Shah
    Samkit Shah 10 months ago

    I have trained modified model tiny-yolo-voc.1c for 1 class but it is not detecting for the object taken which is "bottle".