Using deep learning with style transfer
This is Part 3 of 3 in a tutorial series for getting started with GPU-driven deep learning. These tutorials are intended for anyone who is interested in figuring out what it takes to get started with deep learning on a personal machine that costs less than $2000. This assumes you have some familiarity with machine learning and deep learning. This series will cover:
- Part 1: How to build a GPU deep learning machine
- Part 2: Setting up Ubuntu and Docker for deep learning
- Part 3: Using deep learning with style transfer
Examples of Style Transfer
docker imagesdisplays a list of Docker images on your machine
docker psdisplays a list of active Docker containers
docker rm -f <container_ID>shuts down an active container
- More commands
Installing the Style Transfer Docker Image
When you use Docker, any changes you make inside of a Docker container will be lost unless you take some action to preserve the changes. I recommend having a dedicated directory for Docker projects and for style transfer, specifically. When we launch our container, we can then create a link between certain directories in the Docker container to the project-specific directories we’ve created on our local machine. If we store any data in these directories when we’re running the container, then that data will be available after the container is shut down. We can also make data from our local machine accessible to the container by putting it in the linked folder.
Setting Up Project Directory Tree
- Open up a new terminal session.
- Because this is a freshly installed OS, I’m going to create a directory called “projects” in my user directory. I’ll type
- Create a directory for your docker projects. I’m going to create a directory called “docker_projects” inside of my “projects” folder. To do this, I’ll type
- Create a directory for our neural style project. I’ll type
- Create three new directories called “images”, “outputs”, and “models” inside of your style transfer project folder. I’ll type
- Navigate to the inside of your style_transfer directory. I’ll type
cd ~/projects/docker_projects/style_transfer/. This step is important for when we use
$PWDto copy the models and to attach the local folders to the container folders. Alternatively, you can define the file path.
- Open a new tab in terminal by typing ctrl+shift+t and switch to it.
Setting Up The Neural Style Docker Image
- Install the the Docker image:
docker pull danhigham/cuda-neural-style. This image is several GBs and could take a few minutes to download.
- Run the container:
nvidia-docker run -it danhigham/cuda-neural-style.
- Navigate to the models folder:
- Run the script to download the models:
- Switch to the other tab in terminal. Determine the container ID of the neural style container by typing
- Copy the contents of the “models” directory in the Docker container to the “models” directory in our local style transfer project folder by typing:
docker cp <container_ID>:/root/torch/neural-style/models/. $PWD. In my case,
cp b04d2f650ff4:/root/torch/neural-style/models/. $PWD. Now our models will persist after we close the container.
- Switch back to the terminal tab with the Docker session. Close the container by typing
exitand selecting enter.
From now on, when we want to open a style transfer container, we can use the following code when we’re inside of our style transfer project directory:
nvidia-docker run -it -v $PWD/images:/root/torch/neural-style/images -v $PWD/outputs:/root/torch/neural-style/outputs -v $PWD/models:/root/torch/neural-style/models danhigham/cuda-neural-style:latest
Neural Style Transfer
Now that we have our models, we’re ready to do some neural style transfer! Note, there are images available in examples/inputs and examples/outputs.
Download this image and save it as “klimt_the_kiss.jpg” in “images”.
Download this image and save it as “data.jpg” in “images”.
Download this image and save it as “socrates.jpg” in “ images”.
Launch the neural style container. Run the following code
th neural_style.lua -style_image images/klimt_the_kiss.jpg -content_image images/socrates.jpg -output_image outputs/socrates_kiss_01.png -backend cudnn
It may be a minute or so before you see any response after executing the code. Eventually, you will see something that looks like the following:
Note, if your style losses are NaN values, then you’re going to end up with completely black images. If this happens, you can cancel the execution by selecting ctrl+c. Consider trying to adjust the parameters to try and fix this. Using the adam optimizer, adjusting the image size, or adjusting the weights is often helpful. You can find more information here.
You can find your new stylized images in the “output” folder.
Now try using “data.jpg” as a style image, making sure to rename the output file so that you don’t accidentally erase your last set of outputs. Hint: In terminal, you can press the up arrow on your keyboard to scroll through previously executed commands.
After you run style transfer again, you should see something like the following in the “output” folder.
Congratulations! You’ve successfully used GPU-driven deep learning for neural style transfer. I encourage you play with different parameters, to use your images of your own art, and to have fun!
This is a living document, subject to change. If you find any inaccuracies or have any comments, reply below!