예제 #1
0
    def __iter__(self):
        for path_a, path_b in self.ds.__iter__():
            img_a = read_image(path_a)
            img_b = read_image(path_b)

            if self.fit_resize:
                min_ratio_a = max(self.crop_size[0] / img_a.shape[0],
                                  self.crop_size[1] / img_a.shape[1])
                ratio_a = np.random.uniform(
                    min_ratio_a, min_ratio_a * self.random_fit_max_ratio
                ) if self.random_fit else min_ratio_a
                img_a = resize_image_by_ratio(img_a, ratio_a)

                min_ratio_b = max(self.crop_size[0] / img_b.shape[0],
                                  self.crop_size[1] / img_b.shape[1])
                ratio_b = np.random.uniform(
                    min_ratio_b, min_ratio_b * self.random_fit_max_ratio
                ) if self.random_fit else min_ratio_b
                img_b = resize_image_by_ratio(img_b, ratio_b)

            if self.crop_size is not None:
                w_offset_a = np.random.randint(
                    0, img_a.shape[1] - self.crop_size[1] + 1)
                h_offset_a = np.random.randint(
                    0, img_a.shape[0] - self.crop_size[0] + 1)
                img_a = img_a[h_offset_a:h_offset_a + self.crop_size[0],
                              w_offset_a:w_offset_a + self.crop_size[1], :]
                assert img_a.shape[:2] == self.crop_size

                w_offset_b = np.random.randint(
                    0, img_b.shape[1] - self.crop_size[1] + 1)
                h_offset_b = np.random.randint(
                    0, img_b.shape[0] - self.crop_size[0] + 1)
                img_b = img_b[h_offset_b:h_offset_b + self.crop_size[0],
                              w_offset_b:w_offset_b + self.crop_size[1], :]
                assert img_b.shape[:2] == self.crop_size

            if self.random_flip and np.random.random() > 0.5:
                img_a = np.fliplr(img_a)
                img_b = np.fliplr(img_b)

            img_a = img_a.astype(np.float32)
            img_b = img_b.astype(np.float32)

            if self.random_brightness:
                img_a = np.clip(img_a + 255 * np.random.uniform(-0.05, 0.05),
                                0, 255)
                img_b = np.clip(img_b + 255 * np.random.uniform(-0.05, 0.05),
                                0, 255)

            if self.random_contrast:
                img_a = np.clip(
                    (img_a - 127.5) * np.random.uniform(0.9, 1.1) + 127.5, 0,
                    255)
                img_b = np.clip(
                    (img_b - 127.5) * np.random.uniform(0.9, 1.1) + 127.5, 0,
                    255)

            # normalize
            yield normalize_image(img_a), normalize_image(img_b)
def load_image_train(image_file, height, width, resize_ratio_before_crop):
    input_image, real_image = vertical_split(load_image(image_file))
    input_image, real_image = random_jitter(input_image, real_image, height,
                                            width, resize_ratio_before_crop)
    input_image, real_image = normalize_image(input_image), normalize_image(
        real_image)

    return input_image, real_image
def load_image_test(image_file, height, width):
    input_image, real_image = vertical_split(load_image(image_file))
    input_image, real_image = resize_image(input_image, height,
                                           width), resize_image(
                                               real_image, height, width)
    input_image, real_image = normalize_image(input_image), normalize_image(
        real_image)

    return input_image, real_image
예제 #4
0
    def _preprocess_ct_img(self, img_slc):
        """
        Set pixels with hounsfield value great than 1200, to zero.
        Clip all hounsfield values to the range [-100, 400]
        Normalize values to [0, 1]
        Rescale img and label slices to 388x388
        Pad img slices with 92 pixels on all sides (so total shape is 572x572)
        
        Args:
            img_slc: raw image slice
        Return:
            Preprocessed image slice
        """
        img_slc = img_slc.astype(np.float)
        img_slc = normalize_image(img_slc)

        return img_slc
예제 #5
0
def main(file, use_horovod, gpus, config_path, checkpoint):
    config = process_config(config_path, use_horovod, gpus, checkpoint)
    setup_tf_config()

    if os.path.exists(file):
        image = Image.open(file).convert('L')
        image = np.array(ImageOps.invert(image))
        image = np.expand_dims(image, axis=2)
        image = np.expand_dims(image, axis=0)
        image = normalize_image(image)

        data_loader = MNISTDataLoader(config)
        model, _ = build_model_and_trainer(config, data_loader)

        prediction = model.predict(image)
        print(np.argmax(np.squeeze(prediction)))
    else:
        raise ValueError('File does not exist')
예제 #6
0
 def preprocess(data):
     x, y = data
     return normalize_image(x), y
예제 #7
0
def load_image_test(image_file, label_file, height, width):
    input_image, real_image = load_image(image_file), load_image(label_file)
    input_image, real_image = normalize_image(input_image), normalize_image(real_image)

    return input_image, real_image
예제 #8
0
def load_image_train(image_file, label_file, height, width, resize_ratio_before_crop):
    input_image, real_image = load_image(image_file), load_image(label_file)
    input_image, real_image = normalize_image(input_image), normalize_image(real_image)

    return input_image, real_image
예제 #9
0
 def __iter__(self):
     for x, y in self.ds.__iter__():
         # normalize
         yield normalize_image(x), y