def extract_dynamic_texture(colors, states):
    """
    extract dynamic texture of given frame.
    three frames are given, (i-1)-th, i-th, (i+1)-th, and they are 3ch color image.
    convert them into gray-scale first, then extract dynamic texture.
    It returns feature of i-th frame.


    :param colors: 3ch color image
    :param states: states == step
    :return: float64 image [-1,1]
    """
    grays = []
    for i in range(states):
        gray = cv2.cvtColor(colors[i], cv2.COLOR_RGB2GRAY)
        grays.append(gray)

    size = grays[0].size
    shape = grays[0].shape
    length = len(grays)

    data = np.zeros((size, length))  # size of image, number of images

    for i in range(len(grays)):
        data[:, i] = grays[i].reshape(-1)

    lds = LinearDS(states, False, False)  # number of data
    lds.suboptimalSysID(data)

    for i in range(states):
        a = lds._Chat[:, i].reshape(shape)
        grays.append(a)

    return lds._Chat[:, 1].reshape(shape)
Example #2
0
def test_convertToJCF():
    """Test JCF w.r.t. similarity transforms (random orth. matrices)
    """
    
    np.random.seed(1234)
    dsFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    ds = pickle.load(open(dsFile))
    
    # extract real (A,C) pair
    A,C,N = ds._Ahat, ds._Chat, ds._nStates
    P = LinearDS.computeJCFTransform(A,C)
    
    # compute JCF of original LDS's (A,C) pair
    Ac = P*A*np.linalg.inv(P) 
    Cc = C*np.linalg.inv(P)

    # create random orthogonal matrices
    for i in range(100):
        Q = orth(np.random.random((N,N)))
        
        # apply similarity transform on (A,C)
        tC = C.dot(np.linalg.inv(Q))
        tA = Q*A*np.linalg.inv(Q)

        # now, compute the JCF transform and apply it
        R = LinearDS.computeJCFTransform(tA,tC)
        Ar = R*tA*np.linalg.inv(R)
        Cr = tC*np.linalg.inv(R) 
        
        # ensure that (A,C)'s remain equal
        errA = np.linalg.norm(Ac-Ar, 'fro') 
        errC = np.linalg.norm(Cc-Cr, 'fro') 
        np.testing.assert_almost_equal(errA, 0, 5)
        np.testing.assert_almost_equal(errC, 0, 5)
Example #3
0
def test_convertToJCF():
    """Test JCF w.r.t. similarity transforms (random orth. matrices)
    """

    np.random.seed(1234)
    dsFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    ds = pickle.load(open(dsFile))

    # extract real (A,C) pair
    A,C,N = ds._Ahat, ds._Chat, ds._nStates
    P = LinearDS.computeJCFTransform(A,C)

    # compute JCF of original LDS's (A,C) pair
    Ac = P*A*np.linalg.inv(P)
    Cc = C*np.linalg.inv(P)

    # create random orthogonal matrices
    for i in range(100):
        Q = orth(np.random.random((N,N)))

        # apply similarity transform on (A,C)
        tC = C.dot(np.linalg.inv(Q))
        tA = Q*A*np.linalg.inv(Q)

        # now, compute the JCF transform and apply it
        R = LinearDS.computeJCFTransform(tA,tC)
        Ar = R*tA*np.linalg.inv(R)
        Cr = tC*np.linalg.inv(R)

        # ensure that (A,C)'s remain equal
        errA = np.linalg.norm(Ac-Ar, 'fro')
        errC = np.linalg.norm(Cc-Cr, 'fro')
        np.testing.assert_almost_equal(errA, 0, 2)
        np.testing.assert_almost_equal(errC, 0, 2)
Example #4
0
def test_ldsMartinDistance():
    """Test Martin distance computation for LDS's (5 states).

    Config: 5 states (observation centering - default).
    """

    fileA = os.path.join(TESTBASE, "data/data1.txt")
    fileB = os.path.join(TESTBASE, "data/data2.txt")

    # load data files
    dataA, _ = loadDataFromASCIIFile(fileA)
    dataB, _ = loadDataFromASCIIFile(fileB)

    ldsA = LinearDS(5, False, False)
    ldsB = LinearDS(5, False, False)

    # estimate LDS's
    ldsA.suboptimalSysID(dataA)
    ldsB.suboptimalSysID(dataB)

    # compute distances A<->A, A<->B
    dAA = ldsMartinDistance(ldsA, ldsA, 20)
    dAB = ldsMartinDistance(ldsA, ldsB, 20)

    truth = np.genfromtxt(os.path.join(TESTBASE, "data/ldsMartinDistanceData1Data2.txt"))
    np.testing.assert_almost_equal(dAB, truth, 2)
    np.testing.assert_almost_equal(dAA, 0, 2)
Example #5
0
def test_LinearDS_suboptimalSysID(): 
    """Test LinearDS system identification.
    """
    
    dataFile = os.path.join(TESTBASE, "data/data1.txt")
    data, _ = loadDataFromASCIIFile(dataFile)
     
    lds = LinearDS(5, False, False)
    lds.suboptimalSysID(data)
     
    baseLDSFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    baseLDS = pickle.load(open(baseLDSFile))
     
    _, err = LinearDS.stateSpaceMap(baseLDS, lds)
    assert np.allclose(err, 0.0) == True
Example #6
0
def test_LinearDS_suboptimalSysID():
    """Test LinearDS system identification.
    """

    dataFile = os.path.join(TESTBASE, "data/data1.txt")
    data, _ = loadDataFromASCIIFile(dataFile)

    lds = LinearDS(5, False, False)
    lds.suboptimalSysID(data)

    baseLDSFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    baseLDS = pickle.load(open(baseLDSFile))

    err = ldsMartinDistance(lds, baseLDS, N=20)
    np.testing.assert_almost_equal(err, 0, 2)
Example #7
0
def test_computeRJF_part0():
    """Test computeRJF() with random 3x3 ... 10x10 matrices.
    """
    
    np.random.seed(1234)
    for d in range(3,10):
        A = np.random.random((d,d))
        J,T,_ = LinearDS.computeRJF(A)
        Ac = T.dot(A).dot(np.linalg.inv(T))
        np.testing.assert_almost_equal(np.linalg.norm(J-Ac, 'fro'), 0)
Example #8
0
def test_computeRJF_part0():
    """Test computeRJF() with random 3x3 ... 10x10 matrices.
    """

    np.random.seed(1234)
    for d in range(3,10):
        A = np.random.random((d,d))
        J,T,_ = LinearDS.computeRJF(A)
        Ac = T.dot(A).dot(np.linalg.inv(T))
        np.testing.assert_almost_equal(np.linalg.norm(J-Ac, 'fro'), 0, 2)
Example #9
0
def test_computeRJF_part1():
    """Test computeRJF() with MATLAB example.
    """ 
    
    # MATLAB test example, see 'doc jordan' in MATLAB
    A = np.asarray([[1,-3,-2],
                    [-1,1,-1],
                    [2,4,5]])
    J,T,_ = LinearDS.computeRJF(A)
    ref = np.asarray([[3,0,0],
                      [0,2,0],
                      [0,0,2]])
    np.testing.assert_almost_equal(np.linalg.norm(J-ref,'fro'), 0)
Example #10
0
def test_computeRJF_part1():
    """Test computeRJF() with MATLAB example.
    """

    # MATLAB test example, see 'doc jordan' in MATLAB
    A = np.asarray([[1,-3,-2],
                    [-1,1,-1],
                    [2,4,5]])
    J,T,_ = LinearDS.computeRJF(A)
    ref = np.asarray([[3,0,0],
                      [0,2,0],
                      [0,0,2]])
    np.testing.assert_almost_equal(np.linalg.norm(J-ref,'fro'), 0, 2)
Example #11
0
def main(argv=None):
    if argv is None: 
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-i", dest="iListFile") 
    parser.add_option("-o", dest="oListFile")
    parser.add_option("-d", dest="dMatFile")
    parser.add_option("-b", dest="iBase")
    parser.add_option("-k", dest="kCenter", type="int", default=5)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False) 
    options, args = parser.parse_args()
    
    if options.shoHelp: 
        usage()
    
    iBase = options.iBase
    dMatFile = options.dMatFile
    iListFile = options.iListFile
    oListFile = options.oListFile
    kCenter = options.kCenter
    verbose = options.verbose

    assert kCenter > 0, "Oops (kCenter < 1) ..."
 
    iList = pickle.load(open(iListFile))
 
    if verbose:
        dsinfo.info("Loaded list with %d DT models!" % len(iList))
 
    # load DT's
    dts = []
    for dtFile in iList:
        dts.append(pickle.load(open(os.path.join(iBase, dtFile))))
    
    if verbose:
        dsinfo.info("Running DT clustering with %d clusters ..." % kCenter)
    
    D = None
    if not dMatFile is None:
        if os.path.exists(dMatFile):    
            if verbose:
                dsinfo.info("Try loading distance matrix %s!" % dMatFile)
            D = pickle.load(open(dMatFile))

    if D is None:
        if verbose:
            dsinfo.info("Computing pairwise distances ...")
        nDTs = len(dts)
        D = np.zeros((nDTs, nDTs))
        for i in range(nDTs):
            for j in range(nDTs):
                D[i,j] = ldsMartinDistance(dts[i], dts[j], 50)
    
    # run clustering
    (eData, ids) = LinearDS.cluster(D, kCenter, verbose)
    ids = list(ids)
    
    #write list of DT representatives
    oList = []
    for j, dtFile in enumerate(iList):
            if j in ids:
                oList.append(dtFile)
            else:
                oList.append("Dummy")
    pickle.dump(oList, open(oListFile, "w"))

    if verbose:
        dsinfo.info("Wrote list of representative DS's to %s!" % oListFile)

    if not dMatFile is None:
        if verbose:
            dsinfo.info("Writing distance matrix to %s!" % dMatFile)
        pickle.dump(D, open(dMatFile, "w"))
Example #12
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-i", dest="iListFile")
    parser.add_option("-o", dest="oListFile")
    parser.add_option("-d", dest="dMatFile")
    parser.add_option("-b", dest="iBase")
    parser.add_option("-k", dest="kCenter", type="int", default=5)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False)
    options, args = parser.parse_args()

    if options.shoHelp:
        usage()

    iBase = options.iBase
    dMatFile = options.dMatFile
    iListFile = options.iListFile
    oListFile = options.oListFile
    kCenter = options.kCenter
    verbose = options.verbose

    assert kCenter > 0, "Oops (kCenter < 1) ..."

    iList = pickle.load(open(iListFile))

    if verbose:
        dsinfo.info("Loaded list with %d DT models!" % len(iList))

    # load DT's
    dts = []
    for dtFile in iList:
        dts.append(pickle.load(open(os.path.join(iBase, dtFile))))

    if verbose:
        dsinfo.info("Running DT clustering with %d clusters ..." % kCenter)

    D = None
    if not dMatFile is None:
        if os.path.exists(dMatFile):
            if verbose:
                dsinfo.info("Try loading distance matrix %s!" % dMatFile)
            D = pickle.load(open(dMatFile))

    if D is None:
        if verbose:
            dsinfo.info("Computing pairwise distances ...")
        nDTs = len(dts)
        D = np.zeros((nDTs, nDTs))
        for i in range(nDTs):
            for j in range(nDTs):
                D[i, j] = ldsMartinDistance(dts[i], dts[j], 50)

    # run clustering
    (eData, ids) = LinearDS.cluster(D, kCenter, verbose)
    ids = list(ids)

    #write list of DT representatives
    oList = []
    for j, dtFile in enumerate(iList):
        if j in ids:
            oList.append(dtFile)
        else:
            oList.append("Dummy")
    pickle.dump(oList, open(oListFile, "w"))

    if verbose:
        dsinfo.info("Wrote list of representative DS's to %s!" % oListFile)

    if not dMatFile is None:
        if verbose:
            dsinfo.info("Writing distance matrix to %s!" % dMatFile)
        pickle.dump(D, open(dMatFile, "w"))
Example #13
0
File: dt.py Project: rkwitt/PyDSTK
def main(argv=None):
    if argv is None: 
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-p", dest="pFile")
    parser.add_option("-i", dest="iFile") 
    parser.add_option("-t", dest="iType")
    parser.add_option("-o", dest="oFile")
    parser.add_option("-n", dest="nStates", type="int", default=+5)
    parser.add_option("-m", dest="doMovie", type="int", default=-1)
    parser.add_option("-a", dest="svdRand", action="store_true", default=False)
    parser.add_option("-e", dest="doEstim", action="store_true", default=False)
    parser.add_option("-s", dest="doSynth", action="store_true", default=False)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False) 
    opt, args = parser.parse_args()
    
    if opt.shoHelp: 
        usage()
    
    dataMat = None
    dataSiz = None
    try:
        if opt.iType == 'vFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVideoFile(opt.iFile)
        elif opt.iType == 'aFile':
            (dataMat, dataSiz) = dsutil.loadDataFromASCIIFile(opt.iFile)
        elif opt.iType == 'lFile':
            (dataMat, dataSiz) = dsutil.loadDataFromIListFile(opt.iFile)
        else:
            msg.fail("Unsupported file type : %s", opt.iType)    
            return -1
    except Exception as e:
        dsinfo.fail(e)
        return -1

    try:
        # try loading the DT model
        if not opt.pFile is None:
            with open(opt.pFile) as fid:
                dsinfo.info('trying to load model %s' % opt.pFile)
                dt = pickle.load(fid)

        # run estimation
        if opt.doEstim:
            if not opt.pFile is None:
                dsinfo.fail('re-estimation attempt detected!')
                return -1
            dt = LinearDS(opt.nStates, approx=opt.svdRand, verbose=opt.verbose)
            dt.suboptimalSysID(dataMat)

        # synthesize output
        if opt.doSynth:
           dataSyn, _ = dt.synthesize(tau=50, mode='s')

        # show a movie of the synthesis result
        if opt.doMovie > 0:
            if opt.doSynth:
                dsutil.showMovie(dataSyn, dataSiz, fps=opt.doMovie)

        # write DT model to file
        if not opt.oFile is None:
            dsinfo.info('writing model to %s' % opt.oFile)
            with open(opt.oFile, 'w') as fid:
                pickle.dump(dt, fid)
     
    # catch pyds exceptions
    except ErrorDS as e:
        dsinfo.fail(e)
        return -1
Example #14
0
def test_LinearDS_check():
    """Test LinearDS parameter checking.
    """
    
    lds = LinearDS(5, False, False)
    assert lds.check() is False
Example #15
0
def test_LinearDS_check():
    """Test LinearDS parameter checking.
    """

    lds = LinearDS(5, False, False)
    assert lds.check() is False
Example #16
0
def main(argv=None):
    if argv is None: 
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-p", dest="pFile")
    parser.add_option("-i", dest="iFile") 
    parser.add_option("-t", dest="iType")
    parser.add_option("-o", dest="oFile")
    parser.add_option("-n", dest="nStates", type="int", default=+5)
    parser.add_option("-m", dest="doMovie", type="int", default=-1)
    parser.add_option("-a", dest="svdRand", action="store_true", default=False)
    parser.add_option("-e", dest="doEstim", action="store_true", default=False)
    parser.add_option("-s", dest="doSynth", action="store_true", default=False)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False) 
    opt, args = parser.parse_args()
    
    if opt.shoHelp: 
        usage()
    
    dataMat = None
    dataSiz = None
    try:
        if opt.iType == 'vFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVideoFile(opt.iFile)
        elif opt.iType == 'aFile':
            (dataMat, dataSiz) = dsutil.loadDataFromASCIIFile(opt.iFile)
        elif opt.iType == 'lFile':
            (dataMat, dataSiz) = dsutil.loadDataFromIListFile(opt.iFile)
        elif opt.iType == 'mFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVolumeFile(opt.iFile)
        else:
            msg.fail("Unsupported file type : %s", opt.iType)    
            return -1
    except Exception as e:
        dsinfo.fail(e)
        return -1

    try:
        # try loading the DT model
        if not opt.pFile is None:
            with open(opt.pFile) as fid:
                dsinfo.info('trying to load model %s' % opt.pFile)
                dt = pickle.load(fid)

        # run estimation
        if opt.doEstim:
            if not opt.pFile is None:
                dsinfo.fail('re-estimation attempt detected!')
                return -1
            dt = LinearDS(opt.nStates, approx=opt.svdRand, verbose=opt.verbose)
            dt.suboptimalSysID(dataMat)

        # synthesize output
        if opt.doSynth:
           dataSyn, _ = dt.synthesize(tau=50, mode='s')

        # show a movie of the synthesis result
        if opt.doMovie > 0:
            if opt.doSynth:
                dsutil.showMovie(dataSyn, dataSiz, fps=opt.doMovie)

        # write DT model to file
        if not opt.oFile is None:
            dsinfo.info('writing model to %s' % opt.oFile)
            with open(opt.oFile, 'w') as fid:
                pickle.dump(dt, fid)
     
    # catch pyds exceptions
    except ErrorDS as e:
        dsinfo.fail(e)
        return -1