jcjohnson / neural-style

Torch implementation of neural style algorithm
MIT License
18.31k stars 2.7k forks source link

Script to generate larger image #337

Open 0000sir opened 8 years ago

0000sir commented 8 years ago

Following the instructions from https://github.com/jcjohnson/neural-style/wiki/Techniques-For-Increasing-Image-Quality-Without-Buying-a-Better-GPU , I wrote a script to generate larger image file ( 3000px * 3000px in this case )

Checkout https://github.com/0000sir/larger-neural-style for original files or copy script below as bigbrush.sh

Usage: bigbrush.sh input_file.jpg style_file.jpg

It works for me, wish it's useful to you.

#! /bin/bash

main(){
    #1. defines
    input=$1
    input_file=`basename $input`
    clean_name="${input_file%.*}"

    style=$2
    style_dir=`dirname $style`
    style_file=`basename $style`
    style_name="${style_file%.*}"

    output="./output"
    out_file=$output/$input_file

    #2. neural-style the original input
    if [ ! -s $out_file ] ; then
        neural_style $input $style $out_file
    fi
  #convert $input -resize 1000x1000 $out_file #juse resize

    w1=`convert $out_file -format "%w" info:`
    h1=`convert $out_file -format "%h" info:`

    #3. tile it
    out_dir=$output/$clean_name
    mkdir -p $out_dir
    convert $out_file -crop 3x3+20+20@ +repage +adjoin $out_dir/$clean_name"_%d.jpg"
    # tile style
    #convert $style -crop 3x3+20+20@ +repage +adjoin $style_dir/$style_name"_%d.jpg"

    w2=`convert $out_dir/$clean_name'_0.jpg' -format "%w" info:`
    h2=`convert $out_dir/$clean_name'_0.jpg' -format "%h" info:`
    w_percent=`echo 20 $w2 | awk '{print $1/$2}'`
    h_percent=`echo 20 $h2 | awk '{print $1/$2}'`

    border_w=`echo $w1 $w_percent | awk '{print $1*$2}'`
    border_h=`echo $h1 $h_percent | awk '{print $1*$2}'`

    #4. neural-style each tile
    tiles_dir="$out_dir/tiles"
    mkdir -p $tiles_dir
    for tile in `ls $out_dir | grep $clean_name"_"[0-9]".jpg"`
    do
        #for i in $( seq 0 8 ); do
        #   neural_style $out_dir/$clean_name"_$i.jpg" $style_dir/$style_name"_$i.jpg" $tiles_dir/$clean_name"_$i.jpg"
        #done
        neural_style $out_dir/$tile $style $tiles_dir/$tile
    done

    #5. feather tiles
    feathered_dir=$out_dir/feathered
    mkdir -p $feathered_dir
    for tile in `ls $tiles_dir | grep $clean_name"_"[0-9]".jpg"`
    do
        tile_name="${tile%.*}"
        convert $tiles_dir/$tile -alpha set -virtual-pixel transparent -channel A -morphology Distance Euclidean:1,50\! +channel "$feathered_dir/$tile_name.png"
    done

    #6. merge feathered tiles
    montage $feathered_dir/$clean_name'_0.png' $feathered_dir/$clean_name'_1.png' \
                    $feathered_dir/$clean_name'_2.png' $feathered_dir/$clean_name'_3.png' \
                    $feathered_dir/$clean_name'_4.png' $feathered_dir/$clean_name'_5.png' \
                    $feathered_dir/$clean_name'_6.png' $feathered_dir/$clean_name'_7.png' \
                    $feathered_dir/$clean_name'_8.png'  -tile 3x3 -geometry -$border_w-$border_h $output/$clean_name.large.jpg
}
retry=0
neural_style(){
    echo "Neural Style Transfering "$1
    if [ ! -s $3 ]; then
        th neural_style.lua -content_image $1 -style_image $2 -output_image $3 \
                -image_size 1000 -print_iter 100 -backend cudnn -gpu 0 -save_iter 0 \
                -style_weight 20 -num_iterations 500
                #-original_colors 1
    fi
    if [ ! -s $3 ] && [ $retry -lt 3 ] ;then
            echo "Transfer Failed, Retrying for $retry time(s)"
            retry=`echo 1 $retry | awk '{print $1+$2}'`
            neural_style $1 $2 $3
    fi
    retry=0
}

main $1 $2 $3
ProGamerGov commented 8 years ago

I and many others have tried to create such a script, but failed at the overlapping crops, and feathering. It can get really tedious doing it all manually. Thank you for sharing the script with us!

ProGamerGov commented 8 years ago

@0000sir Testing out the script, does it only take your style and content image as inputs, or can the rest of the Neural-Style parameters also used?

htoyryla commented 8 years ago

ProGamerGov notifications@github.com kirjoitti 15.10.2016 kello 20.43:

@0000sir https://github.com/0000sir Testing out the script, does it only take your style and content image as inputs, or can the rest of the Neural-Style parameters also used?

I tried it today, had to go edit the other parameters in the script file, the neural style command is towards the end.

ProGamerGov commented 8 years ago

@htoyryla

Yep, they are at the second last section of the script:

neural_style(){
    echo "Neural Style Transfering "$1
    if [ ! -s $3 ]; then
        th neural_style.lua -content_image $1 -style_image $2 -output_image $3 \
                -image_size 1000 -print_iter 100 -backend cudnn -gpu 0 -save_iter 0 \
                -style_weight 20 -num_iterations 500
                #-original_colors 1

Also, how well did the script work for you @htoyryla?

0000sir commented 8 years ago

@ProGamerGov It's a bit busy these days, just like @htoyryla said, neural-style command is located at the end of script. I'm sure it works, I generated over 30 images with this script. Thank you @htoyryla 👍

ttoinou commented 8 years ago

Do you have example output ? "Feathering" is just blending colors ?