This code uses the VGG19 architecture for neural style transfer. Given a content image and a style image, it applies the style of the style image to the content image.
This code requires the following packages to be installed:
- numpy
- tensorflow
- Pillow
- tqdm
Run the script test.py and provide the path to the content image, the style image, and the output image.
python main.py [-h] [--norm] content_path style_path output_pathThe script will generate an image with the style of the style image applied to the content image, and save it to the specified output path.
Positional arguments:
content_pathpath to the content imagestyle_pathpath to the style imageoutput_pathpath to the output image
Options:
-h,--helpshow this help message and exit--normuse instance normalization
The code loads the VGG19 architecture with pre-trained ImageNet weights. It then extracts the features of the content image and the style image from specific layers of the network. The content loss is calculated as the mean squared error between the feature maps of the content image and the generated image. The style loss is calculated as the mean squared error between the Gram matrices of the feature maps of the style image and the generated image. The total loss is a weighted sum of the content loss and the style loss. The generated image is updated using the Adam optimizer to minimize the total loss.
| Content | Style |
|---|---|
![]() |
![]() |
ezgif-3-7803673716.mp4
- num_iterations: The number of iterations to run the optimization for (default: 1000).
- content_weight: The weight given to the content loss (default: 1e3).
- style_weight: The weight given to the style loss (default: 1e-2).
Gatys, L. A., Ecker, A. S., & Bethge, M. (2015). A Neural Algorithm of Artistic Style. arXiv preprint arXiv:1508.06576.

