[PYTHON] [Machine learning] Try to detect objects using Selective Search

What is Selective Search?

A task to detect "object-like" parts in an image. As a rough flow, prepare multiple rectangles and slide them on the image to find the "object-like" part. Note that it doesn't just detect what an object is or a specific object. For example, it is not possible to find only a car in an image. It is a famous task that is also used in R-CNN, which is famous for its object detection method, but when I looked it up, there were surprisingly few articles, so I summarized it in this article.

environment

Installation

Easy to install using pip

pip install selective-search

Install opencv with pip

Target image

Let's execute it for the following four images. The image is Pascal VOC.

car dog furniture
000071.jpg 000108.jpg 000102.jpg

Patter meter setting

random There is a random parameter, but the reference says:

If random set to True, function will carry out pseudo random sorting. It only alters sequences of bounding boxes, instead of locations, which prevents heavily emphasis on large regions as combing proposals from up to 80 different strategies[1]. This only has a significant impact when selecting a subset of region proposals with high rankings, as in RCNN.

Apparently, in selective search, large rectangles are given priority and presented. When detecting an object using RCNN etc., it becomes impossible to recognize small objects, so by setting random, it is possible to prevent only large objects.

mode There are three modes in selective search

According to the reference, there are the following differences. mode.png

That is, as single, fast, quality, the number of rectangles, in other words, the number of trials increases. On the other hand, there is also the problem that the speed drops. The following is the trial result.

mode car furniture dog
single 000071.jpg 000102.jpg 000108.jpg
fast 000071.jpg 000102.jpg 000108.jpg
quality 000071.jpg 000102.jpg 000108.jpg

Estimated objects increase as single, fast, quality, but there are almost no cases that require so many rectangles. Basically, it is enough to implement it with single.

Even if single is set, there are too many rectangles, so when implementing this time, we decided to suppress the output of rectangles that do not have a certain size.

Implementation

Directory structure

├── pic #Original image
├── result #Result image
│   ├── fast
│   ├── quality
│   └── single
└── exe.py #Executable file

Executable file

import cv2
import glob
from selective_search import selective_search


###parameter settings
MODE="single"
#MODE="fast"
#MODE="quality"
MINH=100
MINW=100


###I / O directory settings
ORGDIR="./pic/"
RSTDIR="./result/"+MODE+"/"


def search():

    ###Read input file path
    tgtpaths=glob.glob(ORGDIR+"*")

    for tgtpath in tgtpaths:

        ###Output file path setting
        rstpath=tgtpath.replace(ORGDIR,RSTDIR)

        ###Target image reading
        tgtimg = cv2.imread(tgtpath, cv2.IMREAD_COLOR)

        ###Run Selective Search
        boxes = selective_search(tgtimg, mode=MODE,random=False)

        for box in boxes:

            ###Do not display rectangles without a certain length
            if abs(box[2]-box[0]) < MINW or abs(box[3]-box[1])<MINH:
                continue

            ###Rectangle drawing
            cv2.rectangle(tgtimg, (box[0],box[1]), (box[2],box[3]), (0,255,0), thickness=1)

        ###output
        print(rstpath)
        cv2.imwrite(rstpath, tgtimg)



if __name__=="__main__":
    search()

result

car furniture dog
000071.jpg 000102.jpg 000108.jpg

Selectivesearch presented "object-like" parts. In RCNN etc., object detection is performed by applying these candidates to a classifier.

reference

Recommended Posts

[Machine learning] Try to detect objects using Selective Search
Try to detect fusion movement using AnyMotion
Try to forecast power demand by machine learning
Try using Jupyter Notebook of Azure Machine Learning
Introduction to machine learning
Try to write code from 1 using the machine learning framework chainer (mnist edition)
Try to predict forex (FX) with non-deep machine learning
Machine learning beginners try to make a decision tree
I tried to compress the image using machine learning
An introduction to machine learning
Super introduction to machine learning
Try machine learning with Kaggle
Try to evaluate the performance of machine learning / regression model
Try to evaluate the performance of machine learning / classification model
Machine learning beginners try to reach out to Naive Bayes (2) --Implementation
Try to predict if tweets will burn with machine learning
Machine learning beginners try to reach out to Naive Bayes (1) --Theory
[Machine learning] Try studying decision trees
Machine learning beginners try linear regression
Try using pynag to configure Nagios
Try machine learning with scikit-learn SVM
Try to get statistics using e-Stat
Introduction to Machine Learning Library SHOGUN
Application development using Azure Machine Learning
How to collect machine learning data
Try to predict the value of the water level gauge by machine learning using the open data of Data City Sabae
Aiming to become a machine learning engineer from sales positions using MOOCs
Search for technical blogs by machine learning focusing on "easiness to understand"
I want to detect objects with OpenCV
Introduction to Machine Learning: How Models Work
scikit-learn How to use summary (machine learning)
Stock price forecast using machine learning (scikit-learn)
Record the steps to understand machine learning
[Machine learning] LDA topic classification using scikit-learn
I installed Python 3.5.1 to study machine learning
An introduction to OpenCV for machine learning
[Machine learning] FX prediction using decision trees
I tried using Selective search as R-CNN
[Machine learning] Supervised learning using kernel density estimation
Introduction to ClearML-Easy to manage machine learning experiments-
Try to operate Excel using Python (Xlwings)
How to enjoy Coursera / Machine Learning (Week 10)
An introduction to Python for machine learning
Stock price forecast using machine learning (regression)
Let's try neural machine translation using Transformer
[Machine learning] Regression analysis using scikit learn
Try to download Youtube videos using Pytube
Try to draw a "weather map-like front" by machine learning based on weather data (5)
[Super Introduction] Machine learning using Python-From environment construction to implementation of simple perceptron-
Try to draw a "weather map-like front" by machine learning based on weather data (3)
Machine learning
Try to draw a "weather map-like front" by machine learning based on weather data (1)
Try to draw a "weather map-like front" by machine learning based on weather data (4)
Try to draw a "weather map-like front" by machine learning based on weather data (2)
I tried to implement various methods for machine learning (prediction model) using scikit-learn.
[Python] Easy introduction to machine learning with python (SVM)
[Super Introduction to Machine Learning] Learn Pytorch tutorials
An introduction to machine learning for bot developers
A story about simple machine learning using TensorFlow
Data supply tricks using deques in machine learning
Try using django-import-export to add csv data to django