Beispiel #1
0
    def enc_full_grid_mtx_nd(self,
                             pts_enc,
                             axis=1,
                             returnFlat=False,
                             returnGrid=False):
        ''' Expand a distribution into full distribution over non-legit ab values
		INPUTS
		 	pts_enc 		NxCxXxY		encoded nd mtx (typically)
		 	axis 			integer
		 	returnFlat 		bool
		 	returnGrid 		bool 		expand dimension to AxB
		OUTPUTS
		 	pts_full 		if returnGrid 		NXYxAxB or NxAxBxXxY
		 	 				if not returnGrid 	NXYxC_full or NxC_fullxXxY '''
        pts_flt = rz.flatten_nd_array(pts_enc, axis=axis)
        P = pts_flt.shape[0]

        pts_full_flt = np.zeros((P, self.grid.AB), dtype='float32')
        pts_full_flt[:, self.grid_mask] = pts_flt
        if (returnFlat):
            ret_mtx = pts_full_flt
            ret_axis = 1
        else:
            ret_mtx = rz.unflatten_2d_array(pts_full_flt, pts_enc, axis=axis)
            ret_axis = axis

        if (returnGrid):
            return rz.reshape_single_axis(ret_mtx,
                                          self.grid.A,
                                          self.grid.B,
                                          axis=ret_axis)
        else:
            return ret_mtx
Beispiel #2
0
def entropy_mtx_nd(distr_nd, axis=1, eps=1e-10):
    distr_flt = rz.flatten_nd_array(distr_nd, axis=axis)
    entropy_flt = -np.sum(distr_flt * np.log10(distr_flt + eps),
                          axis=1)[:, rz.na()]
    entropy_nd = rz.unflatten_2d_array(entropy_flt,
                                       distr_nd,
                                       axis=axis,
                                       squeeze=True)
    return entropy_nd
Beispiel #3
0
def nd_argmax_4hot_conv22(pts_enc_nd, A, B, axis=1):
    # Compute highest index along axis, return 4-hot encoding along that axis
    # run a 2x2 convolution to figure out how which 4 indices to keep
    # (1) Reshape axis to be 2d
    # (2) Find maximum location
    # (3) Map to the 4 indices in that location
    # INPUTS
    # 	pts_enc_nd 		NxCxXxY (typically), or any nd array
    # 	A 				integer 	AxB=C
    # 	B 				integer
    # 	axis 			integer 	which contains channel C dimension
    # OUTPUTS
    # 	pts_enc_1hot_4d 	NxCxXxY (typically) with 1 non-zero values along each channel

    # flatten into NXYxC
    # reshape into NXYxAxB

    # t = rz.Timer()
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    NXY = pts_enc_flt.shape[0]
    pts_enc_flt = pts_enc_flt.reshape((NXY, A, B))
    # print '  %s'%t.tocStr()

    # run 2x2 convolution
    pts_enc_flt_conv22 = pts_enc_flt[:, :-1, :
                                     -1] + pts_enc_flt[:, 1:, :
                                                       -1] + pts_enc_flt[:, :-1,
                                                                         1:] + pts_enc_flt[:,
                                                                                           1:,
                                                                                           1:]
    pts_enc_flt_conv22 = pts_enc_flt_conv22.reshape((NXY, (A - 1) * (B - 1)))
    high_ind = np.argmax(pts_enc_flt_conv22, axis=1)
    high_sub = rz.ind2sub2(high_ind, (A - 1, B - 1))
    # print '  %s'%t.tocStr()

    # find top location
    ogrid = np.ogrid[:NXY, :A, :B]

    mask = np.zeros((NXY, A, B), dtype='bool')
    mask[ogrid[0][:, 0, 0], high_sub[:, 0], high_sub[:, 1]] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0] + 1, high_sub[:, 1]] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0], high_sub[:, 1] + 1] = True
    mask[ogrid[0][:, 0, 0], high_sub[:, 0] + 1, high_sub[:, 1] + 1] = True
    # print '  %s'%t.tocStr()

    # mask & renormalize
    pts_enc_flt = (pts_enc_flt * mask).reshape((NXY, A * B))
    pts_enc_flt = pts_enc_flt / np.sum(pts_enc_flt, axis=1)[:, rz.na()]
    pts_enc_flt = rz.unflatten_2d_array(pts_enc_flt, pts_enc_nd, axis=axis)
    # print '  %s'%t.tocStr()

    return pts_enc_flt
Beispiel #4
0
    def encode_4hot_mtx_nd(self, pts_nd, axis=1):
        ''' Encode by
		# 	(1) finding 4 corners around grid
		# 	(2) weighting as a linear interpolation
		# 	(3) masking out points which are in hull
		# 	(4) re-normalizing (in case some points are on border) '''

        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        pts_enc_full_flt = self.grid.encode_points_mtx_nd(pts_nd,
                                                          axis=axis,
                                                          returnFlat=True)
        pts_enc_flt = pts_enc_full_flt[:, self.grid_mask]
        pts_enc_flt = pts_enc_flt / np.sum(pts_enc_flt, axis=1)[:, rz.na()]
        return rz.unflatten_2d_array(pts_enc_flt, pts_nd, axis=axis)
Beispiel #5
0
    def encode_points_mtx_nd(self, pts_nd, axis=1, returnSparse=False):
        t = rz.Timer()
        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        P = pts_flt.shape[0]

        (dists, inds) = self.nbrs.kneighbors(pts_flt)

        pts_enc_flt = np.zeros((P, self.K))
        wts = np.exp(-dists**2 / (2 * self.sigma**2))
        wts = wts / np.sum(wts, axis=1)[:, rz.na()]

        pts_enc_flt[np.arange(0, P, dtype='int')[:, rz.na()], inds] = wts
        pts_enc_nd = rz.unflatten_2d_array(pts_enc_flt, pts_nd, axis=axis)

        return pts_enc_nd
Beispiel #6
0
def nd_argmax_1hot(pts_enc_nd, axis=1):
    # Run 2x2 convolution, pick highest index, return
    # INPUTS
    # 	pts_enc_nd 	nd array
    # 	axis 			integer to perform argmax over
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    N = pts_enc_flt.shape[0]

    pts_enc_1hot_flt = np.zeros_like(pts_enc_flt)
    max_inds = np.argmax(pts_enc_flt, axis=1)
    pts_enc_1hot_flt[np.arange(0, N), max_inds] = True

    pts_enc_1hot_nd = rz.unflatten_2d_array(pts_enc_1hot_flt,
                                            pts_enc_nd,
                                            axis=axis)

    return pts_enc_1hot_nd
Beispiel #7
0
def nd_argmax_1hot(pts_enc_nd, axis=1):
    # Compute highest index along 'axis', return 1 hot encoding of that axis
    # INPUTS
    # 	pts_enc_nd 	nd array
    # 	axis 			integer to perform argmax over
    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    N = pts_enc_flt.shape[0]

    pts_enc_1hot_flt = np.zeros_like(pts_enc_flt)
    max_inds = np.argmax(pts_enc_flt, axis=1)
    pts_enc_1hot_flt[np.arange(0, N), max_inds] = True

    pts_enc_1hot_nd = rz.unflatten_2d_array(pts_enc_1hot_flt,
                                            pts_enc_nd,
                                            axis=axis)

    return pts_enc_1hot_nd
Beispiel #8
0
def expand_axis_mask(pts, grid_mask, axis=1, returnFlat=False):
    ''' Expand a single axis by a mask.
	INPUTS
		pts 		N0xN1x...xNn 	n-dimensional matrix
	 	grid_mask 	M bool vector
	 	axis 		integer 		axis to expand
	 	returnFlat 	boolean 		if True, return a PxM vector
	 								if False, return a N0xN1x...xNn, but with Naxis replaced with M '''
    AB = grid_mask.size
    pts_flt = rz.flatten_nd_array(pts, axis=axis)
    P = pts_flt.shape[0]
    pts_full_flt = np.zeros((P, AB), dtype='float32')
    pts_full_flt[:, grid_mask] = pts_flt
    if (returnFlat):
        return pts_full_flt
    else:
        return rz.unflatten_2d_array(pts_full_flt, pts, axis=axis)
Beispiel #9
0
def collect_pixel_distr(HDF5_FILENAME,
                        ab_inc=2,
                        SS=8,
                        OFF=0,
                        N=1000,
                        toNorm=True,
                        filtGray=True):
    # Collects distribution of pixels across validation set
    # INPUTS
    # 	HDF_FILENAME 	string 		hdf file with Nx3xXxY LAB images
    # 	ab_inc 			integer 	grid granularity
    # 	SS 				integer 	subsample pixels
    # 	OFF 			integer 	offset
    # 	N 				integer 	number of images to load
    # 	toNorm			boolean 	normalize output so it is a PMF
    # OUTPUTS
    # 	hist_ab_cnt_accum  	grid.a x grid.b array
    # 	grid 			grid object incremented at ab_inc

    val_data = rz.load_from_hdf5(HDF5_FILENAME)
    (X, Y) = val_data['data'][0, :, :, :].shape[1:]
    grid = rz.grid_ab(ab_inc)

    hist_ab_cnt_accum = np.zeros((grid.A, grid.B))
    b = rz.Batcher(N, 100, update_interval=10)
    for bb in range(b.B):
        b_inds = OFF + b.increment()
        data_l = val_data['data'][b_inds, :, :, :][:, [0], ::SS, ::SS]
        data_ab = val_data['data'][b_inds, :, :, :][:, [1, 2], ::SS, ::SS]
        if (filtGray):
            mask = is_data_lab_gray(data_ab)
            data_ab = data_ab[~mask, :, :, :]
            # print '  Filtered out %i grayscale images...'%np.sum(mask)

        data_ab_flt = rz.flatten_nd_array(data_ab, axis=1)  # flatten
        hist_ab_cnt_accum += np.histogram2d(data_ab_flt[:, 0],
                                            data_ab_flt[:, 1],
                                            bins=(grid.a_vals_edge,
                                                  grid.b_vals_edge))[0]

        b.print_update()

    if (toNorm):
        hist_ab_cnt_accum = 1. * hist_ab_cnt_accum / np.sum(hist_ab_cnt_accum)
    return (hist_ab_cnt_accum, grid)
Beispiel #10
0
    def decode_points_mtx_nd(self, pts_enc_nd, axis=1):
        # INPUTS
        # 	pts_enc_nd 		encoded nd points in ab space
        # 	axis 			axis containing ab

        pts_nd_polar_sqrt = self.grid.decode_points_mtx_nd(pts_enc_nd,
                                                           axis=axis)
        pts_flt_polar_sqrt = rz.flatten_nd_array(pts_nd_polar_sqrt, axis=axis)

        pts_flt_polar = pts_flt_polar_sqrt.copy()
        pts_flt_polar[:, 0] = pts_flt_polar[:, 0]**2
        pts_flt_polar[:, 1] = pts_flt_polar[:, 1] * np.pi / 180

        pts_flt = np.zeros_like(pts_flt_polar)
        pts_flt[:, 0] = pts_flt_polar[:, 0] * np.sin(pts_flt_polar[:, 1])
        pts_flt[:, 1] = pts_flt_polar[:, 0] * np.cos(pts_flt_polar[:, 1])

        pts_nd = rz.unflatten_2d_array(pts_flt, pts_enc_nd, axis=axis)
        return pts_nd
Beispiel #11
0
def collect_pixel_distr_pyramid(HDF5_FILENAME,
                                gp,
                                ab_inc=2,
                                SS=8,
                                OFF=0,
                                N=1000,
                                toNorm=True):
    # Collects distribution of pixels across validation set
    # INPUTS
    # 	HDF_FILENAME 	string 		hdf file with Nx3xXxY LAB images
    # 	gp 				GaussianPyramid object
    # 	ab_inc 			integer 	grid granularity
    # 	SS 				integer 	subsample pixels
    # 	OFF 			integer 	offset
    # 	N 				integer 	number of images to load
    # 	toNorm			boolean 	normalize output so it is a PMF

    val_data = rz.load_from_hdf5(HDF5_FILENAME)
    (X, Y) = val_data['data'][0, :, :, :].shape[1:]
    grid = rz.grid_ab(ab_inc)

    hist_ab_cnt_accum = np.zeros((grid.A, grid.B))
    b = rz.Batcher(N, 100, update_interval=10)
    for bb in range(b.B):
        b_inds = OFF + b.increment()

        data_lab = val_data['data'][b_inds, :, :, :]
        gp.pyramid_img_lab(data_lab)

        data_ab_flt = rz.flatten_nd_array(data_ab, axis=1)  # flatten
        hist_ab_cnt_accum += np.histogram2d(data_ab_flt[:, 0],
                                            data_ab_flt[:, 1],
                                            bins=(grid.a_vals_edge,
                                                  grid.b_vals_edge))[0]

        b.print_update()

    if (toNorm):
        hist_ab_cnt_accum = 1. * hist_ab_cnt_accum / np.sum(hist_ab_cnt_accum)
    return (hist_ab_cnt_accum, grid)
Beispiel #12
0
def nd_argmax_med_grid(pts_enc_nd, A, B, axis=1):
    # Compute index for median, assuming distribution is over a grid
    # (1) Reshape axis to be 2d
    # (2) Run cum-sum in those 2 dimensions
    # (3) Find median point in those 2 dimensions
    # INPUTS
    # 	pts_enc_nd 		NxCxXxY (typically), or any nd array
    # 	A 				integer 	AxB=C
    # 	B 				integer
    # 	axis 			integer 	which contains channel C dimension
    # OUTPUTS
    # 	med_enc_nd 		NxCxXxY

    pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
    NXY = pts_enc_flt.shape[0]
    pts_enc_flt = pts_enc_flt.reshape((NXY, A, B))

    # marginalize out in either dimension
    pts_enc_full_flt_marg0 = np.sum(pts_enc_flt, axis=2)
    pts_enc_full_flt_marg1 = np.sum(pts_enc_flt, axis=1)

    med_inds0 = np.argmin(
        np.abs(np.cumsum(pts_enc_full_flt_marg0, axis=1) - .5), axis=1)
    med_inds1 = np.argmin(
        np.abs(np.cumsum(pts_enc_full_flt_marg1, axis=1) - .5), axis=1)

    # plt.hist(np.min(np.abs(np.cumsum(pts_enc_full_flt_marg0,axis=1)-.5),axis=1))
    # plt.show()

    med_subs = np.concatenate((med_inds0[:, rz.na()], med_inds1[:, rz.na()]),
                              axis=1)
    med_inds = rz.sub2ind2(med_subs, (A, B))

    med_enc_flt = np.zeros((NXY, A * B), dtype='float32')
    med_enc_flt[np.arange(0, NXY), med_inds] = 1

    med_enc_nd = rz.unflatten_2d_array(med_enc_flt, pts_enc_nd, axis=axis)

    return med_enc_nd
Beispiel #13
0
    def encode_points_mtx_nd(self, pts_nd, axis=1):
        # INPUTS
        # 	pts_nd 	nd points in ab space
        # 	axis 	axis containing ab

        # self.grid()
        pts_flt = rz.flatten_nd_array(pts_nd, axis=axis)
        # print pts_flt.shape

        pts_flt_polar_sqrt = np.zeros_like(pts_flt)
        pts_flt_polar_sqrt[:, 0] = np.sqrt(np.sqrt(np.sum(pts_flt**2, axis=1)))
        pts_flt_polar_sqrt[:, 1] = np.arctan2(pts_flt[:, 0],
                                              pts_flt[:, 1]) * 180 / np.pi
        # print pts_flt_polar_sqrt.shape
        # print pts_flt_polar_sqrt

        pts_flt_enc = self.grid.encode_points(pts_flt_polar_sqrt,
                                              returnMatrix=True)
        # print pts_flt_enc.shape

        pts_enc_nd = rz.unflatten_2d_array(pts_flt_enc, pts_nd, axis=axis)

        return pts_enc_nd
Beispiel #14
0
 def decode_points_mtx_nd(self, pts_enc_nd, axis=1):
     pts_enc_flt = rz.flatten_nd_array(pts_enc_nd, axis=axis)
     pts_dec_flt = np.dot(pts_enc_flt, self.cc)
     pts_dec_nd = rz.unflatten_2d_array(pts_dec_flt, pts_enc_nd, axis=axis)
     return pts_dec_nd