def _init_memory(self): logger.info("Populating replay memory with epsilon={} ...".format(self.exploration)) with get_tqdm(total=self.init_memory_size) as pbar: while len(self.mem) < self.init_memory_size: self._populate_exp() pbar.update() self._init_memory_flag.set()
def _init_memory(self): logger.info("Populating replay memory with epsilon={} ...".format(self.exploration)) with get_tqdm(total=self.init_memory_size) as pbar: while len(self.mem) < self.init_memory_size: self.runner.step(self.exploration) pbar.update() self._init_memory_flag.set()
def _fake_init_memory(self): from copy import deepcopy with get_tqdm(total=self.init_memory_size) as pbar: while len(self.mem) < 5: self.runner.step(self.exploration) pbar.update() while len(self.mem) < self.init_memory_size: self.mem.append(deepcopy(self.mem._hist[0])) pbar.update() self._init_memory_flag.set()
def eval_with_funcs(predictors, nr_eval, get_player_fn, verbose=False): """ Args: predictors ([PredictorBase]) """ class Worker(StoppableThread, ShareSessionThread): def __init__(self, func, queue): super(Worker, self).__init__() self._func = func self.q = queue def func(self, *args, **kwargs): if self.stopped(): raise RuntimeError("stopped!") return self._func(*args, **kwargs) def run(self): with self.default_sess(): player = get_player_fn(train=False) while not self.stopped(): try: score = play_one_episode(player, self.func) except RuntimeError: return self.queue_put_stoppable(self.q, score) q = queue.Queue() threads = [Worker(f, q) for f in predictors] for k in threads: k.start() time.sleep(0.1) # avoid simulator bugs stat = StatCounter() def fetch(): r = q.get() stat.feed(r) if verbose: logger.info("Score: {}".format(r)) for _ in get_tqdm(range(nr_eval)): fetch() # waiting is necessary, otherwise the estimated mean score is biased logger.info("Waiting for all the workers to finish the last run...") for k in threads: k.stop() for k in threads: k.join() while q.qsize(): fetch() if stat.count > 0: return (stat.average, stat.max) return (0, 0)
def compute_mean_std(db, fname): ds = LMDBSerializer.load(db, shuffle=False) ds.reset_state() o = OnlineMoments() for dp in get_tqdm(ds): feat = dp[0] # len x dim for f in feat: o.feed(f) logger.info("Writing to {} ...".format(fname)) with open(fname, 'wb') as f: f.write(serialize.dumps([o.mean, o.std]))
def predict_dataflow(df, model_func, tqdm_bar=None): """ Args: df: a DataFlow which produces (image, image_id) model_func: a callable from the TF model. It takes image and returns (boxes, probs, labels, [masks]) tqdm_bar: a tqdm object to be shared among multiple evaluation instances. If None, will create a new one. Returns: list of dict, in the format used by `DatasetSplit.eval_inference_results` """ df.reset_state() all_results = [] with ExitStack() as stack: # tqdm is not quite thread-safe: https://github.com/tqdm/tqdm/issues/323 if tqdm_bar is None: tqdm_bar = stack.enter_context(get_tqdm(total=df.size())) for img, img_id in df: results = predict_image(img, model_func) for r in results: # int()/float() to make it json-serializable # adding new outputs if hasattr(r, 'proposal_box'): res = { 'image_id': img_id, 'category_id': r.class_id, 'bbox': r.box, 'score': r.score, } res.update({ 'proposal_box': r.proposal_box, 'proposal_score': r.proposal_score, 'frcnn_score': r.frcnn_score }) else: res = { 'image_id': img_id, 'category_id': int(r.class_id), 'bbox': [round(float(x), 4) for x in r.box], 'score': round(float(r.score), 4), } # also append segmentation to results if r.mask is not None: rle = cocomask.encode(np.array(r.mask[:, :, None], order='F'))[0] rle['counts'] = rle['counts'].decode('ascii') res['segmentation'] = rle all_results.append(res) tqdm_bar.update(1) return all_results
def _init_memory(self): logger.info("Populating replay memory with epsilon={} ...".format(self.exploration)) # fill some for the history for k in range(self.history_len): self._populate_exp() with get_tqdm(total=self.init_memory_size) as pbar: while len(self.mem) < self.init_memory_size: self._populate_exp() pbar.update() self._init_memory_flag.set()
def predict_dataflow(df, model_func, tqdm_bar=None): """ Args: df: a DataFlow which produces (image, image_id) model_func: a callable from the TF model. It takes image and returns (boxes, probs, labels, [masks]) tqdm_bar: a tqdm object to be shared among multiple evaluation instances. If None, will create a new one. Returns: list of dict, in the format used by `DatasetSplit.eval_inference_results` """ df.reset_state() all_results = [] with ExitStack() as stack: # tqdm is not quite thread-safe: https://github.com/tqdm/tqdm/issues/323 if tqdm_bar is None: tqdm_bar = stack.enter_context(get_tqdm(total=df.size())) for img, img_id in df: results = predict_image(img, model_func, as_named_tuple=True) for r in results: # int()/float() to make it json-serializable # our model can return only the highest score, or the scores of all entities # here, we are just interested in the highest score if isinstance(r.score, Iterable): score = max(r.score) else: score = r.score res = { 'image_id': img_id, 'category_id': int(r.class_id), 'bbox': [round(float(x), 4) for x in r.box], 'score': round(float(score), 4), } # also append segmentation to results if r.mask is not None: import pycocotools.mask as cocomask rle = cocomask.encode( np.array(r.mask[:, :, None], order='F'))[0] rle['counts'] = rle['counts'].decode('ascii') res['segmentation'] = rle all_results.append(res) tqdm_bar.update(1) return all_results
def eval_with_funcs(predictors, nr_eval, get_player_fn, verbose=False): """ Args: predictors ([PredictorBase]) """ class Worker(StoppableThread, ShareSessionThread): def __init__(self, func, queue): super(Worker, self).__init__() self._func = func self.q = queue self.n_ep = 0 def func(self, *args, **kwargs): if self.stopped(): raise RuntimeError("stopped!") return self._func(*args, **kwargs) def run(self): with self.default_sess(): player = get_player_fn(train=False) while not self.stopped(): try: self.n_ep += 1 parser = argparse.ArgumentParser() parser.add_argument( '--gpu', help='comma separated list of GPU(s) to use.') parser.add_argument('--load', help='load model') parser.add_argument('--task', help='task to perform', choices=['play', 'eval', 'train'], default='train') parser.add_argument( '--env', required=True, help= 'either an atari rom file (that ends with .bin) or a gym atari environment name' ) parser.add_argument( '--algo', help='algorithm', choices=['DQN', 'Double', 'Dueling'], default='Double') parser.add_argument('--num-eval', default=50, type=int) parser.add_argument('--ntrain', default='0') args = parser.parse_args() if args.task == 'eval': game = args.env.split('.')[0] + "-" + args.ntrain folder = args.load.split('/')[3].split('.')[0] # game = "seaquest-0" # folder = "model-12500000" with open( "eval/{}/{}/reward_log_{}.txt".format( game, folder, self.n_ep), "w") as f: f.write("TIME\tSTEP\tREWARD\tACTION\n") with open( "eval/{}/{}/image_log_{}.txt".format( game, folder, self.n_ep), "w") as f: f.write( "STEP\tMSE30\tSSIM30\tMSE60\tSSIM60\tMSE120\tSSIM120\tMSE240\tSSIM240\n" ) score = play_one_episode(player, self.func, self.n_ep) except RuntimeError: return self.queue_put_stoppable(self.q, score) q = queue.Queue() threads = [Worker(f, q) for f in predictors] while len(threads) != 1: threads.pop() logger.info("{} workers in threads".format(len(threads))) for k in threads: k.start() time.sleep(0.1) # avoid simulator bugs stat = StatCounter() def fetch(): r = q.get() stat.feed(r) if verbose: logger.info("Score: {}".format(r)) for i in get_tqdm(range(nr_eval)): fetch() # waiting is necessary, otherwise the estimated mean score is biased logger.info("Waiting for all the workers to finish the last run...") for k in threads: k.stop() for k in threads: k.join() while q.qsize(): fetch() if stat.count > 0: return (stat.average, stat.max) return (0, 0)