"""
Mask R-CNN
Train on the toy Balloon dataset and implement color splash effect.

Copyright (c) 2018 Matterport, Inc.
Licensed under the MIT License (see LICENSE for details)
Written by Waleed Abdulla

"""

import os
import sys
import json
import datetime
import numpy as np
import skimage.draw

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import Mask RCNN
sys.path.append(ROOT_DIR)
from config import Config
import utils
import model as modellib

# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

############################################################
#  Configurations
############################################################


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "glob"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 1  # Background + baloon

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 100

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9


############################################################
#  Dataset
############################################################

class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes. We have only one class to add.
        self.add_class("glomerulus", 1, "glomerulus")

        

        # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region
        json_path="../../data/hubmap/train/aaa6a05cc.json"
    annotations = json.load(open(json_path))
    #print(type(annotations))
    #print(annotations)
    annotations = [a["geometry"] for a in annotations]

    # Add images
    for a in annotations:
        # Get the x, y coordinates of points of the polygons that make up
        # the outline of each object instance, stored in the geometry of each shape dictionary
        polygons = [r for r in a['coordinates']]

        image_path = "../../data/hubmap/train/aaa6a05cc.tiff"
        image = io.imread(image_path)
        height, width = image.shape[:2]

        self.add_image(
            "glomerulus",
            image_id="aaa6a05cc.tiff",  # use file name as a unique image id
            path=image_path,
            width=width, height=height,
            polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a balloon dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != "glomerulus":
            return super(self.__class__, self).load_mask(image_id)

        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance. Since we have
        # one class ID only, we return an array of 1s
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "balloon":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)


def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=30,
                layers='heads')


############################################################
#  Training
############################################################


# Configurations
if args.command == "train":
    config = BalloonConfig()
else:
    class InferenceConfig(BalloonConfig):
            # Set batch size to 1 since we'll be running inference on
            # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()

# Create model
if args.command == "train":
    model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
else:
    model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

# Select weights file to load
if args.weights.lower() == "coco":
    weights_path = COCO_WEIGHTS_PATH
# Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == "last":
      # Find last trained weights
    weights_path = model.find_last()[1]
elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights

# Load weights
print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
            "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True)

    # Train or evaluate
if args.command == "train":
    train(model)
elif args.command == "splash":
    detect_and_color_splash(model, image_path=args.image,
                                video_path=args.video)
else:
    print("'{}' is not recognized. "
              "Use 'train' or 'splash'".format(args.command))

c5a84495e841fcc68a1506196ca58b88e9d8664e
<mask token>


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    NAME = 'glob'
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 1 + 1
    STEPS_PER_EPOCH = 100
    DETECTION_MIN_CONFIDENCE = 0.9


class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        self.add_class('glomerulus', 1, 'glomerulus')
        json_path = '../../data/hubmap/train/aaa6a05cc.json'
    annotations = json.load(open(json_path))
    annotations = [a['geometry'] for a in annotations]
    for a in annotations:
        polygons = [r for r in a['coordinates']]
        image_path = '../../data/hubmap/train/aaa6a05cc.tiff'
        image = io.imread(image_path)
        height, width = image.shape[:2]
        self.add_image('glomerulus', image_id='aaa6a05cc.tiff', path=
            image_path, width=width, height=height, polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        image_info = self.image_info[image_id]
        if image_info['source'] != 'glomerulus':
            return super(self.__class__, self).load_mask(image_id)
        info = self.image_info[image_id]
        mask = np.zeros([info['height'], info['width'], len(info['polygons'
            ])], dtype=np.uint8)
        for i, p in enumerate(info['polygons']):
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info['source'] == 'balloon':
            return info['path']
        else:
            super(self.__class__, self).image_reference(image_id)


<mask token>

<mask token>


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    NAME = 'glob'
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 1 + 1
    STEPS_PER_EPOCH = 100
    DETECTION_MIN_CONFIDENCE = 0.9


class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        self.add_class('glomerulus', 1, 'glomerulus')
        json_path = '../../data/hubmap/train/aaa6a05cc.json'
    annotations = json.load(open(json_path))
    annotations = [a['geometry'] for a in annotations]
    for a in annotations:
        polygons = [r for r in a['coordinates']]
        image_path = '../../data/hubmap/train/aaa6a05cc.tiff'
        image = io.imread(image_path)
        height, width = image.shape[:2]
        self.add_image('glomerulus', image_id='aaa6a05cc.tiff', path=
            image_path, width=width, height=height, polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        image_info = self.image_info[image_id]
        if image_info['source'] != 'glomerulus':
            return super(self.__class__, self).load_mask(image_id)
        info = self.image_info[image_id]
        mask = np.zeros([info['height'], info['width'], len(info['polygons'
            ])], dtype=np.uint8)
        for i, p in enumerate(info['polygons']):
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info['source'] == 'balloon':
            return info['path']
        else:
            super(self.__class__, self).image_reference(image_id)


def train(model):
    """Train the model."""
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, 'train')
    dataset_train.prepare()
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, 'val')
    dataset_val.prepare()
    print('Training network heads')
    model.train(dataset_train, dataset_val, learning_rate=config.
        LEARNING_RATE, epochs=30, layers='heads')


<mask token>

<mask token>
ROOT_DIR = os.getcwd()
sys.path.append(ROOT_DIR)
<mask token>
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, 'logs')


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    NAME = 'glob'
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 1 + 1
    STEPS_PER_EPOCH = 100
    DETECTION_MIN_CONFIDENCE = 0.9


class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        self.add_class('glomerulus', 1, 'glomerulus')
        json_path = '../../data/hubmap/train/aaa6a05cc.json'
    annotations = json.load(open(json_path))
    annotations = [a['geometry'] for a in annotations]
    for a in annotations:
        polygons = [r for r in a['coordinates']]
        image_path = '../../data/hubmap/train/aaa6a05cc.tiff'
        image = io.imread(image_path)
        height, width = image.shape[:2]
        self.add_image('glomerulus', image_id='aaa6a05cc.tiff', path=
            image_path, width=width, height=height, polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        image_info = self.image_info[image_id]
        if image_info['source'] != 'glomerulus':
            return super(self.__class__, self).load_mask(image_id)
        info = self.image_info[image_id]
        mask = np.zeros([info['height'], info['width'], len(info['polygons'
            ])], dtype=np.uint8)
        for i, p in enumerate(info['polygons']):
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info['source'] == 'balloon':
            return info['path']
        else:
            super(self.__class__, self).image_reference(image_id)


def train(model):
    """Train the model."""
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, 'train')
    dataset_train.prepare()
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, 'val')
    dataset_val.prepare()
    print('Training network heads')
    model.train(dataset_train, dataset_val, learning_rate=config.
        LEARNING_RATE, epochs=30, layers='heads')


if args.command == 'train':
    config = BalloonConfig()
else:


    class InferenceConfig(BalloonConfig):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()
if args.command == 'train':
    model = modellib.MaskRCNN(mode='training', config=config, model_dir=
        args.logs)
else:
    model = modellib.MaskRCNN(mode='inference', config=config, model_dir=
        args.logs)
if args.weights.lower() == 'coco':
    weights_path = COCO_WEIGHTS_PATH
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == 'last':
    weights_path = model.find_last()[1]
elif args.weights.lower() == 'imagenet':
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights
print('Loading weights ', weights_path)
if args.weights.lower() == 'coco':
    model.load_weights(weights_path, by_name=True, exclude=[
        'mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask'])
else:
    model.load_weights(weights_path, by_name=True)
if args.command == 'train':
    train(model)
elif args.command == 'splash':
    detect_and_color_splash(model, image_path=args.image, video_path=args.video
        )
else:
    print("'{}' is not recognized. Use 'train' or 'splash'".format(args.
        command))

<mask token>
import os
import sys
import json
import datetime
import numpy as np
import skimage.draw
ROOT_DIR = os.getcwd()
sys.path.append(ROOT_DIR)
from config import Config
import utils
import model as modellib
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, 'logs')


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    NAME = 'glob'
    IMAGES_PER_GPU = 1
    NUM_CLASSES = 1 + 1
    STEPS_PER_EPOCH = 100
    DETECTION_MIN_CONFIDENCE = 0.9


class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        self.add_class('glomerulus', 1, 'glomerulus')
        json_path = '../../data/hubmap/train/aaa6a05cc.json'
    annotations = json.load(open(json_path))
    annotations = [a['geometry'] for a in annotations]
    for a in annotations:
        polygons = [r for r in a['coordinates']]
        image_path = '../../data/hubmap/train/aaa6a05cc.tiff'
        image = io.imread(image_path)
        height, width = image.shape[:2]
        self.add_image('glomerulus', image_id='aaa6a05cc.tiff', path=
            image_path, width=width, height=height, polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        image_info = self.image_info[image_id]
        if image_info['source'] != 'glomerulus':
            return super(self.__class__, self).load_mask(image_id)
        info = self.image_info[image_id]
        mask = np.zeros([info['height'], info['width'], len(info['polygons'
            ])], dtype=np.uint8)
        for i, p in enumerate(info['polygons']):
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info['source'] == 'balloon':
            return info['path']
        else:
            super(self.__class__, self).image_reference(image_id)


def train(model):
    """Train the model."""
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, 'train')
    dataset_train.prepare()
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, 'val')
    dataset_val.prepare()
    print('Training network heads')
    model.train(dataset_train, dataset_val, learning_rate=config.
        LEARNING_RATE, epochs=30, layers='heads')


if args.command == 'train':
    config = BalloonConfig()
else:


    class InferenceConfig(BalloonConfig):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()
if args.command == 'train':
    model = modellib.MaskRCNN(mode='training', config=config, model_dir=
        args.logs)
else:
    model = modellib.MaskRCNN(mode='inference', config=config, model_dir=
        args.logs)
if args.weights.lower() == 'coco':
    weights_path = COCO_WEIGHTS_PATH
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == 'last':
    weights_path = model.find_last()[1]
elif args.weights.lower() == 'imagenet':
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights
print('Loading weights ', weights_path)
if args.weights.lower() == 'coco':
    model.load_weights(weights_path, by_name=True, exclude=[
        'mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask'])
else:
    model.load_weights(weights_path, by_name=True)
if args.command == 'train':
    train(model)
elif args.command == 'splash':
    detect_and_color_splash(model, image_path=args.image, video_path=args.video
        )
else:
    print("'{}' is not recognized. Use 'train' or 'splash'".format(args.
        command))

"""
Mask R-CNN
Train on the toy Balloon dataset and implement color splash effect.

Copyright (c) 2018 Matterport, Inc.
Licensed under the MIT License (see LICENSE for details)
Written by Waleed Abdulla

"""

import os
import sys
import json
import datetime
import numpy as np
import skimage.draw

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import Mask RCNN
sys.path.append(ROOT_DIR)
from config import Config
import utils
import model as modellib

# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

############################################################
#  Configurations
############################################################


class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "glob"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 1  # Background + baloon

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 100

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9


############################################################
#  Dataset
############################################################

class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes. We have only one class to add.
        self.add_class("glomerulus", 1, "glomerulus")

        

        # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region
        json_path="../../data/hubmap/train/aaa6a05cc.json"
    annotations = json.load(open(json_path))
    #print(type(annotations))
    #print(annotations)
    annotations = [a["geometry"] for a in annotations]

    # Add images
    for a in annotations:
        # Get the x, y coordinates of points of the polygons that make up
        # the outline of each object instance, stored in the geometry of each shape dictionary
        polygons = [r for r in a['coordinates']]

        image_path = "../../data/hubmap/train/aaa6a05cc.tiff"
        image = io.imread(image_path)
        height, width = image.shape[:2]

        self.add_image(
            "glomerulus",
            image_id="aaa6a05cc.tiff",  # use file name as a unique image id
            path=image_path,
            width=width, height=height,
            polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a balloon dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != "glomerulus":
            return super(self.__class__, self).load_mask(image_id)

        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance. Since we have
        # one class ID only, we return an array of 1s
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "balloon":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)


def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=30,
                layers='heads')


############################################################
#  Training
############################################################


# Configurations
if args.command == "train":
    config = BalloonConfig()
else:
    class InferenceConfig(BalloonConfig):
            # Set batch size to 1 since we'll be running inference on
            # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()

# Create model
if args.command == "train":
    model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
else:
    model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

# Select weights file to load
if args.weights.lower() == "coco":
    weights_path = COCO_WEIGHTS_PATH
# Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == "last":
      # Find last trained weights
    weights_path = model.find_last()[1]
elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights

# Load weights
print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
            "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True)

    # Train or evaluate
if args.command == "train":
    train(model)
elif args.command == "splash":
    detect_and_color_splash(model, image_path=args.image,
                                video_path=args.video)
else:
    print("'{}' is not recognized. "
              "Use 'train' or 'splash'".format(args.command))
