<jupyter_start><jupyter_text>TF OD API**Workspace Setup**<jupyter_code># Create Directories Inside Workspace Direcotry
# training, data, data/train_labels, data/test_labels
!mkdir workspace workspace/training workspace/data workspace/data/train_labels workspace/data/test_labels

# Copy Training Data to Workspace
!cp -r ../input/labeled-mask-dataset-pascal-voc-format/labeled_mask_dataset/annotations workspace/data
!cp -r ../input/labeled-mask-dataset-pascal-voc-format/labeled_mask_dataset/images workspace/data

# Train Test Split

# lists the files inside 'annotations' in a random order (not really random, by their hash value instead)
# Moves the first 274/1370 labels (20% of the labels) to the testing dir: `test_labels`
!ls workspace/data/annotations/* | sort -R | head -274 | xargs -I{} mv {} workspace/data/test_labels/


# Moves the rest of the labels ( 1096 labels ) to the training dir: `train_labels`
!ls workspace/data/annotations/* | xargs -I{} mv {} workspace/data/train_labels/
import os
import glob
import xml.etree.ElementTree as ET
import pandas as pd
import tensorflow as tf
import shutil

os.getcwd()
print(len(os.listdir('workspace/data/train_labels/')), len(os.listdir('workspace/data/test_labels/')))<jupyter_output>1096 274<jupyter_text>**Creating CSV from XML and Label Map**<jupyter_code>def xml_to_csv(path):
    classes_names = []
    xml_list = []

    for xml_file in glob.glob(path + "/*.xml"):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall("object"):
            classes_names.append(member[0].text)
            value = (
                root.find("filename").text,
                int(root.find("size")[0].text),
                int(root.find("size")[1].text),
                member[0].text,
                int(member[4][0].text),
                int(member[4][1].text),
                int(member[4][2].text),
                int(member[4][3].text),
            )
            xml_list.append(value)
    column_name = ["filename", "width", "height", "class", "xmin", "ymin", "xmax", "ymax"]
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    classes_names = list(set(classes_names))
    classes_names.sort()
    return xml_df, classes_names
%cd workspace/data
for label_path in ["train_labels", "test_labels"]:
    image_path = os.path.join(os.getcwd(), label_path)
    xml_df, classes = xml_to_csv(label_path)
    xml_df.to_csv(f"{label_path}.csv", index=None)
    print(f"Successfully converted {label_path} xml to csv.")

label_map_path = os.path.join("label_map.pbtxt")
pbtxt_content = ""

for i, class_name in enumerate(classes):
    pbtxt_content = pbtxt_content + "item {{\n    id: {0}\n    name: '{1}'\n}}\n\n".format(i + 1, class_name)
pbtxt_content = pbtxt_content.strip()
with open(label_map_path, "w") as f:
    f.write(pbtxt_content)
    print("Successfully created label_map.pbtxt ")<jupyter_output>Successfully converted train_labels xml to csv.
Successfully converted test_labels xml to csv.
Successfully created label_map.pbtxt<jupyter_text>**TF OD API Installation**<jupyter_code>%%capture cap_1

%cd ../../

# clone the tensorflow models on the colab cloud vm
!git clone --q https://github.com/tensorflow/models.git

#navigate to /models/research folder to compile protos
%cd models/research

# Compile protos.
!protoc object_detection/protos/*.proto --python_out=.

# Install TensorFlow Object Detection API.
!cp object_detection/packages/tf2/setup.py .
!python -m pip install .

# testing the model builder
!python object_detection/builders/model_builder_tf2_test.py<jupyter_output><empty_output><jupyter_text>**Generating TF Records**<jupyter_code>%cd ../../workspace/data
#Usage:  
#!python generate_tfrecord.py output.csv output_pb.txt /path/to/images output.tfrecords

#For train.record
!python ../../../input/tf-od-api-helper-scripts/generate_tfrecord.py train_labels.csv  label_map.pbtxt images/ train.record

#For test.record
!python ../../../input/tf-od-api-helper-scripts/generate_tfrecord.py test_labels.csv  label_map.pbtxt images/ test.record<jupyter_output>groups: 100%|█████████████████████████████| 1096/1096 [00:00<00:00, 1181.52it/s]
Successfully created the TFRecords: /kaggle/working/workspace/data/train.record
groups: 100%|███████████████████████████████| 274/274 [00:00<00:00, 1146.07it/s]
Successfully created the TFRecords: /kaggle/working/workspace/data/test.record<jupyter_text>**Downloading Pretrained Model Checkpoint**[Checkpoints](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md)<jupyter_code>!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz
!tar -xzvf ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz<jupyter_output>--2022-04-23 04:59:49--  http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz
Resolving download.tensorflow.org (download.tensorflow.org)... 192.168.127.12, fc00:db20:35b:7399::5
Connecting to download.tensorflow.org (download.tensorflow.org)|192.168.127.12|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20515344 (20M) [application/x-tar]
Saving to: ‘ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz’

ssd_mobilenet_v2_fp 100%[===================>]  19.56M  78.5MB/s    in 0.2s    

2022-04-23 04:59:50 (78.5 MB/s) - ‘ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz’ saved [20515344/20515344]

ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/
ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint/
ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint/ckpt-0.data-00000-of-00001
ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint/checkpoint
ssd_mobilenet_v2_fpnlite_32[...]<jupyter_text>**Model Config**You need to make the following changes:* change num_classes to number of your classes.* change test.record path, train.record path & labelmap path to the paths where you have created these files (paths should be relative to your current working directory while training).* change fine_tune_checkpoint to the path of the directory where the downloaded checkpoint is.* change fine_tune_checkpoint_type with value classification or detection depending on the type..* change batch_size to any multiple of 8 depending upon the capability of your GPU. (eg:- 24,128,...,512).* change num_steps to number of steps you want the detector to train.<jupyter_code># !cat ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/pipeline.config
%%writefile ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config
model {
  ssd {
    num_classes: 2
    image_resizer {
      fixed_shape_resizer {
        height: 320
        width: 320
      }
    }
    feature_extractor {
      type: "ssd_mobilenet_v2_fpn_keras"
      depth_multiplier: 1.0
      min_depth: 16
      conv_hyperparams {
        regularizer {
          l2_regularizer {
            weight: 3.9999998989515007e-05
          }
        }
        initializer {
          random_normal_initializer {
            mean: 0.0
            stddev: 0.009999999776482582
          }
        }
        activation: RELU_6
        batch_norm {
          decay: 0.996999979019165
          scale: true
          epsilon: 0.0010000000474974513
        }
      }
      use_depthwise: true
      override_base_feature_extractor_hyperparams: true
      fpn {
        min_level: 3
        max_level: 7
        additional_layer_depth: 128
      }
    }
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
        use_matmul_gather: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    box_predictor {
      weight_shared_convolutional_box_predictor {
        conv_hyperparams {
          regularizer {
            l2_regularizer {
              weight: 3.9999998989515007e-05
            }
          }
          initializer {
            random_normal_initializer {
              mean: 0.0
              stddev: 0.009999999776482582
            }
          }
          activation: RELU_6
          batch_norm {
            decay: 0.996999979019165
            scale: true
            epsilon: 0.0010000000474974513
          }
        }
        depth: 128
        num_layers_before_predictor: 4
        kernel_size: 3
        class_prediction_bias_init: -4.599999904632568
        share_prediction_tower: true
        use_depthwise: true
      }
    }
    anchor_generator {
      multiscale_anchor_generator {
        min_level: 3
        max_level: 7
        anchor_scale: 4.0
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        scales_per_octave: 2
      }
    }
    post_processing {
      batch_non_max_suppression {
        score_threshold: 9.99999993922529e-09
        iou_threshold: 0.6000000238418579
        max_detections_per_class: 100
        max_total_detections: 100
        use_static_shapes: false
      }
      score_converter: SIGMOID
    }
    normalize_loss_by_num_matches: true
    loss {
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      classification_loss {
        weighted_sigmoid_focal {
          gamma: 2.0
          alpha: 0.25
        }
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    encode_background_as_zeros: true
    normalize_loc_loss_by_codesize: true
    inplace_batchnorm_update: true
    freeze_batchnorm: false
  }
}
train_config {
  batch_size: 8
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    random_crop_image {
      min_object_covered: 0.0
      min_aspect_ratio: 0.75
      max_aspect_ratio: 3.0
      min_area: 0.75
      max_area: 1.0
      overlap_thresh: 0.0
    }
  }
  sync_replicas: true
  optimizer {
    momentum_optimizer {
      learning_rate {
        cosine_decay_learning_rate {
          learning_rate_base: 0.07999999821186066
          total_steps: 50000
          warmup_learning_rate: 0.026666000485420227
          warmup_steps: 1000
        }
      }
      momentum_optimizer_value: 0.8999999761581421
    }
    use_moving_average: false
  }
  fine_tune_checkpoint: "/kaggle/working/workspace/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint/ckpt-0"
  num_steps: 10_000
  startup_delay_steps: 0.0
  replicas_to_aggregate: 8
  max_number_of_boxes: 100
  unpad_groundtruth_tensors: false
  fine_tune_checkpoint_type: "detection"
  fine_tune_checkpoint_version: V2
}
train_input_reader {
  label_map_path: "/kaggle/working/workspace/data/label_map.pbtxt"
  tf_record_input_reader {
    input_path: "/kaggle/working/workspace/data/train.record"
  }
}
eval_config {
  metrics_set: "coco_detection_metrics"
  use_moving_averages: false
}
eval_input_reader {
  label_map_path: "/kaggle/working/workspace/data/label_map.pbtxt"
  shuffle: false
  num_epochs: 1
  tf_record_input_reader {
    input_path: "/kaggle/working/workspace/data/test.record"
  }
}<jupyter_output>Writing ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config<jupyter_text>**Training**<jupyter_code>%cd ../../models/research/object_detection
%%capture cap_2

!python model_main_tf2.py --pipeline_config_path=../../../workspace/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config --model_dir=../../../workspace/training --alsologtostderr<jupyter_output><empty_output><jupyter_text>**Evaluating**<jupyter_code># !python model_main_tf2.py --pipeline_config_path=../../../workspace/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config --model_dir=../../../workspace/training/ --checkpoint_dir=../../../workspace/training/ --alsologtostderr<jupyter_output><empty_output><jupyter_text>**Export and Inference**<jupyter_code>%%capture cap_3

!python exporter_main_v2.py --trained_checkpoint_dir=../../../workspace/training --pipeline_config_path=../../../workspace/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config --output_directory ../../../workspace/data/inference_graph
import tensorflow as tf
import time
import numpy as np
import warnings
import cv2
warnings.filterwarnings('ignore')
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
IMAGE_SIZE = (12, 8)
PATH_TO_SAVED_MODEL="../../../workspace/data/inference_graph/saved_model"
print('Loading model...', end='')

detect_fn=tf.saved_model.load(PATH_TO_SAVED_MODEL)
print('Done!')

def load_image_into_numpy_array(path):
    return np.array(Image.open(path))

image_path = "../../../workspace/data/images/134.jpg"
image_np = load_image_into_numpy_array(image_path)
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]

detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
              for key, value in detections.items()}
detections['num_detections'] = num_detections
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
for class_idx in [1, 2]:
    image_np_with_detections = image_np.copy()
    for idx in range(num_detections):
        if detections['detection_classes'][idx] == class_idx:
            xmin, xmax, ymin, ymax = bbox = detections['detection_boxes'][idx]
            xmin, xmax, ymin, ymax = int(xmin*320), int(xmax*320), int(ymin*428), int(ymax*428)
            cv2.rectangle(image_np_with_detections, (xmin, ymin), (xmax, ymax), (255,0, 0))
    plt.figure(figsize=IMAGE_SIZE)
    plt.axis("off")
    plt.suptitle(f'Class {class_idx}')
    plt.imshow(image_np_with_detections)
%cd ../../../workspace
with open('installation.log', 'w') as f: f.write(cap_1.stdout)
with open('training.log', 'w') as f: f.write(cap_2.stdout)
with open('export.log', 'w') as f: f.write(cap_3.stdout)<jupyter_output>/kaggle/working/workspace
{'fraction_non_alphanumeric': 0.11317543610835343, 'fraction_numerical': 0.056658629981562895, 'mean_word_length': 3.3989394884591393, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 2, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '600275', 'license': 'MIT', 'n_tokens_mistral': 5561, 'n_tokens_neox': 4956, 'n_words': 858, 'path': 'TF-OD-API.ipynb', 'repo_name': 'Ritvik19/Implemented-Data-Science', 'size': '14105'}