def test_lanenet_batch(image_list, weights_path, save_dir):
    """

    :param src_dir:
    :param weights_path:
    :param save_dir:
    :return:
    """
    # assert ops.exists(src_dir), '{:s} not exist'.format(src_dir)
    save_dir = ops.join(save_dir, "ckpt")
    os.makedirs(save_dir, exist_ok=True)

    input_tensor = tf.placeholder(dtype=tf.float32,
                                  shape=[1, 256, 512, 3],
                                  name='input_tensor')

    net = lanenet.LaneNet(phase='test', net_flag='vgg')
    binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor,
                                                     name='lanenet_model')

    postprocessor = lanenet_postprocess.LaneNetPostProcessor()

    saver = tf.train.Saver()

    # Set sess configuration
    sess_config = tf.ConfigProto()
    sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.TEST.GPU_MEMORY_FRACTION
    sess_config.gpu_options.allow_growth = CFG.TRAIN.TF_ALLOW_GROWTH
    sess_config.gpu_options.allocator_type = 'BFC'

    sess = tf.Session(config=sess_config)

    with sess.as_default():

        saver.restore(sess=sess, save_path=weights_path)

        # image_list = glob.glob('{:s}/**/*.jpg'.format(src_dir), recursive=True)
        # image_list = sample(image_list, 5564)
        # 返回所有匹配的文件路径列表
        avg_time_cost = []
        for index, image_path in tqdm.tqdm(enumerate(image_list),
                                           total=len(image_list)):
            # tqdm: 进度条
            image = cv2.imread(image_path, cv2.IMREAD_COLOR)
            image_vis = image
            image = cv2.resize(image, (512, 256),
                               interpolation=cv2.INTER_LINEAR)
            image = image / 127.5 - 1.0

            t_start = time.time()
            binary_seg_image, instance_seg_image = sess.run(
                [binary_seg_ret, instance_seg_ret],
                feed_dict={input_tensor: [image]})
            avg_time_cost.append(time.time() - t_start)

            postprocess_result = postprocessor.postprocess(
                binary_seg_result=binary_seg_image[0],
                instance_seg_result=instance_seg_image[0],
                source_image=image_vis)

            if index % 100 == 0:
                log.info(
                    'Mean inference time every single image: {:.5f}s'.format(
                        np.mean(avg_time_cost)))
                with open(ops.join(save_dir, 'log'), 'a') as log_file:
                    print('Mean inference time every single image: {:.5f}s'.
                          format(np.mean(avg_time_cost)),
                          file=log_file)
                avg_time_cost.clear()

            input_image_dir = ops.split(image_path.split('clips')[1])[0][1:]
            input_image_name = ops.split(image_path)[1]
            output_image_dir = ops.join(save_dir, input_image_dir)
            os.makedirs(output_image_dir, exist_ok=True)
            output_image_path = ops.join(output_image_dir, input_image_name)
            if ops.exists(output_image_path):
                continue

            cv2.imwrite(output_image_path, postprocess_result['source_image'])

    return
def test_lanenet(image_path, weights_path, pb_path):
    """

    :param image_path:
    :param weights_path:
    :return:
    """
    assert ops.exists(image_path), '{:s} not exist'.format(image_path)

    log.info('Start reading image and preprocessing')
    t_start = time.time()
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    image_vis = image
    image = cv2.resize(image, (512, 256), interpolation=cv2.INTER_LINEAR)
    image = image / 127.5 - 1.0
    log.info('Image load complete, cost time: {:.5f}s'.format(time.time() - t_start))

    postprocessor = lanenet_postprocess.LaneNetPostProcessor()

    # Set sess configuration
    sess_config = tf.ConfigProto()
    sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.TEST.GPU_MEMORY_FRACTION
    sess_config.gpu_options.allow_growth = CFG.TRAIN.TF_ALLOW_GROWTH
    sess_config.gpu_options.allocator_type = 'BFC'

    sess = tf.Session(config=sess_config)

    if pb_path is not None:
        assert ops.exists(pb_path), '{:s} not exist'.format(pb_path)
        print('Start running with pb file ...')
        model_f = gfile.FastGFile(pb_path, 'rb')
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(model_f.read())
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')
        input_img = sess.graph.get_tensor_by_name("input_tensor:0")
        print(input_img)
        out_binary = sess.graph.get_tensor_by_name("lanenet_model/vgg_backend/binary_seg/ArgMax:0")
        print(out_binary)
        out_seg = sess.graph.get_tensor_by_name("lanenet_model/vgg_backend/instance_seg/pix_embedding_conv/pix_embedding_conv:0")
        print(out_seg)
        t_start = time.time()
        binary_seg_image, instance_seg_image = sess.run((out_binary, out_seg), feed_dict={input_img: [image]})
        t_cost = time.time() - t_start
        log.info('Single imgae inference cost time: {:.5f}s'.format(t_cost))

        # print("Binary :" + str(binary_seg_image.shape))
        # print(binary_seg_image)
        # print("Instance :" + str(instance_seg_image.shape))
        # print(instance_seg_image)
        print('done')
    else:
        print('Start running with ckpt ...')
        input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input_tensor')
        net = lanenet.LaneNet(phase='test', net_flag='vgg')
        binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor, name='lanenet_model')

        sess.as_default()
        saver = tf.train.Saver()
        saver.restore(sess=sess, save_path=weights_path)

        t_start = time.time()
        binary_seg_image, instance_seg_image = sess.run(
            [binary_seg_ret, instance_seg_ret],
            feed_dict={input_tensor: [image]}
        )
        t_cost = time.time() - t_start
        log.info('Single imgae inference cost time: {:.5f}s'.format(t_cost))

    postprocess_result = postprocessor.postprocess(
        binary_seg_result=binary_seg_image[0],
        instance_seg_result=instance_seg_image[0],
        source_image=image_vis
    )
    mask_image = postprocess_result['mask_image']

    for i in range(CFG.TRAIN.EMBEDDING_FEATS_DIMS):
        instance_seg_image[0][:, :, i] = minmax_scale(instance_seg_image[0][:, :, i])
    embedding_image = np.array(instance_seg_image[0], np.uint8)

    plt.figure('mask_image')
    plt.imshow(mask_image[:, :, (2, 1, 0)])
    plt.figure('src_image')
    plt.imshow(image_vis[:, :, (2, 1, 0)])
    plt.figure('instance_image')
    plt.imshow(embedding_image[:, :, (2, 1, 0)])
    plt.figure('binary_image')
    plt.imshow(binary_seg_image[0] * 255, cmap='gray')
    plt.show()

    cv2.imwrite('instance_mask_image.png', mask_image)
    cv2.imwrite('source_image.png', postprocess_result['source_image'])
    cv2.imwrite('binary_mask_image.png', binary_seg_image[0] * 255)

    sess.close()

    return
def test_lanenet_batch_tflite(image_list, model_path, save_dir):
    """
    测试 tflite 模型的处理速度
    """
    save_dir = ops.join(save_dir, "tflite")
    os.makedirs(save_dir, exist_ok=True)

    interpreter = tf.lite.Interpreter(model_path=model_path)

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    interpreter.allocate_tensors()

    postprocessor = lanenet_postprocess.LaneNetPostProcessor()

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    avg_time_cost = []
    for index, image_path in tqdm.tqdm(enumerate(image_list),
                                       total=len(image_list)):
        image = cv2.imread(image_path, cv2.IMREAD_COLOR)
        image_vis = image
        image = cv2.resize(image, (512, 256), interpolation=cv2.INTER_LINEAR)
        image = image / 127.5 - 1.0
        image = image.reshape(1, 256, 512, 3)
        image = image.astype((np.float32))

        t_start = time.time()
        interpreter.set_tensor(input_details[0]['index'], image)

        interpreter.invoke()

        final_binary_output = interpreter.get_tensor(
            output_details[0]['index'])
        final_embedding_output = interpreter.get_tensor(
            output_details[1]['index'])
        avg_time_cost.append(time.time() - t_start)
        """
        postprocess_result =  postprocessor.postprocess(
            binary_seg_result=final_binary_output[0],
            instance_seg_result=final_embedding_output[0],
            source_image=image_vis
        )
"""
        if index % 100 == 0:
            log.info('Mean inference time every single image: {:.5f}s'.format(
                np.mean(avg_time_cost)))
            with open(ops.join(save_dir, 'log'), 'a') as log_file:
                print('Mean inference time every single image: {:.5f}s'.format(
                    np.mean(avg_time_cost)),
                      file=log_file)
            avg_time_cost.clear()
        """
        input_image_dir = ops.split(image_path.split('clips')[1])[0][1:]
        input_image_name = ops.split(image_path)[1]
        output_image_dir = ops.join(save_dir, input_image_dir)
        os.makedirs(output_image_dir, exist_ok=True)
        output_image_path = ops.join(output_image_dir, input_image_name)
        if ops.exists(output_image_path):
            continue

        cv2.imwrite(output_image_path, postprocess_result['source_image'])
"""
    return
Beispiel #4
0
def test_lanenet(image_path, weights_path):
    """

    :param image_path:
    :param weights_path:
    :return:
    """
    assert ops.exists(image_path), '{:s} not exist'.format(image_path)

    LOG.info('Start reading image and preprocessing')
    t_start = time.time()
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    image_vis = image
    image = cv2.resize(image, (512, 256), interpolation=cv2.INTER_LINEAR)
    image = image / 127.5 - 1.0
    LOG.info('Image load complete, cost time: {:.5f}s'.format(time.time() -
                                                              t_start))

    input_tensor = tf.placeholder(dtype=tf.float32,
                                  shape=[1, 256, 512, 3],
                                  name='input_tensor')

    net = lanenet.LaneNet(phase='test', cfg=CFG)
    binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor,
                                                     name='LaneNet')

    postprocessor = lanenet_postprocess.LaneNetPostProcessor(cfg=CFG)

    # Set sess configuration
    sess_config = tf.ConfigProto()
    sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.GPU.GPU_MEMORY_FRACTION
    sess_config.gpu_options.allow_growth = CFG.GPU.TF_ALLOW_GROWTH
    sess_config.gpu_options.allocator_type = 'BFC'

    sess = tf.Session(config=sess_config)

    # define moving average version of the learned variables for eval
    with tf.variable_scope(name_or_scope='moving_avg'):
        variable_averages = tf.train.ExponentialMovingAverage(
            CFG.SOLVER.MOVING_AVE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()

    # define saver
    saver = tf.train.Saver(variables_to_restore)

    with sess.as_default():
        saver.restore(sess=sess, save_path=weights_path)

        t_start = time.time()
        loop_times = 500
        for i in range(loop_times):
            binary_seg_image, instance_seg_image = sess.run(
                [binary_seg_ret, instance_seg_ret],
                feed_dict={input_tensor: [image]})
        t_cost = time.time() - t_start
        t_cost /= loop_times
        LOG.info('Single imgae inference cost time: {:.5f}s'.format(t_cost))

        postprocess_result = postprocessor.postprocess(
            binary_seg_result=binary_seg_image[0],
            instance_seg_result=instance_seg_image[0],
            source_image=image_vis)
        mask_image = postprocess_result['mask_image']

        for i in range(CFG.MODEL.EMBEDDING_FEATS_DIMS):
            instance_seg_image[0][:, :,
                                  i] = minmax_scale(instance_seg_image[0][:, :,
                                                                          i])
        embedding_image = np.array(instance_seg_image[0], np.uint8)

        # plt.figure('mask_image')
        # plt.imshow(mask_image[:, :, (2, 1, 0)])
        # plt.figure('src_image')
        # plt.imshow(image_vis[:, :, (2, 1, 0)])
        # plt.figure('instance_image')
        # plt.imshow(embedding_image[:, :, (2, 1, 0)])
        # plt.figure('binary_image')
        # plt.imshow(binary_seg_image[0] * 255, cmap='gray')
        # plt.show()
        cv2.imwrite("mask.jpg", mask_image)
        cv2.imwrite("src.jpg", image_vis)
        cv2.imwrite("instance.jpg", embedding_image)
        cv2.imwrite("binary.jpg", binary_seg_image[0] * 255)

    sess.close()

    return