Skip to content

Instantly share code, notes, and snippets.

View KostaMalsev's full-sized avatar
📱
Building

Kosta Malsev KostaMalsev

📱
Building
View GitHub Profile
@KostaMalsev
KostaMalsev / retrain-object-detection_ssd_mobilenetv2.ipynb
Last active December 13, 2022 10:16
Retrain-Object-Detection_ssd_mobilenetv2.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import os
import pathlib
# Clone the tensorflow models repository if it doesn't already exist
if "models" in pathlib.Path.cwd().parts:
while "models" in pathlib.Path.cwd().parts:
os.chdir('..')
elif not pathlib.Path('models').exists():
!git clone --depth 1 https://github.com/tensorflow/models
# Install the Object Detection API
%%bash
cd /content/models/research/
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .
import matplotlib
import matplotlib.pyplot as plt
import os
import random
import io
import imageio
import glob
import scipy.misc
import numpy as np
#Downloading data Training set made by Roboflow
%cd /content
#Download Training set from git by cloning rep:
import os
import pathlib
# Clone the training set repository if it doesn't already exist
if "RetrainModelExample" in pathlib.Path.cwd().parts:
while "RetrainModelExample" in pathlib.Path.cwd().parts:
os.chdir('..')
#You can change chosen model to deploy different models available in the TF2 object detection zoo
MODELS_CONFIG = {
'ssd_mobilenet_v2_320x320_coco17': {
'model_name': 'ssd_mobilenet_v2_320x320_coco17_tpu-8',
'base_pipeline_file': 'ssd_mobilenet_v2_320x320_coco17_tpu-8.config',
'pretrained_checkpoint': 'ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.gz',
'batch_size': 16
}
}
chosen_model = 'ssd_mobilenet_v2_320x320_coco17'
#Download pretrained weights
%mkdir /content/deploy/
%cd /content/deploy/
import tarfile
download_tar = 'http://download.tensorflow.org/models/object_detection/tf2/20200711/' + pretrained_checkpoint
!wget {download_tar}
tar = tarfile.open(pretrained_checkpoint)
tar.extractall()
tar.close()
!python /content/models/research/object_detection/model_main_tf2.py \
--pipeline_config_path={pipeline_file} \
--model_dir={model_dir} \
--alsologtostderr \
--num_train_steps={num_steps} \
--sample_1_of_n_eval_examples=1 \
--num_eval_steps={num_eval_steps}
#run conversion script to save the retrained model:
#Saved model will be in saved_model.pb file:
import re
import numpy as np
output_directory = '/content/fine_tuned_model'
#place the model weights you would like to export here
last_model_path = '/content/training/'
import os
import glob
import matplotlib
import matplotlib.pyplot as plt
import io
import scipy.misc
import numpy as np
from six import BytesIO
from PIL import Image, ImageDraw, ImageFont