Exemple #1
0
def train_model(model):
    validation_batch_size = 128
    train_batch_size = 128
    bag_tracklets = multibag.find_bag_tracklets(
        '/data/didi/didi-round1/Didi-Release-2/Data/',
        '/old_data/output/tracklet/')

    # Good shuffle seeds: (7, 0.15)
    shuffleseed = 7
    multibag.shuffle(bag_tracklets, shuffleseed)
    split = multibag.train_validation_split(bag_tracklets, 0.15)

    validation_stream = multibag.MultiBagStream(split.validation_bags)
    validation_generator = TrainDataGenerator(validation_stream,
                                              include_ground_truth=True)

    training_stream = multibag.MultiBagStream(split.train_bags)
    training_generator = TrainDataGenerator(training_stream,
                                            include_ground_truth=True)

    print('train: ', training_generator.get_count(), ', validation: ',
          validation_generator.get_count())

    checkpoint_path = get_model_filename(
        CHECKPOINT_DIR, suffix='e{epoch:02d}-vl{val_loss:.2f}')

    # Set up callbacks. Stop early if the model does not improve. Save model checkpoints.
    # Source: http://stackoverflow.com/questions/37293642/how-to-tell-keras-stop-training-based-on-loss-value
    callbacks = [
        EarlyStopping(monitor='val_loss', patience=2, verbose=0),
        ModelCheckpoint(checkpoint_path,
                        monitor='val_loss',
                        save_best_only=False,
                        verbose=0),
    ]

    hist = model.fit_generator(
        training_generator.generate(train_batch_size),
        steps_per_epoch=(training_generator.get_count() / train_batch_size),
        epochs=100,
        # Values for quick testing:
        # steps_per_epoch = (128 / batch_size),
        # epochs = 2,
        validation_data=validation_generator.generate(validation_batch_size),
        validation_steps=(validation_generator.get_count() /
                          validation_batch_size),
        callbacks=callbacks)
    model.save(get_model_filename(MODEL_DIR))
    # print(hist)

    with open(get_model_filename(HISTORY_DIR, '', 'p'), 'wb') as f:
        pickle.dump(hist.history, f)
Exemple #2
0
def try_undistort(desired_count):
    undist = cc.CameraConverter()

    bagdir = '/data/bags/didi-round2/release/car/training/suburu_leading_at_distance'
    bt = mb.find_bag_tracklets(bagdir, '/data/tracklets')
    multi = mb.MultiBagStream(bt, ns.generate_numpystream)
    generator = multi.generate(infinite = False)
    count = 0
    output_count = 0
    for numpydata in generator:
        im = numpydata.image
        frame_idx, obs = numpydata.obs
        im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        undistorted = undist.undistort_image(im)
        if count % 25 == 0:
            cv2.imwrite('/data/dev/camera/orig_{}.png'.format(count), im)

            # Print center.
            img_point = undist.project_point(obs.position)
            cv2.circle(undistorted, (int(img_point[0]), int(img_point[1])), radius = 5, color = (255, 0, 0), thickness=2)

            # Print bbox corners.
            img_points = undist.project_points(obs.get_bbox().transpose())
            for img_point in img_points:
                cv2.circle(undistorted, (int(img_point[0]), int(img_point[1])), radius = 5, color = (0, 255, 0), thickness=2)

            cv2.imwrite('/data/dev/camera/undist_{}.png'.format(count), undistorted)
            output_count += 1
        count += 1
        if desired_count is not None and output_count == desired_count:
            return
Exemple #3
0
def try_detector():
    detector = get_latest_detector()

    bagdir = '/data/bags/didi-round2/release/car/training/suburu_leading_front_left'
    bt = mb.find_bag_tracklets(bagdir, '/data/tracklets')
    multi = mb.MultiBagStream(bt, numpystream.generate_numpystream)
    generator = generate_birdseye_boxes_single(multi, infinite=False)
    for birdseye_box, yaw in generator:
        prediction = detector.detect_rotation(birdseye_box)
        print('gt_yaw: [{}], predicted_yaw: [{}]'.format(yaw, prediction))
Exemple #4
0
def predict_tracklet(model_file,
                     bag_file,
                     metadata_path,
                     output_file,
                     pickle_file=None):
    md = extract_metadata(load_metadata(metadata_path), 'obs1')
    tracklet = generate_tracklet.Tracklet(object_type=md['object_type'],
                                          l=md['l'],
                                          w=md['w'],
                                          h=md['h'],
                                          first_frame=0)

    model = keras.models.load_model(model_file)

    datastream = multibag.MultiBagStream(
        [multibag.BagTracklet(bag_file, None)], use_pickle_adapter=False)
    input_generator = generator.TrainDataGenerator(datastream,
                                                   include_ground_truth=False)

    batch_size = 64
    print('input_generator.get_count()', input_generator.get_count())
    steps = input_generator.get_count() / batch_size
    if input_generator.get_count() % batch_size > 0:
        steps += 1
    print('steps', steps)
    predictions = model.predict_generator(input_generator.generate(batch_size),
                                          steps)

    print('predictions.shape:', predictions.shape)

    # Remove extra predictions due to using batch-based input_generator.
    predictions = predictions[:input_generator.get_count()]

    print('predictions.shape:', predictions.shape)

    tracklet.poses = numpy_preds_to_dicts(predictions)
    tracklet_collection = generate_tracklet.TrackletCollection()
    tracklet_collection.tracklets.append(tracklet)
    tracklet_collection.write_xml(output_file)

    if pickle_file:
        result_map = dict()
        result_map['model_file'] = model_file
        result_map['bag_file'] = bag_file
        result_map['predictions'] = predictions

        with open(pickle_file, 'wb') as f:
            pickle.dump(result_map, f)
Exemple #5
0
def try_draw_panoramas():
    import cv2
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    bagdir = '/data/bags/'
    # bagdir = '/data/bags/didi-round2/release/car/training/suburu_leading_front_left'
    # bagdir = '/data/bags/didi-round2/release/pedestrian/'
    # bag_file = '/data/bags/didi-round2/release/car/testing/ford02.bag'
    bt = mb.find_bag_tracklets(bagdir, '/data/tracklets')
    multi = mb.MultiBagStream(bt, ns.generate_numpystream)

    # numpystream = ns.generate_numpystream(bag_file, tracklet)
    generator = generate_panoramas_multi(multi)

    id = 1
    for im, bbox, obs in generator:
        cv2.rectangle(im, tuple(bbox[0]), tuple(bbox[1]), color = (255, 0, 0))
        im = cv2.resize(im, (0,0), fx=1.0, fy=8.0)
        plt.imshow(im)
        plt.show()
Exemple #6
0
def try_rotating_images(train_dir):
    bagdir = '/data/bags/didi-round2/release/car/training/suburu_leading_front_left'
    bt = mb.find_bag_tracklets(bagdir, '/data/tracklets')

    multi = mb.MultiBagStream(bt, numpystream.generate_numpystream)
    generator = generate_birdseye_boxes_single(multi, infinite=False)
    count = 0
    frames_since_last_conversion = 0
    for birdseye_box, yaw in generator:
        if (yaw > (math.pi / 4) or yaw <
            (-math.pi / 4)) and frames_since_last_conversion > 10:
            # Try to undo rotation with negative yaw.
            rotated = rotate_image(birdseye_box, -yaw)
            # Expect car to have zero rotation in image.
            cv2.imwrite('rotate_test_{}.png'.format(count), rotated)
            print('count: {}, orig_yaw: {}'.format(count, yaw))
            count += 1
            frames_since_last_conversion = 0
            if count % 10 == 0:
                return
        else:
            frames_since_last_conversion += 1
Exemple #7
0
def try_write():
    bagdir = '/data/bags/'
    # bagdir = '/data/bags/didi-round2/release/car/training/suburu_leading_front_left'
    bt = mb.find_bag_tracklets(bagdir, '/data/tracklets')
    multi = mb.MultiBagStream(bt, ns.generate_numpystream)
    write_train_data(multi, '/home/eljefec/repo/squeezeDet/data/KITTI/panorama912x48')
Exemple #8
0
def generate_kitti(bag_tracklets, imagedir, labeldir, output_bbox,
                   slice_config):
    if not os.path.exists(imagedir):
        os.makedirs(imagedir)
    if not os.path.exists(labeldir):
        os.makedirs(labeldir)

    print('bag_tracklets', bag_tracklets)
    print('imagedir', imagedir)
    print('labeldir', labeldir)
    print('output_bbox', output_bbox)
    print('slice_config', slice_config)

    id = 0
    expected_shape = get_expected_shape(slice_config)
    new_shape = (expected_shape[0] - 1, expected_shape[1] - 1,
                 expected_shape[2])

    assert_shape(new_shape)

    print('expected_shape', expected_shape)
    print('new_shape', new_shape)

    stopwatch = util.stopwatch.Stopwatch()

    stream = multibag.MultiBagStream(bag_tracklets,
                                     numpystream.generate_numpystream)

    stopwatch.stop()
    print('Elapsed time: {}'.format(stopwatch.format_duration()))
    stopwatch.start()

    for numpydata in stream.generate(infinite=False):
        lidar = numpydata.lidar
        obs = numpydata.obs
        if lidar is not None:
            frame_idx, obs = obs
            bbox = bbox_points(obs)

            birdseye = ld.lidar_to_birdseye(lidar, slice_config)
            birdseye_bbox = ld.lidar_to_birdseye(bbox,
                                                 slice_config,
                                                 return_points=True)

            if birdseye_bbox.shape[0] == 2 and birdseye_bbox.shape[1] == 2:
                if output_bbox:
                    bbox_tuple = ((birdseye_bbox[0][0], birdseye_bbox[0][1]),
                                  (birdseye_bbox[1][0], birdseye_bbox[1][1]))
                else:
                    bbox_tuple = None

                crop = ci.crop_image(birdseye, expected_shape, new_shape)
                image_file = os.path.join(imagedir, '{:06d}.png'.format(id))
                imlib.save_np_image(crop, image_file, bbox_tuple)

                label_path = os.path.join(labeldir, '{:06d}.txt'.format(id))
                write_kitti_annotation(obs, birdseye_bbox, label_path)

                id += 1
                if id % 1000 == 0:
                    print('Working... Processed {} samples.'.format(id))
                    stopwatch.stop()
                    print('Elapsed time: {}'.format(
                        stopwatch.format_duration()))
                    stopwatch.start()
    print('DONE. Processed {} samples.'.format(id))
    stopwatch.stop()
    print('Elapsed time: {}'.format(stopwatch.format_duration()))