anishathalye /neural-style

Neural style in TensorFlow! 🎨

★ 5,545⑂ 1,478👁 212⑃ 53● 138▤ 0
+0
日增长
5,545
stars
1,478
forks
212
watchers
53
prs
138
issues
0
releases

README

neural-style 

An implementation of [neural style][paper] in TensorFlow.

This implementation is a lot simpler than a lot of the other ones out there,
thanks to TensorFlow's really nice API and [automatic differentiation][ad].

TensorFlow doesn't support [L-BFGS][l-bfgs] (which is what the original authors
used), so we use [Adam][adam]. This may require a little bit more
hyperparameter tuning to get nice results.

 Running

This project uses the uv project and package manager; you can install it by following these instructions (e.g., installing it with your system package manager, like brew install uv).

You also need to download required data files.

After that, you can run this program with:

bash
uv run neural_style.py --content  --styles  --output 


Run uv run neural_style.py --help to see a list of all options.

Use --checkpoint-output and --checkpoint-iterations to save checkpoint images.

Use --iterations to change the number of iterations (default 1000). For a 512×512 pixel content file, 1000 iterations take 90 seconds on an M3 MacBook Pro, and significantly less time with a more powerful (e.g., NVIDIA) GPU.

 Example 1

Running it for 500-2000 iterations seems to produce nice results. With certain
images or output sizes, you might need some hyperparameter tuning (especially
--content-weight, --style-weight, and --learning-rate).

The following example was run for 1000 iterations to produce the result (with
default parameters):

!output

These were the input images used (me sleeping at a hackathon and Starry Night):

!input-content

!input-style

 Example 2

The following example demonstrates style blending, and was run for 1000
iterations to produce the result (with style blend weight parameters 0.8 and
0.2):

!output

The content input image was a picture of the Stata Center at MIT:

!input-content

The style input images were Picasso's "Dora Maar" and Starry Night, with the
Picasso image having a style blend weight of 0.8 and Starry Night having a
style blend weight of 0.2:

!input-style
!input-style

 Tweaking

--style-layer-weight-exp command line argument could be used to tweak how "abstract"
the style transfer should be. Lower values mean that style transfer of a finer features
will be favored over style transfer of a more coarse features, and vice versa. Default
value is 1.0 - all layers treated equally. Somewhat extreme examples of what you can achieve:

!--style-layer-weight-exp 0.2
!--style-layer-weight-exp 2.0

(left: 0.2 - finer features style transfer; right: 2.0 - coarser features style transfer)

--content-weight-blend specifies the coefficient of content transfer layers. Default value -
1.0, style transfer tries to preserve finer grain content details. The value should be
in range [0.0; 1.0].

!--content-weight-blend 1.0
!--content-weight-blend 0.1

(left: 1.0 - default value; right: 0.1 - more abstract picture)

--pooling allows to select which pooling layers to use (specify either max or avg).
Original VGG topology use

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON