<jupyter_start><jupyter_text>Mask R-CNN - Inspect Trained ModelCode and visualizations to test, debug, and evaluate the Mask R-CNN model.<jupyter_code>import os
import sys
import random
import math
import re
import time
import numpy as np
import tensorflow as tf
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Root directory of the project
ROOT_DIR = os.path.abspath("../../")

# 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

%matplotlib inline 

# 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)

# Path to Shapes trained weights
SHAPES_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_shapes.h5")<jupyter_output>Using TensorFlow backend.<jupyter_text>Configurations<jupyter_code># Run one of the code blocks

# Shapes toy dataset
# import shapes
# config = shapes.ShapesConfig()

# MS COCO Dataset
import coco
config = coco.CocoConfig()
COCO_DIR = "path to COCO dataset"  # TODO: enter value here
# Override the training configurations with a few
# changes for inferencing.
class InferenceConfig(config.__class__):
    # Run detection on one image at a time
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()
config.display()<jupyter_output>Configurations:
BACKBONE                       resnet101
BACKBONE_STRIDES               [4, 8, 16, 32, 64]
BATCH_SIZE                     1
BBOX_STD_DEV                   [0.1 0.1 0.2 0.2]
COMPUTE_BACKBONE_SHAPE         None
DETECTION_MAX_INSTANCES        100
DETECTION_MIN_CONFIDENCE       0.7
DETECTION_NMS_THRESHOLD        0.3
FPN_CLASSIF_FC_LAYERS_SIZE     1024
GPU_COUNT                      1
GRADIENT_CLIP_NORM             5.0
IMAGES_PER_GPU                 1
IMAGE_CHANNEL_COUNT            3
IMAGE_MAX_DIM                  1024
IMAGE_META_SIZE                93
IMAGE_MIN_DIM                  800
IMAGE_MIN_SCALE                0
IMAGE_RESIZE_MODE              square
IMAGE_SHAPE                    [1024 1024    3]
LEARNING_MOMENTUM              0.9
LEARNING_RATE                  0.001
LOSS_WEIGHTS                   {'rpn_class_loss': 1.0, 'rpn_bbox_loss': 1.0, 'mrcnn_class_loss': 1.0, 'mrcnn_bbox_loss': 1.0, 'mrcnn_mask_loss': 1.0}
MASK_POOL_SIZE                 14
MASK_SHAPE         [...]<jupyter_text>Notebook Preferences<jupyter_code># Device to load the neural network on.
# Useful if you're training a model on the same 
# machine, in which case use CPU and leave the
# GPU for training.
# DEVICE = "/cpu:0"  # /cpu:0 or /gpu:0
DEVICE = "/gpu:0"

# Inspect the model in training or inference modes
# values: 'inference' or 'training'
# TODO: code for 'training' test mode not ready yet
TEST_MODE = "inference"
def get_ax(rows=1, cols=1, size=16):
    """Return a Matplotlib Axes array to be used in
    all visualizations in the notebook. Provide a
    central point to control graph sizes.
    
    Adjust the size attribute to control how big to render images
    """
    _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
    return ax<jupyter_output><empty_output><jupyter_text>Load Validation Dataset<jupyter_code>'''
# Build validation dataset
if config.NAME == 'shapes':
    dataset = shapes.ShapesDataset()
    dataset.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
elif config.NAME == "coco":
    dataset = coco.CocoDataset()
    dataset.load_coco(COCO_DIR, "minival", auto_download=True)

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

print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names))
'''<jupyter_output><empty_output><jupyter_text>Load Model<jupyter_code># Create model in inference mode
with tf.device(DEVICE):
    model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR,
                              config=config)

# Set weights file path
if config.NAME == "shapes":
    weights_path = SHAPES_MODEL_PATH
elif config.NAME == "coco":
    weights_path = COCO_MODEL_PATH
# Or, uncomment to load the last model you trained
# weights_path = model.find_last()

# Load weights
print("Loading weights ", weights_path)
model.load_weights(weights_path, by_name=True)
labels = ["BG",                                                
"person",                                            
"bicycle",                                           
"car",                                               
"motorcycle",                                        
"airplane",                                          
"bus",                                               
"train",                                             
"truck",                                             
"boat",                                              
"traffic light",                                     
"fire hydrant",                                      
"stop sign",                                         
"parking meter",                                     
"bench",                                             
"bird",                                              
"cat",                                               
"dog",                                               
"horse",                                             
"sheep",                                             
"cow",                                               
"elephant",                                          
"bear",                                              
"zebra",                                             
"giraffe",                                           
"backpack",                                          
"umbrella",                                          
"handbag",                                           
"tie",                                               
"suitcase",                                          
"frisbee",                                           
"skis",                                              
"snowboard",                                         
"sports ball",                                       
"kite",                                              
"baseball bat",                                      
"baseball glove",                                    
"skateboard",                                        
"surfboard",                                         
"tennis racket",                                     
"bottle",                                            
"wine glass",                                        
"cup",                                               
"fork",                                              
"knife",                                             
"spoon",                                             
"bowl",                                              
"banana",                                            
"apple",                                             
"sandwich",                                          
"orange",                                            
"broccoli",                                          
"carrot",                                            
"hot dog",                                           
"pizza",                                             
"donut",                                             
"cake",                                              
"chair",                                             
"couch",                                             
"potted plant",                                      
"bed",                                               
"dining table",                                      
"toilet",                                            
"tv",                                                
"laptop",                                            
"mouse",                                             
"remote",                                            
"keyboard",                                          
"cell phone",                                        
"microwave",                                         
"oven",                                              
"toaster",                                           
"sink",                                              
"refrigerator",                                      
"book",                                              
"clock",                                             
"vase",                                              
"scissors",                                          
"teddy bear",                                        
"hair drier",                                        
"toothbrush"]

ids = [x for x in range(len(labels))]<jupyter_output><empty_output><jupyter_text>Run Detection on images<jupyter_code># image_id = random.choice(dataset.image_ids)
# image, image_meta, gt_class_id, gt_bbox, gt_mask =\
#     modellib.load_image_gt(dataset, config, image_id, use_mini_mask=False)
# info = dataset.image_info[image_id]
# print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id, 
#                                        dataset.image_reference(image_id)))

'''
import skimage.io

image_name = "../../images/2502287818_41e4b0c4fb_z.jpg"


image = skimage.io.imread(image_name)

# Run object detection
results = model.detect([image], verbose=1)

# Display results
ax = get_ax(1)
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                            labels, r['scores'], ax=ax,
                            title="Predictions") #dataset.class_names
# log("gt_class_id", gt_class_id)
# log("gt_bbox", gt_bbox)
# log("gt_mask", gt_mask)
'''<jupyter_output><empty_output><jupyter_text>Run Detection on videos<jupyter_code>'''
import cv2

cap = cv2.VideoCapture("../../videos/russian_traffic.mp4")
if cap.isOpened():
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    out = cv2.VideoWriter("output.avi", fourcc, fps, (width, height))
    while cap.isOpened():
        ret, frame = cap.read()
        
        if not ret:
            break
       
        # Segment the image
        results = model.detect([frame], verbose=1)

        # TODO: Draw boxes, labels, and masks
        # TODO: Implement a version of visualize.display_instances which draws using opencv and returns the image
        # TODO: Use a fixed color per class in order to avoid each frame having a different color mask for the
        #       same object
        r = results[0]
        ax = get_ax(1)
        frame = visualize.merge_results_on_image(frame, r['rois'], r['masks'], r['class_ids'], 
                                    labels, r['scores'], ax=ax,
                                    title="Predictions") #dataset.class_names
        
        out.write(frame)

        cv2.imshow('frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
            
    out.release()
    
    
cap.release()
cv2.destroyAllWindows()
'''
import cv2
from tqdm import tqdm

cap = cv2.VideoCapture("../../videos/russian_traffic.mp4")

if cap.isOpened():
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
#     out_width = width // 4
#     out_height = height // 4
    out_width = width
    out_height = height

    fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    out = cv2.VideoWriter("output.mp4", fourcc, fps, (out_width, out_height))
    
    for i in tqdm(range(num_frames)):
        ret, frame = cap.read()
        
        if not ret:
            break
            
        # reduce image size for speed
#         if width != out_width:
#             frame = cv2.resize(frame, (out_width, out_height), interpolation = cv2.INTER_CUBIC)
       
        # Segment the image
        results = model.detect([frame], verbose=0)

        # draw bounding boxes and segmentation masks
        r = results[0]
        frame = visualize.merge_results_on_image(frame, r['rois'], r['masks'], r['class_ids'], 
                                    labels, r['scores'],
                                    title="Predictions") #dataset.class_names

        # write to disk
        out.write(frame)
        
        if i == 0:
            cv2.imshow('frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
            
    out.release()


cap.release()
cv2.destroyAllWindows()<jupyter_output>23%|███████████████████████████████▌                                                                                                            | 633/2804 [07:56<25:18,  1.43it/s]
{'fraction_non_alphanumeric': 0.09065807639208467, 'fraction_numerical': 0.012808712992790305, 'mean_word_length': 1.2148802446067606, 'pattern_counts': {'":': 3, '<': 27, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 130, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '143055', 'license': 'MIT', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3329, 'n_words': 796, 'path': 'samples/carpedrestian/car_pedrestian_model/car_pedrestian.ipynb', 'repo_name': 'Risvil/CarPedrestianDetectorAPI', 'size': '13038'}