Exemple #1
0
def augment(images):
    image_mirror_lr = []
    image_mirror_ud = []
    image_rotate = []
    for i in range(0, images.shape[0]):
        band_1 = images[i, :, :, 0]
        band_2 = images[i, :, :, 1]
        band_3 = images[i, :, :, 2]

        # mirror left-right
        band_1_mirror_lr = np.flip(band_1, 0)
        band_2_mirror_lr = np.flip(band_2, 0)
        band_3_mirror_lr = np.flip(band_3, 0)
        image_mirror_lr.append(
            np.dstack((band_1_mirror_lr, band_2_mirror_lr, band_3_mirror_lr)))

        # mirror up-down
        band_1_mirror_ud = np.flip(band_1, 1)
        band_2_mirror_ud = np.flip(band_2, 1)
        band_3_mirror_ud = np.flip(band_3, 1)
        image_mirror_ud.append(
            np.dstack((band_1_mirror_ud, band_2_mirror_ud, band_3_mirror_ud)))

        #rotate
        band_1_rotate = rot(band_1, 30, reshape=False)
        band_2_rotate = rot(band_2, 30, reshape=False)
        band_3_rotate = rot(band_3, 30, reshape=False)
        image_rotate.append(
            np.dstack((band_1_rotate, band_2_rotate, band_3_rotate)))

    mirrorlr = np.array(image_mirror_lr)
    mirrorud = np.array(image_mirror_ud)
    rotated = np.array(image_rotate)
    images = np.concatenate((images, mirrorlr, mirrorud, rotated))
    return images
Exemple #2
0
def prepare_data_for(angle, DEF=0):
    # Get the sets of images and labels for training, validation, and
    # test on MNIST.
    np.random.seed(0)
    (train_images,
     train_labels), (test_images,
                     test_labels) = tf.keras.datasets.mnist.load_data()

    TRAIN_SIZE = 1000
    TEST_SIZE = 1000

    idxs = np.random.choice(np.arange(len(train_images)),
                            TRAIN_SIZE,
                            replace=False)
    train_images = train_images[idxs].astype(np.float32)
    train_labels = train_labels[idxs].tolist()

    idxs = np.random.choice(np.arange(len(test_images)),
                            TEST_SIZE,
                            replace=False)
    test_images = test_images[idxs].astype(np.float32)
    test_labels = test_labels[idxs].tolist()
    #   test_labels = test_labels.tolist()

    # transform all train and test images
    _train_images, _train_labels, _train_uids = [], [], []
    train_per_domain, test_per_domain = {}, {}

    _timgs, _labels = [], []
    for angle in range(15, 90, 15):
        for ti in tqdm.tqdm(range(len(train_images)),
                            desc="Transforming train images"):
            _timgs.append(rot(train_images[ti], angle, reshape=False))
        _labels += train_labels

    train = [
        np.array(_timgs),
        np.array(_labels),
        np.array([DEF] * len(_timgs))
    ]

    _timgs = []
    _labels = []
    angles = [_ for _ in range(-20, 15, 5)]
    angles += [_ for _ in range(80, 125, 5)]
    for angle in angles:
        for ti in tqdm.tqdm(range(len(test_images)),
                            desc="Transforming test images"):
            _timgs.append(rot(test_images[ti], angle, reshape=False))
        _labels += test_labels
    test = [
        np.array(_timgs),
        np.array(_labels),
        np.array([DEF] * len(_labels))
    ]

    return train, test
def process_image(img_path, img_list, label_list, fruit_name, dim):
    image = cv2.imread(img_path, cv2.IMREAD_COLOR)
    image = cv2.resize(image, (dim, dim))
    img_list.append((image / 255.).tolist())

    image_flip_vert = np.flipud(image)
    img_list.append((image_flip_vert / 255.).tolist())

    image_rotate90 = rot(image, 90)
    img_list.append((image_rotate90 / 255.).tolist())

    image_rotate270 = rot(image, 270)
    img_list.append((image_rotate270 / 255.).tolist())

    label_list += [fruit_name] * 4
Exemple #4
0
def rotate(original_image, obj_center, obj_joints, angle):
    rotated_image = rot(original_image, angle)
    rotation_origin = (original_image.shape[0] // 2,
                       original_image.shape[1] // 2)

    points = np.copy(obj_center)
    points = points.reshape(-1, 2)
    points = np.append(points, obj_joints, axis=0)
    points = np.append(points, np.ones(shape=(len(points), 1)), axis=1)

    rotated_points = rotate_points(points, rotation_origin[1],
                                   rotation_origin[0], angle,
                                   original_image.shape[0],
                                   original_image.shape[1])

    rotated_center = rotated_points[0]
    rotated_joints = rotated_points[1:, :]

    return rotated_image, rotated_center, rotated_joints
Exemple #5
0
def custom_rot(img, angle, reshape=False):
    _rot = rot(img, angle, reshape=reshape)
    mx, mn = 1, 0
    return (_rot - mn) / (mx - mn)
Exemple #6
0
def prepare_data(leftout_angles):
    # Get the sets of images and labels for training, validation, and
    # test on MNIST.
    (train_images,
     train_labels), (test_images,
                     test_labels) = tf.keras.datasets.mnist.load_data()
    TRAIN_SIZE = 1000
    TEST_SIZE = 1000

    np.random.seed(0)
    idxs = np.random.choice(np.arange(len(train_images)),
                            TRAIN_SIZE,
                            replace=False)
    idxs2 = np.random.choice(np.arange(len(test_images)),
                             TEST_SIZE,
                             replace=False)
    train_images = train_images[idxs].astype(np.float32)
    train_labels = train_labels[idxs].tolist()
    test_images = test_images[idxs2].astype(np.float32)
    test_labels = test_labels[idxs2].tolist()

    train_images = (train_images - 128.) / 128.
    test_images = (test_images - 128.) / 128.

    # transform all train and test images
    _train_images, _train_labels, _train_uids = [], [], []
    _test_images, _test_labels, _test_uids = [], [], []
    for ai, angle in enumerate(range(0, 90, 15)):
        if angle in leftout_angles:
            _timgs = []
            for ti in tqdm.tqdm(range(len(test_images)),
                                desc="Transforming test images"):
                _tr = test_images[ti]
                _timgs.append(rot(_tr, angle, reshape=False))

            _test_images += _timgs
            _test_labels += test_labels
            _test_uids += [ai - 1] * len(test_images)
        else:
            _timgs = []
            for ti in tqdm.tqdm(range(len(train_images)),
                                desc="Transforming train images"):
                _tr = train_images[ti]
                _timgs.append(rot(_tr, angle, reshape=False))

            _train_images += _timgs
            _train_labels += train_labels
            _train_uids += [ai - 1] * len(train_images)

    train_images, train_labels, train_uids = np.array(_train_images), np.array(
        _train_labels), np.array(_train_uids)
    test_images, test_labels, test_uids = np.array(_test_images), np.array(
        _test_labels), np.array(_test_uids)

    train = (train_images, train_labels, train_uids)
    test = (test_images, test_labels, test_uids)
    print(np.max(train[0]), np.min(train[0]))
    print(np.max(test[0]), np.min(test[0]))

    print("Num Train: %d num test: %d" %
          (len(train_images), len(_test_images)))
    return train, test, test
Exemple #7
0
    def __getitem__(self, index):
        sig_size = self.sig_size
        TR = self.TR
        sample_rate = self.sample_rate
        tmax = self.max_time
        output_size = self.output_size

        #phase error_make
        start_time = (tmax - TR * output_size -
                      1 / sample_rate) * random.random()
        amp = self.rand_min + (self.rand_max - self.rand_min) * random.random()
        sig_match = random.randint(0, sig_size - 1)  # subject of signal
        res_time = np.expand_dims(np.int32(
            np.round(
                np.linspace(start_time, start_time + TR * output_size,
                            output_size) * sample_rate)),
                                  axis=1)  # time of signal

        temp_sig = np.transpose(self.sig_data[sig_match, res_time], (1, 0))
        temp_sig = temp_sig / np.max(np.abs(temp_sig)) * amp
        phase = np.exp(1j * 2 * np.pi * self.const * temp_sig)

        img = self.image_data[:, :, index]
        #rotation
        if (self.isrot > 0):
            angle = (random.random() - 0.5) * 2 * self.isrot
            img = rot(
                np.real(img), angle,
                reshape=False) + rot(np.imag(img), angle, reshape=False) * 1j

        #flip
        if (random.random() < self.isfilp):
            img = img[:, ::-1]

        # phase error applying
        temp_k = np.fft.fftshift(np.fft.fft2(img), axes=(0, 1)) * phase

        #SNR
        if (self.snr_max > 0):
            mean_intensity = np.mean(np.abs(img))
            SNR = self.snr_min + (self.snr_max -
                                  self.snr_min) * random.random()
            noise_std = mean_intensity / SNR
            temp_k += (
                np.random.normal(0, noise_std,
                                 size=(output_size, output_size)) + 1j *
                np.random.normal(0, noise_std, size=(output_size, output_size))
            ) * output_size

        #split image# into real & imag.
        ref_comp_img = np.fft.ifft2(np.fft.fftshift(temp_k, axes=(0, 1)),
                                    axes=(0, 1))
        ref_comp_img = ref_comp_img / (
            4 * np.std(np.abs(ref_comp_img), axis=(0, 1), keepdims=True))

        ref_images = np.zeros((2, 224, 224), dtype=np.float32)

        ref_images[0, :, :] = np.real(ref_comp_img)
        ref_images[1, :, :] = np.imag(ref_comp_img)

        return ref_images, temp_sig[0, :]