def nudge_dataset(X, Y):
        """
        This produces a dataset 5 times bigger than the original one,
        by moving the 8x8 images in X around by 1px to left, right, down, up
        """
        direction_vectors = [
            [[0, 1, 0],
             [0, 0, 0],
             [0, 0, 0]],

            [[0, 0, 0],
             [1, 0, 0],
             [0, 0, 0]],

            [[0, 0, 0],
             [0, 0, 1],
             [0, 0, 0]],

            [[0, 0, 0],
             [0, 0, 0],
             [0, 1, 0]]]

        shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant',
                                      weights=w).ravel()
        X = np.concatenate([X] +
                           [np.apply_along_axis(shift, 1, X, vector)
                            for vector in direction_vectors])
        Y = np.concatenate([Y for _ in range(5)], axis=0)
        return X, Y
Exemple #2
0
    def compute_dr_wrt(self, wrt):
        result = ProjectPoints.compute_dr_wrt(self, wrt)
        if result is None:
            return None

        if sp.issparse(result):
            drz = self.z_coords.dr_wrt(wrt).tocoo()
            result = result.tocoo()
            result.row = result.row*3/2
            
            IS = np.concatenate((result.row, drz.row*3+2))
            JS = np.concatenate((result.col, drz.col))
            data = np.concatenate((result.data, drz.data))
            
            result = sp.csc_matrix((data, (IS, JS)), shape=(self.v.r.size, wrt.r.size))
        else:
            try:
                bigger = np.zeros((result.shape[0]/2, 3, result.shape[1]))
                bigger[:, :2, :] = result.reshape((-1, 2, result.shape[-1]))
                drz = self.z_coords.dr_wrt(wrt)
                if drz is not None:
                    if sp.issparse(drz):
                        drz = drz.todense()
                    bigger[:,2,:] = drz.reshape(bigger[:,2,:].shape)

                result = bigger.reshape((-1, bigger.shape[-1]))
            except:
                import pdb; pdb.set_trace()
                    

        return result            
Exemple #3
0
def test_em_gmm_cv():
    # Comparison of different GMMs using cross-validation

    # generate some data
    dim = 2
    xtrain = np.concatenate((nr.randn(100, dim), 3 + 2 * nr.randn(100, dim)))
    xtest = np.concatenate((nr.randn(1000, dim), 3 + 2 * nr.randn(1000, dim)))
    
    #estimate different GMMs for xtrain, and test it on xtest
    prec_type = 'full'
    k, maxiter, delta = 2, 300, 1.e-4
    ll = []
    
    # model 1
    lgmm = GMM(k,dim,prec_type)
    lgmm.initialize(xtrain)
    bic = lgmm.estimate(xtrain,maxiter, delta)
    ll.append(lgmm.test(xtest).mean())

    # model 2
    prec_type = 'diag'
    lgmm = GMM(k, dim, prec_type)
    lgmm.initialize(xtrain)
    bic = lgmm.estimate(xtrain, maxiter, delta)
    ll.append(lgmm.test(xtest).mean())
        
    for  k in [1, 3, 10]:
        lgmm = GMM(k,dim,prec_type)
        lgmm.initialize(xtrain)
        ll.append(lgmm.test(xtest).mean())
            
    assert_true(ll[4] < ll[1])
def parse_eph(filenm):
    global period, time
    suffix = filenm.split(".")[-1]
    if suffix == "bestprof":
        x = bestprof.bestprof(filenm)
        fs = pu.p_to_f(x.p0_bary, x.p1_bary, x.p2_bary)
        epoch = x.epochi_bary + x.epochf_bary
        T = x.T
    elif suffix == "par":
        x = parfile.psr_par(filenm)
        # Try to see how many freq derivs we have
        fs = [x.F0]
        for ii in range(1, 20):  # hopefully 20 is an upper limit!
            attrib = "F%d" % ii
            if hasattr(x, attrib):
                fs.append(getattr(x, attrib))
            else:
                break
        epoch = x.PEPOCH
        T = (x.FINISH - x.START) * 86400.0
    else:
        print "I don't recognize the file type for", filenm
        sys.exit()
    newts = epoch + num.arange(int(T / 10.0 + 0.5), dtype=num.float) / 8640.0
    time = num.concatenate((time, newts))
    newps = 1.0 / pu.calc_freq(newts, epoch, *fs)
    period = num.concatenate((period, newps))
    print "%13.7f (%0.1f sec): " % (epoch, T), fs
 def test_testUfuncs1 (self):
     "Test various functions such as sin, cos."
     (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
     self.assertTrue (eq(numpy.cos(x), cos(xm)))
     self.assertTrue (eq(numpy.cosh(x), cosh(xm)))
     self.assertTrue (eq(numpy.sin(x), sin(xm)))
     self.assertTrue (eq(numpy.sinh(x), sinh(xm)))
     self.assertTrue (eq(numpy.tan(x), tan(xm)))
     self.assertTrue (eq(numpy.tanh(x), tanh(xm)))
     olderr = numpy.seterr(divide='ignore', invalid='ignore')
     try:
         self.assertTrue (eq(numpy.sqrt(abs(x)), sqrt(xm)))
         self.assertTrue (eq(numpy.log(abs(x)), log(xm)))
         self.assertTrue (eq(numpy.log10(abs(x)), log10(xm)))
     finally:
         numpy.seterr(**olderr)
     self.assertTrue (eq(numpy.exp(x), exp(xm)))
     self.assertTrue (eq(numpy.arcsin(z), arcsin(zm)))
     self.assertTrue (eq(numpy.arccos(z), arccos(zm)))
     self.assertTrue (eq(numpy.arctan(z), arctan(zm)))
     self.assertTrue (eq(numpy.arctan2(x, y), arctan2(xm, ym)))
     self.assertTrue (eq(numpy.absolute(x), absolute(xm)))
     self.assertTrue (eq(numpy.equal(x, y), equal(xm, ym)))
     self.assertTrue (eq(numpy.not_equal(x, y), not_equal(xm, ym)))
     self.assertTrue (eq(numpy.less(x, y), less(xm, ym)))
     self.assertTrue (eq(numpy.greater(x, y), greater(xm, ym)))
     self.assertTrue (eq(numpy.less_equal(x, y), less_equal(xm, ym)))
     self.assertTrue (eq(numpy.greater_equal(x, y), greater_equal(xm, ym)))
     self.assertTrue (eq(numpy.conjugate(x), conjugate(xm)))
     self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((xm, ym))))
     self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((x, y))))
     self.assertTrue (eq(numpy.concatenate((x, y)), concatenate((xm, y))))
     self.assertTrue (eq(numpy.concatenate((x, y, x)), concatenate((x, ym, x))))
def runLabeling(file_path, gps_filename, output_name, frames_to_skip, final_frame, lp, rp, pickle_loc):
    video_reader = WarpedVideoReader(file_path)
    #video_reader.setSubsample(True)
    video_reader.setPerspectives(pickle_loc)
    gps_reader = GPSReader(gps_filename)
    gps_dat = gps_reader.getNumericData()

    cam = getCameraParams()
    cam_to_use = cam[int(output_name[-1]) - 1]

    lp = pixelTo3d(lp, cam_to_use)
    rp = pixelTo3d(rp, cam_to_use)
    tr = GPSTransforms(gps_dat, cam_to_use)
    pitch = -cam_to_use['rot_x']
    height = 1.106
    R_camera_pitch = euler_matrix(cam_to_use['rot_x'], cam_to_use['rot_y'], cam_to_use['rot_z'], 'sxyz')[0:3, 0:3]
    Tc = np.eye(4)
    Tc[0:3, 0:3] = R_camera_pitch.transpose()
    Tc[0:3, 3] = [-0.2, -height, -0.5]
    lpts = np.zeros((lp.shape[0], 4))
    rpts = np.zeros((rp.shape[0], 4))
    for t in range(min(tr.shape[0], lp.shape[0])):
        lpts[t, :] = np.dot(tr[t, :, :], np.linalg.solve(Tc, np.array([lp[t, 0], lp[t, 1], lp[t, 2], 1])))
        rpts[t, :] = np.dot(tr[t, :, :], np.linalg.solve(Tc, np.array([rp[t, 0], rp[t, 1], rp[t, 2], 1])))

    ldist = np.apply_along_axis(np.linalg.norm, 1, np.concatenate((np.array([[0, 0, 0, 0]]), lpts[1:] - lpts[0:-1])))
    rdist = np.apply_along_axis(np.linalg.norm, 1, np.concatenate((np.array([[0, 0, 0, 0]]), rpts[1:] - rpts[0:-1])))
    start_frame = frames_to_skip
    runBatch(video_reader, gps_dat, cam_to_use, output_name, start_frame, final_frame, lpts, rpts, ldist, rdist, tr)

    print "Done with %s" % output_name
def Haffine_from_points(fp, tp):
    '''计算仿射变换的单应性矩阵H,使得tp是由fp经过仿射变换得到的'''
    if fp.shape != tp.shape:
        raise RuntimeError('number of points do not match')

    # 对点进行归一化
    # 映射起始点
    m = numpy.mean(fp[:2], axis=1)
    maxstd = numpy.max(numpy.std(fp[:2], axis=1)) + 1e-9
    C1 = numpy.diag([1/maxstd, 1/maxstd, 1])
    C1[0, 2] = -m[0] / maxstd
    C1[1, 2] = -m[1] / maxstd
    fp_cond = numpy.dot(C1, fp)

    # 映射对应点
    m = numpy.mean(tp[:2], axis=1)
    maxstd = numpy.max(numpy.std(tp[:2], axis=1)) + 1e-9
    C2 = numpy.diag([1/maxstd, 1/maxstd, 1])
    C2[0, 2] = -m[0] / maxstd
    C2[1, 2] = -m[1] / maxstd
    tp_cond = numpy.dot(C2, tp)

    # 因为归一化之后点的均值为0,所以平移量为0
    A = numpy.concatenate((fp_cond[:2], tp_cond[:2]), axis=0)
    U, S, V = numpy.linalg.svd(A.T)
    # 创建矩阵B和C
    tmp = V[:2].T
    B = tmp[:2]
    C = tmp[2:4]

    tmp2 = numpy.concatenate((numpy.dot(C, numpy.linalg.pinv(B)), numpy.zeros((2, 1))), axis=1)
    H = numpy.vstack((tmp2, [0, 0, 1]))

    H = numpy.dot(numpy.linalg.inv(C2), numpy.dot(H, C1))  # 反归一化
    return H / H[2, 2]  # 归一化,然后返回
Exemple #8
0
def extractFeatures(ind):
    global featSeq
    global labelSeq
    global position
    global nS, nO
    Fs, data = wavfile.read('DataSet/timitData/' + featSeq[ind] + '.wav')
    data_len = len(data)

    trainFeature = data[0:nStack * nS]
    minLen = 0
    maxLen = nS
    featMat = np.array(list(data[minLen:maxLen]))
    labelMat = np.array([-1])
    minLen = minLen + nO
    maxLen = maxLen + nO
    while maxLen < len(data):
        featMat = np.concatenate((featMat,list(data[minLen:maxLen])))
        minLen = minLen + nO
        maxLen = maxLen + nO
    featMat_len = featMat.size / nS
    begin = featMat[0:nS]
    end = featMat[-nS:]
    for i in range((nStack-1)/2):
        featMat = np.concatenate((begin,featMat))
        featMat = np.concatenate((featMat,end))
    for i in range(featMat_len):
        trainFeature = np.concatenate((trainFeature, list(featMat[(i)*nS:(i + nStack)*nS])))
        labelMat = np.concatenate((labelMat, [int(labelSeq[position + i][2])]))
    trainFeature = trainFeature[nStack * nS:]
    labelMat = labelMat[1:]
    position += featMat_len

    return trainFeature, labelMat
def get_diffs_bw(detect_buffer):
  temp_buff = detect_buffer
  temp_buff_size = temp_buff.shape[0]
  diff_buff = None
  count = 0

  #Store contents of detect_buffer into a temp array
  #absdiff every third capture and throw it into a new temp array
  #Repeat until temp array has a size of 2, then return the bitwise and.
  while True:
    if temp_buff_size == 2:
      cummulativeFrames = cv2.bitwise_and(temp_buff[0], temp_buff[1])
      break
    else:
      count = 0 
      while count < (temp_buff_size / 3):

        diff1 = np.array([cv2.absdiff(temp_buff[(count*3)+2], temp_buff[(count*3)+1])])
        diff2 = np.array([cv2.absdiff(temp_buff[(count*3)+1], temp_buff[(count*3)+0])])

        if diff_buff == None:
          diff_buff = np.concatenate((diff1,diff2),axis=0)
        else:
          diff_buff = np.concatenate((diff_buff,diff1,diff2),axis=0)

        count+=1
    temp_buff = diff_buff 
    diff_buff = None
    temp_buff_size = temp_buff.shape[0]

  return cummulativeFrames
Exemple #10
0
    def merge(name,sublattices,vectors=(),neighbours=1):
        '''
        Merge sublattices into a new lattice.

        Parameters
        ----------
        name : str
            The name of the new lattice.
        sublattices : list of Lattice
            The sublattices of the new lattice.
        vectors : list of 1d ndarray, optional
            The translation vectors of the new lattice.
        neighbours : dict, optional
            The neighbour-length map of the lattice.

        Returns
        -------
        Lattice
            The merged lattice.
        '''
        return Lattice(
                    name=           name,
                    pids=           [pid for lattice in sublattices for pid in lattice.pids],
                    rcoords=        np.concatenate([lattice.rcoords for lattice in sublattices]),
                    icoords=        np.concatenate([lattice.icoords for lattice in sublattices]),
                    vectors=        vectors,
                    neighbours=     neighbours
                    )
Exemple #11
0
    def _basic_insertion(self, celltype):
        # generate a list of which cell each point in self._data belongs in
        cell_indices = self._get_indices_for_points(self._data)

        # We now look for ranges of points belonging to the same cell.
        # 1. shift lengthwise and difference; runs of cells with the same
        # (i,j) indices will be zero, and nonzero value for i or j will
        # indicate a transition to a new cell.  (Just like find_runs().)
        differences = cell_indices[1:] - cell_indices[:-1]

        # Since nonzero() only works for 1D arrays, we merge the X and Y columns
        # together to detect any point where either X or Y are nonzero.  We have
        # to add 1 because we shifted cell_indices before differencing (above).
        diff_indices = nonzero(differences[:,0] + differences[:,1])[0] + 1

        start_indices = concatenate([[0], diff_indices])
        end_indices = concatenate([diff_indices, [len(self._data)]])

        for start,end in zip(start_indices, end_indices):
            gridx, gridy = cell_indices[start]  # can use 'end' here just as well
            if celltype == RangedCell:
                self._cellgrid[gridx,gridy].add_ranges([(start,end)])
            else:
                self._cellgrid[gridx,gridy].add_indices(range(start,end))
        return
def mnist_elm(n_hidden=50, domain=[-1., 1.]):
    print "hidden:", n_hidden

    # initialize
    train_set, valid_set, test_set = load_mnist()
    train_data, train_target = train_set
    valid_data, valid_target = valid_set
    test_data, test_target = test_set
    
    # size
    train_size = 50000 # max 50000
    valid_size = 10000 # max 10000
    test_size = 10000 # max 10000

    train_data, train_target = train_data[:train_size], train_target[:train_size]
    valid_data, valid_target = valid_data[:valid_size], valid_target[:valid_size]
    test_data, test_target = test_data[:test_size], test_target[:test_size]

    # train = train + valid
    train_data = np.concatenate((train_data, valid_data))
    train_target = np.concatenate((train_target, valid_target))
    
    # model
    model = ELMClassifier(n_hidden = n_hidden, domain = domain)

    # fit
    #print "fitting ..."
    model.fit(train_data, train_target)

    # test
    print "test score is ",
    score = model.score(test_data, test_target)
    print score
Exemple #13
0
    def get_points(self, peak_shape=triangle, num_discrete=10):
        """
        Returns two lists of coordinates x y representing the whole spectrum, both the continuous and discrete components.
        The mesh is chosen by extending x to include details of the discrete peaks.

        Args:
            peak_shape: The window function used to calculate the peaks. See :obj:`triangle` for an example.
            num_discrete: Number of points that are added to mesh in each peak.

        Returns:
            (tuple): tuple containing:

                x2 (List[float]): The list of x coordinates (energy) in the whole spectrum.

                y2 (List[float]): The list of y coordinates (density) in the whole spectrum.

        """
        if peak_shape is None or self.discrete == []:
            return self.x[:], self.y[:]
        # A mesh for each discrete component:
        discrete_mesh = np.concatenate(list(map(lambda x: np.linspace(
            x[0] - x[2], x[0] + x[2], num=num_discrete, endpoint=True), self.discrete)))
        x2 = sorted(np.concatenate((discrete_mesh, self.x)))
        f = self.get_continuous_function()
        peak = np.vectorize(peak_shape)

        def g(x):
            t = 0
            for l in self.discrete:
                t += peak(x, loc=l[0], size=l[2]) * l[1]
            return t

        y2 = [f(x) + g(x) for x in x2]
        return x2, y2
Exemple #14
0
def prepare_data(data_x, data_mask, data_y):
    '''
    将数据分为训练集,验证集和测试集

    注意,因为要进行hstack, 行向量会变为列向量
    '''
    data_len = len(data_y)
    train_end = numpy.floor(data_len * 0.5)
    test_end = numpy.floor(data_len * 0.8)

    if data_x.ndim == 1:
        data_x.resize((data_x.shape[0],1))
    if data_mask != []  and data_mask.ndim == 1:
        data_mask.resize((data_mask.shape[0],1))
    if data_y.ndim == 1:
        data_y.resize((data_y.shape[0],1))

    if data_mask == []:
        allData = numpy.concatenate((data_x,data_y), axis=1)
    else:
        allData = numpy.concatenate((data_x,data_mask,data_y), axis=1)

    train_data = allData[:train_end,...]
    test_data = allData[train_end:test_end,...]
    valid_data = allData[test_end:,...]

    return train_data, valid_data, test_data 
Exemple #15
0
def orthonormal_VanillaLSTMBuilder(lstm_layers, input_dims, lstm_hiddens, dropout_x=0., dropout_h=0., debug=False):
    """Build a standard LSTM cell, with variational dropout,
    with weights initialized to be orthonormal (https://arxiv.org/abs/1312.6120)

    Parameters
    ----------
    lstm_layers : int
        Currently only support one layer
    input_dims : int
        word vector dimensions
    lstm_hiddens : int
        hidden size
    dropout_x : float
        dropout on inputs, not used in this implementation, see `biLSTM` below
    dropout_h : float
        dropout on hidden states
    debug : bool
        set to True to skip orthonormal initialization

    Returns
    -------
    lstm_cell : VariationalDropoutCell
        A LSTM cell
    """
    assert lstm_layers == 1, 'only accept one layer lstm'
    W = orthonormal_initializer(lstm_hiddens, lstm_hiddens + input_dims, debug)
    W_h, W_x = W[:, :lstm_hiddens], W[:, lstm_hiddens:]
    b = nd.zeros((4 * lstm_hiddens,))
    b[lstm_hiddens:2 * lstm_hiddens] = -1.0
    lstm_cell = rnn.LSTMCell(input_size=input_dims, hidden_size=lstm_hiddens,
                             i2h_weight_initializer=mx.init.Constant(np.concatenate([W_x] * 4, 0)),
                             h2h_weight_initializer=mx.init.Constant(np.concatenate([W_h] * 4, 0)),
                             h2h_bias_initializer=mx.init.Constant(b))
    wrapper = VariationalDropoutCell(lstm_cell, drop_states=dropout_h)
    return wrapper
Exemple #16
0
def honeycomb2square(h):
  """Transforms a honeycomb lattice into a square lattice"""
  ho = deepcopy(h) # output geometry
  g = h.geometry # geometry
  go = deepcopy(g) # output geometry
  go.a1 = - g.a1 - g.a2 
  go.a2 = g.a1 - g.a2
  go.x = np.concatenate([g.x,g.x-g.a1[0]])
  go.y = np.concatenate([g.y,g.y-g.a1[1]])
  go.z = np.concatenate([g.z,g.z])
  go.xyz2r() # update r atribbute
  zero = csc(h.tx*0.)
  # define sparse
  intra = csc(h.intra)
  tx = csc(h.tx)
  ty = csc(h.ty)
  txy = csc(h.txy)
  txmy = csc(h.txmy)
  # define new hoppings
  ho.intra = bmat([[intra,tx.H],[tx,intra]]).todense()
  ho.tx = bmat([[txy.H,zero],[ty.H,txy.H]]).todense()
  ho.ty = bmat([[txmy,ty.H],[txmy,zero]]).todense()
  ho.txy = bmat([[zero,None],[None,zero]]).todense()
  ho.txmy = bmat([[zero,zero],[tx.H,zero]]).todense()
  ho.geometry = go
  return ho
Exemple #17
0
    def display(self, xaxis, alpha, new=True):
        """
        E.display(xaxis, alpha = .8)

        :Arguments: xaxis, alpha

        Plots the CI region on the current figure, with respect to
        xaxis, at opacity alpha.

        :Note: The fill color of the envelope will be self.mass
            on the grayscale.
        """
        if new:
            figure()
        if self.ndim == 1:
            if self.mass>0.:
                x = concatenate((xaxis,xaxis[::-1]))
                y = concatenate((self.lo, self.hi[::-1]))
                fill(x,y,facecolor='%f' % self.mass,alpha=alpha, label = ('centered CI ' + str(self.mass)))
            else:
                pyplot(xaxis,self.value,'k-',alpha=alpha, label = ('median'))
        else:
            if self.mass>0.:
                subplot(1,2,1)
                contourf(xaxis[0],xaxis[1],self.lo,cmap=cm.bone)
                colorbar()
                subplot(1,2,2)
                contourf(xaxis[0],xaxis[1],self.hi,cmap=cm.bone)
                colorbar()
            else:
                contourf(xaxis[0],xaxis[1],self.value,cmap=cm.bone)
                colorbar()
def pad_array(nx,ny,nMap,pdw):
    nMap_pad = np.pad(nMap, ((pdw,pdw),(pdw,pdw)), 'constant', constant_values=0)
    dx=(nx[-1]-nx[0])/(len(nx)-1)
    nx_pad = np.concatenate((np.arange(nx[0]-pdw*dx,nx[0],dx),nx,np.arange(nx[-1],nx[-1]+pdw*dx,dx)))
    dy=(ny[-1]-ny[0])/(len(ny)-1)
    ny_pad = np.concatenate((np.arange(ny[0]-pdw*dy,ny[0],dy),ny,np.arange(ny[-1],ny[-1]+pdw*dy,dy)))
    return nx_pad,ny_pad,nMap_pad
def compute_tau(density,nf,pretty=False):
    z0,d0 = get_z_d(0,cfg.pms['zi'],proper=True)
    z1,d1 = get_z_d(cfg.pms['zi'],cfg.pms['zf'],proper=True)
    z = np.concatenate((z0,z1))
    d = np.concatenate((d0,d1))

    nb = cfg.pms['nb0']*(1+z)**3
    chiHII = np.ones_like(z)
    chiHII[len(d0):] = np.average((1.-nf)*(1+density),axis=(0,1))
    chiHeIII = np.zeros_like(chiHII)
    chiHeIII[np.where(z<3)] = 1

    fn = cfg.pms['sigT']*nb*(chiHII+0.25*chiHeIII*cfg.pms['YBBNp'])*mToMpc
    tau = sp.integrate.cumtrapz(fn,x=d,initial=0)
    
    if pretty:
        plt.plot(z,chiHII,label='chiHII')
        plt.plot(z,chiHeIII,label='chiHeIII')
        plt.ylim([0,1.1])
        plt.xlabel("z"); plt.ylabel("chi")
        plt.legend(loc='lower left'); plt.show() #bbox_to_anchor=(1.3,0.6)
        
        plt.plot(z,tau)
        plt.xlabel('z'); plt.ylabel('tau'); plt.show()

    # NOTE: This returns the proper distance!
    return z1,d1,tau[len(d0):]
Exemple #20
0
 def _submit_mapnode(self, jobid):
     if jobid in self.mapnodes:
         return True
     self.mapnodes.append(jobid)
     mapnodesubids = self.procs[jobid].get_subnodes()
     numnodes = len(mapnodesubids)
     logger.info('Adding %d jobs for mapnode %s' % (numnodes,
                                                    self.procs[jobid]._id))
     for i in range(numnodes):
         self.mapnodesubids[self.depidx.shape[0] + i] = jobid
     self.procs.extend(mapnodesubids)
     self.depidx = ssp.vstack((self.depidx,
                               ssp.lil_matrix(np.zeros(
                                   (numnodes, self.depidx.shape[1])))),
                              'lil')
     self.depidx = ssp.hstack((self.depidx,
                               ssp.lil_matrix(
                                   np.zeros((self.depidx.shape[0],
                                             numnodes)))),
                              'lil')
     self.depidx[-numnodes:, jobid] = 1
     self.proc_done = np.concatenate((self.proc_done,
                                      np.zeros(numnodes, dtype=bool)))
     self.proc_pending = np.concatenate((self.proc_pending,
                                         np.zeros(numnodes, dtype=bool)))
     return False
def test_rescale(arr, ndv, dst_dtype):
    if dst_dtype == np.__dict__['uint16']:
        assert np.array_equal(
                _rescale(arr, ndv, dst_dtype),
                np.concatenate(
                    [
                        (arr).astype(dst_dtype),
                        _simple_mask(
                            arr.astype(dst_dtype),
                            (ndv, ndv, ndv)
                        ).reshape(1, arr.shape[1], arr.shape[2])
                    ]
                )
            )
    else:
        assert np.array_equal(
                _rescale(arr, ndv, dst_dtype),
                np.concatenate(
                    [
                        (arr / 257.0).astype(dst_dtype),
                        _simple_mask(
                            arr.astype(dst_dtype),
                            (ndv, ndv, ndv)
                        ).reshape(1, arr.shape[1], arr.shape[2])
                    ]
                )
            )
Exemple #22
0
def getData():
    data_list = [
        unpickle("data/cifar-10-batches-py/data_batch_1"),
        unpickle("data/cifar-10-batches-py/data_batch_2"),
        unpickle("data/cifar-10-batches-py/data_batch_3"),
        unpickle("data/cifar-10-batches-py/data_batch_4"),
        unpickle("data/cifar-10-batches-py/data_batch_5"),
        unpickle("data/cifar-10-batches-py/test_batch"),
    ]

    # samples = np.empty_like(data_list[0]['data'].astype(np.float32))
    samples = data_list[0]["data"].astype(np.float32)
    labels = np.array(data_list[0]["labels"], dtype=np.float32)
    for idx, d in enumerate(data_list):
        if idx != 0:
            samples = np.concatenate([samples, d["data"].astype(np.float32)])
            labels = np.concatenate([labels, np.array(d["labels"], dtype=np.float32)])

    img_idxs = []
    for idx, l in enumerate(labels):
        # 3 is cat, 5 is dog
        if l == 3 or l == 5:
            img_idxs.append(idx)

    data = samples[img_idxs]
    responses = labels[img_idxs]
    print ("Number of images used: " + str(len(img_idxs)))

    for idx, l in enumerate(responses):
        if l == 3:
            responses[idx] = 0
        else:
            responses[idx] = 1

    return (data, responses)
Exemple #23
0
def test_clipping():
    exterior = mpath.Path.unit_rectangle().deepcopy()
    exterior.vertices *= 4
    exterior.vertices -= 2
    interior = mpath.Path.unit_circle().deepcopy()
    interior.vertices = interior.vertices[::-1]
    clip_path = mpath.Path(vertices=np.concatenate([exterior.vertices,
                                                    interior.vertices]),
                           codes=np.concatenate([exterior.codes,
                                                 interior.codes]))

    star = mpath.Path.unit_regular_star(6).deepcopy()
    star.vertices *= 2.6

    ax1 = plt.subplot(121)
    col = mcollections.PathCollection([star], lw=5, edgecolor='blue',
                                      facecolor='red', alpha=0.7, hatch='*')
    col.set_clip_path(clip_path, ax1.transData)
    ax1.add_collection(col)

    ax2 = plt.subplot(122, sharex=ax1, sharey=ax1)
    patch = mpatches.PathPatch(star, lw=5, edgecolor='blue', facecolor='red',
                               alpha=0.7, hatch='*')
    patch.set_clip_path(clip_path, ax2.transData)
    ax2.add_patch(patch)

    ax1.set_xlim([-3, 3])
    ax1.set_ylim([-3, 3])
Exemple #24
0
    def run_epoch(self, split, train=False, batch_size=128, return_pred=False):
        total = total_loss = 0
        func = self.model.train_on_batch if train else self.model.test_on_batch
        ids, preds, targs = [], [], []
        prog = Progbar(split.num_examples)
        for idx, X, Y, types in split.batches(batch_size):
            X.update({k: np.concatenate([v, types], axis=1) for k, v in Y.items()})
            batch_end = time()
            loss = func(X)
            prob = self.model.predict(X, verbose=0)['p_relation']
            prob *= self.typechecker.get_valid_cpu(types[:, 0], types[:, 1])
            pred = prob.argmax(axis=1)

            targ = Y['p_relation'].argmax(axis=1)
            ids.append(idx)
            targs.append(targ)
            preds.append(pred)
            total_loss += loss
            total += 1
            prog.add(idx.size, values=[('loss', loss), ('acc', np.mean(pred==targ))])
        preds = np.concatenate(preds).astype('int32')
        targs = np.concatenate(targs).astype('int32')
        ids = np.concatenate(ids).astype('int32')

        ret = {
            'f1': f1_score(targs, preds, average='micro', labels=self.labels),
            'precision': precision_score(targs, preds, average='micro', labels=self.labels),
            'recall': recall_score(targs, preds, average='micro', labels=self.labels),
            'accuracy': accuracy_score(targs, preds),
            'loss': total_loss / float(total),
            }
        if return_pred:
            ret.update({'ids': ids.tolist(), 'preds': preds.tolist(), 'targs': targs.tolist()})
        return ret
def transfer_f(dw,aas,aai,eps,deltaw,f):
    """
    Args:
    dw: size of the grid spacing
    aas=relative slowness of the signal mode
    aai=relative slowness of the idler mode
    lnl=inverse of the strength of the nonlinearity
    deltaw:  specifies the size of the frequency grid going from
    -deltaw to deltaw for each frequency
    f: shape of the pump function
    """
    ddws=np.arange(-deltaw-dw/2,deltaw+dw/2,dw)
    deltaks=aas*ddws
    ddwi=np.arange(-deltaw-dw/2,deltaw+dw/2,dw)
    deltaki=aai*ddwi
    ds=np.diag(deltaks)
    di=np.diag(deltaki)


    def ff(x,y):
        return f(x+y)
    
    v=eps*(dw)*ff(ddwi[:,None],ddws[None,:])
    G=1j*np.concatenate((np.concatenate((ds,v),axis=1),np.concatenate((-v,-di),axis=1)),axis=0)
    z=1;
    dsi=np.concatenate((deltaks,-deltaki),axis=0)
    U0=linalg.expm(-1j*np.diag(dsi)*z/2)
    GG=np.dot(np.dot(U0,linalg.expm(G)),U0)
    n=len(ddws)
    return (GG[0:n,0:n],GG[n:2*n,0:n],GG[0:n,n:2*n],GG[n:2*n,n:2*n])
Exemple #26
0
def paradata(wdir='.', type = 'phi', time = 0):
    " Read array with rank = 0 and read Np and Nq from header file "
    name = fname(wdir, type, time)
    name0 = name[0] + '.' + "%.2d" % 0
    pget(wdir,type,time)
    fhandler = open(name0)
    header = fhandler.readline().split()
    Np = int(header[5])
    Nq = int(header[6])
    size = Np*Nq
    for rank in range(size+1):
        if (rank < size):
            name_rank = name[0] + '.' + "%.2d" % rank
            print name_rank
            data = fread(name_rank)
        if (rank % Nq == 0):
            if (rank == Nq):
                datarr = datacol
                datacol = data
            elif (rank > 0):
                datarr = np.concatenate((datarr,datacol),axis=0)
                datacol = data
            else:
                datacol = data
        else:
            datacol = np.concatenate((datacol,data),axis=1)
    return datarr
Exemple #27
0
def phi_glob(i, elements, nodes, resolution_per_element=41):
    """
    Compute (x, y) coordinates of the curve y = phi_i(x),
    where i is a global node number (used for plotting, e.g.).
    Method: Run through each element and compute the pieces
    of phi_i(x) on this element in the reference coordinate
    system. Adding up the patches yields the complete phi_i(x).
    """
    x_patches = []
    phi_patches = []
    for e in range(len(elements)):
        Omega_e = (nodes[elements[e][0]], nodes[elements[e][-1]])
        local_nodes = elements[e]
        d = len(local_nodes) - 1
        X = np.linspace(-1, 1, resolution_per_element)
        if i in local_nodes:
            r = local_nodes.index(i)
            phi = phi_r(r, X, d)
            phi_patches.append(phi)
            x = affine_mapping(X, Omega_e)
            x_patches.append(x)
        else:
            # i is not a node in the element, phi_i(x)=0
            x_patches.append(Omega_e)
            phi_patches.append([0, 0])
    x = np.concatenate(x_patches)
    phi = np.concatenate(phi_patches)
    return x, phi
Exemple #28
0
def load(root_path, debug = True):
    '''
    load cifar-10 dataset
    '''
    xs = []
    ys = []
    for b in xrange(1, 6):
        file = os.path.join(root_path, 'data_batch_%d' % (b, ))
        X, y = load_batch(file)
        xs.append(X)
        ys.append(y)
    X = np.concatenate(xs)
    y = np.concatenate(ys)
    file = os.path.join(root_path, 'test_batch')
    X_test, y_test = load_batch(file)
    
    if debug:
        # As a sanity check, we print out the size of the training and test data.
        print 'Cifar-10 dataset has been loaded'
        print 'X shape', X.shape
        print 'y shape', y.shape
        print 'X_test shape', X_test.shape
        print 'y_test shape', y_test.shape

    return X, y, X_test, y_test
Exemple #29
0
def paradata_init(ldir='.', type = 'phi', dim=2):
    name = fname_init(ldir,type)
    name0 = name + '.' + "%.2d" % 0
    fhandler = open(name0)
    header = fhandler.readline().split()
    Np = int(header[0])/int(header[2])
    Nq = int(header[1])/int(header[3])
    size = Np*Nq
    for rank in range(size+1):
        if (rank < size):
            name_rank = name + '.' + "%.2d" % rank
            data = fread(name_rank)
        if (rank % Nq == 0):
            if (rank == Nq):
                datarr = datacol
                datacol = data
            elif (rank > 0):
                datarr = np.concatenate((datarr,datacol),axis=0)
                datacol = data
            else:
                datacol = data
        else:
            datacol = np.concatenate((datacol,data),axis=1)
    if (dim == 2):
        datarr = datarr.reshape(int(header[0]),int(header[1]))

    return datarr
def test_negative_binomial_generator():
    ctx = mx.context.current_context()
    for dtype in ['float16', 'float32', 'float64']:
        success_num = 2
        success_prob = 0.2
        buckets = [(-1.0, 2.5), (2.5, 5.5), (5.5, 8.5), (8.5, np.inf)]
        probs = [ss.nbinom.cdf(bucket[1], success_num, success_prob) -
                 ss.nbinom.cdf(bucket[0], success_num, success_prob) for bucket in buckets]
        generator_mx = lambda x: mx.nd.random.negative_binomial(success_num, success_prob,
                                                                shape=x, ctx=ctx, dtype=dtype).asnumpy()
        verify_generator(generator=generator_mx, buckets=buckets, probs=probs)
        generator_mx_same_seed = \
            lambda x: np.concatenate(
                [mx.nd.random.negative_binomial(success_num, success_prob, shape=x // 10, ctx=ctx, dtype=dtype).asnumpy()
                 for _ in range(10)])
        verify_generator(generator=generator_mx_same_seed, buckets=buckets, probs=probs)
        # Also test the Gamm-Poisson Mixture
        alpha = 1.0 / success_num
        mu = (1.0 - success_prob) / success_prob / alpha
        generator_mx = lambda x: mx.nd.random.generalized_negative_binomial(mu, alpha,
                                                                            shape=x, ctx=ctx, dtype=dtype).asnumpy()
        verify_generator(generator=generator_mx, buckets=buckets, probs=probs)
        generator_mx_same_seed = \
            lambda x: np.concatenate(
                [mx.nd.random.generalized_negative_binomial(mu, alpha, shape=x // 10, ctx=ctx, dtype=dtype).asnumpy()
                 for _ in range(10)])
        verify_generator(generator=generator_mx_same_seed, buckets=buckets, probs=probs)
Exemple #31
0
def main():
    # Define input file names
    EOFileName = '../param/EO_P1_L.txt'
    IOFileName = '../param/IO.txt'
    ptFileName = '../ptCloud/P1_L.txt'
    imgFileName = '../images/P1_C.jpg'
    outputFileName = 'result.txt'

    # SIFT matching parameters
    ratio = 0.8
    showResult = True

    # Use n nearest neighbor point value for image interpolation
    n = 4

    # Read point cloud information
    data = pd.read_csv(ptFileName, header=0, delim_whitespace=True)

    # Read interior/exterior orientation parameters
    IO = getIO(IOFileName)

    # XL, YL, ZL, O, P, K, SigXL, SigYL, SigZL, SigO, SigP, SigK
    EO = np.genfromtxt(EOFileName)

    # Reproject the object point to reference image plane
    x, y = getxy(IO, EO, data)
    rowColArr = xy2RowCol(IO, x, y)

    # Create KD-tree object with projected point coordinate (col, row)
    tree = spatial.cKDTree(np.roll(rowColArr, 1, axis=1))

    # Generate a false image for matching process between
    # object and image points
    targetImg = imread(imgFileName)
    height, width = targetImg.shape[:2]
    xi, yi = np.meshgrid(np.arange(width), np.arange(height))
    pts = np.dstack((xi.ravel(), yi.ravel())).reshape(-1, 2)

    distance, location = tree.query(
        pts, k=n, distance_upper_bound=1.5)
    mask = np.sum(~np.isinf(distance), axis=1) != 0

    valR = data.R[location[mask].ravel()].values.reshape(-1, n)
    valR[np.isnan(valR)] = 0
    valG = data.G[location[mask].ravel()].values.reshape(-1, n)
    valG[np.isnan(valG)] = 0
    valB = data.B[location[mask].ravel()].values.reshape(-1, n)
    valB[np.isnan(valB)] = 0
    weight = 1.0 / distance[mask]

    falseImg = np.zeros((1280, 1920, 3), dtype=np.uint8)
    falseImg[mask.reshape(xi.shape), 0] = np.sum(valR * weight, axis=1) / \
        np.sum(weight, axis=1)
    falseImg[mask.reshape(xi.shape), 1] = np.sum(valG * weight, axis=1) / \
        np.sum(weight, axis=1)
    falseImg[mask.reshape(xi.shape), 2] = np.sum(valB * weight, axis=1) / \
        np.sum(weight, axis=1)

    # Perform SIFT matching with LiDAR image and photo
    falseImgPt, imgPt = map(lambda x: x.reshape(-1, 2), match(
        falseImg,
        targetImg,
        ratio,
        show=showResult))

    # Query 3D coordinates with nearest false image points
    dis, loc = tree.query(falseImgPt, k=1)
    imgPt = np.dstack((allDist(imgPt[:, 1], imgPt[:, 0], IO))).reshape(-1, 2)
    objPt = data[['X', 'Y', 'Z']].values[loc]

    ptSet = np.concatenate((imgPt, objPt), axis=1)

    # Write out the result
    np.savetxt(
        outputFileName,
        pd.DataFrame(ptSet).drop_duplicates().values,
        fmt="Pt %.6f %.6f %.6f %.6f %.6f" + " 0.005" * 3,
        header=str(IO['f']),
        comments='')
Exemple #32
0
def dataset_generator(target_dir,
                      normal_dir_name="normal",
                      abnormal_dir_name="abnormal",
                      ext="wav"):
    """
    target_dir : str
        base directory path of the dataset
    normal_dir_name : str (default="normal")
        directory name the normal data located in
    abnormal_dir_name : str (default="abnormal")
        directory name the abnormal data located in
    ext : str (default="wav")
        filename extension of audio files 

    return : 
        train_data : numpy.array( numpy.array( float ) )
            training dataset
            * dataset.shape = (total_dataset_size, fearture_vector_length)
        train_files : list [ str ]
            file list for training
        train_labels : list [ boolean ] 
            label info. list for training
            * normal/abnnormal = 0/1
        eval_files : list [ str ]
            file list for evaluation
        eval_labels : list [ boolean ] 
            label info. list for evaluation
            * normal/abnnormal = 0/1
    """
    logger.info("target_dir : {}".format(target_dir))

    # 01 normal list generate
    normal_files = sorted(
        glob.glob("{dir}/{normal_dir_name}/*.{ext}".format(
            dir=target_dir, normal_dir_name=normal_dir_name, ext=ext)))
    normal_labels = numpy.zeros(len(normal_files))
    if len(normal_files) == 0: logger.exception(f'{"no_wav_data!!"}')

    # 02 abnormal list generate
    abnormal_files = sorted(
        glob.glob("{dir}/{abnormal_dir_name}/*.{ext}".format(
            dir=target_dir, abnormal_dir_name=abnormal_dir_name, ext=ext)))
    abnormal_labels = numpy.ones(len(abnormal_files))
    if len(abnormal_files) == 0: logger.exception(f'{"no_wav_data!!"}')
    # 03 separate train & eval
    train_files = normal_files[len(abnormal_files):]
    train_labels = normal_labels[len(abnormal_files):]
    '''MODIFICATION'''
    num_val = round(len(train_files) * (10) / 100)
    val_files = normal_files[:num_val]
    val_labels = normal_labels[:num_val]
    '''-----------'''
    eval_files = numpy.concatenate(
        (normal_files[:len(abnormal_files)], abnormal_files), axis=0)
    eval_labels = numpy.concatenate(
        (normal_labels[:len(abnormal_files)], abnormal_labels), axis=0)

    logger.info("train_file num : {num}".format(num=len(train_files)))
    '''MODIFICATION'''
    logger.info("val_file num : {num}".format(num=len(val_files)))
    '''-----------'''
    logger.info("eval_file  num : {num}".format(num=len(eval_files)))
    '''MODIFICATION'''
    return train_files, train_labels, val_files, val_labels, eval_files, eval_labels
    def __call__(self, samples, context=None):
        sample_id = 0
        for sample in samples:
            gt_bboxes_raw = sample['gt_bbox']
            gt_labels_raw = sample['gt_class']
            im_c, im_h, im_w = sample['image'].shape[:]
            gt_masks_raw = sample['gt_segm'].astype(np.uint8)
            mask_feat_size = [
                int(im_h / self.sampling_ratio),
                int(im_w / self.sampling_ratio)
            ]
            gt_areas = np.sqrt((gt_bboxes_raw[:, 2] - gt_bboxes_raw[:, 0]) *
                               (gt_bboxes_raw[:, 3] - gt_bboxes_raw[:, 1]))
            ins_ind_label_list = []
            idx = 0
            for (lower_bound, upper_bound), num_grid \
                    in zip(self.scale_ranges, self.num_grids):

                hit_indices = ((gt_areas >= lower_bound) &
                               (gt_areas <= upper_bound)).nonzero()[0]
                num_ins = len(hit_indices)

                ins_label = []
                grid_order = []
                cate_label = np.zeros([num_grid, num_grid], dtype=np.int64)
                ins_ind_label = np.zeros([num_grid**2], dtype=np.bool)

                if num_ins == 0:
                    ins_label = np.zeros(
                        [1, mask_feat_size[0], mask_feat_size[1]],
                        dtype=np.uint8)
                    ins_ind_label_list.append(ins_ind_label)
                    sample['cate_label{}'.format(idx)] = cate_label.flatten()
                    sample['ins_label{}'.format(idx)] = ins_label
                    sample['grid_order{}'.format(idx)] = np.asarray(
                        [sample_id * num_grid * num_grid + 0])
                    idx += 1
                    continue
                gt_bboxes = gt_bboxes_raw[hit_indices]
                gt_labels = gt_labels_raw[hit_indices]
                gt_masks = gt_masks_raw[hit_indices, ...]

                half_ws = 0.5 * (gt_bboxes[:, 2] -
                                 gt_bboxes[:, 0]) * self.coord_sigma
                half_hs = 0.5 * (gt_bboxes[:, 3] -
                                 gt_bboxes[:, 1]) * self.coord_sigma

                for seg_mask, gt_label, half_h, half_w in zip(
                        gt_masks, gt_labels, half_hs, half_ws):
                    if seg_mask.sum() == 0:
                        continue
                    # mass center
                    upsampled_size = (mask_feat_size[0] * 4,
                                      mask_feat_size[1] * 4)
                    center_h, center_w = ndimage.measurements.center_of_mass(
                        seg_mask)
                    coord_w = int(
                        (center_w / upsampled_size[1]) // (1. / num_grid))
                    coord_h = int(
                        (center_h / upsampled_size[0]) // (1. / num_grid))

                    # left, top, right, down
                    top_box = max(
                        0,
                        int(((center_h - half_h) / upsampled_size[0]) //
                            (1. / num_grid)))
                    down_box = min(
                        num_grid - 1,
                        int(((center_h + half_h) / upsampled_size[0]) //
                            (1. / num_grid)))
                    left_box = max(
                        0,
                        int(((center_w - half_w) / upsampled_size[1]) //
                            (1. / num_grid)))
                    right_box = min(
                        num_grid - 1,
                        int(((center_w + half_w) / upsampled_size[1]) //
                            (1. / num_grid)))

                    top = max(top_box, coord_h - 1)
                    down = min(down_box, coord_h + 1)
                    left = max(coord_w - 1, left_box)
                    right = min(right_box, coord_w + 1)

                    cate_label[top:(down + 1), left:(right + 1)] = gt_label
                    seg_mask = self._scale_size(seg_mask,
                                                scale=1. / self.sampling_ratio)
                    for i in range(top, down + 1):
                        for j in range(left, right + 1):
                            label = int(i * num_grid + j)
                            cur_ins_label = np.zeros(
                                [mask_feat_size[0], mask_feat_size[1]],
                                dtype=np.uint8)
                            cur_ins_label[:seg_mask.shape[0], :seg_mask.
                                          shape[1]] = seg_mask
                            ins_label.append(cur_ins_label)
                            ins_ind_label[label] = True
                            grid_order.append(
                                [sample_id * num_grid * num_grid + label])
                if ins_label == []:
                    ins_label = np.zeros(
                        [1, mask_feat_size[0], mask_feat_size[1]],
                        dtype=np.uint8)
                    ins_ind_label_list.append(ins_ind_label)
                    sample['cate_label{}'.format(idx)] = cate_label.flatten()
                    sample['ins_label{}'.format(idx)] = ins_label
                    sample['grid_order{}'.format(idx)] = np.asarray(
                        [sample_id * num_grid * num_grid + 0])
                else:
                    ins_label = np.stack(ins_label, axis=0)
                    ins_ind_label_list.append(ins_ind_label)
                    sample['cate_label{}'.format(idx)] = cate_label.flatten()
                    sample['ins_label{}'.format(idx)] = ins_label
                    sample['grid_order{}'.format(idx)] = np.asarray(grid_order)
                    assert len(grid_order) > 0
                idx += 1
            ins_ind_labels = np.concatenate([
                ins_ind_labels_level_img
                for ins_ind_labels_level_img in ins_ind_label_list
            ])
            fg_num = np.sum(ins_ind_labels)
            sample['fg_num'] = fg_num
            sample_id += 1

        return samples
    def __call__(self, samples, context=None):
        assert len(self.object_sizes_of_interest) == len(self.downsample_ratios), \
            "object_sizes_of_interest', and 'downsample_ratios' should have same length."

        for sample in samples:
            # im, gt_bbox, gt_class, gt_score = sample
            im = sample['image']
            im_info = sample['im_info']
            bboxes = sample['gt_bbox']
            gt_class = sample['gt_class']
            gt_score = sample['gt_score']
            bboxes[:, [0, 2]] = bboxes[:, [0, 2]] * np.floor(im_info[1]) / \
                np.floor(im_info[1] / im_info[2])
            bboxes[:, [1, 3]] = bboxes[:, [1, 3]] * np.floor(im_info[0]) / \
                np.floor(im_info[0] / im_info[2])
            # calculate the locations
            h, w = sample['image'].shape[1:3]
            points, num_points_each_level = self._compute_points(w, h)
            object_scale_exp = []
            for i, num_pts in enumerate(num_points_each_level):
                object_scale_exp.append(
                    np.tile(np.array([self.object_sizes_of_interest[i]]),
                            reps=[num_pts, 1]))
            object_scale_exp = np.concatenate(object_scale_exp, axis=0)

            gt_area = (bboxes[:, 2] - bboxes[:, 0]) * (bboxes[:, 3] -
                                                       bboxes[:, 1])
            xs, ys = points[:, 0], points[:, 1]
            xs = np.reshape(xs, newshape=[xs.shape[0], 1])
            xs = np.tile(xs, reps=[1, bboxes.shape[0]])
            ys = np.reshape(ys, newshape=[ys.shape[0], 1])
            ys = np.tile(ys, reps=[1, bboxes.shape[0]])

            l_res = xs - bboxes[:, 0]
            r_res = bboxes[:, 2] - xs
            t_res = ys - bboxes[:, 1]
            b_res = bboxes[:, 3] - ys
            reg_targets = np.stack([l_res, t_res, r_res, b_res], axis=2)
            if self.center_sampling_radius > 0:
                is_inside_box = self._check_inside_boxes_limited(
                    bboxes, xs, ys, num_points_each_level)
            else:
                is_inside_box = np.min(reg_targets, axis=2) > 0
            # check if the targets is inside the corresponding level
            max_reg_targets = np.max(reg_targets, axis=2)
            lower_bound = np.tile(np.expand_dims(object_scale_exp[:, 0],
                                                 axis=1),
                                  reps=[1, max_reg_targets.shape[1]])
            high_bound = np.tile(np.expand_dims(object_scale_exp[:, 1],
                                                axis=1),
                                 reps=[1, max_reg_targets.shape[1]])
            is_match_current_level = \
                (max_reg_targets > lower_bound) & \
                (max_reg_targets < high_bound)
            points2gtarea = np.tile(np.expand_dims(gt_area, axis=0),
                                    reps=[xs.shape[0], 1])
            points2gtarea[is_inside_box == 0] = self.INF
            points2gtarea[is_match_current_level == 0] = self.INF
            points2min_area = points2gtarea.min(axis=1)
            points2min_area_ind = points2gtarea.argmin(axis=1)
            labels = gt_class[points2min_area_ind] + 1
            labels[points2min_area == self.INF] = 0
            reg_targets = reg_targets[range(xs.shape[0]), points2min_area_ind]
            ctn_targets = np.sqrt((reg_targets[:, [0, 2]].min(axis=1) / \
                                  reg_targets[:, [0, 2]].max(axis=1)) * \
                                  (reg_targets[:, [1, 3]].min(axis=1) / \
                                   reg_targets[:, [1, 3]].max(axis=1))).astype(np.float32)
            ctn_targets = np.reshape(ctn_targets,
                                     newshape=[ctn_targets.shape[0], 1])
            ctn_targets[labels <= 0] = 0
            pos_ind = np.nonzero(labels != 0)
            reg_targets_pos = reg_targets[pos_ind[0], :]
            split_sections = []
            beg = 0
            for lvl in range(len(num_points_each_level)):
                end = beg + num_points_each_level[lvl]
                split_sections.append(end)
                beg = end
            labels_by_level = np.split(labels, split_sections, axis=0)
            reg_targets_by_level = np.split(reg_targets,
                                            split_sections,
                                            axis=0)
            ctn_targets_by_level = np.split(ctn_targets,
                                            split_sections,
                                            axis=0)
            for lvl in range(len(self.downsample_ratios)):
                grid_w = int(np.ceil(w / self.downsample_ratios[lvl]))
                grid_h = int(np.ceil(h / self.downsample_ratios[lvl]))
                if self.norm_reg_targets:
                    sample['reg_target{}'.format(lvl)] = \
                        np.reshape(
                            reg_targets_by_level[lvl] / \
                            self.downsample_ratios[lvl],
                            newshape=[grid_h, grid_w, 4])
                else:
                    sample['reg_target{}'.format(lvl)] = np.reshape(
                        reg_targets_by_level[lvl],
                        newshape=[grid_h, grid_w, 4])
                sample['labels{}'.format(lvl)] = np.reshape(
                    labels_by_level[lvl], newshape=[grid_h, grid_w, 1])
                sample['centerness{}'.format(lvl)] = np.reshape(
                    ctn_targets_by_level[lvl], newshape=[grid_h, grid_w, 1])
        return samples
 def filter(self, mode, *args):
     """Applies a filter to the image.
     The existant filters are: GRAY, INVERT, OPAQUE, THRESHOLD, POSTERIZE,
     ERODE, DILATE and BLUR. This method requires numpy."""
     if not npy: raise ImportError("Numpy is required")
     if mode == GRAY:
         # Gray value = (77*(n>>16&0xff) + 151*(n>>8&0xff) + 28*(n&0xff)) >> 8
         # Where n is the ARGB color of the pixel
         lum1 = numpy.multiply(
             numpy.bitwise_and(numpy.right_shift(self.pixels, 16), 0xff),
             77)
         lum2 = numpy.multiply(
             numpy.bitwise_and(numpy.right_shift(self.pixels, 8), 0xff),
             151)
         lum3 = numpy.multiply(numpy.bitwise_and(self.pixels, 0xff), 28)
         lum = numpy.right_shift(numpy.add(numpy.add(lum1, lum2), lum3), 8)
         self.pixels = numpy.bitwise_and(self.pixels, 0xff000000)
         self.pixels = numpy.bitwise_or(self.pixels,
                                        numpy.left_shift(lum, 16))
         self.pixels = numpy.bitwise_or(self.pixels,
                                        numpy.left_shift(lum, 8))
         self.pixels = numpy.bitwise_or(self.pixels, lum)
     elif mode == INVERT:
         # This is the same as applying an exclusive or with the maximum value
         self.pixels = numpy.bitwise_xor(self.pixels, 0xffffff)
     elif mode == BLUR:
         if not args: args = [3]
         # Makes the image square by adding zeros.
         # This avoids the convolution (via fourier transform multiplication)
         # from jumping to another extreme of the image when a border is reached
         if self.width > self.height:
             dif = self.width - self.height
             updif = numpy.zeros(self.width * dif / 2, dtype=numpy.uint32)
             downdif = numpy.zeros(self.width * (dif - dif / 2),
                                   dtype=numpy.uint32)
             self.pixels = numpy.concatenate((updif, self.pixels, downdif))
             size = self.width
         elif self.width < self.height:
             dif = self.height - self.width
             leftdif = numpy.zeros(self.height * dif / 2,
                                   dtype=numpy.uint32)
             rightdif = numpy.zeros(self.height * (dif - dif / 2),
                                    dtype=numpy.uint32)
             self.pixels = self.pixels.reshape(self.height, self.width)
             self.pixels = numpy.transpose(self.pixels)
             self.pixels = self.pixels.reshape(self.width * self.height)
             self.pixels = numpy.concatenate(
                 (leftdif, self.pixels, rightdif))
             self.pixels = self.pixels.reshape(self.height, self.height)
             self.pixels = numpy.transpose(self.pixels)
             self.pixels = self.pixels.reshape(self.height * self.height)
             size = self.height
         else:
             size = self.height
         # Creates a gaussian kernel of the image's size
         _createKernel2d(args[0], size)
         # Divides the image's R, G and B channels, reshapes them
         # to square matrixes and applies two dimensional fourier transforms
         red = numpy.bitwise_and(numpy.right_shift(self.pixels, 16), 0xff)
         red = numpy.reshape(red, (size, size))
         red = numpy.fft.fft2(red)
         green = numpy.bitwise_and(numpy.right_shift(self.pixels, 8), 0xff)
         green = numpy.reshape(green, (size, size))
         green = numpy.fft.fft2(green)
         blue = numpy.bitwise_and(self.pixels, 0xff)
         blue = numpy.reshape(blue, (size, size))
         blue = numpy.fft.fft2(blue)
         # Does a element-wise multiplication of each channel matrix
         # and the fourier transform of the kernel matrix
         kernel = numpy.fft.fft2(weights)
         red = numpy.multiply(red, kernel)
         green = numpy.multiply(green, kernel)
         blue = numpy.multiply(blue, kernel)
         # Reshapes them back to arrays and converts to unsigned integers
         red = numpy.reshape(numpy.fft.ifft2(red).real, size * size)
         green = numpy.reshape(numpy.fft.ifft2(green).real, size * size)
         blue = numpy.reshape(numpy.fft.ifft2(blue).real, size * size)
         red = red.astype(numpy.uint32)
         green = green.astype(numpy.uint32)
         blue = blue.astype(numpy.uint32)
         self.pixels = numpy.bitwise_or(numpy.left_shift(green, 8), blue)
         self.pixels = numpy.bitwise_or(numpy.left_shift(red, 16),
                                        self.pixels)
         # Crops out the zeros added
         if self.width > self.height:
             self.pixels = self.pixels[self.width * dif / 2:size * size -
                                       self.width * (dif - dif / 2)]
         elif self.width < self.height:
             self.pixels = numpy.reshape(self.pixels, (size, size))
             self.pixels = numpy.transpose(self.pixels)
             self.pixels = numpy.reshape(self.pixels, size * size)
             self.pixels = self.pixels[self.height * dif / 2:size * size -
                                       self.height * (dif - dif / 2)]
             self.pixels = numpy.reshape(self.pixels,
                                         (self.width, self.height))
             self.pixels = numpy.transpose(self.pixels)
             self.pixels = numpy.reshape(self.pixels,
                                         self.height * self.width)
     elif mode == OPAQUE:
         # This is the same as applying an bitwise or with the maximum value
         self.pixels = numpy.bitwise_or(self.pixels, 0xff000000)
     elif mode == THRESHOLD:
         # Maximum = max((n & 0xff0000) >> 16, max((n & 0xff00)>>8, (n & 0xff)))
         # Broken down to Maximum = max(aux,aux2)
         # The pixel will be white if its maximum is greater than the threshold
         # value, and black if not. This was implemented via a boolean matrix
         # multiplication.
         if not args:
             args = [0.5]
         thresh = args[0] * 255
         aux = numpy.right_shift(numpy.bitwise_and(self.pixels, 0xff00), 8)
         aux = numpy.maximum(aux, numpy.bitwise_and(self.pixels, 0xff))
         aux2 = numpy.right_shift(numpy.bitwise_and(self.pixels, 0xff0000),
                                  16)
         boolmatrix = numpy.greater_equal(numpy.maximum(aux, aux2), thresh)
         self.pixels.fill(0xffffff)
         self.pixels = numpy.multiply(self.pixels, boolmatrix)
     elif mode == POSTERIZE:
         # New channel = ((channel*level)>>8)*255/(level-1)
         if not args: args = [8]
         levels1 = args[0] - 1
         rlevel = numpy.bitwise_and(numpy.right_shift(self.pixels, 16),
                                    0xff)
         glevel = numpy.bitwise_and(numpy.right_shift(self.pixels, 8), 0xff)
         blevel = numpy.bitwise_and(self.pixels, 0xff)
         rlevel = numpy.right_shift(numpy.multiply(rlevel, args[0]), 8)
         rlevel = numpy.divide(numpy.multiply(rlevel, 255), levels1)
         glevel = numpy.right_shift(numpy.multiply(glevel, args[0]), 8)
         glevel = numpy.divide(numpy.multiply(glevel, 255), levels1)
         blevel = numpy.right_shift(numpy.multiply(blevel, args[0]), 8)
         blevel = numpy.divide(numpy.multiply(blevel, 255), levels1)
         self.pixels = numpy.bitwise_and(self.pixels, 0xff000000)
         self.pixels = numpy.bitwise_or(self.pixels,
                                        numpy.left_shift(rlevel, 16))
         self.pixels = numpy.bitwise_or(self.pixels,
                                        numpy.left_shift(glevel, 8))
         self.pixels = numpy.bitwise_or(self.pixels, blevel)
     elif mode == ERODE:
         # Checks the pixels directly above, under and to the left and right
         # of each pixel of the image. If it has a greater luminosity, then
         # the center pixel receives its color
         colorOrig = numpy.array(self.pixels)
         colOut = numpy.array(self.pixels)
         colLeft = numpy.roll(colorOrig, 1)
         colRight = numpy.roll(colorOrig, -1)
         colUp = numpy.roll(colorOrig, self.width)
         colDown = numpy.roll(colorOrig, -self.width)
         currLum1 = numpy.bitwise_and(numpy.right_shift(colorOrig, 16),
                                      0xff)
         currLum1 = numpy.multiply(currLum1, 77)
         currLum2 = numpy.bitwise_and(numpy.right_shift(colorOrig, 8), 0xff)
         currLum2 = numpy.multiply(currLum2, 151)
         currLum3 = numpy.multiply(numpy.bitwise_and(colorOrig, 0xff), 28)
         currLum = numpy.add(numpy.add(currLum1, currLum2), currLum3)
         lumLeft1 = numpy.bitwise_and(numpy.right_shift(colLeft, 16), 0xff)
         lumLeft1 = numpy.multiply(lumLeft1, 77)
         lumLeft2 = numpy.bitwise_and(numpy.right_shift(colLeft, 8), 0xff)
         lumLeft2 = numpy.multiply(lumLeft2, 151)
         lumLeft3 = numpy.multiply(numpy.bitwise_and(colLeft, 0xff), 28)
         lumLeft = numpy.add(numpy.add(lumLeft1, lumLeft2), lumLeft3)
         lumRight1 = numpy.bitwise_and(numpy.right_shift(colRight, 16),
                                       0xff)
         lumRight1 = numpy.multiply(lumRight1, 77)
         lumRight2 = numpy.bitwise_and(numpy.right_shift(colRight, 8), 0xff)
         lumRight2 = numpy.multiply(lumRight2, 151)
         lumRight3 = numpy.multiply(numpy.bitwise_and(colRight, 0xff), 28)
         lumRight = numpy.add(numpy.add(lumRight1, lumRight2), lumRight3)
         lumDown1 = numpy.bitwise_and(numpy.right_shift(colDown, 16), 0xff)
         lumDown1 = numpy.multiply(lumDown1, 77)
         lumDown2 = numpy.bitwise_and(numpy.right_shift(colDown, 8), 0xff)
         lumDown2 = numpy.multiply(lumDown2, 151)
         lumDown3 = numpy.multiply(numpy.bitwise_and(colDown, 0xff), 28)
         lumDown = numpy.add(numpy.add(lumDown1, lumDown2), lumDown3)
         lumUp1 = numpy.bitwise_and(numpy.right_shift(colUp, 16), 0xff)
         lumUp1 = numpy.multiply(lumUp1, 77)
         lumUp2 = numpy.bitwise_and(numpy.right_shift(colUp, 8), 0xff)
         lumUp2 = numpy.multiply(lumUp2, 151)
         lumUp3 = numpy.multiply(numpy.bitwise_and(colUp, 0xff), 28)
         lumUp = numpy.add(numpy.add(lumUp1, lumUp2), lumUp3)
         numpy.putmask(colOut, lumLeft > currLum, colLeft)
         numpy.putmask(currLum, lumLeft > currLum, lumLeft)
         numpy.putmask(colOut, lumRight > currLum, colRight)
         numpy.putmask(currLum, lumRight > currLum, lumRight)
         numpy.putmask(colOut, lumUp > currLum, colUp)
         numpy.putmask(currLum, lumUp > currLum, lumUp)
         numpy.putmask(colOut, lumDown > currLum, colDown)
         numpy.putmask(currLum, lumDown > currLum, lumDown)
         self.pixels = colOut
     elif mode == DILATE:
         # Checks the pixels directly above, under and to the left and right
         # of each pixel of the image. If it has a lesser luminosity, then
         # the center pixel receives its color
         colorOrig = numpy.array(self.pixels)
         colOut = numpy.array(self.pixels)
         colLeft = numpy.roll(colorOrig, 1)
         colRight = numpy.roll(colorOrig, -1)
         colUp = numpy.roll(colorOrig, self.width)
         colDown = numpy.roll(colorOrig, -self.width)
         currLum1 = numpy.bitwise_and(numpy.right_shift(colorOrig, 16),
                                      0xff)
         currLum1 = numpy.multiply(currLum1, 77)
         currLum2 = numpy.bitwise_and(numpy.right_shift(colorOrig, 8), 0xff)
         currLum2 = numpy.multiply(currLum2, 151)
         currLum3 = numpy.multiply(numpy.bitwise_and(colorOrig, 0xff), 28)
         currLum = numpy.add(numpy.add(currLum1, currLum2), currLum3)
         lumLeft1 = numpy.bitwise_and(numpy.right_shift(colLeft, 16), 0xff)
         lumLeft1 = numpy.multiply(lumLeft1, 77)
         lumLeft2 = numpy.bitwise_and(numpy.right_shift(colLeft, 8), 0xff)
         lumLeft2 = numpy.multiply(lumLeft2, 151)
         lumLeft3 = numpy.multiply(numpy.bitwise_and(colLeft, 0xff), 28)
         lumLeft = numpy.add(numpy.add(lumLeft1, lumLeft2), lumLeft3)
         lumRight1 = numpy.bitwise_and(numpy.right_shift(colRight, 16),
                                       0xff)
         lumRight1 = numpy.multiply(lumRight1, 77)
         lumRight2 = numpy.bitwise_and(numpy.right_shift(colRight, 8), 0xff)
         lumRight2 = numpy.multiply(lumRight2, 151)
         lumRight3 = numpy.multiply(numpy.bitwise_and(colRight, 0xff), 28)
         lumRight = numpy.add(numpy.add(lumRight1, lumRight2), lumRight3)
         lumDown1 = numpy.bitwise_and(numpy.right_shift(colDown, 16), 0xff)
         lumDown1 = numpy.multiply(lumDown1, 77)
         lumDown2 = numpy.bitwise_and(numpy.right_shift(colDown, 8), 0xff)
         lumDown2 = numpy.multiply(lumDown2, 151)
         lumDown3 = numpy.multiply(numpy.bitwise_and(colDown, 0xff), 28)
         lumDown = numpy.add(numpy.add(lumDown1, lumDown2), lumDown3)
         lumUp1 = numpy.bitwise_and(numpy.right_shift(colUp, 16), 0xff)
         lumUp1 = numpy.multiply(lumUp1, 77)
         lumUp2 = numpy.bitwise_and(numpy.right_shift(colUp, 8), 0xff)
         lumUp2 = numpy.multiply(lumUp2, 151)
         lumUp3 = numpy.multiply(numpy.bitwise_and(colUp, 0xff), 28)
         lumUp = numpy.add(numpy.add(lumUp1, lumUp2), lumUp3)
         numpy.putmask(colOut, lumLeft < currLum, colLeft)
         numpy.putmask(currLum, lumLeft < currLum, lumLeft)
         numpy.putmask(colOut, lumRight < currLum, colRight)
         numpy.putmask(currLum, lumRight < currLum, lumRight)
         numpy.putmask(colOut, lumUp < currLum, colUp)
         numpy.putmask(currLum, lumUp < currLum, lumUp)
         numpy.putmask(colOut, lumDown < currLum, colDown)
         numpy.putmask(currLum, lumDown < currLum, lumDown)
         self.pixels = colOut
     self.updatePixels()
def pfnn_inference_2():
    dataset = BVHDataset2(base_dir + bvh_path)
    print(dataset.in_features, dataset.out_features)
    pfnn = PFNN(dataset.in_features, dataset.out_features).float()
    pfnn.load_state_dict(torch.load('models/2_pfnn_params199.pkl'))
    bvh = dataset.bvh
    init_state = dataset.bvh.motions[0, :num_of_root_infos]
    init_angles = bvh.motion_angles[0]
    phase = 0.2
    # print(len(dataset[0]))
    trajectory = dataset[0][0][0][:num_of_root_infos *
                                  trajectory_length]
    # print(len(trajectory))
    motions = np.zeros((frames, bvh.num_of_angles+num_of_root_infos))
    angles = init_angles
    state = init_state
    # print(angles.shape)
    # print(trajectory)
    # fake_trajectory = np.zeros((trajectory_length, num_of_root_infos))
    # fake_trajectory = np.array(
    #     [[0, 0, 0.1, 0, 0, 0]
    #      for i in range(trajectory_length)])

    fake_trajectory = np.concatenate(
        [np.array([0, 0, 0.1, 0, 0, 0])*(x+1)
         for x in range(trajectory_length)], axis=0).\
        reshape(trajectory_length, num_of_root_infos)
    # print(fake_trajectory)
    # fake_trajectory[:, 1] = 0.2*delta_scale

    left_trajectory = np.concatenate(
        [np.array([0.1, 0, 0, 0, 0, 0])*(x+1)
         for x in range(trajectory_length)], axis=0).\
        reshape(trajectory_length, num_of_root_infos)
    left_trajectory[:, 0] -= 50
    left_trajectory[:, 2] += 10
    fake_trajectory = left_trajectory

    for i in range(frames):
        print('i:  ', i)
        x = torch.cat(
            (torch.tensor(fake_trajectory, dtype=torch.float32)
             .view(1, num_of_root_infos*trajectory_length),
             torch.tensor(angles, dtype=torch.float32).view(1, 90)), dim=1)
        y = pfnn(x, phase)
        # print(y.shape)
        # trajectory = y[:, :trajectory_length*num_of_root_infos]
        # trajectory = trajectory.view((trajectory_length, num_of_root_infos))
        # fake_trajectory[:, 2] += 0.1
        fake_trajectory[:, 0] += 0.1
        phase += y[0, trajectory_length*num_of_root_infos]/phase_scale
        phase = phase.detach()
        print(phase)
        angles = y[:, trajectory_length*num_of_root_infos+1:]
        # print(y.shape)
        # print(angles.shape)
        # print(state.reshape(1, num_of_root_infos).shape)
        # print(angles.detach().numpy().shape)
        state = y[:, :num_of_root_infos].detach().numpy()
        motions[i] = np.concatenate(
            (fake_trajectory[0, :num_of_root_infos].reshape(
                1, num_of_root_infos), angles.detach().numpy()),
            axis=1)
    bvh.save("2_left"+output_path, motions)
    smoothed_motions = np.concatenate(
        (np.zeros((frames, num_of_root_infos)),
         motions[:, num_of_root_infos:]),
        axis=1)
    bvh.save("2_smooth_left"+output_path, smoothed_motions)
def pfnn_inference_3():
    dataset = BVHDataset3(base_dir + bvh_path)
    bvh = dataset.bvh
    print(dataset.in_features, dataset.out_features)
    pfnn = PFNN(dataset.in_features, dataset.out_features).float().cuda()
    pfnn.load_state_dict(torch.load('models_7/pfnn_params199.pkl'))  # 13
    x, phase, y = dataset[198]
    print(x.shape, phase.shape, y.shape)
    x = torch.tensor(x).float()
    all_angles = x[:, trajectory_length*num_of_trajectory_infos:]
    fake_trajectory = np.zeros((trajectory_length, num_of_trajectory_infos))
    fake_trajectory[:, 1] = 0.2
    fake_trajectory = (fake_trajectory -
                       dataset.trajectory_mean) / dataset.trajectory_std
    fake_trajectory *= trajectory_scale
    print(fake_trajectory)
    motions = np.zeros((frames, bvh.num_of_angles+num_of_root_infos))
    num_of_angles = bvh.num_of_angles
    last_angles = np.zeros((1, num_of_angles))
    for i in range(frames):
        print('i:  ', i)
        if i == 0:
            angles = all_angles[:, :num_of_angles].numpy()
            angles_delta = all_angles[:, num_of_angles:]
            last_angles = (angles / angles_scale) * \
                dataset.angles_std + dataset.angles_mean
        else:
            angles_delta = output_angles - last_angles
            angles_delta = (angles_delta - dataset.angles_delta_mean) / \
                (dataset.angles_delta_std+(dataset.angles_delta_std == 0))
            angles_delta *= angles_scale
            last_angles = output_angles
        x = torch.cat(
            (torch.tensor(fake_trajectory, dtype=torch.float32)
             .view(1, num_of_trajectory_infos*trajectory_length),
             torch.tensor(angles, dtype=torch.float32).view(1, 90),
             torch.tensor(angles_delta, dtype=torch.float32).view(1, 90)
             ),
            dim=1)
        y = pfnn(x.cuda(), phase)
        phase += (y[0, -1].detach().cpu().double().numpy() / phase_scale) * \
            dataset.phase_deltas_std + dataset.phase_deltas_mean
        # phase = phase.detach()
        print(phase)
        angles_index = trajectory_length*num_of_trajectory_infos
        angles = y[:, angles_index:angles_index+dataset.bvh.num_of_angles]
        # all_angles = y[:, angles_index:-1]
        # x = y[:, :-1]
        # print(y.shape)
        # print(angles.shape)
        # print(state.reshape(1, num_of_root_infos).shape)
        # print(angles.detach().numpy().shape)
        # state = y[:, :num_of_root_infos].detach().numpy()
        # motions[i] = np.concatenate(
        #     (fake_trajectory[0, :num_of_root_infos].reshape(
        #         1, num_of_root_infos), angles.detach().numpy()),
        #     axis=1)
        output_angles = (angles.detach().cpu().numpy() / angles_scale) * \
            dataset.angles_std + dataset.angles_mean
        motions[i] = np.concatenate((
            np.zeros((1, num_of_root_infos)), output_angles),
            axis=1)
    print(motions.shape)
    bvh.save("7_"+output_path, motions)
Exemple #38
0
def build_super_images2(real_imgs, captions, cap_lens, ixtoword,
                        attn_maps, att_sze, vis_size=256, topK=5):
    batch_size = real_imgs.size(0)
    max_word_num = np.max(cap_lens)
    text_convas = np.ones([batch_size * FONT_MAX,
                           max_word_num * (vis_size + 2), 3],
                           dtype=np.uint8)

    real_imgs = \
        nn.Upsample(size=(vis_size, vis_size), mode='bilinear')(real_imgs)
    # [-1, 1] --> [0, 1]
    real_imgs.add_(1).div_(2).mul_(255)
    real_imgs = real_imgs.data.numpy()
    # b x c x h x w --> b x h x w x c
    real_imgs = np.transpose(real_imgs, (0, 2, 3, 1))
    pad_sze = real_imgs.shape
    middle_pad = np.zeros([pad_sze[2], 2, 3])

    # batch x seq_len x 17 x 17 --> batch x 1 x 17 x 17
    img_set = []
    num = len(attn_maps)

    text_map, sentences = \
        drawCaption(text_convas, captions, ixtoword, vis_size, off1=0)
    text_map = np.asarray(text_map).astype(np.uint8)

    bUpdate = 1
    for i in range(num):
        attn = attn_maps[i].cpu().view(1, -1, att_sze, att_sze)
        #
        attn = attn.view(-1, 1, att_sze, att_sze)
        attn = attn.repeat(1, 3, 1, 1).data.numpy()
        # n x c x h x w --> n x h x w x c
        attn = np.transpose(attn, (0, 2, 3, 1))
        num_attn = cap_lens[i]
        thresh = 2./float(num_attn)
        #
        img = real_imgs[i]
        row = []
        row_merge = []
        row_txt = []
        row_beforeNorm = []
        conf_score = []
        for j in range(num_attn):
            one_map = attn[j]
            mask0 = one_map > (2. * thresh)
            conf_score.append(np.sum(one_map * mask0))
            mask = one_map > thresh
            one_map = one_map * mask
            if (vis_size // att_sze) > 1:
                one_map = \
                    skimage.transform.pyramid_expand(one_map, sigma=20,
                                                     upscale=vis_size // att_sze)
            minV = one_map.min()
            maxV = one_map.max()
            one_map = (one_map - minV) / (maxV - minV)
            row_beforeNorm.append(one_map)
        sorted_indices = np.argsort(conf_score)[::-1]

        for j in range(num_attn):
            one_map = row_beforeNorm[j]
            one_map *= 255
            #
            PIL_im = Image.fromarray(np.uint8(img))
            PIL_att = Image.fromarray(np.uint8(one_map))
            merged = \
                Image.new('RGBA', (vis_size, vis_size), (0, 0, 0, 0))
            mask = Image.new('L', (vis_size, vis_size), (180))  # (210)
            merged.paste(PIL_im, (0, 0))
            merged.paste(PIL_att, (0, 0), mask)
            merged = np.array(merged)[:, :, :3]

            row.append(np.concatenate([one_map, middle_pad], 1))
            #
            row_merge.append(np.concatenate([merged, middle_pad], 1))
            #
            txt = text_map[i * FONT_MAX:(i + 1) * FONT_MAX,
                           j * (vis_size + 2):(j + 1) * (vis_size + 2), :]
            row_txt.append(txt)
        # reorder
        row_new = []
        row_merge_new = []
        txt_new = []
        for j in range(num_attn):
            idx = sorted_indices[j]
            row_new.append(row[idx])
            row_merge_new.append(row_merge[idx])
            txt_new.append(row_txt[idx])
        row = np.concatenate(row_new[:topK], 1)
        row_merge = np.concatenate(row_merge_new[:topK], 1)
        txt = np.concatenate(txt_new[:topK], 1)
        if txt.shape[1] != row.shape[1]:
            print('Warnings: txt', txt.shape, 'row', row.shape,
                  'row_merge_new', row_merge_new.shape)
            bUpdate = 0
            break
        row = np.concatenate([txt, row_merge], 0)
        img_set.append(row)
    if bUpdate:
        img_set = np.concatenate(img_set, 0)
        img_set = img_set.astype(np.uint8)
        return img_set, sentences
    else:
        return None
Exemple #39
0
 def get_new_cnn(self, t):
     t = np.concatenate((self.last_state_cnn, t), axis=1)
     return t
Exemple #40
0
def build_super_images(real_imgs, captions, ixtoword,
                       attn_maps, att_sze, lr_imgs=None,
                       batch_size=cfg.TRAIN.BATCH_SIZE,
                       max_word_num=cfg.TEXT.WORDS_NUM):
    nvis = 8
    real_imgs = real_imgs[:nvis]
    if lr_imgs is not None:
        lr_imgs = lr_imgs[:nvis]
    if att_sze == 17:
        vis_size = att_sze * 16
    else:
        vis_size = real_imgs.size(2)

    text_convas = \
        np.ones([batch_size * FONT_MAX,
                 (max_word_num + 2) * (vis_size + 2), 3],
                dtype=np.uint8)

    for i in range(max_word_num):
        istart = (i + 2) * (vis_size + 2)
        iend = (i + 3) * (vis_size + 2)
        text_convas[:, istart:iend, :] = COLOR_DIC[i]


    real_imgs = \
        nn.Upsample(size=(vis_size, vis_size), mode='bilinear')(real_imgs)
    # [-1, 1] --> [0, 1]
    real_imgs.add_(1).div_(2).mul_(255)
    real_imgs = real_imgs.data.numpy()
    # b x c x h x w --> b x h x w x c
    real_imgs = np.transpose(real_imgs, (0, 2, 3, 1))
    pad_sze = real_imgs.shape
    middle_pad = np.zeros([pad_sze[2], 2, 3])
    post_pad = np.zeros([pad_sze[1], pad_sze[2], 3])
    if lr_imgs is not None:
        lr_imgs = \
            nn.Upsample(size=(vis_size, vis_size), mode='bilinear')(lr_imgs)
        # [-1, 1] --> [0, 1]
        lr_imgs.add_(1).div_(2).mul_(255)
        lr_imgs = lr_imgs.data.numpy()
        # b x c x h x w --> b x h x w x c
        lr_imgs = np.transpose(lr_imgs, (0, 2, 3, 1))

    # batch x seq_len x 17 x 17 --> batch x 1 x 17 x 17
    seq_len = max_word_num
    img_set = []
    num = nvis  # len(attn_maps)

    text_map, sentences = \
        drawCaption(text_convas, captions, ixtoword, vis_size)
    text_map = np.asarray(text_map).astype(np.uint8)

    bUpdate = 1
    for i in range(num):
        attn = attn_maps[i].cpu().view(1, -1, att_sze, att_sze)
        # --> 1 x 1 x 17 x 17
        attn_max = attn.max(dim=1, keepdim=True)
        attn = torch.cat([attn_max[0], attn], 1)
        #
        attn = attn.view(-1, 1, att_sze, att_sze)
        attn = attn.repeat(1, 3, 1, 1).data.numpy()
        # n x c x h x w --> n x h x w x c
        attn = np.transpose(attn, (0, 2, 3, 1))
        num_attn = attn.shape[0]
        #
        img = real_imgs[i]
        if lr_imgs is None:
            lrI = img
        else:
            lrI = lr_imgs[i]
        row = [lrI, middle_pad]
        row_merge = [img, middle_pad]
        row_beforeNorm = []
        minVglobal, maxVglobal = 1, 0

        print(attn[0].shape)
        for j in range(num_attn):
            one_map = attn[j]
            if (vis_size // att_sze) > 1:
                one_map = \
                    skimage.transform.pyramid_expand(one_map, sigma=20,
                                                     upscale=vis_size // att_sze)
            row_beforeNorm.append(one_map)
            minV = one_map.min()
            maxV = one_map.max()
            if minVglobal > minV:
                minVglobal = minV
            if maxVglobal < maxV:
                maxVglobal = maxV
        for j in range(seq_len + 1):
            if j < num_attn:
                one_map = row_beforeNorm[j]
                one_map = (one_map - minVglobal) / (maxVglobal - minVglobal)
                one_map *= 255
                #
                print(img.shape)
                PIL_im = Image.fromarray(np.uint8(img))
                print(one_map.shape)
                PIL_att = Image.fromarray(np.uint8(one_map))
                merged = \
                    Image.new('RGBA', (vis_size, vis_size), (0, 0, 0, 0))
                mask = Image.new('L', (vis_size, vis_size), (210))
                merged.paste(PIL_im, (0, 0))
                merged.paste(PIL_att, (0, 0), mask)
                merged = np.array(merged)[:, :, :3]
            else:
                one_map = post_pad
                merged = post_pad
            row.append(one_map)
            row.append(middle_pad)
            #
            row_merge.append(merged)
            row_merge.append(middle_pad)
        row = np.concatenate(row, 1)
        row_merge = np.concatenate(row_merge, 1)
        txt = text_map[i * FONT_MAX: (i + 1) * FONT_MAX]
        if txt.shape[1] != row.shape[1]:
            print('txt', txt.shape, 'row', row.shape)
            bUpdate = 0
            break
        row = np.concatenate([txt, row, row_merge], 0)
        img_set.append(row)
    if bUpdate:
        img_set = np.concatenate(img_set, 0)
        img_set = img_set.astype(np.uint8)
        return img_set, sentences
    else:
        return None
Exemple #41
0
def Joint(gr1, gr2, ID = 0):
  
   if gr1 == None and gr2 != None:
     return gr2
   if gr1 != None and gr2 == None:
     return gr1
   if gr1 == None and gr2 == None:
     return None  
   
   gr = GroupNode()
   gr.id = ID
   
   L1 = 10**gr1.logK
   L2 = 10**gr2.logK
   
   if L1 == 1: L1 = 0
   if L2 == 1: L2 = 0
   
   L_tot = L1 + L2
   
   gr.logK = log10(L_tot)
   
   
   d1 = gr1.dist
   d2 = gr2.dist
   
   sgl1 = gr1.sgl
   sgl2 = gr2.sgl
   sgb1 = gr1.sgb
   sgb2 = gr2.sgb
   
   gl1 = gr1.gl
   gl2 = gr2.gl
   gb1 = gr1.gb
   gb2 = gr2.gb 
   
   ra1 = gr1.ra
   ra2 = gr2.ra
   dec1 = gr1.dec
   dec2 = gr2.dec   

   v1 = gr1.Vls
   v2 = gr2.Vls   
   gr.Vls  = (L1*v1 + L2*v2)/L_tot
   
   gr.sgl, gr.sgb, d     = barycenter(L1,  sgl1,  sgb1, d1, L2,  sgl2,  sgb2, d2)
   gr.gl,  gr.gb,  d0    = barycenter(L1, gl1, gb1, d1, L2, gl2, gb2, d2)
   gr.ra,  gr.dec, d00   = barycenter(L1, ra1, dec1, d1, L2, ra2, dec2, d2)
   ###if abs(d- d0)>1 :
     ###print gr1.id, gr2.id
     ###print "  ", d, d0, d00
     
   gr.dist = np.median([d,d0,d00])
   Mk_sun = 3.28   # K-band
   M = Mk_sun - (gr.logK / 0.4)
   gr.Ks = M + 5*log10(gr.dist) + 30 - 5
   
   gr.M_v2 = Mass(10**gr.logK)
   gr.R_2t2 = 0.215*((gr.M_v2/1.E12)**(1./3))  # Mpc
   gr.r2t = sqrt(1.5)*gr.R_2t2
   gr.r1t = 3.5*gr.r2t
   
   gr.mDist =  np.median([d,d0,d00])

   gr.flag = 5
   
   
   if  gr1.ngal>1:
     gr1.flag = 4
   else: 
     gr1.flag = 3
     
   if  gr2.ngal>1:
     gr2.flag = 4
   else: 
     gr2.flag = 3     
   
   gr.ngal = gr1.ngal + gr2.ngal
   gr.Vls_list = np.asarray([gr.Vls])
   gr.Vls_list = np.concatenate((gr.Vls_list, gr1.Vls_list[1:], gr2.Vls_list[1:]))
   
   
   
   tmp, gr.sigma = gr.v_ave(gr.Vls_list[1:])
   
   
   return gr
Exemple #42
0
 def get_new_oth(self,t):
     t = np.concatenate((self.last_state_oth, t), axis=1)
     return t
Exemple #43
0
def post_process(sess,
                 x,
                 is_training,
                 out,
                 pathList,
                 imgSize,
                 num_classes,
                 class_names,
                 anchors,
                 save_weights=False):
    def arg_max(x):
        xmax = 0
        id1 = 0
        id2 = 0
        w1, w2 = x.shape
        for i in range(w1):
            for j in range(w2):
                if x[i, j] > xmax:
                    xmax = x[i, j]
                    id1 = i
                    id2 = j
        return id1, id2

    imgTotal = []
    imgSrcTotal = []
    for path in pathList:
        imgSrc = cv2.imread(path, 1)
        img = np.float32(cv2.cvtColor(imgSrc, cv2.COLOR_BGR2RGB) / 255)
        img = cv2.resize(img, (imgSize, imgSize))[np.newaxis, :, :, :]
        imgTotal.append(img)
        imgSrcTotal.append(imgSrc)
    imgTotal = np.concatenate(imgTotal, axis=0)

    bboxTotal, obj_probsTotal, class_probsTotal = sess.run(
        decode(out, imgSize, num_classes, anchorTotal=anchors),
        feed_dict={
            x: imgTotal,
            is_training: False
        })
    if save_weights:
        saver = tf.train.Saver()
        saver.save(sess, './Weights/weightsInit.ckpt')

    for i, imgSrc in enumerate(imgSrcTotal):
        H, W, _ = imgSrc.shape
        #[:, 3]; [:, 3, 4]
        class_name = np.argmax(class_probsTotal[i], axis=2)
        class_probs = np.max(class_probsTotal[i], axis=2)
        obj_probs = obj_probsTotal[i]
        bbox = bboxTotal[i]
        bbox[bbox > 1] = 1
        bbox[bbox < 0] = 0
        confidence = class_probs * obj_probs
        conf = confidence.copy()
        confidence[
            confidence <
            0.5] = 0  ######################################################################################

        det_indTotal = []
        while (np.max(confidence) != 0):
            id1, id2 = arg_max(confidence)
            bbox1_area = (bbox[id1, id2, 3] - bbox[id1, id2, 1]) * (
                bbox[id1, id2, 2] - bbox[id1, id2, 0])
            sign = 0
            for coor in det_indTotal:
                if coor == None:
                    det_indTotal.append([id1, id2])
                else:
                    xi1 = max(bbox[id1, id2, 0], bbox[coor[0], coor[1], 0])
                    yi1 = max(bbox[id1, id2, 1], bbox[coor[0], coor[1], 1])
                    xi2 = min(bbox[id1, id2, 2], bbox[coor[0], coor[1], 2])
                    yi2 = min(bbox[id1, id2, 3], bbox[coor[0], coor[1], 3])
                    int_area = (max(yi2, 0) - max(yi1, 0)) * (max(xi2, 0) -
                                                              max(xi1, 0))
                    bbox2_area = (
                        bbox[coor[0], coor[1], 3] - bbox[coor[0], coor[1], 1]
                    ) * (bbox[coor[0], coor[1], 2] - bbox[coor[0], coor[1], 0])
                    uni_area = bbox1_area + bbox2_area - int_area
                    iou = int_area / uni_area
                    if iou > 0.3:  ###########################################################################################
                        sign = 1
                        break
            if sign == 0:
                det_indTotal.append([id1, id2])
            confidence[id1, id2] = 0

        depict = []
        for [id1, id2] in det_indTotal:
            x1, y1, x2, y2 = bbox[id1, id2]
            x1 = int(x1 * W)
            x2 = int(x2 * W)
            y1 = int(y1 * H)
            y2 = int(y2 * H)
            name = class_names[class_name[id1, id2]]
            depict.append([x1, y1, x2, y2])
            cv2.rectangle(imgSrc, (x1, y1), (x2, y2), (255, 0, 0), 2)

            y = y1 - 8 if y1 - 8 > 8 else y1 + 8
            cv2.putText(imgSrc, '%s %.2f' % (name, conf[id1, id2]), (x1, y),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)

        cv2.imshow('%d' % i, imgSrc)
Exemple #44
0
  def decode_onestep(self, sess, batch, latest_tokens, enc_states, dec_init_states, prev_coverage):
    """For beam search decoding. Run the decoder for one step.

    Args:
      sess: Tensorflow session.
      batch: Batch object containing single example repeated across the batch
      latest_tokens: Tokens to be fed as input into the decoder for this timestep
      enc_states: The encoder states.
      dec_init_states: List of beam_size LSTMStateTuples; the decoder states from the previous timestep
      prev_coverage: List of np arrays. The coverage vectors from the previous timestep. List of None if not using coverage.

    Returns:
      ids: top 2k ids. shape [beam_size, 2*beam_size]
      probs: top 2k log probabilities. shape [beam_size, 2*beam_size]
      new_states: new states of the decoder. a list length beam_size containing
        LSTMStateTuples each of shape ([hidden_dim,],[hidden_dim,])
      attn_dists: List length beam_size containing lists length attn_length.
      p_gens: Generation probabilities for this step. A list length beam_size. List of None if in baseline mode.
      new_coverage: Coverage vectors for this step. A list of arrays. List of None if coverage is not turned on.
    """

    beam_size = len(dec_init_states)

    # Turn dec_init_states (a list of LSTMStateTuples) into a single LSTMStateTuple for the batch
    cells = [np.expand_dims(state.c, axis=0) for state in dec_init_states]
    hiddens = [np.expand_dims(state.h, axis=0) for state in dec_init_states]
    new_c = np.concatenate(cells, axis=0)  # shape [batch_size,hidden_dim]
    new_h = np.concatenate(hiddens, axis=0)  # shape [batch_size,hidden_dim]
    new_dec_in_state = tf.contrib.rnn.LSTMStateTuple(new_c, new_h)

    feed = {
        self._enc_states: enc_states,
        self._enc_padding_mask: batch.enc_padding_mask,
        self._dec_in_state: new_dec_in_state,
        self._dec_batch: np.transpose(np.array([latest_tokens])),
    }

    to_return = {
      "ids": self._topk_ids,
      "probs": self._topk_log_probs,
      "states": self._dec_out_state,
      "attn_dists": self.attn_dists
    }

    if FLAGS.pointer_gen:
      feed[self._enc_batch_extend_vocab] = batch.enc_batch_extend_vocab
      feed[self._max_art_oovs] = batch.max_art_oovs
      to_return['p_gens'] = self.p_gens

    if self._hps.coverage:
      feed[self.prev_coverage] = np.stack(prev_coverage, axis=0)
      to_return['coverage'] = self.coverage

    results = sess.run(to_return, feed_dict=feed) # run the decoder step

    # Convert results['states'] (a single LSTMStateTuple) into a list of LSTMStateTuple -- one for each hypothesis
    new_states = [tf.contrib.rnn.LSTMStateTuple(results['states'].c[i, :], results['states'].h[i, :]) for i in xrange(beam_size)]

    # Convert singleton list containing a tensor to a list of k arrays
    assert len(results['attn_dists'])==1
    attn_dists = results['attn_dists'][0].tolist()

    if FLAGS.pointer_gen:
      # Convert singleton list containing a tensor to a list of k arrays
      assert len(results['p_gens'])==1
      p_gens = results['p_gens'][0].tolist()
    else:
      p_gens = [None for _ in xrange(beam_size)]

    # Convert the coverage tensor to a list length k containing the coverage vector for each hypothesis
    if FLAGS.coverage:
      new_coverage = results['coverage'].tolist()
      assert len(new_coverage) == beam_size
    else:
      new_coverage = [None for _ in xrange(beam_size)]

    return results['ids'], results['probs'], new_states, attn_dists, p_gens, new_coverage
def pack_folders(folder_list, output_folder, output_filename, continous_channels, prefix, digital_channels):
    """

    :param folder_list:
    :param output_folder:
    :param output_filename:
    :param continous_channels:
    :param digital_channels:
    :param prefix:
    :return:
    """

    output_path_dat = os.path.join(output_folder, output_filename + '.dat')
    output_path_h5 = os.path.join(output_folder, output_filename + '.hdf5')

    if os.path.isfile(output_path_dat) or os.path.isfile(output_path_h5):
        raise IOError('Output path already exists!')

    h5_file = h5py.File(output_path_h5)
    h5_file.attrs['device'] = 'tetrode'
    _ = h5_file.create_dataset('channels', data=continous_channels)

    curr_folder_start_ind = 0
    data_all = []
    sampling_rate = None

    for i, folder in enumerate(folder_list):

        curr_group = h5_file.create_group('folder' + ft.int2str(i, 4))
        curr_group.attrs['path'] = folder
        curr_con_group = curr_group.create_group('continuous')
        curr_dig_group = curr_group.create_group('digital')
        curr_ts_group = curr_group.create_group('timestamps')

        curr_trace_dict, curr_sample_num, fs = pack_folder(folder, prefix, digital_channels=digital_channels)
        all_channels = curr_trace_dict.keys()
        print '\nall channels in folder ', folder, ':'
        print all_channels
        print

        if sampling_rate is None:
            sampling_rate = fs
        else:
            if fs != sampling_rate:
                err = 'The sampling rate (' + str(fs) + 'Hz) of folder: (' + folder + ') does not match the sampling' +\
                    ' rate (' + str(sampling_rate) + ') of other folders.'
                raise ValueError(err)

        curr_data_array = []

        # add electrode channels
        for channel in continous_channels:
            curr_prefix = prefix + '_CH' + str(channel)

            curr_key = [k for k in all_channels if k[:len(curr_prefix)] == curr_prefix]
            if len(curr_key) == 0:
                raise LookupError('no file is found in ' + folder +' for channel ' + str(channel) + '!')
            elif len(curr_key) > 1:
                raise LookupError('more than one files are found in ' + folder +' for channel ' + str(channel) + '!')
            curr_key = curr_key[0]
            curr_dset = curr_con_group.create_dataset('channel_' + ft.int2str(int(channel), 4),
                                                      data=curr_trace_dict[curr_key])
            curr_dset.attrs['unit'] = 'arbitrary_unit'
            curr_data_array.append(curr_trace_dict[curr_key])
        curr_data_array = np.array(curr_data_array, dtype=np.int16)
        data_all.append(curr_data_array.flatten(order='F'))

        # add continuous channels
        for ch, trace in curr_trace_dict.iteritems():
            if '_CH' not in ch and ch != 'events':
                curr_dset = curr_con_group.create_dataset(ch[len(prefix) + 1:], data=trace)
                curr_dset.attrs['unit'] = 'volt'

        # add digital events
        events = curr_trace_dict['events']
        for dch, dch_dict in events.iteritems():
            curr_dch_group = curr_dig_group.create_group(dch)
            curr_dch_group.create_dataset('rise', data=dch_dict['rise'])
            curr_dch_group.create_dataset('fall', data=dch_dict['fall'])

        curr_group.attrs['start_index'] = curr_folder_start_ind
        curr_group.attrs['end_index'] = curr_folder_start_ind + curr_sample_num
        curr_folder_start_ind += curr_sample_num

    h5_file.create_dataset('fs_hz', data=float(sampling_rate))

    h5_file.close()

    data_all = np.concatenate(data_all)

    data_all.tofile(output_path_dat)
def findSim():
    """
    finds cosine similarity of word vectors of window size 3 
    and saves output in windowLambda_dressJean.txt
    this data needs to be sorted on cosine sim
    that task is done by sort_data()
    """
    f1 = open('data/sentences_dress.txt','r')
    g1 = open('data/sentences_jean.txt','r')
    zeros = np.zeros(300)
    jeans = g1.readlines()
    jean_window_vectors = [] # key: (sentno,centralWordPos,tag); Value = wordvector of window 3
    jean_list = []
    dress_window_vectors = []
    dress_list = []
    for line in jeans:  # line is :  word tag centerword centertag word tag centerwordpos sentno
        line = line.strip().split(" ")
        centralTag =  line[3].strip()
        if(centralTag =='O' or len(line)!=8):
            continue
        sentNo = int(line[7].strip())
        centralWordPos = int(line[6].strip())
        if(js[sentNo]<centralWordPos):
            print("ouch")
        if(line[0].lower() in model):
            vector = model[line[0].lower()]
        else:
            vector = zeros
            
        if(line[2].lower() in model):

            vector = np.concatenate((vector,model[line[2].lower()]))
        else:

            vector = np.concatenate((vector,zeros))
        if(line[4].lower() in model):

            vector = np.concatenate((vector,model[line[4].lower()]))
        else:
            vector= np.concatenate((vector,zeros))
        jean_list.append([sentNo,centralWordPos,centralTag,vector])        
        jean_window_vectors.append(vector)
    f1.close()
    g1.close()
    zeros = np.zeros(300)
    f1 = open('data/sentences_dress.txt','r')
    dress = f1.readlines()
    g1 = open('data/sentences_jean.txt','r')
    jean = g1.readlines()
    for idx,line in enumerate(dress):
        line = line.strip().split(" ") 
        centralTag =  line[3].strip()
        if(centralTag =='O' or len(line)!=8):
            continue
        sentNo = int(line[7].strip())
        centralWordPos = int(line[6].strip())
        if(ds[sentNo]<centralWordPos):
            print("ouch")
        if(line[0].lower() in model):
            dress_vector = model[line[0].lower()]
        else:
            dress_vector = zeros       
        if(line[2].lower() in model):
            dress_vector = np.concatenate((dress_vector,model[line[2].lower()]))
        else:
            dress_vector = np.concatenate((dress_vector,zeros))
        if(line[4].lower() in model):
            dress_vector = np.concatenate((dress_vector,model[line[4].lower()]))
        else:
            dress_vector= np.concatenate((dress_vector,zeros))
        dress_list.append([sentNo,centralWordPos,centralTag,dress_vector])        
        dress_window_vectors.append(dress_vector)
    dress_window_vectors_np = np.array(dress_window_vectors)
    jean_window_vectors_np = np.array(jean_window_vectors)
    cosine_sim = cosine_similarity(dress_window_vectors_np,jean_window_vectors_np)
    cosine_8 = np.nonzero(cosine_sim>0.79)  #creates a tuple ([x cordinate],[y cordinate])
    createLambdaLists( cosine_8,dress_list,jean_list)
def main(args):
	post_ids = p.load(open(args.post_ids_train, 'rb'))
	post_ids = np.array(post_ids)
	post_vectors = p.load(open(args.post_vectors_train, 'rb'))
	if len(post_vectors) != len(post_ids): #for ubuntu,unix,superuser combined data we don't have all train post_ids
		post_ids = np.zeros(len(post_vectors))

	ques_list_vectors = p.load(open(args.ques_list_vectors_train, 'rb'))
	ans_list_vectors = p.load(open(args.ans_list_vectors_train, 'rb'))
	utility_post_vectors = p.load(open(args.utility_post_vectors_train, 'rb'))
	utility_labels = p.load(open(args.utility_labels_train, 'rb'))

	post_ids_test = p.load(open(args.post_ids_test, 'rb'))
	post_ids_test = np.array(post_ids_test)
	post_vectors_test = p.load(open(args.post_vectors_test, 'rb'))
	ques_list_vectors_test = p.load(open(args.ques_list_vectors_test, 'rb'))
	ans_list_vectors_test = p.load(open(args.ans_list_vectors_test, 'rb'))
	utility_post_vectors_test = p.load(open(args.utility_post_vectors_test, 'rb'))
	utility_labels_test = p.load(open(args.utility_labels_test, 'rb'))

	out_file = open(args.test_predictions_output, 'w')
	out_file.close()
	out_file = open(args.dev_predictions_output, 'w')
	out_file.close()

	word_embeddings = p.load(open(args.word_embeddings, 'rb'))
	word_embeddings = np.asarray(word_embeddings, dtype=np.float32)
	vocab_size = len(word_embeddings)
	word_emb_dim = len(word_embeddings[0])
	freeze = False
	N = args.no_of_candidates
	
	print 'vocab_size ', vocab_size, 

	start = time.time()
	print 'generating data'
	posts, post_masks, ques_list, ques_masks_list, ans_list, ans_masks_list = \
					generate_data(post_vectors, ques_list_vectors, ans_list_vectors, args)
	posts_test, post_masks_test, ques_list_test, ques_masks_list_test, ans_list_test, ans_masks_list_test = \
					generate_data(post_vectors_test, ques_list_vectors_test, ans_list_vectors_test, args)
	print 'done! Time taken: ', time.time() - start

	train_size = int(len(posts)*0.8)
	dev_size = int(len(posts)*0.2)/2
	train = [posts[:train_size], post_masks[:train_size], ques_list[:train_size], ques_masks_list[:train_size], ans_list[:train_size], ans_masks_list[:train_size], post_ids[:train_size]]

	dev = [posts[train_size: train_size+dev_size], \
			post_masks[train_size: train_size+dev_size], \
			ques_list[train_size: train_size+dev_size], \
			ques_masks_list[train_size: train_size+dev_size], \
			ans_list[train_size: train_size+dev_size], \
			ans_masks_list[train_size: train_size+dev_size], \
			post_ids[train_size: train_size+dev_size]]

	test = [np.concatenate((posts_test, posts[train_size+dev_size:])), \
			np.concatenate((post_masks_test, post_masks[train_size+dev_size:])), \
			np.concatenate((ques_list_test, ques_list[train_size+dev_size:])), \
			np.concatenate((ques_masks_list_test, ques_masks_list[train_size+dev_size:])), \
			np.concatenate((ans_list_test, ans_list[train_size+dev_size:])), \
			np.concatenate((ans_masks_list_test, ans_masks_list[train_size+dev_size:])), \
			np.concatenate((post_ids_test, post_ids[train_size+dev_size:]))]

	print 'Size of training data: ', train_size
	print 'Size of dev data: ', dev_size
	
	start = time.time()
	print 'generating utility data'
	data_utility_posts, data_utility_post_masks, data_utility_labels = \
					generate_utility_data(utility_post_vectors, utility_labels, args)
	data_utility_posts_test, data_utility_post_masks_test, data_utility_labels_test = \
					generate_utility_data(utility_post_vectors_test, utility_labels_test, args)
	print 'done! Time taken: ', time.time() - start

	utility_train_size = int(len(data_utility_posts)*0.8)
	utility_dev_size = int(len(data_utility_posts)*0.2)/2

	utility_train = [data_utility_posts[:utility_train_size], \
					data_utility_post_masks[:utility_train_size], \
					data_utility_labels[:utility_train_size]]

	utility_dev = [data_utility_posts[utility_train_size: utility_train_size+utility_dev_size], \
					data_utility_post_masks[utility_train_size: utility_train_size+utility_dev_size], \
					data_utility_labels[utility_train_size: utility_train_size+utility_dev_size]]

	utility_test = [np.concatenate((data_utility_posts_test, \
									data_utility_posts[utility_train_size+utility_dev_size:])), \
					np.concatenate((data_utility_post_masks_test, \
									data_utility_post_masks[utility_train_size+utility_dev_size:])), \
					np.concatenate((data_utility_labels_test, \
									data_utility_labels[utility_train_size+utility_dev_size:]))] 

	print 'Size of utility training data: ', utility_train_size
	print 'Size of utility dev data: ', utility_dev_size

	start = time.time()
	print 'compiling graph...'
	utility_train_fn, utility_dev_fn = build_evpi_model(word_embeddings, vocab_size, word_emb_dim, N, args, freeze=freeze)
	print 'done! Time taken: ', time.time()-start

	#train_fn, dev_fn, utility_train_fn, utility_dev_fn = None, None, None, None
	# train network
	for epoch in range(args.no_of_epochs):
		validate(utility_train_fn, 'Train', epoch, train, utility_train, args)
		validate(utility_dev_fn, '\t DEV', epoch, dev, utility_dev, args, args.dev_predictions_output)
		#validate(dev_fn, utility_dev_fn, '\t TEST', epoch, test, utility_test, args, args.test_predictions_output)
		print "\n"
Exemple #48
0
# print 'Data'
# print X[:10]

#Plot
plt.scatter(final_positions[0], final_positions[1])
for i in range(len(final_positions[0])):
	plt.annotate(str(i), xy = (final_positions[0][i], final_positions[1][i]))
plt.show()


#Output positions into JSON
#if 2d, add a 0 to the third dimension
if final_positions.shape[0] == 2:
	print 'Its 2D'
	final_positions = np.reshape(final_positions, (2,final_positions.shape[1]))
	final_positions = np.concatenate((final_positions, np.zeros((1,final_positions.shape[1]))), axis=0)
	print 'now 3D'
elif final_positions.shape[0] == 3:
	print 'Its 3D'
	final_positions = np.reshape(final_positions, (3,final_positions.shape[1]))

final_positions = final_positions.T
print final_positions.shape

data = []
for i in range(len(final_positions)):
	data.append(list(final_positions[i]))

# Writing JSON data
with open('../threejs/data.json', 'w') as f:
	json.dump(data, f)
Exemple #49
0
mod = 10 ** 9 + 7
n, k = map(int, input().split())
A = np.array(input().split(), np.int64)

I = (np.abs(A)).argsort()
A = A[I][::-1]

pos = A[A >= 0]
neg = A[A < 0]

nums = [1]
if k & 1:
    nums = [pos[0]]
    pos = pos[1:]
    k -= 1

if len(pos) & 1:
    pos = pos[:-1]
if len(neg) & 1:
    neg = neg[:-1]

pos = pos[::2] * pos[1::2]
neg = neg[::2] * neg[1::2]

A = np.concatenate([pos, neg])
A.sort()
A = A[::-1]
print(np.concatenate([nums, A[:k // 2]]))

Exemple #50
0




    print ('Loading data')
    file_ = home+'/Documents/cifar-10-batches-py/data_batch_'

    for i in range(1,6):
        file__ = file_ + str(i)
        b1 = unpickle(file__)
        if i ==1:
            train_x = b1['data']
            train_y = b1['labels']
        else:
            train_x = np.concatenate([train_x, b1['data']], axis=0)
            train_y = np.concatenate([train_y, b1['labels']], axis=0)

    file__ = home+'/Documents/cifar-10-batches-py/test_batch'
    b1 = unpickle(file__)
    test_x = b1['data']
    test_y = b1['labels']


    train_x = train_x / 255.
    test_x = test_x / 255.

    # train_x = torch.from_numpy(train_x).float()
    # test_x = torch.from_numpy(test_x)
    # train_y = torch.from_numpy(train_y)
Exemple #51
0

# In[5]:

paths = glob.glob('/data/BraTS/BraTS17/MICCAI_BraTS17_Data_Training/HGG/*')
data_HGG, hdr_HGG = Get_data(paths)
paths = glob.glob('/data/BraTS/BraTS17/MICCAI_BraTS17_Data_Training/LGG/*')
data_LGG, hdr_LGG = Get_data(paths)

label = np.zeros((285))
label[0:len(data_HGG)] = 1
label[len(data_HGG):len(data_HGG) + len(data_LGG)] = 0

# In[6]:

data = np.concatenate((data_HGG, data_LGG)) / 4.0

# In[21]:

data[0].shape

# In[7]:

# def Segmet(img, up, down, axis):
#     for i in range(up):
#         img = np.delete(img, 90 - up, axis)
#     for i in range(down):
#         img = np.delete(img, 0, axis)
#     return img

# In[8]:
Exemple #52
0
def readSnap(basePath,
             snapNum,
             partType,
             fields=None,
             chunkNum=None,
             sq=True,
             sample_rate=1.0,
             seed=None,
             verbose=False,
             **kwargs):
    """
    Load a subset of fields for all particles/cells of a given particle type

    Parameters
    ----------
    basePath : string
        base path

    snapNum : int
        snapshot number

    partType : int
        particle type

    fields : list, optional
        list of field names (strings)

    chunkNum : int, optional
        chunk number, must be in the range [0,1024].
        If None, all chunks are read.
        If a list of legnth 2 is passed, the range is read in

    sq : Boolean, optional
        If True, return a numpy array instead of a dict if len(fields)==1.

    sample_rate : float, optional
        random sampling rate, must be in the range (0,1.0].
        Note that for sampling purposes, each particle is weighed "evenly".

    seed : int
        random seed used when sample_rate<1.0

    Returns
    -------
    dict : dictionary
        dictionary storing requested fields

    Notes
    -----
    The Gadget snapshot files for MBII use a custom block ordering.
    This block ordering is defined in ``mb2_python.data.py``.
    In order to use the pygadgetreader module,
    the block ordering attribute of the header object is replaced
    within this function.
    """

    # check to see if requested file exists
    if not os.path.isdir(basePath):
        msg = ('`basePath` does not point to directory on this machine!')
        raise ValueError(msg)
    if not os.path.exists(snapPath(basePath, snapNum, chunkNum)):
        path = snapPath(basePath, snapNum, chunkNum)
        msg = ('snapshot file not found: {0}'.format(path))
        raise ValueError(msg)

    # enforece particle type is 0-5
    assert (partType >= 0) & (partType <= 5)

    # enforce a reasonable random sampling rate
    assert (sample_rate <= 1.0) & (sample_rate > 0.0)

    # load header information
    snap = snapPath(basePath, snapNum, chunkNum=chunkNum)
    h = pyg_header.Header(snap, 0, kwargs)
    h.BLOCKORDER = BLOCKORDERING['CMU']

    f = h.f

    # intialize dictionary to store results
    return_dict = {}
    for key in fields:
        if key in h.BLOCKORDER.keys():
            return_dict[key] = None
        else:
            msg = ('key: {0} not available.'.format(key))
            raise ValueError(msg)

    f.close()

    if chunkNum is None:
        min_chunk = 0
        max_chunk = h.nfiles
    elif isinstance(chunkNum, list):
        if len != 2:
            msg = ('chunkNum keyword not valid.'.format(key))
            raise ValueError(msg)
        assert chunkNum[0] < chunkNum[1]
    else:
        min_chunk = int(chunkNum)
        max_chunk = min_chunk + 1
    num_chunks = (max_chunk - min_chunk)

    if progress_bar & verbose:
        pbar = tqdm(total=num_chunks)
    elif verbose:
        start = time.time()
        num_chunks_read = 0

    # loop through requested chunks
    for i in range(min_chunk, max_chunk):

        if verbose & progress_bar:
            pass
        elif verbose:
            step_start = time.time()
            print("reading in chunk {0} out {1}".format(i + 1, max_chunk))

        # loop over requested fields
        for field in fields:

            # process header
            h = pyg_header.Header(snap, i, kwargs)
            h.BLOCKORDER = BLOCKORDERING['CMU']
            f = h.f
            d, p = pollOptions(h, kwargs, field, partType)
            h.reading = d
            initUnits(h)

            # read in data
            if h.fileType == 'gadget1':
                arr = gadget1.gadget_read(f, h, p, d)
            elif h.fileType == 'gadget2':
                arr = gadget2.gadget_type2_read(f, h, p)

            # if down sampling data
            if sample_rate < 1.0:
                n_ptcls = len(arr)
                mask = down_sample_ptcls(n_ptcls, rate=0.01, seed=seed)
                arr = arr[mask]

            # put arrays together
            if return_dict[field] is None:
                return_dict[field] = arr
            else:
                return_dict[field] = np.concatenate((return_dict[key], arr))

            f.close()

        # report progress
        if verbose & progress_bar:
            pbar.update(1)
        elif verbose:
            num_chunks_read += 1

            dt = time.time() - step_start
            msg = ("\t time to read chunk: {0} s.".format(dt))
            print(msg)

            num_chunks_to_go = num_chunks - num_chunks_read
            time_so_far = time.time() - start
            avg_time_per_chunk = (1.0 * time_so_far) / (num_chunks_read)

            t_remain = avg_time_per_chunk * num_chunks_to_go / 60.0
            msg = ("\t estimated time remaining: {0} min".format(t_remain))
            print(msg)

    # only a single field--return the array instead of a single item dict
    if sq and len(fields) == 1:
        return return_dict[fields[0]]

    return return_dict
Exemple #53
0
def knnW(data, k=2, p=2, ids=None, pct_unique=0.25):
    """
    Creates nearest neighbor weights matrix based on k nearest
    neighbors.

    Parameters
    ----------

    data        : array
                  (n,k) or KDTree where KDtree.data is array (n,k)
                  n observations on k characteristics used to measure
                  distances between the n objects
    k           : int
                  number of nearest neighbors
    p           : float
                  Minkowski p-norm distance metric parameter:
                  1<=p<=infinity
                  2: Euclidean distance
                  1: Manhattan distance
    ids         : list
                  identifiers to attach to each observation
    pct_unique  : float
                  threshold percentage of unique points in data. Below this
                  threshold tree is built on unique values only

    Returns
    -------

    w         : W
                instance
                Weights object with binary weights

    Examples
    --------

    >>> x,y=np.indices((5,5))
    >>> x.shape=(25,1)
    >>> y.shape=(25,1)
    >>> data=np.hstack([x,y])
    >>> wnn2=knnW(data,k=2)
    >>> wnn4=knnW(data,k=4)
    >>> set([1,5,6,2]) == set(wnn4.neighbors[0])
    True
    >>> set([0,6,10,1]) == set(wnn4.neighbors[5])
    True
    >>> set([1,5]) == set(wnn2.neighbors[0])
    True
    >>> set([0,6]) == set(wnn2.neighbors[5])
    True
    >>> "%.2f"%wnn2.pct_nonzero
    '8.00'
    >>> wnn4.pct_nonzero
    16.0
    >>> wnn3e=knnW(data,p=2,k=3)
    >>> set([1,5,6]) == set(wnn3e.neighbors[0])
    True
    >>> wnn3m=knnW(data,p=1,k=3)
    >>> a = set([1,5,2])
    >>> b = set([1,5,6])
    >>> c = set([1,5,10])
    >>> w0n = set(wnn3m.neighbors[0])
    >>> a==w0n or b==w0n or c==w0n
    True

    ids

    >>> wnn2 = knnW(data,2)
    >>> wnn2[0]
    {1: 1.0, 5: 1.0}
    >>> wnn2[1]
    {0: 1.0, 2: 1.0}

    now with 1 rather than 0 offset

    >>> wnn2 = knnW(data,2, ids = range(1,26))
    >>> wnn2[1]
    {2: 1.0, 6: 1.0}
    >>> wnn2[2]
    {1: 1.0, 3: 1.0}
    >>> 0 in wnn2.neighbors
    False

    Notes
    -----

    Ties between neighbors of equal distance are arbitrarily broken.

    See Also
    --------
    pysal.weights.W

    """

    if issubclass(type(data), scipy.spatial.KDTree):
        kd = data
        data = kd.data
        nnq = kd.query(data, k=k + 1, p=p)
        info = nnq[1]
    elif type(data).__name__ == 'ndarray':
        # check if unique points are a small fraction of all points
        ind = np.lexsort(data.T)
        u = data[np.concatenate(
            ([True], np.any(data[ind[1:]] != data[ind[:-1]], axis=1)))]
        pct_u = len(u) * 1. / len(data)
        if pct_u < pct_unique:
            tree = KDTree(u)
            nnq = tree.query(data, k=k + 1, p=p)
            info = nnq[1]
            uid = [np.where((data == ui).all(axis=1))[0][0] for ui in u]
            new_info = np.zeros((len(data), k + 1), 'int')
            for i, row in enumerate(info):
                new_info[i] = [uid[j] for j in row]
            info = new_info
        else:
            kd = KDTree(data)
            # calculate
            nnq = kd.query(data, k=k + 1, p=p)
            info = nnq[1]
    else:
        print 'Unsupported type'
        return None

    neighbors = {}
    for i, row in enumerate(info):
        row = row.tolist()
        if i in row:
            row.remove(i)
            focal = i
        if ids:
            row = [ids[j] for j in row]
            focal = ids[i]
        neighbors[focal] = row
    return pysal.weights.W(neighbors, id_order=ids)
Exemple #54
0
    def scale_kernel(scale):
        '''Defines the scale kernel, representing scaling by an integer between samples'''

        scale = np.concatenate(([1], scale))
        return np.outer(scale, scale)
    MEAN_2D = np.array([mean, mean])
    I_2D = np.matrix(np.eye(2))  # Creating m1,aka MEAN1 as an np.array
    COV_MATRIX_2D = sigma * I_2D  # Could use np.array as well instead of eye, np.array([[1,0,0],[0,1,0],[0,0,1]])
    SAMPLE_SET = np.random.multivariate_normal(MEAN_2D, COV_MATRIX_2D,
                                               N_observations).T
    #print("MEAN_2D:\n", MEAN_2D); print("\nCOV_MATRIX_2D:\n", COV_MATRIX_2D);    print("\nI_2D:\n", I_2D) ;    print("\nSAMPLE_SET.shape:", SAMPLE_SET.shape)
    return (SAMPLE_SET)


#%%
# Calling create_multivariate_Gauss_2D_dataset function with desired parameters:
SAMPLE_SET_220 = (create_multivariate_Gauss_2D_dataset(1, 0.5, 220))
SAMPLE_SET_280 = (create_multivariate_Gauss_2D_dataset(-1, 0.75, 280))

# Merge into one unified unlabeled dataset:
DATASET = np.concatenate((SAMPLE_SET_220, SAMPLE_SET_280), axis=1)
#%%

# CODE BLOCK FOR PLOTTING UNIFIED DATASET, NO LABELS:

from matplotlib import pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
#from mpl_toolkits.mplot3d import proj3d
from matplotlib import style

style.use('bmh')

fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
#plt.rcParams['legend.fontsize'] = 7
Exemple #56
0
                tmp[2] = translated_tmp[2]
                heatmap_per_frame.append(tmp[[0, 1, 2, 3, 5]])
                intensities.append(tmp[3])
                doppler.append(tmp[5])

            # if count == 100:
            #     pptk.viewer(np.array(test_frame)[:,0:3])

            # do not add empty frames
            if not heatmap_per_frame:
                continue

            #align frames
            for i in range(0, len(timestamps)):
                if abs(int(timestamp) - int(timestamps[i])) <= align_interval:
                    frames[i] = np.concatenate(
                        (frames[i], np.array(heatmap_per_frame)))
                    if topic == '_slash_mmWaveDataHdl_slash_RScan_left':
                        valid_data[i][0] = 1
                    elif topic == '_slash_mmWaveDataHdl_slash_RScan_right':
                        valid_data[i][1] = 1
    print(valid_data)
    # pptk.viewer(np.array(frames[100])[:,0:3])

    # overlay frames accounting for sparse pcl
    nb_overlay_frames = cfg['pcl2depth']['nb_overlay_frames']
    # frame_buffer_ls = deque(maxlen=nb_overlay_frames)
    overlay_frames = list()
    frames_array = np.array(frames)
    for i in range(frames_array.shape[0]):
        if i < nb_overlay_frames:
            tmp = frames_array[i:i + nb_overlay_frames]
Exemple #57
0
def pcca(P: np.ndarray, m: int, pi: np.ndarray = None, transition_matrix_tol: float = 1e-12):
    """
    PCCA+ spectral clustering method with optimized memberships [1]_

    Clusters the first m eigenvectors of a transition matrix in order to cluster the states.
    This function does not assume that the transition matrix is fully connected. Disconnected sets
    will automatically define the first metastable states, with perfect membership assignments.

    Parameters
    ----------
    P : ndarray (n,n)
        Transition matrix.
    m : int
        Number of clusters to group to.
    pi : ndarray(n,), optional, default=None
        Stationary distribution if available. Should be defined piecewise over the connected sets.
    transition_matrix_tol : float, optional, default=1e-12
        Tolerance under which P is checked to be a transition matrix.

    Returns
    -------
    chi : ndarray (n x m)
        A matrix containing the probability or membership of each state to be assigned to each cluster.
        The rows sum to 1.

    References
    ----------
    [1] S. Roeblitz and M. Weber, Fuzzy spectral clustering by PCCA+:
        application to Markov state models and data classification.
        Adv Data Anal Classif 7, 147-179 (2013).
    [2] F. Noe, multiset PCCA and HMMs, in preparation.
    """
    # imports
    from deeptime.markov.tools.estimation import connected_sets
    from deeptime.markov.tools.analysis import eigenvalues, is_transition_matrix, hitting_probability

    # validate input
    n = np.shape(P)[0]
    if m > n:
        raise ValueError(f"Number of metastable states m={m} exceeds number of states of transition matrix n={n}")
    if not is_transition_matrix(P, tol=transition_matrix_tol):
        raise ValueError("Input matrix is not a transition matrix.")
    if pi is not None and pi.ndim > 1:
        raise ValueError("Stationary distribution must be given as one-dimensional array or left None.")
    if pi is not None and pi.shape[0] != n:
        raise ValueError(f"Stationary distribution must be defined on entire space, piecewise if the transition matrix "
                         f"has multiple connected components. It covered {pi.shape[0]} != {n} states.")

    # prepare output
    chi = np.zeros((n, m))

    # test connectivity
    components = connected_sets(P)
    n_components = len(components)  # (n_components, labels) = connected_components(P, connection='strong')

    # store components as closed (with positive equilibrium distribution)
    # or as transition states (with vanishing equilibrium distribution)
    closed_components = []
    transition_states = []
    for i in range(n_components):
        component = components[i]  # np.argwhere(labels==i).flatten()
        rest = list(set(range(n)) - set(component))
        # is component closed?
        if np.sum(P[component, :][:, rest]) == 0:
            closed_components.append(component)
        else:
            transition_states.append(component)
    n_closed_components = len(closed_components)
    closed_states = np.concatenate(closed_components)
    if len(transition_states) == 0:
        transition_states = np.array([], dtype=int)
    else:
        transition_states = np.concatenate(transition_states)

    # check if we have enough clusters to support the disconnected sets
    if m < len(closed_components):
        raise ValueError(f"Number of metastable states m={m} is too small. Transition matrix "
                         f"has {len(closed_components)} disconnected components.")

    # We collect eigenvalues in order to decide which
    closed_components_Psub = []
    closed_components_ev = []
    closed_components_enum = []
    for i in range(n_closed_components):
        component = closed_components[i]

        # compute eigenvalues in submatrix
        Psub = P[component, :][:, component]
        closed_components_Psub.append(Psub)
        closed_components_ev.append(eigenvalues(Psub))
        closed_components_enum.append(i * np.ones((component.size,), dtype=int))

    # flatten
    closed_components_ev_flat = np.hstack(closed_components_ev)
    closed_components_enum_flat = np.hstack(closed_components_enum)
    # which components should be clustered?
    component_indexes = closed_components_enum_flat[np.argsort(closed_components_ev_flat)][0:m]
    # cluster each component
    ipcca = 0
    for i in range(n_closed_components):
        component = closed_components[i]
        # how many PCCA states in this component?
        m_by_component = np.shape(np.argwhere(component_indexes == i))[0]

        # if 1, then the result is trivial
        if m_by_component == 1:
            chi[component, ipcca] = 1.0
            ipcca += 1
        elif m_by_component > 1:
            # print "submatrix: ",closed_components_Psub[i]
            chi[component, ipcca:ipcca + m_by_component] = _pcca_connected(
                closed_components_Psub[i], m_by_component, pi=None if pi is None else pi[closed_components[i]]
            )
            ipcca += m_by_component
        else:
            raise RuntimeError(f"Component {i} spuriously has {m_by_component} pcca sets")

    # finally assign all transition states
    if transition_states.size > 0:
        # make all closed states absorbing, so we can see which closed state we hit first
        Pabs = P.copy()
        Pabs[closed_states, :] = 0.0
        Pabs[closed_states, closed_states] = 1.0
        for i in range(closed_states.size):
            # hitting probability to each closed state
            h = hitting_probability(Pabs, closed_states[i])
            for j in range(transition_states.size):
                # transition states belong to closed states with the hitting probability, and inherit their chi
                chi[transition_states[j]] += h[transition_states[j]] * chi[closed_states[i]]

    # check if we have m metastable sets. If less than m, we must raise
    nmeta = np.count_nonzero(chi.sum(axis=0))
    if nmeta < m:
        raise RuntimeError(f"{m} metastable states requested, but transition matrix only has {nmeta}. "
                           f"Consider using a prior or request less metastable states.")

    return chi
    def __init__(self,model,module,**parameters ):
        self.set_parameters(**parameters)
        names={'tbmodels':'TBmodels', 'pythtb':'PythTB'}
        self.seedname='model_{}'.format(names[module])

        if module=='tbmodels':
            # Extract the parameters from the model
            real=model.uc
            self.num_wann=model.size
            if self.spin : raise ValueError("System_{} class cannot be used for evaluation of spin properties".format(names[module]))
            self.spinors=False
            positions=model.pos
            Rvec=np.array([R[0] for R in model.hop.items()],dtype=int)
        elif module=='pythtb':
            real=model._lat
            self.num_wann=model._norb
            if model._nspin==1:
                self.spinors=False
            elif model._nspin==2:
                self.spinors=True
            else:
                raise Exception("\n\nWrong value of nspin!")
            positions = model._orb
            Rvec=np.array([R[-1] for R in model._hoppings],dtype=int)
        else : 
            raise ValueError("unknown tight-binding module {}".format(module))



        self.dimr=real.shape[1]
        self.norb=positions.shape[0] 
        self.wannier_centres_reduced=np.zeros((self.norb,3))
        self.wannier_centres_reduced[:,:self.dimr]=positions
        self.real_lattice=np.eye((3),dtype=float)
        self.real_lattice[:self.dimr,:self.dimr]=np.array(real)
        self.wannier_centres_cart = self.wannier_centres_reduced.dot(self.real_lattice)
#        self.periodic[:self.dimr]=True
        self.periodic[self.dimr:]=False
        self.recip_lattice=2*np.pi*np.linalg.inv(self.real_lattice).T
        Rvec = [tuple(row) for row in Rvec] 
        Rvecs=np.unique(Rvec,axis=0).astype('int32')   
        
        nR=Rvecs.shape[0]
        for i in range(3-self.dimr):
            column=np.zeros((nR),dtype='int32')
            Rvecs=np.column_stack((Rvecs,column))
            
        Rvecsneg=np.array([-r for r in Rvecs])
        R_all=np.concatenate((Rvecs,Rvecsneg),axis=0)
        R_all=np.unique(R_all,axis=0)
        # Find the R=[000] index (used later)
        index0=np.argwhere(np.all(([0,0,0]-R_all)==0, axis=1))
        # make sure it exists; otherwise, add it manually
        # add it manually
        if index0.size==0:
            R_all=np.column_stack((np.array([0,0,0]),R_all.T)).T
            index0=0
        elif index0.size==1:
            print ("R=0 found at position(s) {}".format(index0))
            index0=index0[0][0]
        else : 
            raise RuntimeError("wrong value of index0={}, with R_all={}".format(index0,R-all))
        
        self.iRvec = R_all
        nRvec=self.iRvec.shape[0]
        self.nRvec0=nRvec
        # Define HH_R matrix from hoppings
        self.HH_R=np.zeros((self.num_wann,self.num_wann,self.nRvec0),dtype=complex)
        if module=='tbmodels':
            for hop in model.hop.items():
                R=np.array(hop[0],dtype=int)
                hops=np.array(hop[1]).reshape((self.num_wann,self.num_wann))
                iR=int(np.argwhere(np.all((R-R_all[:,:self.dimr])==0, axis=1)))
                inR=int(np.argwhere(np.all((-R-R_all[:,:self.dimr])==0, axis=1)))
                self.HH_R[:,:,iR]+=hops
                self.HH_R[:,:,inR]+=np.conjugate(hops.T)
        elif module=='pythtb':
            for nhop in model._hoppings:
                i=nhop[1]
                j=nhop[2]
                iR=np.argwhere(np.all((nhop[-1]-self.iRvec[:,:self.dimr])==0, axis=1))
                inR=np.argwhere(np.all((-nhop[-1]-self.iRvec[:,:self.dimr])==0, axis=1))
                self.HH_R[i,j,iR]+=nhop[0]
                self.HH_R[j,i,inR]+=np.conjugate(nhop[0])
            # Set the onsite energies at H(R=[000])
            for i in range(model._norb):
                self.HH_R[i,i,index0]=model._site_energies[i]

        if self.getAA:
            self.AA_R=np.zeros((self.num_wann,self.num_wann,self.nRvec0,3),dtype=complex)
            for i in range(self.num_wann):
                self.AA_R[i,i,index0,:]=positions[i,:].dot(self.real_lattice[:positions.shape[1]])

        if self.getBB:
            self.BB_R=np.zeros((self.num_wann,self.num_wann,self.nRvec0,3),dtype=complex)
            for i in range(self.num_wann):
                self.BB_R[i,i,index0,:]=self.AA_R[i,i,index0,:]*self.HH_R[i,i,index0]

        if self.getCC:
            self.CC_R=np.zeros((self.num_wann,self.num_wann,self.nRvec0,3),dtype=complex)
#        print(self.AA_R)
#   TODO: generate the SS_R matrix        

        self.set_symmetry()
        self.check_periodic()
                
        print ("Number of wannier functions:",self.num_wann)
        print ("Number of R points:", self.nRvec)
        print ("Recommended size of FFT grid", self.NKFFT_recommended)
        print ("Real-space lattice:\n",self.real_lattice)
        cprint ("Reading the system from {} finished successfully".format(names[module]),'green', attrs=['bold'])
Exemple #59
0
def get_markers(size, gt, nr_classes, objectness_settings, downsample_ind = 0, maps_list = []):

    #   ds_factors, downsample_marker, overlap_solution, samp_func, samp_args
    #
    #   build objectness gt
    #   size:               size of the largest feature map the gt is built for
    #   ds_factors:         factors of downsampling for which de gt is built
    #   gt:                 list of the ground-truth bounding boxes
    #   downsample_marker:  if True the marker will be downsampled accordingly otherwise the marker has the same size
    #                       for each feature map
    #   overlap_solution:   what to do with overlaps
    #                       "no": the values just get overwritten
    #                       "max": the maximum of both values persists (per pixel)
    #                       "closest": each pixel gets assigned according to its closet center
    #                       --> prints to console if conflicts exist
    #   stamp_func:         function that defines individual markers
    #   stamp_args:         dict with  additional arguments passed on to stamp_func


    # downsample size and bounding boxes
    samp_factor = 1.0/objectness_settings["ds_factors"][downsample_ind]
    sampled_size = (int(size[1]*samp_factor),int(size[2]*samp_factor))

    sampled_gt = [x*[samp_factor,samp_factor,samp_factor,samp_factor,1] for x in gt]


    #init canvas
    last_dim = objectness_settings["stamp_func"][1](None,objectness_settings["stamp_args"],nr_classes)


    if objectness_settings["stamp_args"]["loss"] == "softmax":
        canvas = np.eye(last_dim)[np.zeros(sampled_size, dtype=np.int)]

    else:
        canvas = np.zeros(sampled_size + (last_dim,), dtype=np.float)

    if objectness_settings["stamp_func"][0] == "stamp_energy" and objectness_settings["stamp_args"]["loss"] == "reg":
        canvas = canvas - 10

    used_coords = []
    for bbox in sampled_gt:
        stamp, coords = objectness_settings["stamp_func"][1](bbox, objectness_settings["stamp_args"], nr_classes)
        stamp, coords = get_partial_marker(canvas.shape,coords,stamp)

        if stamp is None:
            print("skipping element")
            stamp, coords = objectness_settings["stamp_func"][1](bbox, objectness_settings["stamp_args"], nr_classes)
            stamp, coords = get_partial_marker(canvas.shape, coords, stamp)
            print(bbox)
            continue #skip this element

        if objectness_settings["stamp_args"]["loss"] == "softmax":
            stamp_zeros = stamp[:, :, 0] == 1
            stamp_zeros = np.expand_dims(stamp_zeros,-1).astype("int")
            stamp = stamp * ((stamp_zeros*-1)+1) + canvas[coords[1]:coords[3], coords[0]:coords[2]] * stamp_zeros

        else:
            stamp_zeros = stamp == 0
            stamp_zeros = stamp_zeros.astype("int")
            stamp = stamp * ((stamp_zeros*-1)+1) + canvas[coords[1]:coords[3], coords[0]:coords[2]] * stamp_zeros

        if objectness_settings["overlap_solution"] == "max":
            if objectness_settings["stamp_args"]["loss"]=="softmax":
                maskout_map = np.argmax(canvas[coords[1]:coords[3],coords[0]:(coords[2])], -1) < np.argmax(stamp,-1)
                canvas[coords[1]:coords[3], coords[0]:coords[2]][maskout_map] = stamp[maskout_map]
            else:
                canvas[coords[1]:coords[3], coords[0]:coords[2]] = np.expand_dims(np.max(np.concatenate([canvas[coords[1]:coords[3], coords[0]:coords[2]], stamp],-1),-1),-1)
        elif objectness_settings["overlap_solution"] == "no":
            canvas[coords[1]:coords[3], coords[0]:coords[2]] = stamp
        elif objectness_settings["overlap_solution"] == "nearest":
            closest_mask = get_closest_mask(coords, used_coords)
            if objectness_settings["stamp_args"]["loss"] == "softmax":
                closest_mask = np.expand_dims(closest_mask,-1)
                canvas[coords[1]:coords[3],coords[0]:coords[2]] = \
                    (1 - closest_mask) * canvas[coords[1]:coords[3], coords[0]:coords[2]] + closest_mask * stamp
            else:
                if len(closest_mask.shape) < len(stamp.shape):
                    closest_mask = np.expand_dims(closest_mask,-1)

                canvas[coords[1]:coords[3], coords[0]:coords[2]] = (1-closest_mask)*canvas[coords[1]:coords[3], coords[0]:coords[2]] + closest_mask*stamp

            used_coords.append(coords)
            # get overlapping bboxes
            # shave off pixels that are closer to another center
        else:
            raise NotImplementedError("overlap solution unkown")
    #color_map(canvas, objectness_settings, True)
    # set base energ y to minus 10




    # if downsample marker --> use cv2 to downsample gt
    if objectness_settings["downsample_marker"]:
        if objectness_settings["stamp_args"]["loss"] == "softmax":
            sm_dim = canvas.shape[-1]
            canvas_un_oh = np.argmax(canvas, -1).astype("uint8")

            for x in objectness_settings["ds_factors"][1:]:
                shape = (int(np.ceil(float(canvas_un_oh.shape[1]) / x)), int(np.ceil(float(canvas_un_oh.shape[0]) / x)))
                resized = np.round(cv2.resize(canvas_un_oh, shape, cv2.INTER_NEAREST))
                resized_oh = np.eye(sm_dim)[resized[:,:]]
                maps_list.append(np.expand_dims(resized_oh,0))
        else:
            for x in objectness_settings["ds_factors"][1:]:
                # if has 3rd dimension downsample dim3 one by one
                # if len(canvas.shape) == 3:
                #     for dim in range(3):
                #         rez_l = []
                #         rez_l.append()
                # else:
                shape = (int(np.ceil(float(canvas.shape[1]) / x)), int(np.ceil(float(canvas.shape[0]) / x)))
                resized = cv2.resize(canvas, shape, cv2.INTER_NEAREST)
                if len(resized.shape) < len(canvas.shape):
                    maps_list.append(np.expand_dims(np.expand_dims(resized, -1),0))
                else:
                    maps_list.append(np.expand_dims(resized, 0))

        # one-hot encoding
        maps_list.insert(0,np.expand_dims(canvas, 0))
    # if do not downsample marker, recursively rebuild for each ds level
    else:
        if (downsample_ind+1) == len(objectness_settings["ds_factors"]):
            return maps_list
        else:
            return get_markers(size, gt, nr_classes, objectness_settings,downsample_ind+1, maps_list)

    return maps_list
Exemple #60
0
    positive_flip_180 = np.load(
        save_dir_matFiles +
        'original_resolution/augment/pos_1_100_flip_clock180.npy')[:, :74]
    positive_flip_counterclock90 = np.load(
        save_dir_matFiles +
        'original_resolution/augment/pos_1_100_flip_counterclock90.npy'
    )[:, :74]

    negative = pickle.load(
        open(
            save_dir_matFiles +
            'original_resolution/clear_test/neg_1_100_all.pkl', 'rb'))

    positive = np.concatenate(
        (positive_ori, positive_clock90, positive_180, positive_counterclock90,
         positive_flip, positive_flip_clock90, positive_flip_180,
         positive_flip_counterclock90),
        axis=0)

    print("augmented pos and neg shape:", positive.shape, negative.shape)

    X_1 = np.concatenate((positive, negative), axis=0)

    # augmented positive target
    pos_target_ori = np.load(save_dir_matFiles +
                             'original_resolution/clear_test/pos_target.npy')
    pos_target_clock90 = np.load(
        save_dir_matFiles +
        'original_resolution/augment/pos_target_clock90.npy')
    pos_target_180 = np.load(
        save_dir_matFiles +