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("-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 loadDB(videoDir, modelDir, dbFile): """Load database information. Parameters ---------- videoDir : string Base directory where the videos reside. modelDir: string Base directory where the models reside. dbFile: string Filename of the database file in JOSN format. Returns ------- db : list List of N dictionaries with keys: "video" -- Name of DS file "label" -- Name of corresponding AVI video file "model" -- Loaded DS model winSize : int Length (#frames) of the templates nStates : int Number of DS models for templates dynType : string DS type (i.e., the result of type(...)) """ dbinfo = json.load(open(dbFile)) db, labels = [], [] winSize = set() nStates = set() dynType = set() for entry in dbinfo: tplEntry = entry["ks"] # Key sequence name labEntry = entry["cl"] # Class label of key sequence res = glob.glob(os.path.join(videoDir, '%s*.avi' % tplEntry)) for entry in res: videoFile = os.path.basename(entry) modelFile = os.path.splitext(videoFile)[0] + ".pkl" modelFile = os.path.join(modelDir, modelFile) if not os.path.exists(modelFile): dsinfo.fail("%s does not exist!" % modelFile) raise Exception() db.append({ "model": pickle.load(open(modelFile)), "video": videoFile, "label": labEntry }) winSize.add(db[-1]["model"]._Xhat.shape[1]) nStates.add(db[-1]["model"]._nStates) dynType.add(type(db[-1]["model"])) if not (len(winSize) == 1 and len(nStates) == 1 and len(dynType) == 1): dsinfo.fail("Incompatible template configuration!") raise Exception() return ( db, iter(winSize).next(), # common sliding window size iter(nStates).next(), # common number of DS states iter(dynType).next()) # common DS type
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 loadDB(videoDir, modelDir, dbFile): """Load database information. Parameters ---------- videoDir : string Base directory where the videos reside. modelDir: string Base directory where the models reside. dbFile: string Filename of the database file in JOSN format. Returns ------- db : list List of N dictionaries with keys: "video" -- Name of DS file "label" -- Name of corresponding AVI video file "model" -- Loaded DS model winSize : int Length (#frames) of the templates nStates : int Number of DS models for templates dynType : string DS type (i.e., the result of type(...)) """ dbinfo = json.load(open(dbFile)) db, labels = [], [] winSize = set() nStates = set() dynType = set() for entry in dbinfo: tplEntry = entry["ks"] # Key sequence name labEntry = entry["cl"] # Class label of key sequence res = glob.glob(os.path.join(videoDir, "%s*.avi" % tplEntry)) for entry in res: videoFile = os.path.basename(entry) modelFile = os.path.splitext(videoFile)[0] + ".pkl" modelFile = os.path.join(modelDir, modelFile) if not os.path.exists(modelFile): dsinfo.fail("%s does not exist!" % modelFile) raise Exception() db.append({"model": pickle.load(open(modelFile)), "video": videoFile, "label": labEntry}) winSize.add(db[-1]["model"]._Xhat.shape[1]) nStates.add(db[-1]["model"]._nStates) dynType.add(type(db[-1]["model"])) if not (len(winSize) == 1 and len(nStates) == 1 and len(dynType) == 1): dsinfo.fail("Incompatible template configuration!") raise Exception() return ( db, iter(winSize).next(), # common sliding window size iter(nStates).next(), # common number of DS states iter(dynType).next(), ) # common DS type
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 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