This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from detectron2.data.datasets import register_coco_instances | |
register_coco_instances("fruits_nuts", {}, "./data/trainval.json", "./data/images") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# download, decompress the data | |
!wget https://github.com/Tony607/detectron2_instance_segmentation_demo/releases/download/V0.1/data.zip | |
!unzip data.zip > /dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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 |
NewerOlder