Beispiel #1
0
 def fun_rcdt_single(self, I):
     # I: (width, height)
     radoncdt = RadonCDT(self.thetas)
     Ircdt = radoncdt.forward(x0_range,
                              self.template / np.sum(self.template),
                              x_range, I / np.sum(I), self.rm_edge)
     return Ircdt
Beispiel #2
0
 def fun_rcdt_single(self, I):
     # I: (rows, columns)
     radoncdt = RadonCDT(self.thetas)
     template = np.ones(I.shape, dtype=I.dtype)
     Ircdt = radoncdt.forward(x0_range, template / np.sum(template),
                              x_range, I / np.sum(I), self.rm_edge)
     return Ircdt
Beispiel #3
0
 def forward_seq(self, X, template):
     self.template = template
     radoncdt = RadonCDT(self.thetas)
     x_hat = []
     for i in range(X.shape[0]):
         x_hat.append(
             radoncdt.forward(x0_range,
                              self.template / np.sum(self.template),
                              x_range, X[i, :] / np.sum(X[i, :]),
                              self.rm_edge))
     return np.asarray(x_hat)
Beispiel #4
0
    assert not args.use_gpu
    import torch
    import skorch
    from torch import nn
    from skorch import NeuralNetClassifier

num_classes, img_size, po_train_max, rm_edge = dataset_config(args.dataset)
theta = np.linspace(0, 176, 45)

# Functions to calculate RCDT when MLP classifier is used
from pytranskit.optrans.continuous.radoncdt import RadonCDT

eps = 1e-6
x0_range = [0, 1]
x_range = [0, 1]
radoncdt = RadonCDT(theta)


def fun_rcdt_single(I):
    # I: (width, height)
    template = np.ones(I.shape, dtype=I.dtype)
    Ircdt = radoncdt.forward(x0_range, template / np.sum(template), x_range,
                             I / np.sum(I), rm_edge)
    return Ircdt


def fun_rcdt_batch(data):
    # data: (n_samples, width, height)
    dataRCDT = [
        fun_rcdt_single(data[j, :, :] + eps) for j in range(data.shape[0])
    ]
Beispiel #5
0
 def fun_ircdt_single(self, Ihat):
     radoncdt = RadonCDT(self.thetas)
     Iircdt = radoncdt.apply_inverse_map(Ihat, self.template, x_range)
     return Iircdt
Beispiel #6
0
    def visualize(self, directions=5, points=5, thetas=theta, SD_spread=1):
        dir_num = directions
        gI_num = points
        b_hat1 = self.basis_hat1
        b_hat2 = self.basis_hat2
        s_tilde_tr1 = self.cca_proj_tr1
        s_tilde_tr2 = self.cca_proj_tr2
        s_tilde_te1 = self.cca_proj_te1
        s_tilde_te2 = self.cca_proj_te2
        cca_dirs1 = b_hat1[:dir_num, :]
        cca_dirs2 = b_hat2[:dir_num, :]
        cca_proj1 = s_tilde_tr1[:, :dir_num]
        cca_proj2 = s_tilde_tr2[:, :dir_num]

        radoncdt = RadonCDT(thetas)

        ## figure 1 of 3
        viz_cca1 = np.zeros((dir_num, self.R, self.C * gI_num))
        viz_cca2 = np.zeros((dir_num, self.R, self.C * gI_num))
        for a in range(dir_num):
            lamb1 = np.linspace(-SD_spread * np.std(cca_proj1[:, a]),
                                SD_spread * np.std(cca_proj1[:, a]),
                                num=gI_num)
            lamb2 = np.linspace(-SD_spread * np.std(cca_proj2[:, a]),
                                SD_spread * np.std(cca_proj2[:, a]),
                                num=gI_num)
            mode_var1 = np.zeros([gI_num, self.Rtr, self.Ctr])
            mode_var2 = np.zeros([gI_num, self.Rtr, self.Ctr])
            mode_var_recon1 = np.zeros([gI_num, self.R, self.C])
            mode_var_recon2 = np.zeros([gI_num, self.R, self.C])
            for b in range(gI_num):
                mode_var1[b, :, :] = self.mean_tr + lamb1[b] * cca_dirs1[a, :]
                mode_var2[b, :, :] = self.mean_tr + lamb2[b] * cca_dirs2[a, :]
                mode_var_recon1[b, :, :] = radoncdt.apply_inverse_map(
                    mode_var1[b, :, :], self.template, x_range)
                mode_var_recon2[b, :, :] = radoncdt.apply_inverse_map(
                    mode_var2[b, :, :], self.template, x_range)
                t1 = mode_var_recon1[b]
                t2 = mode_var_recon2[b]
                t1 = t1 - np.min(t1)
                t1 = t1 / np.max(t1)
                t2 = t2 - np.min(t2)
                t2 = t2 / np.max(t2)
                mode_var_recon1[b] = t1
                mode_var_recon2[b] = t2

            viz_cca1[a, :] = mode_var_recon1.transpose(2, 0,
                                                       1).reshape(self.C, -1)
            viz_cca2[a, :] = mode_var_recon2.transpose(2, 0,
                                                       1).reshape(self.C, -1)

        for a in range(dir_num):
            if a == 0:
                F1 = viz_cca1[a, :]
                F2 = viz_cca2[a, :]
            else:
                F1 = np.concatenate((F1, viz_cca1[a, :]), axis=0)
                F2 = np.concatenate((F2, viz_cca2[a, :]), axis=0)
        r1, c1 = np.shape(F1)
        r2, c2 = np.shape(F2)

        fig, (ax0, ax1) = plt.subplots(ncols=2,
                                       figsize=(16, 8),
                                       sharex=True,
                                       sharey=True)
        ax0.imshow(np.transpose(F1), cmap='gray')
        ax0.set_xlabel('Modes of variation', fontsize=12)
        ax0.set_ylabel('($\sigma$)', fontsize=12)
        ax0.set_title('Variation along the prominant CCA modes (Class 0)')
        plt.xticks(
            np.linspace(r1 / (2 * dir_num), r1 - r1 / (2 * dir_num), dir_num),
            np.array(range(1, dir_num + 1)))
        plt.yticks(
            np.linspace(1, c1, 5),
            np.array([-SD_spread, -SD_spread / 2, 0, SD_spread / 2,
                      SD_spread]))

        ax1.imshow(np.transpose(F2), cmap='gray')
        ax1.set_xlabel('Modes of variation', fontsize=12)
        ax1.set_ylabel('($\sigma$)', fontsize=12)
        ax1.set_title('Variation along the prominant CCA modes (Class 1)')
        plt.xticks(
            np.linspace(r1 / (2 * dir_num), r1 - r1 / (2 * dir_num), dir_num),
            np.array(range(1, dir_num + 1)))
        plt.yticks(
            np.linspace(1, c1, 5),
            np.array([-SD_spread, -SD_spread / 2, 0, SD_spread / 2,
                      SD_spread]))
        plt.show()

        ## figure 2 of 3
        viz_dirs1 = viz_cca1[:2, :]
        viz_dirs2 = viz_cca2[:2, :]
        proj_tr1 = s_tilde_tr1[:, :2]
        proj_tr2 = s_tilde_tr2[:, :2]
        proj_te1 = s_tilde_te1[:, :2]
        proj_te2 = s_tilde_te2[:, :2]

        plt.figure(figsize=(18, 7))
        leg_str = ['Variable X', 'Variable Y']

        bas1 = np.array([0, 1])
        bas1a = bas1 * np.min(proj_tr1[:, 0])
        bas1b = bas1 * np.max(proj_tr1[:, 0])
        basy = [bas1a[0], bas1b[0]]
        basx = [bas1a[1], bas1b[1]]
        ax0 = plt.subplot2grid((4, 10), (0, 1), colspan=3, rowspan=3)
        ax0.grid(linestyle='--')

        X = proj_tr1
        Y = proj_tr2
        ax0.scatter(X[:, 0], X[:, 1], color='C' + str(1))
        ax0.scatter(Y[:, 0], Y[:, 1], color='C' + str(2))

        ax0.legend(leg_str)
        ax0.plot(basx, basy, color='C4')
        ax0.set_title(
            'Projection of training data on the first 2 CCA directions')
        ax1 = plt.subplot2grid((4, 10), (3, 1), colspan=3, rowspan=1)
        xax = viz_dirs1[0, :]
        ax1.imshow(xax, cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
        ax2 = plt.subplot2grid((4, 10), (0, 0), colspan=1, rowspan=3)
        yax = np.transpose(viz_dirs1[1, :])
        ax2.imshow(yax, cmap='gray')
        ax2.set_xticks([])
        ax2.set_yticks([])

        bas1a = bas1 * np.min(proj_te1[:, 0])
        bas1b = bas1 * np.max(proj_te1[:, 0])
        basy = [bas1a[0], bas1b[0]]
        basx = [bas1a[1], bas1b[1]]
        ax0 = plt.subplot2grid((4, 10), (0, 6), colspan=3, rowspan=3)
        ax0.grid(linestyle='--')

        X = proj_te1
        Y = proj_te2
        ax0.scatter(X[:, 0], X[:, 1], color='C' + str(1))
        ax0.scatter(Y[:, 0], Y[:, 1], color='C' + str(2))

        ax0.legend(leg_str)
        ax0.plot(basx, basy, color='C4')
        ax0.set_title('Projection of test data on the first 2 CCA directions')
        ax1 = plt.subplot2grid((4, 10), (3, 6), colspan=3, rowspan=1)
        xax = viz_dirs1[0, :]
        ax1.imshow(xax, cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
        ax2 = plt.subplot2grid((4, 10), (0, 5), colspan=1, rowspan=3)
        yax = np.transpose(viz_dirs1[1, :])
        ax2.imshow(yax, cmap='gray')
        ax2.set_xticks([])
        ax2.set_yticks([])

        ## figure 3 of 3
        which_direction = 1
        viz_dirs1 = viz_cca1[which_direction - 1:which_direction, :]
        viz_dirs2 = viz_cca2[which_direction - 1:which_direction, :]
        proj_tr1 = s_tilde_tr1[:, which_direction - 1]
        proj_te1 = s_tilde_te1[:, which_direction - 1]
        proj_tr2 = s_tilde_tr2[:, which_direction - 1]
        proj_te2 = s_tilde_te2[:, which_direction - 1]

        plt.figure(figsize=(16, 7))

        leg_str = ['Variable X']
        ax0 = plt.subplot2grid((4, 8), (0, 0), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        XX = proj_tr1
        ax0.hist(XX, color=['C1'])
        ax0.legend(leg_str)
        ax0.set_title('Projection of training data on the first CCA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 0), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs1[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])

        ax0 = plt.subplot2grid((4, 8), (0, 5), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        XX = proj_te1
        ax0.hist(XX, color=['C1'])
        ax0.legend(leg_str)
        ax0.set_title('Projection of test data on the first CCA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 5), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs1[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])

        plt.figure(figsize=(16, 7))

        leg_str = ['Variable Y']
        ax0 = plt.subplot2grid((4, 8), (0, 0), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        XX = proj_tr2
        ax0.hist(XX, color=['C2'])
        ax0.legend(leg_str)
        ax0.set_title('Projection of training data on the first CCA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 0), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs2[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])

        ax0 = plt.subplot2grid((4, 8), (0, 5), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        XX = proj_te2
        ax0.hist(XX, color=['C2'])
        ax0.legend(leg_str)
        ax0.set_title('Projection of test data on the first CCA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 5), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs2[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
Beispiel #7
0
    def visualize(self, directions=5, points=5, thetas=theta, SD_spread=1):
        dir_num = directions
        gI_num = points
        b_hat = self.basis_hat
        s_tilde_tr = self.plda_proj_tr
        s_tilde_te = self.plda_proj_te
        plda_dirs = b_hat[:dir_num, :]
        plda_proj = s_tilde_tr[:, :dir_num]

        radoncdt = RadonCDT(thetas)

        ## figure 1 of 3
        viz_plda = np.zeros((dir_num, self.R, self.C * gI_num))
        for a in range(dir_num):
            lamb = np.linspace(-SD_spread * np.std(plda_proj[:, a]),
                               SD_spread * np.std(plda_proj[:, a]),
                               num=gI_num)
            mode_var = np.zeros([gI_num, self.Rtr, self.Ctr])
            mode_var_recon = np.zeros([gI_num, self.R, self.C])
            for b in range(gI_num):
                mode_var[b, :, :] = self.mean_tr + lamb[b] * plda_dirs[a, :]
                mode_var_recon[b, :, :] = radoncdt.apply_inverse_map(
                    mode_var[b, :, :], self.template, x_range)
                t = mode_var_recon[b]
                t = t - np.min(t)
                t = t / np.max(t)
                mode_var_recon[b] = t
            viz_plda[a, :] = mode_var_recon.transpose(2, 0,
                                                      1).reshape(self.C, -1)

        for a in range(dir_num):
            if a == 0:
                F1 = viz_plda[a, :]
            else:
                F1 = np.concatenate((F1, viz_plda[a, :]), axis=0)
        r, c = np.shape(F1)

        plt.figure(figsize=(7, 7))
        plt.imshow(np.transpose(F1), cmap='gray')
        plt.xticks(
            np.linspace(r / (2 * dir_num), r - r / (2 * dir_num), dir_num),
            np.array(range(1, dir_num + 1)))
        plt.xlabel('Modes of variation', fontsize=12)
        plt.yticks(
            np.linspace(1, c, 5),
            np.array([-SD_spread, -SD_spread / 2, 0, SD_spread / 2,
                      SD_spread]))
        plt.ylabel('($\sigma$)', fontsize=12)
        plt.title('Variation along the prominant PLDA modes')

        ## figure 2 of 3
        y_train = self.y_train
        y_test = self.y_test
        viz_dirs = viz_plda[:2, :]
        proj_tr = self.plda_proj_tr[:, :2]
        proj_te = self.plda_proj_te[:, :2]

        plt.figure(figsize=(18, 7))
        leg_str = ['class 1', 'class 2']

        bas1 = np.array([0, 1])
        bas1a = bas1 * np.min(proj_tr[:, 0])
        bas1b = bas1 * np.max(proj_tr[:, 0])
        basy = [bas1a[0], bas1b[0]]
        basx = [bas1a[1], bas1b[1]]
        ax0 = plt.subplot2grid((4, 10), (0, 1), colspan=3, rowspan=3)
        ax0.grid(linestyle='--')
        y_unique = np.unique(y_train)
        for a in range(len(y_unique)):
            t = np.where(a == y_train)
            X = proj_tr[t]
            ax0.scatter(X[:, 0], X[:, 1], color='C' + str(a + 1))
        ax0.legend(leg_str)
        ax0.plot(basx, basy, color='C4')
        ax0.set_title(
            'Projection of training data on the first 2 PLDA directions')
        ax1 = plt.subplot2grid((4, 10), (3, 1), colspan=3, rowspan=1)
        xax = viz_dirs[0, :]
        ax1.imshow(xax, cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
        ax2 = plt.subplot2grid((4, 10), (0, 0), colspan=1, rowspan=3)
        yax = np.transpose(viz_dirs[1, :])
        ax2.imshow(yax, cmap='gray')
        ax2.set_xticks([])
        ax2.set_yticks([])

        bas1a = bas1 * np.min(proj_te[:, 0])
        bas1b = bas1 * np.max(proj_te[:, 0])
        basy = [bas1a[0], bas1b[0]]
        basx = [bas1a[1], bas1b[1]]
        ax0 = plt.subplot2grid((4, 10), (0, 6), colspan=3, rowspan=3)
        ax0.grid(linestyle='--')
        y_unique = np.unique(y_test)
        for a in range(len(y_unique)):
            t = np.where(a == y_test)
            X = proj_te[t]
            ax0.scatter(X[:, 0], X[:, 1], color='C' + str(a + 1))
        ax0.legend(leg_str)
        ax0.plot(basx, basy, color='C4')
        ax0.set_title('Projection of test data on the first 2 PLDA directions')
        ax1 = plt.subplot2grid((4, 10), (3, 6), colspan=3, rowspan=1)
        xax = viz_dirs[0, :]
        ax1.imshow(xax, cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
        ax2 = plt.subplot2grid((4, 10), (0, 5), colspan=1, rowspan=3)
        yax = np.transpose(viz_dirs[1, :])
        ax2.imshow(yax, cmap='gray')
        ax2.set_xticks([])
        ax2.set_yticks([])

        ## figure 3 of 3
        which_direction = 1
        viz_dirs = viz_plda[which_direction - 1:which_direction, :]
        proj_tr = s_tilde_tr[:, which_direction - 1]
        proj_te = s_tilde_te[:, which_direction - 1]
        plt.figure(figsize=(16, 7))
        leg_str = ['class 1', 'class 2']

        ax0 = plt.subplot2grid((4, 8), (0, 0), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        y_unique = np.unique(y_train)
        for a in range(len(y_unique)):
            t = np.where(a == y_train)
            y = y_train[t]
            X = proj_tr[t]
            X = np.reshape(X, (len(y)))
            if a == 0:
                XX = [X]
            else:
                XX.append(X)
        ax0.hist(XX, color=['C1', 'C2'])
        ax0.legend(leg_str)
        ax0.set_title(
            'Projection of training data on the first PLDA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 0), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])

        ax0 = plt.subplot2grid((4, 8), (0, 5), colspan=3, rowspan=2)
        ax0.grid(linestyle='--')
        y_unique = np.unique(y_test)
        for a in range(len(y_unique)):
            t = np.where(a == y_test)
            y = y_test[t]
            X = proj_te[t]
            X = np.reshape(X, (len(y)))
            if a == 0:
                XX = [X]
            else:
                XX.append(X)
        ax0.hist(XX, color=['C1', 'C2'])
        ax0.legend(leg_str)
        ax0.set_title('Projection of test data on the first PLDA direction')
        ax1 = plt.subplot2grid((4, 8), (2, 5), colspan=3, rowspan=1)
        ax1.imshow(viz_dirs[0, :], cmap='gray')
        ax1.set_xticks([])
        ax1.set_yticks([])
Beispiel #8
0
from pypapi import events, papi_high as high
import numpy as np
from pytranskit.optrans.continuous.radoncdt import RadonCDT
import sys
from skimage.transform import rotate

input_size = int(sys.argv[1])

eps = 1e-6
x0_range = [0, 1]
x_range = [0, 1]
Rdown = 4  # downsample radon projections (w.r.t. angles)
theta = np.linspace(0, 176, 180 // Rdown)
radoncdt = RadonCDT(theta)

I = np.random.rand(input_size, input_size)
template = np.ones(I.shape, dtype=I.dtype)

high.start_counters([
    events.PAPI_FP_OPS,
])
rcdt = radoncdt.forward(x0_range, template / np.sum(template), x_range,
                        I / np.sum(I), False)
print(high.stop_counters()[0] / 1e9)


def discrete_radon_transform(image, steps):
    R = np.zeros((steps, image.shape[0]), dtype=np.float32)
    for s in range(steps):
        rotation = rotate(image, -s * 180 / steps).astype(np.float32)
        R[s] = np.sum(rotation, axis=1)