<jupyter_start><jupyter_text>Mask-RCNN DataLoader. Balloon dataset<jupyter_code>import os
import numpy as np
import matplotlib.pyplot as plt
os.chdir('../src')

from samples.balloon import balloon
from preprocess import preprocess
from preprocess import augmentation as aug

%matplotlib inline
%load_ext watermark
%watermark
%watermark --iversions<jupyter_output>Last updated: 2021-09-25T15:23:56.371384+03:00

Python implementation: CPython
Python version       : 3.7.7
IPython version      : 7.16.1

Compiler    : GCC 7.3.0
OS          : Linux
Release     : 5.4.0-65-generic
Machine     : x86_64
Processor   : x86_64
CPU cores   : 12
Architecture: 64bit

matplotlib: 3.2.2
numpy     : 1.18.5<jupyter_text>We set paths to train and val parts of balloon dataset<jupyter_code>base_dir = os.getcwd().replace('src', 'balloon')
train_dir = os.path.join(base_dir, 'train')
val_dir = os.path.join(base_dir, 'val')
from common.config import CONFIG<jupyter_output><empty_output><jupyter_text>In `common.config` you can inspect general Mask-RCNN config as a dict<jupyter_code>CONFIG.update(balloon.BALLOON_CONFIG)
CONFIG<jupyter_output><empty_output><jupyter_text>---For dataset preparation first we create train and validation instances of __BalloonDataset__.__BalloonDataset__ inherits all the functions from the base __SegmentationDataset__ class in `/src/common/preprocess/preprocess.py` which was prepared for VGG Image Annotation tool. Hovewer, some details of annotations can be different as in balloon dataset annotation so we made `BalloonDataset` class.__SegmentationDataset__ (__BalloonDataset__) also includes arguments for data augmentation and normalization if necessary. An example of training augmentation is in `/src/common/preprocess/augmentation.py`.For augmentation we use very flexible __albumentations__ library.Then we pass __BalloonDataset__ to __DataLoader__ class which inherits tf.keras.utils.Sequence functionality for Mask-RCNN model.<jupyter_code>train_dataset = balloon.BalloonDataset(images_dir=train_dir,
                                       class_key='object',
                                       classes_dict=CONFIG['class_dict'],
                                       preprocess_transform=preprocess.get_input_preprocess(
                                           normalize=CONFIG['normalization']
                                       ),
                                       augmentation=aug.get_training_augmentation(),
                                       json_annotation_key=None,
                                       **CONFIG
                                            )

val_dataset = balloon.BalloonDataset(images_dir=val_dir,
                                     class_key='object',
                                     classes_dict=CONFIG['class_dict'],
                                     preprocess_transform=preprocess.get_input_preprocess(
                                         normalize=CONFIG['normalization']
                                     ),
                                     json_annotation_key=None,
                                     **CONFIG
                                    )

# cast_output=False here is only to avoid gpu memory allocation in tensorflow casting
train_dataloader = preprocess.DataLoader(train_dataset,
                                         shuffle=True,
                                         cast_output=False,
                                         **CONFIG
                                        )
val_dataloader = preprocess.DataLoader(val_dataset,
                                       shuffle=False,
                                       cast_output=False,
                                       **CONFIG
                                      )<jupyter_output>Found annotation file: via_region_data.json in dataset path: /home/alexander/Documents/py_projects/github/maskrcnn_tf2/balloon/train
Found annotation file: via_region_data.json in dataset path: /home/alexander/Documents/py_projects/github/maskrcnn_tf2/balloon/val
dataloader DataLoader. Steps per epoch: 61
dataloader DataLoader. Steps per epoch: 13<jupyter_text>Create data generators for training and validation<jupyter_code>train_iterated = iter(train_dataloader)
val_iterated = iter(val_dataloader)
"""
inputs = [batch_images, batch_image_meta, batch_rpn_match, batch_rpn_bbox,
          batch_gt_class_ids, batch_gt_boxes, batch_gt_masks]
outputs = []
"""
train_batch = next(train_iterated)<jupyter_output><empty_output><jupyter_text>Inspect batch shapes<jupyter_code>batch_images, batch_image_meta, batch_rpn_match, batch_rpn_bbox,\
batch_gt_class_ids, batch_gt_boxes, batch_gt_masks = train_batch[0]

for x in train_batch[0]:
    print(x.shape)<jupyter_output>(1, 512, 512, 3)
(1, 14)
(1, 65472, 1)
(1, 256, 4)
(1, 100)
(1, 100, 4)
(1, 512, 512, 100)<jupyter_text>Inspect train and validation image examples<jupyter_code>for img in batch_images:
    fig = plt.figure(figsize=(10,10))
    plt.imshow(img)
batch_gt_masks.shape
show_masks = 3
for mask in np.moveaxis(batch_gt_masks[0], -1, 0)[:show_masks]:
    fig = plt.figure(figsize=(10,10))
    plt.imshow(mask)
val_batch = next(val_iterated)

val_batch_images, val_batch_image_meta, val_batch_rpn_match, val_batch_rpn_bbox,\
val_batch_gt_class_ids, val_batch_gt_boxes, val_batch_gt_masks = val_batch[0]

for x in val_batch[0]:
    print(x.shape)
for img in val_batch_images:
    fig = plt.figure(figsize=(10,10))
    plt.imshow(img )
show_masks = 3
for mask in np.moveaxis(val_batch_gt_masks[0], -1, 0)[:show_masks]:
    fig = plt.figure(figsize=(10,10))
    plt.imshow(mask)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08015605603830467, 'fraction_numerical': 0.023763078560028374, 'mean_word_length': 2.5606060606060606, 'pattern_counts': {'":': 0, '<': 26, '<?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': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '89426', 'license': 'MIT', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1639, 'n_words': 362, 'path': 'notebooks/example_data_loader_balloon.ipynb', 'repo_name': 'alexander-pv/maskrcnn_tf2', 'size': '5639'}