Skip to content

Instantly share code, notes, and snippets.

View Tony607's full-sized avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / smooth_dice_coeff.py
Created November 2, 2019 11:24
Automatic Defect Inspection with End-to-End Deep Learning | DLology
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 November 2, 2019 11:23
Automatic Defect Inspection with End-to-End Deep Learning | DLology
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 October 14, 2019 05:35
How to train Detectron2 with Custom COCO Datasets | DLology
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 October 14, 2019 05:35
How to train Detectron2 with Custom COCO Datasets | DLology
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 October 14, 2019 05:35
How to train Detectron2 with Custom COCO Datasets | DLology
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 October 14, 2019 05:35
How to train Detectron2 with Custom COCO Datasets | DLology
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 October 14, 2019 05:35
How to train Detectron2 with Custom COCO Datasets | DLology
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 October 14, 2019 05:34
How to train Detectron2 with Custom COCO Datasets | DLology
from detectron2.data.datasets import register_coco_instances
register_coco_instances("fruits_nuts", {}, "./data/trainval.json", "./data/images")
@Tony607
Tony607 / download.py
Created October 14, 2019 05:34
How to train Detectron2 with Custom COCO Datasets | DLology
# 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 October 14, 2019 05:33
How to train Detectron2 with Custom COCO Datasets | DLology
!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