`Berkeley Segmentation Data Set and Benchmarks 500 dataset `_. Produce ``(image, label)`` pair, where ``image`` has shape (321, 481, 3(BGR)) and ranges in [0,255]. ``Label`` is a floating
| 18 | |
| 19 | |
| 20 | class BSDS500(RNGDataFlow): |
| 21 | """ |
| 22 | `Berkeley Segmentation Data Set and Benchmarks 500 dataset |
| 23 | <http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/resources.html#bsds500>`_. |
| 24 | |
| 25 | Produce ``(image, label)`` pair, where ``image`` has shape (321, 481, 3(BGR)) and |
| 26 | ranges in [0,255]. |
| 27 | ``Label`` is a floating point image of shape (321, 481) in range [0, 1]. |
| 28 | The value of each pixel is ``number of times it is annotated as edge / total number of annotators for this image``. |
| 29 | """ |
| 30 | |
| 31 | def __init__(self, name, data_dir=None, shuffle=True): |
| 32 | """ |
| 33 | Args: |
| 34 | name (str): 'train', 'test', 'val' |
| 35 | data_dir (str): a directory containing the original 'BSR' directory. |
| 36 | """ |
| 37 | # check and download data |
| 38 | if data_dir is None: |
| 39 | data_dir = get_dataset_path('bsds500_data') |
| 40 | if not os.path.isdir(os.path.join(data_dir, 'BSR')): |
| 41 | download(DATA_URL, data_dir, expect_size=DATA_SIZE) |
| 42 | filename = DATA_URL.split('/')[-1] |
| 43 | filepath = os.path.join(data_dir, filename) |
| 44 | import tarfile |
| 45 | tarfile.open(filepath, 'r:gz').extractall(data_dir) |
| 46 | self.data_root = os.path.join(data_dir, 'BSR', 'BSDS500', 'data') |
| 47 | assert os.path.isdir(self.data_root) |
| 48 | |
| 49 | self.shuffle = shuffle |
| 50 | assert name in ['train', 'test', 'val'] |
| 51 | self._load(name) |
| 52 | |
| 53 | def _load(self, name): |
| 54 | image_glob = os.path.join(self.data_root, 'images', name, '*.jpg') |
| 55 | image_files = glob.glob(image_glob) |
| 56 | gt_dir = os.path.join(self.data_root, 'groundTruth', name) |
| 57 | self.data = np.zeros((len(image_files), IMG_H, IMG_W, 3), dtype='uint8') |
| 58 | self.label = np.zeros((len(image_files), IMG_H, IMG_W), dtype='float32') |
| 59 | |
| 60 | for idx, f in enumerate(image_files): |
| 61 | im = cv2.imread(f, cv2.IMREAD_COLOR) |
| 62 | assert im is not None |
| 63 | if im.shape[0] > im.shape[1]: |
| 64 | im = np.transpose(im, (1, 0, 2)) |
| 65 | assert im.shape[:2] == (IMG_H, IMG_W), "{} != {}".format(im.shape[:2], (IMG_H, IMG_W)) |
| 66 | |
| 67 | imgid = os.path.basename(f).split('.')[0] |
| 68 | gt_file = os.path.join(gt_dir, imgid) |
| 69 | gt = loadmat(gt_file)['groundTruth'][0] |
| 70 | n_annot = gt.shape[0] |
| 71 | gt = sum(gt[k]['Boundaries'][0][0] for k in range(n_annot)) |
| 72 | gt = gt.astype('float32') |
| 73 | gt *= 1.0 / n_annot |
| 74 | if gt.shape[0] > gt.shape[1]: |
| 75 | gt = gt.transpose() |
| 76 | assert gt.shape == (IMG_H, IMG_W) |
| 77 |
no outgoing calls
no test coverage detected
searching dependent graphs…