Example #1
0
def test_net(sess, net, imdb, weights_filename):
    timer = Timer()
    timer.tic()
    np.random.seed(cfg.RNG_SEED)
    """Test a Fast R-CNN network on an image database."""
    num_images = len(imdb.image_index)
    output_dir = get_output_dir(imdb, weights_filename)
    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    # all_boxes = []
    all_boxes = [[[] for _ in range(imdb.num_classes)]
                 for _ in range(num_images)]
    print(all_boxes)
    for i in range(num_images):
        print('***********', imdb.image_path_at(i))
        img = cv2.imread(imdb.image_path_at(i))
        img, scale = resize_im(img, scale=TextLineCfg.SCALE, max_scale=TextLineCfg.MAX_SCALE)
        scores, boxes = test_ctpn(sess, net, img)
        textdetector = TextDetector()
        boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
        print(('Detection took {:.3f}s for '
               '{:d} object proposals').format(timer.total_time, boxes.shape[0]))
        boxes = check_unreasonable_box(boxes, scale)
        all_boxes[i][1] += boxes
    det_file = os.path.join(output_dir, 'detections.pkl')
    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    imdb.evaluate_detections(all_boxes, output_dir)
    timer.toc()
Example #2
0
def test_net(sess,
             net,
             imdb,
             weights_filename,
             max_per_image=100,
             thresh=0.05):
    np.random.seed(cfg.FLAGS.rng_seed)
    """Test a Fast R-CNN network on an image database."""
    num_images = len(imdb.image_index)
    # all detections are collected into:
    #  all_boxes[cls][image] = N x 5 array of detections in
    #  (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(imdb.num_classes)]

    output_dir = get_output_dir(imdb, weights_filename)
    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}

    for i in range(num_images):
        im = cv2.imread(imdb.image_path_at(i))

        _t['im_detect'].tic()
        scores, boxes = im_detect(sess, net, im)
        _t['im_detect'].toc()

        _t['misc'].tic()

        # skip j = 0, because it's the background class
        for j in range(1, imdb.num_classes):
            inds = np.where(scores[:, j] > thresh)[0]
            cls_scores = scores[inds, j]
            cls_boxes = boxes[inds, j * 4:(j + 1) * 4]
            cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
                .astype(np.float32, copy=False)
            keep = nms(cls_dets, 0.3)
            cls_dets = cls_dets[keep, :]
            all_boxes[j][i] = cls_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack(
                [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        _t['misc'].toc()

        print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
              .format(i + 1, num_images, _t['im_detect'].average_time,
                      _t['misc'].average_time))

    det_file = os.path.join(output_dir, 'detections.pkl')
    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    imdb.evaluate_detections(all_boxes, output_dir)
Example #3
0
    def predict(self, h, w, loc, conf, threshold=0.6, check_time=False):
        # make sure the input channel is 3
        # assert img.shape[2] == 3
        # scale = torch.Tensor([img.shape[1::-1], img.shape[1::-1]])
        # scale = [img.shape[1], img.shape[0], img.shape[1], img.shape[0]]
        scale = torch.Tensor([w, h, w, h])

        _t = {
            'preprocess': Timer(),
            'net_forward': Timer(),
            'detect': Timer(),
            'output': Timer()
        }

        # preprocess image
        # _t['preprocess'].tic()
        # with torch.no_grad():
        #     x = Variable(self.preprocessor(img)[0].unsqueeze(0))
        # if self.use_gpu:
        #     x = x.cuda()
        # if self.half:
        #     x = x.half()
        # preprocess_time = _t['preprocess'].toc()

        # forward
        # _t['net_forward'].tic()
        # out = self.net.forward()  # forward pass
        # net_forward_time = _t['net_forward'].toc()

        loc = torch.Tensor(loc)
        conf = torch.Tensor(conf)
        # detect
        _t['detect'].tic()
        detections = self.detector.forward(loc, conf)
        detect_time = _t['detect'].toc()

        # output
        _t['output'].tic()
        labels, scores, coords = [list() for _ in range(3)]
        # for batch in range(detections.size(0)):
        #     print('Batch:', batch)
        batch = 0
        for classes in range(detections.size(1)):
            num = 0
            while detections[batch, classes, num, 0] >= threshold:
                scores.append(detections[batch, classes, num, 0])
                labels.append(classes - 1)
                coords.append(detections[batch, classes, num, 1:] * scale)
                num += 1
        output_time = _t['output'].toc()
        # total_time = preprocess_time + net_forward_time + detect_time + output_time

        # if check_time is True:
        #     return labels, scores, coords, (total_time, preprocess_time, net_forward_time, detect_time, output_time)
        #     # total_time = preprocess_time + net_forward_time + detect_time + output_time
        #     # print('total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
        #     #     total_time, preprocess_time, net_forward_time, detect_time, output_time
        #     # ))
        return labels, scores, coords
Example #4
0
    def predict(self, img, threshold=0.6, check_time=False):
        # make sure the input channel is 3
        assert img.shape[2] == 3
        scale = torch.Tensor([img.shape[1::-1], img.shape[1::-1]]).view(-1)
        # print(scale)
        _t = {
            'preprocess': Timer(),
            'net_forward': Timer(),
            'detect': Timer(),
            'output': Timer()
        }

        # preprocess image
        _t['preprocess'].tic()
        with torch.no_grad():
            x = Variable(self.preprocessor(img)[0].unsqueeze(0))
        if self.use_gpu:
            x = x.cuda()
        if self.half:
            x = x.half()
        preprocess_time = _t['preprocess'].toc()

        # forward
        _t['net_forward'].tic()
        out = self.model(x)  # forward pass
        net_forward_time = _t['net_forward'].toc()

        # detect
        _t['detect'].tic()
        detections = self.detector.forward(out)
        detect_time = _t['detect'].toc()

        # output
        _t['output'].tic()
        labels, scores, coords = [list() for _ in range(3)]
        # for batch in range(detections.size(0)):
        #     print('Batch:', batch)
        batch = 0
        for classes in range(detections.size(1)):
            num = 0
            while detections[batch, classes, num, 0] >= threshold:
                scores.append(detections[batch, classes, num, 0])
                labels.append(classes - 1)
                coords.append(detections[batch, classes, num, 1:] * scale)
                num += 1
        output_time = _t['output'].toc()
        total_time = preprocess_time + net_forward_time + detect_time + output_time

        if check_time is True:
            return labels, scores, coords, (total_time, preprocess_time,
                                            net_forward_time, detect_time,
                                            output_time)
            # total_time = preprocess_time + net_forward_time + detect_time + output_time
            # print('total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
            #     total_time, preprocess_time, net_forward_time, detect_time, output_time
            # ))
        return labels, scores, coords
Example #5
0
def extract_net(sess,
                net,
                imdb,
                weights_filename,
                roidb,
                max_per_image=100,
                thresh=0.05):
    #change from test_net, and this function is used to extract features from conv5 of vgg16
    np.random.seed(cfg.RNG_SEED)
    """Test a Fast R-CNN network on an image database."""
    num_images = len(imdb.image_index)
    # all detections are collected into:
    #  all_boxes[cls][image] = N x 5 array of detections in
    #  (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(imdb.num_classes)]

    output_dir = get_output_dir(imdb, weights_filename)
    print("output_dir is:", output_dir)
    print("\n")
    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}

    data_input = []
    #N_save is the number of processed images which will be saved into one file
    N_save = 100
    for i in range(1000):
        im = cv2.imread(imdb.image_path_at(i))

        _t['im_detect'].tic()
        feature_maps = im_extract(sess, net, im)

        fea = np.squeeze(feature_maps)
        data_temp = {}
        data_temp['img_path'] = imdb.image_path_at(i)
        data_temp['box'] = roidb[i]['boxes']
        data_temp['img_shape'] = np.shape(im)
        data_temp['fea'] = fea
        data_input.append(data_temp)
        if (i + 1) % N_save == 0:
            file_name_of_input = '/home/yangxu/project/rd/input/input' + format(
                int((i + 1) / N_save), '03') + '.npz'
            np.savez(file_name_of_input, data_input=data_input)
            data_input = []

        _t['im_detect'].toc()

        _t['misc'].tic()


        print('im_extract: {:d}/{:d} {:.3f}s {:.3f}s' \
            .format(i + 1, num_images, _t['im_detect'].average_time,
                _t['misc'].average_time))
        print(
            "i is: {0}, the path of image is {1}, shape of the image is {2}, the shape of the feature map is{3}\n"
            .format(i, imdb.image_path_at(i), np.shape(im), np.shape(fea)))
Example #6
0
    def test_epoch(self, model, data_loader, detector, output_dir, use_gpu):
        model.eval()

        dataset = data_loader.dataset
        num_images = len(dataset)
        num_classes = detector.num_classes
        all_boxes = [[[] for _ in range(num_images)]
                     for _ in range(num_classes)]
        empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))

        _t = Timer()

        for i in iter(range((num_images))):
            img = dataset.pull_image(i)
            scale = [img.shape[1], img.shape[0], img.shape[1], img.shape[0]]
            with torch.no_grad():
                images = torch.Tensor(
                    dataset.preproc(img)[0].unsqueeze(0).to(self.device)).to(
                        self.device)

            _t.tic()
            # forward
            out = model(images, phase='eval')

            # detect
            detections = detector.forward(out)

            time = _t.toc()

            # TODO: make it smart:
            for j in range(1, num_classes):
                cls_dets = list()
                for det in detections[0][j]:
                    if det[0] > 0:
                        d = det.cpu().numpy()
                        score, box = d[0], d[1:]
                        box *= scale
                        box = np.append(box, score)
                        cls_dets.append(box)
                if len(cls_dets) == 0:
                    cls_dets = empty_array
                all_boxes[j][i] = np.array(cls_dets)

            # log per iter
            log = '{iters:d}/{epoch_size:d} in {time:.3f}s [{prograss}]\r'.format(
                prograss='#' * int(round(10 * i / num_images)) +
                '-' * int(round(10 * (1 - i / num_images))),
                iters=i,
                epoch_size=num_images,
                time=time)
            sys.stdout.write(log)
            sys.stdout.flush()

        # write result to pkl
        with open(os.path.join(output_dir, 'detections.pkl'), 'wb') as f:
            pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

        # currently the COCO dataset do not return the mean ap or ap 0.5:0.95 values
        print('Evaluating detections')
        data_loader.dataset.evaluate_detections(all_boxes, output_dir)
Example #7
0
def demo(net, matlab, image_filepath, classes, method, par1, par2):
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    # Load pre-computed Selected Search object proposals
    obj_proposals = ROI_boxes(matlab, image_filepath, method, par1, par2)
    global OP_num
    OP_num = len(obj_proposals)
    if len(obj_proposals)==0:
        dets = []
        timer.toc()
        return dets, timer.total_time

    # Load the demo image
    im = cv2.imread(image_filepath)
    scores, boxes = im_detect(net, im, obj_proposals)
    timer.toc()

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls in classes:
        cls_ind = CLASSES.index(cls)
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        keep = np.where(cls_scores >= CONF_THRESH)[0]
        cls_boxes = cls_boxes[keep, :]
        cls_scores = cls_scores[keep]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
    return dets, timer.total_time
Example #8
0
def ctpn(sess, net, image_name):
    timer = Timer()
    timer.tic()

    img = cv2.imread(image_name)
    height, width = img.shape[:2]
    img = img[int(2 * height / 3.0):height, :]
    img, scale = resize_im(img,
                           scale=TextLineCfg.SCALE,
                           max_scale=TextLineCfg.MAX_SCALE)
    scores, boxes = test_ctpn(sess, net, img)
    # for box in boxes:
    #     color = (0, 255, 0)
    #     cv2.line(img, (int(box[0]), int(box[1])), (int(box[2]), int(box[1])), color, 2)
    #     cv2.line(img, (int(box[0]), int(box[1])), (int(box[0]), int(box[3])), color, 2)
    #     cv2.line(img, (int(box[2]), int(box[1])), (int(box[2]), int(box[3])), color, 2)
    #     cv2.line(img, (int(box[0]), int(box[3])), (int(box[2]), int(box[3])), color, 2)
    # base_name = image_name.split('/')[-1]
    # cv2.imwrite("data/results/test_"+base_name, img)
    # draw_boxes(img, image_name, boxes, scale)
    # print(boxes)
    # assert 0
    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(
        'G:\DeepLearning\Project\LJProject\Faster-RCNN\Faster-RCNN-TensorFlow-Python3-master-NEU\data\demo',
        image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    # 此处的boxes是经过bbox_pre修正过的Bbox的位置坐标,并且对于预测的每一个类别,都有一个预测的Bbox坐标
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(
        timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.1
    #对每个类别进行一次画图
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        #利用非极大值抑制,从300个proposal中剔除掉与更大得分的proposal的IOU大于0.1的proposal
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Example #10
0
def SignalImage_Test(sess, net,image_path):

    im=cv2.imread(image_path)
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.4
    NMS_THRESH = 0.35


    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]


        # print('\nboxes:',boxes)
        # print('\ncls_boxes:',cls_boxes)
        # print('\n ',boxes.shape)
        # print(len(cls_boxes),len(boxes))]


        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Example #11
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = cv2.imread(image_name)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for '
          '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')

    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, ax, thresh=CONF_THRESH)
Example #12
0
    def detect(self, image):
        """Detect object classes in an image using pre-computed object proposals."""

        # Load the demo image
        # Detect all object classes and regress object bounds
        image = image_transform_1_3(image)
        timer = Timer()
        timer.tic()
        scores, boxes = self.im_detect(image)
        timer.toc()
        print('kkk', np.argmax(scores, axis=1))
        print('lll', scores[np.argmax(scores, axis=1) == 4, 4])
        print('Detection took {:.3f}s for '
              '{:d} object proposals'.format(timer.total_time, boxes.shape[0]))

        CONF_THRESH = 0.3
        NMS_THRESH = 0.5
        dets_list = []
        for cls_ind, cls in enumerate(self.classes_detect[1:]):
            inds = np.where(scores[:, cls_ind] > CONF_THRESH)[0]
            cls_ind += 1  # because we skipped background
            cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack(
                (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets[inds, :], NMS_THRESH)
            dets = dets[keep, :]
            inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
            cls_ind_list = np.empty((len(inds), 1), np.int32)
            cls_ind_list.fill(cls_ind)
            dets = np.hstack((dets[inds, :-1], cls_ind_list))
            dets_list.append(dets)
        dets = np.vstack(dets_list)
        print('jjj', dets)
        return dets
Example #13
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    # 使用已经训练好的网络模型检测当前图片中所有的物体,得到所有predict boxes
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(
        timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        """
        对于每个类,找到对应的predict boxes的概率得分和坐标描述,先进行nms缩减相近的boxes,对于保留的boxes,当概率得分大于CONF_THRESH
        阈值时,通过vis_detections函数将box画出来。
        """
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Example #14
0
    def train_model(self, max_iters, snapshot_iters):
        """
        Train the model with max_iters.
        :return saved model paths
        """
        last_snapshot_iter = -1
        timer = Timer()
        model_paths = []

        print "Begin training the model."
        while self._solver.iter < max_iters:
            timer.tic()
            self._solver.step(1)
            timer.toc()

            # print the speed
            if self._solver.iter % 1000 == 0:
                print 'speed: {:.3f}s / iter.'.format(timer.average_time)
            # snapshot the weights
            if self._solver.iter % snapshot_iters == 0:
                last_snapshot_iter = self._solver.iter
                model_paths.append(self.snapshot())

        if last_snapshot_iter != self._solver.iter:
            model_paths.append(self.snapshot())

        return model_paths
Example #15
0
    def process_frame(self, video_name, im_name, CLASSES, CONF_THRESH):
        # Output frame path
        im_path_ = os.path.join(api_config.upload_folder,
                                video_name.split(".")[0],
                                "annotated-frames", os.path.basename(im_name))
        im = np.array(Image.open(im_name))
        im = im[:, :, ::-1]
        timer = Timer()
        timer.tic()
        scores, boxes = im_detect(self.sess, self.net, im)
        timer.toc()
        print ('Detection took {:.3f}s for '
               '{:d} object proposals').format(timer.total_time,
                                               boxes.shape[0])

        NMS_THRESH = 0.3
        im = im[:, :, (2, 1, 0)]
        fig, ax = plt.subplots(figsize=(12, 12))
        ax.imshow(im, aspect='equal')
        self.annotation = xml_setup(im_name, im.shape)
        for cls_ind, cls in enumerate(CLASSES[1:]):
            cls_ind += 1  # because we skipped background
            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack((cls_boxes,
                              cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets, NMS_THRESH)
            dets = dets[keep, :]
            self.draw(im_path_, cls, dets, ax, thresh=CONF_THRESH)
        xml_write(video_name, os.path.basename(im_name), self.annotation)
        plt.savefig(im_path_, bbox_inches='tight')
        plt.close()
    def setup(self, bottom, top):
        # print ('in hdp setup')
        if len(bottom) != 4:
            raise Exception(
                "Requires four blobs: original coordinate, KNN index, blob that was used for KNN reference (second image blob), and the blob for number of coords"
            )

        # Save pair coordinates for backward
        self._pair_coords = []
        layer_params = yaml.load(self.param_str)

        self._downsampling_factor = layer_params['down_sampling_factor']
        self.negative_type = HardNegativeStr2Enum[
            layer_params['negative_type']]  # ['

        self._pck_radii = cfg.PCK_RADII

        # Generate random negatives
        # self._sample = False
        self._timer = Timer()

        # Set the correspondence coord output
        top[0].reshape(1)
        # Set the PCK output
        top[1].reshape(len(self._pck_radii))
        top[2].reshape(1)
        top[3].reshape(1)
        top[4].reshape(1)
        self._return_all_pcks = False
Example #17
0
 def __init__(self, name):
     assert name in DATASETS.keys(), \
         'Unknown dataset name: {}'.format(name)
     assert os.path.exists(DATASETS[name][IM_DIR]), \
         'Image directory \'{}\' not found'.format(DATASETS[name][IM_DIR])
     assert os.path.exists(DATASETS[name][ANN_FN]), \
         'Annotation file \'{}\' not found'.format(DATASETS[name][ANN_FN])
     logger.debug('Creating: {}'.format(name))
     self.name = name
     self.image_directory = DATASETS[name][IM_DIR]
     self.image_prefix = ('' if IM_PREFIX not in DATASETS[name] else
                          DATASETS[name][IM_PREFIX])
     self.COCO = COCO(DATASETS[name][ANN_FN])
     self.debug_timer = Timer()
     # Set up dataset classes
     category_ids = self.COCO.getCatIds()
     categories = [c['name'] for c in self.COCO.loadCats(category_ids)]
     self.category_to_id_map = dict(zip(categories, category_ids))
     self.classes = ['__background__'] + categories
     self.num_classes = len(self.classes)
     self.json_category_id_to_contiguous_id = {
         v: i + 1
         for i, v in enumerate(self.COCO.getCatIds())
     }
     self.contiguous_category_id_to_json_id = {
         v: k
         for k, v in self.json_category_id_to_contiguous_id.items()
     }
     self._init_keypoints()
def ctpn(sess,
         net,
         image_name,
         dst,
         draw_img=False,
         show_area=False,
         area_min=-0.1,
         area_max=1.1):
    timer = Timer()
    timer.tic()

    img = cv2.imread(image_name)
    img, scale = resize_im(img,
                           scale=TextLineCfg.SCALE,
                           max_scale=TextLineCfg.MAX_SCALE)
    scores, boxes = test_ctpn(sess, net, img)

    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    ret = draw_boxes(img,
                     image_name,
                     boxes,
                     scale,
                     dst,
                     draw_img=draw_img,
                     show_area=show_area,
                     area_min=area_min,
                     area_max=area_max)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))

    return ret
    def train_predict(self, data):
        """It handles the training of the model. """
        self.validate_input()  # Validates the input
        self.__timer = Timer()  # Start a timer

        print("Training process has started for experiment: {}".format(self.__title))
        X_train, X_test, y_train, y_test = prepare_data(data, self.__method)
        self.__model.fit(X_train, y_train)

        if self.__kind == "FeatureSelection_RF":
            print("Final features in selection: {}".format(
                str(self.__model.named_steps["feature_selection"].get_support().shape)))

        print("Performing predictions for experiment: {}".format(self.__title))
        y_pred_train = self.__model.predict(X_train)
        y_pred_test = self.__model.predict(X_test)

        print("Calculating metrics for experiment: {}.".format(self.__title))

        metrics_report_train, class_report_train, conf_report_train = produce_detailed_report(y_train, y_pred_train)
        metrics_report_test, class_report_test, conf_report_test = produce_detailed_report(y_test, y_pred_test)

        report = "Title: " + self.__title + "\n\n" + "Results of Training Set\n" + "\n--------------------\n" + \
                 str(metrics_report_train) + "\n\n" + str(class_report_train) + "\n\n" + str(conf_report_train) + \
                 "\n\nResults of Test Set\n" + "\n--------------------\n" + \
                 str(metrics_report_test) + "\n\n" + str(class_report_test) + "\n\n" + str(conf_report_test) + \
                 "\n\nTotal training time: " + str(round(self.__timer.get_time(), 2)) + " secs or " \
                 + str(round(self.__timer.get_time() / 60, 2)) + " mins"

        print("Metrics for experiment have been stored to file: {}".format(self.__filepath))
        store_report("experiments/" + self.__filepath, report)
Example #20
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    #im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name)
    im_file = os.path.join(path1, image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(
        timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.5
    NMS_THRESH = 0.1
    thresh = CONF_THRESH

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')

    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        #vis_detections(im, cls, dets, thresh=CONF_THRESH)
        inds = np.where(dets[:, -1] >= thresh)[0]
        if len(inds) == 0:
            continue
        for i in inds:
            bbox = dets[i, :4]
            score = dets[i, -1]

            ax.add_patch(
                plt.Rectangle((bbox[0], bbox[1]),
                              bbox[2] - bbox[0],
                              bbox[3] - bbox[1],
                              fill=False,
                              edgecolor='red',
                              linewidth=3.5))
            ax.text(bbox[0],
                    bbox[1] - 2,
                    '{:s} {:.3f}'.format(cls, score),
                    bbox=dict(facecolor='blue', alpha=0.5),
                    fontsize=14,
                    color='white')

    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    os.chdir(path2)
    plt.savefig(im_name)
Example #21
0
def test_net_on_dataset(args,
                        dataset_name,
                        proposal_file,
                        output_dir,
                        multi_gpu=False,
                        gpu_id=0):
    """Run inference on a dataset."""
    dataset = JsonDataset(dataset_name)
    test_timer = Timer()
    test_timer.tic()
    if multi_gpu:
        num_images = len(dataset.get_roidb())
        all_boxes, all_segms, all_keyps = multi_gpu_test_net_on_dataset(
            args, dataset_name, proposal_file, num_images, output_dir)
    else:
        all_boxes, all_segms, all_keyps = test_net(args,
                                                   dataset_name,
                                                   proposal_file,
                                                   output_dir,
                                                   gpu_id=gpu_id)
    test_timer.toc()
    logger.info('Total inference time: {:.3f}s'.format(
        test_timer.average_time))
    results = task_evaluation.evaluate_all(dataset, all_boxes, all_segms,
                                           all_keyps, output_dir)
    return results
Example #22
0
    def detect(self, image):
        """Detect object classes in an image using pre-computed object proposals."""

        # Load the demo image
        # Detect all object classes and regress object bounds
        image = image_transform_1_3(image)
        timer = Timer()
        timer.tic()
        scores, boxes = self.im_detect(image)
        timer.toc()
        # print('rois--------------', scores)
        print('Detection took {:.3f}s for '
              '{:d} object proposals'.format(timer.total_time, boxes.shape[0]))

        CONF_THRESH = 0.7
        NMS_THRESH = 0.1
        for cls_ind, cls in enumerate(self.classes_detect[1:]):
            cls_ind += 1  # because we skipped background
            cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack(
                (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets, NMS_THRESH)
            dets = dets[keep, :]
            inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
            dets = dets[inds, :]
        return dets
Example #23
0
def ctpn(sess, net, image_name):
    timer = Timer()
    timer.tic()

    img = cv2.imread(image_name)
    img, scale = resize_im(img,
                           scale=TextLineCfg.SCALE,
                           max_scale=TextLineCfg.MAX_SCALE)

    #将OPENCV图像转换为PIL图像,
    pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    #求图片清晰度
    imageVar = cv2.Laplacian(img, cv2.CV_64F).var()
    if imageVar <= 5000:
        pil_img = ImageEnhance.Sharpness(pil_img).enhance(3.0)
    #将PIL图像转换为opencv图像
    img = cv2.cvtColor(np.asarray(pil_img), cv2.COLOR_RGB2BGR)

    scores, boxes = test_ctpn(sess, net, img)

    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))
Example #24
0
def demo(sess, net, image_name, thresh=0.05):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    image = PIL.Image.open(image_name)
    im = cv2.imread(image_name)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()

    im_num = os.path.split(image_name)[1].split('.')[0]
    scores, boxes = im_detect(sess,
                              net,
                              im,
                              save_feature=True,
                              feature_path='./data/conv.npy')
    timer.toc()
    print('Detection took {:.3f}s for '
          '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    im = im[:, :, (2, 1, 0)]
    # fig, ax = plt.subplots(figsize=(12, 12))
    # ax.imshow(im, aspect='equal')

    CONF_THRESH = 0.7
    NMS_THRESH = 0.3
    results = []
    name = image_name.split('/')[-1]
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        cls_lables = np.full_like(cls_scores, cls_ind)
        dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis],
                          cls_lables[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        inds = np.where(dets[:, -2] > thresh)[0]
        dets = dets[inds]
        for i in range(dets.shape[0]):
            name = str(name)
            category = int(dets[i, -1])
            bbox = list(map(float, dets[i, :4]))
            bbox = [round(b, 2) for b in bbox]
            score = float(dets[i, -2])
            dic = collections.OrderedDict()
            dic['name'] = str(name)
            dic['category'] = int(category)
            dic['bbox'] = bbox
            dic['score'] = float(score)
            results.append(dic)
        im = vis_detections(image, cls, dets, ax=None, thresh=CONF_THRESH)

    out_path = './data/detection_result'
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    out_path = os.path.join(out_path, os.path.split(image_name)[-1])
    image.save(out_path)
Example #25
0
 def __init__(self, epoch_count, one_batch_count, pattern):
     self.total_count = one_batch_count
     self.current_index = 0
     self.current_epoch = 1
     self.epoch_count = epoch_count
     self.train_timer = Timer()
     self.pattern = pattern
Example #26
0
def boxdetect(sess, net, im_file, output_path):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the image
    im_file = im_file.replace('\\', '/')
    im = cv2.imread(im_file)
    image_name = im_file.split(r'/')[-1]
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(
        timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.1
    geetcode_bbox = []
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        bbox = vis_detections(im,
                              cls,
                              dets,
                              image_name,
                              output_path,
                              thresh=CONF_THRESH)
        geetcode_bbox.append(bbox)
    return geetcode_bbox
Example #27
0
    def __init__(self, image_dir, anno_file):
        #pass
        if logger:
            # logger.info('Creating: {}'.format(name))
            logger.info('creating......image_oath:{},anno_file:{}'.format(
                image_dir, anno_file))
        self.image_directory = image_dir
        self.image_prefix = ''
        self.COCO = COCO(anno_file)
        self.debug_timer = Timer()
        #Set up dataset classes
        category_ids = self.COCO.getCatIds()
        categories = [c['name'] for c in self.COCO.loadCats(category_ids)]

        self.category_to_id_map = dict(zip(categories, category_ids))
        self.classes = ['__background__'] + categories
        self.num_classes = len(self.classes)

        self.json_category_id_to_contiguous_id = {
            v: i + 1
            for i, v in enumerate(self.COCO.getCatIds())
        }
        self.contiguous_category_id_to_json_id = {
            v: k
            for k, v in self.json_category_id_to_contiguous_id.items()
        }
Example #28
0
def demo(sess, net, image_name):
    # 根据路径,使用opencv读取图片数据
    im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name)
    im = cv2.imread(im_file)

    # 进行目标检查
    timer = Timer()
    timer.tic()
    # 进行预测返回300个box的得分和位置
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(
        timer.total_time, boxes.shape[0]))

    # 每个类最高得分上图的阈值
    CONF_THRESH = 0.1
    # 每个类NMS阈值
    NMS_THRESH = 0.1
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # +1需要跳过背景
        # 获取到所有候选框对应这个分类的位置
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        # 获取到所有候选框对应这个分类的得分
        cls_scores = scores[:, cls_ind]
        # 合并所有的位置和得分,(x1,y1,x2,y2,score)
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        # 通过非极大值抑制保留0.1的候选框以及得分
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        # 上图
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Example #29
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.1
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Example #30
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = readimage(image_name)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    # print('rois--------------', scores)
    print('Detection took {:.3f}s for '
          '{:d} object proposals'.format(timer.total_time, boxes.shape[0]))

    CONF_THRESH = 0.7
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis(im, image_name, cls, dets, thresh=CONF_THRESH)