Neural style and texture filters¶
| Fact | Value |
|---|---|
| Also called | Fast neural style, Style transfer, Perceptual loss style |
| Status | Available through an optional pretrained model |
| Family | Deep learning and learned processing |
| Engine | Science engine |
| Type | Nonlinear, learned, feed-forward style transfer |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/learned.py, fast_style |
| Operator code | engine/src/pixelnexus_engine/filters/learned.py, style_representation |
| Packages | ONNX Runtime (optional) |
Overview¶
Fast neural style transfer, after Johnson, Alahi and Fei-Fei, repaints an image in the style of a painting in one pass. Each network was trained once, for one painting, to keep the content of any photo while matching the textures and colors of the painting. PixelNexus offers the five models of the ONNX model zoo. Each is a separate download. The filter runs in the PixelNexus engine.
It needs an optional model. The model downloads only when you click Download model in the Properties panel, which first shows its size and license, and the engine checks its SHA-256 before it keeps it. Models published as PyTorch weights are converted to ONNX once on your computer with tools/export_models.py. The engine itself never uses PyTorch. Results carry the label: AI result. Detail may not exist in the original scene.
How it works¶
- Run the network for the chosen style on the image, at its own size.
- Crop the output to the image size.
- Mix with the input by the strength.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(x\): The input image
- \(f_{\text{style}}\): The network for one style
- \(s\): The strength
f is a network of convolutions, residual blocks with instance normalization, and upsampling layers. It was trained to make the deep features of y, in a fixed VGG network, match the feature correlations of the painting and the features of x. After training, one pass styles any image. s mixes the result with the input, our addition.
During training, a fixed VGG network measured two things. The content loss compared the deep features of the output with those of the photo. The style loss compared their correlations, the Gram matrices, with those of the painting. The style network learned to make both small. After training, no VGG is needed. One pass of the style network is enough.
Kernel or operator¶
A trained network has many learned kernels, not one. The Inspector shows the stages and the model with its license.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Style style |
Mosaic | Mosaic, Candy, Udnie, Rain princess, Pointilism | The painting the network was trained on. Each style downloads on its own. |
Strength strength |
1 | 0 to 1 | How much of the styled image to mix with the input. Our choice. |
Implementation¶
fast_style in engine/src/pixelnexus_engine/filters/learned.py runs the zoo model of the chosen style. The models declare a fixed 224 x 224 input, although the network is fully convolutional. The engine frees the height and width in memory with the onnx package, and leaves the downloaded file unchanged. The output is rounded up to a multiple of 4 and cropped to the image. Strength mixes the result with the input, our choice.
Worked example¶
Style transfer has no ground truth, so there is no score. The figure shows three styles on one test image. Brush strokes have a fixed size in pixels, so a large image gets relatively finer strokes than a small one.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Only the five trained styles.
- Large images give smaller brush strokes than small ones, since the network works in pixels.
Artifacts
- Repeating texture in flat areas.
Tests¶
Tiny ONNX models made inside the tests check the plumbing in every test run. Tiles with enough margin must equal a run on the whole image. Color, alpha and padding must be handled as described. Where the real model is downloaded, as on the development machine, the real network is checked too. A model with a fixed 224 x 224 input must run at another size, and the strength must mix the result with the input.
References¶
- Johnson, J., Alahi, A., and Fei-Fei, L. Perceptual losses for real-time style transfer and super-resolution. Computer Vision, ECCV 2016, Lecture Notes in Computer Science, 694 to 711, 2016. DOI 10.1007/978-3-319-46475-6_43.
- ONNX model zoo, fast neural style transfer models. https://github.com/onnx/models