<jupyter_start><jupyter_text>Inspection of the Training  dataInspect and visualize the training images and the corresponding ground truth data  Installing and getting the crucial software libraries<jupyter_code>!pip install imgaug
!pip install Cython
!pip install pycocotools


import time
import cv2
import datetime
import skimage.draw
import os
import sys
import itertools
import math
import logging
import json
import re
import random
from collections import OrderedDict
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.lines as lines
from matplotlib.patches import Polygon

import warnings
warnings.filterwarnings('ignore')

%matplotlib inline<jupyter_output>Requirement already satisfied: imgaug in /usr/local/lib/python3.6/dist-packages (0.2.9)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from imgaug) (3.0.3)
Requirement already satisfied: imageio in /usr/local/lib/python3.6/dist-packages (from imgaug) (2.4.1)
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from imgaug) (1.3.0)
Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from imgaug) (4.3.0)
Requirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from imgaug) (1.16.4)
Requirement already satisfied: scikit-image>=0.11.0 in /usr/local/lib/python3.6/dist-packages (from imgaug) (0.15.0)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from imgaug) (1.12.0)
Requirement already satisfied: Shapely in /usr/local/lib/python3.6/dist-packages (from imgaug) (1.6.4.post2)
Requirement already satisfied: opencv-python in /usr/local/lib/p[...]<jupyter_text>Download the workshop git repo to access the mask-rcnn software library and the dataset<jupyter_code>os.chdir('/content')
!rm -rf Mask_RCNN
!git clone https://github.com/aneeshchauhan/Mask_RCNN<jupyter_output>Cloning into 'Mask_RCNN'...
remote: Enumerating objects: 4, done.[K
remote: Counting objects: 100% (4/4), done.[K
remote: Compressing objects: 100% (4/4), done.[K
remote: Total 2706 (delta 0), reused 1 (delta 0), pack-reused 2702[K
Receiving objects: 100% (2706/2706), 119.58 MiB | 16.06 MiB/s, done.
Resolving deltas: 100% (610/610), done.
Checking out files: 100% (1720/1720), done.<jupyter_text>**Change to the workshop working directory**<jupyter_code>os.chdir('/content/Mask_RCNN/samples')
!pwd<jupyter_output>/content/Mask_RCNN/samples<jupyter_text>**Setup to start using the library**<jupyter_code># Root directory of the project
ROOT_DIR = os.path.abspath("../")
print(ROOT_DIR)
# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library

from mrcnn import utils
from mrcnn import visualize
from mrcnn.visualize import display_images
import mrcnn.model as modellib
from mrcnn.model import log
from mrcnn.config import Config

from samples.leaf import leaf

#Root directory of the project
sys.path.append(ROOT_DIR)  # To find local version of the library
# #Root directory of the project
# DATASET_DIR = '../datasets/Ara2013-Canon'

# # Import Mask RCNN
# sys.path.append(ROOT_DIR)  # To find local version of the library
# from mrcnn.config import Config
# from mrcnn import model as modellib, utils

# # Path to trained weights file
# COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

# # Directory to save logs and model checkpoints, if not provided
# # through the command line argument --logs

# #Options are coco or imagenet
# # WHICH_WEIGHTS = 'imagenet'
# WHICH_WEIGHTS = 'coco'
# DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs/coco_heads")

# # Directory to save logs and trained model
# MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# # Local path to trained weights file
# COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# # Download COCO trained weights from Releases if needed
# if not os.path.exists(COCO_MODEL_PATH):
#     utils.download_trained_weights(COCO_MODEL_PATH)<jupyter_output><empty_output><jupyter_text>Configurations<jupyter_code>config = leaf.LeafConfig()
#Configurations are some basic routines defined in samples/leaf/leaf.py<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>DATASET_DIR = '../datasets/Ara2013-Canon'

dataset = leaf.LeafDataset()
dataset.load_leaf(DATASET_DIR, "train")

# Must call before using the dataset
dataset.prepare()

print("Image Count: {}".format(len(dataset.image_ids)))
print("Class Count: {}".format(dataset.num_classes))
for i, info in enumerate(dataset.class_info):
    print("{:3}. {:50}".format(i, info['name']))<jupyter_output>loading annotations into memory...
Done (t=0.04s)
creating index...
index created!
Image_DIR:  ../datasets/Ara2013-Canon/train/images
Class ids [1]
Image Count: 153
Class Count: 2
  0. BG                                                
  1. leaf<jupyter_text>Load and Display Samples<jupyter_code>image_id = np.random.choice(dataset.image_ids, 1)[0]
image, image_meta, class_ids, bbox, mask = modellib.load_image_gt(
    dataset, config, image_id, use_mini_mask=False)

log("image", image)
log("image_meta", image_meta)
log("class_ids", class_ids)
log("bbox", bbox)
log("mask", mask)
print(mask.shape)


display_images([image]+[mask[:,:,i] for i in range(min(mask.shape[-1], 50))])
# Load and display random samples
num_images = 2
aux_image_ids = np.random.choice(dataset.image_ids, num_images)
for aux_image_id in aux_image_ids:
    aux_image = dataset.load_image(aux_image_id)
    aux_mask, aux_class_ids = dataset.load_mask(aux_image_id)  
    visualize.display_top_masks(aux_image, aux_mask, aux_class_ids, dataset.class_names, 1)<jupyter_output><empty_output><jupyter_text>Bounding BoxesThese are rectangular regions that cover the objects of interest.<jupyter_code>visualize.display_instances(image, bbox, mask, class_ids, dataset.class_names)
#Let's look at some more examples

# Load random image and mask.
image_id = random.choice(dataset.image_ids)
image = dataset.load_image(image_id)
mask, class_ids = dataset.load_mask(image_id)
# Compute Bounding box
bbox = utils.extract_bboxes(mask)

# Display image and additional stats
print("image_id ", image_id, dataset.image_reference(image_id))
log("image", image)
log("mask", mask)
log("class_ids", class_ids)
log("bbox", bbox)
# Display image and instances
visualize.display_instances(image, bbox, mask, class_ids, dataset.class_names)<jupyter_output>image_id  28 leaf.404
image                    shape: (130, 108, 3)         min:    0.00000  max:  178.00000  uint8
mask                     shape: (130, 108, 6)         min:    0.00000  max:    1.00000  bool
class_ids                shape: (6,)                  min:    1.00000  max:    1.00000  int32
bbox                     shape: (6, 4)                min:   10.00000  max:  118.00000  int32
{'fraction_non_alphanumeric': 0.11002337814143776, 'fraction_numerical': 0.03419053185271771, 'mean_word_length': 4.791032148900169, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '407904', 'license': 'MIT', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2310, 'n_words': 622, 'path': 'samples/leaf/[1]_Leaf_data_visualization_colab_notebook.ipynb', 'repo_name': 'aneeshchauhan/Mask_RCNN', 'size': '6844'}