示例#1
0
    def predict(self, img):

        rgb_origin = img
        img_numpy = img

        img = torch.from_numpy(img.copy()).float()
        img = img.cuda()

        img_h, img_w = img.shape[0], img.shape[1]
        img_trans = FastBaseTransform()(img.unsqueeze(0))

        net_outs = self.net(img_trans)
        nms_outs = NMS(net_outs, 0)

        results = after_nms(nms_outs,
                            img_h,
                            img_w,
                            crop_masks=not self.args.no_crop,
                            visual_thre=self.args.visual_thre)
        torch.cuda.synchronize()

        temp = self.time_here
        self.time_here = time.time()

        self.frame_times.add(self.time_here - temp)
        fps = 1 / self.frame_times.get_avg()

        frame_numpy = draw_img(results,
                               img,
                               self.args,
                               class_color=True,
                               fps=fps)

        return frame_numpy
示例#2
0
def evaluate(net,
             dataset,
             max_num=-1,
             during_training=False,
             cocoapi=False,
             traditional_nms=False):
    frame_times = MovingAverage()
    dataset_size = len(dataset) if max_num < 0 else min(max_num, len(dataset))
    dataset_indices = list(range(len(dataset)))
    dataset_indices = dataset_indices[:dataset_size]
    progress_bar = ProgressBar(40, dataset_size)

    # For each class and iou, stores tuples (score, isPositive)
    # Index ap_data[type][iouIdx][classIdx]
    ap_data = {
        'box': [[APDataObject() for _ in cfg.dataset.class_names]
                for _ in iou_thresholds],
        'mask': [[APDataObject() for _ in cfg.dataset.class_names]
                 for _ in iou_thresholds]
    }
    make_json = Make_json()

    for i, image_idx in enumerate(dataset_indices):
        timer.reset()

        with timer.env('Data loading'):
            img, gt, gt_masks, h, w, num_crowd = dataset.pull_item(image_idx)

            batch = img.unsqueeze(0)
            if cuda:
                batch = batch.cuda()

        with timer.env('Network forward'):
            #changed
            net_outs = net(batch)
            nms_outs = NMS(net_outs, traditional_nms)
            prep_metrics(ap_data, nms_outs, gt, gt_masks, h, w, num_crowd,
                         dataset.ids[image_idx], make_json, cocoapi)

        # First couple of images take longer because we're constructing the graph.
        # Since that's technically initialization, don't include those in the FPS calculations.
        fps = 0
        if i > 1 and not during_training:
            frame_times.add(timer.total_time())
            fps = 1 / frame_times.get_avg()

        progress = (i + 1) / dataset_size * 100
        progress_bar.set_val(i + 1)
        print('\rProcessing:  %s  %d / %d (%.2f%%)  %.2f fps  ' %
              (repr(progress_bar), i + 1, dataset_size, progress, fps),
              end='')
    else:
        table, box_row, mask_row = calc_map(ap_data)
        print(table)
        return table, box_row, mask_row
示例#3
0
    if cuda:
        net = net.cuda()

    # detect images
    if args.image is not None:
        images = glob.glob(args.image + '/*.jpg')
        num = len(images)

        for i, one_img in enumerate(images):
            img_name = one_img.split('/')[-1]
            img_origin = torch.from_numpy(cv2.imread(one_img)).cuda().float()
            img_h, img_w = img_origin.shape[0], img_origin.shape[1]
            img_trans = FastBaseTransform()(img_origin.unsqueeze(0))
            net_outs = net(img_trans)
            nms_outs = NMS(net_outs, args.traditional_nms)

            show_lincomb = bool(args.show_lincomb and args.image_path)
            with timer.env('after nms'):
                results = after_nms(nms_outs,
                                    img_h,
                                    img_w,
                                    show_lincomb=show_lincomb,
                                    crop_masks=not args.no_crop,
                                    visual_thre=args.visual_thre,
                                    img_name=img_name)

                torch.cuda.synchronize()

            img_numpy = draw_img(results, img_origin, args)
示例#4
0
def process():
    try:
        destFile = ""
        if request.method == 'POST':
            file = request.files['file']
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                destFile = os.path.join(app.config['UPLOAD_FOLDER'], filename)
                file.save(destFile)
                app.logger.warning('filename=(%s)', filename)
        else:
            app.logger.warning("Request dictionary data: {}".format(request.data))
            app.logger.warning("Request dictionary form: {}".format(request.form))
            url = request.form["url"]
            print("url:", url)
            # download file
            destFile = download_file(url)

        # app.logger.error('An error occurred')
        app.logger.warning('destFile=(%s)', destFile)

        img_name = destFile.split('/')[-1]
        app.logger.warning('img_name=(%s)', img_name)

        # img_origin = cv2.imread(one_img)
        # img_tensor = torch.from_numpy(img_origin).float()

        img_origin = cv2.imread(destFile)
	#		torch.from_numpy(img_origin).float()
        img_tensor = torch.from_numpy(img_origin).float()
        if cuda:
            # img_origin = img_origin.cuda()
            img_tensor = img_tensor.cuda()

        img_h, img_w = img_tensor.shape[0], img_tensor.shape[1]
        img_trans = FastBaseTransform()(img_tensor.unsqueeze(0))

        net_outs = net(img_trans)
        nms_outs = NMS(net_outs, args.traditional_nms)

        show_lincomb = bool(args.show_lincomb)
        results = after_nms(nms_outs, img_h, img_w, show_lincomb=show_lincomb, crop_masks=not args.no_crop,
                                visual_thre=args.visual_thre, img_name=img_name)

        # img_h, img_w = img_origin.shape[0], img_origin.shape[1]
        # img_trans = FastBaseTransform()(img_origin.unsqueeze(0))
        # net_outs = net(img_trans)
        # nms_outs = NMS(net_outs, args.traditional_nms)

        app.logger.warning('img_h=(%s)', img_h)
        app.logger.warning('img_w=(%s)', img_w)

        app.logger.warning('cuda=(%s)', cuda)
        app.logger.warning('args.show_lincomb=(%s)', args.show_lincomb)
        app.logger.warning('args.no_crop=(%s)', args.no_crop)
        app.logger.warning('args.visual_thre=(%s)', args.visual_thre)
        app.logger.warning('args=(%s)', args)

        # show_lincomb = bool(args.show_lincomb)
        with timer.env('after nms'):
            results = after_nms(nms_outs, img_h, img_w, show_lincomb=show_lincomb, crop_masks=not args.no_crop,
                                visual_thre=args.visual_thre, img_name=img_name)
            if cuda:
                torch.cuda.synchronize()

        # app.logger.warning('results=(%s)', results)
        # img_numpy = draw_img(results, img_origin, args)
        img_numpy = draw_img(results, img_origin, img_name, args)

        cv2.imwrite(f'results/images/{img_name}', img_numpy)
        # print(f'\r{i + 1}/{num}', end='')

        try:
            im = Image.open(f'results/images/{img_name}')
            # im = Image.open(destFile)
            io = BytesIO()
            im.save(io, format='JPEG')
            return Response(io.getvalue(), mimetype='image/jpeg')

        except IOError:
            abort(404)

        # return send_from_directory('.', filename), 200
        callback = json.dumps({"results": results})
        return callback, 200

    except:
        traceback.print_exc()
        return {'message': 'input error'}, 400
示例#5
0
def evaluate(net,
             dataset,
             max_num=-1,
             during_training=False,
             benchmark=False,
             cocoapi=False,
             traditional_nms=False):
    frame_times = MovingAverage()
    dataset_size = len(dataset) if max_num < 0 else min(max_num, len(dataset))
    dataset_indices = list(range(len(dataset)))
    dataset_indices = dataset_indices[:dataset_size]
    progress_bar = ProgressBar(40, dataset_size)

    if benchmark:
        timer.disable('Data loading')
    else:
        # For each class and iou, stores tuples (score, isPositive)
        # Index ap_data[type][iouIdx][classIdx]
        ap_data = {
            'box': [[APDataObject() for _ in cfg.dataset.class_names]
                    for _ in iou_thresholds],
            'mask': [[APDataObject() for _ in cfg.dataset.class_names]
                     for _ in iou_thresholds]
        }
        make_json = Make_json()

    for i, image_idx in enumerate(dataset_indices):
        timer.reset()

        with timer.env('Data loading'):
            img, gt, gt_masks, h, w, num_crowd = dataset.pull_item(image_idx)

            batch = Variable(img.unsqueeze(0))
            if cuda:
                batch = batch.cuda()

        with timer.env('Network forward'):
            net_outs = net(batch)
            nms_outs = NMS(net_outs, traditional_nms)

        if benchmark:
            prep_benchmark(nms_outs, h, w)
        else:
            prep_metrics(ap_data, nms_outs, gt, gt_masks, h, w, num_crowd,
                         dataset.ids[image_idx], make_json, cocoapi)

        # First couple of images take longer because we're constructing the graph.
        # Since that's technically initialization, don't include those in the FPS calculations.
        fps = 0
        if i > 1 and not during_training:
            frame_times.add(timer.total_time())
            fps = 1 / frame_times.get_avg()

        progress = (i + 1) / dataset_size * 100
        progress_bar.set_val(i + 1)
        print('\rProcessing:  %s  %d / %d (%.2f%%)  %.2f fps  ' %
              (repr(progress_bar), i + 1, dataset_size, progress, fps),
              end='')

    if benchmark:
        print('\n\nStats for the last frame:')
        timer.print_stats()
        avg_seconds = frame_times.get_avg()
        print('Average: %5.2f fps, %5.2f ms' %
              (1 / frame_times.get_avg(), 1000 * avg_seconds))

    else:
        if cocoapi:
            make_json.dump()
            print(
                f'\nJson files dumped, saved in: {json_path}, start evaluting.'
            )

            gt_annotations = COCO(cfg.dataset.valid_info)
            bbox_dets = gt_annotations.loadRes(
                f'{json_path}/bbox_detections.json')
            mask_dets = gt_annotations.loadRes(
                f'{json_path}/mask_detections.json')

            print('\nEvaluating BBoxes:')
            bbox_eval = COCOeval(gt_annotations, bbox_dets, 'bbox')
            bbox_eval.evaluate()
            bbox_eval.accumulate()
            bbox_eval.summarize()

            print('\nEvaluating Masks:')
            bbox_eval = COCOeval(gt_annotations, mask_dets, 'segm')
            bbox_eval.evaluate()
            bbox_eval.accumulate()
            bbox_eval.summarize()
            return

        table, mask_row = calc_map(ap_data)
        print(table)
        return table, mask_row