def extractPatches(im, patchsize, overlap, border, varargin):
    dfs = {'type': 'none', 'filters': []}
    opts = getPrmDflt(varargin, dfs, 1)
    a, b, c = np.shape(im)
    if c != 1:
        raise np.error('im should have only a single channel')
    if patchsize < 3:
        raise np.error('patchsize shoud be >= 3')
    grid = getSamplingGrid(np.array(np.shape(im)), patchsize, overlap, border,
                           1)
    if opts['type'] == 'none':
        f = im[grid]
        patches = f.reshape(f.shape[0] * f.shape[1], f.shape[2])
    elif opts['type'] == 'fliters':
        a, b = np.shape(opts['fliters'])
        feature_size = patchsize[0] * patchsize[1] * opts['fliters'].size
        patches = np.zeros([feature_size, grid.shape[2]])
        for i in opts['fliters'].size:
            f = signal.convolve2d(im, opts['filters'][i], mode='same')
            f = f[grid]
            f = f.reshape(f.shape[0] * f.shape[1], f.shape[2])
            patches[(i - 1) * f.shape[0]:f.shape[0] +
                    (i - 1) * f.shape[0], :] = f
    else:
        raise np.error('UNknown featuers')
    return patches
Ejemplo n.º 2
0
def SampleColorNaming(s):

    if np.size(s) != 3:
        np.error('Error: s must be a 1 x 3 vector [R G B]')

    # Constants
    #colors=['Red','Orange','Brown','Yellow','Green','Blue','Purple','Pink','Black','Grey','White']
    numColors = 11  # Number of colors
    numAchromatics = 3  # Number of achromatic colors
    numChromatics = numColors - numAchromatics  # Number of chromatic colors

    # Initializations
    numLevels = np.size(thrL) - 1  # Number of Lightness levels in the model
    CD = np.zeros((1, numColors))  # Color descriptor to store results

    Lab = RGB2Lab(np.double(np.reshape(s, (1, 1, 3))))
    L = Lab[:, :, 0].flatten()
    a = Lab[:, :, 1].flatten()
    b = Lab[:, :, 2].flatten()

    # Assignment of the sample to its corresponding level
    m = np.zeros(np.shape(L))
    m[np.where(L == 0)[0]] = 1.0  # Pixels with L=0 assigned to level 1
    k = 0
    for k in range(numLevels):
        m = m + np.double(thrL[:, k] < L) * np.double(L <= thrL[:, k + 1]) * k

    m = int(np.squeeze(m))

    # Computing membership values to chromatic categories
    for k in range(numChromatics):
        tx = parameters[k, 0, m - 1]
        ty = parameters[k, 1, m - 1]
        alfa_x = parameters[k, 2, m - 1]
        alfa_y = parameters[k, 3, m - 1]
        beta_x = parameters[k, 4, m - 1]
        beta_y = parameters[k, 5, m - 1]
        beta_e = parameters[k, 6, m - 1]
        ex = parameters[k, 7, m - 1]
        ey = parameters[k, 8, m - 1]
        angle_e = parameters[k, 9, m - 1]
        CD[:, k] = np.double(beta_e != 0.0) * TripleSigmoid_E(
            np.array([a, b]), tx, ty, alfa_x, alfa_y, beta_x, beta_y, beta_e,
            ex, ey, angle_e)

    # Computing membership values to achromatic categories
    valueAchro = max(1.0 - sum(CD, 1), 0)
    CD[:, numChromatics +
       0] = valueAchro * Sigmoid(L, paramsAchro[0, 0], paramsAchro[0, 1])
    CD[:, numChromatics + 1] = valueAchro * Sigmoid(
        L, paramsAchro[1, 0], paramsAchro[1, 1]) * Sigmoid(
            L, paramsAchro[2, 0], paramsAchro[2, 1])
    CD[:, numChromatics +
       2] = valueAchro * Sigmoid(L, paramsAchro[3, 0], paramsAchro[3, 1])

    # Returning the color name corresponding to the maximum membership value
    index = np.argmax(CD, axis=1)
    res = colors[index]

    return CD, res
def imageDownsample(imH, sf, varargin):
    dfs = {'kernel': 'bicubic', 'sigma': .4}

    opTs = getPrmDflt(varargin, dfs, 1)

    if opTs['kernel'] == 'bicubic':
        imL = misc.imresize(imH, 1 / sf, 'bicubic')
    elif opTs['kernel'] == 'Gaussian':
        np.error('Not implemented yet!')
    else:
        np.error('Unknown kernel')

    return imL
def createLeaf(Xsrc, Xtar, leaflearntype, lamdda, autolambda):
    if leaflearntype == 'constant':
        I = Xtar.sum(axis=1) / Xtar.shape[1]
        predmodeltype = 0
    elif leaflearntype == 'linear':
        Xsrc = np.row_stack((Xsrc, np.ones(Xsrc.shape[1])))
        matinv = Xsrc.dot(Xsrc.T)
        if autolambda == 1:
            lamdda = estimateLambda(matinv)
        I = Xtar.dot(
            np.linalg.lstsq((matinv + lamdda * np.eye(Xsrc.shape[0])),
                            Xsrc)[0].T)
        predmodeltype = 1
    elif leaflearntype == 'polynomial':
        Xsrc = np.vstack((Xsrc, np.ones(Xsrc.shape[1], Xsrc**2)))
        matinv = Xsrc.dot(Xsrc.T)
        if autolambda == 1:
            lamdda = estimateLambda(matinv)
        I = Xtar.dot(
            np.linalg.lstsq((matinv + lamdda * np.eye(Xsrc.shape[0])),
                            Xsrc)[0].T)
        predmodeltype = 2
    else:
        raise np.error('Unknown leaf node prediction type')
    leaf = {'T': [], 'type': -1, 'id': -1}
    leaf['T'] = I
    leaf['type'] = predmodeltype
    leaf['id'] = -1

    return leaf
Ejemplo n.º 5
0
def bag_of_words(sentence):
    sentence_words = clean_up_sentence(sentence)
    bag = [0] * len(words)
    for w in sentence_words:
        for i, word in enumerate(words):
            if word == w:
                bag[i] = 1
    return np.error(bag)
Ejemplo n.º 6
0
def flowToColor(flow, varargin=None):
    '''
    Convert optical flow to RGB image
    From:
    https://github.com/stefanoalletto/TransFlow/blob/master/
    flowToColor.pyeadapted from
    '''
    # TODO: cleanup all the translator crap
    [height, widht, nBands] = flow.shape
    if nBands != 2.:
        np.error('flowToColor: image must have two bands')

    u = flow[:, :, 0]
    v = flow[:, :, 1]
    # print u.shape,v.shape
    maxu = -999.
    maxv = -999.
    minu = 999.
    minv = 999.
    maxrad = -1.
    # % fix unknown flow
    # idxUnknown = np.logical_or(np.abs(u) > UNKNOWN_FLOW_THRESH, np.abs(v) > UNKNOWN_FLOW_THRESH)
    # print np.array(idxUnknown)
    # u[int(idxUnknown)-1] = 0.
    # v[int(idxUnknown)-1] = 0.
    maxu = max(maxu, np.max(u))
    minu = max(minu, np.max(u))
    maxv = max(maxv, np.max(v))
    minv = max(minv, np.max(v))
    rad = np.sqrt((u**2. + v**2.))
    maxrad = max(maxrad, np.max(rad))
    # print 'max flow:',maxrad, ' flow range: u =', minu, maxu, 'v =', minv, maxv
    # if isempty(varargin) == 0.:
    #    maxFlow = varargin.cell[0]
    #    if maxFlow > 0.:
    #        maxrad = maxFlow
    u = u / (maxrad + 1e-5)
    v = v / (maxrad + 1e-5)
    # % compute color
    img = computeColor(u, v)
    # % unknown flow
    # IDX = np.repmat(idxUnknown, np.array(np.hstack((1., 1., 3.))))
    # img[int(IDX)-1] = 0.
    return img / 255.
def imresize(im, sf, interpkernel):
    #""" 使用PIL 对象重新定义图像数组的大小"""
    if interpkernel == 'bicubic':
        pil_im = cv2.resize(im,
                            None,
                            fx=sf,
                            fy=sf,
                            interpolation=cv2.INTER_CUBIC)
        return pil_im
    else:
        raise np.error('error')
def findSplitAndThresh(resp, Xsrc, Xtar, F2, splitevaltype, lamdda, minChild,
                       kappa):
    F1 = resp.shape[0]
    rerr = float("inf")
    fid = 1
    thr = float("inf")
    Ft = Xtar.shape[0]
    Fs = Xsrc.shape[0]
    #special treatment for random tree growing
    if splitevaltype == 'random':
        F1 = 1
        F2 = 1
    for s in range(F1):
        #get thresholds to evaluate
        if F2 == 0:
            tthrs = np.median(resp[s, :], axis=0)
        else:
            respmin = min(resp[s, :])
            respmax = max(resp[s, :])
            tthrs = np.zeros((F2 + 1, 1), np.float32)
            tthrs[0:-1] = np.random.rand(
                1, 5) * 0.95 * (respmax - respmin) + respmin
            tthrs[-1] = np.median(resp[s, :])
        for t in range(len(tthrs)):
            tthr = tthrs[t]
            left = resp[s, :] < tthr
            right = ~left
            nl = 0
            nr = 0
            for i in range(len(resp)):
                if left[i] == 1:
                    nl = nl + 1
                else:
                    nr = nr + 1
#            left=left.astype('int')
#            right=right.astype('int')
            if nl < minChild or nr < minChild:
                continue
#  mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:] #nonzero对应于去掉特征数据缺失值
#  mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:]
            XsrcL = Xsrc[:, left]
            XsrcR = Xsrc[:, right]
            XtarL = Xtar[:, left]
            XtarR = Xtar[:, right]
            if splitevaltype == 'random':
                trerr = 0
            elif splitevaltype == 'banlanced':
                trerr = np.square(nl - nr)
            elif splitevaltype == 'variance':
                trerrL = sum(np.var(XtarL, axis=1, ddof=1)) / Ft
                trerrR = sum(np.var(XtarR, axis=1, ddof=1)) / Ft
                if kappa > 0:
                    trerrLsrc = sum(np.var(XsrcL, axis=1, ddof=1)) / Fs
                    trerrRsrc = sum(np.var(XsrcR, axis=1, ddof=1)) / Fs
                    trerrL = (trerrL + kappa * trerrLsrc) / 2
                    trerrR = (trerrR + kappa * trerrRsrc) / 2
                trerr = (nl * trerrL + nr * trerrR) / (nl + nr)
            elif splitevaltype == 'reconstruction':
                XsrcL = np.row_stack((XsrcL, np.ones(XsrcL.shape[1])))
                TL = XtarL.dot(
                    np.linalg.lstsq(((XsrcL.dot(XsrcL.T)) +
                                     lamdda * np.eye(XsrcL.shape[0])),
                                    XsrcL)[0].T)
                XsrcR = np.row_stack((XsrcR, np.ones(XsrcR.shape[1])))
                TR = XtarR.dot(
                    np.linalg.lstsq(((XsrcR.dot(XsrcR.T)) +
                                     lamdda * np.eye(XsrcR.shape[0])),
                                    XsrcR)[0].T)
                trerrL = np.sqrt(sum(sum((XtarL - TL * XsrcL)**2)) / nl)
                trerrR = np.sqrt(sum(sum((XtarR - TR * XsrcR)**2)) / nr)
                if kappa > 0:
                    trerrLsrc = sum(np.var(XsrcL, axis=1, ddof=1)) / Fs
                    trerrRsrc = sum(np.var(XsrcR, axis=1, ddof=1)) / Fs
                    trerrL = (trerrL + kappa * trerrLsrc) / 2
                    trerrR = (trerrR + kappa * trerrRsrc) / 2
                trerr = (nl * trerrL + nr * trerrR) / (nl + nr)
            else:
                raise np.error('Unknown split evaluation type')
            if trerr < rerr:
                rerr = trerr
                thr = tthr
                fid = s
    return fid, thr, rerr
def forestRegrTrain(Xfeat, Xsrc, Xtar, varargin):
    dfs = {
        'M': 1,
        'minChild': 64,
        'minCount': 128,
        'N1': [],
        'F1': [],
        'F2': 5,
        'maxDepth': 64,
        'fWts': [],
        'splitfuntype': 'pair',
        'nodesubsample': 1000,
        'splitevaltype': 'variance',
        'lambda': 0.01,
        'estimatelambda': 0,
        'kappa': 1,
        'leaflearntype': 'linear',
        'usepf': 0,
        'verbose': 0
    }
    opts = srForestTrain.getPrmDflt(varargin, dfs, 1)
    if len(sys.argv) == 0:
        forest = opts
        return forest

    Ff, N = np.shape(Xfeat)
    Ncheck = Xsrc.shape[1]
    assert (N == Ncheck)
    Ncheck = Xtar.shape[1]
    assert (N == Ncheck)
    if len(opts['N1']) == 0:
        opts['N1'] = round(N * 0.75)
        opts['N1'] = min(N, opts['N1'])
    if len(opts['F1']) == 0:
        opts['F1'] = round(math.sqrt(Ff))
        opts['F1'] = min(Ff, opts['F1'])
    if opts['F2'] < 0:
        raise np.error('F2 should be > -1')
    if opts['fWts'] == []:
        opts['fWts'] = np.ones((1, Ff), np.float32)
        opts['fWts'] = opts['fWts'] / sum(sum(opts['fWts']))
    if opts['nodesubsample'] < opts['minChild'] * 2:
        raise np.error('nodesubsample < 2*minChild')
    if opts['nodesubsample'] < opts['minChild'] * 3:
        warnings.warn('nodesubsample < 3*minChild', DeprecationWarning)

#make sure data has correct types
    pass

    #train M random trees on different subsets of data
    dWtsUni = np.ones((1, N), np.float32)
    dWtsUni = dWtsUni / sum(sum(dWtsUni))
    if opts['usepf'] == 1:
        tem_forest = []
        for i in range(opts['M']):
            if N == opts['N1']:
                d = np.arange(0, N)
            else:
                d = wswor(dWtsUni, opts.N1, 4)
            Xfeat1 = Xfeat[:, d]
            Xsrc1 = Xsrc[:, d]
            Xtar1 = Xtar[:, d]
            temforest = treeRegrTrain(Xfeat1, Xsrc1, Xtar1, opts)
            tem_forest.append(temforest)
        forest = []
        #forest=tem_forest
        for i in range(opts['M']):
            forest.append(tem_forest[i])
    else:
        for i in range(opts['M']):
            if N == opts['N1']:
                d = np.arange(0, N)
            else:
                d = wswor(dWtsUni, opts.N1, 4)
            Xfeat1 = Xfeat[:, d]
            Xsrc1 = Xsrc[:, d]
            Xtar1 = Xtar[:, d]
            tree = treeRegrTrain(Xfeat1, Xsrc1, Xtar1, opts)
            forest = []
            forest = tree

    return forest
def treeRegrTrain(Xfeat, Xsrc, Xtar, opts):

    #    define some constants and the tree model
    N = Xfeat.shape[1]
    K = 2 * N - 1
    thrs = np.zeros((K, 1), np.float32)
    if opts['splitfuntype'] == 'single':
        fids = np.zeros((K, 1), np.int32)
    elif opts['splitfuntype'] == 'pair':
        fids = np.zeros((K, 2), np.int32)
    else:
        raise np.error('Unknown splitfunction type')
    child = np.zeros((K, 1), np.int32)
    count = child
    depth = child
    leaf_info = {'T': [], 'type': -1, 'id': -1}
    #     leafinfo=leafinfo(np.ones((K,1)))
    "map函数的使用?"
    leafinfo = []
    for i in range(K):
        leafinfo.append(leaf_info)
    dids = []
    for j in range(K):
        dids.append([])
    dids[0] = np.arange(K)
    k = 0
    K = 2
    msgnodestep = 200
    # train the tree
    while k < K:
        dids1 = dids[k]
        count[k] = len(dids1)
        XfeatNode = Xtar[:, dids1]
        XsrcNode = Xsrc[:, dids1]
        XtarNode = Xtar[:, dids1]
        if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
            print('Node %d, depth %d, %d samples () ', k, depth[k], count[k])
        if count[k] <= opts['minCount'] or depth[k] > opts[
                'maxDepth'] or count[k] < (2 * opts['minChild']):
            if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
                print('becomes a leaf (stop criterion active)\n')
            leafinfo[k] = createLeaf(XsrcNode, XtarNode, opts['leaflearntype'],
                                     opts['lambda'], opts['estimatelambda'])
            k = k + 1
    #     continue
        if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
            print('find split () ')

    #compute responses for all data samples
        if opts['splitfuntype'] == 'single':
            pass
        elif opts['splitfuntype'] == 'pair':
            fids1 = np.array([
                wswor(opts['fWts'], opts['F1'], 4),
                wswor(opts['fWts'], opts['F1'], 4)
            ])
            # Caution: same feature id could be sampled  -> all zero responses
            resp = XfeatNode[fids1[0, :], :] - XfeatNode[fids1[1, :], :]
        else:
            raise np.error('Unknown splitfunction type')
    #subsample the data for splitfunction node optimization 随机选样本
        if opts['nodesubsample'] > 0 and opts['nodesubsample'] < count[k]:
            randinds = np.random.permutation(count[k])[0:opts['nodesubsample']]
            respSub = resp[:, randinds]
            XsrcSub = XsrcNode[:, randinds]
            XtarSub = XtarNode[:, randinds]
        else:
            respSub = resp
            XsrcSub = XsrcNode
            XtarSub = XtarNode
        if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
            print('subsmpl = %07d/%07d () ', respSub.shape[1], resp.shape[1])
    #find best splitting function and corresponding threshold寻找最优的节点分裂函数和相对应的阈值
        [fid, thr,
         rerr] = findSplitAndThresh(respSub, XsrcSub, XtarSub, opts['F2'],
                                    opts['splitevaltype'], opts['lambda'],
                                    opts['minChild'], opts['kappa'])
        #check validity of the splitting function
        validsplit = 0
        left = resp[fid, :] < thr
        count0 = np.count_nonzero(left)
        fid = fids1[:, fid]
        if rerr != np.inf and count0 >= opts['minChild'] and (
                count[k] - count0) >= opts['minChild']:
            validsplit = 1
        if validsplit == 1:
            child[k] = K
            fids[k, :] = fid
            thrs[k] = thr
            dids[K - 1] = dids1[left]
            dids[K] = dids[~left]
            depth[K - 1:K + 1] = depth[K] + 1
            K = K + 2
            dids[k] = []
            if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
                print('valid split (loss=%.6f)\n', rerr)
        else:
            if opts['verbose'] == 1 and (k % msgnodestep == 0 or k == 1):
                print('invalid split -> leaf\n')
            leafinfo[k] = createLeaf(XsrcNode, XtarNode, opts['leaflearntype'],
                                     opts['lambda'], opts['estimatelambda'])
        k = k + 1


#    K=K-1
#create output model struct
    tree = {
        'fids': fids[0:K, :],
        'thrs': thrs[0:K],
        'child': child[0:K],
        'count': count[0:K],
        'depth': depth[0:K],
        'leafinfo': leafinfo[0:K],
        'dids': []
    }
    #create the leaf-node id mapping
    leafcnt = 0
    for i in range(len(tree['leafinfo'])):
        if len(tree['leafinfo'][i]['T']) != 0:
            leafcnt = leafcnt + 1
            tree['leafinfo'][i]['id'] = leafcnt

    return tree
def arlo(te, y, *args, **kwargs):
    varargin = arlo.varargin
    nargin = arlo.nargin

    # r2 = arlo(te,y)
    #
    # output
    #   r2 r2-map (in Hz)
    #      empty when only one echo is provided

    # input
    #   te  array containing te values (in s)
    #   y   a multi-echo data set of arbitrary dimension
    #       echo should be the last dimension
    #
    # If you use this function please cite
    #
    # Pei M, Nguyen TD, Thimmappa ND, Salustri C, Dong F, Cooper MA, Li J,
    # Prince MR, Wang Y. Algorithm for fast monoexponential fitting based
    # on Auto-Regression on Linear Operations (ARLO) of data.
    # Magn Reson Med. 2015 Feb;73(2):843-50. doi: 10.1002/mrm.25137.
    # Epub 2014 Mar 24. PubMed PMID: 24664497;
    # PubMed Central PMCID:PMC4175304.

    nte = len(te)
    # arlo.m:22
    if nte < 2:
        r2 = []
        # arlo.m:24
        return r2

    sz = np.shape(y)
    # arlo.m:28
    edx = np.size(sz)
    # arlo.m:29
    if sz[edx - 1] != nte:
        np.error(
            np.concat(('Last dimension of y has size ', np.num2str(sz(edx)),
                       ', expected ', np.num2str(nte))))

    yy = np.zeros((np.arange(0, edx - 2)))
    # arlo.m:35
    yx = np.zeros((np.arange(0, edx - 2)))
    # arlo.m:36
    beta_yx = np.zeros((np.arange(0, edx - 2)))
    # arlo.m:37
    beta_xx = np.zeros((np.arange(0, edx - 2)))
    # arlo.m:38
    s1 = []
    # arlo.m:39
    d1 = []
    # arlo.m:39
    crd = tile(':', (0, edx - 1))
    # arlo.m:40
    crd0 = copy(crd)
    # arlo.m:41
    crd1 = copy(crd)
    # arlo.m:41
    crd2 = copy(crd)
    # arlo.m:41
    for j in arange(0, nte - 3).reshape(-1):
        alpha = dot((te[j + 2] - te[j]),
                    (te[j + 2] - te[j])) / 2 / (te[j + 1] - te[j])
        # arlo.m:43
        tmp = (dot(dot(2, te[j + 2]), te[j + 2]) - dot(te[j], te[j + 2]) -
               dot(te[j], te[j]) + dot(dot(3, te[j]), te[j + 1]) -
               dot(dot(3, te[j + 1]), te[j + 2])) / 6
        # arlo.m:44
        beta = tmp / (te[j + 2] - te[j + 1])
        # arlo.m:45
        gamma = tmp / (te[j + 1] - te[j])
        # arlo.m:46
        crd0[edx - 1] = j
        # arlo.m:47
        crd1[edx - 1] = j + 1
        # arlo.m:47
        crd2[edx - 1] = j + 2
        # arlo.m:47
        #     [te(j+2)-te(j)-alpha+gamma alpha-beta-gamma beta]/((te(2)-te(1))/3)
        y1 = dot(y(crd0[arange()]), (te(j + 2) - te(j) - alpha + gamma)) + dot(
            y(crd1[arange()]),
            (alpha - beta - gamma)) + dot(y(crd2[arange()]), beta)
        # arlo.m:49
        x1 = y(crd0[arange()]) - y(crd2[arange()])
        # arlo.m:50
        yy = yy + multiply(y1, y1)
        # arlo.m:51
        yx = yx + multiply(y1, x1)
        # arlo.m:52
        beta_yx = beta_yx + multiply(dot(beta, y1), x1)
        # arlo.m:53
        beta_xx = beta_xx + multiply(dot(beta, x1), x1)
# arlo.m:54

    r2 = (yx + beta_xx) / (beta_yx + yy)
    # arlo.m:57
    r2[isnan(r2)] = 0
    # arlo.m:58
    r2[isinf(r2)] = 0