Example #1
0
 def _eval(self):
     all_results_1st = eval_coco(self.df, lambda img: detect_one_image(img, self.pred_1st))
     all_results_2nd = eval_coco(self.df, lambda img: detect_one_image(img, self.pred_2nd))
     all_results_3rd = eval_coco(self.df, lambda img: detect_one_image(img, self.pred_3rd))
     output_file_1st = os.path.join(
         logger.get_logger_dir(), '1st_outputs{}.json'.format(self.global_step))
     output_file_2nd = os.path.join(
         logger.get_logger_dir(), '2nd_outputs{}.json'.format(self.global_step))
     output_file_3rd = os.path.join(
         logger.get_logger_dir(), '3rd_outputs{}.json'.format(self.global_step))
     with open(output_file_1st, 'w') as f:
         json.dump(all_results_1st, f)
     with open(output_file_2nd, 'w') as f:
         json.dump(all_results_2nd, f)
     with open(output_file_3rd, 'w') as f:
         json.dump(all_results_3rd, f)
     try:
         scores_1st = print_evaluation_scores(output_file_1st)
         scores_2nd = print_evaluation_scores(output_file_2nd)
         scores_3rd = print_evaluation_scores(output_file_3rd)
     except Exception:
         logger.exception("Exception in COCO evaluation.")
         scores = {}
     for k, v in scores_1st.items():
         self.trainer.monitors.put_scalar(k, v)
     for k, v in scores_2nd.items():
         self.trainer.monitors.put_scalar(k, v)
     for k, v in scores_3rd.items():
         self.trainer.monitors.put_scalar(k, v)
Example #2
0
 def _eval(self):
     all_results = eval_on_dataflow(
         self.df, lambda img: detect_one_image(img, self.pred))
     output_file = os.path.join(logger.get_logger_dir(),
                                'outputs{}.json'.format(self.global_step))
     with open(output_file, 'w') as f:
         json.dump(all_results, f)
Example #3
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    print(len(results))
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    tpviz.interactive_imshow(viz)
Example #4
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    # tpviz.interactive_imshow(viz)
    cv2.imwrite(os.path.basename(input_file), viz)
Example #5
0
def offline_evaluate(pred_func, output_file):
    df = get_eval_dataflow()
    all_results = eval_coco(
        df, lambda img: detect_one_image(img, pred_func))
    with open(output_file, 'w') as f:
        json.dump(all_results, f)
    print_evaluation_scores(output_file)
 def _eval(self):
     all_results, local_score = eval_on_dataflow(
         self.df, lambda img: detect_one_image(img, self.pred))
     scores = {}
     scores['local'] = local_score
     for k, v in scores.items():
         self.trainer.monitors.put_scalar(k, v)
Example #7
0
def offline_evaluate(pred_func, output_file):
    df = get_eval_dataflow()
    all_results = eval_on_dataflow(
        df, lambda img: detect_one_image(img, pred_func))
    with open(output_file, 'w') as f:
        json.dump(all_results, f)
    print_evaluation_scores(output_file)
Example #8
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    # tpviz.interactive_imshow(viz)
    cv2.imwrite('../drive/train_log/predicted_image.jpg', viz)
    cv2.destroyAllWindows()
Example #9
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    cv2.imwrite("output.png", viz)
    logger.info("Inference output written to output.png")
    tpviz.interactive_imshow(viz)
Example #10
0
 def _eval(self):
     all_results = eval_coco(self.df, lambda img: detect_one_image(img, self.pred))
     output_file = os.path.join(
         logger.get_logger_dir(), 'outputs{}.json'.format(self.global_step))
     with open(output_file, 'w') as f:
         json.dump(all_results, f)
     scores = print_evaluation_scores(output_file)
     for k, v in scores.items():
         self.trainer.monitors.put_scalar(k, v)
Example #11
0
def predict_many(pred_func, input_files):
    if not os.path.exists('output'):
        os.mkdir('output')
    for idx, input_file in enumerate(input_files):
        img = cv2.imread(input_file, cv2.IMREAD_COLOR)
        results = detect_one_image(img, pred_func)
        final = draw_final_outputs(img, results)
        plt.imshow(final)
        plt.savefig(os.path.join('output', str(idx) + '.png'))
Example #12
0
 def _eval(self):
     all_results = eval_coco(self.df, lambda img: detect_one_image(img, self.pred))
     output_file = os.path.join(
         logger.get_logger_dir(), 'outputs{}.json'.format(self.global_step))
     with open(output_file, 'w') as f:
         json.dump(all_results, f)
     scores = print_evaluation_scores(output_file)
     for k, v in scores.items():
         self.trainer.monitors.put_scalar(k, v)
def offline_evaluate(pred_func, output_file):
    df = get_test_dataflow(add_mask=False)
    all_results = eval_on_dataflow(
        df, lambda img: detect_one_image(img, pred_func))
    #print(all_results)
    #    input()
    with open(output_file, 'w') as f:
        json.dump(all_results, f, cls=MyEncoder)
    ret = print_evaluation_scores(output_file)
    print(ret)
Example #14
0
def predict_one_image(input_file, pred_func):
    print('Predicting '+input_file)
    path = '/home/bonniehu/projects/def-jjclark/bonniehu/FasterRCNN/pred_out/'
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    trimap_path = 'trimap/'
    trimaps = trimap_outputs(img, results, trimap_path, input_file.split('/')[-1][:-4])
    final = draw_final_outputs(img, results)
    #viz = np.concatenate((img, final), axis=1)
    #not support in ubuntu 
    #tpviz.interactive_imshow(viz)
    misc.imsave(path+input_file.split('/')[-1][:-4]+'_mask.png', final)
 def _eval(self):
     all_results, local_score = eval_on_dataflow(
         self.df, lambda img: detect_one_image(img, self.pred))
     """
     output_file = os.path.join(
         logger.get_logger_dir(), 'outputs{}.json'.format(self.global_step))
     with open(output_file, 'w') as f:
         json.dump(all_results, f, cls=MyEncoder)
     scores = print_evaluation_scores(output_file)
     """
     scores = {}
     scores['local'] = local_score
     for k, v in scores.items():
         self.trainer.monitors.put_scalar(k, v)
Example #16
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    t = time.time()
    results = detect_one_image(img, pred_func)
    t_final = time.time() - t
    height, width, channels = img.shape
    print("Time: ", t_final, " for ", height * width / 1e6,
          " Mpixels , i.e. Width=", width, " Height=", height, " Channels=",
          channels)
    print("Throughput: ", height * width / 1e6 / t_final, " Mpixels/Sec")

    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    tpviz.interactive_imshow(viz)
Example #17
0
def predict(model_path, input_file):
    pred = OfflinePredictor(
        PredictConfig(model=Model(),
                      session_init=get_model_loader(model_path),
                      input_names=['image'],
                      output_names=[
                          'fastrcnn_fg_probs',
                          'fastrcnn_fg_boxes',
                      ]))
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred)
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    tpviz.interactive_imshow(viz)
Example #18
0
def offline_evaluate(model_path, output_file):
    pred = OfflinePredictor(
        PredictConfig(model=Model(),
                      session_init=get_model_loader(model_path),
                      input_names=['image'],
                      output_names=[
                          'fastrcnn_fg_probs',
                          'fastrcnn_fg_boxes',
                      ]))
    df = get_eval_dataflow()
    df = PrefetchDataZMQ(df, 1)
    all_results = eval_on_dataflow(df, lambda img: detect_one_image(img, pred))
    with open(output_file, 'w') as f:
        json.dump(all_results, f)
    print_evaluation_scores(output_file)
Example #19
0
def offline_evaluate(pred_config, output_file):
    num_gpu = cfg.TRAIN.NUM_GPUS
    graph_funcs = MultiTowerOfflinePredictor(pred_config, list(
        range(num_gpu))).get_predictors()
    predictors = []
    dataflows = []
    for k in range(num_gpu):
        predictors.append(
            lambda img, pred=graph_funcs[k]: detect_one_image(img, pred))
        dataflows.append(get_eval_dataflow(shard=k, num_shards=num_gpu))
    if num_gpu > 1:
        all_results = multithread_eval_coco(dataflows, predictors)
    else:
        all_results = eval_coco(dataflows[0], predictors[0])
    with open(output_file, 'w') as f:
        json.dump(all_results, f)
    print_coco_metrics(output_file)
def predict(pred_func, input_file):
    image_paths = []
    if os.path.isdir(input_file):
        for inp_file in os.listdir(input_file):
            image_paths += [input_file + inp_file]
    for image_path in image_paths:

        (path1,file1)=os.path.split(image_path)
        (filename1, filename2) = os.path.splitext(file1)
        objectfile = open('results/%s.txt' % filename1,'w')
        img = cv2.imread(image_path, cv2.IMREAD_COLOR)
        start=time.time()
        results = detect_one_image(img, pred_func)
        end=time.time()
        print(end-start)
        final = draw_final_outputs(img, results,objectfile)
        #viz = np.concatenate((img, final), axis=1)
        viz=final
        cv2.imwrite('outimages/%s.png'%filename1,viz)
def forward_protobuf(pred_func, output_folder, forward_dataset,
                     generic_images_folder, generic_images_pattern):
    tf.gfile.MakeDirs(output_folder)
    hypo_set = HypothesisSet()
    with open(
            "/work/merkelbach/datasets/dashcam_mining/tracking_results_raw/vid_01/eval/12-Mar-2018--19-25-00vayV/hypos_protobuf/hypotheses_500frames_1860hyp___work__pv182253__data__dashcam__dashcam_videos_frames__vid_01__frames_cropped__clip_005.hypset",
            "rb") as f:
        hypo_set.ParseFromString(f.read())
    for hyp in hypo_set.hypotheses:
        boxes = hyp.bounding_boxes_2D_with_timestamps
        for t, box in boxes.items():
            img_filename = "/work/merkelbach/datasets/dashcam_mining/videos/vid_01/frames_cropped/all_frames/video_0001_frames" + "%09d" % t + ".png"
            print(img_filename, box)
            img_val = cv2.imread(img_filename, cv2.IMREAD_COLOR)
            assert config.PROVIDE_BOXES_AS_INPUT
            input_boxes = np.array(
                [[box.x0, box.y0, box.x0 + box.w, box.y0 + box.h]],
                dtype=np.float32)
            results = detect_one_image(img_val, pred_func, input_boxes)
            print(results)
Example #22
0
def generate_nii(pred_func, input_files):
    arg = []
    for idx, input_file in tqdm.tqdm(enumerate(input_files)):
        boxes = collections.defaultdict(list)
        masks = collections.defaultdict(list)

        img = cv2.imread(input_file, cv2.IMREAD_COLOR)
        results = detect_one_image(img, pred_func)
        for res in results:
            cl = config.CLASS_NAMES[res.class_id]
            boxes[cl].append(list(res.box) + [res.score])
            masks[cl].append(res.mask)

        arg.append({
            'image': input_file,
            'boxes': boxes,
            'masks': masks,
            'im_info': (200, 200)
        })

    evaluate_mask_mean_iou(arg)
Example #23
0
def offline_evaluate(pred_config, output_file):
    num_gpu = cfg.TRAIN.NUM_GPUS
    graph_funcs = MultiTowerOfflinePredictor(pred_config, list(
        range(num_gpu))).get_predictors()

    predictors = []
    for k in range(num_gpu):
        predictors.append(
            lambda img, pred=graph_funcs[k]: detect_one_image(img, pred))
    for dataset in cfg.DATA.VAL:
        logger.info("Evaluating {} ...".format(dataset))
        dataflows = [
            get_eval_dataflow(dataset, shard=k, num_shards=num_gpu)
            for k in range(num_gpu)
        ]
        if num_gpu > 1:
            all_results = multithread_eval_coco(dataflows, predictors)
        else:
            all_results = eval_coco(dataflows[0], predictors[0])
        output = output_file + '-' + dataset
        with open(output, 'w') as f:
            json.dump(all_results, f)
        print_coco_metrics(dataset, output)
Example #24
0
def predict(pred_func, input_file):
    img = cv2.imread(input_file, cv2.IMREAD_COLOR)
    results = detect_one_image(img, pred_func)
    final = draw_final_outputs(img, results)
    viz = np.concatenate((img, final), axis=1)
    tpviz.interactive_imshow(viz)
def forward(pred_func,
            output_folder,
            forward_dataset,
            generic_images_folder=None,
            generic_images_pattern=None):
    if forward_dataset.lower() == "davis":
        # name = "/home/luiten/vision/PReMVOS/home_data/%s/images/*"%config.DAVIS_NAME
        # imgs = glob.glob(name)
        # seq_idx = -2

        # name = config.DAVIS_NAME
        # imgs = glob.glob(name+"/*/*")
        # seq_idx = -2

        name = config.DAVIS_NAME
        pre = '/'.join(name.split('/')[:-1])
        print(name)
        imgs = []
        f = open(name, 'r')
        while True:
            x = f.readline()
            x = x.rstrip()
            if not x: break
            print(pre + '/' + x)
            imgs = imgs + glob.glob(pre + '/' + x + '/*')

        # for file in open(name):
        #     imgs = imgs + glob.glob(file+'/*')
        print(imgs)
        seq_idx = -2

    elif forward_dataset.lower() == "oxford":
        imgs = [
            "/fastwork/" + os.environ["USER"] + "/mywork/" + x.strip() for x in
            open("/home/" + os.environ["USER"] +
                 "/vision/TrackingAnnotationTool/to_annotate_single_imgs.txt").
            readlines() if len(x.strip()) > 0
        ]
        seq_idx = -4
    elif forward_dataset.lower() == "kitti_tracking":
        imgs = glob.glob(
            "/fastwork/" + os.environ["USER"] +
            "/mywork/data/kitti_training_minimum/training/image_02/*/*.png")
        seq_idx = -2
    elif forward_dataset.lower() == "generic":
        generic_images_pattern = generic_images_pattern.replace("//", "/")
        assert generic_images_folder is not None, "For a generic dataset, a data folder must be given."
        assert generic_images_pattern is not None, "For a generic dataset, an image pattern must be given."
        # Find the images in the folder
        imgs = sorted(
            glob.glob(
                os.path.join(generic_images_folder, generic_images_pattern)))
        # The sequence (if there is any) is assumed to be the folder the images are in
        #seq_idx = -1 - generic_images_pattern.count("/")
        seq_idx = None
    elif forward_dataset.lower() == "protobuf":
        forward_protobuf(pred_func, output_folder, forward_dataset,
                         generic_images_folder, generic_images_pattern)
    else:
        assert False, ("Unknown dataset", forward_dataset)
    tf.gfile.MakeDirs(output_folder)
    n_total = len(imgs)
    for idx, img in enumerate(imgs):
        print(idx, "/", n_total)
        if forward_dataset.lower() == "generic":
            seq = "/".join(
                img.replace(generic_images_folder, "").split("/")[:-1])
            seq_folder = output_folder + "/" + seq
        else:
            seq = img.split("/")[seq_idx]
            seq_folder = output_folder + "/" + seq
        img_filename = img.split("/")[-1]
        tf.gfile.MakeDirs(seq_folder)

        output_filename = seq_folder + "/" + img_filename.replace(
            ".jpg", ".json").replace(".png", ".json")
        if os.path.isfile(output_filename):
            print("skipping", output_filename, "because it already exists")
            continue
        else:
            print(output_filename)

        img_val = cv2.imread(img, cv2.IMREAD_COLOR)
        assert img_val is not None, ("unable to load", img)
        if config.PROVIDE_BOXES_AS_INPUT:
            # TODO get some reasonable boxes (not sure which format)
            input_boxes = np.array([[202.468384, 566.15271, 999, 800],
                                    [0, 0, 100, 100], [100, 100, 200, 200]],
                                   np.float32)
            results = detect_one_image(img_val, pred_func, input_boxes)
        else:
            results = detect_one_image(img_val, pred_func)
        print(len(results))

        # store visualization (slow + optional)
        if args.forward_visualization:
            final = draw_final_outputs(img_val, results)
            viz = np.concatenate((img_val, final), axis=1)
            #tpviz.interactive_imshow(viz)
            viz_output_filename = seq_folder + "/" + img_filename.replace(
                ".png", ".jpg")
            cv2.imwrite(viz_output_filename, viz)

        # store as json
        results_json = convert_results_to_json(results, idx)
        with open(output_filename, 'w') as f:
            json.dump(results_json, f)
Example #26
0
def offline_evaluate(pred_func, output_file):
    df = get_test_dataflow()
    all_results = eval_on_dataflow(
        df, lambda img: detect_one_image(img, pred_func))
    print(all_results)
def predict(pred_func, input_file):
    for frame_name in os.listdir(input_file):

        intensity_file = pd.read_csv(os.path.join(intensity_path, frame_name))
        pts_file = pd.read_csv(os.path.join(pts_path, frame_name))
        file_name = str.split(frame_name, '.')[0]
        pts = pd.read_csv(os.path.join(pts_path, file_name + '.csv'),
                          header=None)
        pts = np.array(pts)
        out = np.zeros((len(pts), 1), dtype=np.int8)
        start = time.time()
        PointCloud = np.hstack((pts_file, intensity_file))
        img = makeBVFeature(PointCloud, size_ROI, size_cell)
        results = detect_one_image(img, pred_func)
        #objectfile=open('results1/%s.txt'%file_name,'w')
        pts[:, 0] = maxX - pts[:, 0]  # 坐标系统一为左上角
        pts[:, 1] = maxY - pts[:, 1]
        # PointCloud_grid = np.copy(PointCloud )
        pts[:, 0] = np.int_(np.floor(pts[:, 0] /
                                     size_cell))  # np.floor 返回不大于输入参数的最大整数;
        pts[:, 1] = np.int_(np.floor(pts[:, 1] / size_cell))
        for r in results:
            label = cfg.DATA.CLASS_NAMES[r.class_id][0:3]
            #if r.score<0.4:
            #label=0
            indx = np.where((pts[:, 1] > int(r[0][0]))
                            & (pts[:, 1] < int(r[0][2]))
                            & (pts[:, 0] < int(r[0][3]))
                            & (pts[:, 0] > int(r[0][1])))
            if label == 'cyc':

                if r.score < 0.6:
                    label = 0
                else:
                    label = 1
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (pts[:, 0] > int(r[0][1]))& (pts[:, 2] <-0.3))

            elif label == 'tri':
                label = 2
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #                 pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.3))
            elif label == 'sma':
                label = 3
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < 0))
            elif label == 'big':
                label = 4
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) )
            elif label == 'ped':
                if r.score < 0.4:
                    label = 0
                else:
                    label = 5
                #label = 5
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.1))
            elif label == 'cro':
                label = 6
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.1))
            elif label == 'unk':
                if r.score < 0.3:
                    label = 0
                # else:
                #     label=7
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) )
                label = 7
            out[indx] = label
        end = time.time()
        np.savetxt(os.path.join(upload_path, file_name + '.csv'),
                   out,
                   delimiter=',',
                   fmt='%d')
        print(end - start)
        # autotune is too slow for inference
        os.environ['TF_CUDNN_USE_AUTOTUNE'] = '0'

        assert args.load
        print_config()

    if args.evaluate:
        pred = OfflinePredictor(
            PredictConfig(model=Model(),
                          session_init=get_model_loader(args.load),
                          input_names=['image'],
                          output_names=get_model_output_names()))
        df = get_test_dataflow(add_mask=True)
        df.reset_state()
        all_results, local_score = eval_on_dataflow(
            df, lambda img: detect_one_image(img, pred))
        print("F2 Score: ", local_score)

    elif args.predict:
        imgs = Detection.load_many(
            config.BASEDIR, config.TEST_DATASET,
            add_gt=False)  # to load the class names into caches
        # filter with zero-ship
        imgs = [(img['image_data'], img['id']) for img in imgs]
        pred = OfflinePredictor(
            PredictConfig(model=Model(),
                          session_init=get_model_loader(args.load),
                          input_names=['image'],
                          output_names=get_model_output_names()))
        predict_many(pred, imgs)
    else:
Example #29
0
 def _build_coco_predictor(self, idx):
     graph_func = self.trainer.get_predictor(self._in_names,
                                             self._out_names,
                                             device=idx)
     return lambda img: detect_one_image(img, graph_func)
def predict_many(pred_func, input_files):
    import time
    import pandas as pd
    with open("./id_no_shop_384_2.pkl", "rb") as f:
        id_no_ship = pickle.load(f)
    #fid = open('result.csv','w+')
    #fid.write('ImageId,EncodedPixels\n')
    ship_list_dict = []
    with tqdm.tqdm(total=len(input_files)) as pbar:
        for idx, imgData in enumerate(input_files):
            img = cv2.imread(imgData[0])
            filename = imgData[1]
            ImageId = filename
            if ImageId in id_no_ship:
                ship_list_dict.append({
                    'ImageId': ImageId,
                    'EncodedPixels': np.nan
                })
                pbar.update()
                continue
            #s = time.time()
            results = detect_one_image(img.copy(), pred_func)
            mask_instances = [r.mask for r in results]
            score_instances = [r.score for r in results]
            #mask_whole = detect_one_image(img.copy(), pred_func)
            """
            #print(time.time() - s)
            if (len(results) == 0):
                # no detection in image
                result_one_line = ImageId+','+ "" +'\n'
                ship_list_dict.append({'ImageId':ImageId,'EncodedPixels':np.nan})
                #fid.write(result_one_line)
                pbar.update()
                continue
            
            r = {}
            r['class_ids'] = []
            r['scores'] = []
            r['masks'] = []
            for det in results:
                if np.count_nonzero(det.mask) > 0:
                    r['class_ids'].append(det.class_id)
                    r['scores'].append(det.score)
                    r['masks'].append(det.mask)
            if len(r['masks']) == 0:
                ship_list_dict.append({'ImageId':ImageId,'EncodedPixels':np.nan})
                print('no_mask')
                pbar.update()
                continue
            
            r['masks']  = np.array(r['masks']) # n, h, w
            #r['masks'] = np.transpose(r['masks'], [1,2,0])
            ImageId = filename
            #print(filename, r['masks'].shape)
            LabelId = r['class_ids'] 
            mask_whole = np.zeros((img.shape[0], img.shape[1]))
            #mask_clean = clean_overlap_instance(r['masks'], r['scores'])
            
            mask_clean = r['masks']
            for i in range(mask_clean.shape[0]):
                mask_whole[mask_clean[i] > 0] = 1
                #EncodedPixels = rle_encoding(mask_clean[i])
                #ship_list_dict.append({'ImageId':ImageId,'EncodedPixels':EncodedPixels})
            """
            masks = clean_overlap_instance(mask_instances, score_instances)
            #masks = split_mask(mask_whole)
            if len(masks) == 0:
                ship_list_dict.append({
                    'ImageId': ImageId,
                    'EncodedPixels': np.nan
                })
                print('no_mask!!!')
                pbar.update()
                continue
            for mask in masks:
                ship_list_dict.append({
                    'ImageId': ImageId,
                    'EncodedPixels': rle_encoding(mask)
                })
            #if idx < 30:
            #    cv2.imwrite(os.path.join("output", filename), mask_whole*255)
            pbar.update()
        #fid.close()
        pred_df = pd.DataFrame(ship_list_dict)
        pred_df = pred_df[['ImageId', 'EncodedPixels']]
        pred_df.to_csv('submission.csv', index=False)
        print('done!')
Example #31
0
def forward(pred_func,
            output_folder,
            forward_dataset,
            generic_images_folder=None,
            generic_images_pattern=None):
    if forward_dataset.lower() == "davis":
        imgs = glob.glob("/fastwork/" + os.environ["USER"] +
                         "/mywork/data/DAVIS2017/JPEGImages/480p/*/00000.jpg")
        seq_idx = -2
    elif forward_dataset.lower() == "oxford":
        mywork_dir = "/fastwork/" + os.environ["USER"] + "/mywork/"
        if args.mywork_folder is not None:
            mywork_dir = args.mywork_folder
        imgs = [
            os.path.join(mywork_dir, x.strip()) for x in
            open("/home/" + os.environ["USER"] +
                 "/vision/TrackingAnnotationTool/to_annotate_single_imgs.txt").
            readlines() if len(x.strip()) > 0
        ]
        seq_idx = -4
    elif forward_dataset.lower() == "kitti_tracking":
        imgs = glob.glob(
            "/fastwork/" + os.environ["USER"] +
            "/mywork/data/kitti_training_minimum/training/image_02/*/*.png")
        seq_idx = -2
    elif forward_dataset.lower() == "generic":
        generic_images_pattern = generic_images_pattern.replace("//", "/")
        assert generic_images_folder is not None, "For a generic dataset, a data folder must be given."
        assert generic_images_pattern is not None, "For a generic dataset, an image pattern must be given."
        # Find the images in the folder
        imgs = sorted(
            glob.glob(
                os.path.join(generic_images_folder, generic_images_pattern)))

        print("Num. images: %d" % len(imgs))
        seq_idx = None
    else:
        assert False, ("Unknown dataset", forward_dataset)
    tf.gfile.MakeDirs(output_folder)
    n_total = len(imgs)

    t0 = time.time()
    for idx, img in enumerate(imgs):
        print(idx, "/", n_total)
        if forward_dataset.lower() == "generic":
            seq = "/".join(
                img.replace(generic_images_folder, "").split("/")[:-1])
            seq_folder = output_folder + "/" + seq
        else:
            seq = img.split("/")[seq_idx]
            seq_folder = output_folder + "/" + seq
        img_filename = img.split("/")[-1]
        tf.gfile.MakeDirs(seq_folder)

        output_filename = seq_folder + "/" + img_filename.replace(
            ".jpg", ".json").replace(".png", ".json")
        output_filename_embeddings = seq_folder + "/" + img_filename.replace(
            ".jpg", ".npz").replace(".png", ".npz")
        if os.path.isfile(output_filename):
            print("skipping", output_filename, "because it already exists")
            continue
        else:
            print(output_filename)

        img_val = cv2.imread(img, cv2.IMREAD_COLOR)
        assert img_val is not None, ("unable to load", img)
        if config.PROVIDE_BOXES_AS_INPUT:
            # TODO get some reasonable boxes (not sure which format)
            input_boxes = np.array([[202.468384, 566.15271, 999, 800],
                                    [0, 0, 100, 100], [100, 100, 200, 200]],
                                   np.float32)
            results = detect_one_image(img_val, pred_func, input_boxes)

        else:
            t0_det = time.time()
            results = detect_one_image(img_val, pred_func)
            t1_det = time.time()
        print(len(results))

        # store visualization (slow + optional)
        if args.forward_visualization:
            final = draw_final_outputs(img_val, results)
            viz = np.concatenate((img_val, final), axis=1)
            #tpviz.interactive_imshow(viz)
            viz_output_filename = seq_folder + "/" + img_filename.replace(
                ".png", ".jpg")
            cv2.imwrite(viz_output_filename, viz)

        # store as json
        t0_json = time.time()
        results_json = convert_results_to_json(results, idx)
        t1_json = time.time()
        print('Json conv. time: %f s' % (t1_json - t0_json))
        with open(output_filename, 'w') as f:
            #json.dump(results_json, f, sort_keys=True, indent=4, separators=(',', ': '))
            json.dump(results_json, f)  # Non-pretty

        M_emb = convert_embeddings_to_binary(results)

        t0_cmp_npz = time.time()
        np.savez_compressed(output_filename_embeddings,
                            a=M_emb.astype(np.float16))
        t1_cmp_npz = time.time()

    t1 = time.time()
    time_elapsed = t1 - t0
Example #32
0
 def _build_coco_predictor(self, idx):
     graph_func = self.trainer.get_predictor(self._in_names, self._out_names, device=idx)
     return lambda img: detect_one_image(img, graph_func)