遮罩rcnn无法用于高分辨率的图像

时间:2018-08-09 10:33:10

标签: python-3.x tensorflow neural-network keras

在参考文献Mask-Rcnn之后,我用VIA工具使用Mask rcnn usage 来训练图像集(注意,具有高分辨率Eg:2400 * 1920)。在这里,我编辑了 Ballon.py ,代码如下:

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

项目的根目录

ROOT_DIR = os.path.abspath("../../")

# 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

训练重量文件的路径

COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

if COCO_WEIGHTS_PATH is None:
    print('weights not available')
else:
    print('weights available')


DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

配置

class NeuralCodeConfig(Config):

NAME = "screens"

# 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 + 10 # Background + other region classes

# Number of training steps per epoch
STEPS_PER_EPOCH = 30

# Skip detections with < 90% confidence
DETECTION_MIN_CONFIDENCE = 0.9

数据集

class NeuralCodeDataset(utils.Dataset):

def load_screen(self, dataset_dir, subset):
    """Load a subset of the screens dataset.
    dataset_dir: Root directory of the dataset.
    subset: Subset to load: train or val
    """
    # Add classes.
    self.add_class("screens",1,"logo")
    self.add_class("screens",2,"slider")
    self.add_class("screens",3,"navigation")
    self.add_class("screens",4,"forms")
    self.add_class("screens",5,"social_media_icons")
    self.add_class("screens",6,"video")
    self.add_class("screens",7,"map")
    self.add_class("screens",8,"pagination")
    self.add_class("screens",9,"pricing_table_block")
    self.add_class("screens",10,"gallery")


    # Train or validation dataset?
    assert subset in ["train", "val"]
    dataset_dir = os.path.join(dataset_dir, subset)

     # 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
    annotations = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
    if annotations is None:
        print ("region data json not loaded")
    else:
        print("region data json loaded")
    # print(annotations)
    annotations = list(annotations.values())  # don't need the dict keys

    # The VIA tool saves images in the JSON even if they don't have any
    # annotations. Skip unannotated images.
    annotations = [a for a in annotations if a['regions']]

    # Add images
    for a in annotations:
        # Get the x, y coordinaets of points of the polygons that make up
        # the outline of each object instance. There are stores in the
        # shape_attributes and region_attributes (see json format above)
        polygons = [r['shape_attributes'] for r in a['regions']]
        screens = [r['region_attributes']for r in a['regions']]
        #getting the filename by spliting 
        class_name = screens[0]['html']
        file_name = a['filename'].split("/")
        file_name = file_name[len(file_name)-1]

        #getting class_ids with file_name
        class_ids = class_name+"_"+file_name
        # #getting width an height of the images
        # height = [h['height'] for h in polygons]
        # width = [w['width'] for w in polygons]
        # print(height,'height')
        # print('polygons',polygons)

        # load_mask() needs the image size to convert polygons to masks.
        # Unfortunately, VIA doesn't include it in JSON, so we must readpath
        # the image. This is only managable since the dataset is tiny.
        image_path = os.path.join(dataset_dir,file_name)
        image = skimage.io.imread(image_path)
         #resizing images
        # image = utils.resize_image(image, min_dim=800, max_dim=1000, min_scale=None, mode="square")
        # print('image',image)
        height,width = image.shape[:2]
        # print('height',height)
        # print('width',width)
        # height = 800
        # width = 800


        self.add_image(
            "screens",
            image_id=file_name,  # use file name as a unique image id
            path=image_path,
            width=width, height=height,
            polygons=polygons,
            class_ids=class_ids)

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 screens dataset image, delegate to parent class.
    image_info = self.image_info[image_id]
    if image_info["source"] != "screens":
        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['y'], p['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.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)
    # class_ids = np.array(class_ids,dtype=np.int32)
    return mask,class_ids

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


def train(model):

训练模型。

训练数据集。

dataset_train = NeuralCodeDataset()
dataset_train.load_screen(args.dataset, "train")
dataset_train.prepare()

# Validation dataset
dataset_val = NeuralCodeDataset()
dataset_val.load_screen(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')

培训

if __name__ == '__main__':
    import argparse

# Parse command line arguments
parser = argparse.ArgumentParser(
    description='Train Mask R-CNN to detect screens.')
parser.add_argument("command",
                    metavar="<command>",
                    help="'train' or 'splash'")
parser.add_argument('--dataset', required='True',
                    metavar="../../datasets/screens",
                    help='Directory of the screens dataset')
parser.add_argument('--weights', required=True,
                    metavar="/weights.h5",
                    help="Path to weights .h5 file or 'coco'")
parser.add_argument('--logs', required=False,
                    default=DEFAULT_LOGS_DIR,
                    metavar="../../logs/",
                    help='Logs and checkpoints directory (default=logs/)')
parser.add_argument('--image', required=False,
                    metavar="path or URL to image",
                    help='Image to apply the color splash effect on')
parser.add_argument('--video', required=False,
                    metavar="path or URL to video",
                    help='Video to apply the color splash effect on')
args = parser.parse_args()

# Validate arguments
if args.command == "train":
    assert args.dataset, "Argument --dataset is required for training"
elif args.command == "splash":
    assert args.image or args.video,\
           "Provide --image or --video to apply color splash"

print("Weights: ", args.weights)
print("Dataset: ", args.dataset)
print("Logs: ", args.logs)

# Configurations
if args.command == "train":
    config = NeuralCodeConfig()
else:
    class InferenceConfig(NeuralCodeConfig):
        # 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()
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))

使用预训练的COCO数据集训练数据集时,出现以下错误:

UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory. "Converting sparse IndexedSlices to a dense Tensor of unknown shape. " 2018-08-09 13:52:27.993239: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory. 2018-08-09 13:52:28.037704: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory. /home/scit/anaconda3/lib/python3.6/site-packages/keras/engine/training.py:2022: UserWarning: Using a generator with use_multiprocessing = True and multiple workers may duplicate your data. Please consider using the keras.utils.Sequence类。   UserWarning('与use_multiprocessing=True一起使用生成器' `

    `ERROR:root:Error processing image {'id': '487.jpg', 'source': 'screens', 'path': '../../datasets/screens/train/487.jpg', 'width': 1920, 'height': 7007, 'polygons': [{'name': 'rect', 'x': 384, 'y': 5, 'width': 116, 'height': 64}, {'name': 'rect', 'x': 989, 'y': 17, 'width': 516, 'height': 42}, {'name': 'rect', 'x': 984, 'y': 5933, 'width': 565, 'height': 273}, {'name': 'rect', 'x': 837, 'y': 6793, 'width': 238, 'height': 50}], 'class_ids': 'logo_487.jpg'}
Traceback (most recent call last):
  File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1717, in data_generator
    use_mini_mask=config.USE_MINI_MASK)
  File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1219, in load_image_gt
    mask, class_ids = dataset.load_mask(image_id)
  File "neural_code.py", line 235, in load_mask
    rr, cc = skimage.draw.polygon(p['y'], p['x'])
  File "/home/scit/anaconda3/lib/python3.6/site-packages/skimage/draw/draw.py", line 441, in polygon
    return _polygon(r, c, shape)
  File "skimage/draw/_draw.pyx", line 217, in skimage.draw._draw._polygon (skimage/draw/_draw.c:4402)
OverflowError: Python int too large to convert to C ssize_t`

请注意,我的笔记本电脑规格如下:

Nvidia geforce 830M(2gb)with 250 cuda cores

CPU规格:

intel core i5(4th gen) , 8gb ram

这里可能是什么情况?是图像的分辨率还是我的GPU无法使用?我应该继续使用CPU吗?

请帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

我正在训练Mask RCNN时与Mask RCNN分享我的观察结果。

我的数据集包含各种尺寸的图像(即最小的图像大约有1700 x 1600像素,最大的图像大约有8500 x 4600像素)。

我正在nVIDIA RTX 2080Ti,32 GB DDR4 RAM上进行培训,并且在进行培训时收到以下提到的警告;但培训过程完成了。

  

Blockquote

UserWarning:将稀疏的IndexedSlices转换为形状未知的密集Tensor。这可能会占用大量内存。

“将稀疏的IndexedSlices转换为形状未知的密集张量。”

2019-05-23 15:25:23.433774:W T:\ src \ github \ tensorflow \ tensorflow \ core \ common_runtime \ bfc_allocator.cc:219]分配器(GPU_0_bfc)内存不足,试图分配3.14GiB。调用方表示这不是故障,但可能意味着如果有更多可用内存,则可能会提高性能。

  

Blockquote

几个月前,我在装有12 GB RAM和nVIDIA 920M(2GB GPU)的笔记本电脑上尝试了Matterport Splash of Color Example;并遇到了类似的内存错误。

因此,我们可以怀疑GPU内存的大小是造成此错误的原因。

此外,批次大小是另一个因素。但我看到您已经设置了IMAGE_PER_GPU = 1。如果在mrcnn文件夹中存在的config.py文件中搜索BATCH_SIZE,则会找到–

self.BATCH_SIZE = self.IMAGES_PER_GPU * self.GPU_COUNT

因此,在您的情况下,batch_size为1。

最后,我建议您在功能更强大的GPU上尝试相同的代码。