<jupyter_start><jupyter_text>Mask R-CNN Training (Demo) - DEEPCADDThis notebook shows how to train Mask R-CNN on your own dataset.This is a demo version which allows you to train your detector for 1 class.The full tutorial is available here: [Tutorial](https://pysource.com/2021/08/10/train-mask-r-cnn-for-image-segmentation-online-free-gpu)  **1. Installation**Load your dataset<jupyter_code>%tensorflow_version 1.x
!pip install --upgrade h5py==2.10.0
!git clone https://github.com/armbrustsamuel/deepcadd.git
import sys
sys.path.append("/content/deepcadd/demo")
from train_mask_rcnn_demo import *
%matplotlib inline
!pip install Ipython --upgrade
%load_ext autoreload
%autoreload 2
# !nvidia-smi<jupyter_output><empty_output><jupyter_text>**2. Image Dataset**Load your annotated dataset<jupyter_code>from google.colab import drive
drive.mount('/content/drive' )
# Extract Images
# images_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/dataset.zip"
images_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/debora/dataset.zip"

# annotated by me
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/coco-annotation.json"
# annotated by Debora
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/coco-debora-annotation.json"
annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/debora/coco-debora-annotation.json"
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/debora/coco-debora-annotation-2.json"


# annotated but isolating not processed data
# images_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/82images-rcnn/dataset.zip"
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/82images-rcnn/coco-debora-annotation.json"

# images_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/dataset-separation/narrowing/dataset.zip"
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/dataset-separation/narrowing/coco-annotation-narrow.json"

# images_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/dataset-separation/grayscale/dataset.zip"
# annotations_path = "/content/drive/MyDrive/Colab Notebooks/RCNN/dataset-separation/grayscale/coco-annotation-grayscale.json"

extract_images(os.path.join("/content/",images_path), "/content/dataset")
dataset_train = load_image_dataset(os.path.join("/content/", annotations_path), "/content/dataset", "train")
dataset_val = load_image_dataset(os.path.join("/content/", annotations_path), "/content/dataset", "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>%cd "deepcadd"
!git pull origin main
%cd ..
# Load Configuration
config = CustomConfig(class_number)

config.CAD_KERNEL = True
#config.display()
model = load_training_model(config)
# Start Training
# This operation might take a long time.
train_head(model, dataset_train, dataset_train, config)
# Custom training hyperparameters
model.train(dataset_train, dataset_val,
            learning_rate=config.LEARNING_RATE,
            epochs=7,
            layers='heads')
# Load Test Model
# The latest trained model will be loaded
test_model, inference_config = load_test_model(class_number)<jupyter_output>WARNING:tensorflow:From /content/deepcadd/mrcnn/model.py:720: The name tf.sets.set_intersection is deprecated. Please use tf.sets.intersection instead.

WARNING:tensorflow:From /content/deepcadd/mrcnn/model.py:722: The name tf.sparse_tensor_to_dense is deprecated. Please use tf.sparse.to_dense instead.

WARNING:tensorflow:From /content/deepcadd/mrcnn/model.py:772: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.cast` instead.
Loading weights from  /content/deepcadd/logs/object20211227T2151/mask_rcnn_object_0005.h5
Re-starting from epoch 5<jupyter_text>**4. Detection (test your model on a random image)**<jupyter_code># Test on a random image
test_random_image(test_model, dataset_val, inference_config)<jupyter_output>original_image           shape: (512, 512, 3)         min:    0.00000  max:  246.00000  uint8
Trained model result
Processing 1 images
image                    shape: (512, 512, 3)         min:    0.00000  max:  246.00000  uint8
molded_images            shape: (1, 512, 512, 3)      min: -123.70000  max:  142.10000  float64
image_metas              shape: (1, 14)               min:    0.00000  max:  512.00000  int64
anchors                  shape: (1, 65472, 4)         min:   -0.70849  max:    1.58325  float32
Annotation<jupyter_text>Testing the model<jupyter_code>from mrcnn.visualize import random_colors, get_mask_contours, draw_mask
from google.colab.patches import cv2_imshow

dir_root = "/content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/imagens"
# dir_root = "/content/drive/MyDrive/Colab Notebooks/RCNN/91imagens-wo-stenosis"

def print_default(filename, print_box=True):
  img = cv2.imread(filename)

  image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

  # Detect results
  r = test_model.detect([image])[0]
  if print_box == True:
    visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                                dataset_val.class_names, r['scores'], ax=get_ax(), show_bbox=True)
  else:
    visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                                dataset_val.class_names, r['scores'], ax=get_ax(), show_bbox=False)


def print_image(filename):
  img = cv2.imread(filename)

  image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

  # Detect results
  r = test_model.detect([image])[0]

  # Get Coordinates and show it on the image
  object_count = len(r["class_ids"])
  if object_count != 0:
    colors = random_colors(object_count)
  else:
    print("No stenosis found for img at: " + filename)
    return
  for i in range(object_count):
      # 1. Mask
      mask = r["masks"][:, :, i]
      contours = get_mask_contours(mask)
      for cnt in contours:
          cv2.polylines(img, [cnt], True, colors[i], 2)
          img = draw_mask(img, [cnt], colors[i])

  cv2_imshow(img)
for filename in os.listdir(dir_root):
    if filename.endswith("png"): 
      # Your code comes here such as 
      print(filename)
      print_image(dir_root+"/"+filename)
      # print_default(dir_root+"/"+filename)
# Load Image
# img = cv2.imread("/content/img-12.png") 
img_filename = "/content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/imagens/img-104.png"

print_image(img_filename)<jupyter_output>No stenosis found for img at: /content/drive/MyDrive/Colab Notebooks/RCNN/122images-rcnn/imagens/img-104.png<jupyter_text>Evaluation<jupyter_code>from os import listdir
from xml.etree import ElementTree
from numpy import zeros
from numpy import asarray
from numpy import expand_dims
from numpy import mean
from mrcnn.config import Config
from mrcnn.model import MaskRCNN
from mrcnn.utils import Dataset
from mrcnn.utils import compute_ap, compute_recall
from mrcnn.model import load_image_gt
from mrcnn.model import mold_image
# from train_mask_rcnn_pro import *

def model_evaluation_custom(dataset_val, test_model, inference_config):
    APs = list(); 
    ARs = list();
    F1_scores = list();  
    print("Testing the model on {} validation images.".format(len(dataset_val.image_ids)))
    for image_id in dataset_val.image_ids:
        # Load image and ground truth data
        image, image_meta, gt_class_id, gt_bbox, gt_mask = \
            modellib.load_image_gt(dataset_val, inference_config,
                                   image_id, use_mini_mask=False)
        molded_images = np.expand_dims(modellib.mold_image(image, inference_config), 0)
        # Run object detection
        results = test_model.detect([image], verbose=0)
        r = results[0]
        # Compute AP
        AP, precisions, recalls, overlaps = \
            utils.compute_ap(gt_bbox, gt_class_id, gt_mask,
                             r["rois"], r["class_ids"], r["scores"], r['masks'])
        AR, positive_ids = compute_recall(r["rois"], gt_bbox, iou=0.2)
        ARs.append(AR)
        F1_scores.append((2* (mean(precisions) * mean(recalls)))/(mean(precisions) + mean(recalls)))
        APs.append(AP)
    mAP = mean(APs)
    mAR = mean(ARs)
    return mAP, mAR, F1_scores

mAP, mAR, F1_score = model_evaluation_custom(dataset_val, test_model, inference_config)
print("mAP: %.3f" % mAP)
print("mAR: %.3f" % mAR)

F1_score_2 = (2 * mAP * mAR)/(mAP + mAR)
print('f1-score: ', F1_score_2)<jupyter_output>Testing the model on 14 validation images.
mAP: 0.156
mAR: 0.554
f1-score:  0.24386581965284007<jupyter_text>Additional - can be hidden<jupyter_code># source: https://github.com/matterport/Mask_RCNN/issues/2165



def evaluate_model(dataset, model, cfg):
  APs = list(); 
  ARs = list();
  F1_scores = list(); 
  for image_id in dataset.image_ids:
    image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt(dataset, cfg, image_id, use_mini_mask=False)
    scaled_image = mold_image(image, cfg)
    sample = expand_dims(scaled_image, 0)
    yhat = model.detect(sample, verbose=0)
    r = yhat[0]
    AP, precisions, recalls, overlaps = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'])
    AR, positive_ids = compute_recall(r["rois"], gt_bbox, iou=0.2)
    ARs.append(AR)
    F1_scores.append((2* (mean(precisions) * mean(recalls)))/(mean(precisions) + mean(recalls)))
    APs.append(AP)
  mAP = mean(APs)
  mAR = mean(ARs)
  return mAP, mAR, F1_scores

mAP, mAR, F1_score = evaluate_model(dataset_val, test_model, inference_config)
print("mAP: %.3f" % mAP)
print("mAR: %.3f" % mAR)
print("first way calculate f1-score: ", F1_score)

F1_score_2 = (2 * mAP * mAR)/(mAP + mAR)
print('second way calculate f1-score_2: ', F1_score_2)<jupyter_output>mAP: 0.000
mAR: 0.143
first way calculate f1-score:  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
second way calculate f1-score_2:  0.0
{'fraction_non_alphanumeric': 0.12207587969333596, 'fraction_numerical': 0.030273245527816, 'mean_word_length': 4.277489626556017, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '42617', 'license': 'MIT', 'n_tokens_mistral': 3827, 'n_tokens_neox': 3540, 'n_words': 743, 'path': 'colab/DeepCADD-training-with-ResNet50.ipynb', 'repo_name': 'armbrustsamuel/deepcadd', 'size': '10174'}