[PYTHON] I tried running YOLO v3 on Google Colab

Introduction

This time, I tried running YOLO v3 in the environment of Google Colaboratory.

For instructions on setting up YOLO v3 on Google Colaboratory YOLO setup Please refer to the chapter.

What is YOLO v3

** YOLO ** is a ** real-time object detection system **. It's part of a neural network framework called Darknet. The word YOLO is an acronym for "You only look once". YOLO v3 is version 3 of YOLO, which is currently the latest version. For details, please refer to YOLO official page.

YOLO v3 on Google Colab

environment

The environment used this time is Google Colaboratory. Other versions are as follows.

import platform
import cv2

print("Python " + platform.python_version())
print("OpenCV " + cv2.__version__)
# Python 3.6.9
# OpenCV 4.1.2

Preparation

Import the library required to display the image.

import cv2
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib

YOLO setup

Now let's set up YOLO v3 on Google Colab. We will create a working directory and work in it. Note that this setup is not necessary after the first one (for that purpose, work under the working directory).

import os

os.mkdir(working_dir) # working_dir is the working directory
os.chdir(working_dir)

Clone darknet.

!git clone https://github.com/pjreddie/darknet

After cloning, move to the darknet directory and execute make.

os.chdir(working_dir + 'darknet')
!make

After make is finished, download the trained model (weight).

!wget https://pjreddie.com/media/files/yolov3.weights

This completes the setup of YOLO v3 on Google Colab.

Let's move YOLO

Now, let's move YOLO to detect the object. Use the sample image already prepared. The sample image is under darknet / data.

!./darknet detect cfg/yolov3.cfg yolov3.weights 'data/dog.jpg'

# layer     filters    size              input                output
#     0 conv     32  3 x 3 / 1   608 x 608 x   3   ->   608 x 608 x  32  0.639 BFLOPs
#     1 conv     64  3 x 3 / 2   608 x 608 x  32   ->   304 x 304 x  64  3.407 BFLOPs
#     2 conv     32  1 x 1 / 1   304 x 304 x  64   ->   304 x 304 x  32  0.379 BFLOPs
#     3 conv     64  3 x 3 / 1   304 x 304 x  32   ->   304 x 304 x  64  3.407 BFLOPs
#     4 res    1                 304 x 304 x  64   ->   304 x 304 x  64
#     5 conv    128  3 x 3 / 2   304 x 304 x  64   ->   152 x 152 x 128  3.407 BFLOPs
#     6 conv     64  1 x 1 / 1   152 x 152 x 128   ->   152 x 152 x  64  0.379 BFLOPs
#     7 conv    128  3 x 3 / 1   152 x 152 x  64   ->   152 x 152 x 128  3.407 BFLOPs
#     8 res    5                 152 x 152 x 128   ->   152 x 152 x 128
# .........
#    97 upsample            2x    38 x  38 x 128   ->    76 x  76 x 128
#    98 route  97 36
#    99 conv    128  1 x 1 / 1    76 x  76 x 384   ->    76 x  76 x 128  0.568 BFLOPs
#   100 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
#   101 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
#   102 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
#   103 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128  0.379 BFLOPs
#   104 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256  3.407 BFLOPs
#   105 conv    255  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 255  0.754 BFLOPs
#   106 yolo
# Loading weights from yolov3.weights...Done!
# data/dog.jpg: Predicted in 22.825540 seconds.
# dog: 100%
# truck: 92%
# bicycle: 99%

Object detection is complete. Let's display the image and check it. The image that depicts the result of object detection is darknet / predictions.jpg.

img_in = cv2.imread('data/dog.jpg')
img_out = cv2.imread('predictions.jpg')
plt.figure(figsize=[20,10])
plt.subplot(121);plt.imshow(img_in[:,:,::-1]);plt.axis('off')
plt.subplot(122);plt.imshow(img_out[:,:,::-1]);plt.axis('off')

image.png

"Dog", "bicycle" and "car" can be detected.

Let's check other images as well.

!./darknet detect cfg/yolov3.cfg yolov3.weights 'data/horses.jpg'
img_in = cv2.imread('data/horses.jpg')
img_out = cv2.imread('predictions.jpg')
plt.figure(figsize=[20,10])
plt.subplot(121);plt.imshow(img_in[:,:,::-1]);plt.axis('off')
plt.subplot(122);plt.imshow(img_out[:,:,::-1]);plt.axis('off')

image.png

!./darknet detect cfg/yolov3.cfg yolov3.weights 'data/person.jpg'
img_in = cv2.imread('data/person.jpg')
img_out = cv2.imread('predictions.jpg')
plt.figure(figsize=[20,10])
plt.subplot(121);plt.imshow(img_in[:,:,::-1]);plt.axis('off')
plt.subplot(122);plt.imshow(img_out[:,:,::-1]);plt.axis('off')

image.png

!./darknet detect cfg/yolov3.cfg yolov3.weights 'data/kite.jpg'
img_in = cv2.imread('data/kite.jpg')
img_out = cv2.imread('predictions.jpg')
plt.figure(figsize=[20,10])
plt.subplot(121);plt.imshow(img_in[:,:,::-1]);plt.axis('off')
plt.subplot(122);plt.imshow(img_out[:,:,::-1]);plt.axis('off')

image.png

Summary

This time, I tried running YOLO v3 in the environment of Google Colaboratory. Object detection was performed using the sample images already prepared. I think it would be interesting to prepare various images and detect objects.

reference

-YOLO official page -[Practice with YOLO v3] Introduction to object detection by deep learning (Udemy)

Recommended Posts

I tried running YOLO v3 on Google Colab
[Python] I tried using YOLO v3
I tried object detection with YOLO v3 (TensorFlow 2.0) on a windows CPU!
I tried running pymc
I tried running TensorFlow
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!
I tried running the app on the IoT platform "Rimotte"
Plotly Dash on Google Colab
Run YOLO v3 on AWS v2
Run YOLO v3 on AWS
I tried MLflow on Databricks
I tried running PIFuHD on Windows for the time being
An error that stumbled upon learning YOLO on Google Colab
I tried AdaNet on table data
I tried running GAN in Colaboratory
I tried Cython on Ubuntu on VirtualBox
I tried Grumpy (Go running Python).
I tried running prolog with python 3.8.2.
Play with Turtle on Google Colab
Notes on running M5Stick V with uPyLoader
I tried using YOUTUBE Data API V3
Machine learning with Pytorch on Google Colab
Image segment using Oxford_iiit_pet on Google Colab
I tried running Flask on Raspberry Pi 3 Model B + using Nginx and uWSGI
I tried using Remote API on GAE / J
I tried "License OCR" with Google Vision API
I tried using the Google Cloud Vision API
I tried launching jupyter nteract on heroku server
[Pythonocc] I tried using CAD on jupyter notebook
Try running Distributed TensorFlow on Google Cloud Platform
I tried running faiss with python, Go, Rust
I tried running python -m summpy.server -h 127.0.0.1 -p 8080
I tried "Receipt OCR" with Google Vision API
I tried running Deep Floor Plan with Python 3.6.10.
I tried running alembic, a Python migration tool
I tried LINE Message API (line-bot-sdk-python) on GAE
I tried playing with the calculator on tkinter
I tried simple image processing with Google Colaboratory.
I tried scraping
I tried PyQ
I tried AutoKeras
[Rails] v1.0 came out on google-cloud-vision of gem, so I tried to support it
I tried papermill
I tried django-slack
I tried Django
YOLO on Chainer
I tried spleeter
I tried cgo
I tried Python on Mac for the first time.
I tried to implement Minesweeper on terminal with python
I tried running BERT with Sakura VPS (without GPU)
I tried running python etc. from a bat file
I tried python on heroku for the first time
From python to running instance on google cloud platform
I tried running TensorFlow in AWS Lambda environment: Preparation
I tried a visual regression test on GitHub Pages
[Python] I tried running a local server using flask
I tried using PySpark from Jupyter 4.x on EMR
I tried input interpolation on UE4 Python VS Code
I tried Kaokore, a Japanese classic dataset, on EfficientNet.
I tried installing the Linux kernel on virtualbox + vagrant