crowsonkb / style-transfer-pytorch

Neural style transfer in PyTorch.
MIT License
475 stars 113 forks source link

It is not running on GPU in google colab :/ #1

Closed aertist closed 3 years ago

aertist commented 3 years ago

Hi, awesome repo. Thanks for sharing. It throws this error every time I try to run this on GPU.

Traceback (most recent call last): File "/usr/local/bin/style_transfer", line 33, in <module> sys.exit(load_entry_point('style-transfer-pytorch', 'console_scripts', 'style_transfer')()) File "/content/style-transfer-pytorch/style_transfer/cli.py", line 213, in main props = torch.cuda.get_device_properties(device) File "/usr/local/lib/python3.6/dist-packages/torch/cuda/__init__.py", line 296, in get_device_properties _lazy_init() # will define _get_device_properties File "/usr/local/lib/python3.6/dist-packages/torch/cuda/__init__.py", line 172, in _lazy_init torch._C._cuda_init() RuntimeError: The NVIDIA driver on your system is too old (found version 10010). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver.

So I'm forced to run it on the CPU instead and which takes forever. 😪 Can you make it compatible with the old drivers? 😅

I made this [colab] notebook. You can reproduce the error yourself.

crowsonkb commented 3 years ago

On Colab I've found you have to install a version of PyTorch compiled for CUDA 10.1 or earlier. You can do this by running the following command:

!pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

Does this help?

aertist commented 3 years ago

On Colab I've found you have to install a version of PyTorch compiled for CUDA 10.1 or earlier. You can do this by running the following command:

!pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

Does this help?

Thanks man it is working now. Can you like add more parameters to play with? Like these one> def stylize(self, content_image, style_images, *, style_weights=None, content_weight: float = 0.01, tv_weight: float = 0.3, min_scale: int = 128, end_scale: int = 1920, iterations: int = 300, initial_iterations: int = 700, step_size: float = 0.02, avg_decay: float = 0.99, init: str = 'content', style_scale_fac: float = 1., style_size: int = None, callback=None):

crowsonkb commented 3 years ago

Thanks man it is working now. Can you like add more parameters to play with? Like these one> def stylize(self, content_image, style_images, *, style_weights=None, content_weight: float = 0.01, tv_weight: float = 0.3, min_scale: int = 128, end_scale: int = 1920, iterations: int = 300, initial_iterations: int = 700, step_size: float = 0.02, avg_decay: float = 0.99, init: str = 'content', style_scale_fac: float = 1., style_size: int = None, callback=None):

All of the parameters that have type annotations are available from the command line, try --help to find out their names and defaults. 🙂

aertist commented 3 years ago

Damn my bad. 😅 Can you explain why this is so accurate in transferring the style? I have tried 3 different implementation. And this by far gives most accurate and beautiful results. (It is a bit slow. Any specific reason?)

aertist commented 3 years ago

I was messing around with this. Here are some results. Absolutely stunning.

Input: example

Style Image: style2

Output: output

Parameters I used: -ms 256 \ -s 1280 \ -i 100 \ -ii 100 \ -ss 0.02 \ -ad 0.99 \ -sw 50 \ -cw 0.01 \ -tw 0.3 \ --pooling max \ --save-every 50 \ --device cuda:0 \

crowsonkb commented 3 years ago

Can you explain why this is so accurate in transferring the style? I have tried 3 different implementation. And this by far gives most accurate and beautiful results. (It is a bit slow. Any specific reason?)

the other two implementations are this(tensorflow) and this(pytorch)

I took a look at these and they aren't even doing automatic multi-scale style transfer. (It was proposed in "Controlling Perceptual Factors in Neural Style Transfer" but I discovered it independently in 2016 before this paper was published.) My code starts at an image size of 128 pixels by default and, once it is done, re-stylizes the result at a larger scale. The scales go up by a factor of sqrt(2), so 128, 181, 256, 362, 512, 724, 1024... This is better than the scheme given in the paper which only uses two scales. Anyway, this is necessary for good quality at resolutions over about 512x512. My code is probably slower due to doing so many iterations at different scales (1000 on the first, 500 on all after that.)

There are other improvements I've made to the basic algorithm, but this is probably the main difference from those two repos.

aertist commented 3 years ago

Can you explain why this is so accurate in transferring the style? I have tried 3 different implementation. And this by far gives most accurate and beautiful results. (It is a bit slow. Any specific reason?) the other two implementations are this(tensorflow) and this(pytorch)

I took a look at these and they aren't even doing automatic multi-scale style transfer. (It was proposed in "Controlling Perceptual Factors in Neural Style Transfer" but I discovered it independently in 2016 before this paper was published.) My code starts at an image size of 128 pixels by default and, once it is done, re-stylizes the result at a larger scale. The scales go up by a factor of sqrt(2), so 128, 181, 256, 362, 512, 724, 1024... This is better than the scheme given in the paper which only uses two scales. Anyway, this is necessary for good quality at resolutions over about 512x512. My code is probably slower due to doing so many iterations at different scales (1000 on the first, 500 on all after that.)

There are other improvements I've made to the basic algorithm, but this is probably the main difference from those two repos.

wow that's interesting. I'm a newbie and still learning all these stuff related to AI x Art.

Can you tell me which parameters are the best for an 1920x1080 image? Like which pooling type and content and style wt. values?

crowsonkb commented 3 years ago

Can you tell me which parameters are the best for an 1920x1080 image? Like which pooling type and content and style wt. values?

The values of the parameters are supposed to be independent of what size the output image is. But sometimes when producing large images I set the minimum scale (-ms) to 256 or thereabouts.

aertist commented 3 years ago

Can you tell me which parameters are the best for an 1920x1080 image? Like which pooling type and content and style wt. values?

The values of the parameters are supposed to be independent of what size the output image is. But sometimes when producing large images I set the minimum scale (-ms) to 256 or thereabouts.

Can we use multiple style images at once? (with making a collage of style images) Also do you know about this site ? I think it is using a same style transfer code.

crowsonkb commented 3 years ago

Can we use multiple style images at once? (with making a collage of style images)

You can use multiple style images (just list the additional ones after the first one) and it will create a blended style from them and apply it to the content. You can also use the style weights parameter (-sw) to specify the ratios of the style images: for instance, if you have three style images, -sw 1 2 1 will use them in a 1:2:1 ratio.