class ComputeImagesMean():

    def __init__(self, image_size):
        self.image_size = image_size
        self.dir_process = DirProcess()
        self.image_process = ImageProcess()
        self.dataset_process = ImageDataSetProcess()

    def compute(self, train_path):
        numpy_images = []
        path, _ = os.path.split(train_path)
        images_dir = os.path.join(path, "../JPEGImages")
        for line_data in self.dir_process.getFileData(train_path):
            data_list = [x.strip() for x in line_data.split() if x.strip()]
            if len(data_list) >= 1:
                image_path = os.path.join(images_dir, data_list[0])
                src_image, rgb_image = self.image_process.readRgbImage(image_path)
                rgb_image = self.dataset_process.image_resize(rgb_image, self.image_size)
                normaliza_image = self.dataset_process.image_normaliza(rgb_image)
                numpy_images.append(normaliza_image)
            else:
                print("read %s image path error!" % data_list)
        numpy_images = np.stack(numpy_images)
        mean = np.mean(numpy_images, axis=(0, 1, 2))
        std = np.std(numpy_images, axis=(0, 1, 2))
        return mean, std
Пример #2
0
class SegmentResultProcess():
    def __init__(self):
        self.dataset_process = ImageDataSetProcess()

    def get_detection_result(self, prediction, threshold=0):
        result = None
        if prediction.ndim == 2:
            result = (prediction >= threshold).astype(int)
            # print(set(list(result.flatten())))
        elif prediction.ndim == 3:
            result = np.argmax(prediction, axis=0)
        elif prediction.ndim == 4:
            result = np.argmax(prediction, axis=1)
        return result

    def resize_segmention_result(self, src_size, image_size,
                                 segmention_result):
        ratio, pad = self.dataset_process.resize_square_size(
            src_size, image_size)
        start_h = pad[1] // 2
        stop_h = image_size[1] - (pad[1] - (pad[1] // 2))
        start_w = pad[0] // 2
        stop_w = image_size[0] - (pad[0] - (pad[0] // 2))
        result = segmention_result[start_h:stop_h, start_w:stop_w]
        result = result.astype(np.float32)
        result = self.dataset_process.image_resize(result, src_size)
        return result

    def output_feature_map_resize(self, input_data, target):
        target = target.type(input_data.dtype)
        n, c, h, w = input_data.size()
        nt, ht, wt = target.size()
        # Handle inconsistent size between input and target
        if h > ht and w > wt:  # upsample labels
            target = target.unsequeeze(1)
            target = torch.nn.functional.upsample(target,
                                                  size=(h, w),
                                                  mode='nearest')
            target = target.sequeeze(1)
        elif h < ht and w < wt:  # upsample images
            input_data = torch.nn.functional.upsample(input_data,
                                                      size=(ht, wt),
                                                      mode='bilinear')
        elif h == ht and w == wt:
            pass
        else:
            print("input_data: (%d,%d) and target: (%d,%d) error " %
                  (h, w, ht, wt))
            raise Exception("segment_data_resize error")
        return input_data, target
Пример #3
0
class ClassifyDatasetProcess(BaseDataSetProcess):
    def __init__(self, mean, std):
        super().__init__()
        self.dataset_process = ImageDataSetProcess()
        self.mean = np.array(mean, dtype=np.float32)
        self.std = np.array(std, dtype=np.float32)
        self.normalize_transform = self.torchvision_process.torch_normalize(
            flag=0, mean=self.mean, std=self.std)

    def normaliza_dataset(self, src_image, normaliza_type=0):
        result = None
        if normaliza_type == 0:  # numpy normalize
            normaliza_image = self.dataset_process.image_normaliza(src_image)
            image = self.dataset_process.numpy_normaliza(
                normaliza_image, self.mean, self.std)
            image = self.dataset_process.numpy_transpose(image, image.dtype)
            result = self.numpy_to_torch(image, flag=0)
        elif normaliza_type == 1:  # torchvision normalize
            result = self.normalize_transform(src_image)
        return result

    def resize_image(self, src_image, image_size):
        image = self.dataset_process.image_resize(src_image, image_size)
        return image