{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "146BB11JpfDA" }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "42hJEdo_pfDB" }, "outputs": [], "source": [ "CUSTOM_MODEL_NAME = 'my_ssd_mobnet' \n", "PRETRAINED_MODEL_NAME = 'ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8'\n", "PRETRAINED_MODEL_URL = 'http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz'\n", "TF_RECORD_SCRIPT_NAME = 'generate_tfrecord.py'\n", "LABEL_MAP_NAME = 'label_map.pbtxt'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hbPhYVy_pfDB" }, "outputs": [], "source": [ "paths = {\n", " 'WORKSPACE_PATH': os.path.join('Tensorflow', 'workspace'),\n", " 'SCRIPTS_PATH': os.path.join('Tensorflow','scripts'),\n", " 'APIMODEL_PATH': os.path.join('Tensorflow','models'),\n", " 'ANNOTATION_PATH': os.path.join('Tensorflow', 'workspace','annotations'),\n", " 'IMAGE_PATH': os.path.join('Tensorflow', 'workspace','images'),\n", " 'MODEL_PATH': os.path.join('Tensorflow', 'workspace','models'),\n", " 'PRETRAINED_MODEL_PATH': os.path.join('Tensorflow', 'workspace','pre-trained-models'),\n", " 'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME), \n", " 'OUTPUT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'export'), \n", " 'TFJS_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfjsexport'), \n", " 'TFLITE_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfliteexport'), \n", " 'PROTOC_PATH':os.path.join('Tensorflow','protoc')\n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "LwhWZMI0pfDC" }, "outputs": [], "source": [ "files = {\n", " 'PIPELINE_CONFIG':os.path.join('Tensorflow', 'workspace','models', CUSTOM_MODEL_NAME, 'pipeline.config'),\n", " 'TF_RECORD_SCRIPT': os.path.join(paths['SCRIPTS_PATH'], TF_RECORD_SCRIPT_NAME), \n", " 'LABELMAP': os.path.join(paths['ANNOTATION_PATH'], LABEL_MAP_NAME)\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "HR-TfDGrpfDC" }, "outputs": [], "source": [ "for path in paths.values():\n", " if not os.path.exists(path):\n", " if os.name == 'posix':\n", " !mkdir -p {path}\n", " if os.name == 'nt':\n", " !mkdir {path}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "K-Cmz2edpfDE", "scrolled": true }, "outputs": [], "source": [ "if os.name=='nt':\n", " !pip install wget\n", " import wget" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "iA1DIq5OpfDE" }, "outputs": [], "source": [ "if not os.path.exists(os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection')):\n", " !git clone https://github.com/tensorflow/models {paths['APIMODEL_PATH']}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "rJjMHbnDs3Tv" }, "outputs": [], "source": [ "# Install Tensorflow Object Detection \n", "if os.name=='posix': \n", " !apt-get install protobuf-compiler\n", " !cd Tensorflow/models/research && protoc object_detection/protos/*.proto --python_out=. && cp object_detection/packages/tf2/setup.py . && python -m pip install . \n", " \n", "if os.name=='nt':\n", " url=\"https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/protoc-3.15.6-win64.zip\"\n", " wget.download(url)\n", " !move protoc-3.15.6-win64.zip {paths['PROTOC_PATH']}\n", " !cd {paths['PROTOC_PATH']} && tar -xf protoc-3.15.6-win64.zip\n", " os.environ['PATH'] += os.pathsep + os.path.abspath(os.path.join(paths['PROTOC_PATH'], 'bin')) \n", " !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\n", " !cd Tensorflow/models/research/slim && pip install -e . " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "VERIFICATION_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'builders', 'model_builder_tf2_test.py')\n", "# Verify Installation\n", "!python {VERIFICATION_SCRIPT}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import object_detection" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "!pip list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "csofht2npfDE", "outputId": "ff5471b2-bed2-43f2-959c-327a706527b6" }, "outputs": [], "source": [ "if os.name =='posix':\n", " !wget {PRETRAINED_MODEL_URL}\n", " !mv {PRETRAINED_MODEL_NAME+'.tar.gz'} {paths['PRETRAINED_MODEL_PATH']}\n", " !cd {paths['PRETRAINED_MODEL_PATH']} && tar -zxvf {PRETRAINED_MODEL_NAME+'.tar.gz'}\n", "if os.name == 'nt':\n", " wget.download(PRETRAINED_MODEL_URL)\n", " !move {PRETRAINED_MODEL_NAME+'.tar.gz'} {paths['PRETRAINED_MODEL_PATH']}\n", " !cd {paths['PRETRAINED_MODEL_PATH']} && tar -zxvf {PRETRAINED_MODEL_NAME+'.tar.gz'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "p1BVDWo7pfDC" }, "outputs": [], "source": [ "labels = [{'name':'Density1Benign', 'id':1}, {'name':'Density1Malignant', 'id':2}]\n", "\n", "with open(files['LABELMAP'], 'w') as f:\n", " for label in labels:\n", " f.write('item { \\n')\n", " f.write('\\tname:\\'{}\\'\\n'.format(label['name']))\n", " f.write('\\tid:{}\\n'.format(label['id']))\n", " f.write('}\\n')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KWpb_BVUpfDD", "outputId": "56ce2a3f-3933-4ee6-8a9d-d5ec65f7d73c" }, "outputs": [], "source": [ "if not os.path.exists(files['TF_RECORD_SCRIPT']):\n", " !git clone https://github.com/nicknochnack/GenerateTFRecord {paths['SCRIPTS_PATH']}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UPFToGZqpfDD", "outputId": "0ebb456f-aadc-4a1f-96e6-fbfec1923e1c" }, "outputs": [], "source": [ "!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')} \n", "!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')} " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cOjuTFbwpfDF" }, "outputs": [], "source": [ "if os.name =='posix':\n", " !cp {os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'pipeline.config')} {os.path.join(paths['CHECKPOINT_PATH'])}\n", "if os.name == 'nt':\n", " !copy {os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'pipeline.config')} {os.path.join(paths['CHECKPOINT_PATH'])}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Z9hRrO_ppfDF" }, "outputs": [], "source": [ "import tensorflow as tf\n", "from object_detection.utils import config_util\n", "from object_detection.protos import pipeline_pb2\n", "from google.protobuf import text_format" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "c2A0mn4ipfDF" }, "outputs": [], "source": [ "config = config_util.get_configs_from_pipeline_file(files['PIPELINE_CONFIG'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uQA13-afpfDF", "outputId": "907496a4-a39d-4b13-8c2c-e5978ecb1f10" }, "outputs": [], "source": [ "config" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9vK5lotDpfDF" }, "outputs": [], "source": [ "pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()\n", "with tf.io.gfile.GFile(files['PIPELINE_CONFIG'], \"r\") as f: \n", " proto_str = f.read() \n", " text_format.Merge(proto_str, pipeline_config) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "rP43Ph0JpfDG" }, "outputs": [], "source": [ "pipeline_config.model.ssd.num_classes = len(labels)\n", "pipeline_config.train_config.batch_size = 4\n", "pipeline_config.train_config.fine_tune_checkpoint = os.path.join(paths['PRETRAINED_MODEL_PATH'], PRETRAINED_MODEL_NAME, 'checkpoint', 'ckpt-0')\n", "pipeline_config.train_config.fine_tune_checkpoint_type = \"detection\"\n", "pipeline_config.train_input_reader.label_map_path= files['LABELMAP']\n", "pipeline_config.train_input_reader.tf_record_input_reader.input_path[:] = [os.path.join(paths['ANNOTATION_PATH'], 'train.record')]\n", "pipeline_config.eval_input_reader[0].label_map_path = files['LABELMAP']\n", "pipeline_config.eval_input_reader[0].tf_record_input_reader.input_path[:] = [os.path.join(paths['ANNOTATION_PATH'], 'test.record')]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "oJvfgwWqpfDG" }, "outputs": [], "source": [ "config_text = text_format.MessageToString(pipeline_config) \n", "with tf.io.gfile.GFile(files['PIPELINE_CONFIG'], \"wb\") as f: \n", " f.write(config_text) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "B-Y2UQmQpfDG" }, "outputs": [], "source": [ "TRAINING_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'model_main_tf2.py')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "jMP2XDfQpfDH" }, "outputs": [], "source": [ "command = \"python {} --model_dir={} --pipeline_config_path={} --num_train_steps=2000\".format(TRAINING_SCRIPT, paths['CHECKPOINT_PATH'],files['PIPELINE_CONFIG'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "A4OXXi-ApfDH", "outputId": "117a0e83-012b-466e-b7a6-ccaa349ac5ab" }, "outputs": [], "source": [ "print(command)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "i3ZsJR-qpfDH", "outputId": "cabec5e1-45e6-4f2f-d9cf-297d9c1d0225" }, "outputs": [], "source": [ "!{command}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "80L7-fdPpfDH" }, "outputs": [], "source": [ "command = \"python {} --model_dir={} --pipeline_config_path={} --checkpoint_dir={}\".format(TRAINING_SCRIPT, paths['CHECKPOINT_PATH'],files['PIPELINE_CONFIG'], paths['CHECKPOINT_PATH'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lYsgEPx9pfDH", "outputId": "8632d48b-91d2-45d9-bcb8-c1b172bf6eed" }, "outputs": [], "source": [ "print(command)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lqTV2jGBpfDH" }, "outputs": [], "source": [ "!{command}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "8TYk4_oIpfDI" }, "outputs": [], "source": [ "import os\n", "import tensorflow as tf\n", "from object_detection.utils import label_map_util\n", "from object_detection.utils import visualization_utils as viz_utils\n", "from object_detection.builders import model_builder\n", "from object_detection.utils import config_util" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "tDnQg-cYpfDI" }, "outputs": [], "source": [ "# Load pipeline config and build a detection model\n", "configs = config_util.get_configs_from_pipeline_file(files['PIPELINE_CONFIG'])\n", "detection_model = model_builder.build(model_config=configs['model'], is_training=False)\n", "\n", "# Restore checkpoint\n", "ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)\n", "ckpt.restore(os.path.join(paths['CHECKPOINT_PATH'], 'ckpt-9')).expect_partial()\n", "\n", "@tf.function\n", "def detect_fn(image):\n", " image, shapes = detection_model.preprocess(image)\n", " prediction_dict = detection_model.predict(image, shapes)\n", " detections = detection_model.postprocess(prediction_dict, shapes)\n", " return detections" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Y_MKiuZ4pfDI" }, "outputs": [], "source": [ "import cv2 \n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cBDbIhNapfDI" }, "outputs": [], "source": [ "category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Lx3crOhOzITB" }, "outputs": [], "source": [ "IMAGE_PATH = os.path.join(paths['IMAGE_PATH'], 'test', '20587612 (36).png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "Tpzn1SMry1yK", "outputId": "c392a2c5-10fe-4fc4-9998-a1d4c7db2bd3" }, "outputs": [], "source": [ "img = cv2.imread(IMAGE_PATH)\n", "image_np = np.array(img)\n", "\n", "input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)\n", "detections = detect_fn(input_tensor)\n", "\n", "num_detections = int(detections.pop('num_detections'))\n", "detections = {key: value[0, :num_detections].numpy()\n", " for key, value in detections.items()}\n", "detections['num_detections'] = num_detections\n", "\n", "# detection_classes should be ints.\n", "detections['detection_classes'] = detections['detection_classes'].astype(np.int64)\n", "\n", "label_id_offset = 1\n", "image_np_with_detections = image_np.copy()\n", "\n", "viz_utils.visualize_boxes_and_labels_on_image_array(\n", " image_np_with_detections,\n", " detections['detection_boxes'],\n", " detections['detection_classes']+label_id_offset,\n", " detections['detection_scores'],\n", " category_index,\n", " use_normalized_coordinates=True,\n", " max_boxes_to_draw=5,\n", " min_score_thresh=.2,\n", " agnostic_mode=False)\n", "\n", "plt.imshow(cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB))\n", "plt.show()" ] } ], "metadata": { "accelerator": "GPU", "colab": { "name": "3. Training and Detection.ipynb", "provenance": [] }, "kernelspec": { "display_name": "hamza1", "language": "python", "name": "hamza1" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.0" } }, "nbformat": 4, "nbformat_minor": 4 }