예제 #1
0
def main():
    if not exists('results'):
        os.makedirs('results')
    file_names = []
    with open(join(config.base_dir, 'val.txt')) as reader:
        lines = reader.readlines()
    for line in lines:
        file_names.append(line.rstrip().split(' ')[0])

    model = nn.build_model(training=False)
    model.load_weights(f"weights/model_{config.version}.h5", True)

    for file_name in tqdm.tqdm(file_names):
        image = cv2.imread(join(config.base_dir, config.image_dir, file_name + '.jpg'))
        image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image_np, scale, dw, dh = util.resize(image_np)
        image_np = image_np.astype(np.float32) / 255.0

        boxes, scores, _ = model.predict(image_np[np.newaxis, ...])

        boxes, scores = np.squeeze(boxes, 0), np.squeeze(scores, 0)

        boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dw) / scale
        boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dh) / scale
        image = draw_bbox(image, boxes, scores)
        cv2.imwrite(f'results/{file_name}.jpg', image)
예제 #2
0
    def test_fn():
        if not os.path.exists('results'):
            os.makedirs('results')
        file_names = []
        with open(os.path.join(config.data_dir, 'test.txt')) as f:
            for file_name in f.readlines():
                file_names.append(file_name.rstrip())

        model = nn.build_model(training=False)
        model.load_weights(f"weights/model_{config.version}.h5", True)

        for file_name in tqdm.tqdm(file_names):
            image = cv2.imread(
                os.path.join(config.data_dir, config.image_dir,
                             file_name + '.jpg'))
            image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            image_np, scale, dw, dh = util.resize(image_np)
            image_np = image_np.astype(numpy.float32) / 255.0

            boxes, scores, labels = model.predict(image_np[numpy.newaxis, ...])

            boxes, scores, labels = numpy.squeeze(boxes, 0), numpy.squeeze(
                scores, 0), numpy.squeeze(labels, 0)

            boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dw) / scale
            boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dh) / scale
            image = draw_bbox(image, boxes)
            cv2.imwrite(f'results/{file_name}.jpg', image)
예제 #3
0
    def show(self, img):
        if self.frompipeline._suppress_ui:
            return

        if img is None:
            self.logger.warning(
                'image in CVPreviewStep "%s" is None, will not show',
                self.stepname)
            self.close_preview_window()
            return

        # Resize preview image
        if self.frompipeline.force_resize_preview_w > 0:
            img = resize(img, self.frompipeline.force_resize_preview_w)
            # self._winname += '(resized)'
        is_existed = isCvWindowsExists(self._winname)
        cv2.imshow(self._winname, img)
        self._has_shown = True

        # Arrange the window... when open many windows, it gets so messy
        if not is_existed:  # Don't arrange the window if already existed (maybe user have moved it..)
            w, h = img.shape[1], img.shape[0]
            if self._win_x is None:
                self._win_x, self._win_y = self.frompipeline.preview_window_arranger.add_window(
                    self._winname, w, h)
            cv2.moveWindow(self._winname, self._win_x, self._win_y)
예제 #4
0
def main():
    nb_classes = len(config.classes)

    input_layer = tf.keras.layers.Input([config.image_size, config.image_size, 3])
    feature_maps = nn.build_model(input_layer, nb_classes)

    bbox_tensors = []
    for i, fm in enumerate(feature_maps):
        bbox_tensor = nn.decode(fm, i, nb_classes)
        bbox_tensors.append(bbox_tensor)

    model = tf.keras.Model(input_layer, bbox_tensors)
    model.load_weights("weights/model.h5")

    image = cv2.imread('../Dataset/VOC2012/IMAGES/2007_000039.jpg')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    image_size = image.shape[:2]
    image_data = util.resize(image)

    pred_bbox = model.predict(image_data[np.newaxis, ...].astype(np.float32) / 255.0)
    pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
    pred_bbox = tf.concat(pred_bbox, axis=0)
    bbox = postprocess_boxes(pred_bbox, image_size, config.image_size, 0.3)
    bbox = nms(bbox)

    image = draw_bbox(image, bbox, nb_classes)
    cv2.imwrite('result.png', image)
예제 #5
0
def main():
    model = nn.build_model(training=False)
    model.load_weights("weights/model2.h5", True)
    f_names = []
    with open(join(config.data_dir, 'val.txt')) as f:
        for f_name in f.readlines()[:100]:
            f_names.append(f_name.rstrip().split(' ')[0])
    for f_name in f_names[:1]:
        image = cv2.imread(
            join(config.data_dir, config.image_dir, f'{f_name}.jpg'))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image_data, scale, dw, dh = util.resize(image)
        image_data = image_data.astype(np.float32) / 255.0
        mean = [0.485, 0.456, 0.406]
        std = [0.229, 0.224, 0.225]
        image_data -= mean
        image_data /= std

        boxes, score, label = model.predict(image_data[np.newaxis, ...])
        boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dw) / scale
        boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dh) / scale

        image = draw_bbox(image, boxes)
        cv2.imwrite(f'results/{f_name}.png',
                    cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
예제 #6
0
    def __getitem__(self, index):
        image = util.load_image(self.file_names[index])
        boxes, label = util.load_label(self.file_names[index])
        image, boxes = util.resize(image, boxes)
        image, boxes = util.random_flip(image, boxes)

        image = image[:, :, ::-1].astype(numpy.float32)
        image = image / 255.0
        y_true_1, y_true_2, y_true_3 = util.process_box(boxes, label)
        return image, y_true_1, y_true_2, y_true_3
예제 #7
0
def GPU_Bicubic(variable, scale):
    tensor = variable.cpu().data
    B, C, H, W = tensor.size()
    H_new = int(H / scale)
    W_new = int(W / scale)
    tensor_view = tensor.view((B * C, 1, H, W))
    re_tensor = torch.zeros((B * C, 1, H_new, W_new))
    for i in range(B * C):
        img = to_pil_image(tensor_view[i])
        re_tensor[i] = to_tensor(
            resize(img, (H_new, W_new), interpolation=Image.BICUBIC))
    re_tensor_view = re_tensor.view((B, C, H_new, W_new))
    return re_tensor_view
예제 #8
0
    def __getitem__(self, index):
        image = load_image(self.file_names[index])
        boxes, label = load_label(self.file_names[index])
        boxes = np.concatenate(
            (boxes,
             np.full(
                 shape=(boxes.shape[0], 1), fill_value=1., dtype=np.float32)),
            axis=-1)

        image, boxes = resize(image, boxes)

        image = image.astype(np.float32)
        image = image / 255.0
        y_true_1, y_true_2, y_true_3 = process_box(boxes, label)
        return image, y_true_1, y_true_2, y_true_3
예제 #9
0
def write_tf_record(queue, sentinel):
    def byte_feature(value):
        if not isinstance(value, bytes):
            if not isinstance(value, list):
                value = value.encode('utf-8')
            else:
                value = [val.encode('utf-8') for val in value]
        if not isinstance(value, list):
            value = [value]
        return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))

    while True:
        file_name = queue.get()

        if file_name == sentinel:
            break
        in_image = util.load_image(file_name)[:, :, ::-1]
        boxes, label = util.load_label(file_name)

        in_image, boxes = util.resize(in_image, boxes)

        y_true_1, y_true_2, y_true_3 = util.process_box(boxes, label)

        in_image = in_image.astype('float32')
        y_true_1 = y_true_1.astype('float32')
        y_true_2 = y_true_2.astype('float32')
        y_true_3 = y_true_3.astype('float32')

        in_image = in_image.tobytes()
        y_true_1 = y_true_1.tobytes()
        y_true_2 = y_true_2.tobytes()
        y_true_3 = y_true_3.tobytes()

        features = tf.train.Features(
            feature={
                'in_image': byte_feature(in_image),
                'y_true_1': byte_feature(y_true_1),
                'y_true_2': byte_feature(y_true_2),
                'y_true_3': byte_feature(y_true_3)
            })
        tf_example = tf.train.Example(features=features)
        opt = tf.io.TFRecordOptions('GZIP')
        with tf.io.TFRecordWriter(
                os.path.join(config.data_dir, 'TF', file_name + ".tf"),
                opt) as writer:
            writer.write(tf_example.SerializeToString())
예제 #10
0
    def __getitem__(self, index):
        image = load_image(self.f_names[index])
        boxes, label = load_label(self.f_names[index])
        boxes = np.concatenate(
            (boxes,
             np.full(
                 shape=(boxes.shape[0], 1), fill_value=1., dtype=np.float32)),
            axis=-1)
        image, boxes = random_horizontal_flip(image, boxes)
        image, boxes = random_crop(image, boxes)
        image, boxes = random_translate(image, boxes)
        image, boxes = resize(image, boxes)

        image = image.astype(np.float32)
        image /= 255.
        mean = [0.485, 0.456, 0.406]
        std = [0.229, 0.224, 0.225]
        image -= mean
        image /= std
        y_true_1, y_true_2, y_true_3 = process_box(boxes, label)
        return image, y_true_1, y_true_2, y_true_3
예제 #11
0
def main():
    if not exists('results'):
        os.makedirs('results')
    f_names = []
    with open(join(config.base_dir, 'val.txt')) as reader:
        lines = reader.readlines()
    for line in lines:
        f_names.append(line.rstrip().split(' ')[0])
    result_dict = {}

    model = nn.build_model(training=False)
    model.load_weights("weights/model245.h5", True)

    for f_name in tqdm.tqdm(f_names):
        image_path = join(config.base_dir, config.image_dir, f_name + '.jpg')
        label_path = join(config.base_dir, config.label_dir, f_name + '.xml')
        image = cv2.imread(image_path)
        image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image_np, scale, dw, dh = util.resize(image_np)
        image_np = image_np.astype(np.float32) / 255.0

        boxes, scores, _ = model.predict(image_np[np.newaxis, ...])
        boxes[:, [0, 2]] = (boxes[:, [0, 2]] - dw) / scale
        boxes[:, [1, 3]] = (boxes[:, [1, 3]] - dh) / scale
        true_boxes = []
        for element in parse_fn(label_path).getroot().iter('object'):
            true_boxes.append(parse_annotation(element)[2])
        result = {
            'pred_boxes': boxes,
            'true_boxes': true_boxes,
            'confidence': scores
        }
        result_dict[f'{f_name}.jpg'] = result
        image = draw_bbox(image, boxes, scores)
        cv2.imwrite(f'results/{f_name}.png', image)
    with open(join('results/yolo.pickle'), 'wb') as writer:
        pickle.dump(result_dict, writer)
예제 #12
0
def build_example(f_name):
    import tensorflow as tf
    image = load_image(f_name)
    label = load_label(f_name)
    image, label = util.resize(image, label)
    s_label, m_label, l_label, s_boxes, m_boxes, l_boxes = preprocess(label)

    path = join(config.data_dir, 'record', f_name + '.jpg')

    util.write_image(path, image)

    s_label = s_label.astype('float32')
    m_label = m_label.astype('float32')
    l_label = l_label.astype('float32')
    s_boxes = s_boxes.astype('float32')
    m_boxes = m_boxes.astype('float32')
    l_boxes = l_boxes.astype('float32')

    s_label = s_label.tobytes()
    m_label = m_label.tobytes()
    l_label = l_label.tobytes()

    s_boxes = s_boxes.tobytes()
    m_boxes = m_boxes.tobytes()
    l_boxes = l_boxes.tobytes()

    features = tf.train.Features(
        feature={
            'path': byte_feature(path.encode('utf-8')),
            's_label': byte_feature(s_label),
            'm_label': byte_feature(m_label),
            'l_label': byte_feature(l_label),
            's_boxes': byte_feature(s_boxes),
            'm_boxes': byte_feature(m_boxes),
            'l_boxes': byte_feature(l_boxes)
        })

    return tf.train.Example(features=features)
예제 #13
0
def evaluate():
    model = torch.load(os.path.join('weights', 'model.pt'),
                       device)['model'].float().eval()

    half = device.type != 'cpu'
    if half:
        model.half()
    model.eval()

    img = torch.zeros((1, 3, config.image_size, config.image_size),
                      device=device)
    _ = model(img.half() if half else img) if device.type != 'cpu' else None

    file_names = [
        file_name[:-4] for file_name in os.listdir(
            os.path.join(config.data_dir, config.image_dir))
    ]

    for file_name in file_names:
        image = util.load_image(file_name)
        label = util.load_label(file_name)

        image, label = util.resize(image, label)

        image = torch.from_numpy(np.expand_dims(image.transpose(2, 0, 1), 0))
        image = image.to(device, non_blocking=True)
        image = image.half() if half else image.float()

        pred = model(image / 255.0)
        pred = np.array(pred.cpu().detach().numpy()[0, :, :, :].transpose(
            1, 2, 0))
        pred = util.reverse_one_hot(pred)
        pred = util.colour_code_segmentation(pred, palette)
        pred = np.uint8(pred)
        util.save_images([label, pred], ['TRUE', 'PRED'],
                         os.path.join('results', f'{file_name}.jpg'))
예제 #14
0
            k = cv2.waitKey(1) & 0xFF
            if k == ord('m'):
                # Looping modes
                mode = modes[(modes.index(mode) +1) % len(modes)]
                self.logger.info('selection mode changed to "%s"', mode)
                redraw()
            if k == ord('r'):
                reset()
            if k == ord('q'):
                break

        cv2.destroyWindow(self.name)

    def get_selected(self):
        """Return selected contours"""
        # Applying the T-F table
        l_ = list(filter(lambda i: i[0], zip(self.selected_tf_table, self.initcontour)))
        l_ = [v for e, v in l_]
        # l_ = []
        # if self.selected_tf_table
        # for i,v in enumerate(self.initcontour):
        #     if self.selected_tf_table[i]
        return l_



if __name__ == '__main__':

    test_img = util.resize(cv2.imread('../test_dataset/DSC01332.jpg'), 500)
    c = ContourSelector(test_img, [])
    c.show_and_interact()
예제 #15
0
    scores = tf.boolean_mask(box_class_scores, prediction_mask)
    _classes = tf.boolean_mask(box_classes, prediction_mask)

    _boxes = _boxes * config.image_size

    selected_idx = tf.image.non_max_suppression(_boxes, scores, config.max_boxes, iou_threshold)
    return backend.gather(_boxes, selected_idx), backend.gather(_classes, selected_idx)


if __name__ == '__main__':
    inputs = tf.keras.layers.Input((config.image_size, config.image_size, 3))
    outputs = nn.build_model(inputs, len(config.classes), False)
    model = tf.keras.models.Model(inputs, outputs)
    model.load_weights(join('weights', 'model15.h5'))
    path = '../Dataset/VOC2012/IMAGES/2007_000027.jpg'
    image = cv2.imread(path)
    image = image[:, :, ::-1]
    image = util.resize(image)
    input_image = image.astype('float32') / 127.5 - 1.0
    input_image = np.expand_dims(input_image, 0)
    boxes, classes = get_box(model.predict_on_batch(input_image), 0.45, 0.4)
    for i, box in enumerate(boxes):
        x1 = int(box[0])
        y1 = int(box[1])
        x2 = int(box[2])
        y2 = int(box[3])
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 1)
        class_name = list(config.classes.keys())[list(config.classes.values()).index(classes[i].numpy())]
        cv2.putText(image, class_name, (x1, y1 - 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, 0, 1)
    util.write_image('result.png', image)
예제 #16
0
def build_example(f_name):
    import tensorflow as tf
    image = load_image(f_name)
    label = load_label(f_name)
    image, label = util.resize(image, label)
    boxes = np.zeros((1, config.max_boxes, 5))
    boxes[0, :label.shape[0], :5] = label
    boxes = boxes[0]
    anchors = copy.deepcopy(config.anchors)
    anchors = anchors.reshape(len(anchors) // 2, 2)

    nb_anchors = len(anchors)

    grid = np.zeros(boxes.shape)
    mask = np.zeros((config.image_size // config.scale,
                     config.image_size // config.scale, nb_anchors, 1))
    true_boxes = np.zeros((config.image_size // config.scale,
                           config.image_size // config.scale, nb_anchors, 5))

    for index, box in enumerate(boxes):
        w = (box[2] - box[0]) / config.scale
        h = (box[3] - box[1]) / config.scale
        x = ((box[0] + box[2]) / 2) / config.scale
        y = ((box[1] + box[3]) / 2) / config.scale
        grid[:box.shape[0], ...] = np.array([x, y, w, h, box[4]])
        if w * h > 0:
            best_iou = 0
            best_anchor = 0
            for i in range(nb_anchors):
                intersect = np.minimum(w, anchors[i, 0]) * np.minimum(
                    h, anchors[i, 1])
                union = (anchors[i, 0] * anchors[i, 1]) + (w * h) - intersect
                iou = intersect / union
                if iou > best_iou:
                    best_iou = iou
                    best_anchor = i
            if best_iou > 0:
                x_coord = np.floor(x).astype('int')
                y_coord = np.floor(y).astype('int')
                mask[y_coord, x_coord, best_anchor] = 1
                true_boxes[y_coord, x_coord,
                           best_anchor] = np.array([x, y, w, h, box[4]])

    path = join(config.data_dir, 'record', f_name + '.jpg')

    box = true_boxes.astype('float32')
    mask = mask.astype('float32')
    grid = grid.astype('float32')

    box = box.tobytes()
    mask = mask.tobytes()
    grid = grid.tobytes()

    features = tf.train.Features(
        feature={
            'path': byte_feature(path.encode('utf-8')),
            'box': byte_feature(box),
            'mask': byte_feature(mask),
            'grid': byte_feature(grid)
        })

    return tf.train.Example(features=features), image