[974c13]: / FastRCNN / utils / DefectDataset.py

Download this file

132 lines (108 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import os
from skimage import exposure, filters
import chainer
from chainercv import utils
from chainercv import transforms
import warnings
root = './Data/'
root2 = './Data/'
class DetectionDataset(chainer.dataset.DatasetMixin):
"""Base class for defect defection dataset
"""
def __init__(self, data_dir='auto', split='', img_size=1024, resize=False):
if data_dir == 'auto':
data_dir = root
self.data_dir = data_dir
self.img_size = img_size
self.resize = resize
images_file = os.path.join(self.data_dir, '{}images.txt'.format(split))
self.images = [line.strip() for line in open(images_file)]
def obtain_image_name(self,i):
#print(i)
return self.images[i]
def __len__(self):
return len(self.images)
def get_example(self, i):
"""Returns the i-th example.
Args:
i (int): The index of the example.
Returns:
tuple of an image and its label.
The image is in CHW format and its color channel is ordered in
RGB.
a bounding box is appended to the returned value.
"""
img = utils.read_image(
os.path.join(self.data_dir, 'images', self.images[i]),
color=True)
# Add processing to the other two channels
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img[1, :, :] = exposure.rescale_intensity(exposure.equalize_adapthist(
exposure.rescale_intensity(img[1, :, :])), out_range=(0, 255))
img[2, :, :] = exposure.rescale_intensity(filters.gaussian(
exposure.rescale_intensity(img[2, :, :])), out_range=(0, 255))
# bbs should be a matrix (m by 4). m is the number of bounding
# boxes in the image
# labels should be an integer array (m by 1). m is the same as the bbs
bbs_file = os.path.join(self.data_dir, 'bounding_boxes', self.images[i][0:-4]+'.txt')
bbs = np.stack([line.strip().split() for line in open(bbs_file)]).astype(np.float32)
label = np.stack([0]*bbs.shape[0]).astype(np.int32)
_, H, W = img.shape
if self.resize and (H != self.img_size or W != self.img_size):
img = transforms.resize(img, (self.img_size, self.img_size))
bbs = transforms.resize_bbox(bbs, (H, W), (self.img_size, self.img_size))
return img, bbs, label
class MultiDetectionDataset(chainer.dataset.DatasetMixin):
"""Base class for multi defect defection dataset
"""
def __init__(self, data_dir='auto', split='', img_size=1024, resize=False):
if data_dir == 'auto':
data_dir = root2
self.data_dir = data_dir
self.img_size = img_size
self.resize = resize
images_file = os.path.join(self.data_dir, '{}images.txt'.format(split))
self.images = [
line.strip() for line in open(images_file)]
def __len__(self):
return len(self.images)
def get_example(self, i):
"""Returns the i-th example.
Args:
i (int): The index of the example.
Returns:
tuple of an image and its label.
The image is in CHW format and its color channel is ordered in
RGB.
a bounding box is appended to the returned value.
"""
img = utils.read_image(
os.path.join(self.data_dir, 'images', self.images[i]),
color=True)
# Add processing to the other two channels
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img[1, :, :] = exposure.rescale_intensity(exposure.equalize_adapthist(
exposure.rescale_intensity(img[1, :, :])), out_range=(0, 255))
img[2, :, :] = exposure.rescale_intensity(filters.gaussian(
exposure.rescale_intensity(img[2, :, :])), out_range=(0, 255))
# bbs should be a matrix (m by 4). m is the number of bounding
# boxes in the image
# labels should be an integer array (m by 1). m is the same as the bbs
bbs_file = os.path.join(self.data_dir, 'bounding_boxes', self.images[i][0:-4]+'.txt')
print(bbs_file)
#bbs_file = "./Data/bounding_boxes/a.txt"
label_bbs = np.loadtxt(bbs_file, dtype=np.float32)
print(label_bbs)
# only 1D for medical case
#label = label_bbs[:,0].astype(np.int32)
#bbs = label_bbs[:,1:5]
label = label_bbs[0].astype(np.int32)
bbs = label_bbs[1:5]
_, H, W = img.shape
if self.resize and (H != self.img_size or W != self.img_size):
img = transforms.resize(img, (self.img_size, self.img_size))
bbs = transforms.resize_bbox(bbs, (H, W), (self.img_size, self.img_size))
return img, bbs, label