def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) 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("-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: dsinfo.fail("Unsupported file type : %s" % opt.iType) return -1 # catch pyds exceptions except ErrorDS as e: msg.fail(e) return -1 try: kpcaP = KPCAParam() kpcaP._kPar = RBFParam() kpcaP._kPar._kCen = True kpcaP._kFun = rbfK kdt = NonLinearDS(opt.nStates, kpcaP, opt.verbose) kdt.suboptimalSysID(dataMat) if not opt.oFile is None: if not kdt.check(): dsinfo.fail('cannot write invalid model!') return -1 dsinfo.info('writing model to %s' % opt.oFile) with open(opt.oFile, 'w') as fid: pickle.dump(kdt, fid) except ErrorDS as e: dsinfo.fail(e) return -1
def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) parser.add_option("-s", dest="model1File") parser.add_option("-r", dest="model2File") parser.add_option("-n", dest="iterations", type="int", default=20) 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() with open(opt.model1File, 'r') as fid: dt1 = pickle.load(fid) with open(opt.model2File, 'r') as fid: dt2 = pickle.load(fid) martinD = dsdist.ldsMartinDistance(dt1, dt2, opt.iterations) dsinfo.info('D(%s,%s) = %.4f' % (opt.model1File, opt.model2File, martinD))
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"))
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"))
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
def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) parser.add_option("-s", dest="inFile") parser.add_option("-d", dest="dbFile") parser.add_option("-m", dest="models") parser.add_option("-v", dest="videos") parser.add_option("-c", dest="config") parser.add_option("-o", dest="mdFile") parser.add_option("-h", dest="doUsage", action="store_true", default=False) parser.add_option("-x", dest="verbose", action="store_true", default=False) options, args = parser.parse_args() if options.doUsage: usage() # read config file config = json.load(open(options.config)) # get DS config settings dynType = config["dynType"] shiftMe = config["shiftMe"] numIter = config["numIter"] verbose = options.verbose # I/O configuration inFile = options.inFile dbFile = options.dbFile models = options.models videos = options.videos mdFile = options.mdFile # check if the required options are present if (inFile is None or dbFile is None or models is None or videos is None): dsinfo.warn('Options missing!') usage() inVideo, inVideoSize = dsutil.loadDataFromVideoFile(inFile) if verbose: dsinfo.info("Loaded source video with %d frames!" % inVideo.shape[1]) (db, winSize, nStates, dynType) = loadDB(videos, models, dbFile) if verbose: dsinfo.info("#Templates: %d #States: %d, WinSize: %d, Shift: %d" % (len(db), nStates, winSize, shiftMe)) if dynType.__name__ == "LinearDS": # create online version of LinearDS ds = OnlineLinearDS(nStates, winSize, shiftMe, False, verbose) elif dynType.__name__ == "NonLinearDS": kpcaP = KPCAParam() # select kernel if config["kdtKern"] == "rbf": kpcaP._kPar = RBFParam() kpcaP._kFun = rbfK else: dsinfo.fail("Kernel %s not supported!" % kdtKern) return -1 # configure kernel if config["kCenter"] == 1: kpcaP._kPar._kCen = True else: kpcaP._kPar._kCen = False # create online version of KDT ds = OnlineNonLinearDS(nStates, kpcaP, winSize, shiftMe, verbose) else: dsinfo.fail('System type %s not supported!' % options.dsType) return -1 dList = [] for f in range(inVideo.shape[1]): ds.update(inVideo[:, f]) if ds.check() and ds.hasChanged(): dists = np.zeros((len(db), )) for j, dbentry in enumerate(db): dists[j] = { "LinearDS": dsdist.ldsMartinDistance, "NonLinearDS": dsdist.nldsMartinDistance }[dynType.__name__](ds, dbentry["model"], numIter) dList.append(dists) # write distance matrix if not mdFile is None: np.savetxt(mdFile, np.asmatrix(dList), fmt='%.5f', delimiter=' ')
def suboptimalSysID(self, Y): """Suboptimal system identification using SVD. Suboptimal system identification based on SVD, as proposed in the original work of Doretto et al. [1]. Parameters ---------- Y : numpy array, shape = (N, D) Input data with D observations as N-dimensional column vectors. """ nStates = self._nStates if self._verbose: dsinfo.info("using suboptimal SVD-based estimation!") (N, tau) = Y.shape Yavg = np.mean(Y, axis=1) Y = Y - Yavg[:,np.newaxis] if self._approx: if self._verbose: with Timer('randomized_svd'): (U, S, V) = randomized_svd(Y, nStates) else: (U, S, V) = randomized_svd(Y, nStates) else: if self._verbose: with Timer('np.linalg.svd'): (U, S, V) = np.linalg.svd(Y, full_matrices=0) else: (U, S, V) = np.linalg.svd(Y, full_matrices=0) Chat = U[:,0:nStates] Xhat = (np.diag(S)[0:nStates,0:nStates] * np.asmatrix(V[0:nStates,:])) initM0 = np.mean(Xhat[:,0], axis=1) initS0 = np.zeros((nStates, 1)) pind = range(tau-1); phi1 = Xhat[:,pind] phi2 = Xhat[:,[i+1 for i in pind]] Ahat = phi2*np.linalg.pinv(phi1) Vhat = phi2-Ahat*phi1; Qhat = 1.0/Vhat.shape[1] * Vhat*Vhat.T errorY = Y - Chat*Xhat Rhat = np.var(errorY.ravel()) # save parameters self._initS0 = initS0 self._initM0 = initM0 self._Yavg = Yavg self._Ahat = Ahat self._Chat = Chat self._Xhat = Xhat self._Qhat = Qhat self._Rhat = Rhat if self.check(): self._ready = True
def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) parser.add_option("-s", dest="inFile") parser.add_option("-d", dest="dbFile") parser.add_option("-m", dest="models") parser.add_option("-v", dest="videos") parser.add_option("-c", dest="config") parser.add_option("-o", dest="mdFile") parser.add_option("-h", dest="doUsage", action="store_true", default=False) parser.add_option("-x", dest="verbose", action="store_true", default=False) options, args = parser.parse_args() if options.doUsage: usage() # read config file config = json.load(open(options.config)) # get DS config settings dynType = config["dynType"] shiftMe = config["shiftMe"] numIter = config["numIter"] verbose = options.verbose # I/O configuration inFile = options.inFile dbFile = options.dbFile models = options.models videos = options.videos mdFile = options.mdFile # check if the required options are present if inFile is None or dbFile is None or models is None or videos is None: dsinfo.warn("Options missing!") usage() inVideo, inVideoSize = dsutil.loadDataFromVideoFile(inFile) if verbose: dsinfo.info("Loaded source video with %d frames!" % inVideo.shape[1]) (db, winSize, nStates, dynType) = loadDB(videos, models, dbFile) if verbose: dsinfo.info("#Templates: %d #States: %d, WinSize: %d, Shift: %d" % (len(db), nStates, winSize, shiftMe)) if dynType.__name__ == "LinearDS": # create online version of LinearDS ds = OnlineLinearDS(nStates, winSize, shiftMe, False, verbose) elif dynType.__name__ == "NonLinearDS": kpcaP = KPCAParam() # select kernel if config["kdtKern"] == "rbf": kpcaP._kPar = RBFParam() kpcaP._kFun = rbfK else: dsinfo.fail("Kernel %s not supported!" % kdtKern) return -1 # configure kernel if config["kCenter"] == 1: kpcaP._kPar._kCen = True else: kpcaP._kPar._kCen = False # create online version of KDT ds = OnlineNonLinearDS(nStates, kpcaP, winSize, shiftMe, verbose) else: dsinfo.fail("System type %s not supported!" % options.dsType) return -1 dList = [] for f in range(inVideo.shape[1]): ds.update(inVideo[:, f]) if ds.check() and ds.hasChanged(): dists = np.zeros((len(db),)) for j, dbentry in enumerate(db): dists[j] = {"LinearDS": dsdist.ldsMartinDistance, "NonLinearDS": dsdist.nldsMartinDistance}[ dynType.__name__ ](ds, dbentry["model"], numIter) dList.append(dists) # write distance matrix if not mdFile is None: np.savetxt(mdFile, np.asmatrix(dList), fmt="%.5f", delimiter=" ")
def suboptimalSysID(self, Y): """Suboptimal system identification using SVD. Suboptimal system identification based on SVD, as proposed in the original work of Doretto et al. [1]. Parameters ---------- Y : numpy array, shape = (N, D) Input data with D observations as N-dimensional column vectors. """ nStates = self._nStates if self._verbose: dsinfo.info("using suboptimal SVD-based estimation!") (N, tau) = Y.shape Yavg = np.mean(Y, axis=1) Y = Y - Yavg[:, np.newaxis] if self._approx: if self._verbose: with Timer('randomized_svd'): (U, S, V) = randomized_svd(Y, nStates) else: (U, S, V) = randomized_svd(Y, nStates) else: if self._verbose: with Timer('np.linalg.svd'): (U, S, V) = np.linalg.svd(Y, full_matrices=0) else: (U, S, V) = np.linalg.svd(Y, full_matrices=0) Chat = U[:, 0:nStates] Xhat = (np.diag(S)[0:nStates, 0:nStates] * np.asmatrix(V[0:nStates, :])) initM0 = np.mean(Xhat[:, 0], axis=1) initS0 = np.zeros((nStates, 1)) pind = range(tau - 1) phi1 = Xhat[:, pind] phi2 = Xhat[:, [i + 1 for i in pind]] Ahat = phi2 * np.linalg.pinv(phi1) Vhat = phi2 - Ahat * phi1 Qhat = 1.0 / Vhat.shape[1] * Vhat * Vhat.T errorY = Y - Chat * Xhat Rhat = np.var(errorY.ravel()) # save parameters self._initS0 = initS0 self._initM0 = initM0 self._Yavg = Yavg self._Ahat = Ahat self._Chat = Chat self._Xhat = Xhat self._Qhat = Qhat self._Rhat = Rhat if self.check(): self._ready = True
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