-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.
--Open Google Colaboratory and change "Runtime Type Change" to "GPU" on the "Runtime" tab.
――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
-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)
--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
--Run the model
Model execution
!python demo.py -c=configs/m2det512_vgg.py -m=m2det512_vgg.pth
--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)
--Other images were also carried out
https://github.com/hiraku00/m2det_test
-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