|
|
import os |
|
|
import sys |
|
|
import random |
|
|
import math |
|
|
import re |
|
|
import time |
|
|
import numpy as np |
|
|
import cv2 |
|
|
import matplotlib |
|
|
import matplotlib.pyplot as plt |
|
|
import pandas as pd |
|
|
import pdb |
|
|
from sklearn.model_selection import train_test_split |
|
|
import glob |
|
|
|
|
|
|
|
|
|
|
|
ROOT_DIR = os.path.abspath("./Mask_RCNN/") |
|
|
|
|
|
|
|
|
sys.path.append(ROOT_DIR) |
|
|
from mrcnn.config import Config |
|
|
from mrcnn import utils |
|
|
import mrcnn.model as modellib |
|
|
from mrcnn import visualize |
|
|
from mrcnn.model import log |
|
|
|
|
|
|
|
|
MODEL_DIR = os.path.join(ROOT_DIR, "logs") |
|
|
|
|
|
|
|
|
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") |
|
|
|
|
|
|
|
|
if not os.path.exists(COCO_MODEL_PATH): |
|
|
utils.download_trained_weights(COCO_MODEL_PATH) |
|
|
|
|
|
|
|
|
class MineSectorConfig(Config): |
|
|
"""Configuration for training on the toy shapes dataset. |
|
|
Derives from the base Config class and overrides values specific |
|
|
to the toy shapes dataset. |
|
|
""" |
|
|
|
|
|
NAME = "mining-sectors" |
|
|
|
|
|
|
|
|
|
|
|
GPU_COUNT = 1 |
|
|
IMAGES_PER_GPU = 8 |
|
|
|
|
|
|
|
|
NUM_CLASSES = 1 + 9 |
|
|
|
|
|
|
|
|
|
|
|
IMAGE_MIN_DIM = 128 |
|
|
IMAGE_MAX_DIM = 128 |
|
|
|
|
|
|
|
|
RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128) |
|
|
|
|
|
|
|
|
|
|
|
TRAIN_ROIS_PER_IMAGE = 32 |
|
|
|
|
|
|
|
|
STEPS_PER_EPOCH = 100 |
|
|
|
|
|
|
|
|
VALIDATION_STEPS = 5 |
|
|
|
|
|
|
|
|
config = MineSectorConfig() |
|
|
config.display() |
|
|
|
|
|
|
|
|
class MineSectDataset(utils.Dataset): |
|
|
|
|
|
def __init__(self, path): |
|
|
self.path = path |
|
|
self.mine_ids = [] |
|
|
|
|
|
|
|
|
def load_mine_sectors(self): |
|
|
"""Generate the requested number of synthetic images. |
|
|
count: number of images to generate. |
|
|
height, width: the size of the generated images. |
|
|
""" |
|
|
|
|
|
self.add_class("shapes", 1, "square") |
|
|
self.add_class("shapes", 2, "circle") |
|
|
self.add_class("shapes", 3, "triangle") |
|
|
|
|
|
|
|
|
''' |
|
|
self.add_class("mine_sector", 3, "lh") |
|
|
self.add_class("mine_sector", 4, "mf") |
|
|
self.add_class("mine_sector", 5, "op") |
|
|
self.add_class("mine_sector", 6, "pp") |
|
|
self.add_class("mine_sector", 7, "sy") |
|
|
self.add_class("mine_sector", 8, "tsf") |
|
|
self.add_class("mine_sector", 9, "wr") |
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df = create_df() |
|
|
print('Total Images: ', len(df)) |
|
|
mine_ids = np.array([]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for patch in df['id'].values: |
|
|
mine_id = int(patch.split(".")[0]) |
|
|
if mine_id not in mine_ids: |
|
|
self.mine_ids = np.append(self.mine_ids, mine_id) |
|
|
|
|
|
|
|
|
def load_image(self, filename): |
|
|
|
|
|
|
|
|
pdb.set_trace() |
|
|
fpath = os.path.join(self.path, filename) |
|
|
image = cv2.imread(fpath) |
|
|
return image |
|
|
|
|
|
def load_mask(self, filename): |
|
|
|
|
|
|
|
|
pdb.set_trace() |
|
|
fpath = os.path.join(self.path, filename) |
|
|
image = cv2.imread(fpath) |
|
|
return image |
|
|
|
|
|
|
|
|
def image_reference(self, image_id): |
|
|
"""Return the shapes data of the image.""" |
|
|
info = self.image_info[image_id] |
|
|
if info["source"] == "shapes": |
|
|
return info["shapes"] |
|
|
else: |
|
|
super(self.__class__).image_reference(self, image_id) |
|
|
|
|
|
|
|
|
IMG_PATH = 'dataset/images/' |
|
|
MASK_PATH = 'dataset/masks/' |
|
|
|
|
|
batch_size = 32 |
|
|
num_classes = 10 |
|
|
|
|
|
|
|
|
|
|
|
def create_df(): |
|
|
name = [] |
|
|
for dir, subdir, filenames in os.walk(IMG_PATH): |
|
|
for filename in filenames: |
|
|
name.append(filename[:-4]) |
|
|
|
|
|
return pd.DataFrame({'id': name}, index=np.arange(0, len(name))) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
|
|
|
mine_sect_ds = MineSectDataset(IMG_PATH) |
|
|
mine_sect_ds.load_mine_sectors() |
|
|
mine_sect_ds.load_image(mine_sect_ds.mine_ids[0]) |
|
|
|
|
|
|
|
|
|
|
|
MID_trainval, MID_test = train_test_split(mine_ids, test_size=0.15, random_state=42) |
|
|
MID_train, MID_val = train_test_split(MID_trainval, test_size=0.25, random_state=42) |
|
|
|
|
|
X_train = np.array([]) |
|
|
for id in MID_train: |
|
|
X_train = np.append(X_train, np.transpose([os.path.basename(x) for x in glob.glob(os.path.join(IMG_PATH, str(int(id)) + "*.tif"))])) |
|
|
|
|
|
X_val = np.array([]) |
|
|
for id in MID_val: |
|
|
X_val = np.append(X_val, np.transpose([os.path.basename(x) for x in glob.glob(os.path.join(IMG_PATH, str(int(id)) + "*.tif"))])) |
|
|
|
|
|
X_test = np.array([]) |
|
|
for id in MID_test: |
|
|
X_test = np.append(X_test, np.transpose([os.path.basename(x) for x in glob.glob(os.path.join(IMG_PATH, str(int(id)) + "*.tif"))])) |
|
|
|
|
|
|
|
|
print('Train Size: ', len(X_train)) |
|
|
print('Validation Size: ', len(X_val)) |
|
|
print('Test Size: ', len(X_test)) |
|
|
|