def demo(networks, images, fixed_noise, ac_scale, sample_scale, result_dir, epoch=0, idx=0): netE = networks['encoder'] netG = networks['generator'] def image_filename(*args): image_path = os.path.join(result_dir, 'images') name = '_'.join(str(s) for s in args) name += '_{}'.format(int(time.time() * 1000)) return os.path.join(image_path, name) + '.jpg' demo_fakes = netG(fixed_noise, sample_scale) img = demo_fakes.data[:16] filename = image_filename('samples', 'scale', sample_scale) caption = "S scale={} epoch={} iter={}".format(sample_scale, epoch, idx) imutil.show(img, filename=filename, resize_to=(256, 256), caption=caption) aac_before = images[:8] aac_after = netG(netE(aac_before, ac_scale), ac_scale) img = torch.cat((aac_before, aac_after)) filename = image_filename('reconstruction', 'scale', ac_scale) caption = "R scale={} epoch={} iter={}".format(ac_scale, epoch, idx) imutil.show(img, filename=filename, resize_to=(256, 256), caption=caption)
def main(): print('Starting NMF demo') print('Downloading MNIST data...') mnist = get_mnist(digit=None) filename = get_filename() imutil.show(mnist.mean(0), filename=filename, resize_to=(256, 256), caption="Average MNIST digit") print('Output average MNIST digit to {}'.format(filename)) print('Computing factorization, please wait...') start_time = time.time() clusters = factorize(mnist) print('Completed factorization with {} components in {:.3f}s'.format( n_components, time.time() - start_time)) filename = get_filename() caption = "Summarizing {} samples in {} clusters".format( len(mnist), n_components) imutil.show(clusters, filename=filename, resize_to=(512, 512), caption=caption) print('Wrote output file to {}'.format(filename))
def play(args): print("Creating gym with args: {}".format(args)) env = gym.make(args.env) env.unwrapped.frameskip = 4 env._max_episode_steps = None state = torch.Tensor(preprocess(env.reset())) # get first state agent = MCTSAgent(env.action_space) num_frames = 0 num_games = 0 cumulative_reward = 0 while num_frames < MAX_FRAMES: # Decide which action to take action = agent.act(state, env) # Take that action state, reward, done, _ = env.step(action) cumulative_reward += reward imutil.show(state, video_filename='pure_mcts.mjpeg') if done: print("Restarting game after frame {}".format(num_frames)) num_games += 1 env.reset() state = torch.Tensor(preprocess(state)) # Record the results num_frames += 1 print("Finished after {} frames with {:.3f} reward/game".format( num_frames, cumulative_reward / num_games))
def generate_open_set(networks, dataloader, **options): """ # TODO: Fix Dropout/BatchNormalization outside of training for net in networks: networks[net].eval() """ result_dir = options['result_dir'] # Start with randomly-selected images from the dataloader start_images, _ = dataloader.get_batch() openset_class = dataloader.num_classes images = generate_counterfactual_column(networks, start_images, openset_class, **options) images = np.array(images).transpose((0,2,3,1)) dummy_class = 0 video_filename = make_video_filename(result_dir, dataloader, dummy_class, dummy_class, label_type='grid') # Save the images in npy/jpg format as input for the labeling system trajectory_filename = video_filename.replace('.mjpeg', '.npy') np.save(trajectory_filename, images) imutil.show(images, display=False, filename=video_filename.replace('.mjpeg', '.jpg')) # Save the images in jpg format to display to the user name = 'counterfactual_{}.jpg'.format(int(time.time())) jpg_filename = os.path.join(result_dir, 'images', name) imutil.show(images, filename=jpg_filename) return images
def play(args, AgentType): print("Creating gym with args: {}".format(args)) env = gym.make(args.env) env.unwrapped.frameskip = 4 env._max_episode_steps = None state = torch.Tensor(preprocess(env.reset())) # get first state agent = AgentType(env.action_space) start_time = time.time() num_frames = 0 num_games = 0 cumulative_reward = 0 while num_frames < MAX_FRAMES: # Decide which action to take action = agent.act(state, env) # Take that action state, reward, done, _ = env.step(action) cumulative_reward += reward imutil.show(state, video_filename=args.video, display=(num_frames % 10 == 0)) state = torch.Tensor(preprocess(state)) #imutil.show(state, save=False) if done: print("Restarting game after frame {}".format(num_frames)) num_games += 1 env.reset() num_frames += 1 num_games += 1 print("Finished after {} frames with {:.3f} reward/game".format( num_frames, cumulative_reward / num_games)) print("Speed: {:.3f} frames per second".format(num_frames / (time.time() - start_time)))
def generate_grid(networks, dataloader, **options): result_dir = options['result_dir'] result_dir = options['result_dir'] # Generate a K by K square grid of examples, one column per class K = dataloader.num_classes images = [[] for _ in range(N)] for img_batch, label_batch, _ in dataloader: for img, label in zip(img_batch, label_batch): if label < N and len(images[label]) < N: images[label].append(img.cpu().numpy()) if all(len(images[i]) == N for i in range(N)): break flat = [] for i in range(N): for j in range(N): flat.append(images[j][i]) images = flat images = np.array(images).transpose((0,2,3,1)) start_class = 0 video_filename = make_video_filename(result_dir, dataloader, start_class, start_class, label_type='grid') # Save the images in npy format to re-load as training data trajectory_filename = video_filename.replace('.mjpeg', '.npy') np.save(trajectory_filename, images) # Save the images in jpg format to display to the user imutil.show(images, filename=video_filename.replace('.mjpeg', '.jpg'))
def compare_active_learning(eval_filename, baseline_eval_filename, title=None, this_name='This Method', baseline_name='Baseline', prefix='grid', statistic='accuracy'): try: x, y = parse_active_learning_series(eval_filename, prefix=prefix, statistic=statistic) x2, y2 = parse_active_learning_series(baseline_eval_filename, prefix=prefix, statistic=statistic) except NoDataAvailable: return None print("Comparing {} with baseline {}".format(eval_filename, baseline_eval_filename)) plt.plot(x, y, "g") # this method plt.plot(x2, y2, "b") # baseline this_approach_name = eval_filename.split('/') plt.ylabel('Accuracy') plt.xlabel('Number of Queries') plt.legend([this_name, baseline_name]) if title: plt.suptitle(title) fig_filename = eval_filename.replace('.json', '-vs-baseline.png') plt.savefig(fig_filename) show(fig_filename) return fig_filename
def generate_counterfactuals(encoder, generator, classifier, dataset): cf_open_set_images = [] for images, labels in dataset: counterfactuals = generate_cf( encoder, generator, classifier, images) cf_open_set_images.append(counterfactuals) print("Generated {} batches of counterfactual images".format(len(cf_open_set_images))) imutil.show(counterfactuals, filename='example_counterfactuals.jpg', img_padding=8) return cf_open_set_images
def test_video_output(self): # If video_filename is specified, then save ONLY to video x = np.zeros((128, 128)) listing_before = os.listdir('.') for i in range(10): x += 10. imutil.show(x, video_filename='test_output.mjpeg') listing_after = os.listdir('.') assert len(listing_after) == len(listing_before) + 1
def plot_active_learning(eval_filename="results_epoch_0025.json"): try: x, y = parse_active_learning_series(eval_filename) except NoDataAvailable: return None plot = plot_xy(x, y, x_axis="Number of Examples", y_axis="Accuracy", title=eval_filename) plot_filename = "{}.jpg".format(eval_filename.replace('.json', '')) show(plot, filename=plot_filename) return plot
def demo(model): image, label = load_data(1) show(image) pred = model.predict(image) if np.argmax(pred) == 0: print("This is a cat with probability {:.2f}".format(pred.max())) else: print("This is a dog with probability {:.2f}".format(pred.max())) print('\n')
def main(render=False): unique_id = int(time.time()) if render: video_filename = 'video_{}.mp4'.format(unique_id) env = macro_strategy.MacroStrategyEnvironment( video_filename=video_filename, verbose=True) else: env = macro_strategy.MacroStrategyEnvironment(render=False, verbose=True) state = env.reset() done = False agent = RandomAgent(env.action_space()) while not done: # Our agent takes the state as input and selects actions to maximize reward action = agent.step(state) # Take an action and simulate the game for one time step (~5 seconds) state, reward, done, info = env.step(action) # The state is a tuple of: # features_minimap: np array of features from the minimap view # features_screen: np array of features from the camera view # rgb_minimap: np array of an RGB pixel view of the minimap (if render=True) # rgb_screen: np array of RGB pixel rendered frame of Starcraft II (if render=True) features_minimap, features_screen, rgb_minimap, rgb_screen = state # Example code for visualizing the state filename = "output_frame_{}_{:05d}.jpg".format(unique_id, env.steps) caption = macro_strategy.action_to_name[action] caption = 't={} Reward={} Action: {}'.format(env.steps, reward, caption) left = imutil.show(colorize(features_minimap[4]), resize_to=(256, 256), return_pixels=True, display=False, save=False) right = imutil.show(colorize(features_minimap[5], mode='nipy'), resize_to=(256, 256), return_pixels=True, display=False, save=False) pixels = np.concatenate([left, right], axis=1) if render: screenshot = imutil.show(rgb_screen, resize_to=(512, 288), return_pixels=True, display=False, save=False) pixels = np.concatenate([pixels, screenshot], axis=0) imutil.show(pixels, filename=filename, caption=caption) print('Finished game with final reward {}'.format(reward))
def main(): # Environment options: # rollout_video=True: Generate .mp4 video of the battle at the end # verbose=False: Print debug statements unique_id = int(time.time()) video_filename = 'video_{}.mp4'.format(unique_id) env = fog_of_war.FogOfWarMultiplayerEnvironment( video_filename=video_filename) state = env.reset() done = False # Two agents play each other: the learner (blue) and the adversary (red) blue_agent = RandomAgent(env.action_space()) red_agent = RandomAgent(env.action_space()) while not done: # The buildable units are named Rock, Paper, and Scissors # 1: Build paper in reserves # 2: Build paper in front # 3: Build rock in reserves # 4: Build rock in front # 5: Build scissors in reserves # 6: Build scissors in front # 7: Scout to reveal the enemy's army import random blue_action = random.choice([1, 4, 7]) red_action = random.choice([5, 6, 7]) # Take an action and simulate the game for one time step (~10 seconds) state, reward, done, info = env.step(blue_action, red_action) # The state is a tuple of: # features_minimap: np array of features from the minimap view # features_screen: np array of features from the camera view # rgb_minimap: np array of an RGB pixel view of the minimap # rgb_screen: np array of RGB pixel rendered frame of Starcraft II features_minimap, features_screen, rgb_minimap, rgb_screen = state # Example code for visualizing the state filename = "output_frame_{}_{:05d}.jpg".format(unique_id, env.steps) blue_caption = fog_of_war.action_to_name[blue_action] red_caption = fog_of_war.action_to_name[red_action] caption = 't={} R={} Left: {} Right: {}'.format( env.steps, reward, blue_caption, red_caption) top = imutil.show(rgb_minimap, resize_to=(800, 480), return_pixels=True, display=False) bottom = imutil.show(rgb_screen, resize_to=(800, 480), return_pixels=True, display=False) imutil.show(np.concatenate([top, bottom], axis=0), filename=filename, caption=caption) print('Finished game')
def route_step(): action = flask.request.json.get('action') print('action from form is: {}'.format(action)) state, reward, done, info = env.step(action) imutil.show(state, filename='static/screenshot.jpg') frame_num = env.ale.getFrameNumber() print('took action, now at frame num {}'.format(frame_num)) if done: env.reset() #return flask.Response('OK', mimetype='application/json') #return flask.jsonify({'frame_num': frame_num}) return 'OK'
def demo(model, user_input=None, **params): batch_size = params['batch_size'] # Data for demo prediction X, Y = get_batch(**params) if user_input: X[1][0] = words.indices(user_input) preds = model.predict(X) print("Target (left) vs. Network Output (right):") input_pixels, input_words = X[0][0], X[1][0] print(words.words(input_words)) left = input_pixels + Y[0] * 255. right = input_pixels + map_to_img(preds[0], **params) imutil.show(np.concatenate((left, right), axis=1))
def generate_counterfactual(networks, dataloader, **options): """ # TODO: Fix Dropout/BatchNormalization outside of training for net in networks: networks[net].eval() """ result_dir = options['result_dir'] # NOTE: Too many classes in datasets like cub200 K = min(dataloader.num_classes, 10) # Make the batch size large enough to form a square grid cf_count = K + 2 # Start with randomly-selected images from the dataloader start_images, _ = dataloader.get_batch() start_images = start_images[:cf_count] # assume batch_size >= cf_count batches = [start_images.cpu().numpy()] for target_class in range(K + 1): # Generate one column of the visualization, corresponding to a target class img_batch = generate_counterfactual_column(networks, start_images, target_class, **options) batches.append(img_batch) images = [] for i in range(cf_count): for batch in batches: images.append(batch[i]) images = np.array(images).transpose((0, 2, 3, 1)) dummy_class = 0 video_filename = make_video_filename(result_dir, dataloader, dummy_class, dummy_class, label_type='grid') # Save the images in npy/jpg format as input for the labeling system trajectory_filename = video_filename.replace('.mjpeg', '.npy') np.save(trajectory_filename, images) imutil.show(images, display=False, filename=video_filename.replace('.mjpeg', '.jpg')) # Save the images in jpg format to display to the user name = 'counterfactual_{}.jpg'.format(int(time.time())) jpg_filename = os.path.join(result_dir, 'images', name) imutil.show(images, filename=jpg_filename) return images
def dream(models, datasets, **params): video_filename = params['video_filename'] dream_frames_per_example = params['dream_fps'] dream_examples = params['dream_examples'] if params['batch_size'] < 2: raise ValueError( "--batch-size of {} is too low, dream() requires a larger batch size" .format(params['batch_size'])) encoder = models['encoder'] decoder = models['decoder'] encoder_dataset = datasets['encoder'] # Select two inputs in the dataset start_idx = np.random.randint(encoder_dataset.count()) end_idx = np.random.randint(encoder_dataset.count()) for _ in range(dream_examples): input_start = encoder_dataset.get_example(start_idx, **params) input_end = encoder_dataset.get_example(end_idx, **params) # Use the encoder to get the latent vector for each example X_list = encoder_dataset.empty_batch(**params) for X, x in zip(X_list, input_start): X[0] = x for X, x in zip(X_list, input_end): X[1] = x latent = encoder.predict(X_list) latent_start, latent_end = latent[0], latent[1] # Interpolate between the two latent vectors, and output # the result of the decoder at each step # TODO: Something other than linear interpolation? for i in range(dream_frames_per_example): c = float(i) / dream_frames_per_example v = c * latent_end + (1 - c) * latent_start img = decoder.predict(np.expand_dims(v, axis=0))[0] # Uncomment for slow/verbose logging # decoder_dataset.unformat_output(img) caption = '{} {}'.format(start_idx, i) imutil.show(img, video_filename=video_filename, resize_to=(512, 512), display=(i % 100 == 0), caption=caption) print("Done") start_idx = end_idx end_idx = np.random.randint(encoder_dataset.count())
def test_reshape_normalize(self): x = np.random.normal(size=(128, 128)) # Reshaping the image should rescale it to 0,255 by default reshaped = imutil.show(x, resize_height=480, resize_width=640, return_pixels=True) assert reshaped.min() >= 0 # Reshaping with normalize=False should leave the scale unchanged reshaped_denorm = imutil.show(x, resize_height=480, resize_width=640, normalize=False, return_pixels=True) assert reshaped_denorm.min() < 0
def test_resize(self): x = np.random.uniform(0, 1, size=(128, 128)) x_resized = imutil.show(x, resize_height=512, resize_width=150, return_pixels=True) assert x_resized.shape == (512, 150, 3)
def check_latent_distribution(encoder, X_real): # Aside: What is the distribution of E(X)? Is it Gaussian? latent = encoder.predict(X_real) import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.figsize = (30, 30) from imutil import show import scipy for i in range(10): c = scipy.stats.pearsonr(latent[:, 0], latent[:, i]) print("Correlation between axes {} and {} is {}".format(0, i, c)) plt.scatter(latent[:, 0], latent[:, i]) plt.savefig('/tmp/foobar.png') show('/tmp/foobar.png', resize_to=None)
def compare_multiple(list_of_filenames, list_of_names, output_filename, title=None): styles = ['-', '--', '-.', ':', '-', '--', '-.', ':'] assert len(list_of_filenames) <= len(styles) plt.figure(figsize=(9.5,6)) for filename, style in zip(list_of_filenames, styles): x, y = parse_active_learning_series(filename) plt.plot(x, y, style) plt.ylabel('Accuracy') plt.xlabel('Number of Queries') plt.legend(list_of_names) if title: plt.suptitle(title) plt.savefig(output_filename) show(output_filename) return output_filename
def play_episode(agent, env): unique_id = int(time.time()) state = env.reset() done = False for i in range(EPISODES): # Our agent takes the state as input and selects actions to maximize reward action = agent.step(state) # Take an action and simulate the game for one time step (~5 seconds) state, reward, done, info = env.step(action) # The state is a tuple of: # features_minimap: np array of features from the minimap view # features_screen: np array of features from the camera view # rgb_minimap: np array of an RGB pixel view of the minimap (if render=True) # rgb_screen: np array of RGB pixel rendered frame of Starcraft II (if render=True) features_minimap, features_screen, rgb_minimap, rgb_screen = state # Example code for visualizing the state filename = "output_frame_{}_{:05d}.jpg".format(unique_id, env.steps) caption = fog_of_war.action_to_name[action] caption = 't={} Reward={} Action: {}'.format(env.steps, reward, caption) left = imutil.show(colorize(features_minimap[4]), resize_to=(256, 256), return_pixels=True, display=False, save=False) right = imutil.show(colorize(features_minimap[5], mode='nipy'), resize_to=(256, 256), return_pixels=True, display=False, save=False) pixels = np.concatenate([left, right], axis=1) if render: screenshot = imutil.show(rgb_screen, resize_to=(512, 288), return_pixels=True, display=False, save=False) pixels = np.concatenate([pixels, screenshot], axis=0) imutil.show(pixels, filename=filename, caption=caption) print('Finished game with final reward {}'.format(reward)) return reward
def train_agent(train_episodes=1000, epochs=100): # This environment teaches win/loss outcomes vs different enemies env = SimpleTacticalEnvironment() agent = ConvNetQLearningAgent(num_input_layers=env.layers(), num_actions=env.actions()) demo_state = env.reset() for epoch in range(epochs): # Train agent env = SimpleTacticalEnvironment() cumulative_reward = 0 cumulative_loss = 0 for i in range(train_episodes): # Each episode consists of a "before", a single action, and an "after" # The state is a tuple of: # features_minimap: np array of features from the minimap view # features_screen: np array of features from the camera view # rgb_minimap: np array of an RGB pixel view of the minimap # rgb_screen: np array of RGB pixel rendered frame of Starcraft II initial_state = env.reset() # Select one of four actions selected_action = agent.step(initial_state) # Take the action and simulate the game for one time step (~10 seconds) result_state, reward, done, info = env.step(selected_action) loss = agent.update(reward) cumulative_loss += loss cumulative_reward += reward avg_reward = cumulative_reward / (i + 1) avg_loss = cumulative_loss / (i + 1) print('Step {}/{} average reward {:.3f} avg. loss {:.3f}'.format( i, train_episodes, avg_reward, avg_loss)) agent.epsilon **= 0.9 print('Updating epsilon to {}'.format(agent.epsilon)) # Evaluate agent print('Evaluating:') selected_action = agent.step(demo_state) estimates = agent.model(to_tensor(demo_state[1]))[0].cpu().data.numpy() caption = 'NW {:.02f}, NE {:.02f}, SE {:.02f}, SW {:.02f}'.format( estimates[0], estimates[1], estimates[2], estimates[3]) imutil.show(demo_state[3], filename="eval_epoch_{:04d}.png".format(epoch), caption=caption, resize_to=(512,512), font_size=10) print('Finished {} epochs'.format(epochs))
def demo_latent_dimensions(before, encoder, decoder, transition, latent_size, num_actions): batch_size = before.shape[0] actions = torch.zeros(batch_size, num_actions).cuda() actions[:, 0] = 1. z = transition(encoder(before), actions) for i in range(latent_size): images = [] dim_min = z.min(dim=1)[0][i] dim_max = z.max(dim=1)[0][i] N = batch_size for j in range(N): dim_range = dim_max - dim_min val = dim_min + dim_range * 1.0 * j / N zp = z.clone() zp[:, i] = val images.append(decoder(zp)) imutil.show(torch.cat(images, dim=0))
def step(self, action_player1, action_player2=None): if self.game_over(): print('Game is over, cannot take further actions') return if self.verbose: print('Taking action_player1={}, action_player2={} at t={}'.format( action_player1, action_player2, self.steps)) self.steps += 1 if self.steps >= MAX_STEPS: if self.verbose: print( 'Game has reached limit of {} actions: simulating endgame'. format(MAX_STEPS)) self.step_until_endgame() else: if action_player1 > 0: player1_ability_id = action_to_ability_id[action_player1] self.use_custom_ability(player1_ability_id, 1) if self.num_players > 1 and action_player2 > 0: player2_ability_id = action_to_ability_id[action_player2] self.use_custom_ability(player2_ability_id, 2) if self.render: # Move forward 5 ticks at a time self.sc2env._step_mul = 5 for i in range(FIVE_SECONDS // self.sc2env._step_mul): self.step_sc2env() filename = 'demo_fog_of_war_frame_output_{:06d}_{:04d}.jpg'.format( self.steps, i) imutil.show(self.unpack_state()[0][3], filename=filename, resize_to=(2 * 800, 2 * 480)) print('Saving file {}'.format(filename)) else: # Move forward 5 seconds in time (in a single step) self.step_sc2env() if self.video: screenshot = self.unpack_state()[0][3] for _ in range(10): self.video.write_frame(screenshot) return self.unpack_state()
def render(self): if self.vis is None: return obs_image = imutil.show(self.last_timestep.observation['rgb_screen'], filename="test.jpg") opts = dict(title = "state", width = 360, height = 350) if self.image_window is None: self.image_window = self.vis.image(obs_image, opts = opts) else: self.vis.image(obs_image, opts = opts, win = self.image_window)
def run_example_code(nets, dataloader, **options): netG = nets['generator'] print("My favorite number is {}".format(options['example_parameter'])) print("I have {} examples in the {} set".format(len(dataloader), options['fold'])) print("This is what a random image looks like:") image_size = options['image_size'] # Torch uses <channels, height, width> image = np.random.rand(3, image_size, image_size) x = np.expand_dims(image, 0) x = torch.autograd.Variable(torch.FloatTensor(x)) x = x.cuda() show(x) my_results = { 'dataset_size': len(dataloader), } return my_results
def render_causal_graph(scm): # Headless matplotlib fix import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import networkx as nx plt.cla() # The SCM will have more rows than columns # Pad with zeros to create a square adjacency matrix rows, cols = scm.shape adjacency = np.zeros((rows, rows)) adjacency[:, :cols] = scm[:] print(adjacency) edge_alphas = adjacency.flatten()**2 from networkx.classes.multidigraph import DiGraph G = DiGraph(np.ones(adjacency.shape)) pos = nx.layout.circular_layout(G) node_sizes = [10 for i in range(len(G))] M = G.number_of_edges() #edge_colors = range(2, M + 2) #edge_alphas = [(5 + i) / (M + 4) for i in range(M)] #edge_colors = [2 for i in range(len(G))] nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color='blue') edges = nx.draw_networkx_edges(G, pos, node_size=node_sizes, arrowstyle='->', arrowsize=20, edge_cmap=plt.cm.Blues, width=2) labels = ['$z_{}$'.format(i) for i in range(cols) ] + ['$a_{}$'.format(i) for i in range(rows - cols)] labels = {i: labels[i] for i in range(len(labels))} pos = {k: (v[0], v[1] + .1) for (k, v) in pos.items()} nx.draw_networkx_labels(G, pos, labels, font_size=16) # set alpha value for each edge for i in range(M): edges[i].set_alpha(edge_alphas[i]) ax = plt.gca() ax.set_axis_off() plt.show() return imutil.show(plt, return_pixels=True, display=False, save=False)
def generate_comparison(networks, dataloader, **options): # ISSUE: Unexpected BatchNorm behavior causes bad output if .eval() is set """ for net in networks: networks[net].eval() """ netG = networks['generator'] netD = networks['discriminator'] result_dir = options['result_dir'] image_size = options['image_size'] latent_size = options['latent_size'] output_frame_count = options['counterfactual_frame_count'] speed = options['speed'] momentum_mu = options['momentum_mu'] max_iters = options['counterfactual_max_iters'] result_dir = options['result_dir'] K = dataloader.num_classes batches = [] for class_idx in range(K): img_batch = generate_images_for_class(networks, dataloader, class_idx, **options) batches.append(img_batch) images = [] for i in range(K): for batch in batches: images.append(batch[i]) images = np.array(images).transpose((0,2,3,1)) dummy_class = 0 video_filename = make_video_filename(result_dir, dataloader, dummy_class, dummy_class, label_type='grid') # Save the images in npy format to re-load as training data trajectory_filename = video_filename.replace('.mjpeg', '.npy') np.save(trajectory_filename, images) # Save the images in jpg format to display to the user imutil.show(images, filename=video_filename.replace('.mjpeg', '.jpg'))
def compute_causal_graph(encoder, transition, datasource, iter=0): # Max over 10 runs weights_runs = [] for i in range(10): src_z, onehot_a = sample_transition(encoder, transition, datasource) causal_edge_weights = compute_causal_edge_weights( src_z, transition, onehot_a) weights_runs.append(causal_edge_weights) causal_edge_weights = np.max(weights_runs, axis=0) imutil.show(causal_edge_weights, resize_to=(256, 256), filename='causal_matrix_iter_{:06d}.png'.format(iter)) latent_dim = src_z.shape[1] print('Causal Graph Edge Weights') print('Latent Factor -> Latent Factor dim={}'.format(latent_dim)) for i in range(causal_edge_weights.shape[0]): for j in range(causal_edge_weights.shape[1]): print('{:.03f}\t'.format(causal_edge_weights[i, j]), end='') print('') graph_img = render_causal_graph(causal_edge_weights) imutil.show(graph_img, filename='causal_graph_iter_{:06d}.png'.format(iter))