ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Deep Learning - 3
    machine learning 2020. 12. 10. 11:52

    진행했던 프로젝트의 목표가 차량 손상 인식이기 때문에 Image Segmentation을 해주는 모델이 필요했다. 좋은 모델을 찾던 와중 Mask-RCNN을 알게 되었다. Mask-RCNN에 대한 자세한 설명은 생략하고.. 사용 방법부터 정리해 보겠다.

    https://github.com/matterport/Mask_RCNN

     

    matterport/Mask_RCNN

    Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow - matterport/Mask_RCNN

    github.com

    https://github.com/SriRamGovardhanam/wastedata-Mask_RCNN-multiple-classes

     

    SriRamGovardhanam/wastedata-Mask_RCNN-multiple-classes

    predicting more than 2 classes from publicly available image data, for annotation we are using VGG annotator latest version - SriRamGovardhanam/wastedata-Mask_RCNN-multiple-classes

    github.com

    첫번째 링크만 사용했던 것 같은데.. 두번째 링크도 사용했던것 같기도 하고.. 일단 첫번째 링크만 clone해서 하다가 안되면 두번째 링크도 clone해야할듯 싶다.

    우선 예시를 진행해 보았다. 텐서플로 1.x버전을 사용하기 위해

    # tensorflow 버전 1.x 적용
    %tensorflow_version 1.x

    하고 gdrive와 연동을 위해

    from google.colab import drive
    drive.mount('/gdrive')
    %cd /gdrive

    해줬다.

    그 다음 requirements.txt에 써있는 것들을 설치하기위해 경로 이동한 다음

    pip install -r requirements.txt
    !python setup.py install

    설치해 주고

    samples/coco로 가서

    import coco

    해준다.

    그 후 예시대로 쭉 따라가면

    import os
    import sys
    import random
    import math
    import numpy as np
    import skimage.io
    import matplotlib
    import matplotlib.pyplot as plt
    
    # Root directory of the project
    ROOT_DIR = os.path.abspath("../")
    print(ROOT_DIR)
    # 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, "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")
    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
    
    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']
    IMAGE_DIR = '/gdrive/My Drive/COLAB/exercise/Mask_RCNN/images'
    # Load a random image from the images folder
    file_names = next(os.walk(IMAGE_DIR))[2]
    image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
    
    # Run detection
    results = model.detect([image], verbose=1)
    
    # Visualize results
    r = results[0]
    visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                                class_names, r['scores'])

    위 코드를 쭉~ 쳐주고 실행시키면

    위와 같이 segmentation을 잘 해낸것을 볼 수 있다.

    'machine learning' 카테고리의 다른 글

    Deep Learning - 6  (0) 2020.12.11
    Deep Learning - 5  (0) 2020.12.10
    Deep Learning - 4  (0) 2020.12.10
    Deep Learning - 2  (0) 2020.12.10
    Deep Learning - 1  (0) 2020.12.10
Designed by Tistory.