<jupyter_start><jupyter_text>For the most up to date version of this notebook, please copy from this link---[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1ZmbeTro4SqT7h_TfW63MLdqbrCUk_1brscrollTo=KwDS9qqBbMQa)  Overview💡 Recommendation: Open this blog post on [how to train EfficientDet](https://towardsdatascience.com/training-efficientdet-object-detection-model-with-a-custom-dataset-25fb0f190555) to continue.In this notebook we show an example of how to train EfficientDet using a pytorch implementation on a custom dataset that has been uploaded through RoboFlow. The example provides a flexible framework, so you can apply it to your own dataset with a custom number of classes and a different objective. We we tackle chess here. ![Chess Example](https://i.imgur.com/nkjobw1.png) **Our Data and Roboflow**Our dataset of 289 chess images (and 2894 annotations!) is hosted publicly on Roboflow [here](https://public.roboflow.ai/object-detection/chess-full). Roboflow also hosts many other public datasets and you can easily upload your own custom dataset for your use case, augment, and export in flexible formats. Our tutorial uses Coco Json, but you might have another format (say tfrecord). No problem! Upload your dataset and we will export it in the required format. **Model and Training**For a deep dive on the EfficientDet model please see [the paper](https://arxiv.org/abs/1911.09070). For a shorter look, here is a great [blog post](https://towardsdatascience.com/efficientdet-scalable-and-efficient-object-detection-review-4472ffc34fd9)! We use a pytorch implementation of EfficientDet using the [image detection library](https://github.com/roboflow-ai/Monk_Object_Detection) from Tessellate-Imaging for object detection. Our implementation uses the base version of EfficientDet-d0.  We train from the EfficientNet base backbone, without using a pretrained checkpoint for the detector. **Inference**We witness some fast inference on a few basic examples from our test set to see that our approach is heading in the right direction. **Export**We export our model weights to google drive for future utilization. **Next Steps**We will be exploring evaluation on custom RoboFlow datasets and objectives compared to yoloV3, including training time, inference time, model size, and performance. We will also explore comparing performance from the Coco pretrained checkpoint! **Stay in touch!**If you run into any hurdles on your own data set or just want to share some cool results in your own domain, [reach out to us](roboflow.ai)!  ![Roboflow Workmark](https://i.imgur.com/WHFqYSJ.png)  Setting up our envionment<jupyter_code>#our fork of the Tessellate-Imaging image detection library
#!rm -rf Monk_Object_Detection
! git clone https://github.com/roboflow-ai/Monk_Object_Detection.git
# For colab use the command below
# Set up library requirments
! cd Monk_Object_Detection/3_mxrcnn/installation && cat requirements_colab.txt | xargs -n 1 -L 1 pip install
#fixed version of tqdm output for Colab
!pip install --force https://github.com/chengs/tqdm/archive/colab.zip
#IGNORE restart runtime warning, it is indeed installed
#missing a few extra packages that we will need later! 
!pip install efficientnet_pytorch
!pip install tensorboardX<jupyter_output>Collecting https://github.com/chengs/tqdm/archive/colab.zip
  Using cached https://github.com/chengs/tqdm/archive/colab.zip
Building wheels for collected packages: tqdm
  Building wheel for tqdm (setup.py) ... [?25l[?25hdone
  Created wheel for tqdm: filename=tqdm-4.28.1-py2.py3-none-any.whl size=47867 sha256=53241244de5d542ba1161f2be41f81c4d89419baa79ce388cdc03a93c62139b4
  Stored in directory: /tmp/pip-ephem-wheel-cache-xv9igesq/wheels/41/18/ee/d5dd158441b27965855b1bbae03fa2d8a91fe645c01b419896
Successfully built tqdm
[31mERROR: spacy 2.2.4 has requirement tqdm<5.0.0,>=4.38.0, but you'll have tqdm 4.28.1 which is incompatible.[0m
Installing collected packages: tqdm
  Found existing installation: tqdm 4.28.1
    Uninstalling tqdm-4.28.1:
      Successfully uninstalled tqdm-4.28.1
Successfully installed tqdm-4.28.1<jupyter_text>Let's get some data! The best part about Roboflow is the efficient management of your datasets. [Upload you dataset](roboflow.ai) and you will recieve a fresh curl code to ouput it in whatever augmented and annotated format you need.<jupyter_code>#fresh curl courtesy of roboflow.ai, outputing our dataset in Coco Json format

##THIS CELL NEEDS CHANGED##

!curl -L https://public.roboflow.ai/ds/frnjH3vWjO?key=b8exJpgaE0 > roboflow.zip; unzip roboflow.zip; rm roboflow.zip
#let's take a look at our directory
#notice the data came down in train, valid, test, splits - this is pre set during the dataset upload process
%ls
#let's take a peak in train
#jpg images and some coco json annotations
%ls train
#let's take a peek at the annotations
#class categories, and bounding boxes are provided for our train set
%pycat train/_annotations.coco.json
#in the next three cells, we move the data into a structure that the image detection library will be expecting
#but no file data manipulation is necessary
#images can also be segmented into class folders, but we combine all classes here
!mkdir Chess
!mkdir Chess/annotations
!mkdir Chess/Annotations
!mkdir Chess/Images
%cp train/_annotations.coco.json Chess/annotations/instances_Images.json
%cp train/*.jpg Chess/Images/<jupyter_output><empty_output><jupyter_text>TrainingIn this section we set up the efficientDet-d0 model from backbone and train to our custom case<jupyter_code>import os
import sys
sys.path.append("Monk_Object_Detection/4_efficientdet/lib/");
from train_detector import Detector
gtf = Detector();
#directs the model towards file structure
root_dir = "./";
coco_dir = "Chess";
img_dir = "./";
set_dir = "Images";
#smells like some free compute from Colab, nice
gtf.Train_Dataset(root_dir, coco_dir, img_dir, set_dir, batch_size=8, image_size=512, use_gpu=True)
gtf.Model();
gtf.Set_Hyperparams(lr=0.0001, val_interval=1, es_min_delta=0.0, es_patience=0)
%%time
gtf.Train(num_epochs=100, model_output_dir="trained/");<jupyter_output><empty_output><jupyter_text>Inference<jupyter_code>import os
import sys
sys.path.append("Monk_Object_Detection/4_efficientdet/lib/");
from infer_detector import Infer
gtf = Infer();
#our trained model weights are in here in onxx format
gtf.Model(model_dir="trained/")
#extract class list from our annotations
import json
with open('train/_annotations.coco.json') as json_file:
    data = json.load(json_file)
class_list = []
for category in data['categories']:
  class_list.append(category['name'])
class_list
%%time
#bang!
img_path = "test/2f6fb003bb89cd401322a535acb42f65_jpg.rf.49b342a7b1f6de3f0e328beaf094a945.jpg";
duration, scores, labels, boxes = gtf.Predict(img_path, class_list, vis_threshold=0.2);
from IPython.display import Image
Image(filename='output.jpg')<jupyter_output><empty_output><jupyter_text>Export Trained Weights<jupyter_code>#export trained model
# mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
%mkdir trained_export
%cp ./trained/signatrix_efficientdet_coco.onnx ./trained_export/signatrix_efficientdet_coco_$(date +%F-%H:%M).onnx
%cp ./trained/signatrix_efficientdet_coco.pth ./trained_export/signatrix_efficientdet_coco_$(date +%F-%H:%M).pth
%mv ./trained_export/* /content/drive/My\ Drive/<jupyter_output><empty_output><jupyter_text>Reloading Trained Weights after ExportImagine you have exported your trained model and would like to reaccess it later. This portion of the notebook picks up the trained model and starts at inference<jupyter_code>#export trained model
# mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
#our fork of the Tessellate-Imaging image detection library
#!rm -rf Monk_Object_Detection
! git clone https://github.com/roboflow-ai/Monk_Object_Detection.git
# For colab use the command below
# Set up library requirments
! cd Monk_Object_Detection/3_mxrcnn/installation && cat requirements_colab.txt | xargs -n 1 -L 1 pip install

#fixed version of tqdm output for Colab
!pip install --force https://github.com/chengs/tqdm/archive/colab.zip
#IGNORE restart runtime warning, it is indeed installed
#missing a few extra packages that we will need later! 
!pip install efficientnet_pytorch
!pip install tensorboardX
#recover trained weights
!mkdir '/trained'
#note these are old weights that I didn't train for too long
!cp '/content/drive/My Drive/signatrix_efficientdet_coco_2020-04-10-15:18.onnx' '/trained/signatrix_efficientdet_coco.onnx'
!cp '/content/drive/My Drive/signatrix_efficientdet_coco_2020-04-10-15:18.pth' '/trained/signatrix_efficientdet_coco.pth'
import os
import sys
sys.path.append("Monk_Object_Detection/4_efficientdet/lib/");
from infer_detector import Infer
gtf = Infer();
#our trained model weights are in here in onxx format
gtf.Model(model_dir="/trained")
#download some test data
!curl -L [YOUR LINK HERE] | jar -x
!ls test
#extract class list from our annotations
#in your application you will probably already have this saved
import json
with open('train/_annotations.coco.json') as json_file:
    data = json.load(json_file)
class_list = []
for category in data['categories']:
  class_list.append(category['name'])
class_list
%%time
#bang!
img_path = "test/2f6fb003bb89cd401322a535acb42f65_jpg.rf.49b342a7b1f6de3f0e328beaf094a945.jpg";
duration, scores, labels, boxes = gtf.Predict(img_path, class_list, vis_threshold=0.2);
from IPython.display import Image
Image(filename='output.jpg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08870214752567694, 'fraction_numerical': 0.03195352214960058, 'mean_word_length': 5.769662921348314, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '249745', 'license': 'Apache-2.0', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3038, 'n_words': 998, 'path': 'Roboflow_EfficientDet_v2.ipynb', 'repo_name': 'wjshamblin/Monk_Object_Detection', 'size': '9639'}