[PYTHON] I tried to detect an object with M2Det!

things to do

-Previous time and Last time performed object detection with SSD and YOLO v3, respectively. I tried, but this time I will try to detect the object with M2Det --This time, we will run it on Google Colaboratory.

Overview

  1. Preparation of execution environment (executed by Google Colaboratory)
  2. Download the model from Google Drive
  3. Copy image file
  4. Run the model
  5. Display results

Execution environment

1. Preparation of execution environment (executed by Google Colaboratory)

--Open Google Colaboratory and change "Runtime Type Change" to "GPU" on the "Runtime" tab. スクリーンショット 2019-12-15 18.54.06.png

――After that, do the following --Install various packages --Clone repository --Run shell

Preparation of execution environment


!pip install torch torchvision
!pip install opencv-python tqdm addict
!git clone https://github.com/qijiezhao/M2Det.git
%cd M2Det/
!sh make.sh

2. Download the model from Google Drive

-The README of GitHub has the link destination of the trained model (https://drive.google.com/file/d/1NM1UDdZnwHwiNDxhcP-nndaWj24m- 90L / view), so I will download it on the code from this Google Drive link --For download, I referred to the code of here. --You can download by command by executing download_file_from_google_drive.

python


import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

file_id = '1NM1UDdZnwHwiNDxhcP-nndaWj24m-90L'
destination = './m2det512_vgg.pth'
download_file_from_google_drive(file_id, destination)

3. Copy image file

--Mount Google Drive --After that, copy all the jpg files in the folder containing the image files to "imgs" that executes M2Det (in my environment, the image files are stored under My Drive> ML> work).

Google Drive mount


from google.colab import drive
drive.mount('/content/drive')

Copy image file


!cp /content/drive/My\ Drive/ML/work/*.jpg ./imgs

4. Run the model

--Run the model

Model execution


!python demo.py -c=configs/m2det512_vgg.py -m=m2det512_vgg.pth

5. Display results

--Display the result of execution --The executed image file will be created as "XXX_m2det.jpg "

python


import cv2
import matplotlib.pyplot as plt

plt.figure(figsize=(5, 5), dpi=200)
img = cv2.imread('imgs/herd_of_horses_m2det.jpg')
show_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(show_img)

ダウンロード1.png

--Other images were also carried out

ダウンロード2.png

ダウンロード3.png

ダウンロード4.png

Source code

https://github.com/hiraku00/m2det_test

reference

-I tried M2Det, which is rumored to be faster than YOLO and SSD -Try demo / evaluation / learning with M2Det -I tried to move the latest and strongest object detection technology M2Det

Recommended Posts

I tried to detect an object with M2Det!
I tried to detect Mario with pytorch + yolov3
I tried to detect motion quickly with OpenCV
I tried to implement an artificial perceptron with python
I tried to make an OCR application with PySimpleGUI
I tried to find an alternating series with tensorflow
I tried to create an article in Wiki.js with SQLAlchemy
I want to detect objects with OpenCV
I tried sending an SMS with Twilio
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I tried sending an email with python.
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
I tried to easily detect facial landmarks with python and dlib
I tried to make an image similarity function with Python + OpenCV
I tried to predict next year with AI
I tried to implement reading Dataset with PyTorch
I tried to use lightGBM, xgboost with Boruta
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
I tried to get an image by scraping
I want to be an OREMO with setParam!
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to visualize Google's general object recognition NN, Inception-v3 with Tensorboard
I tried to debug.
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I tried to paste
I tried to operate Linux with Discord Bot
I tried sending an email with SendGrid + Python
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I tried to extract a line art from an image with Deep Learning
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
I tried to create an environment to check regularly using Selenium with AWS Fargate
I tried to learn the sin function with chainer
I want to convert an image to WebP with lollipop
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to create a table only with Django
I want to detect unauthorized login to facebook with Jubatus (1)
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to read and save automatically with VOICEROID2 2
I tried to detect the iris from the camera image
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried Smith standardizing an integer matrix with Numpy
I tried to automatically read and save with VOICEROID2
I tried to get started with blender python script_Part 02
I tried to generate ObjectId (primary key) with pymongo
I tried to build ML Pipeline with Cloud Composer
I tried to implement time series prediction with GBDT
I tried to get an AMI using AWS Lambda
I tried to become an Ann Man using OpenCV