<jupyter_start><jupyter_text>EfficientDet Inference       View source on github        Run in Google Colab  0. Install<jupyter_code>#@title
# Install tensorflow and pycocotools
!pip install tensorflow
!pip install pytype
# The default pycocotools doesn't work for python3: https://github.com/cocodataset/cocoapi/issues/49
!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'

import os
import sys
import tensorflow.compat.v1 as tf

# Download source code.
if "efficientdet" not in os.getcwd():
  !git clone --depth 1 https://github.com/google/automl
  os.chdir('automl/efficientdet')
  sys.path.append('.')
else:
  !git pull
MODEL = 'efficientdet-d0'  #@param
# Download checkpoint.
!wget https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/{MODEL}.tar.gz
!tar zxf {MODEL}.tar.gz
ckpt_path = os.path.join(os.getcwd(), MODEL)
print('Use model in {}'.format(ckpt_path))<jupyter_output>--2020-03-24 08:35:31--  https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/efficientdet-d0.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 192.168.127.12, fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b
Connecting to storage.googleapis.com (storage.googleapis.com)|192.168.127.12|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 29040887 (28M) [application/octet-stream]
Saving to: ‘efficientdet-d0.tar.gz.1’

efficientdet-d0.tar 100%[===================>]  27.70M  55.9MB/s    in 0.5s    

2020-03-24 08:35:31 (55.9 MB/s) - ‘efficientdet-d0.tar.gz.1’ saved [29040887/29040887]

Use model in /content/automl/efficientdet/efficientdet-d0<jupyter_text>Inference a single image.<jupyter_code># Image is from issue: https://github.com/google/automl/issues/27
image_url =  'https://user-images.githubusercontent.com/11736571/77320690-099af300-6d37-11ea-9d86-24f14dc2d540.png'#@param
image_name = 'img.png' #@param
!wget {image_url} -O img.png

import os
img_path = os.path.join(os.getcwd(), 'img.png')
img_out_dir = os.path.join(os.getcwd(), 'img_out')

if not os.path.exists(img_out_dir):
  os.mkdir(img_out_dir)

# Get the largest of height/width and round to 128.
import PIL
image_size = max(PIL.Image.open(img_path).size)  // 128 * 128
min_score_thresh = 0.2  #@param
max_boxes_to_draw = 100  #@param
line_thickness = 4  #@param

# Approach 1: use model_inspect.
# !python model_inspect.py --runmode=infer --model_name={MODEL} --ckpt_path={ckpt_path} --input_image={img_path} --output_image_dir={img_out_dir}

# Approach 2: call InferenceDriver
import inference
tf.reset_default_graph()
driver = inference.InferenceDriver(MODEL, ckpt_path, image_size=image_size)
driver.inference(img_path,
                 img_out_dir,
                 min_score_thresh=min_score_thresh,
                 max_boxes_to_draw=max_boxes_to_draw,
                 line_thickness=line_thickness)
from IPython import display
display.display(display.Image(os.path.join(img_out_dir, '0.jpg')))<jupyter_output><empty_output><jupyter_text>Run COCO Evaluation<jupyter_code>!wget http://images.cocodataset.org/zips/val2017.zip
!wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
!unzip val2017.zip
!unzip annotations_trainval2017.zip
# Convert to tfrecord
!mkdir tfrecrod
!PYTHONPATH=".:$PYTHONPATH"  python dataset/create_coco_tfrecord.py \
      --image_dir=val2017 \
      --caption_annotations_file=annotations/captions_val2017.json \
      --output_file_prefix=tfrecord/val \
      --num_shards=32
# Evalute on validation set
!python main.py --mode=eval  \
    --model_name={MODEL}  --model_dir={ckpt_path}  \
    --validation_file_pattern=tfrecord/val*  \
    --val_json_file=annotations/instances_val2017.json  \
    --hparams="use_bfloat16=false" --use_tpu=False<jupyter_output>WARNING:tensorflow:From /tensorflow-1.15.0/python3.6/tensorflow_core/python/compat/v2_compat.py:68: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING:tensorflow:From main.py:213: The name tf.estimator.tpu.TPUConfig is deprecated. Please use tf.compat.v1.estimator.tpu.TPUConfig instead.

W0324 08:38:52.020046 140117296494464 module_wrapper.py:139] From main.py:213: The name tf.estimator.tpu.TPUConfig is deprecated. Please use tf.compat.v1.estimator.tpu.TPUConfig instead.

WARNING:tensorflow:From main.py:218: The name tf.estimator.tpu.InputPipelineConfig is deprecated. Please use tf.compat.v1.estimator.tpu.InputPipelineConfig instead.

W0324 08:38:52.020277 140117296494464 module_wrapper.py:139] From main.py:218: The name tf.estimator.tpu.InputPipelineConfig is deprecated. Please use tf.compat.v1.estimator.tpu.InputPipeli[...]
{'fraction_non_alphanumeric': 0.1273792093704246, 'fraction_numerical': 0.06546747542355155, 'mean_word_length': 4.8246041412911085, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '52442', 'license': 'Apache-2.0', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1728, 'n_words': 317, 'path': 'efficientdet/tutorial.ipynb', 'repo_name': 'CraigWang1/automl', 'size': '4762'}