class Visualization(object): """Helper class to visualize the progress of the GAN training procedure. """ def __init__(self, file_name, model_name, fps=15): """Initialize the helper class. :param fps: The number of frames per second when saving the gif animation. """ self.fps = fps self.figure, (self.ax2) = plt.subplots(1, 1, figsize=(5, 5)) self.figure.suptitle("{}".format(model_name)) sns.set(color_codes=True, style='white', palette='colorblind') sns.despine(self.figure) plt.show(block=False) self.real_data = Dataset() self.step = 0 self.writer = ImageMagickWriter(fps=self.fps) self.writer.setup(self.figure, file_name, dpi=100) def plot_progress(self, gen_net): """Plot the progress of the training procedure. This can be called back from the GAN fit method. :param gan: The GAN we are fitting. :param session: The current session of the GAN. :param data: The data object from which we are sampling the input data. """ real = self.real_data.next_batch(batch_size=10000) r1, r2 = self.ax2.get_xlim() x = np.linspace(r1, r2, 10000)[:, np.newaxis] g = gen_net(torch.FloatTensor(np.random.randn(10000, 1))) self.ax2.clear() self.ax2.set_ylim([0, 1]) self.ax2.set_xlim([0, 8]) sns.kdeplot(real.numpy().flatten(), shade=True, ax=self.ax2, label='Real data') sns.kdeplot(g.detach().numpy().flatten(), shade=True, ax=self.ax2, label='Generated data') self.ax2.set_title('Distributions') self.ax2.set_title('{} iterations'.format(self.step * 50)) self.ax2.set_xlabel('Input domain') self.ax2.set_ylabel('Probability density') self.ax2.legend(loc='upper left', frameon=True) self.figure.canvas.draw() self.figure.canvas.flush_events() self.writer.grab_frame() self.step += 1
class Visualization(object): """Helper class to visualize the progress of the GAN training procedure. """ def __init__(self, save_animation=False, fps=30): """Initialize the helper class. :param save_animation: Whether the animation should be saved as a gif. Requires the ImageMagick library. :param fps: The number of frames per second when saving the gif animation. """ self.save_animation = save_animation self.fps = fps self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2, figsize=(8, 4)) self.figure.suptitle("1D GAAN") sns.set(color_codes=True, style='white', palette='colorblind') sns.despine(self.figure) plt.show(block=False) if self.save_animation: self.writer = ImageMagickWriter(fps=self.fps) self.writer.setup(self.figure, 'Toy_1D_GAAN.gif', dpi=100) def plot_progress(self, gan, session, data): """Plot the progress of the training procedure. This can be called back from the GAN fit method. :param gan: The GAN we are fitting. :param session: The current session of the GAN. :param data: The data object from which we are sampling the input data. """ # Plot the training curve. steps = gan.log_interval * np.arange(len(gan.loss_d_curve)) self.ax1.clear() self.ax1.plot(steps, gan.loss_d_curve, label='D loss') self.ax1.plot(steps, gan.loss_g_curve, label='G loss') self.ax1.set_title('Learning curve') self.ax1.set_xlabel('Iteration') if gan.model == 'gan' or gan.model == 'rgan' or gan.model == 'mdgan' or gan.model == 'vaegan' or gan.model == 'gaan': title = 'Loss' elif gan.model == 'wgangp': title = 'Negative critic loss' self.ax1.set_ylabel(title) # Plot the generated and the input data distributions. g = gan.sample(session) r1, r2 = self.ax2.get_xlim() x = np.linspace(r1, r2, gan.n_sample)[:, np.newaxis] critic = gan.dreal(session, x) if gan.model == 'wgangp': # Normalize the critic to be in [0, 1] to make visualization easier. critic = (critic - critic.min()) / (critic.max() - critic.min()) d, _ = data.next_batch(gan.n_sample) if gan.model == 'gaan' or gan.model == 'rgan' or gan.model == 'mdgan': r = gan.reconstruct(session, d) e = gan.encode(session, d) self.ax2.clear() self.ax2.set_ylim([0, 1]) self.ax2.set_xlim([-8, 8]) if gan.model == 'gan' or gan.model == 'rgan' or gan.model == 'mdgan' or gan.model == 'gaan' or gan.model == 'vaegan': self.ax2.plot(x, critic, label='Decision boundary') elif gan.model == 'wgangp': self.ax2.plot(x, critic, label='Critic (normalized)') sns.kdeplot(d.flatten(), shade=True, ax=self.ax2, label='Real data') sns.kdeplot(g.flatten(), shade=True, ax=self.ax2, label='Generated data') self.ax2.set_title('Distributions') self.ax2.set_xlabel('Input domain') self.ax2.set_ylabel('Probability density') self.ax2.legend(loc='upper left', frameon=True) if len(steps) - 1 == gan.n_step // gan.log_interval: if self.save_animation: wait_seconds = 3 [ self.writer.grab_frame() for _ in range(wait_seconds * self.fps) ] self.writer.finish() plt.show() else: self.figure.canvas.draw() self.figure.canvas.flush_events() if self.save_animation: self.writer.grab_frame()
class Visualization(object): """Helper class to visualize the progress of the GAN training procedure. """ def __init__(self, save_animation=False, fps=30): """Initialize the helper class. :param save_animation: Whether the animation should be saved as a gif. Requires the ImageMagick library. :param fps: The number of frames per second when saving the gif animation. """ self.save_animation = save_animation self.fps = fps self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2, figsize=(8, 4)) self.figure.suptitle("1D GAAN") sns.set(color_codes=True, style='white', palette='colorblind') sns.despine(self.figure) plt.show(block=False) if self.save_animation: self.writer = ImageMagickWriter(fps=self.fps) self.writer.setup(self.figure, 'Toy_1D_GAAN.gif', dpi=100) def plot_progress(self, gan, session, data): """Plot the progress of the training procedure. This can be called back from the GAN fit method. :param gan: The GAN we are fitting. :param session: The current session of the GAN. :param data: The data object from which we are sampling the input data. """ # Plot the training curve. steps = gan.log_interval * np.arange(len(gan.loss_d_curve)) self.ax1.clear() self.ax1.plot(steps, gan.loss_d_curve, label='D loss') self.ax1.plot(steps, gan.loss_g_curve, label='G loss') self.ax1.set_title('Learning curve') self.ax1.set_xlabel('Iteration') if gan.model == 'gan' or gan.model == 'rgan' or gan.model == 'mdgan' or gan.model == 'vaegan' or gan.model == 'gaan': title = 'Loss' elif gan.model == 'wgangp': title = 'Negative critic loss' self.ax1.set_ylabel(title) # Plot the generated and the input data distributions. g = gan.sample(session) r1,r2 = self.ax2.get_xlim() x = np.linspace(r1, r2, gan.n_sample)[:, np.newaxis] critic = gan.dreal(session, x) if gan.model == 'wgangp': # Normalize the critic to be in [0, 1] to make visualization easier. critic = (critic - critic.min()) / (critic.max() - critic.min()) d, _ = data.next_batch(gan.n_sample) if gan.model == 'gaan' or gan.model == 'rgan' or gan.model == 'mdgan': r = gan.reconstruct(session, d) e = gan.encode(session, d) self.ax2.clear() self.ax2.set_ylim([0, 1]) self.ax2.set_xlim([-8, 8]) if gan.model == 'gan' or gan.model == 'rgan' or gan.model == 'mdgan' or gan.model == 'gaan' or gan.model == 'vaegan': self.ax2.plot(x, critic, label='Decision boundary') elif gan.model == 'wgangp': self.ax2.plot(x, critic, label='Critic (normalized)') sns.kdeplot(d.flatten(), shade=True, ax=self.ax2, label='Real data') sns.kdeplot(g.flatten(), shade=True, ax=self.ax2, label='Generated data') self.ax2.set_title('Distributions') self.ax2.set_xlabel('Input domain') self.ax2.set_ylabel('Probability density') self.ax2.legend(loc='upper left', frameon=True) if len(steps) - 1 == gan.n_step // gan.log_interval: if self.save_animation: wait_seconds = 3 [self.writer.grab_frame() for _ in range(wait_seconds * self.fps)] self.writer.finish() plt.show() else: self.figure.canvas.draw() self.figure.canvas.flush_events() if self.save_animation: self.writer.grab_frame()
# Load the model and an example model = LevelSetMachineLearning.load('./LSML-model.pkl') example = model.testing_data[13] # Set up plotting for the movie frames fig, ax = plt.subplots(1, 1, figsize=(2, 2)) ax.axis('off') ax.imshow(example.img, cmap=plt.cm.gray, interpolation='bilinear') lines = [] # Set up the movie writer and grab the frame at initialization writer = ImageMagickWriter(fps=5) writer.setup(fig, 'evolution.gif', 100) writer.grab_frame() # Define the callback function to be used during segmentation evolution def update_movie(i, u): if i % 10 != 0: return for line in lines: line.remove() lines.clear() lines.extend( plot_iso_contours(ax, u, value=0, c='b')