def animateSimulatedTask(images): print(len(images)) num_across = 5 task_img0 = images # taskid = tasks[40] height = int(math.ceil(len(task_img0) / num_across)) fig, axs = plt.subplots(height, num_across, figsize=(20, 15)) fig.tight_layout() plt.subplots_adjust(hspace=0.2, wspace=0.2) # We can visualize the simulation at each timestep. for i, (ax, image) in enumerate(zip(axs.flatten(), task_img0)): # Convert the simulation observation to images. if image is None: continue img = phyre.observations_to_float_rgb(image) ax.imshow(img) ax.title.set_text(f'Timestep {i}') ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) plt.show() frames = [] for taskimgi in task_img0: if taskimgi is None: continue frames.append(phyre.observations_to_uint8_rgb(taskimgi)) from array2gif import write_gif timestr = time.strftime("%Y%m%d-%H%M%S") write_gif(np.asarray(frames), 'rgbbgr.gif' + timestr, fps=8)
def run(max_epoch): gif = np.empty((max_epoch, 800, 1600, 3), dtype=np.uint8) for i in range(max_epoch): gif[i] = cv2.imread(os.path.join('first_kernel_visualization', 'result%04d.png' % i)) np.maximum(np.minimum(gif, 255), 0) write_gif(gif, 'first_kernel_visualization.gif', fps=2)
def load_and_gif(self): with tf.Session() as sess: sess.run(self.init) self.saver.restore(sess, self.save_directory) frames = [] def add_frame(s): frame = np.reshape(s[:self.rows * self.columns], (self.rows, self.columns)) # Upscale so GIF is larger than 20x10 pixels frame = np.kron(frame, np.ones((20, 20)))[::-1] # Convert to RGB array frame = np.stack((frame, frame, frame)) * 100 frames.append(frame) game = tetris.Game(self.rows, self.columns, self.tetriminos, self.end_at) while not game.game_over: state = game.state() add_frame(state) q_values = sess.run(self.net.q_out, feed_dict={self.net.inputs: [state]}) mask = validity_mask(game.next_tetrimino, self.columns) q_masked = np.where(mask, q_values, -1000) action = np.argmax(q_masked, 1)[0] rotation, column = action_to_rc(action, self.columns) game.place_tetrimino(rotation, column) add_frame(game.state()) write_gif(frames, 'gif/game.gif', fps=2) print('GIF saved')
def main(): env = pistonball_v4.env(n_pistons=20, local_ratio=0, time_penalty=-0.1, continuous=True, random_drop=True, random_rotate=True, ball_mass=0.75, ball_friction=0.3, ball_elasticity=1.5, max_cycles=125) total_reward = 0 obs_list = [] NUM_RESETS = 1 i = 0 for i in range(NUM_RESETS): env.reset() for agent in env.agent_iter(): obs, rew, done, info = env.last() act = policy(obs) if not done else None env.step(act) total_reward += rew i += 1 if i % (len(env.possible_agents) + 1) == 0: obs_list.append( np.transpose(env.render(mode='rgb_array'), axes=(1, 0, 2))) env.close() print("average total reward: ", total_reward / NUM_RESETS) write_gif(obs_list, 'pistonball_ben.gif', fps=15)
def play(env, model, agent_history_length=4): preprocess = _preprocess if len( env.observation_space.shape) == 3 else lambda x: x print('Begin play') for episode in range(sys.maxsize): ob, done = preprocess(env.reset()), False obs = [ob for i in range(agent_history_length)] episode_rew = 0 frames = [] timesteps = 0 ale_lives = 6 while not done: timesteps += 1 # env.render() frames.append(env.render(mode='rgb_array')) observations = np.array([obs]) actions = np.reshape(np.ones(env.action_space.n), [1, -1]) rewards = model.predict_on_batch([observations, actions])[0] action = np.argmax(rewards) ob, rew, done, info = env.step(action) if info['ale.lives'] < ale_lives: ale_lives = info['ale.lives'] ob, rew, done, info = env.step(1) episode_rew += rew obs.pop(0) obs.append(preprocess(ob)) filename = 'gifs/' + env.spec.id + '-' + str(episode) + '.gif' write_gif(np.asarray(frames), filename, fps=20) print("Episode reward", episode_rew) exit(1)
def main(): for x in range(0, 20): for y in range(0, 20): st = gl_FragCoord color = np.array([0.0, 0.0, 0.0]) q = np.array([0.0, 0.0]) q[0] = fbm(st + 0.00 * u_time) q[1] = fbm(st + np.array([1.0, 0.0])) r = np.array([0.0, 0.0]) r[0] = fbm(st + 1.0 * q + np.array([1.7, 9.2]) + 0.15 * u_time) r[1] = fbm(st + 1.0 * q + np.array([8.3, 2.8]) + 0.126 * u_time) f = fbm(st + r) color = mix(np.array([0.101961, 0.619608, 0.666667]), np.array([0.666667, 0.666667, 0.498039]), clamp((f * f) * 4.0, 0.0, 1.0)) color = mix(color, np.array([0, 0, 0.164706]), clamp(math.hypot(*q), 0.0, 1.0)) color = mix(color, np.array([0.666667, 1, 1]), clamp(math.sqrt(r[0]**2), 0.0, 1.0)) viewport[x, y] = np.vectorize(lambda x: int(255 * abs(x)))( np.array([*((f * f * f + .6 * f * f + .5 * f) * color)])) write_gif(viewport, 'test.gif', fps=5)
def to_gif(dataset, filename, fps=8): from array2gif import write_gif print("Frames: {}".format(len(dataset))) colors = [] write_gif([to_two_color(lattice) for lattice in dataset], filename, fps=fps)
def write_gif(img3d, filename, fps=24): """ Write the image to the given paths as a GIF, one frame per layer. """ import array2gif to_rgb = [ np.array([layer, layer, layer]).astype(int) // 100 for layer in img3d ] array2gif.write_gif(to_rgb, filename, fps=fps)
def train_player(env_width, env_height, player, z, nb_episodes, horizon=100): opponent = Opponent(env_width, env_height, z) env = GridWorldEnv(player, opponent, env_width, env_height, horizon) discount_factor = 0.7 epsilon = 0.9 episode_rewards = [] data = [] for episode in range(1, nb_episodes + 1): print(f"Episode {episode}: training with z = {z}") epsilon *= 0.9 (old_image, old_h) = env.reset() done = False frames = [] episode_reward = 0 while not done: data.append((old_image.reshape(-1), z)) player_action = player.take_action(state=old_h, z=z, epsilon=epsilon) opponent_action = opponent.take_action(state=old_h, epsilon=epsilon) (new_image, new_h), reward, done = env.step([player_action, opponent_action]) player.learn(reward, discount_factor, old_h, new_h, player_action, z) old_h, old_image = new_h, new_image episode_reward += reward frame = env.render() frames.append(frame) episode_rewards.append(episode_reward) file_name = f"episodes/training/{z}/episode_{episode}.gif" write_gif(frames, file_name, 3) player.update_data(data) plot_rewards(episode_rewards, f"plots/{z}.png")
def output_to_gif(dataset, filename, fps=8): print("Frames: {}".format(len(dataset))) colors = [] write_gif( [to_two_color(lattice) for lattice in dataset], filename, fps=fps )
def save_gif(hist_theta, fps=10): images = [np.argmax(t, axis=0) * 255 for t in hist_theta] dataset = [np.array([t, t, t]) for t in images] script_dir = os.path.dirname(__file__) results_dir = os.path.join(script_dir, 'output/') write_gif(dataset, results_dir + 'result.gif', fps=fps)
def draw1(): k = 500 t = 1600 ski = np.zeros((k, k), dtype=int) ski_back = np.array([[0, 1, 0], [0, 0, 1], [1, 1, 1]]) ski[:3, :3][ski_back > 0] = 1 outs = lifegame1(ski, t) write_gif(outs, 'output1.gif')
def run(T_over_Tc, width=200, height=200): Tc = 2.269 # Normalized T := kT/J T = T_over_Tc * Tc dataset = None fps = 500 dataset = standard_approach(T, width, height, N=1000) filename = ('ising_{}_{}x{}.gif'.format(T_over_Tc, width, height)) write_gif(dataset, filename, fps)
def draw(): k = 500 t = 1600 ski = torch.zeros(1, 1, k, k, dtype=torch.int64) ski_back = torch.tensor([[0, 1, 0], [0, 0, 1], [1, 1, 1]]) ski[0][0][:3, :3][ski_back > 0] = 1 outs = lifegame(ski, t) write_gif(outs, 'output.gif')
def make_gif(exp, **kwargs): """ Create a gif from the estimates """ estimates = exp.xf filename = f"{exp.save_folder}/{hash(exp)}.gif" write_gif(estimates, filename, **kwargs) Image(filename)
def evaluate(actor_critic, ob_rms, env_name, seed, num_processes, eval_log_dir, device, custom_gym, gif=False): eval_envs = make_vec_envs(env_name, seed + num_processes, num_processes, None, eval_log_dir, device, True, custom_gym) vec_norm = utils.get_vec_normalize(eval_envs) if vec_norm is not None: vec_norm.eval() vec_norm.ob_rms = ob_rms eval_episode_rewards = [] obs = eval_envs.reset() eval_recurrent_hidden_states = torch.zeros( num_processes, actor_critic.recurrent_hidden_state_size, device=device) eval_masks = torch.zeros(num_processes, 1, device=device) images = [] while len(eval_episode_rewards) < 10: with torch.no_grad(): images.append(obs[0, -3:, :].squeeze().cpu().numpy()) _, action, _, eval_recurrent_hidden_states = actor_critic.act( obs, eval_recurrent_hidden_states, eval_masks, deterministic=True) # Obser reward and next obs obs, _, done, infos = eval_envs.step(action) eval_masks = torch.tensor([[0.0] if done_ else [1.0] for done_ in done], dtype=torch.float32, device=device) for info in infos: if 'episode' in info.keys(): eval_episode_rewards.append(info['episode']['r']) images.append(obs[0, -3:, :].squeeze().cpu().numpy()) eval_envs.close() if gif: array2gif.write_gif(images, 'replay.gif', fps=4) config.tensorboard.run.log( {"video": wandb.Video('replay.gif', fps=4, format="gif")}, commit=True) config.tensorboard.run.history._flush() print(" Evaluation using {} episodes: mean reward {:.5f}\n".format( len(eval_episode_rewards), np.mean(eval_episode_rewards)))
def main(t=1.35, w=42, h=42): global T, width, height T, width, height = t, w, h dataset = simulate(80) dataset.append(dataset[-1] * 0) dataset.append(dataset[-1] * 0) filename = 'ising_{}_{}x{}.gif'.format(T, width, height) full_path = os.path.join('img', 'manual', filename) write_gif(dataset, full_path, fps=8)
def get_video_from_model(model_name, parameter_filename, save_directory, filename, reward_level="team", num_agents=2): fitness_calculator = FitnessCalculator(parameter_filename) results = None if reward_level == "team": full_genome = np.load(model_name) agent_list = [] for i in range(num_agents): start = i * int(len(full_genome) / num_agents) end = (i + 1) * int(len(full_genome) / num_agents) sub_genome = full_genome[start:end] agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, sub_genome) agent_list += [agent] '''full_genome = NNAgent.load_model_from_file(model_name) mid = int(len(full_genome) / num_agents) agent_list = [] for i in range(num_agents): genome = full_genome[mid*i : mid*(i+1)] agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome) agent_list += [agent]''' results = fitness_calculator.calculate_fitness(agent_list=agent_list, render=True, time_delay=0, render_mode="rgb_array") elif reward_level == "individual": agent_list = [] for i in range(num_agents): genome = NNAgent.load_model_from_file(model_name) agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome) agent_list += [agent] results = fitness_calculator.calculate_fitness(agent_list=agent_list, render=True, time_delay=0, render_mode="rgb_array") video_data = results['video_frames'] save_to = f"{save_directory}/{filename}" write_gif(video_data, save_to, fps=5)
def gif_gray(arr, fname, fps=10): assert arr.ndim == 3, "Array must be 3D" arr_rgb = np.copy(arr).astype(np.float32) arr_rgb -= arr_rgb.min() arr_rgb /= arr_rgb.ptp() arr_rgb *= 255 arr_rgb = np.repeat(arr_rgb[:, None], 3, 1) write_gif(arr_rgb.astype(np.uint8), fname, fps)
def save(self, filename="out.gif", size=(250,250)): mult = min(size[0] // self.height, size[1] // self.width) frames = np.copy(self.frames) frames = np.repeat(frames,mult,axis=1) frames = np.repeat(frames,mult,axis=2) frames = np.rollaxis(frames,-1,1) frames *= 255 frames = frames.astype(int) write_gif(list(frames), filename, fps=5) return Image(filename)
def random_agent(episodes=10): env = gym.make("GoalGrid-v0") all_states = [] for i in range(5): env.reset() # env.render() for e in range(episodes): action = env.action_space.sample() state, reward, done, _ = env.step(action) all_states.append(state) print(reward) if done: break write_gif(all_states, str(i) + ".gif", fps=10) all_states = []
def create_gif(x, file_path, fs): ''' Creates a gif clip Args: :x: numpy array of size t_steps x height x width :file_path: absolute full path to store the gif :fs: The (integer) frames/second of the animatio :return: None ''' if np.any(x > 255): x = x.astype(np.float64) / x.max() # normalize the data to 0 - 1 x = 255 * x # Scale by 255 x = x.astype(np.uint8) # convert to uint8 write_gif(reshape_for_gif(x), file_path, fs)
def save_gif(self, trajectory, name): """ This method allows to save a trajectory as a gif. """ # We instantiate dataset image_list = list() # We loop through coordinates to write images. for coord in trajectory: coord = (coord + 1) / 2 arr = self.draw(coord.tolist()) * 255.0 arr = arr.astype(np.uint8) arr = arr.reshape([1, 70, 70]) arr = np.repeat(arr, 3, axis=0) image_list.append(arr) array2gif.write_gif(image_list, name, fps=25)
def test_player(env_width, env_height, player, z, horizon=100): epsilon = 0 opponent = Opponent(env_width, env_height, z) env = GridWorldEnv(player, opponent, env_width, env_height, horizon) print(f"Testing with z = {z}") image, h = env.reset() done = False frames = [] while not done: player_action = player.take_action(state=h, image=image, epsilon=epsilon) opponent_action = opponent.take_action(state=h, epsilon=epsilon) (image, h), reward, done = env.step([player_action, opponent_action]) frame = env.render() frames.append(frame) file_name = f"episodes/testing/{z}/episode.gif" write_gif(frames, file_name, 3)
def write_gif_func(self, ip_img, imsize, save_dir,index=0): ''' To save a gif of the input stack of 2D slices input params: ip_img: input stack of 2D slices imsize: image dimensions save_dir:directory to save the gif returns: None ''' y = np.squeeze(ip_img) y_t=np.transpose(y) recons_ims = np.reshape(y_t,(self.img_size_x*self.img_size_y,self.batch_size)) dataset =np.transpose(recons_ims.reshape(1,imsize[0],imsize[1],recons_ims.shape[1]),[3,0,1,2]) np.expand_dims(dataset, axis=1) dataset = np.tile(dataset, [1,3,1,1]) imname=save_dir+'plots/test_slice_index_'+str(index)+'.gif' write_gif((dataset*256).astype(np.uint8), imname, fps=5)
def play(n_play, seed, record): vae_path = sorted(glob.glob(os.path.join(hp.ckpt_dir, 'vae', '*.pth.tar')))[-1] vae_state = torch.load(vae_path, map_location={'cuda:0': str(device)}) rnn_path = sorted(glob.glob(os.path.join(hp.ckpt_dir, 'rnn', '*.pth.tar')))[-1] rnn_state = torch.load(rnn_path, map_location={'cuda:0': str(device)}) agent_path = sorted( glob.glob(os.path.join(hp.ckpt_dir, 'A3C', '*.pth.tar')))[-1] agent_state = torch.load(agent_path, map_location={'cuda:0': str(device)}) vae = VAE(hp.vsize).to(device) vae.load_state_dict(vae_state['model']) vae.eval() rnn = RNN(hp.vsize, hp.asize, hp.rnn_hunits).to(device) rnn.load_state_dict(rnn_state['model']) rnn.eval() print('Loaded VAE: {}\n RNN: {}\n Agent: {}\n'.format( vae_path, rnn_path, agent_path)) state_dims = hp.vsize + hp.rnn_hunits + 100 if hp.use_binary_feature else hp.vsize + hp.rnn_hunits hidden_dims = hp.ctrl_hidden_dims lr = 1e-4 global_agent = A3C(input_dims=state_dims, hidden_dims=hidden_dims, lr=lr).to(device) global_agent.share_memory() global_agent.load_state_dict(agent_state['agent'].state_dict()) _, gif = test_process(global_agent, vae, rnn, 0, 0, state_dims, hidden_dims, lr, n_play, seed, record) if record: gif = list( map(lambda img: np.array(PIL.Image.fromarray(img[::2, ::2, :], 'RGB')\ ).transpose([2,0,1]), gif) ) write_gif(gif, 'a3c.gif', fps=30)
def alpha_evolution(self, steps=100, time_iterations=100, max=2.0, min=1.5, save_figure=True, save_data=False): # First, generate all data data_array = [] rgb_data_array = [] for index, al in enumerate(np.linspace(min, max, steps)): print("Evolution {} out of {}...".format(index, steps)) # Update CML's alpha value self.cml.coupling.map_obj.set_alpha(al) data = self.time_evolution(time_iterations, 1, False, False, False) # For now: artificial RGB for array2gif g_to_rgb = lambda x: [x, x, x] rgb_data = [] for row in data: rgb_row = [] for val in row: rgb_row.append(g_to_rgb(val)) rgb_data.append(rgb_row) rgb_data = np.array(rgb_data) rgb_data = (rgb_data + 1.0) * 255.0 / 2.0 rgb_data = rgb_data.astype(int) data_array.append(data) rgb_data_array.append(rgb_data) if save_figure: write_gif(rgb_data_array, "alpha_evolution.gif", fps=10) if save_data: np.save("alpha_evolution.npy", np.array(data_array)) return np.array(data_array)
def export(self, filename, delay, length=30000): self._run_init() progress = 0 while not self.is_quit: try: self._run_update(delay) self._run_draw() progress += delay if progress >= length: raise StopIteration except StopIteration: self.quit() if filename.endswith('.png'): write_apng(filename, self.seq, delay=delay, use_palette=True) elif filename.endswith('.gif'): # array2gif requires a transposed matrix compared to numpngw. seq = [] for s in self.seq: seq.append(np.transpose(s, axes=[1, 0, 2])) write_gif(seq, filename, fps=int(1000 / delay)) else: raise NotImplementedError('Unsupported file format')
def to_gif(arrays, file_name): '''Exports a list of given arrays as an animated gif with the given file name''' real_arrays = [] for i in range(len(arrays)): temp = np.zeros((len(arrays[i]), len(arrays[i][0]), 3)) for j in range(len(arrays[i])): for k in range(len(arrays[i][0])): color = colors[arrays[i][j][k]] temp[j][k] = color real_arrays.append(temp) arrays = real_arrays for i in range(len(arrays)): arrays[i] = np.transpose(arrays[i], (1, 0, 2)) max_pixels = 500 width = arrays[0].shape[0] height = arrays[0].shape[1] scaling_factor = min(int(max_pixels / width), int(max_pixels / height)) fps = 3 new_width = width * scaling_factor new_height = height * scaling_factor new_arrays = [] for i in range(len(arrays)): temp = np.zeros((new_width, new_height, 3)) for j in range(new_width): for k in range(new_height): temp[j][k] = arrays[i][int(j / scaling_factor)][int( k / scaling_factor)] new_arrays.append(temp) write_gif(new_arrays, file_name, fps)
def render_gif_on_wandb(self): images = [] if hasattr(self.training_env, "envs"): env = self.training_env.envs[0] else: env = self.training_env obs = env.reset() with torch.no_grad(): for i in range(500): action, _states = self.model.predict(obs) obs, rewards, dones, info = env.step(action) if dones: break obs = obs.transpose(1, 0, 2) obs = obs[:, :, ::-1] images.append(obs) array2gif.write_gif(images, 'replay.gif', fps=4) config.tensorboard.run.log( {"video": wandb.Video('replay.gif', fps=4, format="gif")}, commit=True) config.tensorboard.run.history._flush() env.close()
print("") print("Starting the training process") print("") from hyperopt import fmin, tpe, space_eval, Trials trials = Trials() videos = [] for i in range(0,20) : best = fmin(fitnessFunction, space, algo=tpe.suggest, trials=trials, max_evals=max(1,i*10), show_progressbar=False) args = space_eval(space, best) score = int(-1*fitnessFunction(args)) print(i*10,"-->",score) phenotype = ga.Phenotype(shape=[4,5,1],data=args) videos.append( oneTrial(phenotype,demo=True,label="Trial " + str(max(1,i*10))) ) if score >= 500 : break env.close() print("Creating video") video = np.concatenate( videos , 0 ) write_gif(video[::2],"pics/cartpole_hp.gif",fps=15) print("Done") env.close() #env.close()