class Root(object): def __init__(self, **kwargs): if os.path.exists(CNN_FEATURE_EXTRACTOR): app_logger.info("loading... {}".format(CNN_FEATURE_EXTRACTOR)) self.feature_extractor = pickle.load(open(CNN_FEATURE_EXTRACTOR, 'rb')) app_logger.info("done") else: self.feature_extractor = CnnFeatureExtractor(use_gpu, CAFFE_MODEL, MODEL_TYPE, image_feature_dim) pickle.dump(self.feature_extractor, open(CNN_FEATURE_EXTRACTOR, 'wb')) app_logger.info("pickle.dump finished") self.agent_service = AgentService(BRICA_CONFIG_FILE, self.feature_extractor) self.result_logger = ResultLogger() @cherrypy.expose() def flush(self, identifier): self.agent_service.initialize(identifier) @cherrypy.expose def create(self, identifier): body = cherrypy.request.body.read() reward, observation, rotation, movement = unpack(body) inbound_logger.info('reward: {}, depth: {}'.format(reward, observation['depth'])) feature = self.feature_extractor.feature(observation) self.result_logger.initialize() result = self.agent_service.create(reward, feature, identifier) self.result_logger.add_agent(self.agent_service.agents[identifier]) outbound_logger.info('action: {}'.format(result)) return str(result) @cherrypy.expose def step(self, identifier): body = cherrypy.request.body.read() reward, observation, rotation, movement = unpack(body) inbound_logger.info('reward: {}, depth: {}'.format(reward, observation['depth'])) result = self.agent_service.step(reward, observation, identifier) self.result_logger.step() outbound_logger.info('result: {}'.format(result)) return str(result) @cherrypy.expose def reset(self, identifier): body = cherrypy.request.body.read() reward, success, failure, elapsed, finished = unpack_reset(body) inbound_logger.info('reward: {}, success: {}, failure: {}, elapsed: {}'.format( reward, success, failure, elapsed)) result = self.agent_service.reset(reward, identifier) self.result_logger.report(success, failure, finished) outbound_logger.info('result: {}'.format(result)) return str(result)
class Root(object): def __init__(self, sess, logdir, num_workers, visualize): self.latest_stage = -1 self.sess = sess with sess.as_default(): model = make_network() dnds = [] for i in range(3): dnds.append(DND()) global_agent = Agent(model, dnds, 3, name='global') self.agents = [] self.popped_agents = {} self.popped_locks = {} # CREATE NEW AGENT(S) for i in range(num_workers): # CREATE PLOTTER PER AGENT plotter = (AnimatedLineGraph(0, 0, max_val=50) if visualize else None) self.agents.append( Agent(model, dnds, 3, name='worker{}'.format(i), plotter=plotter)) summary_writer = tf.summary.FileWriter(logdir, sess.graph) for agent in self.agents: agent.set_summary_writer(summary_writer) initialize() # load feature extractor (alex net) if os.path.exists(TF_CNN_FEATURE_EXTRACTOR): config = tf.ConfigProto(gpu_options=tf.GPUOptions( visible_device_list='0', allow_growth=True)) gpu_config = config # TODO: remove this app_logger.info( "loading... {}".format(TF_CNN_FEATURE_EXTRACTOR)) self.feature_extractor = FeatureExtractor( sess_name='AlexNet', sess_config=gpu_config) app_logger.info("done") else: raise Exception self.agent_service = AgentService(BRICA_CONFIG_FILE, self.feature_extractor, sess) self.result_logger = ResultLogger() @cherrypy.expose() def flush(self, identifier): if identifier not in self.popped_agents: if len(self.agents) > 0: self.popped_locks[identifier] = Lock() agent = self.agents.pop(0) self.popped_agents[identifier] = agent self.agent_service.initialize(identifier, agent) else: return else: agent = self.popped_agents[identifier] self.popped_locks[identifier].acquire() with self.sess.as_default(): self.agent_service.initialize(identifier, agent) self.popped_locks[identifier].release() @cherrypy.expose def create(self, identifier): if identifier not in self.popped_agents: if __debug__: os.system('spd-say "Agent Created"') if len(self.agents) > 0: self.popped_locks[identifier] = Lock() agent = self.agents.pop(0) self.popped_agents[identifier] = agent else: return else: agent = self.popped_agents[identifier] self.popped_locks[identifier].acquire() with self.sess.as_default(): body = cherrypy.request.body.read() reward, observation, rotation, movement, scene_num = unpack(body) self.latest_stage = max(scene_num, self.latest_stage) inbound_logger.info('id: {}, reward: {}, depth: {}'.format( identifier, reward, observation['depth'])) feature = self.feature_extractor.feature(observation) self.result_logger.initialize() result = self.agent_service.create(reward, feature, identifier, agent) outbound_logger.info('id:{}, action: {}'.format( identifier, result)) self.popped_locks[identifier].release() return str(result) @cherrypy.expose def step(self, identifier): if identifier in self.popped_locks: self.popped_locks[identifier].acquire() with self.sess.as_default(): body = cherrypy.request.body.read() reward, observation, rotation, movement, scene_num = unpack(body) self.latest_stage = max(scene_num, self.latest_stage) inbound_logger.info('id: {}, reward: {}, depth: {}'.format( identifier, reward, observation['depth'])) result = self.agent_service.step(reward, rotation, movement, observation, identifier) self.result_logger.step() outbound_logger.info('id: {}, result: {}'.format( identifier, result)) if identifier in self.popped_locks: self.popped_locks[identifier].release() return str(result) + "/" + str(self.latest_stage) @cherrypy.expose def reset(self, identifier): if identifier in self.popped_locks: self.popped_locks[identifier].acquire() with self.sess.as_default(): body = cherrypy.request.body.read() reward, success, failure, elapsed, finished = unpack_reset(body) inbound_logger.info( 'reward: {}, success: {}, failure: {}, elapsed: {}'.format( reward, success, failure, elapsed)) result = self.agent_service.reset(reward, identifier) self.result_logger.report(success, failure, finished) outbound_logger.info('result: {}'.format(result)) if identifier in self.popped_locks: self.popped_locks[identifier].release() return str(result)