summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxAlpharax <42233094+xAlpharax@users.noreply.github.com>2023-02-27 22:44:09 +0200
committerxAlpharax <42233094+xAlpharax@users.noreply.github.com>2023-02-27 22:44:09 +0200
commitbd19b46d7544ff94be0e8eaccb4e1bd18d44b996 (patch)
tree4ca021df24e00b9b8c4a219fa4dc8fea3f8d989a
parent6fe8a3228e3170600b68d9b19e3b8f07e51222fe (diff)
Multi-threaded renderer implemented successfully. Better error handling. Minor changes in multiple areas
-rw-r--r--Images/Azzalee.jpgbin233885 -> 231884 bytes
-rw-r--r--[-rwxr-xr-x]Images/Jitter_Doll.jpgbin288950 -> 323415 bytes
-rw-r--r--Images/Monet.jpgbin8412856 -> 9424899 bytes
-rw-r--r--Images/Starry_Night.jpgbin2684897 -> 2114264 bytes
-rw-r--r--Images/bunnies.jpgbin110654 -> 104028 bytes
-rw-r--r--Images/colorful.jpgbin296376 -> 277561 bytes
-rw-r--r--[-rwxr-xr-x]Images/cute.jpgbin48794 -> 37477 bytes
-rw-r--r--[-rwxr-xr-x]Images/demon.jpgbin145801 -> 151759 bytes
-rw-r--r--[-rwxr-xr-x]Images/kanade.pngbin251615 -> 255323 bytes
-rw-r--r--[-rwxr-xr-x]Images/kek.pngbin25833 -> 25727 bytes
-rw-r--r--README.md14
-rwxr-xr-xclear_dir.sh4
-rw-r--r--preview/Starry_Night_in_Jitter_Doll.mp4bin298125 -> 0 bytes
-rw-r--r--preview/bunnies_in_Azzalee.mp4bin324140 -> 1861866 bytes
-rw-r--r--preview/bunnies_in_Jitter_Doll.mp4bin0 -> 1479507 bytes
-rw-r--r--preview/colorful_in_Azzalee.mp4bin309791 -> 0 bytes
-rw-r--r--preview/cute_in_Azzalee.mp4bin0 -> 1871323 bytes
-rw-r--r--preview/cute_in_Jitter_Doll.mp4bin1484421 -> 1518058 bytes
-rw-r--r--renderer.py43
-rw-r--r--requirements.txt0
-rwxr-xr-xstylize.sh26
21 files changed, 64 insertions, 23 deletions
diff --git a/Images/Azzalee.jpg b/Images/Azzalee.jpg
index 355c3c2..d7a1283 100644
--- a/Images/Azzalee.jpg
+++ b/Images/Azzalee.jpg
Binary files differ
diff --git a/Images/Jitter_Doll.jpg b/Images/Jitter_Doll.jpg
index 644f8b7..4f772c3 100755..100644
--- a/Images/Jitter_Doll.jpg
+++ b/Images/Jitter_Doll.jpg
Binary files differ
diff --git a/Images/Monet.jpg b/Images/Monet.jpg
index d9a560a..5bd061e 100644
--- a/Images/Monet.jpg
+++ b/Images/Monet.jpg
Binary files differ
diff --git a/Images/Starry_Night.jpg b/Images/Starry_Night.jpg
index 873b050..3f76d19 100644
--- a/Images/Starry_Night.jpg
+++ b/Images/Starry_Night.jpg
Binary files differ
diff --git a/Images/bunnies.jpg b/Images/bunnies.jpg
index 0ba2238..aa39762 100644
--- a/Images/bunnies.jpg
+++ b/Images/bunnies.jpg
Binary files differ
diff --git a/Images/colorful.jpg b/Images/colorful.jpg
index a4b5cf6..9c4a499 100644
--- a/Images/colorful.jpg
+++ b/Images/colorful.jpg
Binary files differ
diff --git a/Images/cute.jpg b/Images/cute.jpg
index d039de4..a262f6d 100755..100644
--- a/Images/cute.jpg
+++ b/Images/cute.jpg
Binary files differ
diff --git a/Images/demon.jpg b/Images/demon.jpg
index 84aba91..b52ece4 100755..100644
--- a/Images/demon.jpg
+++ b/Images/demon.jpg
Binary files differ
diff --git a/Images/kanade.png b/Images/kanade.png
index 9ce09aa..9b069e4 100755..100644
--- a/Images/kanade.png
+++ b/Images/kanade.png
Binary files differ
diff --git a/Images/kek.png b/Images/kek.png
index 6234f73..7eb5d9c 100755..100644
--- a/Images/kek.png
+++ b/Images/kek.png
Binary files differ
diff --git a/README.md b/README.md
index 3a2e442..7d85cf0 100644
--- a/README.md
+++ b/README.md
@@ -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
deleted file mode 100644
index bc75f35..0000000
--- a/preview/Starry_Night_in_Jitter_Doll.mp4
+++ /dev/null
Binary files differ
diff --git a/preview/bunnies_in_Azzalee.mp4 b/preview/bunnies_in_Azzalee.mp4
index 08974da..1cdf496 100644
--- a/preview/bunnies_in_Azzalee.mp4
+++ b/preview/bunnies_in_Azzalee.mp4
Binary files differ
diff --git a/preview/bunnies_in_Jitter_Doll.mp4 b/preview/bunnies_in_Jitter_Doll.mp4
new file mode 100644
index 0000000..0ad194c
--- /dev/null
+++ b/preview/bunnies_in_Jitter_Doll.mp4
Binary files differ
diff --git a/preview/colorful_in_Azzalee.mp4 b/preview/colorful_in_Azzalee.mp4
deleted file mode 100644
index 8b78d66..0000000
--- a/preview/colorful_in_Azzalee.mp4
+++ /dev/null
Binary files differ
diff --git a/preview/cute_in_Azzalee.mp4 b/preview/cute_in_Azzalee.mp4
new file mode 100644
index 0000000..18f2780
--- /dev/null
+++ b/preview/cute_in_Azzalee.mp4
Binary files differ
diff --git a/preview/cute_in_Jitter_Doll.mp4 b/preview/cute_in_Jitter_Doll.mp4
index 897b989..7eed718 100644
--- a/preview/cute_in_Jitter_Doll.mp4
+++ b/preview/cute_in_Jitter_Doll.mp4
Binary files differ
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
diff --git a/stylize.sh b/stylize.sh
index 434c079..ac9beba 100755
--- a/stylize.sh
+++ b/stylize.sh
@@ -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'