Skip to content

Commit

Permalink
Added Ability to Create GIFs
Browse files Browse the repository at this point in the history
  • Loading branch information
ProGamerGov committed Mar 22, 2020
1 parent 2b09596 commit a30f2a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ path or a full absolute path.
* `-use_fft`: Whether to enable Fast Fourier transform (FFT) decorrelation.
* `-fft_block`: The size of your FFT frequency filtering block. Default is `25`.

**GIF Options:**
* `-create_gif`: Whether to create a GIF from the output images after all iterations have been completed.
* `-frame_duration`: The duration for each GIF frame in milliseconds. Default is `100`.

**Help Options:**
* `-print_layers`: Pass this flag to print the names of all usable layers for the selected model.
* `-print_channels`: Pass this flag to print all the selected channels.
Expand Down Expand Up @@ -234,4 +238,4 @@ For example in a server with four GPUs, you can give the flag `-gpu 0,1,2,3` to
We can achieve very high quality results at high resolution by combining multi-GPU processing with multiscale
generation as described in the paper
<a href="https://arxiv.org/abs/1611.07865">**Controlling Perceptual Factors in Neural Style Transfer**</a> by Leon A. Gatys,
Alexander S. Ecker, Matthias Bethge, Aaron Hertzmann and Eli Shechtman.
Alexander S. Ecker, Matthias Bethge, Aaron Hertzmann and Eli Shechtman.
10 changes: 8 additions & 2 deletions neural_dream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import copy
import math
import random
import torch
import torch.nn as nn
Expand Down Expand Up @@ -67,6 +66,10 @@
parser.add_argument("-zoom", type=int, default=0)
parser.add_argument("-zoom_mode", choices=['percent', 'pixel'], default='percent')

# Gif options
parser.add_argument("-create_gif", action='store_true')
parser.add_argument("-frame_duration", type=int, default=100)

# Other options
parser.add_argument("-original_colors", type=int, choices=[0, 1], default=0)
parser.add_argument("-pooling", choices=['avg', 'max'], default='max')
Expand Down Expand Up @@ -296,6 +299,9 @@ def save_output(t, save_img, content_image, iter_name, model_mean):

disp.save(str(filename))

if t == params.num_iterations and params.create_gif:
dream_image.create_gif(output_filename, params.frame_duration)


def maybe_save(t, save_img, content_image, input_mean):
should_save = params.save_iter > 0 and t % params.save_iter == 0
Expand Down Expand Up @@ -579,4 +585,4 @@ def new_img(input_image, scale_factor, mode='bilinear'):


if __name__ == "__main__":
main()
main()
32 changes: 31 additions & 1 deletion neural_dream/dream_image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import torch
import torch.nn as nn
import neural_dream.dream_utils as dream_utils
from PIL import Image


# Adjust tensor contrast
Expand Down Expand Up @@ -31,4 +33,32 @@ def zoom(input, crop_val, mode='percent'):
h, w = input.size(2), input.size(3)
input = center_crop(input.clone(), crop_val, mode=mode)
input = resize_tensor(input, (h,w))
return input
return input


# Get most common Pillow Image size from list
def common_size(l, v):
l = [list(im.size)[v] for im in l]
return max(set(l), key = l.count)


# Create GIF from images
def create_gif(base_name, duration=100):
frames_dir, base_name = os.path.split(base_name)
if frames_dir == '':
frames_dir = '.'
base_name = base_name.rsplit('.', 1)[0]

ext = [".jpg", ".jpeg", ".png", ".tiff"]
image_list = [file for file in os.listdir(frames_dir) if os.path.splitext(file)[1].lower() in ext]
image_list = [im for im in image_list if base_name in im]
if "_" in image_list[0]:
fsorted = sorted(image_list,key=lambda x: int(os.path.splitext(x)[0].rsplit('_', 1)[1]))
else:
fsorted = sorted(image_list[1:],key=lambda x: int(os.path.splitext(x)[0].rsplit('_', 1)[1]))
fsorted.append(image_list[0])

frames = [Image.open(os.path.join(frames_dir, im)).convert('RGBA') for im in fsorted]
w, h = common_size(frames, 0), common_size(frames, 1)
frames = [im for im in frames if list(im.size)[0] == w and list(im.size)[1] == h]
frames[0].save(os.path.join(frames_dir, base_name+'.gif'), format='GIF', append_images=frames[1:], save_all=True, duration=duration, loop=0)

0 comments on commit a30f2a2

Please sign in to comment.