ProGamerGov / neural-style-pt

PyTorch implementation of neural style transfer algorithm
MIT License
833 stars 178 forks source link

How we can call it from code? #56

Open skanel opened 4 years ago

skanel commented 4 years ago

How we can call it from code like call a function to generate style image?

momo-the-monster commented 4 years ago

Here's my suggestion on how to make this function as-is as well as from code. Edit: Now encapsulated in this pull request

in neural_style.py, rename the 'main' function to 'transfer', and have it accept a params object so it looks like this: def transfer(params):

then create another function to replace main like this:

def main():
    transfer(params)

To construct this object, we'll add a Class for all of the known params:

class TransferParams():
    style_image = 'examples/inputs/seated-nude.jpg'
    style_blend_weights = None
    content_image = 'examples/inputs/tubingen.jpg'
    image_size = 512
    gpu = 0
    content_weight = 5e0
    style_weight = 1e2
    normalize_weights = False
    tv_weight = 1e-3
    num_iterations = 1000
    init = 'random'
    init_image = None
    optimizer = 'lbfgs'
    learning_rate = 1e0
    lbfgs_num_correction = 100
    print_iter = 50
    save_iter = 100
    output_image = 'out.png'
    log_level = 10
    style_scale = 1.0
    original_colors = 0
    pooling = 'max'
    model_file = 'models/vgg19-d01eb7cb.pth'
    disable_check = False
    backend = 'nn'
    cudnn_autotune = False
    seed = -1
    content_layers = 'relu4_2'
    style_layers = 'relu1_1,relu2_1,relu3_1,relu4_1,relu5_1'
    multidevice_strategy = '4,7,29'

Finally, you can now use it in another file like this:

import neural_style

params = neural_style.TransferParams()
params.gpu = 0
params.backend = 'cudnn'
params.image_size = 128
neural_style.transfer(params)

I've got a project where it would be handy to have a Style Transfer web server, so this is how I'm implementing it.

owos commented 1 year ago

Hey are you done building already? I would love to see your webserver @momo-the-monster