예제 #1
0
def main():

    cls_map = {
        'VEHICLE': label_pb2.Label.TYPE_VEHICLE,
        'CYCLIST': label_pb2.Label.TYPE_CYCLIST,
        'PEDESTRIAN': label_pb2.Label.TYPE_PEDESTRIAN
    }

    ensemble = metrics_pb2.Objects()
    for file in load_pathnames_classes:
        pathname, cls = file
        print('Processing', pathname, 'for class', cls)

        cls = [cls_map[c] for c in cls]
        # load bin
        objects = metrics_pb2.Objects()
        with open(pathname, 'rb') as f:
            objects.ParseFromString(f.read())
        for o in objects.objects:
            if o.object.type in cls:
                ensemble.objects.append(o)

    # save ensemble
    with open(save_pathname, 'wb') as f:
        f.write(ensemble.SerializeToString())
    print(save_pathname, 'saved.')
def main(_):
    # Check flag file_path
    if not FLAGS.detections_file:
        raise ValueError('file_path must be specified')

    write_ground_truths = FLAGS.write_ground_truths
    detections_file = FLAGS.detections_file

    # detections_file = ["predictions_test/aofinal_frcnn_500_256_extrafeat_2cameras/12_aofinal_frcnn_500_256_extrafeat_886.tfrecord",
    #                    "predictions_test/aofinal_frcnn_500_256_extrafeat_2cameras/13_aofinal_frcnn_500_256_extrafeat_1280.tfrecord"]
    # detections_file = ["predictions_test/aofinal_frcnn_500_256_extrafeat_redsoftmax_2cameras/14_aofinal_frcnn_500_256_extrafeat_redsoftmax_886.tfrecord",
    #                    "predictions_test/aofinal_frcnn_500_256_extrafeat_redsoftmax_2cameras/15_aofinal_frcnn_500_256_extrafeat_redsoftmax_1280.tfrecord"]
    #detections_file = ["predictions_test/aofinal_frcnn_500_256_extrafeat_redsoftmax_2cameras_sample3/aofinal_frcnn_500_256_extrafeat_redsoftmax_886.tfrecord",
    #                   "predictions_test/aofinal_frcnn_500_256_extrafeat_redsoftmax_2cameras_sample3/aofinal_frcnn_500_256_extrafeat_redsoftmax_1280.tfrecord"]

    # detections_file = ["predictions_test/aofinal_frcnn_500_256_extrafeat_2cameras_sample3/aofinal_frcnn_500_256_extrafeat_886_3.tfrecord",
    #                     "predictions_test/aofinal_frcnn_500_256_extrafeat_2cameras_sample3/aofinal_frcnn_500_256_extrafeat_1280_3.tfrecord"]

    detections_dataset = tf.data.TFRecordDataset(detections_file, compression_type='') \
        .map(lambda x: parse_camera_tfrecord_example(x, with_detections=True))

    # Choose default output_path if not defined
    if not FLAGS.output_path:
        output_path = "/"
        output_path = output_path.join(detections_file.split("/")[:-1]) + "/"
    else:
        output_path = FLAGS.output_path

    # output_path = "predictions_test/aofinal_frcnn_500_256_extrafeat_2cameras_sample3/"
    #output_path = "predictions_test/aofinal_frcnn_500_256_extrafeat_redsoftmax_2cameras_sample3/"
    #output_path = "predictions_test/aofinal_frcnn_500_256_extrafeat_weights_2cameras/"

    # Create output directory if it does not exist
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    # Parse data
    predictions = metrics_pb2.Objects()
    ground_truths = metrics_pb2.Objects()

    for i, frame in tqdm(enumerate(detections_dataset)):
        # if frame['image/time_of_day'].numpy()==b'Night':
        frame_gts, frame_pds = create_frame_objects(frame, write_ground_truths)
        predictions.objects.extend(frame_pds)
        if write_ground_truths:
            ground_truths.objects.extend(frame_gts)

    # Write data
    split = "validation" if "validation" in detections_file else "testing"
    write_serialized_string(output_path + split + "_predictions.bin",
                            predictions)
    if write_ground_truths:
        write_serialized_string(output_path + split + "_ground_truths.bin",
                                ground_truths)

    return 0
    def combine(self, pathnames):
        combined = metrics_pb2.Objects()

        for pathname in pathnames:
            objects = metrics_pb2.Objects()
            with open(pathname, 'rb') as f:
                objects.ParseFromString(f.read())
            for o in objects.objects:
                combined.objects.append(o)

        return combined
예제 #4
0
def main():

    # get selected num list
    with open(val_list_load_pathname, 'r') as f:
        lines = f.readlines()
    selected = lines[:int(len(lines) / 10)]

    # get file idx and frame idx
    file_frame = defaultdict(list)
    for num in selected:
        prefix, file_idx, frame_idx = num[0], int(num[1:4]), int(num[4:7])
        file_frame[file_idx].append(frame_idx)

    # get ids
    selected_ids = set()
    tfrecord_pathnames = sorted(glob(join(tfrecords_load_dir, '*.tfrecord')))
    for file_idx, frame_idxs in file_frame.items():

        file_pathname = tfrecord_pathnames[file_idx]
        file_data = tf.data.TFRecordDataset(file_pathname, compression_type='')

        # process each frame
        for frame_idx in frame_idxs:
            frame = file_data[frame_idx]

        # prepare context_name and frame_timestamp_micros
        context_name = frame.context.name
        frame_timestamp_micros = frame.timestamp_micros

        selected_ids.add('{}-{}'.format(str(context_name),
                                        str(frame_timestamp_micros)))

    # get the gt with selected ids
    gt_objects = metrics_pb2.Objects()
    gt_small_objects = metrics_pb2.Objects()
    in_cnt = 0
    out_cnt = 0
    with open(gt_load_pathname, 'rb') as f:
        gt_objects.ParseFromString(f.read())
    for obj in gt_objects.objects:
        context_name = obj.context_name
        frame_timestamp_micros = obj.frame_timestamp_micros
        id = '{}-{}'.format(str(context_name), str(frame_timestamp_micros))
        if id in selected_ids:
            in_cnt += 1
            gt_small_objects.objects.append(obj)
        else:
            out_cnt += 1

    print(in_cnt, out_cnt, 'should be around 1:9')

    with open(gt_small_save_pathname, 'wb') as f:
        f.write(gt_small_objects.SerializeToString())
예제 #5
0
def getData(filename, score):
    with open(filename, 'rb') as f:
        buf = f.read()
        labels_decoded = metrics_pb2.Objects()
        labels_decoded.ParseFromString(buf)
    out = metrics_pb2.Objects()

    for label in labels_decoded.objects:
        if label.score >= float(score):
            out.objects.append(label)

    with open(filename[:-4] + "_truncated.bin", 'wb') as f:
        f.write(out.SerializeToString())
예제 #6
0
def load_prediction():
    objects = metrics_pb2.Objects()
    sample_time_tokens = []
    pred_boxes = defaultdict(list)
    #f = open("/home/gowithrobo/4_semester/tracking/waymo/detection/detection_3d_cyclist_detection_train.bin", 'rb')
    #objects.ParseFromString(f.read())
    #f.close()
    Path = "/scratch/shk642/waymo/detection/"
    filelist = listdir(Path)
    for i in filelist:
        if i.endswith(".bin"):
            with open(Path + i, 'rb') as f:
                objects.ParseFromString(f.read())
                for object in objects.objects:
                    sample_time_tokens.append(object.frame_timestamp_micros)
                    pred_boxes[object.frame_timestamp_micros].append({
                        'sample_time_token':
                        object.frame_timestamp_micros,
                        'translation': (object.object.box.center_x,
                                        object.object.box.center_y,
                                        object.object.box.center_z),
                        'size':
                        (object.object.box.height, object.object.box.width,
                         object.object.box.length),
                        'rotation':
                        object.object.box.heading,
                        'detection_name':
                        object.object.type
                    })
    return pred_boxes, sample_time_tokens
def loadcreatedobjectfiles(objectfilepath):
    objects = metrics_pb2.Objects()
    with open(objectfilepath, "rb") as fd:
        objects.ParseFromString(fd.read())
    print(type(objects)) #<class 'waymo_open_dataset.protos.metrics_pb2.Objects'>
    print('Got ', len(objects.objects), 'objects')
    return objects
예제 #8
0
def create_waymo_sumbit(waymo_annos, out_path):
    print('Creating Waymo submission...')
    objects = metrics_pb2.Objects()

    for token in tqdm(waymo_annos):
        for waymo_anno in waymo_annos[token]:
            o = metrics_pb2.Object()
            o.context_name = waymo_anno['context_name']
            o.frame_timestamp_micros = waymo_anno['frame_timestamp_micros']

            box = label_pb2.Label.Box()
            box.center_x = waymo_anno['ego_box_3d'][0]
            box.center_y = waymo_anno['ego_box_3d'][1]
            box.center_z = waymo_anno['ego_box_3d'][2]
            box.length = waymo_anno['dimension'][2]
            box.width = waymo_anno['dimension'][1]
            box.height = waymo_anno['dimension'][0]
            box.heading = waymo_anno['ego_heading']
            o.object.box.CopyFrom(box)

            o.score = waymo_anno['score']
            o.object.id = str(waymo_anno['object_id'])
            o.object.type = waymo_anno['cat']

            o.object.num_lidar_points_in_box = 100

            objects.objects.append(o)

    # Write objects to a file.
    with open(out_path, 'wb') as f:
        f.write(objects.SerializeToString())
예제 #9
0
    def anns2proto(self, outfile_prefix):
        anns = self.coco.anns
        ground_truths = metrics_pb2.Objects()

        for _, ann in anns.items():
            obj = metrics_pb2.Object()

            img_info = self.data_infos[ann['image_id']]

            x1, y1, w, h = ann['bbox']

            cx = x1 + w / 2
            cy = y1 + h / 2

            lab = label_pb2.Label()
            lab.box.center_x = cx
            lab.box.center_y = cy
            lab.box.length = w
            lab.box.width = h
            lab.type = 4 if ann['category_id'] == 3 else ann['category_id']
            lab.detection_difficulty_level = 1 if ann[
                'det_difficult'] == 0 else ann['det_difficult']
            obj.object.MergeFrom(lab)

            obj.context_name = img_info["context_name"]
            obj.frame_timestamp_micros = img_info["timestamp_micros"]
            obj.camera_name = img_info["camera_id"]

            ground_truths.objects.append(obj)

        f = open(outfile_prefix + "_gt.bin", 'wb')
        serialized = ground_truths.SerializeToString()
        f.write(serialized)
        f.close()
예제 #10
0
def tfRecordToBin(data_file_name, data_type):

    if data_type == 'vehicle':
        data_type_label = label_pb2.Label.TYPE_VEHICLE
    elif data_type == 'pedestrian':
        data_type_label = label_pb2.Label.TYPE_PEDESTRIAN
    elif data_type == 'cyclist':
        data_type_label = label_pb2.Label.TYPE_CYCLIST
    else:
        print("Usage: python tfRecordDataToLabel.py data.tfrecord vehicle")
        sys.exit(1)

    result_file_name = data_file_name[:-9] + "_" + data_type + "_gt" + '.bin'

    objs = metrics_pb2.Objects()

    dataset = tf.data.TFRecordDataset(data_file_name, compression_type='')
    for data in dataset:
        frame = dataset_pb2.Frame()
        frame.ParseFromString(bytearray(data.numpy()))
        for frame_obj in frame.laser_labels:
            if not frame_obj.type == data_type_label:
                continue
            obj = metrics_pb2.Object()
            obj.object.box.CopyFrom(frame_obj.box)
            obj.object.type = frame_obj.type
            obj.object.id = frame_obj.id
            obj.context_name = frame.context.name
            obj.frame_timestamp_micros = frame.timestamp_micros
            objs.objects.append(obj)

    with open(result_file_name, 'wb') as f:
        f.write(objs.SerializeToString())
예제 #11
0
def make_gt_file_native(data_path,
                        gt_save_path,
                        view=dataset_pb2.CameraName.FRONT):

    scene_files = sorted(os.listdir(data_path))
    gt_objects = metrics_pb2.Objects()

    for scene_num, record_file in enumerate(scene_files):
        record = tf.data.TFRecordDataset(os.path.join(data_path, record_file),
                                         compression_type='')

        for frame_data in record:
            frame = dataset_pb2.Frame()
            frame.ParseFromString(bytearray(frame_data.numpy()))

            for camera_labels in frame.camera_labels:
                if camera_labels.name != view: continue
                for label in camera_labels.labels:
                    gt_objects.objects.append(create_gt_obj(
                        frame, label, view))

        print("Finished scene", scene_num)

    f = open(gt_save_path, 'wb')
    f.write(gt_objects.SerializeToString())
    f.close()
예제 #12
0
def compare(filename, filename_gt):
    with open(filename, 'rb') as f:
        buf = f.read()
        dataset = metrics_pb2.Objects()
        dataset.ParseFromString(buf)

    with open(filename_gt, 'rb') as f:
        buf = f.read()
        dataset_gt = metrics_pb2.Objects()
        dataset_gt.ParseFromString(buf)

    names = {}
    global num_ctr
    num_ctr = 0

    def assignNum(name):
        global num_ctr
        if name in names:
            return names[name]
        num_ctr += 1
        names[name] = num_ctr

    unknown = 0
    correct = 0
    incorrect = 0

    print('data', "\t", 'gt')
    for i in range(max(len(dataset.objects), len(dataset_gt.objects))):
        if i < len(dataset.objects):
            label = dataset.objects[i].object.id
        else:
            label = "done"
        if i < len(dataset_gt.objects):
            label_gt = assignNum(dataset_gt.objects[i].object.id)
        else:
            label_gt = "done"
        if label == "":
            label = None
        if label_gt == None or label == None:
            unknown += 1
        elif label == str(label_gt):
            correct += 1
        else:
            incorrect += 1
        print(label, "\t", label_gt)

    print(correct, incorrect, unknown)
예제 #13
0
def _create_pd_file(obj_list, filename):
    """Creates a prediction objects file."""
    objects = metrics_pb2.Objects()
    for obj in obj_list:
        objects.objects.append(obj)
    f = open(filename, 'wb')
    f.write(objects.SerializeToString())
    f.close()
예제 #14
0
def _create_gt_detection(infos, tracking=True):
    """Creates a gt prediction object file for local evaluation."""
    from waymo_open_dataset import label_pb2
    from waymo_open_dataset.protos import metrics_pb2
    
    objects = metrics_pb2.Objects()

    for idx in tqdm(range(len(infos))): 
        info = infos[idx]

        obj = get_obj(info['path'])
        annos = obj['objects']
        num_points_in_gt = np.array([ann['num_points'] for ann in annos])
        box3d = np.array([ann['box'] for ann in annos])

        if len(box3d) == 0:
            continue 

        names = np.array([TYPE_LIST[ann['label']] for ann in annos])

        box3d = box3d[:, [0, 1, 2, 3, 4, 5, -1]]

        for i in range(box3d.shape[0]):
            if num_points_in_gt[i] == 0:
                continue 
            if names[i] == 'UNKNOWN':
                continue 

            det  = box3d[i]
            score = 1.0
            label = names[i]

            o = metrics_pb2.Object()
            o.context_name = obj['scene_name']
            o.frame_timestamp_micros = int(obj['frame_name'].split("_")[-1])

            # Populating box and score.
            box = label_pb2.Label.Box()
            box.center_x = det[0]
            box.center_y = det[1]
            box.center_z = det[2]
            box.length = det[3]
            box.width = det[4]
            box.height = det[5]
            box.heading = det[-1]
            o.object.box.CopyFrom(box)
            o.score = score
            # Use correct type.
            o.object.type = CAT_NAME_TO_ID[label]
            o.object.num_lidar_points_in_box = num_points_in_gt[i]
            o.object.id = annos[i]['name']

            objects.objects.append(o)
        
    # Write objects to a file.
    f = open(os.path.join(args.result_path, 'gt_preds.bin'), 'wb')
    f.write(objects.SerializeToString())
    f.close()
    def parse_objects(self, kitti_result_pathname, T_k2w, context_name,
                      frame_timestamp_micros):
        def parse_one_object(line):
            attrs = line.split()

            cls = attrs[0]
            height = float(attrs[8])
            width = float(attrs[9])
            length = float(attrs[10])
            x = float(attrs[11])
            y = float(attrs[12])
            z = float(attrs[13])
            rotation_y = float(attrs[14])
            score = float(attrs[15])

            # y: downwards; move box origin from bottom center (kitti) to true center (waymo)
            y = float(attrs[12]) - height / 2
            x, y, z = self.transform(T_k2w, x, y,
                                     z)  # frame transformation: kitti -> waymo

            # different conventions
            heading = -(rotation_y + np.pi / 2)
            while heading < -np.pi:
                heading += 2 * np.pi
            while heading > np.pi:
                heading -= 2 * np.pi

            # populate box
            box = label_pb2.Label.Box()
            box.center_x = x
            box.center_y = y
            box.center_z = z
            box.length = length
            box.width = width
            box.height = height
            box.heading = heading

            o = metrics_pb2.Object()
            o.object.box.CopyFrom(box)
            o.object.type = self.k2w_cls_map[cls]
            o.score = score

            # for identification of the frame
            o.context_name = context_name
            o.frame_timestamp_micros = frame_timestamp_micros

            return o

        objects = metrics_pb2.Objects()
        with open(kitti_result_pathname, 'r') as f:
            lines = f.readlines()

        for line in lines:
            o = parse_one_object(line)
            objects.objects.append(o)

        return objects
예제 #16
0
    def dump_detection_output(self, idx: Union[int, tuple],
                              detections: Target3DArray,
                              fout: RawIOBase) -> None:
        '''
        :param detections: detection result
        :param ids: auxiliary information for output, each item contains context name and timestamp
        :param fout: output file-like object
        '''
        try:
            from waymo_open_dataset import label_pb2
            from waymo_open_dataset.protos import metrics_pb2
        except:
            _logger.error(
                "Cannot find waymo_open_dataset, install the package at "
                "https://github.com/waymo-research/waymo-open-dataset, output will be skipped now."
            )
            return

        label_map = {
            WaymoObjectClass.Unknown: label_pb2.Label.TYPE_UNKNOWN,
            WaymoObjectClass.Vehicle: label_pb2.Label.TYPE_VEHICLE,
            WaymoObjectClass.Pedestrian: label_pb2.Label.TYPE_PEDESTRIAN,
            WaymoObjectClass.Sign: label_pb2.Label.TYPE_SIGN,
            WaymoObjectClass.Cyclist: label_pb2.Label.TYPE_CYCLIST
        }

        waymo_array = metrics_pb2.Objects()
        for target in detections:
            waymo_target = metrics_pb2.Object()

            # convert box parameters
            box = label_pb2.Label.Box()
            box.center_x = target.position[0]
            box.center_y = target.position[1]
            box.center_z = target.position[2]
            box.length = target.dimension[0]
            box.width = target.dimension[1]
            box.height = target.dimension[2]
            box.heading = target.yaw
            waymo_target.object.box.CopyFrom(box)

            # convert label
            waymo_target.object.type = label_map[target.tag_top]
            waymo_target.score = target.tag_top_score

            waymo_target.context_name = idx[
                0]  # the name of the sequence is the context
            waymo_target.frame_timestamp_micros = int(
                self.timestamp(idx) * 1e6)
            waymo_array.objects.append(waymo_target)

        bindata = waymo_array.SerializeToString()
        if isinstance(fout, (str, Path)):
            Path(fout).write_bytes(bindata)
        else:
            fout.write(bindata)
예제 #17
0
def create_result(pose_v, theta_v, trackers, tracking_name, scene_token,
                  current_sample_token):
    """Creates a prediction objects file."""
    objects = metrics_pb2.Objects()

    o = metrics_pb2.Object()
    # The following 3 fields are used to uniquely identify a frame a prediction
    # is predicted at. Make sure you set them to values exactly the same as what
    # we provided in the raw data. Otherwise your prediction is considered as a
    # false positive.
    o.context_name = scene_token
    # The frame timestamp for the prediction. See Frame::timestamp_micros in
    # dataset.proto.
    invalid_ts = -1
    o.frame_timestamp_micros = current_sample_token
    # This is only needed for 2D detection or tracking tasks.
    # Set it to the camera name the prediction is for.
    #o.camera_name = dataset_pb2.CameraName.FRONT

    # Populating box and score.
    box = label_pb2.Label.Box()
    box.center_x = pose_v[0]
    box.center_y = pose_v[1]
    box.center_z = pose_v[2]
    box.length = trackers[2]
    box.width = trackers[1]
    box.height = trackers[0]
    box.heading = 0
    o.object.box.CopyFrom(box)
    # This must be within [0.0, 1.0]. It is better to filter those boxes with
    # small scores to speed up metrics computation.
    o.score = 0.5
    # For tracking, this must be set and it must be unique for each tracked
    # sequence.
    # tracking_id = np.array(trackers[7])
    # tracking_id = tracking_id.tobytes()
    # tracking_id = tracking_id.encode('utf-8')
    o.object.id = str(int(trackers[7]))
    # Use correct type.
    if tracking_name == 1:
        o.object.type = label_pb2.Label.TYPE_VEHICLE
    elif tracking_name == 2:
        o.object.type = label_pb2.Label.TYPE_PEDESTRIAN
    elif tracking_name == 4:
        o.object.type = label_pb2.Label.TYPE_CYCLIST
    objects.objects.append(o)

    # Add more objects. Note that a reasonable detector should limit its maximum
    # number of boxes predicted per frame. A reasonable value is around 400. A
    # huge number of boxes can slow down metrics computation.

    # Write objects to a file.
    f = open('/home/shk642/waymo/waymo-od/waymo-dataset-viewer/tmp/pred00.bin',
             'wb')
    f.write(objects.SerializeToString())
    f.close()
    def combine(self, pathnames):
        """Combine predictions in waymo format for each sample together.

        Args:
            pathnames (str): Paths to save predictions.

        Returns:
            :obj:`Objects`: Combined predictions in Objects proto.
        """
        combined = metrics_pb2.Objects()

        for pathname in pathnames:
            objects = metrics_pb2.Objects()
            with open(pathname, 'rb') as f:
                objects.ParseFromString(f.read())
            for o in objects.objects:
                combined.objects.append(o)

        return combined
예제 #19
0
def getData(filename, context_name):
    with open(filename, 'rb') as f:
        buf = f.read()
        labels_decoded = metrics_pb2.Objects()
        labels_decoded.ParseFromString(buf)
    out = metrics_pb2.Objects()
    detected = False
    for label in labels_decoded.objects:
        if label.context_name == context_name:
            detected = True
            out.objects.append(label)
        elif detected:
            break

    if not detected:
        print("Context not found!")
    else:    
        with open(filename[:-4]+"_"+context_name+".bin", 'wb') as f:
            f.write(out.SerializeToString())
예제 #20
0
def readBin(filename):
    with open(filename, 'rb') as f:
        buf = f.read()
        labels_decoded = metrics_pb2.Objects()
        labels_decoded.ParseFromString(buf)
    # frame = labels_decoded.objects[0].frame_timestamp_micros
    for label in labels_decoded.objects:
        # if label.frame_timestamp_micros != frame:
        #     break
        print(label)
예제 #21
0
def create_pd_objects(detections):
    objects = metrics_pb2.Objects()

    for detection in tqdm(detections):
        # each frame
        context_name, frame_timestamp_micros, camera_name = detection[
            'image_id'].split('/')
        o = create_pd_object(detection, context_name,
                             int(frame_timestamp_micros), camera_name)
        objects.objects.append(o)
    return objects
예제 #22
0
def _create_pd_detection(detections, infos, result_path, tracking=False):
    """Creates a prediction objects file."""
    assert tracking is False, "Not Supported Yet"
    from waymo_open_dataset import dataset_pb2
    from waymo_open_dataset import label_pb2
    from waymo_open_dataset.protos import metrics_pb2
    from waymo_open_dataset.utils import box_utils

    objects = metrics_pb2.Objects()

    for token, detection in tqdm(detections.items()):
        info = infos[token]
        obj = get_obj(info['path'])

        box3d = detection["box3d_lidar"].detach().cpu().numpy()
        scores = detection["scores"].detach().cpu().numpy()
        labels = detection["label_preds"].detach().cpu().numpy()
        box3d[:, -1] = -box3d[:, -1] - np.pi / 2

        if box3d.shape[1] > 7:
            # drop velocity
            box3d = box3d[:, [0, 1, 2, 3, 4, 5, -1]]

        for i in range(box3d.shape[0]):
            det = box3d[i]
            score = scores[i]

            label = labels[i]

            o = metrics_pb2.Object()
            o.context_name = obj['scene_name']
            o.frame_timestamp_micros = int(obj['frame_name'].split("_")[-1])

            # Populating box and score.
            box = label_pb2.Label.Box()
            box.center_x = det[0]
            box.center_y = det[1]
            box.center_z = det[2]
            box.length = det[3]
            box.width = det[4]
            box.height = det[5]
            box.heading = det[-1]
            o.object.box.CopyFrom(box)
            o.score = score
            # Use correct type.
            o.object.type = label_to_type(label)  # int(label)+1

            objects.objects.append(o)

    # Write objects to a file.
    f = open(os.path.join(result_path, 'my_preds.bin'), 'wb')
    f.write(objects.SerializeToString())
    f.close()
예제 #23
0
def parse_all_dt_objects(pathname):
    objects = metrics_pb2.Objects()
    with open(pathname, 'rb') as f:
        objects.ParseFromString(f.read())

    all_dt_objects = defaultdict(list)
    for o in objects.objects:
        context_name = o.context_name
        frame_timestamp_micros = o.frame_timestamp_micros
        all_dt_objects[str(context_name) + '-' +
                       str(frame_timestamp_micros)].append(o)

    return all_dt_objects
예제 #24
0
def _create_pd_file_example():
    """Creates a prediction objects file."""
    objects = metrics_pb2.Objects()

    o = metrics_pb2.Object()
    # The following 3 fields are used to uniquely identify a frame a prediction
    # is predicted at. Make sure you set them to values exactly the same as what
    # we provided in the raw data. Otherwise your prediction is considered as a
    # false positive.
    o.context_name = (
        'context_name for the prediction. See Frame::context::name '
        'in  dataset.proto.')
    # The frame timestamp for the prediction. See Frame::timestamp_micros in
    # dataset.proto.
    invalid_ts = -1
    o.frame_timestamp_micros = invalid_ts
    # This is only needed for 2D detection or tracking tasks.
    # Set it to the camera name the prediction is for.
    o.camera_name = dataset_pb2.CameraName.FRONT

    # Populating box and score.
    box = label_pb2.Label.Box()
    box.center_x = 0
    box.center_y = 0
    box.center_z = 0
    box.length = 0
    box.width = 0
    box.height = 0
    box.heading = 0
    o.object.box.CopyFrom(box)
    # This must be within [0.0, 1.0]. It is better to filter those boxes with
    # small scores to speed up metrics computation.
    o.score = 0.5
    # For tracking, this must be set and it must be unique for each tracked
    # sequence.
    o.object.id = 'unique object tracking ID'
    # Use correct type.
    o.object.type = label_pb2.Label.TYPE_PEDESTRIAN

    objects.objects.append(o)

    # Add more objects. Note that a reasonable detector should limit its maximum
    # number of boxes predicted per frame. A reasonable value is around 400. A
    # huge number of boxes can slow down metrics computation.

    # Write objects to a file.
    f = open('/tmp/your_preds.bin', 'wb')
    f.write(objects.SerializeToString())
    f.close()
예제 #25
0
def create_bin(dataset_path):
    dataset = tf.data.TFRecordDataset(
        [str(x.absolute()) for x in Path(dataset_path).rglob('*.tfrecord')])
    print([str(x.absolute()) for x in Path(dataset_path).rglob('*.tfrecord')])
    objects = metrics_pb2.Objects()
    for data in tqdm.tqdm(dataset):
        frame = dataset_pb2.Frame()
        print(frame.context.name)
        frame.ParseFromString(bytearray(data.numpy()))
        o_list = _fancy_deep_learning(frame)
        for o in o_list:
            objects.objects.append(o)

    f = open("/tmp/tmp.bin", 'wb')
    f.write(objects.SerializeToString())
    f.close()
    def process_one(self, file_num):
        file_pathname = self.waymo_tfrecord_pathnames[file_num]
        file_data = tf.data.TFRecordDataset(file_pathname, compression_type='')

        # process each frame
        for frame_num, frame_data in enumerate(file_data):
            frame = open_dataset.Frame()
            frame.ParseFromString(bytearray(frame_data.numpy()))

            kitti_result_pathname = join(
                self.kitti_results_load_dir,
                '{}{:03d}{:03d}.txt'.format(self.prefix, file_num, frame_num))

            # prepare transformation matrix from kitti to waymo
            # here, the kitti frame is a virtual reference frame
            # the bounding boxes are in the vehicle frame
            for camera in frame.context.camera_calibrations:
                if camera.name == 1:  # FRONT = 1, see dataset.proto for details
                    T_front_cam_to_vehicle = np.array(
                        camera.extrinsic.transform).reshape(4, 4)

            T_k2w = T_front_cam_to_vehicle @ self.T_ref_to_front_cam

            # prepare context_name and frame_timestamp_micros
            context_name = frame.context.name
            frame_timestamp_micros = frame.timestamp_micros

            try:
                objects = self.parse_objects(kitti_result_pathname, T_k2w,
                                             context_name,
                                             frame_timestamp_micros)
            except:
                print(kitti_result_pathname, 'not found.')
                objects = metrics_pb2.Objects()

            # print(file_num, frame_num, '\n', objects)

            # Write objects to a file.
            with open(
                    join(
                        self.waymo_results_save_dir,
                        '{}{:03d}{:03d}.bin'.format(self.prefix, file_num,
                                                    frame_num)), 'wb') as f:
                f.write(objects.SerializeToString())
def _create_pd_file_example():
    """Creates a prediction objects file."""
    objects = metrics_pb2.Objects()

    o = metrics_pb2.Object()
    # 이후에 나오는 3개의 field는 예측을 수행하는 프레임 식별에 사용
    # raw data에서 제공한 값들과 field의 값들을 동일하게 설정할 것 - 그렇지 않을 경우 잘못된 것으로 간주될 수 있음
    o.context_name = (
        'context_name for the prediction. See Frame::context::name '
        'in  dataset.proto.')
    # 예측에 대한 timestamp
    invalid_ts = -1
    o.frame_timestamp_micros = invalid_ts
    # 2D 대상의 검출 또는 추적 작업에만 필요한 것
    # 예측 대상을 카메라 이름으로 설정
    o.camera_name = dataset_pb2.CameraName.FRONT

    # box와 score(값)을 매칭
    box = label_pb2.Label.Box()
    box.center_x = 0
    box.center_y = 0
    box.center_z = 0
    box.length = 0
    box.width = 0
    box.height = 0
    box.heading = 0
    o.object.box.CopyFrom(box)
    # 0.0과 1.0 사이의 값이 되어야 함 - 그렇지 않을 경우 필터링을 거쳐 작은 점수로 변환(matrics 연산 속도 높이기 위해 필요)
    o.score = 0.5
    # 추적을 위해 각각의 추적될 sequence에 대해 설정되고, 또한 고유의 값을 가져야만 함
    o.object.id = 'unique object tracking ID'
    # type 올바르게 설정하기
    o.object.type = label_pb2.Label.TYPE_PEDESTRIAN

    objects.objects.append(o)

    # 합리적인 검출 위해서는 프레임 당 box의 수를 제한해야 함(합리적인 값 = 약 400) - box의 수가 많아질 경우 metrics 연산 속도 느려질 수 있음

    # 파일에 객체 작성
    f = open('/tmp/your_preds.bin', 'wb')
    f.write(objects.SerializeToString())
    f.close()
예제 #28
0
    def results2proto(self, results, outfile_prefix):
        if isinstance(results[0], list):
            dict_results = self._det2dicts(results)

        detections = metrics_pb2.Objects()

        for detection in dict_results:
            obj = metrics_pb2.Object()

            # fig = plt.figure()
            # img = mpimg.imread('data/waymococo_f0/val2020/'+detection['filename'])
            # plt.imshow(img)
            #
            # rect = patches.Rectangle((detection['center_x']-detection['length']/2, detection['center_y'] -detection['width']/2 )
            #                          , detection['length'], detection['width'], linewidth=1, edgecolor='r', facecolor='none')
            #
            # ax = plt.gca()
            # # Add the patch to the Axes
            # ax.add_patch(rect)
            # plt.show()

            lab = label_pb2.Label()
            lab.box.center_x = detection['center_x']
            lab.box.center_y = detection['center_y']
            lab.box.length = detection['length']
            lab.box.width = detection['width']
            lab.type = detection['type']

            obj.object.MergeFrom(lab)
            if detection['score']:
                obj.score = detection['score']

            obj.context_name = detection["context_name"]
            obj.frame_timestamp_micros = detection["timestamp_micros"]
            obj.camera_name = detection["camera_name"]

            detections.objects.append(obj)

        f = open(outfile_prefix + ".bin", 'wb')
        serialized = detections.SerializeToString()
        f.write(serialized)
        f.close()
예제 #29
0
def main():

    gt = metrics_pb2.Objects()
    for pathname in tqdm(glob(join(tfrecord_load_dir, '*.tfrecord'))):

        dataset = tf.data.TFRecordDataset(pathname, compression_type='')

        for frame_idx, data in enumerate(dataset):

            frame = open_dataset.Frame()
            frame.ParseFromString(bytearray(data.numpy()))

            for obj in frame.laser_labels:
                if obj.num_lidar_points_in_box < 1:
                    continue

                o = convert(obj, frame.context.name, frame.timestamp_micros)
                gt.objects.append(o)

    with open(gt_bin_save_pathname, 'wb') as f:
        f.write(gt.SerializeToString())
    print(gt_bin_save_pathname, 'saved.')
    def convert_one(self, file_idx):
        """Convert action for single file.

        Args:
            file_idx (int): Index of the file to be converted.
        """
        file_pathname = self.waymo_tfrecord_pathnames[file_idx]
        file_data = tf.data.TFRecordDataset(file_pathname, compression_type='')

        for frame_num, frame_data in enumerate(file_data):
            frame = open_dataset.Frame()
            frame.ParseFromString(bytearray(frame_data.numpy()))

            filename = f'{self.prefix}{file_idx:03d}{frame_num:03d}'

            for camera in frame.context.camera_calibrations:
                # FRONT = 1, see dataset.proto for details
                if camera.name == 1:
                    T_front_cam_to_vehicle = np.array(
                        camera.extrinsic.transform).reshape(4, 4)

            T_k2w = T_front_cam_to_vehicle @ self.T_ref_to_front_cam

            context_name = frame.context.name
            frame_timestamp_micros = frame.timestamp_micros

            if filename in self.name2idx:
                kitti_result = \
                    self.kitti_result_files[self.name2idx[filename]]
                objects = self.parse_objects(kitti_result, T_k2w, context_name,
                                             frame_timestamp_micros)
            else:
                print(filename, 'not found.')
                objects = metrics_pb2.Objects()

            with open(
                    join(self.waymo_results_save_dir, f'{filename}.bin'),
                    'wb') as f:
                f.write(objects.SerializeToString())