diff options
author | xAlpharax <42233094+xAlpharax@users.noreply.github.com> | 2023-02-27 22:44:09 +0200 |
---|---|---|
committer | xAlpharax <42233094+xAlpharax@users.noreply.github.com> | 2023-02-27 22:44:09 +0200 |
commit | bd19b46d7544ff94be0e8eaccb4e1bd18d44b996 (patch) | |
tree | 4ca021df24e00b9b8c4a219fa4dc8fea3f8d989a | |
parent | 6fe8a3228e3170600b68d9b19e3b8f07e51222fe (diff) |
Multi-threaded renderer implemented successfully. Better error handling. Minor changes in multiple areas
-rw-r--r-- | Images/Azzalee.jpg | bin | 233885 -> 231884 bytes | |||
-rw-r--r--[-rwxr-xr-x] | Images/Jitter_Doll.jpg | bin | 288950 -> 323415 bytes | |||
-rw-r--r-- | Images/Monet.jpg | bin | 8412856 -> 9424899 bytes | |||
-rw-r--r-- | Images/Starry_Night.jpg | bin | 2684897 -> 2114264 bytes | |||
-rw-r--r-- | Images/bunnies.jpg | bin | 110654 -> 104028 bytes | |||
-rw-r--r-- | Images/colorful.jpg | bin | 296376 -> 277561 bytes | |||
-rw-r--r--[-rwxr-xr-x] | Images/cute.jpg | bin | 48794 -> 37477 bytes | |||
-rw-r--r--[-rwxr-xr-x] | Images/demon.jpg | bin | 145801 -> 151759 bytes | |||
-rw-r--r--[-rwxr-xr-x] | Images/kanade.png | bin | 251615 -> 255323 bytes | |||
-rw-r--r--[-rwxr-xr-x] | Images/kek.png | bin | 25833 -> 25727 bytes | |||
-rw-r--r-- | README.md | 14 | ||||
-rwxr-xr-x | clear_dir.sh | 4 | ||||
-rw-r--r-- | preview/Starry_Night_in_Jitter_Doll.mp4 | bin | 298125 -> 0 bytes | |||
-rw-r--r-- | preview/bunnies_in_Azzalee.mp4 | bin | 324140 -> 1861866 bytes | |||
-rw-r--r-- | preview/bunnies_in_Jitter_Doll.mp4 | bin | 0 -> 1479507 bytes | |||
-rw-r--r-- | preview/colorful_in_Azzalee.mp4 | bin | 309791 -> 0 bytes | |||
-rw-r--r-- | preview/cute_in_Azzalee.mp4 | bin | 0 -> 1871323 bytes | |||
-rw-r--r-- | preview/cute_in_Jitter_Doll.mp4 | bin | 1484421 -> 1518058 bytes | |||
-rw-r--r-- | renderer.py | 43 | ||||
-rw-r--r-- | requirements.txt | 0 | ||||
-rwxr-xr-x | stylize.sh | 26 |
21 files changed, 64 insertions, 23 deletions
diff --git a/Images/Azzalee.jpg b/Images/Azzalee.jpg Binary files differindex 355c3c2..d7a1283 100644 --- a/Images/Azzalee.jpg +++ b/Images/Azzalee.jpg diff --git a/Images/Jitter_Doll.jpg b/Images/Jitter_Doll.jpg Binary files differindex 644f8b7..4f772c3 100755..100644 --- a/Images/Jitter_Doll.jpg +++ b/Images/Jitter_Doll.jpg diff --git a/Images/Monet.jpg b/Images/Monet.jpg Binary files differindex d9a560a..5bd061e 100644 --- a/Images/Monet.jpg +++ b/Images/Monet.jpg diff --git a/Images/Starry_Night.jpg b/Images/Starry_Night.jpg Binary files differindex 873b050..3f76d19 100644 --- a/Images/Starry_Night.jpg +++ b/Images/Starry_Night.jpg diff --git a/Images/bunnies.jpg b/Images/bunnies.jpg Binary files differindex 0ba2238..aa39762 100644 --- a/Images/bunnies.jpg +++ b/Images/bunnies.jpg diff --git a/Images/colorful.jpg b/Images/colorful.jpg Binary files differindex a4b5cf6..9c4a499 100644 --- a/Images/colorful.jpg +++ b/Images/colorful.jpg diff --git a/Images/cute.jpg b/Images/cute.jpg Binary files differindex d039de4..a262f6d 100755..100644 --- a/Images/cute.jpg +++ b/Images/cute.jpg diff --git a/Images/demon.jpg b/Images/demon.jpg Binary files differindex 84aba91..b52ece4 100755..100644 --- a/Images/demon.jpg +++ b/Images/demon.jpg diff --git a/Images/kanade.png b/Images/kanade.png Binary files differindex 9ce09aa..9b069e4 100755..100644 --- a/Images/kanade.png +++ b/Images/kanade.png diff --git a/Images/kek.png b/Images/kek.png Binary files differindex 6234f73..7eb5d9c 100755..100644 --- a/Images/kek.png +++ b/Images/kek.png @@ -1,15 +1,23 @@ # neural-art Neural Style Transfer done from the CLI using a VGG backbone and presented as an MP4. -Weights can be downloaded from [here](https://m1.afileditch.ch/ajjMsHrRhnikrrCiUXgY.pth). The downloaded file should be placed in `./weights/` and will be ignored when pushing as per `./.gitignore` +Weights can be downloaded from [here](https://m1.afileditch.ch/ajjMsHrRhnikrrCiUXgY.pth). The downloaded file should be placed in `./weights/` and will be ignored when pushing as seen in `./.gitignore` ### Why use this in 2023 ? -Because style transfer hasn't changed drastically in terms of actual results in the past years. I personally find a certain beauty in inputing an image with a desired style rather than a well curated prompt with a dozen of switches. Consider using this repo as a quick *just works* solution that can run on both CPU and GPU. +Because style transfer hasn't changed drastically in terms of actual results in the past years. I personally find a certain beauty in inputing a style and content image rather than a well curated prompt with a dozen of switches. Consider this repo as a quick *just works* solution that can run on both CPU and GPU effectively. ## Usage -The script sits comfortably in `./stylize.sh` so just run it from the project directory: +The script sits comfortably in `./stylize.sh` so just run it in the project directory: ```bash ./stylize.sh path/to/style_image path/to/content_image ``` + +### Requirements + +All requirements are specified in `./requirements.txt` as per python etiquette: + +```bash +pip install -r requirements.txt +``` diff --git a/clear_dir.sh b/clear_dir.sh index 57f891a..fef1764 100755 --- a/clear_dir.sh +++ b/clear_dir.sh @@ -1,7 +1,7 @@ -#!/bin/zsh +#!/bin/bash ### clearing up the directory ### from temporary files rm -rf __pycache__ # Output* future proof -rm -f Output/* *.npy +rm -f Output/*.png *.npy diff --git a/preview/Starry_Night_in_Jitter_Doll.mp4 b/preview/Starry_Night_in_Jitter_Doll.mp4 Binary files differdeleted file mode 100644 index bc75f35..0000000 --- a/preview/Starry_Night_in_Jitter_Doll.mp4 +++ /dev/null diff --git a/preview/bunnies_in_Azzalee.mp4 b/preview/bunnies_in_Azzalee.mp4 Binary files differindex 08974da..1cdf496 100644 --- a/preview/bunnies_in_Azzalee.mp4 +++ b/preview/bunnies_in_Azzalee.mp4 diff --git a/preview/bunnies_in_Jitter_Doll.mp4 b/preview/bunnies_in_Jitter_Doll.mp4 Binary files differnew file mode 100644 index 0000000..0ad194c --- /dev/null +++ b/preview/bunnies_in_Jitter_Doll.mp4 diff --git a/preview/colorful_in_Azzalee.mp4 b/preview/colorful_in_Azzalee.mp4 Binary files differdeleted file mode 100644 index 8b78d66..0000000 --- a/preview/colorful_in_Azzalee.mp4 +++ /dev/null diff --git a/preview/cute_in_Azzalee.mp4 b/preview/cute_in_Azzalee.mp4 Binary files differnew file mode 100644 index 0000000..18f2780 --- /dev/null +++ b/preview/cute_in_Azzalee.mp4 diff --git a/preview/cute_in_Jitter_Doll.mp4 b/preview/cute_in_Jitter_Doll.mp4 Binary files differindex 897b989..7eed718 100644 --- a/preview/cute_in_Jitter_Doll.mp4 +++ b/preview/cute_in_Jitter_Doll.mp4 diff --git a/renderer.py b/renderer.py index 7eaae9a..992b4ab 100644 --- a/renderer.py +++ b/renderer.py @@ -1,32 +1,55 @@ #!/bin/python -### very slow step that will be assigned to multiple -### jobs later in the development of this tool - ### data loading import numpy as np -image_array = np.load("images.npy", allow_pickle=True) +# will need some added functionality there for *.npy +image_array = np.load("images.npy") ### progress bar from tqdm import tqdm +import sys -pbar = tqdm(total = len(image_array)) +if len(sys.argv) == 1: + pbar = tqdm(total=len(image_array), miniters=0, smoothing=0) -### rendering of images +### image rendering from raw data import matplotlib.pyplot as plt +dpi = 258 # dpi 258 -> 720p ; dpi 387 -> 1080p output image resolution + def render(index): + pbar.update(1) + name = 'Output/neural_art_{:04d}.png'.format(index + 1) plt.axis('off') plt.imshow(image_array[index]) - plt.savefig(name, bbox_inches='tight', pad_inches=0) # dpi 258 -> 720p ; dpi 387 -> 1080p output image resolution + plt.savefig(name, dpi=dpi, bbox_inches='tight', pad_inches=0) plt.close('all') -for index in range(0, len(image_array)): - render(index) - pbar.update(1) +### multi-threading + +import os ; n_cores = os.cpu_count() // 2 ; + +if len(sys.argv) == 1: + from concurrent.futures import ThreadPoolExecutor + + with ThreadPoolExecutor(max_workers=n_cores) as executor: + executor.map(render, range(0, len(image_array))) + + exit() + +if sys.argv[1] == "--fix": + for index in range(0, 32): + ### some artifacts may have slipped + ### because of the thread pool + name = 'Output/neural_art_{:04d}.png'.format(index + 1) + + plt.axis('off') + plt.imshow(image_array[index]) + plt.savefig(name, dpi=dpi, bbox_inches='tight', pad_inches=0) + plt.close('all') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/requirements.txt @@ -1,25 +1,35 @@ -#!/bin/zsh +#!/bin/bash ### You must be in the 'neural-art' directory when you run this -if [ $# -eq 0 ] - then +cwd=$(pwd | sed -r 's%.*/%%g') +if [ $cwd != "neural-art" ] ; then + echo "You must be in the 'neural-art' directory when you run this" + exit 1 +fi + +if [ $# -eq 0 ] ; then echo "Style Image and Content Image need to be specified as arguments" echo "Example: ./stylize.sh Images/Jitter_Doll.jpg Images/cute.jpg" exit 1 fi -if [ $# -eq 1 ] - then - echo "Content image needs to be specified" +if [ $# -eq 1 ] ; then + echo "Content image needs to be specified as well" + echo "Example: ./stylize.sh Images/Jitter_Doll.jpg Images/cute.jpg" exit 1 fi +./clear_dir.sh + # stylize data [pair (style, content)] python neuralart.py $1 $2 # render images (actual frames) from (an) images.npy -python renderer.py $1 $2 +python renderer.py + +# render fix +python renderer.py --fix # turn everything into a video -ffmpeg -framerate 60 -pattern_type glob -i 'Output/neural_art_*.png' -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" ${${2/*\/}/.*}_in_${${1/*\/}/.*}.mp4 +ffmpeg -framerate 60 -pattern_type glob -i 'Output/neural_art_*.png' -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" $(basename ${2%.*})'_in_'$(basename ${1%.*})'.mp4' |