Exemplo n.º 1
0
def non_interactive_demo(model, args):
    renderer = create_renderer()
    show_window = not args.no_show
    for rec in tqdm(model.images_list):
        log.info("Starting inference for %s", rec['img_name'])
        image = rec['img']
        distribution, targets = model.infer_sync(image)
        prob = calculate_probability(distribution)
        log.info("Confidence score is %s", prob)
        if prob >= args.conf_thresh ** len(distribution):
            phrase = model.vocab.construct_phrase(targets)
            if args.output_file:
                with open(args.output_file, 'a') as output_file:
                    output_file.write(rec['img_name'] + '\t' + phrase + '\n')
            else:
                print("\n\tImage name: {}\n\tFormula: {}\n".format(rec['img_name'], phrase))
                if renderer is not None:
                    rendered_formula, _ = renderer.render(phrase)
                    if rendered_formula is not None and show_window:
                        cv.imshow("Predicted formula", rendered_formula)
                        cv.waitKey(0)
        else:
            log.info("Confidence score is low. The formula was not recognized.")
    if args.perf_counts:
        log.info("Encoder performance statistics")
        print_stats(model.exec_net_encoder)
        log.info("Decoder performance statistics")
        print_stats(model.exec_net_decoder)
Exemplo n.º 2
0
def calculate_class_probabilities(summaries, data):
    '''
  Helper function to calculate the probabilities of
  each class given  the  current  data  value. This 
  function helps in calculating the aposteriori for
  each class

  Parameter
  ---------
  summaries: the summaries of each feature calculated 
             while  training  the  model on the train 
             dataset

  data: A unit data vector, for which the class 
        probabilities need to be calculated

  Return
  ------
  probabilities: The  aproiori probability  for each 
                 class as calculated by the  standard 
                 formaula and independent assumptions
  '''
    total_rows = sum([summaries[label][0][2] for label in summaries])
    probabilities = dict()
    for class_value, class_summaries in summaries.items():
        probabilities[class_value] = summaries[class_value][0][2] / float(
            total_rows)
        for i in range(len(class_summaries)):
            # for each class get the class summaries and store them
            mean, stdev, _, _ = class_summaries[i]
            # multiply by the likelihood of the data
            probabilities[class_value] *= calculate_probability(
                data[i], mean, stdev)
    return probabilities
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)

    args = build_argparser().parse_args()
    interactive_mode = not (os.path.isdir(args.input)
                            or args.input.endswith('.png')
                            or args.input.endswith('.jpg'))
    model = Model(args, interactive_mode)
    if not interactive_mode:
        non_interactive_demo(model, args)
        return

    height, width = model.encoder.input_info['imgs'].input_data.shape[-2:]
    prev_text = ''
    demo = InteractiveDemo((height, width), resolution=args.resolution)
    show_window = not args.no_show
    capture = create_capture(args.input, demo.resolution)
    if not capture.isOpened():
        log.error("Cannot open camera")
        return 1
    while True:
        ret, frame = capture.read()
        if not ret:
            log.info("End of file or error reading from camera")
            break
        bin_crop = demo.get_crop(frame)
        model_input = prerocess_crop(bin_crop, (height, width),
                                     preprocess_type=args.preprocessing_type)
        frame = demo.put_crop(frame, model_input)
        model_res = model.infer_async(model_input)
        if not model_res:
            phrase = prev_text
        else:
            distribution, targets = model_res
            prob = calculate_probability(distribution)
            log.info("Confidence score is %s", prob)
            if prob >= args.conf_thresh**len(distribution):
                log.info("Prediction updated")
                phrase = model.vocab.construct_phrase(targets)
            else:
                log.info("Confidence score is low, prediction is not complete")
                phrase = ''
        frame = demo.draw(frame, phrase)
        prev_text = phrase
        if show_window:
            cv.imshow('Press q to quit.', frame)
            key = cv.waitKey(1) & 0xFF
            if key in (ord('Q'), ord('q'), ord('\x1b')):
                break
            elif key in (ord('o'), ord('O')):
                demo.resize_window("decrease")
            elif key in (ord('p'), ord('P')):
                demo.resize_window("increase")

    log.info(
        "This demo is an API example, for any performance measurements please use the dedicated benchmark_app tool "
        "from the openVINO toolkit\n")
def main():
    args = build_argparser().parse_args()
    interactive_mode = not (os.path.isdir(args.input)
                            or args.input.endswith('.png')
                            or args.input.endswith('.jpg'))
    model = Model(args, interactive_mode)
    if not interactive_mode:
        non_interactive_demo(model, args)
        return

    height, width = model.encoder.input_info['imgs'].input_data.shape[-2:]
    prev_text = ''
    demo = InteractiveDemo((height, width), resolution=args.resolution)
    show_window = not args.no_show
    capture = create_capture(args.input, demo.resolution)
    if not capture.isOpened():
        log.error("Cannot open camera")
        return 1
    while True:
        ret, frame = capture.read()
        if not ret:
            break
        bin_crop = demo.get_crop(frame)
        model_input = prerocess_crop(bin_crop, (height, width),
                                     preprocess_type=args.preprocessing_type)
        frame = demo.put_crop(frame, model_input)
        model_res = model.infer_async(model_input)
        if not model_res:
            phrase = prev_text
        else:
            distribution, targets = model_res
            prob = calculate_probability(distribution)
            log.debug("Confidence score is {}".format(prob))
            if prob >= args.conf_thresh**len(distribution):
                log.debug("Prediction updated")
                phrase = model.vocab.construct_phrase(targets)
            else:
                log.debug(
                    "Confidence score is low, prediction is not complete")
                phrase = ''
        frame = demo.draw(frame, phrase)
        prev_text = phrase
        if show_window:
            cv.imshow('Press q to quit.', frame)
            key = cv.waitKey(1) & 0xFF
            if key in (ord('Q'), ord('q'), ord('\x1b')):
                break
            elif key in (ord('o'), ord('O')):
                demo.resize_window("decrease")
            elif key in (ord('p'), ord('P')):
                demo.resize_window("increase")
def non_interactive_demo(model, args):
    renderer = create_renderer()
    show_window = not args.no_show
    for rec in tqdm(model.images_list):
        image = rec.img
        distribution, targets = model.infer_sync(image)
        prob = calculate_probability(distribution)
        log.info("Confidence score is {}".format(prob))
        if prob >= args.conf_thresh**len(distribution):
            phrase = model.vocab.construct_phrase(targets)
            if args.output_file:
                with open(args.output_file, 'a') as output_file:
                    output_file.write(rec.img_name + '\t' + phrase + '\n')
            else:
                print("\n\tImage name: {}\n\tFormula: {}\n".format(
                    rec.img_name, phrase))
                if renderer is not None:
                    rendered_formula, _ = renderer.render(phrase)
                    if rendered_formula is not None and show_window:
                        cv.imshow("Predicted formula", rendered_formula)
                        cv.waitKey(0)
        else:
            log.info(
                "Confidence score is low. The formula was not recognized.")