def setUp(self):
     setup_logger()
Example #2
0
    parser = argparse.ArgumentParser(description="Convert a model to Caffe2")
    parser.add_argument("--config-file",
                        default="",
                        metavar="FILE",
                        help="path to config file")
    parser.add_argument("--run-eval", action="store_true")
    parser.add_argument("--output",
                        help="output directory for the converted caffe2 model")
    parser.add_argument(
        "opts",
        help="Modify config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )
    args = parser.parse_args()
    setup_logger()
    logger = setup_logger(name="fashionnet", abbrev_name="fashionnet")
    logger.info("Command line arguments: " + str(args))

    cfg = setup_cfg(args)
    input_names = ["input"]
    # output_names = ["detection_logits", "detection_bbox_reg", "classification_logits"]
    # output_names = [[f"logits_{i}", f"bbox_reg_{i}", f"ctrness_{i}"] for i in range(3, 8)]
    # output_names = list(itertools.chain(*output_names))
    output_names = [
        "box_cls_3", "box_cls_4", "box_cls_5", "box_cls_6", "box_cls_7",
        "box_reg_3", "box_reg_4", "box_reg_5", "box_reg_6", "box_reg_7",
        "classification_logits"
    ]
    output_path = os.path.join(args.output, "fashionnet_e3_fpn_800.onnx")
    dynamic_axes = {
Example #3
0
def setup_config(args, random_seed=None, is_testing=False):
    """
    Sets up config node with probabilistic detectron elements. Also sets up a fixed random seed for all scientific
    computing libraries, and sets up all supported datasets as instances of coco.

    Args:
        args (Namespace): args from argument parser
        random_seed (int): set a fixed random seed throughout torch, numpy, and python
        is_testing (bool): set to true if inference. If true function will return an error if checkpoint directory not
        already existing.
    Returns:
        (CfgNode) detectron2 config object
    """
    # Get default detectron config file
    cfg = get_cfg()
    add_probabilistic_config(cfg)

    # Update default config file with custom config file
    configs_dir = core.configs_dir()
    args.config_file = os.path.join(configs_dir, args.config_file)
    cfg.merge_from_file(args.config_file)

    # Add dropout rate for faster RCNN box head
    cfg.MODEL.ROI_BOX_HEAD.DROPOUT_RATE = cfg.MODEL.PROBABILISTIC_MODELING.DROPOUT_RATE

    # Update config with inference configurations. Only applicable for when in
    # probabilistic inference mode.
    if args.inference_config != "":
        args.inference_config = os.path.join(configs_dir,
                                             args.inference_config)
        cfg.merge_from_file(args.inference_config)

    # Create output directory
    model_name = os.path.split(os.path.split(args.config_file)[0])[-1]
    dataset_name = os.path.split(
        os.path.split(os.path.split(args.config_file)[0])[0])[-1]

    cfg['OUTPUT_DIR'] = os.path.join(core.data_dir(), dataset_name, model_name,
                                     os.path.split(args.config_file)[-1][:-5],
                                     'random_seed_' + str(random_seed))
    if is_testing:
        if not os.path.isdir(cfg['OUTPUT_DIR']):
            raise NotADirectoryError(
                "Checkpoint directory {} does not exist.".format(
                    cfg['OUTPUT_DIR']))

    os.makedirs(cfg['OUTPUT_DIR'], exist_ok=True)

    # copy config file to output directory
    copyfile(
        args.config_file,
        os.path.join(cfg['OUTPUT_DIR'],
                     os.path.split(args.config_file)[-1]))

    # Freeze config file
    cfg['SEED'] = random_seed
    cfg.freeze()

    # Initiate default setup
    default_setup(cfg, args)

    # Setup logger for probabilistic detectron module
    setup_logger(output=cfg.OUTPUT_DIR,
                 distributed_rank=comm.get_rank(),
                 name="Probabilistic Detectron")

    # Set a fixed random seed for all numerical libraries
    if random_seed is not None:
        torch.manual_seed(random_seed)
        np.random.seed(random_seed)
        random.seed(random_seed)

    # Setup datasets
    dataset_dir = os.path.expanduser(args.dataset_dir)
    setup_all_datasets(dataset_dir)
    return cfg
Example #4
0
def build_ss_train_loader(cfg, mapper):
    """Builds a semi-supervised data loader that yields both labeled and unlabeled images.

    Data can be loaded in two modes (defined in `cfg.DATASETS.MODE`):
      - "CROSS_DATASET": labeled and unlabeled images come from two disparate datasets, e.g.
      VOCtrain and VOCtest
      - "RANDOM_SPLIT": labeled and unlabeled images come from the same dataset by splitting it
      into the labeled and unlabeled parts
    For more details see `build_ss_datasets()`.

    Each batch consists of `cfg.SOLVER.IMS_PER_BATCH_LABELED` labeled and
    `cfg.SOLVER.IMS_PER_BATCH_UNLABELED` unlabeled images, which can be modified
    in `csd/config/config.py` or in a custom `configs/*.yaml` config file
    supplied to your training script.

    The actual x-flips happen inside `AspectRatioGroupedSSDataset` that is instantiated by
    `build_ss_batch_data_loader`

    The returned tuple contains (1) a tuple of lists with dicts for labeled and unlabeled images
    and (2) a DataLoader with infinite sampling yielding a pair of batches with labeled and unlabeled
    images with the same aspect ratio within batch.

    Specifically, the returned DataLoader yields a tuple of lists:
    ([labeled_img, labeled_img_xflip], [unlabeled_im, unlabeled_img_xflip]).
    """

    # Load labeled and unlabeled dataset dicts (either use two separate ones or perform a random split)
    labeled_dataset_dicts, unlabeled_dataset_dicts = build_ss_datasets(cfg)

    # Log the datasets sizes
    if comm.is_main_process():
        logger = setup_logger(name=__name__)
        logger.debug(
            "Number of images in the labeled and unlabeled datasets: {}, {}".
            format(len(labeled_dataset_dicts), len(unlabeled_dataset_dicts)))

        # Print updated metadata counts
        print_instances_class_histogram(
            labeled_dataset_dicts,
            MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes)

    # Map metadata into actual objects (note: data augmentations also take place here)
    labeled_dataset = MapDataset(labeled_dataset_dicts, mapper)
    unlabeled_dataset = MapDataset(unlabeled_dataset_dicts, mapper)

    # Define data samplers
    assert cfg.DATALOADER.SAMPLER_TRAIN == "TrainingSampler", "Unsupported training sampler: {}".format(
        cfg.DATALOADER.SAMPLER_TRAIN)
    labeled_sampler = TrainingSampler(len(labeled_dataset))
    unlabeled_sampler = TrainingSampler(len(unlabeled_dataset))

    return (
        labeled_dataset_dicts,
        unlabeled_dataset_dicts,
    ), build_ss_batch_data_loader(  # Initialize actual dataloaders
        (labeled_dataset, unlabeled_dataset),
        (labeled_sampler, unlabeled_sampler),
        cfg.SOLVER.IMS_PER_BATCH_LABELED,
        cfg.SOLVER.IMS_PER_BATCH_UNLABELED,
        aspect_ratio_grouping=cfg.DATALOADER.ASPECT_RATIO_GROUPING,
        num_workers=cfg.DATALOADER.NUM_WORKERS,
    )
    )
    parser.add_argument("--data_dir", help="A directory for input images")

    parser.add_argument("--output",
                        help="A directory to save output visualizations")

    parser.add_argument("--checkpoint", help="A path to a checkpoint file")
    parser.add_argument("--synset_id", default="04530566")
    parser.add_argument("--sample_size")

    return parser


if __name__ == "__main__":
    args = get_parser().parse_args()
    logger = setup_logger(name="demo")
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    output_dir = args.output
    demo = VisualizationDemo(cfg, output_dir=output_dir)

    #     data_dir = './datasets/shapenet/ShapeNetV1processed'
    data_dir = args.data_dir
    dataset = MeshVoxDataset(data_dir, return_mesh=True)

    synset_ids = dataset.synset_ids
    first_idx = synset_ids.index(synset_id)
    model_num = int(synset_ids.count(synset_id) / 24)
        default=0.5,
        help="Minimum score for instance predictions to be shown",
    )
    parser.add_argument(
        "--opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    logger = setup_logger("log")
    logger.info("Arguments: " + str(args))

    args.config_file = os.path.join(
        ROOT_DIR,
        "configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
    args.opts = [
        "MODEL.WEIGHTS",
        "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl",
        "MODEL.DEVICE", "cpu"
    ]

    # detectron2://COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl
    # args.config_file = os.path.join(ROOT_DIR, "configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")

    args.input = [os.path.join(ROOT_DIR, "demo_dir/aoa.jpg")]
Example #7
0
KAGGLE_TEST_DIR = os.getenv("KAGGLE_TEST_DIR")
SAVED_MODEL_DIR = os.getenv("SAVED_MODEL_DIR")
DETECTRON2_DIR = os.getenv("DETECTRON2_DIR")
WORKERS = int(os.getenv("NUM_WORKERS"))

# %% --------------------READ DATA
test_dataframe = KAGGLE_TEST_DIR + "/test_original_dimension.csv"
flag_path = DETECTRON2_DIR + "/retinanet/configurations/v2.yaml"
output_dir = DETECTRON2_DIR + "/retinanet/test/final"
model_dir = DETECTRON2_DIR + f"/retinanet/train/final"

# %% --------------------READ FLAGS
flag = Flags().load_yaml(flag_path)

# %% -------------------- SETUP LOGGER
setup_logger(output=output_dir)

# %% --------------------REGISTER DATASETs and METADATA
thing_classes = [
    "Aortic enlargement", "Atelectasis", "Calcification", "Cardiomegaly",
    "Consolidation", "ILD", "Infiltration", "Lung Opacity", "Nodule/Mass",
    "Other lesion", "Pleural effusion", "Pleural thickening", "Pneumothorax",
    "Pulmonary fibrosis"
]

# lambda is anonymous function
# holdout dataset w/o annotations
DatasetCatalog.register(
    "test",
    lambda: get_test_detectron_dataset(KAGGLE_TEST_DIR, test_dataframe))
MetadataCatalog.get("test").set(thing_classes=thing_classes)
from clearml import Task, Logger
task = Task.init(project_name='DETECTRON2',task_name='Default Model Architecture',task_type='training', output_uri='http://jax79sg.hopto.org:9000/clearml-models/artifact')
task.set_base_docker("quay.io/jax79sg/detectron2:v4 --env GIT_SSL_NO_VERIFY=true --env TRAINS_AGENT_GIT_USER=testuser --env TRAINS_AGENT_GIT_PASS=testuser" )
task.execute_remotely(queue_name="single_gpu", exit_process=True)


import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

# import some common libraries
import numpy as np
import os, json, cv2, random
import boto3
import argparse
# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.structures import BoxMode
from detectron2.engine import DefaultTrainer
from botocore.client import Config

def download_s3_folder(bucket_name, s3_folder, local_dir=None):
    bucket = s3.Bucket(bucket_name)
    for obj in bucket.objects.filter(Prefix=s3_folder):
        target = obj.key if local_dir is None \
                else os.path.join(local_dir, os.path.relpath(obj.key, s3_folder))
        if not os.path.exists(os.path.dirname(target)):
Example #9
0
def main_predict():
    try:
        mp.set_start_method("spawn", force=True)
        # args = get_parser().parse_args()
        args, unknown = get_parser().parse_known_args()
        setup_logger(name="fvcore")
        logger = setup_logger()
        logger.info("Arguments: " + str(args))

        cfg = setup_cfg(args)
        print("A ...................")
        demo = VisualizationDemo(cfg)
        print("B ...................")
        st = time.time()

        if args.video_input:
            # assert os.path.isfile(args.video_input)

            # vid_path = args.video_input
            # vid_name = vid_path.split('.mp4')[0]
            # stream = ffmpeg.input(vid_path)
            # stream = ffmpeg.filter(stream, 'fps', fps=args.fps, round='up')
            # stream = ffmpeg.output(stream, f'{vid_name}_resized.mp4')
            # ffmpeg.run(stream)

            # video = cv2.VideoCapture(f'{vid_name}_resized.mp4')
            # print("A")
            # message = request.get_json(force=True)
            # print("B")
            # encoded = message['image']
            # print("C")
            # decoded = base64.b64decode(encoded)
            # print("D")

            # video_decoded = np.asarray(bytearray(decoded), dtype="uint8")
            # print(f"decoded and shape is >>>>>>>>>> {video_decoded.shape}")

            # video = cv2.VideoCapture(video_decoded)
            print("E>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
            # video = cv2.VideoCapture('/data/work/colab_d2_copy/colab_d2/docker_files/detectron2/demo/new_clip_resized.mp4')

            video = cv2.VideoCapture(
                '/app/docker_files/detectron2/demo/new_clip_resized.mp4')

            width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
            frames_per_second = video.get(cv2.CAP_PROP_FPS)
            num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
            print(
                f'<<<<<<<<<<<<<<<<<<<<< {video.get(cv2.CAP_PROP_FRAME_COUNT)}')
            basename = os.path.basename(args.video_input)

            #######################
            duration = num_frames / frames_per_second
            print(f'num_frames is {num_frames}')
            print(f'duration is {duration} and fps is {frames_per_second}')

            counter = 0
            frames = []
            all_preds = demo.run_on_video(video)
            i = 1
            while i <= num_frames:
                semantic_predictions = next(all_preds)
                # semantic_predictions = item
                objs = []
                for s in semantic_predictions:
                    obj = {}
                    obj["label"] = s["text"]
                    obj['area_percentage'] = float("{0:.2f}".format(s['area'] /
                                                                    1000000))
                    obj["score"] = float(
                        "{0:.2f}".format(s["score"] if "score" in s else 1))
                    objs.append(obj)

                obj_set = {}
                for s in semantic_predictions:
                    k = s["text"]
                    score = s["score"] if "score" in s else 1
                    if not k in obj_set:
                        obj_set[k] = {
                            "scores": [score],
                            "areas": [s["area"]],
                            "label": k
                        }
                    else:
                        obj_set[k]["scores"].append(score)
                        obj_set[k]["areas"].append(s["area"])

                u_objs = []
                for k in obj_set:
                    u = obj_set[k]
                    n = len(u["scores"])
                    score_ave = reduce((lambda x, y: x + y), u["scores"]) / n
                    area_sum = reduce((lambda x, y: x + y), u["areas"])

                    obj = {}
                    obj["label"] = u["label"]
                    obj['area_percentage'] = float("{0:.2f}".format(area_sum /
                                                                    1000000))
                    obj["score"] = float("{0:.2f}".format(score_ave))
                    obj["count"] = n
                    u_objs.append(obj)
                frame = {
                    "frame": i,
                    "instances": objs,
                    "objects": u_objs,
                }

                print(f'num_frame is {i}')

                counter += 1
                i += 1
                frames.append(frame)
            cv2.destroyAllWindows()
        data = {
            "video": {
                "meta": {},
                "base_uri":
                "https://videobank.blob.core.windows.net/videobank",
                "folder": args.video_input,
                "output-frame-path": "pipeline/detectron2"
            },
            "ml-data": {
                "object-detection": {
                    "meta": {
                        'duration': duration,
                        'fps': frames_per_second,
                        'len_frames': len(frames)
                    },
                    "video": {},
                    "frames": frames
                }
            }
        }
        # print(f'data is {data}')
        return jsonify(data)
    except Exception as e:
        print(f"error is >>>>>>>> {e}")
        return "Wrong input format"
Example #10
0
                        help="input exclude quantization layers definition")
    parser.add_argument("--output",
                        help="output directory for the converted model")
    parser.add_argument("--onnx", help="output onnx model path")
    parser.add_argument(
        "opts",
        help="Modify config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )
    args = parser.parse_args()
    if args.debug:
        verbosity = logging.DEBUG
    else:
        verbosity = logging.INFO
    logger = setup_logger(verbosity=verbosity)
    logger.info("Command line arguments: " + str(args))
    os.makedirs(args.output, exist_ok=True)

    cfg = setup_cfg(args)

    # create a torch model
    torch_model = build_model(cfg)
    DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS)

    # get a sample data
    data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])
    data_iter = iter(data_loader)
    first_batch = next(data_iter)

    # convert and save caffe2 model
# You may need to restart your runtime prior to this, to let your installation take effect
# 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 cv2
import sys
import glob
import torch
import pyttsx3
import argparse
import os
import zmq
import time
import tqdm
import multiprocessing as mp
import zmq
import base64

# import some common detectron2 utilities
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
# https://www.geeksforgeeks.org/python-count-occurrences-element-list/
from collections import Counter
Example #12
0
def main():
    parser = argparse.ArgumentParser(
        description="Export model to the onnx format")
    parser.add_argument(
        "--config-file",
        default="configs/FCOS-Detection/R_50_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--output",
        default="output/fcos.onnx",
        metavar="FILE",
        help="path to the output onnx file",
    )
    parser.add_argument(
        "--opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=[],
        nargs=argparse.REMAINDER,
    )

    cfg = get_cfg()
    args = parser.parse_args()
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)

    # norm for ONNX: change FrozenBN back to BN
    cfg.MODEL.BACKBONE.FREEZE_AT = 0
    cfg.MODEL.RESNETS.NORM = "BN"
    # turn on the following configuration according to your own convenience
    #cfg.MODEL.FCOS.NORM = "BN"
    #cfg.MODEL.FCOS.NORM = "NaiveGN"

    cfg.freeze()

    output_dir = cfg.OUTPUT_DIR
    logger = setup_logger(output=output_dir)
    logger.info(cfg)

    model = build_model(cfg)
    model.eval()
    model.to(cfg.MODEL.DEVICE)
    logger.info("Model:\n{}".format(model))

    checkpointer = DetectionCheckpointer(model)
    _ = checkpointer.load(cfg.MODEL.WEIGHTS)
    logger.info("load Model:\n{}".format(cfg.MODEL.WEIGHTS))

    # patch fcos_head
    # step 1. config
    fcos_head = model.proposal_generator.fcos_head
    norm = None if cfg.MODEL.FCOS.NORM == "none" else cfg.MODEL.FCOS.NORM
    head_configs = {
        "cls": (cfg.MODEL.FCOS.NUM_CLS_CONVS, cfg.MODEL.FCOS.USE_DEFORMABLE),
        "bbox": (cfg.MODEL.FCOS.NUM_BOX_CONVS, cfg.MODEL.FCOS.USE_DEFORMABLE),
        "share": (cfg.MODEL.FCOS.NUM_SHARE_CONVS, False)
    }

    # step 2. seperate module
    for l in range(fcos_head.num_levels):
        for head in head_configs:
            tower = []
            num_convs, use_deformable = head_configs[head]
            for i in range(num_convs):
                tower.append(
                    deepcopy(
                        getattr(fcos_head,
                                '{}_tower'.format(head))[i * 3 + 0]))
                if norm in ["GN", "NaiveGN"]:
                    tower.append(
                        deepcopy(
                            getattr(fcos_head,
                                    '{}_tower'.format(head))[i * 3 + 1]))
                elif norm in ["BN", "SyncBN"]:
                    tower.append(
                        deepcopy(
                            getattr(fcos_head,
                                    '{}_tower'.format(head))[i * 3 + 1][l]))
                tower.append(
                    deepcopy(
                        getattr(fcos_head,
                                '{}_tower'.format(head))[i * 3 + 2]))
            fcos_head.add_module('{}_tower{}'.format(head, l),
                                 torch.nn.Sequential(*tower))

    # step 3. override forward
    def fcos_head_forward(self, x):
        logits = []
        bbox_reg = []
        ctrness = []
        bbox_towers = []
        for l, feature in enumerate(x):
            feature = self.share_tower(feature)
            cls_tower = getattr(self, 'cls_tower{}'.format(l))(feature)
            bbox_tower = getattr(self, 'bbox_tower{}'.format(l))(feature)

            logits.append(self.cls_logits(cls_tower))
            ctrness.append(self.ctrness(bbox_tower))
            reg = self.bbox_pred(bbox_tower)
            if self.scales is not None:
                reg = self.scales[l](reg)
            # Note that we use relu, as in the improved FCOS, instead of exp.
            bbox_reg.append(F.relu(reg))

        return logits, bbox_reg, ctrness, bbox_towers

    fcos_head.forward = types.MethodType(fcos_head_forward, fcos_head)

    proposal_generator = FCOS(cfg)
    onnx_model = torch.nn.Sequential(
        OrderedDict([
            ('backbone', model.backbone),
            ('proposal_generator', proposal_generator),
            ('heads', model.proposal_generator.fcos_head),
        ]))

    height, width = 800, 1088
    input_names = ["input_image"]
    dummy_input = torch.zeros((1, 3, height, width)).to(cfg.MODEL.DEVICE)
    output_names = []
    for item in ["logits", "bbox_reg", "centerness"]:
        for l in range(len(cfg.MODEL.FCOS.FPN_STRIDES)):
            fpn_name = "P{}".format(3 + l)
            output_names.extend([fpn_name + item])

    torch.onnx.export(onnx_model,
                      dummy_input,
                      args.output,
                      verbose=True,
                      input_names=input_names,
                      output_names=output_names,
                      keep_initializers_as_inputs=True)

    logger.info("Done. The onnx model is saved into {}.".format(args.output))
Example #13
0
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
# Write log to a folder called 'log'
setup_logger('log')

# import some common libraries
import numpy as np
import os, json, cv2, random
import cv2

# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.engine import DefaultTrainer
from detectron2.evaluation import COCOEvaluator, inference_on_dataset
from detectron2.data.datasets import register_coco_instances

from detectron2.data import detection_utils as utils
import detectron2.data.transforms as T
import copy
import torch
from detectron2.data import build_detection_train_loader
from detectron2.data import build_detection_test_loader
'''
################################### Edit from here ##########################################
'''
Example #14
0
        default=0.5,
        help="Minimum score for instance predictions to be shown",
    )
    parser.add_argument(
        "--opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    demo = VisualizationDemo(cfg)

    if args.input:
        if len(args.input) == 1:
            args.input = glob.glob(os.path.expanduser(args.input[0]))
            assert args.input, "The input path(s) was not found"
        for path in tqdm.tqdm(args.input, disable=not args.output):
            # use PIL, to be consistent with evaluation
            img = read_image(path, format="BGR")
            start_time = time.time()
Example #15
0
def do_test():
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    # 注册数据集
    register_dataset()

    model_demo = VisualizationDemo(cfg)

    predict_list = []
    real_list = []
    # for path in tqdm.tqdm(args.input, disable=not args.output):
    for imgfile in os.listdir(INPUT_IMG_PATH):

        # use PIL, to be consistent with evaluation
        img_fullName = os.path.join(INPUT_IMG_PATH, imgfile)
        img = read_image(img_fullName, format="BGR")
        start_time = time.time()
        predictions, visualized_output = model_demo.run_on_image_detection(img)
        # imgpath.json这个文件里面是图片文件名和类别真值的对应表
        # 模型输出的类别中 0和1 分别代表水印和正常元件,而 2,3,4,5,6 分别是那五种瑕疵,
        # 还缺少一个表示false的数字,所以我们用7来表示
        # 所以在imgpath.json文件中你只能看到2,3,4,5,6,7这6种数字,其实分别就代表了我们最终要分类的6个类别

        img_paths = tools.get_all_class_label_infos()
        pred = predictions.get('pred_classes').tolist()
        if len(pred) == 0:
            print(imgfile)
            predict_list.append('1')
        else:
            predict_list.append(str(pred[0] + 1))
        # temp = str(imgfile).split('_')
        # if temp[1].endswith('.jpg'):
        #     imgfile = temp[0] + '_' + temp[1]
        # else:
        #     imgfile = temp[0] + '_' + temp[1] + '.jpg'
        real_list.append(img_paths[imgfile])
        #visualized_output.save(os.path.join('imgout', imgfile))
        # print(flaw_only(predictions))
        # log会在控制台输出预测所花的时间等等信息
        # logger.info(
        #     "{}: detected {} instances in {:.2f}s".format(
        #         imgfile, len(predictions["instances"]), time.time() - start_time
        #     )
        # )
        # 这个是保存预测错误的图片,会在原图上额外绘画出识别的矩形框、mask、类别、分数等等,好让我们看看预测错误的图片长什么样并且是哪里预测错了
        # if determine_final_class(predictions) != img_paths[imgfile]:
        #     temp = ['watermark', 'commom1', '01', '02', '05', '07', 'other', 'false']
        #     tempstr = temp[img_paths[imgfile]] + '-' + temp[determine_final_class(predictions)]
        #     out_filename = os.path.join(OUTPUT_IMG_PATH, tempstr + imgfile)
        #     visualized_output.save(out_filename)

    # 根据上面两种函数,determine_final_class和contain_class两种方案来统计的情况,生成混淆矩阵、precision、recall
    cm = confusion_matrix(real_list, predict_list)
    ps = precision_score(real_list, predict_list, average=None)
    rc = recall_score(real_list, predict_list, average=None)
    print(cm)
    print(['{:.2%}'.format(x) for x in ps])
    print(['{:.2%}'.format(x) for x in rc])
    '''
Example #16
0
        default=0.5,
        help="Minimum score for instance predictions to be shown",
    )
    parser.add_argument(
        "--opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    # 注册数据集
    register_dataset()

    demo = VisualizationDemo(cfg, instance_mode=ColorMode.SEGMENTATION)

    # for path in tqdm.tqdm(args.input, disable=not args.output):
    for imgfile in os.listdir(INPUT_IMG_PATH):

        # use PIL, to be consistent with evaluation
        img_fullName = os.path.join(INPUT_IMG_PATH, imgfile)
        img = read_image(img_fullName, format="BGR")
    CommonMetricPrinter,
    EventStorage,
    JSONWriter,
    TensorboardXWriter,
)

from detectron2.data import (
    MetadataCatalog,
    build_detection_test_loader,
    build_detection_train_loader,
)

from detectron2.modeling import GeneralizedRCNNWithTTA

from detectron2.utils.logger import setup_logger
setup_logger()  # D2 logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))


def _setup(sm_args):
    """
    Create D2 configs and perform basic setups.  
    """

    # D2 expects ArgParser.NameSpace object to ammend Cfg node.
    # We are constructing artificial ArgParse object here. TODO: consider refactoring it in future.

    config_file_path = f"/opt/ml/code/detectron2/configs/{sm_args.config_file}"  # could just supply a config in source_dir
Example #18
0
    """
    Test the COCO json dataset loader.

    Usage:
        python -m detectron2.data.datasets.coco \
            path/to/json path/to/image_root dataset_name

        "dataset_name" can be "coco_2014_minival_100", or other
        pre-registered ones
    """
    from detectron2.utils.logger import setup_logger
    from detectron2.utils.visualizer import Visualizer
    import detectron2.data.datasets  # noqa # add pre-defined metadata
    import sys

    logger = setup_logger(name=__name__)
    assert sys.argv[3] in DatasetCatalog.list()
    meta = MetadataCatalog.get(sys.argv[3])

    dicts = load_coco_json(sys.argv[1], sys.argv[2], sys.argv[3])
    logger.info("Done loading {} samples.".format(len(dicts)))

    dirname = "coco-data-vis"
    os.makedirs(dirname, exist_ok=True)
    for d in dicts:
        img = np.array(Image.open(d["file_name"]))
        visualizer = Visualizer(img, metadata=meta)
        vis = visualizer.draw_dataset_dict(d)
        fpath = os.path.join(dirname, os.path.basename(d["file_name"]))
        vis.save(fpath)
Example #19
0
def object_d2(files):
    # mp.set_start_method("spawn", force=True)
    args, unknown = get_parser().parse_known_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)
    print("A ...................")
    demo = VisualizationDemo(cfg)
    print("B ...................")
    basepath = '/data1/code_base/mnt_data/kubenetra/integration/vids'
    for video_id in tqdm(files):
        # integration/vids/14686_.mp4
        try:
            print(f'this will be loaded >>>>>>> /mnt/az/kubenetra/blob_vid/{video_id}.mp4')

            video = cv2.VideoCapture(f'{basepath}/{video_id}.mp4')
            print("E>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
            # video = cv2.VideoCapture('/app/new_clip_resized_resized.mp4')

            width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
            # print(f'width is {width}')
            height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
            frames_per_second = video.get(cv2.CAP_PROP_FPS)
            img_pixels = height*width
            if frames_per_second ==0:
                pass
            else:
                num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
                print('<<<<<<<<<<<<<<<<<<<<< ',video.get(cv2.CAP_PROP_FRAME_COUNT))
                #######################

                duration = num_frames/frames_per_second
                
                print('num_frames is ',num_frames)
                # print(f'duration is {duration} and fps is {frames_per_second}')

                counter = 0 
                frames=[]
                all_preds = list(demo.run_on_video(video))
                i=1
                total_frames = num_frames
                # while num_frames!=0:
                for num_frame, semantic_predictions in enumerate(all_preds):
                    # semantic_predictions = next(all_preds)
                    # semantic_predictions = item
                    objs = []
                    for s in semantic_predictions:
                        obj = {}
                        obj["label"] = s["text"]
                        obj['area_percentage'] = float("{0:.2f}".format(s['area']/img_pixels))
                        obj["score"] = float("{0:.2f}".format(s["score"] if "score" in s else 1))
                        objs.append(obj)

                    obj_set = {}
                    for s in semantic_predictions:
                        k = s["text"]
                        score = s["score"] if "score" in s else 1
                        if not k in obj_set:
                            obj_set[k] = {
                                "scores": [score],
                                "areas":  [s["area"]],
                                "label": k
                            }
                        else:
                            obj_set[k]["scores"].append(score)
                            obj_set[k]["areas"].append(s["area"])

                    u_objs = []
                    for k in obj_set:
                        u = obj_set[k]
                        n = len(u["scores"])
                        score_ave = reduce((lambda x, y: x + y), u["scores"])/n
                        area_sum = reduce((lambda x, y: x + y), u["areas"])

                        obj = {}
                        obj["label"] = u["label"]
                        obj['area_percentage'] = float("{0:.2f}".format(area_sum/1000000))
                        obj["score"] = float("{0:.2f}".format(score_ave))
                        obj["count"] = n
                        u_objs.append(obj)
                    frame = {
                        "frame":i,
                        "instances": objs,
                        "objects": u_objs,
                    }
                
                    # print('num_frame is ',total_frames - num_frames + 1)
                    print('num_frame is ',num_frame + 1)
                    # counter+=1
                    # num_frames-=1
                    # i+=1
                    frames.append(frame)
                cv2.destroyAllWindows()
                data = {
                    "video": {
                        "meta": {},
                        "base_uri": "https://videobank.blob.core.windows.net/videobank",
                        "folder": args.video_input,
                        "output-frame-path": "pipeline/detectron2"
                    },
                    "ml-data": {
                        "object-detection": {
                            "meta": {'duration':duration, 'fps':frames_per_second,'len_frames':len(frames)},
                            "video": {},
                            "frames": frames
                        }
                    }
                }
                # print(f'data is {data}')
                # try:
                #     os.remove(f'./blob_vid/{video_id}.txt')
                # except OSError:
                #     pass

                # return data
                print(f'writing OD outs inside >>> {basepath}/{video_id}.json')
                with open(f'{basepath}/{video_id}.json', 'w') as f:
                    json.dump(data,f)
        except Exception as e:
            print(e)
            with open('./err_vids.txt','a') as f:
                f.write(str(e))
            pass