Skip to content

Instantly share code, notes, and snippets.

Avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / smooth_dice_coeff.py
Created Nov 2, 2019
Automatic Defect Inspection with End-to-End Deep Learning | DLology
View smooth_dice_coeff.py
from tensorflow.keras import backend as K
def smooth_dice_coeff(smooth=1.):
smooth = float(smooth)
# IOU or dice coeff calculation
def IOU_calc(y_true, y_pred):
y_true_f = K.flatten(y_true)
@Tony607
Tony607 / get_small_unet.py
Created Nov 2, 2019
Automatic Defect Inspection with End-to-End Deep Learning | DLology
View get_small_unet.py
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Lambda, Conv2DTranspose, concatenate
def get_small_unet():
inputs = Input((img_rows, img_cols, 1))
inputs_norm = Lambda(lambda x: x/127.5 - 1.)
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
@Tony607
Tony607 / benchmark.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View benchmark.py
import time
times = []
for i in range(20):
start_time = time.time()
outputs = predictor(im)
delta = time.time() - start_time
times.append(delta)
mean_delta = np.array(times).mean()
fps = 1 / mean_delta
print("Average(sec):{:.2f},fps:{:.2f}".format(mean_delta, fps))
@Tony607
Tony607 / predict.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View predict.py
from detectron2.utils.visualizer import ColorMode
for d in random.sample(dataset_dicts, 3):
im = cv2.imread(d["file_name"])
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1],
metadata=fruits_nuts_metadata,
scale=0.8,
instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels
)
@Tony607
Tony607 / predictor.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View predictor.py
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set the testing threshold for this model
cfg.DATASETS.TEST = ("fruits_nuts", )
predictor = DefaultPredictor(cfg)
@Tony607
Tony607 / train.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View train.py
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
import os
cfg = get_cfg()
cfg.merge_from_file(
"./detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
)
cfg.DATASETS.TRAIN = ("fruits_nuts",)
cfg.DATASETS.TEST = () # no metrics implemented for this dataset
@Tony607
Tony607 / vis.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View vis.py
import random
from detectron2.utils.visualizer import Visualizer
for d in random.sample(dataset_dicts, 3):
img = cv2.imread(d["file_name"])
visualizer = Visualizer(img[:, :, ::-1], metadata=fruits_nuts_metadata, scale=0.5)
vis = visualizer.draw_dataset_dict(d)
cv2_imshow(vis.get_image()[:, :, ::-1])
@Tony607
Tony607 / register.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View register.py
from detectron2.data.datasets import register_coco_instances
register_coco_instances("fruits_nuts", {}, "./data/trainval.json", "./data/images")
@Tony607
Tony607 / download.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View download.py
# download, decompress the data
!wget https://github.com/Tony607/detectron2_instance_segmentation_demo/releases/download/V0.1/data.zip
!unzip data.zip > /dev/null
@Tony607
Tony607 / install.py
Created Oct 14, 2019
How to train Detectron2 with Custom COCO Datasets | DLology
View install.py
!pip install -U torch torchvision
!pip install git+https://github.com/facebookresearch/fvcore.git
!git clone https://github.com/facebookresearch/detectron2 detectron2_repo
!pip install -e detectron2_repo