nagadomi / waifu2x

Image Super-Resolution for Anime-Style Art
http://waifu2x.udp.jp/
MIT License
27.44k stars 2.71k forks source link

Problems calling from anywhere else except the location of waifu2x.lua #175

Open mxmlnkn opened 7 years ago

mxmlnkn commented 7 years ago

So I would want to be able to call waifu2x from arbitrary locations, but when trying:

th waifu2x.lua -i image.png -o upscaled.png

e.g. in my home directory, I get:

/opt/torch/install/bin/luajit: cannot open <./models/upconv_7/art/noise1_model.t7> in mode r  at /opt/torch/pkg/torch/lib/TH/THDiskFile.c:670
stack traceback:
    [C]: at 0x7ffbb1fbffe0
    [C]: in function 'DiskFile'
    /opt/torch/install/share/lua/5.1/torch/File.lua:405: in function 'load'
    /opt/waifu2x/lib/w2nn.lua:23: in function 'load_model'
    /opt/waifu2x/waifu2x.lua:106: in function 'convert_image'
    /opt/waifu2x/waifu2x.lua:291: in function 'waifu2x'
    /opt/waifu2x/waifu2x.lua:296: in main chunk
    [C]: in function 'dofile'
    /opt/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:150: in main chunk
    [C]: at 0x00405c80

It seems like waifu2x searches for libraries relative to the calling path, not the binary path. Currently I'm using the following script as a hack for this:

waifu2x(){
    # find -i and -o arguments and replace possibly relative paths
    # with absolute paths, because waifu2x can't be called outside from its
    # directory
    local args
    args=()
    while [ -n "$1" ]; do
        case "$1" in
            '-i')
                if [ -f "$2" ] && [ ! "${2:0:1}" = "/" ]; then
                    args+=( "$1" "$(pwd)/$2" )
                    shift 2
                    continue
                fi
                ;;
            '-o')
                local ext=${2##*.}
                if [ ! "${2:0:1}" = "/" ]; then
                    local fname=$2
                    if [ "${ext,,}" != 'png' ]; then
                        fname=${fname%.*}.png
                    fi
                    args+=( "$1" "$(pwd)/$fname" )
                    shift 2
                    continue
                fi
                ;;
        esac
        args+=( "$1" )
        shift
    done
    ( cd /opt/waifu2x/ && th waifu2x.lua "${args[@]}" )
}
nagadomi commented 7 years ago

Use -model_dir option.

# in .zshrc or something
WAIFU2X_DIR=~/dev/waifu2x
waifu2x()
{
    th ${WAIFU2X_DIR}/waifu2x.lua -model_dir ${WAIFU2X_DIR}/models/anime_style_art_rgb $@
}
waifu2x_photo()
{
    th ${WAIFU2X_DIR}/waifu2x.lua -model_dir ${WAIFU2X_DIR}/models/photo $@
}

Example

waifu2x -i in.png -o out.png
waifu2x_photo -m noise -noise_level 3 -i in.png -o out.png
mxmlnkn commented 7 years ago

Ah, thank you, that works.