Skip to content

Instantly share code, notes, and snippets.

View trongan93's full-sized avatar

Trong-An (Andrew) Bui trongan93

View GitHub Profile
bgrInputImage = cv2.imread(img_path)
bgrInputImage = cv2.resize(bgrInputImage,(224,224))
rgb_resized_img = cv2.cvtColor(bgrInputImage,cv2.COLOR_BGR2RGB)
hsvInputImage_Full = cv2.cvtColor(rgb_resized_img.astype(np.float32), cv2.COLOR_RGB2HSV_FULL)
hue_full, sat_full, val_full = cv2.split(hsvInputImage_Full)
hue_16bit = np.array(hue_full,dtype=np.uint16)
# Reference from **Bidimensional Empirical Mode Decomposition** code by Dawid Laszuk (laszukdawid@gmail.com).
# This version is modified by H-BEMD for sin and cos value and optimize Extrema detection and Normolization value
# By Trong-An Bui (trongan93@gmail.com - http://buitrongan.com)
class BEMD:
def __init__(self):
# ProtoIMF related
self.mse_thr = 0.01
self.mean_thr = 0.01
self.FIXE = 1 # Single iteration by default, otherwise results are terrible
# Show the direction of landslide
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(bf_cX, bf_cY, 'go')
# ax.text(bf_cY+1, bf_cX-2, 'center point of region - before landslide')
ax.plot(at_cX, at_cY, 'ro')
# ax.text(at_cY+1, at_cX+1, 'center point of region - before landslide')
hueradians = np.deg2rad(hue_16bit)
cos_hueradians = np.cos(hueradians)
sin_hueradians = np.sin(hueradians)
# BEMD in sin value of hue channel
bemd2 = BEMD()
imfs_sin_hue = bemd2.bemd(sin_hueradians, max_imf=2)
# BEMD in cos value of hue channel
bemd = BEMD()
data_base = "/mnt/d/RarePlanes/datasets/synthetic"
data_train = data_base + "/train"
im_path = data_train + "/images/Chicago_Airport_0_0_899_10974.png"
im_test = cv2.imread(im_path)
plt.imshow(im_test)
plt.show()
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
outputs = predictor(im_test)
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
# import some common libraries
import numpy as np
import os, json, cv2, random
from matplotlib import pyplot as plt
from detectron2.data.datasets import register_coco_instances
register_coco_instances("rareplanes_dataset_train", {}, "/mnt/d/RarePlanes/datasets/synthetic/metadata_annotations/instances_train_aircraft.json", "/mnt/d/RarePlanes/datasets/synthetic/train/images")
register_coco_instances("rareplanes_dataset_val", {}, "/mnt/d/RarePlanes/datasets/synthetic/metadata_annotations/instances_test_aircraft.json", "/mnt/d/RarePlanes/datasets/synthetic/test/images")
from pycocotools.coco import COCO
json_file = "/mnt/d/RarePlanes/datasets/synthetic/metadata_annotations/instances_train_aircraft.json"
coco=COCO(json_file)
# display COCO categories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
from detectron2.engine import DefaultTrainer
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("rareplanes_dataset_train",)
cfg.DATASETS.TEST = ()
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") # Let training initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025 # pick a good LR