pascal_finetune_dataset_save

import os
import numpy as np
import cv2
import pickle

data_dir = r"D:\dataset\rank_saliency_battle\PASCAL-S\train\train_gt"
img_dir = r"D:\dataset\rank_saliency_battle\PASCAL-S\train\train_imgs"
seg_maps_dir = r"D:\dataset\rank_saliency_battle\PASCAL-S\seg_masks"
gt_dir = r"D:\dataset\rank_saliency_battle\PASCAL-S\train\train_gt"

output_dir = r"D:\dataset\rank_saliency_battle\PASCAL-S"

def get_box(im):
    im_x = np.sum(im, axis=0)
    im_y = np.sum(im, axis=1)
    x1, y1, x2, y2 = 0, 0, len(im_x)-1, len(im_y)-1
    for i in range(len(im_x)):
        if im_x[i] == 0:
            x1 += 1
        else:
            break
    for i in range(len(im_x)-1, 0, -1):
        if im_x[i] == 0:
            x2 -= 1
        else:
            break
    for i in range(len(im_y)):
        if im_y[i] == 0:
            y1 += 1
        else:
            break
    for i in range(len(im_y)-1, 0, -1):
        if im_y[i] == 0:
            y2 -= 1
        else:
            break

    w = x2 - x1
    h = y2 - y1

    return x1, y1, w, h


def get_rank_box_seg(gt, gt_seg_map):
    img_w, img_h = gt.shape
    gt = gt.astype(np.float)
    gt_seg_map = gt_seg_map.astype(np.float)
    unique_mask = gt * (1 + gt_seg_map)
    instances = np.unique(unique_mask)[1:]
    np.random.shuffle(instances)

    gt_ranks = np.zeros(len(instances))
    boxes = np.zeros([len(instances), 4])
    segs = np.zeros([len(instances), img_w, img_h])
    for j, instance in enumerate(instances):
        instance_mask = unique_mask == instance
        rank = np.unique(gt[instance_mask])
        assert len(rank) == 1
        gt_ranks[j] = rank
        segs[j][instance_mask] = 1
        x, y, w, h = get_box(segs[j])
        boxes[j, :] = x, y, w, h

    return gt_ranks, boxes, segs


if __name__ == '__main__':
    files = os.listdir(data_dir)
    im_images = {}
    gt_boxes = {}
    gt_segs = {}
    gt_ranks = {}

    for j, file in enumerate(files):
        print(j, file)
        img = cv2.imread(os.path.join(img_dir, file[:-3] + 'jpg'))
        gt_seg_map = cv2.imread(os.path.join(seg_maps_dir, file[:-3] + 'png'))[:, :, 0]
        gt = cv2.imread(os.path.join(gt_dir, file[:-3] + 'png'))[:, :, 0]
        if img is None:
            print(file)
            print(data_dir)
            raise FileNotFoundError

        ranks, boxes, segs = get_rank_box_seg(gt, gt_seg_map)
        im_images[file[:-4]] = img
        gt_boxes[file[:-4]] = boxes
        gt_segs[file[:-4]] = segs
        gt_ranks[file[:-4]] = ranks

    data_set = {}
    data_set['train_imgs'] = im_images
    data_set['train_boxes'] = gt_boxes
    data_set['train_segs'] = gt_segs
    data_set['train_ranks'] = gt_ranks

    with open(os.path.join(output_dir, "pascal_dataset_finetune.pkl"), "wb") as f:
        pickle.dump(data_set, f)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值