Exemple #1
0
def train(h5_file, model_dir, init_with='coco', latest="latest.h5"):
    """
    Train the MRCNN using the 
    Parameters:
    -----------
    h5_file: str
        Path to the h5file that contains the ground truth datasets
    init_with: str
        Name of the h5 file to initilaze the M-RCNN network
    model_dir: str
        Directory to save logs and trained model

    lastes: src 
        The file to use as symlink for the best model
    """

    # Total number of images in the .h5 file

    n_images = get_n_images(h5_file)
    print("number of images:{0}".format(n_images))
    #n_images = 200
    imgs_ind = np.arange(n_images)
    np.random.shuffle(imgs_ind)

    # Split 80-20
    train_last_id = int(n_images * 0.80)

    train_indexes = imgs_ind[0:train_last_id]
    test_indexes = imgs_ind[train_last_id + 1:n_images]
    n_test = len(test_indexes)
    print("Total:{0}, Train:{1}, Test:{2}".format(n_images, len(train_indexes),
                                                  len(test_indexes)))

    dataset_train = CellsDataset()
    dataset_train.load_cells(h5_file, train_indexes)
    dataset_train.prepare()

    dataset_test = CellsDataset()
    dataset_test.load_cells(h5_file, test_indexes)
    dataset_test.prepare()

    MODEL_DIR = model_dir

    config = CellsConfig()

    #GZ: Change to accomodate the real number of passes while
    #executing the schedule below or 200 epochs
    total_passes = 30
    n_epochs = 200
    config.STEPS_PER_EPOCH= int(train_last_id * total_passes / \
        n_epochs / config.BATCH_SIZE)

    config.VALIDATION_STEPS = int(n_test * total_passes / \
        n_epochs / config.BATCH_SIZE)

    #config.STEPS_PER_EPOCH = train_indexes.shape[0]  / config.BATCH_SIZE
    #config.VALIDATION_STEPS = test_indexes.shape[0] / config.BATCH_SIZE
    config.display()

    print("MRCNN Train module:", modellib.__file__)
    model = modellib.MaskRCNN(mode="training",
                              config=config,
                              model_dir=model_dir)

    #print(image1.shape)
    #print( mask1.shape, ids)
    #np.save("image.npy", image1)
    #np.save("mask.npy", mask1)
    #exit()
    # Which weights to start with?
    # imagenet, coco, or last
    print('initializing with {}'.format(init_with))
    initial_layers = "heads"
    if init_with == "imagenet":
        model.load_weights(model.get_imagenet_weights(), by_name=True)
    elif init_with == "coco":
        # Load weights trained on MS COCO, but skip layers that
        # are different due to the different number of classes
        # See README for instructions to download the COCO weights

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

        model.load_weights(COCO_MODEL_PATH,
                           by_name=True,
                           exclude=[
                               "mrcnn_class_logits", "mrcnn_bbox_fc",
                               "mrcnn_bbox", "mrcnn_mask"
                           ])
    elif init_with == "last":
        # Load the last model you trained and continue training
        model.load_weights(model.find_last(), by_name=True)
    elif init_with == "random":
        print("Warning: Model is initialized with random weights")
        initial_layers = "all"
    elif os.path.exists(init_with):
        import inspect
        print(inspect.getfullargspec(model.load_weights))
        print(model.load_weights.__module__)
        model.load_weights(init_with, by_name=True, reset_init_epoch=True)
    else:
        print("ERROR: No model initialization provided")
        exit(1)

    ### TRAIN THE MODEL
    # TGAR, modify how to train model. Epochs accumulate (ex. line first call to model.train means train epochs 1-75 and second call to train means train from epochs 75-100.
    #DEVICE = '/device:GPU:0'
    #with tf.device(DEVICE):

    train_heads_start = time.time()
    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE,
        #augmentation=augmentation,
        epochs=75,
        layers=initial_layers)

    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE / 10,
        #augmentation=augmentation,
        epochs=100,
        layers=initial_layers)

    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE / 100,
        #augmentation=augmentation,
        epochs=125,
        layers=initial_layers)

    train_heads_end = time.time()
    train_heads_time = train_heads_end - train_heads_start
    print('\n Done training {0}. Took {1} seconds'.format(
        initial_layers, train_heads_time))

    # Fine tune all layers
    # Passing layers="all" trains all layers. You can also
    # pass a regular expression to select which layers to
    # train by name pattern.

    train_all_start = time.time()

    t1s = time.time()
    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE / 10,
        #augmentation=augmentation,
        epochs=150,
        layers="all")
    t1e = time.time()
    print(t1e - t1s)

    t2s = time.time()
    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE / 100,
        #augmentation=augmentation,
        epochs=175,
        layers="all")
    t2e = time.time()
    print(t2e - t2s)

    t3s = time.time()
    model.train(
        dataset_train,
        dataset_test,
        learning_rate=config.LEARNING_RATE / 1000,
        #augmentation=augmentation,
        epochs=200,
        layers="all")
    t3e = time.time()
    print(t3e - t3s)

    train_all_end = time.time()
    train_all_time = train_all_end - train_all_start
    print("Here", model.find_last())
    best_model = os.path.abspath(model.find_last())
    os.symlink(best_model, latest)

    print('\n Best model {0} symlinked to {1}'.format(best_model, latest))
    print(
        '\n Done training all layers. Took {} seconds'.format(train_all_time))
Exemple #2
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='MaskRCNN Video Object Detection/Instance Segmentation')
    parser.add_argument('-v', '--video_path', type=str, default='', help='Path to video. If None camera will be used')
    parser.add_argument('-sp', '--save_path', type=str, default='', help= 'Path to save the output. If None output won\'t be saved')
    parser.add_argument('-s', '--show', default=True, action="store_false", help='Show output')
    args = parser.parse_args()

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

    class_names = ['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',
Exemple #3
0
    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.model.lower() == "coco":
        model_path = COCO_WEIGHTS_PATH
        # Download weights file
        if not os.path.exists(model_path):
            utils.download_trained_weights(model_path)
    elif args.model.lower() == "last":
        # Find last trained weights
        model_path = model.find_last()[1]
    elif args.model.lower() == "imagenet":
        # Start from ImageNet trained weights
        model_path = model.get_imagenet_weights()
    else:
        model_path = args.model

    # Load weights
    print("Loading weights ", model_path)

    if args.model.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
Exemple #4
0
    def analyzePicture(self):

        # 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
        import mrcnn.model as modellib
        from mrcnn import visualize
        # Import COCO config
        sys.path.append(os.path.join(ROOT_DIR,
                                     "samples/coco/"))  # To find local version
        import coco

        #%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, "samples/mask_rcnn_coco.h5")
        print(COCO_MODEL_PATH)
        # Download COCO trained weights from Releases if needed
        if not os.path.exists(COCO_MODEL_PATH):
            utils.download_trained_weights(COCO_MODEL_PATH)

        # Directory of images to run detection on
        IMAGE_DIR = os.path.join(ROOT_DIR, "images")

        # Directory of images to run detection on
        #IMAGE_DIR = os.path.join(ROOT_DIR, "peoples")

        class InferenceConfig(coco.CocoConfig):
            # 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
            #BATCH_SIZE = 1

        config = InferenceConfig()
        config.display()

        # Create model object in inference mode.
        model = modellib.MaskRCNN(mode="inference",
                                  model_dir=MODEL_DIR,
                                  config=config)

        # Load weights trained on MS-COCO
        model.load_weights(COCO_MODEL_PATH, by_name=True)

        # COCO Class names
        # Index of the class in the list is its ID. For example, to get ID of
        # the teddy bear class, use: class_names.index('teddy bear')
        class_names = [
            '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'
        ]

        count = 0
        #Load a random image from the images folder

        for i in range(1):
            print(i)
            file_names = next(os.walk(IMAGE_DIR))[2]
            image = skimage.io.imread(
                os.path.join(IMAGE_DIR, random.choice(file_names)))
            #image = skimage.io.imread(os.path.join(IMAGE_DIR, file_names[i]))

            #image  = skimage.io.imread('edge2.jpg')

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

            # Visualize results
            r = results[0]
            #count = 0

            print(r['class_ids'])
            for i in r['class_ids']:
                if i == 1:
                    count = count + 1
            print(count)
            #visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],class_names, r['scores'])
            visualize.display_instances(image,
                                        r['rois'],
                                        r['masks'],
                                        r['class_ids'],
                                        class_names,
                                        r['scores'],
                                        title="Number of People:" + str(count))

        return count
Exemple #5
0
def entry():
    preparser = argparse.ArgumentParser(add_help=False)
    preparser.add_argument('--gui', action='store_true')
    preparser.add_argument('--local', action='store_true')
    preparser.add_argument('-d', '--daemon', action='store_true')
    args_parsered, args_rest = preparser.parse_known_intermixed_args()  # GUI 确认后交给下一解析器
    if args_parsered.gui:
        clientGUI()
        return

    if args_parsered.local and args_parsered.daemon:
        parser = argparse.ArgumentParser(
            description='Client for Tensorflow Serving of Mask RCNN (local-daemon mode)',
            epilog='使用例:./tf_detection.py --local --daemon --out ./output.jpg ./input.png')
    elif args_parsered.local and not args_parsered.daemon:
        parser = argparse.ArgumentParser(
            description='Client for Tensorflow Serving of Mask RCNN (local mode)',
            epilog='使用例:./tf_detection.py --local --out ./output.jpg ./input.png')
        parser.add_argument('file', metavar='FILE', help='待处理的图像文件路径')
    elif not args_parsered.local and args_parsered.daemon:
        parser = argparse.ArgumentParser(
            description='Client for Tensorflow Serving of Mask RCNN (daemon mode)',
            epilog='使用例:./tf_detection.py --daemon --out ./output.jpg 127.0.0.1 ./input.png')
        parser.add_argument('host', metavar='(DOMAIN|IP)', help='服务器域名或 IP')
        parser.add_argument('-p', '--port', metavar='PORT',
                            default='8500', help='通信端口(默认:8500)')
    else:
        parser = argparse.ArgumentParser(
            description='Client for Tensorflow Serving of Mask RCNN',
            epilog='使用例:./tf_detection.py --out ./output.jpg 127.0.0.1 ./input.png')
        parser.add_argument('host', metavar='(DOMAIN|IP)', help='服务器域名或 IP 地址')
        parser.add_argument('-p', '--port', metavar='PORT',
                            default='8500', help='通信端口(默认:8500)')
        parser.add_argument('file', metavar='FILE', help='待处理的图像文件路径')


    parser.add_argument('--local', action='store_true',
                        help='本地模式(默认客户端模式)')
    parser.add_argument('--model-path', metavar='PATH', dest='model_path',
                        default='keras_model/mask_rcnn_coco.h5', help='模型文件路径(默认:keras_model/mask_rcnn_coco.h5)')
    parser.add_argument('--gpus', metavar='NUM',
                        default=0, help='使用的 GPU 数量(默认:0)')
    parser.add_argument('--gui', action='store_true',
                        help='启动图形界面(所有命令行参数都将失效)')
    parser.add_argument('-o', '--out', metavar='OUTPUT',
                        default=None, help='处理后的图像文件输出路径(不指定则输出到屏幕)')
    parser.add_argument('--width', metavar='PIXEL', type=int,
                        default=1920, help='输出图像的宽度(单位:px,默认:1920)')
    parser.add_argument('--height', metavar='PIXEL', type=int,
                        default=1080, help='输出图像的高度(单位:px,默认:1080)')
    parser.add_argument('--dpi', metavar='DPI', type=int,
                        default=96, help='输出图像的DPI(默认:96)')
    parser.add_argument('-d', '--daemon', action='store_true',
                        help='启用 Socket 监听(注:如果传递的数据中包含中文,必须使用 UTF-8 编码)')
    parser.add_argument('--sockip', metavar='IP',
                        default='127.0.0.1', help='Socket 监听地址(默认:127.0.0.1)')
    parser.add_argument('--sockport', metavar='PORT', type=int,
                        default=8912, help='Socket 监听端口(默认:8912)')
    parser.add_argument('-v', '--verbose', action='count',
                        default=0, help='调试等级,可多次指定')
    global args; args = parser.parse_intermixed_args()
    if args.out:
        args.out = os.path.abspath(args.out)
    else:
        args.out = None

    global model_obj
    if args.local:
        # TODO 用户必须手动创建好所有目录,不然会报错 -> 自动创建所有目录
        # Download COCO trained weights from Releases if needed
        if not os.path.exists(args.model_path):
            from mrcnn import utils
            utils.download_trained_weights(args.model_path)
        model_obj = model_initial()
    else:
        model_obj = model_initial(args.host + ':' + args.port)

    if args.daemon:
        if args.verbose > 0:
            print('launching socket server...')
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serversock:
            serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            serversock.bind((args.sockip, args.sockport))
            serversock.listen(5)  # become a server socket
            print('listening on:', (args.sockip, args.sockport))
            serversock.setblocking(False)
            sel.register(serversock, selectors.EVENT_READ, data=None)

            while True:
                events = sel.select(timeout=None)
                for key, mask in events:
                    if key.data is None:
                        accept_wrapper(key.fileobj)
                    else:
                        service_connection(key, mask)
    else:
        clientCLI(args.file, args.out)
    def get(self, picture_name):

        s3 = boto3.resource('s3')
        obj = s3.Object(bucket_name='ndpainteddogs', key=picture_name)
        response = obj.get()
        data = response['Body'].read()

        nparr = np.fromstring(data, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        imor = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # 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

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

        # MS COCO Dataset
        import coco
        config = coco.CocoConfig()
        COCO_DIR = "./coco"  # 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()

        # 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

        # Inspect the model in training or inference modes
        # values: 'inference' or 'training'
        # TODO: code for 'training' test mode not ready yet
        TEST_MODE = "inference"

        # Build validation dataset
        dataset = coco.CocoDataset()
        dataset.load_coco(COCO_DIR, "minival")

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

        # Create model in inference mode
        with tf.device(DEVICE):
            model = modellib.MaskRCNN(mode="inference",
                                      model_dir=MODEL_DIR,
                                      config=config)

        # Set weights file path
        weights_path = COCO_MODEL_PATH

        # Load weights
        print("Loading weights ", weights_path)
        model.load_weights(weights_path, by_name=True)

        from skimage.transform import resize
        im = resize(imor, (1024, 1024), mode="constant", preserve_range=True)
        # Run object detection
        results = model.detect([im], verbose=1)

        def apply_mask(image, mask, color, alpha=1):
            """Apply the given mask to the image.
            """
            for c in range(3):
                image[:, :, c] = np.where(
                    mask == 0,
                    image[:, :, c] * (1 - alpha) + alpha * color[c] * 255,
                    image[:, :, c])
            return image

        # Display results
        r = results[0]
        unique_class_ids = np.unique(r['class_ids'])
        mask_area = [
            np.sum(r['masks'][:, :, np.where(r['class_ids'] == i)[0]])
            for i in unique_class_ids
        ]
        top_ids = [
            v[0] for v in sorted(zip(unique_class_ids, mask_area),
                                 key=lambda r: r[1],
                                 reverse=True) if v[1] > 0
        ]
        # Generate images and titles
        for i in range(1):
            class_id = top_ids[i] if i < len(top_ids) else -1
            # Pull masks of instances belonging to the same class.
            m = r['masks'][:, :, np.where(r['class_ids'] == class_id)[0]]
            m = np.sum(m * np.arange(1, m.shape[-1] + 1), -1)
        image_cropped = im.astype(np.uint32).copy()
        image_cropped = apply_mask(image_cropped, m, (0, 0, 0))
        TARGET_PIXEL_AREA = 160000.0
        ratio = float(imor.shape[1]) / float(imor.shape[0])
        """Calculate the new height"""
        new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
        """Calculate the new width"""
        new_w = int((new_h * ratio) + 0.5)
        imnew = image_cropped.astype(np.uint8)
        imnew = cv2.resize(imnew, (new_w, new_h))
        almost = cv2.imencode('.jpg',
                              cv2.cvtColor(imnew,
                                           cv2.COLOR_RGB2BGR))[1].tostring()
        #return flask.send_file(io.BytesIO(almost), mimetype='image/jpeg')
        s3.Bucket('ndprocessedimages').put_object(Key=picture_name,
                                                  Body=almost)
        response = {"statusCode": 200}
        return response
Exemple #7
0
ROOT_DIR = os.path.abspath("../")
current_dir = os.path.dirname(os.path.abspath(__file__))

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library

from mrcnn import utils
from mrcnn import model as modellib
from mrcnn import visualize
import danger

MODEL_DIR = os.path.join(current_dir, "models")
DANGER_MODEL_PATH = os.path.join(MODEL_DIR, "mask_rcnn_danger.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(DANGER_MODEL_PATH):
    utils.download_trained_weights(DANGER_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

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


class InferenceConfig(danger.DangerConfig):
    # 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
Exemple #8
0
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco

# get_ipython().run_line_magic('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")
COCO_MODEL_PATH = os.path.join(MODEL_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)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")


# ## Configurations
# 
# We'll be using a model trained on the MS-COCO dataset. The configurations of this model are in the ```CocoConfig``` class in ```coco.py```.
# 
# For inferencing, modify the configurations a bit to fit the task. To do so, sub-class the ```CocoConfig``` class and override the attributes you need to change.

# In[2]:


class InferenceConfig(coco.CocoConfig):
Exemple #9
0
def main():
    '''Takes the path of the input directory and the path of the output directory and puts the segmented structures with and without the mask expansion in the
	output directory.'''

    parser = argparse.ArgumentParser(
        description=
        "Select the chemical structures from a scanned literature and save them"
    )
    # Input Arguments
    parser.add_argument(
        '--input_dir',
        help='Enter the name of the directory with the input images',
        required=True)
    parser.add_argument(
        '--output_dir',
        help='Enter the name of the directory for the segmented output images',
        required=True)
    args = parser.parse_args()

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

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join("model_trained/mask_rcnn_molecule.h5")

    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Image Path
    #IMAGE_DIR = os.path.join("/media/data_drive/Kohulan/After-Meeting_20190522/MaskRCNN/Test")
    config = moldetect.MolDetectConfig()

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

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    class_names = ['BG', 'Molecule']

    # Apply the model to every image in the input directory, save the result, apply the mask expansion and save the result again.
    with open(str(args.output_dir) + "/report.txt", "w") as output:
        output.write(
            "File name \t Model detection time \t Expansion time \t Number of depictions \n"
        )
    for file in os.listdir(args.input_dir):
        # Apply Mask R CNN model
        input_dir = args.input_dir
        t0 = time.time()
        r = get_masks(input_dir=input_dir, filename=file, model=model)
        t1 = time.time()
        model_seg = save_segments(expanded_masks=r['masks'],
                                  input_dir=input_dir,
                                  filename=file,
                                  output_dir=args.output_dir,
                                  mask_expansion=False)
        print(model_seg)

        # Expand masks to surround the complete structure
        image = skimage.io.imread(input_dir + "/" + file)
        t2 = time.time()
        expanded_masks = complete_structure.complete_structure_mask(
            image_array=image, mask_array=r['masks'], debug=False)
        t3 = time.time()
        final_seg = save_segments(expanded_masks=expanded_masks,
                                  input_dir=input_dir,
                                  filename=file,
                                  output_dir=args.output_dir,
                                  mask_expansion=True)
        print(final_seg)

        with open(str(args.output_dir) + "./report.txt", "a") as output:
            output.write(
                str(file) + "\t" + str(t1 - t0) + "\t" + str(t3 - t2) + "\t" +
                str(r["masks"].shape[2]) + "\n")
Exemple #10
0
import stone

K.clear_session()
K.set_learning_phase(0)

##############################################################################
# Load model
##############################################################################

# Model Directory
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
DEFAULT_WEIGHTS = os.path.join(ROOT_DIR, "mask_rcnn_stone_0001.h5")
print(DEFAULT_WEIGHTS)
# Download COCO trained weights from Releases if needed
if not os.path.exists(DEFAULT_WEIGHTS):
    utils.download_trained_weights(DEFAULT_WEIGHTS)

##############################################################################
# Load configuration
##############################################################################

# Load Configuration Model
config = stone.StoneConfig()


# Override the training configurations with a few changes for inference.
class InferenceConfig(config.__class__):
    # Run detection on one image at a time
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
Exemple #11
0
def train_map(lr, lm, tpri, rpr, dmc, wd):
    config = dogConfig()
    config.LEARNING_RATE = lr
    config.LEARNING_MOMENTUM = lm
    config.STEPS_PER_EPOCH = 100  # pastovus
    config.VALIDATION_STEPS = 50  # dydziai
    config.TRAIN_ROIS_PER_IMAGE = int(tpri)
    config.ROI_POSITIVE_RATIO = rpr
    config.DETECTION_MIN_CONFIDENCE = dmc
    config.WEIGHT_DECAY = wd

    config.display()
    model = modellib.MaskRCNN(mode="training",
                              config=config,
                              model_dir=DEFAULT_LOGS_DIR)

    weights_path = COCO_WEIGHTS_PATH
    # Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)

    model.load_weights(weights_path,
                       by_name=True,
                       exclude=[
                           "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox",
                           "mrcnn_mask"
                       ])
    """Train the model."""
    # Training dataset.
    dataset_train = dogDataset()
    dataset_train.load_dog(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = dogDataset()
    dataset_val.load_dog(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")
    his = model.train(dataset_train,
                      dataset_val,
                      learning_rate=config.LEARNING_RATE,
                      epochs=1,
                      layers='heads')

    config1 = inferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir='/logs',
                              config=config1)

    weights_path = model.find_last()
    print('weights: ', weights_path)
    model.load_weights(weights_path, by_name=True)

    image_ids = np.random.choice(dataset_val.image_ids, 50)
    APs = get_map(image_ids, dataset_val, config1, model)
    print('mAP: ', np.mean(APs))
    return np.mean(APs)
Exemple #12
0
def train(config):

    # Training dataset
    dataset_train = ImageDataset(config)
    dataset_train.load(config['dataset']['train_indices'], augment=True)
    dataset_train.prepare()

    # Validation dataset
    dataset_val = ImageDataset(config)
    dataset_val.load(config['dataset']['val_indices'])
    dataset_val.prepare()

    # Load config
    image_shape = config['model']['settings']['image_shape']
    config['model']['settings']['image_min_dim'] = min(image_shape)
    config['model']['settings']['image_max_dim'] = max(image_shape)
    train_config = MaskConfig(config['model']['settings'])
    train_config.STEPS_PER_EPOCH = dataset_train.indices.size/(train_config.IMAGES_PER_GPU*train_config.GPU_COUNT)
    train_config.display()

    # Create directory if it doesn't currently exist
    utils.mkdir_if_missing(config['model']['path'])

    # Create the model.
    model = modellib.MaskRCNN(mode='training', config=train_config,
                              model_dir=config['model']['path'])


    # Select weights file to load
    if config['model']['weights'].lower() == "coco":
        weights_path = os.path.join(config['model']['path'], 'mask_rcnn_coco.h5')
        # Download weights file
        if not os.path.exists(weights_path):
            utilslib.download_trained_weights(weights_path)
    elif config['model']['weights'].lower() == "last":
        # Find last trained weights
        weights_path = model.find_last()
    elif config['model']['weights'].lower() == "imagenet":
        # Start from ImageNet trained weights
        weights_path = model.get_imagenet_weights()
    else:
        weights_path = config['model']['weights']

    # Load weights
    exclude_layers = []
    print("Loading weights ", weights_path)
    if config['model']['weights'].lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
        if config['model']['settings']['image_channel_count'] == 1:
            exclude_layers = ['conv1']
        exclude_layers += ["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]
        model.load_weights(weights_path, by_name=True, exclude=exclude_layers)
    elif config['model']['weights'].lower() == "imagenet":
        if config['model']['settings']['image_channel_count'] == 1:
            exclude_layers = ['conv1']
        model.load_weights(weights_path, by_name=True, exclude=exclude_layers)
    elif config['model']['weights'].lower() != "new":
        model.load_weights(weights_path, by_name=True)

    # save config in run folder
    config.save(os.path.join(config['model']['path'], config['save_conf_name']))

    # train and save weights to model_path
    model.train(dataset_train, dataset_val, learning_rate=train_config.LEARNING_RATE,
                epochs=config['model']['epochs'], layers='all')

    # save in the models folder
    current_datetime = time.strftime("%Y%m%d-%H%M%S")
    model_path = os.path.join(config['model']['path'], "mask_rcnn_{}_{}.h5".format(train_config.NAME, current_datetime))
    model.keras_model.save_weights(model_path)
Exemple #13
0
    def train(self, augmentation=None, verbose=1):
        keras.backend.clear_session()
        from .nnmrcnn import Dataset
        import mrcnn.model as modellib
        from mrcnn import utils
        from imgaug import augmenters as iaa
        if verbose:
            self.CONFIG.display()
        model = modellib.MaskRCNN(mode="training",
                                  config=self.CONFIG,
                                  model_dir=self.LOG_DIR)

        # Select weights file to load
        assert self.CONFIG.WEIGHTS and type(self.CONFIG.WEIGHTS) is str

        if self.CONFIG.WEIGHTS.lower() == "coco":
            weights_path = os.path.join(self.LOG_DIR, "mask_rcnn_coco.h5")
            # Download weights file
            if not os.path.exists(weights_path):
                utils.download_trained_weights(weights_path)
        elif self.CONFIG.WEIGHTS.lower() == "last":
            # Find last trained weights
            weights_path = model.find_last()
        elif self.CONFIG.WEIGHTS.lower() == "imagenet":
            # Start from ImageNet trained weights
            weights_path = model.get_imagenet_weights()
        else:
            weights_path = self.CONFIG.WEIGHTS

        # Load weights
        if verbose:
            print("Loading weights ", weights_path)
        if self.CONFIG.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)

        # Training dataset.
        if verbose:
            print("Prepare train data")
        dataset_train = Dataset()
        dataset_train.load_numberplate("train", self.CONFIG)
        dataset_train.prepare()

        # Validation dataset
        if verbose:
            print("Prepare validation data")
        dataset_val = Dataset()
        dataset_val.load_numberplate("val", self.CONFIG)
        dataset_val.prepare()

        # Image augmentation
        # http://imgaug.readthedocs.io/en/latest/source/augmenters.html
        augmentation_default = iaa.SomeOf((0, 2), [
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5),
            iaa.OneOf([
                iaa.Affine(rotate=90),
                iaa.Affine(rotate=180),
                iaa.Affine(rotate=270)
            ]),
            iaa.Multiply((0.8, 1.5)),
            iaa.GaussianBlur(sigma=(0.0, 5.0))
        ])
        augmentation = augmentation or augmentation_default

        # *** 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.
        if verbose:
            print("Training network")
        model.train(dataset_train,
                    dataset_val,
                    learning_rate=self.CONFIG.LEARNING_RATE,
                    epochs=self.CONFIG.EPOCHS,
                    augmentation=augmentation,
                    layers=self.CONFIG.LAYERS)
Exemple #14
0
def run(is_distributed, logs_dir):
    from dataset import ShapesDataset
    from mrcnn.config import Config

    ######################
    class ShapesConfig(Config):
        NAME = "shapes"
        GPU_COUNT = 2
        IMAGES_PER_GPU = 2
        NUM_CLASSES = 1 + 3
        IMAGE_MIN_DIM = 128
        IMAGE_MAX_DIM = 128
        RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)
        TRAIN_ROIS_PER_IMAGE = 32
        STEPS_PER_EPOCH = 10
        VALIDATION_STEPS = 5

    config = ShapesConfig()
    config.display()

    # Training dataset
    dataset_train = ShapesDataset()
    dataset_train.load_shapes(500000, config.IMAGE_SHAPE[0],
                              config.IMAGE_SHAPE[1])
    dataset_train.prepare()

    # Validation dataset
    dataset_val = ShapesDataset()
    dataset_val.load_shapes(5000, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
    dataset_val.prepare()
    if is_distributed:
        import mrcnn.distributed_model as modellib
    else:
        import mrcnn.model as modellib

    from mrcnn import utils

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

    # number of found devices by TF
    from tensorflow.python.client import device_lib
    device_lib.list_local_devices()

    # Create model in training mode
    model = modellib.MaskRCNN("training", config, logs_dir)

    # Load weights trained on MS COCO, but skip layers that
    # are different due to the different number of classes
    # See README  @ https://github.com/matterport/Mask_RCNNfor instructions to download the COCO weights
    model.load_weights(COCO_MODEL_PATH,
                       by_name=True,
                       exclude=[
                           "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox",
                           "mrcnn_mask"
                       ])

    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=1000,
                layers='heads')
Exemple #15
0
def detect_and_color_splash(model, image_path=None, video_path=None):
    assert image_path or video_path

    # Image or video?
    if image_path:
        # Run model detection and generate the color splash effect
        print("Running on {}".format(args.image))
        # Read image
        image = skimage.io.imread(args.image)
        # Detect objects
        r = model.detect([image], verbose=1)[0]
        # Color splash
        splash = color_splash(image, r['masks'])
        # Save output
        file_name = "splash_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
        skimage.io.imsave(file_name, splash)
    elif video_path:
        import cv2
        # Video capture
        vcapture = cv2.VideoCapture(video_path)
        width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = vcapture.get(cv2.CAP_PROP_FPS)

        # Define codec and create video writer
        file_name = "splash_{:%Y%m%dT%H%M%S}.avi".format(datetime.datetime.now())
        vwriter = cv2.VideoWriter(file_name,
                                  cv2.VideoWriter_fourcc(*'MJPG'),
                                  fps, (width, height))
        count = 0
        success = True
        while success:
            print("frame: ", count)
            # Read next image
            success, image = vcapture.read()
            if success:
                # OpenCV returns images as BGR, convert to RGB
                image = image[..., ::-1]
                # Detect objects
                r = model.detect([image], verbose=0)[0]
                # Color splash
                splash = color_splash(image, r['masks'])
                # RGB -> BGR to save image to video
                splash = splash[..., ::-1]
                # Add image to video writer
                vwriter.write(splash)
                count += 1
        vwriter.release()
    print("Saved to ", file_name)
    if __name__ == '__main__':
        import argparse

        # Parse command line arguments
        parser = argparse.ArgumentParser(
            description='Train Mask R-CNN to detect hands.')
        parser.add_argument("command",
                            metavar="<command>",
                            help="'train' or 'splash'")
        parser.add_argument('--dataset', required=False,
                            metavar="/path/to/hand/dataset/",
                            help='Directory of the hand dataset')
        parser.add_argument('--weights', required=True,
                            metavar="/path/to/weights.h5",
                            help="Path to weights .h5 file or 'coco'")
        parser.add_argument('--logs', required=False,
                            default=DEFAULT_LOGS_DIR,
                            metavar="/path/to/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 = HandConfig()
        else:
            class InferenceConfig(HandConfig):
                # 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))
Exemple #16
0
    print("Logs: ", args.logs)

    # Configurations
    config = YCBConfig()

    # Create model
    model = modellib.MaskRCNN(mode="training",
                              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,
Exemple #17
0
def facade():
    points_group = []
    # Import COCO config
    sys.path.append(os.path.join(ROOT_DIR,
                                 "samples/coco/"))  # To find local version

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(MODEL_DIR, "mask_rcnn_shapes_0030.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)
        print("Downloading***********************")

    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)
    # COCO Class names
    # Index of the class in the list is its ID. For example, to get ID of
    # the teddy bear class, use: class_names.index('teddy bear')
    class_names = ['BG', 'facade']

    image = skimage.io.imread(os.path.join("../test/image.png"))
    image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)

    img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    img_gray = cv2.GaussianBlur(img_gray, (3, 3), 0)
    a = datetime.now()
    # Run detection
    results = model.detect([image], verbose=1)
    b = datetime.now()
    # Visualize results
    print("shijian", (b - a).seconds)
    r = results[0]

    facades_mask = []
    facades_w = []
    facades_h = []

    for i in range(len(r['masks'][0, 0, :])):
        mask = r['masks'][:, :, i]

        w = np.sum(np.max(mask, axis=0))
        facades_w.append(w)
        h = np.sum(np.max(mask, axis=1))
        facades_h.append(h)
        print("w=", w, " h=", h)

        kernel = np.ones((int(h / 10), int(w / 10)), np.uint8)
        mask = mask.astype('uint8')
        dilation = cv2.dilate(mask, kernel, iterations=1)
        mask_work = dilation

        # building edges
        edgs_detail = cv2.Canny(img_gray, 50, 150, apertureSize=3)

        # building mask
        building_mask = np.bitwise_and(edgs_detail, dilation)

        # density of building
        mask_area = area(mask)
        builde_area = area(np.bitwise_and(edgs_detail, mask))
        density = builde_area / mask_area
        print(density)

        if w > len(image[0]) / 4 and density > 0.20:
            kernel2 = np.ones((int(h / 10), int(w / 3)), np.uint8)
            mask_erode = cv2.erode(mask, kernel2, iterations=1)
            mask_work = np.bitwise_xor(mask_erode, dilation)

        # building facade part
        building_details = np.bitwise_and(edgs_detail, mask_work)

        facades_mask.append(building_details)

    result = image.copy()
    for i in range(len(facades_mask)):

        building_details = facades_mask[i]
        w = facades_w[i]
        h = facades_h[i]
        h_min = int(h / 8)
        vertical_lines = cv2.HoughLines(building_details, 1, np.pi / 180,
                                        h_min)
        max_vertical = -1
        min_vertical = 30000
        max_vindex = -1
        min_vindex = 30000
        # print(vertical_lines)
        # print("###########################")
        num_max = math.sqrt((w / building_details.shape[1])) * 30
        #num_max = (w/building_details.shape[1])*20
        if vertical_lines is not None:
            num = 0
            for k, line in enumerate(vertical_lines):
                if num > num_max:
                    break
                rho = line[0][0]  #第一个元素是距离rho
                theta = line[0][1]  #第二个元素是角度theta
                if (theta <
                    (1.5 * np.pi / 18.)) or (theta >
                                             (16. * np.pi / 18.0)):  #垂直直线
                    num += 1
                    pt1 = (int(rho / np.cos(theta)), 0)  #该直线与第一行的交点
                    #该直线与最后一行的焦点
                    pt2 = (int((rho - result.shape[0] * np.sin(theta)) /
                               np.cos(theta)), result.shape[0])
                    #cv2.line(byproduction, pt1, pt2, (255)) # 绘制一条白线
                    if (int(rho / np.cos(theta)) > max_vertical):
                        max_vertical = rho / np.cos(theta)
                        max_vindex = k
                    elif (int(rho / np.cos(theta)) < min_vertical):
                        min_vertical = rho / np.cos(theta)
                        min_vindex = k
        if min_vindex != 30000 and max_vindex != -1:
            medle_line = midle_vline(vertical_lines[max_vindex],
                                     vertical_lines[min_vindex])
            if h / w >= 2:
                horizontal_lines = cv2.HoughLines(building_details, 1,
                                                  np.pi / 180, int(w / 8))
                #horizontal_lines = horizontal_lines_order(horizontal_lines,building_details)
            else:
                horizontal_lines = cv2.HoughLines(building_details, 1,
                                                  np.pi / 180, int(w / 4))
                #horizontal_lines = horizontal_lines_order(horizontal_lines,building_details)
            max_horizontal = -1
            min_horizontal = 30000
            max_hindex = -1
            min_hindex = 30000
            num_max = math.sqrt(h / building_details.shape[0]) * 20
            #num_max = (w/building_details.shape[1])*20
            if horizontal_lines is not None:
                num = 0
                for j, line in enumerate(horizontal_lines):
                    if num > num_max:
                        break
                    rho = line[0][0]  #第一个元素是距离rho
                    theta = line[0][1]  #第二个元素是角度theta
                    if (theta > (5. * np.pi / 18.0) and theta <
                        (13. * np.pi / 18.0)):  #水平直线
                        num += 1
                        pt1 = (0, int(rho / np.sin(theta)))  # 该直线与第一列的交点
                        y = intersection(medle_line, line)[0][1]
                        #该直线与最后一列的交点
                        pt2 = (result.shape[1],
                               int((rho - result.shape[1] * np.cos(theta)) /
                                   np.sin(theta)))
                        #cv2.line(byproduction, pt1, pt2, (255), 1)
                        #                     if (int(rho/np.sin(theta))> max_horizontal):
                        #                         max_horizontal = int(rho/np.sin(theta))
                        #                         max_hindex = j
                        #                     if (int(rho/np.sin(theta))< min_horizontal):
                        #                         min_horizontal = int(rho/np.sin(theta))
                        #                         min_hindex = j
                        if y > max_horizontal:
                            max_horizontal = y
                            max_hindex = j
                        elif y < min_horizontal:
                            min_horizontal = y
                            min_hindex = j
        print(min_hindex, max_hindex, min_vindex, max_vindex)
        if (min_hindex != 30000 and min_vindex != 30000 and max_hindex != -1
                and max_vindex != -1):
            segmented = [[
                vertical_lines[max_vindex], vertical_lines[min_vindex]
            ], [horizontal_lines[max_hindex], horizontal_lines[min_hindex]]]
            intersections = segmented_intersections(segmented)

            if len(intersections) == 4:
                intersection0 = tuple(intersections[0][0])
                intersection1 = tuple(intersections[1][0])
                intersection2 = tuple(intersections[3][0])
                intersection3 = tuple(intersections[2][0])
                intersection_group = [
                    intersection0, intersection1, intersection2, intersection3
                ]
                points_group.append(intersection_group)
    print(points_group)
    return points_group
Exemple #18
0
    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=[
Exemple #19
0
import cv2
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import random

from mrcnn import utils
import mrcnn.model as modellib

import coco as coco

if not os.path.join("mask_rcnn_coco.h5"):
    utils.download_trained_weights("mask_rcnn_coco.h5")


class InferenceConfig(coco.CocoConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

class_names = ['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',