コード例 #1
0
    def __init__(self):
        # Create the window
        self.w, self.h = config.WINDOW_WIDTH, config.WINDOW_HEIGHT
        PygameMixin.__init__(self, size=(self.w, self.h), fill=((255,255,255)))

        self.IMAGE_CACHE = ImageCache()
        self._setup()
コード例 #2
0
 def __init__(self, scores, builder, page_entry):
     self.page_entry = page_entry
     self.page_num = 0
     self.show_page_number()
     self.page_count = len(scores)
     self.scores = scores
     self.builder = builder
     self.cache = ImageCache(num_workers=16, max_cache_size=40)
     self.update_page()
コード例 #3
0
class Game(PygameMixin):
    def __init__(self):
        # Create the window
        self.w, self.h = config.WINDOW_WIDTH, config.WINDOW_HEIGHT
        PygameMixin.__init__(self, size=(self.w, self.h), fill=((255,255,255)))

        self.IMAGE_CACHE = ImageCache()
        self._setup()
        
    def _setup(self):
        self._load_resources()
        
        level = Level(self.IMAGE_CACHE)
        level.load("test", "test.txt")
        self.background = level.draw()
        self.screen.blit(self.background, (0, 0))
        
        self.sprites = pygame.sprite.RenderUpdates()
        self.player = Player(image=self.IMAGE_CACHE['sprites'], pos=level.player_spawn(), frame=(0, 1))
        self.sprites.add(self.player)
        
        pygame.display.flip()
        
    def _load_resources(self):
        # load all the tilesets and cache them
        self.IMAGE_CACHE.add('sprites', os.path.join(config.TILESET_PATH, 'sprites.png'))

    def update(self, dt):
        self.update_control()
        self.player.update()
        self.sprites.update()

    def draw(self):
        self.sprites.clear(self.screen, self.background)
        dirty = self.sprites.draw(self.screen)
        pygame.display.update(dirty)
        
    def update_control(self):
        if self.key_pressed(pg.K_UP):
            self.walk(0)
        elif self.key_pressed(pg.K_DOWN):
            self.walk(2)
        elif self.key_pressed(pg.K_LEFT):
            self.walk(3)
        elif self.key_pressed(pg.K_RIGHT):
            self.walk(1)
            
    def walk(self, dir):
        x, y = self.player.pos
        self.player.dir = dir
        self.player.walk_animation()
        self.key_pressed(2)
コード例 #4
0
class TestImageCache(unittest.TestCase):
    def setUp(self):
        self.ic = ImageCache()

    def tearDown(self):
        pass

    def test_ic_construction(self):
        self.assertIsInstance(self.ic, ImageCache)

    def test_ic_get_table_name(self):
        self.assertIsInstance(self.ic.get_table(), str)
コード例 #5
0
    def test_cache_size(self):
        size_unit = 512
        file_size = 2 * size_unit
        limit = 7 * size_unit
        max_files = 1 + int(limit /file_size)

        cache = ImageCache(directory='test_cache', size_limit=limit)

        assert(len(cache.cache_files()) == 0), "Cache not empty on creation"

        for i in range(1, 2*max_files):

            file_name = self.add_to_cache(cache, file_size)

            files = cache.cache_files()

            assert(len(files) == min(i, max_files)), "Cache not removing files"

            assert(files[-1][0] == file_name), "Added file has wrong name"
            assert(files[-1][1] == file_size), "Added file has wrong size"
            assert(file_name not in [f[0] for f in files[:-1]]), "Added file name is not unique"
コード例 #6
0
ファイル: game.py プロジェクト: sir-pinecone/PyGameTemplate
    def __init__(self):
        background_fill = (0, 0, 0)
        self.w, self.h = config.WINDOW_WIDTH, config.WINDOW_HEIGHT
        size = (self.w, self.h)

        self.running = False
        self.clock = pygame.time.Clock() 
        self.screen_size = size
        self.slowmo = False

        # Create the window
        pygame.init()
        self.window = pygame.display.set_mode(size)
        self.window.fill(background_fill)
        pygame.display.flip()

        self.image_cache = ImageCache()
        self._setup()
コード例 #7
0
async def gen_database(path: str, fast: bool) -> None:
    """
    Takes in a target directory and computes information about
    the images contained therin
    """
    ic = ImageCache(fast=fast)
    await ic.gen_cache_from_directory(path)

    report = {}

    queries = {
        #"all_data": "SELECT * FROM {};",
        "image_types": "SELECT COUNT(DISTINCT img_type) FROM {};",
        "total_images": "SELECT COUNT(*) FROM {};",
        "average_size": "SELECT AVG(size) FROM {};",
        "total_size": "SELECT SUM(size) FROM {};",
    }

    for k, v in queries.items():
        rows = ic.query(v.format(ic.get_table()))
        report[k] = rows[0][0]

    # Get duplicate and ambiguous images
    report['duplicates'] = ic.get_duplicates()
    report['ambiguous'] = ic.get_ambiguous()
    report["process_time"] = ic.processing_time

    pp = pprint.PrettyPrinter(indent=2, compact=False)
    pp.pprint(report)

    logger.info("Completed database generation.")
    logger.info(
        f"Processed {ic.get_count()} images in {ic.processing_time} seconds.")
    logger.info(f"Encountered {len(report['duplicates'])} duplicate images.")

    tstamp = datetime.datetime.now().strftime("gen_database_%Y-%m-%d.json")
    with open(tstamp, 'w') as fout:
        fout.write(json.dumps(report))
    logger.info(f"Report written to {tstamp}")
コード例 #8
0
def main(arguments):
    """Main loop."""
    EPOCHS = arguments.epochs
    GPU = arguments.gpu
    GPU_NUMBER = arguments.gpu_number
    # TO_SAMPLE = arguments.sample

    DATA_PATH = 'data/horse2zebra/'

    tf.reset_default_graph() 

    if GPU == 1:
        os.environ["CUDA_VISIBLE_DEVICES"]="{}".format(GPU_NUMBER)
        os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"

        config = tf.ConfigProto(log_device_placement=True)
        config.gpu_options.per_process_gpu_memory_fraction = 0.5  # pylint: disable=no-member
        config.gpu_options.allow_growth = True  # pylint: disable=no-member
        sess = tf.Session(config=config)
    else:
        sess = tf.Session()
    
    it_a, train_A = Images(DATA_PATH + '_trainA.tfrecords', name='trainA').feed()
    it_b, train_B = Images(DATA_PATH + '_trainB.tfrecords', name='trainB').feed()
    # it_at, test_A = Images(DATA_PATH + '_testA.tfrecords', name='test_a').feed()
    # it_bt, test_B = Images(DATA_PATH + '_testB.tfrecords', name='test_b').feed()
    
    gen_a_sample = tf.placeholder(tf.float32, [None, WIDTH, HEIGHT, CHANNEL], name="fake_a_sample")
    gen_b_sample = tf.placeholder(tf.float32, [None, WIDTH, HEIGHT, CHANNEL], name="fake_b_sample")
    learning_rate = tf.placeholder(tf.float32, shape=[], name="lr")

    d_a_train_op, d_b_train_op, g_a_train_op, g_b_train_op, g1, g2 = \
    build_model(train_A, train_B, gen_a_sample, gen_b_sample, learning_rate)

    # testG1 = generator(test_A, name='g_a2b')
    # testG2 = generator(test_B,  name='g_b2a')
    # testCycleA = generator(testG1,  name='d_a')
    # testCycleB = generator(testG2, name='d_b')

    merged = tf.summary.merge_all()
    
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

    with sess:
        sess.run(init)
        writer = tf.summary.FileWriter(LOG_DIR, tf.get_default_graph())
        
        cache_a = ImageCache(50)
        cache_b = ImageCache(50)

        print('Beginning training...')
        start = time.perf_counter()
        for epoch in range(EPOCHS):
            sess.run(it_a)
            sess.run(it_b)
            if epoch < 100:
                lr = 2e-4
            else:
                lr = 2e-4 - (2e-4 * (epoch - 100) / 100)
            try:
                for step in tqdm(range(533)):  # TODO change number of steps
                    gen_a, gen_b, = sess.run([g1, g2])

                    _, _, _, _, summaries = sess.run([d_b_train_op, d_a_train_op, 
                                                      g_a_train_op, g_b_train_op, merged],
                                                     feed_dict={gen_b_sample: cache_b.fetch(gen_b),
                                                                gen_a_sample: cache_a.fetch(gen_a),
                                                                learning_rate: lr})
                    if step % 100 == 0:
                        writer.add_summary(summaries, epoch * 533 + step)

            except tf.errors.OutOfRangeError as e:
                print(e)
                print("Out of range: {}".format(step))
                pass  
           
            print("Epoch {}/{} done.".format(epoch+1, EPOCHS))

            counter = epoch + 1
                
            if np.mod(counter, SAVE_STEP) == 0:
                save_path = save_model(saver, sess, counter)
                print('Running for {:.2f} seconds, saving to {}'.format(time.perf_counter() - start, save_path))
コード例 #9
0
 def setUp(self):
     self.ic = ImageCache()
コード例 #10
0
# threads = 3
# results_dir = 'imitation_data_vision_temp_to_delete'
# scenario = 'vision_harder_small'

number_of_samples_per_workspace = 50
samples_per_file = 5
threads = 10
results_dir = "imitation_data_vision_harder"

if not os.path.exists(results_dir):
    os.makedirs(results_dir)

params_dir = os.path.abspath(
    os.path.expanduser("~/ModelBasedDDPG/scenario_params/{}/".format(scenario))
)
image_cache = ImageCache(params_dir, create_images=False)
collection_queries = []
workspace_ids = []
for cache_item in image_cache.items.values():
    collection_queries.extend(
        [(cache_item.workspace_id, cache_item.full_filename)]
        * number_of_samples_per_workspace
    )
    workspace_ids.append(cache_item.workspace_id)

data_collector = VisionImitationDataCollector(
    config, threads, query_parameters=collection_queries
)
collected = 0

aa = datetime.datetime.now()
コード例 #11
0
 def __init__(self):
     self._cache = ImageCache()
     self._actions = ActionCollection()
     self._imposclib = ImposcIF()
     self._actions.add_functions(self._imposclib)
コード例 #12
0
initial_learn_rate = config['reward']['initial_learn_rate']
decrease_learn_rate_after = config['reward']['decrease_learn_rate_after']
learn_rate_decrease_rate = config['reward']['learn_rate_decrease_rate']
oversample_goal = config['reward']['oversample_goal']
oversample_collision = config['reward']['oversample_collision']

scenario = config['general']['scenario']
# base_data_dir = os.path.join('supervised_data', scenario)
base_data_dir = os.path.join('supervised_data', scenario + '_by_status')
image_cache = None
if 'vision' in scenario:
    params_dir = os.path.abspath(
        os.path.expanduser(
            '~/ModelBasedDDPG/scenario_params/{}/'.format(scenario)))
    image_cache = ImageCache(params_dir)
train_data_dir = os.path.join(base_data_dir, 'train')
test_data_dir = os.path.join(base_data_dir, 'test')

number_of_unzippers = config['general']['number_of_unzippers']

train = Oversampler(train_data_dir,
                    batch_size,
                    oversample_goal,
                    oversample_collision,
                    number_of_unzippers=number_of_unzippers)
test = Oversampler(test_data_dir,
                   batch_size,
                   oversample_goal,
                   oversample_collision,
                   number_of_unzippers=number_of_unzippers)
コード例 #13
0
 def add_to_cache(self, cache: ImageCache, file_size: int) -> str:
     file_name = cache.offer_new_file(extension='dat')
     self.fs.create_file(file_name, st_size=file_size)
     return file_name
コード例 #14
0
class Handler:
    def __init__(self, scores, builder, page_entry):
        self.page_entry = page_entry
        self.page_num = 0
        self.show_page_number()
        self.page_count = len(scores)
        self.scores = scores
        self.builder = builder
        self.cache = ImageCache(num_workers=16, max_cache_size=40)
        self.update_page()

    def cache_nearby(self, pagenum):
        filenames = []

        for i in range(pagenum, pagenum + 10):
            if i > 0 and i < len(self.scores):
                leftfile = self.scores[i][1]
                rightfile = self.scores[i][2]
                filenames.append(leftfile)
                filenames.append(rightfile)

        for i in range(pagenum - 5, pagenum):
            if i > 0 and i < len(self.scores):
                leftfile = self.scores[i][1]
                rightfile = self.scores[i][2]
                filenames.append(leftfile)
                filenames.append(rightfile)

        self.cache.preload(filenames)

    def get_image(self, pagenum):
        left_file = self.scores[self.page_num][1]
        right_file = self.scores[self.page_num][2]

        left_image = self.cache.fetch(left_file)
        right_image = self.cache.fetch(right_file)
        left_image['pixbuf'] = pil_to_pixbuf(left_image['canvas'])
        right_image['pixbuf'] = pil_to_pixbuf(right_image['canvas'])
        return left_image, right_image

    def update_page(self):
        if (len(self.scores) == 0):
            description = Gtk.TextBuffer()
            description.set_text("No similar pairs of images found")
            textview3 = self.builder.get_object("textview3")
            textview3.set_buffer(description)
            return

        score = self.scores[self.page_num][0]

        description = Gtk.TextBuffer()
        description.set_text("Distance between images: %.2f" % (score))
        textview3 = self.builder.get_object("textview3")
        textview3.set_property("justification", Gtk.Justification.CENTER)
        textview3.set_buffer(description)

        left_file = self.scores[self.page_num][1]
        right_file = self.scores[self.page_num][2]

        image2 = self.builder.get_object("image2")
        image1 = self.builder.get_object("image1")
        try:
            left_image, right_image = self.get_image(self.page_num)
            image2.set_from_pixbuf(right_image['pixbuf'])
            image1.set_from_pixbuf(left_image['pixbuf'])
        except FileNotFoundError as e:
            print(e)
            return

        self.cache_nearby(self.page_num)

        # update right
        right_description = "%s\nWidth: %d\nHeight: %d\nFilesize: %d\n" % (
            right_file, right_image["width"], right_image["height"],
            right_image["filesize"])
        if (right_image["filesize"] > left_image["filesize"]):
            right_description = right_description + "(Larger)\n"

        right_buffer = Gtk.TextBuffer()
        right_buffer.set_text(right_description)
        textview2 = self.builder.get_object("textview2")
        textview2.set_buffer(right_buffer)

        # update left
        left_description = "%s\nWidth: %d\nHeight: %d\nFilesize: %d\n" % (
            left_file, left_image["width"], left_image["height"],
            left_image["filesize"])
        if (left_image["filesize"] > right_image["filesize"]):
            left_description = left_description + "(Larger)\n"

        left_buffer = Gtk.TextBuffer()
        left_buffer.set_text(left_description)
        textview1 = self.builder.get_object("textview1")
        textview1.set_buffer(left_buffer)

    def show_page_number(self):
        new_page_str = str(self.page_num + 1)
        self.page_entry.get_buffer().set_text(new_page_str, len(new_page_str))

    def set_page_number(self, num):
        self.page_num = num
        if (self.page_num >= self.page_count):
            self.page_num = 0
        elif (self.page_num < 0):
            self.page_num = self.page_count - 1

        self.update_page()

    def cancel_deletion(self):
        window = self.builder.get_object("window1")
        dialog = DialogExample(window)
        response = dialog.run()

        result = None
        if response == Gtk.ResponseType.OK:
            result = False
        elif response == Gtk.ResponseType.CANCEL:
            result = True

        dialog.destroy()

        return result

    def onDeleteRight(self, *args):
        if (len(self.scores) == 0):
            return
        #TODO: Disable navigation until this finishes to prevent races
        if self.cancel_deletion():
            return

        right_file = self.scores[self.page_num][2]
        temp = []
        for res in self.scores:
            if res[1] != right_file and res[2] != right_file:
                temp.append(res)

        self.scores = temp
        self.page_count = len(self.scores)

        self.set_page_number(self.page_num)
        self.show_page_number()
        try:
            os.remove(right_file)
        except FileNotFoundError as e:
            print(e)
            pass

    def onDeleteLeft(self, *args):
        if (len(self.scores) == 0):
            return
        #TODO: Disable navigation until this finishes to prevent races
        if self.cancel_deletion():
            return

        left_file = self.scores[self.page_num][1]
        temp = []
        for res in self.scores:
            if res[1] != left_file and res[2] != left_file:
                temp.append(res)

        self.scores = temp
        self.page_count = len(self.scores)

        self.set_page_number(self.page_num)
        self.show_page_number()
        try:
            os.remove(left_file)
        except FileNotFoundError as e:
            print(e)
            pass

    def onDeleteWindow(self, *args):
        self.cache.quit()
        Gtk.main_quit(*args)

    def onLeftClicked(self, *args):
        self.set_page_number(self.page_num - 1)
        self.show_page_number()

    def onRightClicked(self, *args):
        self.set_page_number(self.page_num + 1)
        self.show_page_number()

    def onIgnoreClicked(self, *args):
        pass

    def onDeleteBothClicked(self, *args):
        pass

    def page_num_edited(self, *args):
        page_num_str = self.page_entry.get_text()
        try:
            self.page_num = int(page_num_str) - 1

            if (self.page_num >= self.page_count):
                self.page_num = self.page_count - 1
                self.page_entry.set_text(str(self.page_count))
            elif (self.page_num < 0):
                self.page_num = 0
                self.page_entry.set_text(str(1))
            else:
                self.update_page()
        except:
            pass
コード例 #15
0
def run_for_config(config, print_messages):
    # set the name of the model
    model_name = config['general']['name']
    now = datetime.datetime.fromtimestamp(
        time.time()).strftime('%Y_%m_%d_%H_%M_%S')
    model_name = now + '_' + model_name if model_name is not None else now

    # openrave_interface = OpenraveRLInterface(config, None)
    random_seed = config['general']['random_seed']
    np.random.seed(random_seed)
    random.seed(random_seed)
    tf.set_random_seed(random_seed)

    # where we save all the outputs (outputs will be saved according to the scenario)
    scenario = config['general']['scenario']
    working_dir = os.path.join(get_base_directory(), scenario)
    if not os.path.exists(working_dir):
        os.makedirs(working_dir)
    saver_dir = os.path.join(working_dir, 'models', model_name)
    if not os.path.exists(saver_dir):
        os.makedirs(saver_dir)
    best_model_path = None
    config_copy_path = os.path.join(working_dir, 'models', model_name,
                                    'config.yml')
    summaries_dir = os.path.join(working_dir, 'tensorboard', model_name)
    completed_trajectories_dir = os.path.join(working_dir, 'trajectories',
                                              model_name)

    # load images if required
    image_cache = None
    if _is_vision(scenario):
        image_cache = ImageCache(config['general']['params_file'],
                                 create_images=True)

    # load pretrained model if required
    pre_trained_reward = None
    if config['model']['use_reward_model']:
        reward_model_name = config['model']['reward_model_name']
        pre_trained_reward = PreTrainedReward(reward_model_name, config)

    # generate graph:
    network = Network(config,
                      is_rollout_agent=False,
                      pre_trained_reward=pre_trained_reward)

    def unpack_state_batch(state_batch):
        joints = [state[0] for state in state_batch]
        poses = {
            p.tuple: [state[1][p.tuple] for state in state_batch]
            for p in network.potential_points
        }
        jacobians = None
        return joints, poses, jacobians

    def score_for_hindsight(augmented_buffer):
        assert _is_vision(scenario)
        # unzip
        goal_pose_list, goal_joints_list, workspace_image_list, current_state_list, action_used_list, _, is_goal_list,\
        __ = zip(*augmented_buffer)
        # make one hot status vector:
        is_goal_one_hot_list = np.zeros((len(is_goal_list), 3),
                                        dtype=np.float32)
        for i in range(len(is_goal_list)):
            if is_goal_list[i]:
                is_goal_one_hot_list[i, 2] = 1.0  # mark as goal transition
            else:
                is_goal_one_hot_list[i, 0] = 1.0  # mark as free transition
        # unpack current and next state
        current_joints, _, __ = unpack_state_batch(current_state_list)

        fake_rewards, _ = pre_trained_reward.make_prediction(
            sess,
            current_joints,
            goal_joints_list,
            action_used_list,
            goal_pose_list,
            all_transition_labels=is_goal_one_hot_list)
        return list(fake_rewards)

    # initialize replay memory
    replay_buffer = ReplayBuffer(config)
    hindsight_policy = HindsightPolicy(config, replay_buffer,
                                       score_for_hindsight)

    # save model
    latest_saver = tf.train.Saver(max_to_keep=2, save_relative_paths=saver_dir)
    best_saver = tf.train.Saver(max_to_keep=2, save_relative_paths=saver_dir)
    yaml.dump(config, open(config_copy_path, 'w'))
    summaries_collector = SummariesCollector(summaries_dir, model_name)
    rollout_manager = FixedRolloutManager(config, image_cache=image_cache)
    trajectory_eval = TrajectoryEval(config, rollout_manager,
                                     completed_trajectories_dir)

    test_results = []

    def update_model(sess, global_step):
        batch_size = config['model']['batch_size']
        gamma = config['model']['gamma']
        replay_buffer_batch = replay_buffer.sample_batch(batch_size)

        goal_pose, goal_joints, workspace_id, current_state, action, reward, terminated, next_state = \
            replay_buffer_batch

        # get image from image cache
        workspace_image = None
        if image_cache is not None:
            workspace_image = [image_cache.get_image(k) for k in workspace_id]

        current_joints, _, __ = unpack_state_batch(current_state)
        next_joints, _, __ = unpack_state_batch(next_state)

        # get the predicted q value of the next state (action is taken from the target policy)
        next_state_action_target_q = network.predict_policy_q(
            next_joints,
            workspace_image,
            goal_pose,
            goal_joints,
            sess,
            use_online_network=False)

        # compute critic label
        q_label = np.expand_dims(
            np.squeeze(np.array(reward)) +
            np.multiply(np.multiply(1 - np.array(terminated), gamma),
                        np.squeeze(next_state_action_target_q)), 1)
        max_label = np.max(q_label)
        min_label = np.min(q_label)
        limit = 1.0 / (1.0 - gamma)
        if max_label > limit:
            print 'out of range max label: {} limit: {}'.format(
                max_label, limit)
        if min_label < -limit:
            print 'out of range min label: {} limit: {}'.format(
                min_label, limit)

        # # step to use for debug:
        # network.debug_all(current_joints, workspace_image, goal_pose, goal_joints, action, q_label, sess)

        # train critic given the targets
        critic_optimization_summaries, _ = network.train_critic(
            current_joints, workspace_image, goal_pose, goal_joints, action,
            q_label, sess)

        # train actor
        actor_optimization_summaries, _ = network.train_actor(
            current_joints, workspace_image, goal_pose, goal_joints, sess)

        # update target networks
        network.update_target_networks(sess)

        result = [
            critic_optimization_summaries,
            actor_optimization_summaries,
        ]
        return result

    def print_state(prefix, episodes, successful_episodes, collision_episodes,
                    max_len_episodes):
        if not print_messages:
            return
        print '{}: {}: finished: {}, successful: {} ({}), collision: {} ({}), max length: {} ({})'.format(
            datetime.datetime.fromtimestamp(
                time.time()).strftime('%Y-%m-%d %H:%M:%S'), prefix, episodes,
            successful_episodes,
            float(successful_episodes) / episodes, collision_episodes,
            float(collision_episodes) / episodes, max_len_episodes,
            float(max_len_episodes) / episodes)

    def process_example_trajectory(episode_example_trajectory,
                                   episode_agent_trajectory):
        # creates an episode by computing the actions, and setting the rewards to None (will be calculated later)
        _, __, ___, ____, goal_pose, goal_joints, workspace_id = episode_agent_trajectory
        example_trajectory, example_trajectory_poses = episode_example_trajectory
        example_trajectory = [j[1:] for j in example_trajectory]
        # goal reached always
        status = 3
        # get the states (joints, poses, jacobians), for now, ignore the jacobians.
        states = [(example_trajectory[i], example_trajectory_poses[i], None)
                  for i in range(len(example_trajectory))]
        # compute the actions by normalized difference between steps
        actions = [
            np.array(example_trajectory[i + 1]) -
            np.array(example_trajectory[i])
            for i in range(len(example_trajectory) - 1)
        ]
        actions = [a / max(np.linalg.norm(a), 0.00001) for a in actions]

        rewards = [-config['openrave_rl']['keep_alive_penalty']
                   ] * (len(actions) - 1) + [1.0]
        return status, states, actions, rewards, goal_pose, goal_joints, workspace_id

    def do_test(sess, best_model_global_step, best_model_test_success_rate):
        rollout_manager.set_policy_weights(network.get_actor_weights(
            sess, is_online=False),
                                           is_online=False)
        eval_result = trajectory_eval.eval(
            global_step, config['test']['number_of_episodes'])
        test_episodes = eval_result[0]
        test_successful_episodes = eval_result[1]
        test_collision_episodes = eval_result[2]
        test_max_len_episodes = eval_result[3]
        test_mean_reward = eval_result[4]
        if print_messages:
            print_state('test', test_episodes, test_successful_episodes,
                        test_collision_episodes, test_max_len_episodes)
            print('test mean total reward {}'.format(test_mean_reward))
        summaries_collector.write_test_episode_summaries(
            sess, global_step, test_episodes, test_successful_episodes,
            test_collision_episodes, test_max_len_episodes)
        test_results.append(
            (global_step, episodes, test_successful_episodes,
             test_collision_episodes, test_max_len_episodes, test_mean_reward))
        # see if best
        rate = test_successful_episodes / float(test_episodes)
        if best_model_test_success_rate < rate:
            if print_messages:
                print 'new best model found at step {}'.format(global_step)
                print 'old success rate {} new success rate {}'.format(
                    best_model_test_success_rate, rate)
            is_best = True
            best_model_global_step = global_step
            best_model_test_success_rate = rate
        else:
            is_best = False
            if print_messages:
                print 'best model still at step {}'.format(
                    best_model_global_step)
        return is_best, best_model_global_step, best_model_test_success_rate

    def do_end_of_run_validation(sess):
        # restores the model first
        best_saver.restore(sess, best_model_path)
        # set the weights
        rollout_manager.set_policy_weights(network.get_actor_weights(
            sess, is_online=False),
                                           is_online=False)
        eval_result = trajectory_eval.eval(
            -1, config['validation']['number_of_episodes'])
        test_episodes = eval_result[0]
        test_successful_episodes = eval_result[1]
        test_collision_episodes = eval_result[2]
        test_max_len_episodes = eval_result[3]
        test_mean_reward = eval_result[4]
        if print_messages:
            print_state('validation (best model)', test_episodes,
                        test_successful_episodes, test_collision_episodes,
                        test_max_len_episodes)
            print('validation (best model) mean total reward {}'.format(
                test_mean_reward))
        test_results.append(
            (-1, episodes, test_successful_episodes, test_collision_episodes,
             test_max_len_episodes, test_mean_reward))
        # see if best
        rate = test_successful_episodes / float(test_episodes)
        print 'final success rate is {}'.format(rate)
        return rate

    allowed_batch_episode_editor = config['model']['batch_size'] if _is_vision(
        scenario) else None
    regular_episode_editor = EpisodeEditor(
        config['model']['alter_episode'],
        pre_trained_reward,
        image_cache=image_cache,
        allowed_batch=allowed_batch_episode_editor)
    motion_planner_episode_editor = EpisodeEditor(
        config['model']['alter_episode_expert'],
        pre_trained_reward,
        image_cache=image_cache,
        allowed_batch=allowed_batch_episode_editor)

    with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(
            per_process_gpu_memory_fraction=config['general']
        ['gpu_usage']))) as sess:
        sess.run(tf.global_variables_initializer())
        if pre_trained_reward is not None:
            pre_trained_reward.load_weights(sess)
        network.update_target_networks(sess)

        global_step = 0
        episodes = successful_episodes = collision_episodes = max_len_episodes = 0
        best_model_global_step, best_model_test_success_rate = -1, -1.0
        for update_index in range(config['general']['updates_cycle_count']):
            # collect data
            a = datetime.datetime.now()
            rollout_manager.set_policy_weights(network.get_actor_weights(
                sess, is_online=True),
                                               is_online=True)
            episodes_per_update = config['general']['episodes_per_update']
            episode_results = rollout_manager.generate_episodes(
                episodes_per_update, True)
            episodes_agent_trajectory, episodes_times, episodes_example_trajectory = zip(
                *episode_results)

            # alter the episodes based on reward model
            altered_episodes = regular_episode_editor.process_episodes(
                episodes_agent_trajectory, sess)

            # process example episodes for failed interactions
            altered_motion_planner_episodes = []
            failed_motion_planner_trajectories = config['model'][
                'failed_motion_planner_trajectories']
            if failed_motion_planner_trajectories > 0:
                # take a small number of failed motion plans
                failed_episodes_indices = [
                    i for i in range(len(altered_episodes))
                    if altered_episodes[i][0] != 3
                ]
                failed_episodes_indices = failed_episodes_indices[:
                                                                  failed_motion_planner_trajectories]
                motion_planner_episodes = [
                    process_example_trajectory(episodes_example_trajectory[i],
                                               altered_episodes[i])
                    for i in failed_episodes_indices
                ]
                altered_motion_planner_episodes = motion_planner_episode_editor.process_episodes(
                    motion_planner_episodes, sess)

            # add to replay buffer
            hindsight_policy.append_to_replay_buffer(
                list(altered_episodes) + list(altered_motion_planner_episodes))

            # compute times
            total_find_trajectory_time = None
            total_rollout_time = None
            for episode_times in episodes_times:
                # update the times
                find_trajectory_time, rollout_time = episode_times
                if total_find_trajectory_time is None:
                    total_find_trajectory_time = find_trajectory_time
                else:
                    total_find_trajectory_time += find_trajectory_time
                if total_rollout_time is None:
                    total_rollout_time = rollout_time
                else:
                    total_rollout_time += rollout_time

            # compute counters
            for altered_episode in altered_episodes:
                status = altered_episode[0]
                episodes += 1
                if status == 1:
                    max_len_episodes += 1
                elif status == 2:
                    collision_episodes += 1
                elif status == 3:
                    successful_episodes += 1

            b = datetime.datetime.now()
            print 'data collection took: {}'.format(b - a)
            print 'find trajectory took: {}'.format(total_find_trajectory_time)
            print 'rollout time took: {}'.format(total_rollout_time)
            print_state('train', episodes, successful_episodes,
                        collision_episodes, max_len_episodes)

            # do updates
            if replay_buffer.size() > config['model']['batch_size']:
                a = datetime.datetime.now()
                for _ in range(config['general']['model_updates_per_cycle']):
                    summaries = update_model(sess, global_step)
                    if global_step % config['general'][
                            'write_train_summaries'] == 0:
                        summaries_collector.write_train_episode_summaries(
                            sess, global_step, episodes, successful_episodes,
                            collision_episodes, max_len_episodes)
                        summaries_collector.write_train_optimization_summaries(
                            summaries, global_step)
                    global_step += 1
                b = datetime.datetime.now()
                print 'update took: {}'.format(b - a)

            # test if needed
            if update_index % config['test']['test_every_cycles'] == 0:
                is_best, best_model_global_step, best_model_test_success_rate = do_test(
                    sess, best_model_global_step, best_model_test_success_rate)
                if is_best:
                    best_model_path = best_saver.save(sess,
                                                      os.path.join(
                                                          saver_dir, 'best'),
                                                      global_step=global_step)
            if update_index % config['general']['save_model_every_cycles'] == 0:
                latest_saver.save(sess,
                                  os.path.join(saver_dir, 'last_iteration'),
                                  global_step=global_step)
            # see if max score reached (even if validation is not 100%, there will no longer be any model updates...)
            if best_model_test_success_rate > 0.99999:
                print 'stoping run: best test success rate reached {}'.format(
                    best_model_test_success_rate)
                break

        # final test at the end
        is_best, best_model_global_step, best_model_test_success_rate = do_test(
            sess, best_model_global_step, best_model_test_success_rate)
        if is_best:
            best_model_path = best_saver.save(sess,
                                              os.path.join(saver_dir, 'best'),
                                              global_step=global_step)

        # get a validation rate for the best recorded model
        validation_rate = do_end_of_run_validation(sess)

    last_message = 'best model stats at step {} has success rate of {} and validation success rate of {}'.format(
        best_model_global_step, best_model_test_success_rate, validation_rate)
    print last_message

    with open(os.path.join(completed_trajectories_dir, 'final_status.txt'),
              'w') as f:
        f.write(last_message)
        f.flush()

    test_results_file = os.path.join(completed_trajectories_dir,
                                     'test_results.test_results_pkl')
    with bz2.BZ2File(test_results_file, 'w') as compressed_file:
        pickle.dump(test_results, compressed_file)

    rollout_manager.end()
    return test_results
コード例 #16
0
def get_queries():
    with bz2.BZ2File(file_to_use, "r") as compressed_file:
        loaded_data = pickle.load(compressed_file)
    result = []
    for traj, pose_traj, workspace_id in loaded_data:
        start_joints = traj[0]
        goal_joints = traj[-1]
        t = [start_joints, goal_joints, workspace_id, traj]
        result.append(t)
    return result


queries = get_queries()
queries = queries[:query_limit]
image_cache = ImageCache(config["general"]["params_file"], create_images=True)


def run_network_single(sess, openrave_rl_interface, network, trajectory,
                       workspace_image):
    # start the new query
    (
        current_joints,
        goal_joints,
        steps_required_for_motion_plan,
    ) = openrave_rl_interface.start_specific(trajectory)
    current_joints = current_joints[1:]
    # compute the maximal number of steps to execute
    max_steps = int(steps_required_for_motion_plan *
                    config["general"]["max_path_slack"])
    goal_pose = openrave_rl_interface.openrave_manager.get_target_pose(
コード例 #17
0
def get_queries():
    with bz2.BZ2File(file_to_use, 'r') as compressed_file:
        loaded_data = pickle.load(compressed_file)
    result = []
    for traj, pose_traj, workspace_id in loaded_data:
        start_joints = traj[0]
        goal_joints = traj[-1]
        t = [start_joints, goal_joints, workspace_id, traj]
        result.append(t)
    return result


queries = get_queries()
queries = queries[:query_limit]
image_cache = ImageCache(config['general']['params_file'], create_images=True)


def run_network_single(sess, openrave_rl_interface, network, trajectory,
                       workspace_image):
    # start the new query
    current_joints, goal_joints, steps_required_for_motion_plan = openrave_rl_interface.start_specific(
        trajectory)
    current_joints = current_joints[1:]
    # compute the maximal number of steps to execute
    max_steps = int(steps_required_for_motion_plan *
                    config['general']['max_path_slack'])
    goal_pose = openrave_rl_interface.openrave_manager.get_target_pose(
        goal_joints)
    goal_joints = goal_joints[1:]
    # set the start state
コード例 #18
0
class ImposcActions:
    def __init__(self):
        self._cache = ImageCache()
        self._actions = ActionCollection()
        self._imposclib = ImposcIF()
        self._actions.add_functions(self._imposclib)

    def info(self) -> dict:
        return self._actions.info()

    def validate(
        self, args: Dict[str, str]
    ) -> Tuple[Dict[str, Optional[Union[int, float, str]]], List[str]]:
        validated = dict(
            map(
                lambda pair:
                (pair[0], self._actions.validate(pair[0], pair[1])),
                args.items()))

        outcome = list(
            map(
                lambda elem:
                f"Parameter {elem[0]} was supplied with an invalid value {args[elem[0]]}",
                filter(lambda pair: pair is None, validated.items())))

        return validated, outcome

    def impacts(self, **kwargs) -> Path:
        errors = LibErrors()
        try:
            outfile = str(self._cache.offer_new_file())
            if self._imposclib.impacts(
                    outfile=outfile.encode('utf-8'),
                    errorfile=errors.errorFile.encode('utf-8'),
                    **kwargs):
                return outfile
            else:
                return errors.errorPath
        except TypeError as e:
            errors.put(f"{e}")
            return errors.errorPath

    def singularity_set(self, **kwargs) -> Path:
        errors = LibErrors()
        try:
            outfile = str(self._cache.offer_new_file())
            if self._imposclib.singularity_set(
                    outfile=outfile.encode('utf-8'),
                    errorfile=errors.errorFile.encode('utf-8'),
                    **kwargs):
                return outfile
            else:
                return errors.errorPath
        except TypeError as e:
            errors.put(f"{e}")
            return errors.errorPath

    def doa(self, **kwargs) -> Path:
        errors = LibErrors()
        try:
            outfile = str(self._cache.offer_new_file())
            if self._imposclib.doa(outfile=outfile.encode('utf-8'),
                                   errorfile=errors.errorFile.encode('utf-8'),
                                   **kwargs):
                return outfile
            else:
                return errors.errorPath
        except TypeError as e:
            errors.put(f"{e}")
            return errors.errorPath
コード例 #19
0
    def __init__(self):
        super(PyRssReaderWindow, self).__init__()
        uic.loadUi('PyRssReaderWindow.ui', self)

        logging.basicConfig(filename=kLogFile,
                            level=logging.INFO,
                            format='%(asctime)s %(message)s',
                            datefmt='%m/%d/%Y %I:%M:%S %p')

        self.db = Database()
        self.proxy = Proxy()
        self.preferences = Preferences()

        self.languageFilter = LanguageFilter(self.db)
        self.adFilter = AdFilter(self.db)
        self.prefetchStatusbarWidget = PrefetchStatusbarWidget(self)
        self.imageCache = ImageCache(kMaxCacheSize)
        self.imagePrefetcher = ImagePrefetcher(self.db, self.imageCache,
                                               self.proxy)

        self.imagePrefetcher.imagePrefetchStartingSignal.connect(
            self.prefetchStatusbarWidget.prefetchOn)
        self.imagePrefetcher.imagePrefetchDoneSignal.connect(
            self.prefetchStatusbarWidget.prefetchOff)
        self.statusBar.addPermanentWidget(self.prefetchStatusbarWidget)

        self.feedItemFilterMatcher = FeedItemFilterMatcher(self.db)
        self.feedPurger = FeedPurger(self.db, self)
        self.feedPurger.feedPurgedSignal.connect(self.onFeedPurged)
        self.feedPurger.messageSignal.connect(self.showStatusBarMessage)

        self.m_currentFeedId = -1
        self.feedIdsToUpdate = []

        # This is a persistent object, so it won't go out of scope while fetching feeds
        self.feedUpdater = FeedUpdater(self.db)
        self.feedUpdater.feedItemUpdateSignal.connect(self.onFeedItemUpdate)
        self.feedUpdater.feedUpdateMessageSignal.connect(
            self.showStatusBarMessage)

        self.keyboardHandler = KeyboardHandler(self)

        self.keyboardHandler.minimizeApplicationSignal.connect(
            self.onMinimizeApp)

        self.feedTreeObj = FeedTree(self.feedTree, self.db,
                                    self.keyboardHandler)
        self.feedTreeObj.feedSelectedSignal.connect(self.onFeedSelected)
        self.feedTreeObj.feedUpdateRequestedSignal.connect(
            self.onFeedUpdateRequested)
        self.feedTreeObj.feedReadStateSignal.connect(self.onSetFeedReadState)
        self.feedTreeObj.feedPurgeSignal.connect(self.onPurgeSingleFeed)
        self.feedTreeObj.feedDeleteSignal.connect(self.onDeleteFeed)

        self.titleTreeObj = TitleTree(self.db, self.titleTree,
                                      self.languageFilter,
                                      self.keyboardHandler,
                                      self.imagePrefetcher)
        self.titleTreeObj.feedItemSelectedSignal.connect(
            self.onFeedItemSelected)
        self.titleTreeObj.downloadEnclosureSignal.connect(
            self.onDownloadEnclosure)

        self.rssContentViewObj = RssContentView(self, self.languageFilter,
                                                self.adFilter, self.imageCache,
                                                self.keyboardHandler,
                                                self.proxy)
        self.addRssContentViewToLayout()
        self.rssContentViewObj.reselectFeedItemSignal.connect(
            self.onReselectFeedItem)
        self.rssContentViewObj.urlHovered.connect(self.showStatusBarMessage)

        self.feedUpdateTimer = QtCore.QTimer()
        self.feedUpdateTimer.timeout.connect(self.onFeedUpdateTimerTimeout)
        self.feedUpdateTimer.setInterval(60000)  # One-minute interval
        self.minutesSinceLastFeedUpdate = 0  # Minutes since last update of feeds

        self.enclosureDownloader = None

        self.pocketSupport = PocketSupport(self.db, self.proxy)

        QtCore.QTimer.singleShot(0, self.initialize)
コード例 #20
0
ファイル: image_utils.py プロジェクト: muffins/image_utils
async def find_dupes(
    source: str, target: str, skip: bool, fast: bool
) -> Dict[str, any]:
    """
    Use the Image Cache helper class to read in the source directory
    to an sqlite3 DB, compute hashes and any necessary pieces for checking
    if the two images are the same. Then given the target directory, check
    to see if the image already exists, if it does to a pprint report
    about all potential dupes
    """
    ic = ImageCache(fast=fast)
    if not skip:
        await ic.gen_cache_from_directory(source)
        logger.info(f"Processing took {ic.processing_time} seconds.")

    logger.info(
        f"Beginning processing of {target} for potential duplicates. "
        + "Report will be displayed with duplicates, ambiguous files, "
        + "and suggested files for copying when finished. This may take a "
        + "long time."
    )

    report = {
        "duplicates": [],
        "ambiguous": [],
        "migrate": [],
    }

    for root, _, filenames in os.walk(target):
        logger.info(f"Processing {len(filenames)} files in {root}")
        for f in filenames:
            full: str = os.path.join(root, f)
            image: ImageHelper = ImageHelper(full)
            image.check_image_type()
            if not image.is_image:
                continue

            image.read_image()
            image.compute_md5()

            # Check if the file/size exists in the db.
            row = ic.lookup(f"WHERE md5 = '{image.md5}'")
            if len(row) > 0:

                # If file and size are the same, grab the crc32 and md5 to verify dupe
                logger.warning(
                    f"Duplicate image verified: {full} already exists in "
                    + f"at {row[2]}"
                )
                report["duplicates"].append(image.full_path)
                continue
            else:
                row = ic.lookup(
                    f"WHERE crc32 = '{image.crc32}' and size = '{image.size}'"
                )
                if len(row) > 0:
                    logger.warning(
                        f"Ambiguous files detected. {full} has same size and "
                        + f"crc32 as source directory file {row[2]}, but md5 "
                        + "does not match."
                    )
                    report["ambiguous"].append(image.full_path)
                    continue

            # Add the file to the list of potentials to migrate
            report["migrate"].append(image.full_path)

    pp = pprint.PrettyPrinter(indent=2, compact=False)
    pp.pprint(report)

    logger.info("Completed duplicate scan.")
    logger.info(f"Processed {ic.get_count()} images in {ic.processing_time} seconds.")
    logger.info(
        f"Report:\n\tDuplicates:\t{len(report['duplicates'])}"
        + f"\n\tAmbiguous:\t{len(report['ambiguous'])}"
        + f"\n\tUnique:\t{len(report['migrate'])}"
    )
    tstamp = datetime.datetime.now().strftime("find_dupes_%Y-%m-%d.json")
    with open(tstamp, "w") as fout:
        fout.write(json.dumps(report))
    logger.info(f"Report written to {tstamp}")
コード例 #21
0
ファイル: game.py プロジェクト: sir-pinecone/PyGameTemplate
class Game(Event):
    LEFT_MOUSE = 1
    MIDDLE_MOUSE = 2
    RIGHT_MOUSE = 3

    def __init__(self):
        background_fill = (0, 0, 0)
        self.w, self.h = config.WINDOW_WIDTH, config.WINDOW_HEIGHT
        size = (self.w, self.h)

        self.running = False
        self.clock = pygame.time.Clock() 
        self.screen_size = size
        self.slowmo = False

        # Create the window
        pygame.init()
        self.window = pygame.display.set_mode(size)
        self.window.fill(background_fill)
        pygame.display.flip()

        self.image_cache = ImageCache()
        self._setup()

    def _setup(self):
        self._load_resources()
        # Sprite container
        self.sprites = pygame.sprite.RenderUpdates()
        self.set_screen(GameScreen(self))

    def _load_resources(self):
        self.image_cache.add('sprites', os.path.join(config.TILESET_PATH, 'sprites.png'))

    def check_inputs(self):
        """Go through the event queue and fire off callbacks based on the inputs"""
        for event in pygame.event.get():
            self.handle_event(event)

    def set_screen(self, screen):
        if hasattr(self, 'screen'):
            self.screen.hide()
        self.screen = screen
        self.screen.show()

    def update(self, dt):
        self.screen.update(dt)

    def draw(self, dt):
        self.sprites.clear(self.window, self.screen.get_background()) 
        self.screen.draw(dt)
        pygame.display.update(self.sprites.draw(self.window))

    def main_loop(self):
        self.running = True
        self.playtime = 0
        max_fps = 60
        accum_dt = 0

        while self.running:
            dt = min(self.clock.tick_busy_loop(max_fps) * 0.001, 0.1)  
            
            seconds = pygame.time.get_ticks() / 1000.0
            self.playtime = seconds

            pygame.display.set_caption("FPS: %.2f" % self.clock.get_fps())
    
            self.check_inputs()

            if self.slowmo:
                dt /= 3

            accum_dt += dt
            while accum_dt > 0:
                dt = 0.01
                accum_dt -= dt
                self.update(dt)

            self.draw(dt)
            pygame.display.flip()

    # Events
    def on_exit(self):
        self.running = False

    def on_key_up(self, event):
        if event.key == K_ESCAPE:
            self.on_exit()

    def on_key_down(self, event):
        key = event.key

        if key == K_s:
            self.slowmo = True
        else:
            self.slowmo = False
         
    def on_mouse_lbtn_down(self, event):
        pos = event.pos

    def on_mouse_rbtn_up(self, event):
        pos = event.pos
        
    def on_mouse_motion(self, event):
        buttons, pos, rel = event.buttons, event.pos, event.rel