def grid_placed(images, size=None): assert len(images.shape) == 4, f'images should be 4D, but get shape {images.shape}' B, H, W, C = images.shape if size is None: size = squarest_grid_size(B) image_type = images.dtype if (images.shape[3] in (3,4)): img = np.zeros((H * size[0], W * size[1], C), dtype=image_type) for idx, image in enumerate(images): i = idx % size[1] j = idx // size[1] img[j * H:j * H + H, i * W:i * W + W, :] = image if np.issubdtype(image_type, np.uint8): return img if np.min(img) < -.5: # for images in range [-1, 1], make it in range [0, 1] img = (img + 1) / 2 elif np.min(img) < 0: # for images in range [-.5, .5] img = img + .5 assert np.min(img) >= 0, np.min(img) assert np.max(img) <= 1, np.max(img) img = np.clip(255 * img, 0, 255).astype(np.uint8) return img elif images.shape[3]==1: img = np.zeros((H * size[0], W * size[1]), dtype=image_type) for idx, image in enumerate(images): i = idx % size[1] j = idx // size[1] img[j * H:j * H + H, i * W:i * W + W] = image[:,:,0] return img else: NotImplementedError
def video_summary(name, video, size=None, fps=30, step=None): name = name if isinstance(name, str) else name.decode('utf-8') if np.issubdtype(video.dtype, np.floating): video = np.clip(255 * video, 0, 255).astype(np.uint8) while len(video.shape) < 5: video = np.expand_dims(video, 0) B, T, H, W, C = video.shape if size is None and B != 1: bh, bw = squarest_grid_size(B) frames = video.reshape((bh, bw, T, H, W, C)) frames = frames.transpose((2, 0, 3, 1, 4, 5)) frames = frames.reshape((T, bh*H, bw*W, C)) else: if size is None: size = (1, 1) assert size[0] * size[1] == B, f'Size({size}) does not match the batch dimension({B})' frames = video.transpose((1, 2, 0, 3, 4)).reshape((T, size[0]*H, size[1]*W, C)) try: summary = tf1.Summary() image = tf1.Summary.Image(height=B * H, width=T * W, colorspace=C) image.encoded_image_string = encode_gif(frames, fps) summary.value.add(tag=name, image=image) tf.summary.experimental.write_raw_pb(summary.SerializeToString(), step) except (IOError, OSError) as e: print('GIF summaries require ffmpeg in $PATH.', e) frames = video.transpose((0, 2, 1, 3, 4)).reshape((1, B * H, T * W, C)) tf.summary.image(name + '/image', frames, step)
def save_image(images, path, size=None): assert images.shape.ndims == 4, f'images should be 4D, but get shape {images.shape}' num_images = images.shape[0] if size is None: size = utils.squarest_grid_size(num_images) images = grid_placed(images, size) utils.check_make_dir(path) imsave(path, images)
def save_image(images, path, size=None): assert_colorize( len(images.shape) == 4, f'images should be 4D, but get shape {images.shape}') num_images = images.shape[0] if size is None: size = utils.squarest_grid_size(num_images) images = merge(images, size) utils.check_make_dir(path) imsave(path, images)
def _log_train_info(self): num_images = min(self.batch_size, 16) image_shape = self.image_shape[:-1] image_grid = lambda vis_images: tfgan.eval.image_grid( vis_images[:num_images], grid_shape=squarest_grid_size(num_images), image_shape=image_shape) def image_stats(image): means = tf.reduce_mean(image, 0, keep_dims=True) vars = tf.reduce_mean(tf.squared_difference(image, means), 0, keep_dims=True) mean, var = tf.reduce_mean(means), tf.reduce_mean(vars) return mean, var if self.log_tensorboard: with tf.name_scope('train_info'): tf.summary.histogram('z_', self.generator.z) with tf.name_scope('loss'): tf.summary.scalar('generator_loss_', self.gen_loss) tf.summary.scalar('discriminator_loss_', self.dis_loss) tf.summary.scalar('loss_', self.gen_loss + self.dis_loss) with tf.name_scope('image'): tf.summary.image('generated_image_', image_grid(self.gen_image), max_outputs=1) tf.summary.histogram('generated_image_hist_', self.gen_image) gen_mean, gen_var = image_stats(self.gen_image) real_mean, real_var = image_stats(self.image) tf.summary.scalar('gen_mean_', gen_mean) tf.summary.scalar('gen_var_', gen_var) tf.summary.scalar('real_mean_', real_mean) tf.summary.scalar('real_var_', real_var) with tf.name_scope('prob'): tf.summary.histogram('real_prob_hist_', self.real_discriminator.prob) tf.summary.histogram('fake_prob_hist_', self.fake_discriminator.prob) tf.summary.scalar('real_prob_', tf.reduce_mean(self.real_discriminator.prob)) tf.summary.scalar('fake_prob_', tf.reduce_mean(self.fake_discriminator.prob)) with tf.name_scope('logit'): tf.summary.histogram('real_logits_hist_', self.real_discriminator.logits) tf.summary.histogram('fake_logits_hist_', self.fake_discriminator.logits) tf.summary.histogram('real_logits_', tf.reduce_mean(self.real_discriminator.logits)) tf.summary.histogram('fake_logits_', tf.reduce_mean(self.fake_discriminator.logits))
def save_video(name, video, fps=30): name = name if isinstance(name, str) else name.decode('utf-8') video = np.array(video, copy=False) if np.issubdtype(video.dtype, np.floating): video = np.clip(255 * video, 0, 255).astype(np.uint8) while len(video.shape) < 5: video = np.expand_dims(video, 0) B, T, H, W, C = video.shape if B != 1: bh, bw = squarest_grid_size(B) frames = video.reshape((bh, bw, T, H, W, C)) frames = frames.transpose((2, 0, 3, 1, 4, 5)) frames = frames.reshape((T, bh*H, bw*W, C)) else: frames = video.transpose((1, 2, 0, 3, 4)).reshape((T, H, W, C)) f1, *frames = [Image.fromarray(f) for f in frames] if not os.path.isdir('results'): os.mkdir('results') path = f'results/{name}.gif' f1.save(fp=path, format='GIF', append_images=frames, save_all=True, duration=1000//fps, loop=0) print(f"video is saved to '{path}'")