Created
November 26, 2021 19:14
-
-
Save nguaki/0b5b55f7aff30ae24425f52f89148fec to your computer and use it in GitHub Desktop.
Google Colab error: Loaded runtime CuDNN library: 8.0.5 but source was compiled with: 8.1.0. CuDNN library needs to have matching major version and equal or higher minor version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
CUSTOM_MODEL_NAME = 'my_ssd_mobnet' | |
PRETRAINED_MODEL_NAME = 'ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8' | |
PRETRAINED_MODEL_URL = 'http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz' | |
TF_RECORD_SCRIPT_NAME = 'generate_tfrecord.py' | |
LABEL_MAP_NAME = 'label_map.pbtxt' | |
paths = { | |
'WORKSPACE_PATH': os.path.join('Tensorflow', 'workspace'), | |
'SCRIPTS_PATH': os.path.join('Tensorflow','scripts'), | |
'APIMODEL_PATH': os.path.join('Tensorflow','models'), | |
'ANNOTATION_PATH': os.path.join('Tensorflow', 'workspace','annotations'), | |
'IMAGE_PATH': os.path.join('Tensorflow', 'workspace','images'), | |
'MODEL_PATH': os.path.join('Tensorflow', 'workspace','models'), | |
'PRETRAINED_MODEL_PATH': os.path.join('Tensorflow', 'workspace','pre-trained-models'), | |
'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME), | |
'OUTPUT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'export'), | |
'TFJS_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfjsexport'), | |
'TFLITE_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfliteexport'), | |
'PROTOC_PATH':os.path.join('Tensorflow','protoc') | |
} | |
files = { | |
'PIPELINE_CONFIG':os.path.join('Tensorflow', 'workspace','models', CUSTOM_MODEL_NAME, 'pipeline.config'), | |
'TF_RECORD_SCRIPT': os.path.join(paths['SCRIPTS_PATH'], TF_RECORD_SCRIPT_NAME), | |
'LABELMAP': os.path.join(paths['ANNOTATION_PATH'], LABEL_MAP_NAME) | |
} | |
!pwd | |
%cd /content | |
for path in paths.values(): | |
if not os.path.exists(path): | |
if os.name == 'posix': | |
print(path) | |
!mkdir -p {path} | |
if os.name == 'nt': | |
!mkdir {path} | |
# 1. Download TF Models Pretrained Models from Tensorflow Model Zoo and Install TFOD | |
# https://www.tensorflow.org/install/source_windows | |
if os.name=='nt': | |
!pip install wget | |
import wget | |
if not os.path.exists(os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection')): | |
!git clone https://github.com/tensorflow/models {paths['APIMODEL_PATH']} | |
!pip install --upgrade pip | |
# Install Tensorflow Object Detection | |
if os.name=='posix': | |
!apt-get install protobuf-compiler | |
!cd Tensorflow/models/research && protoc object_detection/protos/*.proto --python_out=. && cp object_detection/packages/tf2/setup.py . && python -m pip install . | |
if os.name=='nt': | |
url="https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/protoc-3.15.6-win64.zip" | |
wget.download(url) | |
!move protoc-3.15.6-win64.zip {paths['PROTOC_PATH']} | |
!cd {paths['PROTOC_PATH']} && tar -xf protoc-3.15.6-win64.zip | |
os.environ['PATH'] += os.pathsep + os.path.abspath(os.path.join(paths['PROTOC_PATH'], 'bin')) | |
!cd Tensorflow/models/research && protoc object_detection/protos/*.proto --python_out=. && copy object_detection\\packages\\tf2\\setup.py setup.py && python setup.py build && python setup.py install | |
!cd Tensorflow/models/research/slim && pip install -e . | |
VERIFICATION_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'builders', 'model_builder_tf2_test.py') | |
# Verify Installation | |
!python {VERIFICATION_SCRIPT} | |
#!pip install tensorflow --upgrade | |
#!pip uninstall protobuf matplotlib -y | |
#!pip install protobuf matplotlib==3.2 | |
import object_detection | |
!pip list | |
if os.name =='posix': | |
!wget {PRETRAINED_MODEL_URL} | |
!mv {PRETRAINED_MODEL_NAME+'.tar.gz'} {paths['PRETRAINED_MODEL_PATH']} | |
!cd {paths['PRETRAINED_MODEL_PATH']} && tar -zxvf {PRETRAINED_MODEL_NAME+'.tar.gz'} | |
if os.name == 'nt': | |
wget.download(PRETRAINED_MODEL_URL) | |
!move {PRETRAINED_MODEL_NAME+'.tar.gz'} {paths['PRETRAINED_MODEL_PATH']} | |
!cd {paths['PRETRAINED_MODEL_PATH']} && tar -zxvf {PRETRAINED_MODEL_NAME+'.tar.gz'} | |
# 2. Create Label Map | |
labels = [{'name':'thumbsup', 'id':1}, {'name':'thumbsdown', 'id':2}, {'name':'thankyou', 'id':3}, {'name':'livelong', 'id':4}] | |
with open(files['LABELMAP'], 'w') as f: | |
for label in labels: | |
f.write('item { \n') | |
f.write('\tname:\'{}\'\n'.format(label['name'])) | |
f.write('\tid:{}\n'.format(label['id'])) | |
f.write('}\n') | |
# 3. Create TF records | |
# OPTIONAL IF RUNNING ON COLAB | |
ARCHIVE_FILES = os.path.join(paths['IMAGE_PATH'], 'archive.tar.gz') | |
if os.path.exists(ARCHIVE_FILES): | |
!tar -zxvf {ARCHIVE_FILES} | |
if not os.path.exists(files['TF_RECORD_SCRIPT']): | |
!git clone https://github.com/nicknochnack/GenerateTFRecord {paths['SCRIPTS_PATH']} | |
!python {files['TF_RECORD_SCRIPT']} -x {os.path.join(paths['IMAGE_PATH'], 'train')} -l {files['LABELMAP']} -o {os.path.join(paths['ANNOTATION_PATH'], 'train.record')} | |
!python {files['TF_RECORD_SCRIPT']} -x {os.path.join(paths['IMAGE_PATH'], 'test')} -l {files['LABELMAP']} -o {os.path.join(paths['ANNOTATION_PATH'], 'test.record')} | |
# 4. Copy Model Config to Training Folder | |
if os.name =='posix': | |
!cp {os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'pipeline.config')} {os.path.join(paths['CHECKPOINT_PATH'])} | |
if os.name == 'nt': | |
!copy {os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'pipeline.config')} {os.path.join(paths['CHECKPOINT_PATH'])} | |
# 5. Update Config For Transfer Learning | |
import tensorflow as tf | |
from object_detection.utils import config_util | |
from object_detection.protos import pipeline_pb2 | |
from google.protobuf import text_format | |
config = config_util.get_configs_from_pipeline_file(files['PIPELINE_CONFIG']) | |
config | |
pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() | |
with tf.io.gfile.GFile(files['PIPELINE_CONFIG'], "r") as f: | |
proto_str = f.read() | |
text_format.Merge(proto_str, pipeline_config) | |
pipeline_config.model.ssd.num_classes = len(labels) | |
pipeline_config.train_config.batch_size = 4 | |
pipeline_config.train_config.fine_tune_checkpoint = os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'checkpoint', 'ckpt-0') | |
pipeline_config.train_config.fine_tune_checkpoint_type = "detection" | |
pipeline_config.train_input_reader.label_map_path= files['LABELMAP'] | |
pipeline_config.train_input_reader.tf_record_input_reader.input_path[:] = [os.path.join(paths['ANNOTATION_PATH'], 'train.record')] | |
pipeline_config.eval_input_reader[0].label_map_path = files['LABELMAP'] | |
pipeline_config.eval_input_reader[0].tf_record_input_reader.input_path[:] = [os.path.join(paths['ANNOTATION_PATH'], 'test.record')] | |
config_text = text_format.MessageToString(pipeline_config) | |
with tf.io.gfile.GFile(files['PIPELINE_CONFIG'], "wb") as f: | |
f.write(config_text) | |
# 6. Train the model | |
TRAINING_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'model_main_tf2.py') | |
command = "python {} --model_dir={} --pipeline_config_path={} --num_train_steps=2000".format(TRAINING_SCRIPT, paths['CHECKPOINT_PATH'],files['PIPELINE_CONFIG']) | |
print(command) | |
!{command} | |
# 7. Evaluate the Model | |
command = "python {} --model_dir={} --pipeline_config_path={} --checkpoint_dir={}".format(TRAINING_SCRIPT, paths['CHECKPOINT_PATH'],files['PIPELINE_CONFIG'], paths['CHECKPOINT_PATH']) | |
print(command) | |
!{command} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment