SOL4Py Sample: EfficientDet

SOL4Py Samples

1 Install EfficientDetector

At first, you have to install Microsoft Visual Studio 2019 Community Edition.
How to setting up an environment for AutoML on Windows 10.

pip install tensorflow==2.2.0
pip install cython
git clone https://github.com/google/automl.git
cd automl
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI

# Probably you have to modify extra_compiler_args in setup.py in the following way:
# setup.py
#extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'],
extra_compile_args=['-std=c99'],

python setup.py build_ext install
pip install pyyaml
Please clone EfficientDetector.git in a working directory(det).

det>git clone https://github.com/atlan-antillia/EfficientDetector.git

Copy all files in EfficientDetector created by the cloning into the automl/efficientdet.


2 Run EfficientDetecotor

You have to run the following script to download an EfficientDet model chekcpoint file:

>python DownloadCkpt.py

,and a sample image file:

>python DownloadImage.py


Please run the following command to use EfficientDetector.py in the following way.

efficentdet>python EfficientDetector.py input_image_file_or_dir output_image_dir [optional_filters]

inpur_image_file_or_dir:
     an input_image_file, or directory including jpg or png files.
output_image_dir:
     an output_image_dir where detected image file(s) will be saved.
opitiona_filters :
     a list of classes something like [person, car] to be selected
    in a post-processing stage after an object detection process.


The above command will generate detected_image, detected_objects_csv, objects_stats.csv files in output_image_dir from the input_image file.


3 Inference Examples

Example 1:

efficentdet>python EfficientDetector.py .\images\img.png detected

img.png


img.png.csv


img.png_stats.csv



Example 2:

efficentdet>python EfficientDetector.py .\images\ShinJuku.jpg detected


Example 3: filters=[person]

efficentdet>python EfficientDetector.py .\images\ShinJuku2.jpg detected [person]



You can run the following command to apply a batch of detection processes to all image files under the input_image_dir.

efficentdet>python EfficientDetector.py input_image_dir outout_image_dir otional_filters_like_[person, car,..something]


#******************************************************************************
#
#  Copyright (c) 2020 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#******************************************************************************
#  
# DownloadCkpt.py

import sys
import os
import traceback
import tarfile
import shutil
import tensorflow as tf

def download_checkpoint_file():
  try:
      #Download checkpoint file
      url = "https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/efficientdet-d0.tar.gz"
      folder = "efficientdet-d0"
      tar_file = "efficientdet-d0.tar.gz"

      if os.path.exists(folder) != True:
          print("Try download {}".format(url))

          tar_file = tf.keras.utils.get_file(tar_file, url)
          print("You have downloaded {}".format(tar_file))

          with tarfile.open(tar_file, "r:gz") as tar:
             tar.extractall()
      else:
          print("OK, you have the weight file {}!".format(tar_file))
       
  except Exception as ex:
    traceback.print_exc()


if __name__=="__main__":
         
  try:
      MODEL = "efficientdet-d0"
      ckpt_path = os.path.join(os.getcwd(), MODEL);
      download_checkpoint_file()

  except Exception as ex:
    traceback.print_exc()


#******************************************************************************
#
#  Copyright (c) 2020 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#******************************************************************************
#  
# DownloadImage.py

import sys
import os
import time
import traceback
import numpy as np

import shutil


def download_image_file(img_file):

  try:   
    path = os.path.join(os.getcwd(), "images")
    os.makedirs(path, exist_ok=True)
    local_image_path = os.path.join(path, img_file)
    if os.path.exists(local_image_path) != True:

         url = 'https://user-images.githubusercontent.com/11736571/77320690-099af300-6d37-11ea-9d86-24f14dc2d540.png'
         print("Downloading a file {}".format(url))

         img_file = tf.keras.utils.get_file(img_file, url)
         shutil.move(img_file, local_image_path)

         print("You have downloaded {}".format(local_image_path))
    else:
         print("Found a downloaded file {}".format(local_image_path))

    return local_image_path

  except Exception as ex:
    traceback.print_exc()

if __name__=="__main__":
         
  try:
      img_file="img.png"

      download_image_file(img_file)

  except Exception as ex:
    traceback.print_exc()




#******************************************************************************
#
#  Copyright (c) 2020 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .
#
#******************************************************************************
# 
# 
# EfficientDetector.py

# 2020/06/17:
# Updated EfficientDetector class
# Added detect_all method to DetectionTransformer class.
#   def detect_all(self, input_image_dir, output_image_dir):
#
# This method detects objectes in each image in input_image_dir, and saves the detected image 
# to output_image_dir.
#
# 2020/07/23 
#  Updated inference.py and visualize/vis_utils.py to get detected_objects information
#
    
import sys
import os
import glob
import time
import traceback
import matplotlib
matplotlib.use('Agg')  # Set headless-friendly backend.
import matplotlib.pyplot as plt  # pylint: disable=g-import-not-at-top
import PIL

from PIL import Image
import tensorflow.compat.v1 as tf
#import inference 
import inference2 

from FiltersParser import FiltersParser

class EfficientDetector:

  def __init__(self, model="efficientdet-d0"):
      self.min_score_thresh  = 0.4  
      self.max_boxes_to_draw = 200  
      self.line_thickness    = 2
 
      self.MODEL       = model #"efficientdet-d0"
      self.ckpt_path   = os.path.join(os.getcwd(), self.MODEL)


  #2020/06/17
  # Detect objectes in each image in input_image_dir, and save the detected image 
  # to output_image_dir.
    
  def detect_all(self, filters, input_image_dir, output_image_dir):
      if not os.path.exists(input_image_dir):
          raise Exception("Not found input_image_dir {}".format(input_image_dir))

      output_image_dir = os.path.join(os.getcwd(), output_image_dir)
      if not os.path.exists(output_image_dir):
          os.makedirs(output_image_dir)
      
      image_list = []

      if os.path.isdir(input_image_dir):
        image_list.extend(glob.glob(os.path.join(input_image_dir, "*.png")) )
        image_list.extend(glob.glob(os.path.join(input_image_dir, "*.jpg")) )

      print("image_list {}".format(image_list) )
          
      for image_filename in image_list:
          #image_filename will take images/foo.png
          image_file_path = os.path.abspath(image_filename)
          
          print("filename {}".format(image_file_path))
          
          predictions = self.detect(filters, image_file_path, output_image_dir)


  def detect(self, filters, input_image_filepath, output_image_dir="detected"):
      if not os.path.exists(input_image_filepath):
          raise Exception("Not found image_file {}".format(input_image_filepath)) 
      
      output_image_dir = os.path.join(os.getcwd(), output_image_dir)
      if not os.path.exists(output_image_dir):
          os.makedirs(output_image_dir)
          
      tf.reset_default_graph()
      
      image_size  = max(PIL.Image.open(input_image_filepath).size)
      print("ImageSize {}".format(image_size))

      self.model_params= {"image_size": image_size}

      self.driver = inference2.InferenceDriver2(self.MODEL, 
                                              self.ckpt_path, 
                                 model_params=self.model_params) 
         
      print("Start inference")
      start = time.time()

      predictions = self.driver.inference(filters, 
                 input_image_filepath,
                 output_image_dir,
                 min_score_thresh  = self.min_score_thresh,
                 max_boxes_to_draw = self.max_boxes_to_draw,
                 line_thickness    = self.line_thickness)
      print("Done inference")
      elapsed_time = time.time() - start
      print("Elapsed_time:{0}".format(elapsed_time) + "[sec]")
      return predictions



########################
#
if __name__=="__main__":

  try:
     if len(sys.argv) < 3:
        raise Exception("Usage: {} input_image_file output_image_dir filters".format(sys.argv[0]))
        
     input_image_file = None
     input_image_dir  = None
     output_image_dir = None
     filters          = None  # classnames_list something like this "[person,car]"
     
     if len(sys.argv) >= 2:
       input = sys.argv[1]
       if not os.path.exists(input):
         raise Exception("Not found input {}".format(input))
       if os.path.isfile(input):
         input_image_file = input
       else:
         input_image_dir  = input

     if len(sys.argv) >= 3:
       output_image_dir = sys.argv[2]
       if not os.path.exists(output_image_dir):
         os.makedirs(output_image_dir)
     if len(sys.argv) == 4:
       str_filters = sys.argv[3]
       filtersParser = FiltersParser(str_filters)
       filters = filtersParser.get_filters()
       
     if input_image_file is not None and output_image_dir is not None:

         detector = EfficientDetector()
         detector.detect(filters, input_image_file, output_image_dir)

     if input_image_dir is not None and output_image_dir is not None:
         detector       = EfficientDetector()
         detector.detect_all(filters, input_image_dir, output_image_dir)
  
  except Exception as ex:
    traceback.print_exc()



Last modified: 15 Aug. 2020