gpt4 book ai didi

python-3.x - Mask rcnn 不适用于高分辨率图像

转载 作者:行者123 更新时间:2023-12-02 02:52:10 26 4
gpt4 key购买 nike

我用了Mask-Rcnn按照这篇引用文章 Mask rcnn usage 使用 VIA 工具训练图像集(注意 具有高分辨率,例如:2400*1920) .在这里,我编辑了Ballon.py,代码如下:

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

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

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


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

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


# Dataset
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):
# Train the model.
# Training dataset.

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

# Training
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 class.
UserWarning('Using a generator with `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

我的笔记本电脑显卡规范如下:

具有 250 个 CUDA 内核的 Nvidia GeForce 830M (2 GB)

CPU 规范:

英特尔酷睿 i5(第 4 代),8 GB 内存

这里可能是什么情况?是图像的分辨率还是我的 GPU 的无能为力。我应该继续处理 CPU 吗?

最佳答案

我在训练我的自定义数据集时与 Mask RCNN 分享我的观察结果。

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

我在 nVIDIA RTX 2080Ti、32 GB DDR4 RAM 上进行训练,在训练时我收到以下警告;但训练过程完成。

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. "

2019-05-23 15:25:23.433774: W T:\src\github\tensorflow\tensorflow\core\common_runtime\bfc_allocator.cc:219] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.14GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.

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

因此,我们可以怀疑 GPU 内存的大小是导致此错误的一个因素。

此外,批量大小是另一个影响因素;但我看到您设置了 IMAGE_PER_GPU=1。如果您在 mrcnn 文件夹中的 config.py 文件中搜索 BATCH_SIZE,您会发现 –

self.BATCH_SIZE = self.IMAGES_PER_GPU * self.GPU_COUNT

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

总而言之,我建议您在更强大的 GPU 上尝试相同的代码。

关于python-3.x - Mask rcnn 不适用于高分辨率图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com