This repository contains trained
GeneralNeuralRenderer that learned to mimic
TextureBrush (these are
components of my
Stylized Neural Painting paper reimplementation).
1from PIL import Image
2from huggingface_hub import hf_hub_download
3from painting.brushes import TextureBrush, GeneralNeuralRenderer
4import torchvision.transforms.functional as TF
5
6def load_texture(source: str, device: str):
7 img = Image.open(source).convert("L")
8 tensor = TF.pil_to_tensor(img).float() / 255.0
9 return tensor.to(device)
10
11# Specify your device and output canvas size.
12device = "cuda"
13out_canvas_size = 512
14
15brush = TextureBrush(
16 large_vertical=load_texture("./assets/textures/large_vertical_brush.png", device),
17 large_horizontal=load_texture("./assets/textures/large_horizontal_brush.png", device),
18 small_vertical=load_texture("./assets/textures/small_vertical_brush.png", device),
19 small_horizontal=load_texture("./assets/textures/small_horizontal_brush.png", device),
20 canvas_size=out_canvas_size,
21 device=device,
22)
23differentiable_brush = GeneralNeuralRenderer(
24 brush_params_count=brush.brush_params_count,
25 canvas_size=128,
26 device=device,
27)
28
29weights = hf_hub_download(repo_id="Druudik1/texture-brush", filename="neural_renderer.pt")
30differentiable_brush.load_state_dict(
31 torch.load(weights, map_location=device, weights_only=True)
32)