1. 程式人生 > >Mask_RCNN實現批量測試並儲存測試結果到資料夾內

Mask_RCNN實現批量測試並儲存測試結果到資料夾內

       Mask-RCNN程式碼Mask_RCNN/samples路徑下有一個demo.ipynb的檔案就是用來測試的,所以我們在這個基礎上更改一下,其實主要就是我們新建一個test.ipynb,然後把demo.ipynb程式碼複製過來,根據需要更改。

一、將demo程式碼更改

將demo中的程式碼

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

更改為:這裡面的h5檔案是我們訓練的結果,

COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_shapes_0005.h5")

demo中程式碼class InferenceConfig(coco.CocoConfig):也需要進行更改,要與自己的config匹配。

class InferenceConfig(shapeconfig.ShapesConfig):

ShapesConfig類中程式碼:


from mrcnn.config import Config
class ShapesConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "shapes"

    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 1 # background + 3 shapes

    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 448
    IMAGE_MAX_DIM = 640

    # Use smaller anchors because our image and objects are small
    #RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)  # anchor side in pixels
    RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6)

    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE = 32

    # Use a small epoch since the data is simple
    STEPS_PER_EPOCH = 100

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 5

二、注意

IMAGE_MIN_DIM = 448

 IMAGE_MAX_DIM = 640

是圖片的尺寸。(按照自己圖片的大小更改)

值得注意的是,Mask_RCNN/mrcnn目錄下model.py檔案中第1815行到1819行程式碼

h, w = config.IMAGE_SHAPE[:2]
        if h / 2**6 != int(h / 2**6) or w / 2**6 != int(w / 2**6):
            raise Exception("Image size must be dividable by 2 at least 6 times "
                            "to avoid fractions when downscaling and upscaling."
                            "For example, use 256, 320, 384, 448, 512, ... etc. ")

需要注意圖片尺寸的設定最好符合標準,不然會報錯。

三、批量儲存

這樣我們初步可以測試了,但是隻能在ipython中一張圖片一張圖片的測試,且是從images路徑下隨機取值。

所以我們需要更改程式碼,獲取images資料夾下圖片的張數,作為索引,在test.ipynb檔案中更改為如下程式碼:

count = os.listdir(IMAGE_DIR)
for i in range(0,len(count)):
    path = os.path.join(IMAGE_DIR, count[i])
    if os.path.isfile(path):
        file_names = next(os.walk(IMAGE_DIR))[2]
        image = skimage.io.imread(os.path.join(IMAGE_DIR, count[i]))
        # Run detection
        results = model.detect([image], verbose=1)
        r = results[0]
        visualize.display_instances(count[i],image, r['rois'], r['masks'], r['class_ids'], 
                            class_names, r['scores'])

這時,需要到visualize.py檔案中更改,在Mask_RCNN/mrcnn路徑下找到該檔案開啟,更改display_instances類程式碼,在該類中新增一個引數count,並加上plt.savefig()儲存圖片到指定路徑下:

if auto_show:
        plt.savefig("./test_results/%3s.jpg"%(str(count[4:7])))
        plt.show()

圖片命名方式是取被測試圖片的第4-7位(我的4-7位是數字)命名。(根據需要可自己改)

四、儲存結果

在test_results資料夾中可以正常顯示。