<jupyter_start><jupyter_text>**1. Installation**Load your dataset<jupyter_code>%tensorflow_version 1.x
!pip install --upgrade h5py==2.10.0
!git clone https://github.com/seel-channel/AMPD_Mask_RCNN
%matplotlib inline
import sys
sys.path.append("/content/AMPD_Mask_RCNN")
from train_mask_rcnn import *
!nvidia-smi<jupyter_output>rm: cannot remove 'Mask_RCNN/': Is a directory<jupyter_text>**2. Image Dataset**Load your annotated dataset<jupyter_code>from google.colab import drive
drive.mount('/content/gdrive')
# Extract Images
drive_folder = "/content/gdrive/MyDrive/ampd/"
cache_folder = "/content/dataset/"
test_folder = cache_folder + "test/"
train_folder = cache_folder + "train/"

images_folder = "images/"
generated_folder = "generated/"
annotations_filename = "coco_annotations.json"

train_annotations_path = train_folder + annotations_filename
test_annotations_path = test_folder + annotations_filename

print("Extracting: train")
extract_images(drive_folder + "dataset_train.zip", train_folder)
print("Extracting: test")
extract_images(drive_folder + "dataset_test.zip", test_folder)
import json

def open_json(path: str):
    with open(path) as f:
        return json.loads(f.read())

def save_json(dict: dict, path: str):
    with open(path, 'w+') as f:
        json.dump(dict, f)

def remove_unused_images(annotations_path):
  coco_json = open_json(annotations_path)
  used_images = []
  images = []

  for annotation in coco_json['annotations']:
    image_id = annotation['image_id']
    if not image_id in used_images:
        used_images.append(image_id)
  
  for image in coco_json['images']:
    image_id = image['id']
    if image_id in used_images:
      images.append(image)

  coco_json['images'] = images
  save_json(coco_json, annotations_path)

# Ignore images without annotations
print("Removing train unused images...")
remove_unused_images(train_annotations_path)
print("Removing test unused images...")
remove_unused_images(test_annotations_path)
dataset_train = load_image_dataset(train_annotations_path, train_folder, "train")
dataset_val = load_image_dataset(test_annotations_path,  test_folder, "val")
class_number = dataset_train.count_classes()
print('Train: %d' % len(dataset_train.image_ids))
print('Validation: %d' % len(dataset_val.image_ids))
print("Classes: {}".format(class_number))
# Load image samples
display_image_samples(dataset_train)<jupyter_output><empty_output><jupyter_text>**3. Training**Train Mask RCNN on your custom Dataset.<jupyter_code># Load Configuration
import os

def make_folders(folders: list, remove=False):
    for path in folders:
        if not os.path.exists(path):
            os.makedirs(path)
        elif(remove):
            os.remove(path)

model_dir = drive_folder + "pretrained/"
make_folders([model_dir])

config = CustomConfig(class_number)
model = load_training_model(config, model_dir)
# Start Training. This operation might take a long time.
train_head(model, dataset_train, dataset_train, config, epochs=10)<jupyter_output>Starting at epoch 0. LR=0.001

Checkpoint Path: /content/gdrive/MyDrive/ampd/model/object20211122T0339/mask_rcnn_object_{epoch:04d}.h5
Selecting layers to train
fpn_c5p5               (Conv2D)
fpn_c4p4               (Conv2D)
fpn_c3p3               (Conv2D)
fpn_c2p2               (Conv2D)
fpn_p5                 (Conv2D)
fpn_p2                 (Conv2D)
fpn_p3                 (Conv2D)
fpn_p4                 (Conv2D)
In model:  rpn_model
    rpn_conv_shared        (Conv2D)
    rpn_class_raw          (Conv2D)
    rpn_bbox_pred          (Conv2D)
mrcnn_mask_conv1       (TimeDistributed)
mrcnn_mask_bn1         (TimeDistributed)
mrcnn_mask_conv2       (TimeDistributed)
mrcnn_mask_bn2         (TimeDistributed)
mrcnn_class_conv1      (TimeDistributed)
mrcnn_class_bn1        (TimeDistributed)
mrcnn_mask_conv3       (TimeDistributed)
mrcnn_mask_bn3         (TimeDistributed)
mrcnn_class_conv2      (TimeDistributed)
mrcnn_class_bn2        (TimeDistributed)
mrcnn_mask_conv4       (TimeDistributed)
mr[...]<jupyter_text>**4. Detection (test your model on a random image)**<jupyter_code># Load the latest trained model will be loaded
test_model, inference_config = load_test_model(class_number, model_dir)
# Test on a random image
test_random_image(test_model, dataset_val, inference_config)<jupyter_output>original_image           shape: (832, 832, 3)         min:    0.00000  max:  255.00000  uint8
Trained model result
Processing 1 images
image                    shape: (832, 832, 3)         min:    0.00000  max:  255.00000  uint8
molded_images            shape: (1, 832, 832, 3)      min: -123.70000  max:  151.10000  float64
image_metas              shape: (1, 15)               min:    0.00000  max:  832.00000  int64
anchors                  shape: (1, 172887, 4)        min:   -0.43567  max:    1.35865  float32
Annotation
{'fraction_non_alphanumeric': 0.09865563598759049, 'fraction_numerical': 0.03681489141675284, 'mean_word_length': 3.9397344228804902, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '502383', 'license': 'MIT', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1626, 'n_words': 354, 'path': 'Train_Mask_RCNN_(AMPD).ipynb', 'repo_name': 'seel-channel/Mask_RCNN', 'size': '4835'}