Rohanify commited on
Commit
fb3e330
Β·
verified Β·
1 Parent(s): fbff8ee

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +61 -11
README.md CHANGED
@@ -21,7 +21,7 @@ This repository contains weights for a Fast Neural Style Transfer network based
21
 
22
  ## How to Use Programmatically
23
 
24
- You can run inference using the Hugging Face `transformers` library. The script automatically downloads your weights and applies the necessary ImageNet normalization matching the training routine.
25
 
26
  ### Dependencies
27
  Ensure you have the required packages installed:
@@ -35,18 +35,65 @@ Save the following code as `inference.py`. You can run it via terminal with `pyt
35
  ```python
36
  import sys
37
  import torch
 
38
  from PIL import Image
39
  from torchvision import transforms
40
  from torchvision.utils import save_image
41
- from transformers import AutoModel
42
 
43
  # ── CONFIG ───────────────────────────────────────────────────
44
- REPO_ID = "Rohanify/Brawnz-StyleTransferSN" # Your repository ID
 
45
  IMG_SIZE = 512
46
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
47
  # ─────────────────────────────────────────────────────────────
48
 
49
- # ── LOAD IMAGE ───────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if len(sys.argv) < 2:
51
  print("Usage: python inference.py path_to_input_image.jpg")
52
  sys.exit(1)
@@ -57,24 +104,27 @@ output_path = "output_styled.jpg"
57
  transform = transforms.Compose([
58
  transforms.Resize((IMG_SIZE, IMG_SIZE)),
59
  transforms.ToTensor(),
60
- transforms.Normalize([0.485, 0.456, 0.406],
61
- [0.229, 0.224, 0.225]),
62
  ])
63
 
64
  img = Image.open(input_path).convert("RGB")
65
  x = transform(img).unsqueeze(0).to(DEVICE)
66
 
67
- # ── LOAD MODEL FROM HUGGING FACE ─────────────────────────────
68
- print("Loading model weights from Hugging Face Hub...")
69
- model = AutoModel.from_pretrained(REPO_ID, trust_remote_code=True).to(DEVICE)
 
 
 
70
  model.eval()
 
71
 
72
  # ── RUN INFERENCE ────────────────────────────────────────────
73
- print(f"Running inference on device: {DEVICE}...")
74
  with torch.no_grad():
75
  out = model(x)
76
 
77
- # De-normalize and save output image
78
  save_image(out[0] * 0.5 + 0.5, output_path)
79
  print(f"Success! Styled image saved to: {output_path}")
 
80
  ```
 
21
 
22
  ## How to Use Programmatically
23
 
24
+ You can run inference using the official `huggingface_hub` utility library. The script automatically downloads your weights file directly from the cloud and applies the necessary ImageNet normalization matching the training routine.
25
 
26
  ### Dependencies
27
  Ensure you have the required packages installed:
 
35
  ```python
36
  import sys
37
  import torch
38
+ import torch.nn as nn
39
  from PIL import Image
40
  from torchvision import transforms
41
  from torchvision.utils import save_image
42
+ from huggingface_hub import hf_hub_download
43
 
44
  # ── CONFIG ───────────────────────────────────────────────────
45
+ REPO_ID = "Rohanify/Brawnz-StyleTransferSN"
46
+ FILENAME = "pytorch_model.bin"
47
  IMG_SIZE = 512
48
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
49
  # ─────────────────────────────────────────────────────────────
50
 
51
+ # ── NATIVE PYTORCH NETWORK DEFINITION ────────────────────────
52
+
53
+ def conv_bn_relu(in_c, out_c, k, stride=1, pad=0):
54
+ return nn.Sequential(
55
+ nn.ReflectionPad2d(pad),
56
+ nn.Conv2d(in_c, out_c, k, stride),
57
+ nn.InstanceNorm2d(out_c),
58
+ nn.ReLU(inplace=True),
59
+ )
60
+
61
+ class ResBlock(nn.Module):
62
+ def __init__(self, c):
63
+ super().__init__()
64
+ self.block = nn.Sequential(
65
+ nn.ReflectionPad2d(1),
66
+ nn.Conv2d(c, c, 3),
67
+ nn.InstanceNorm2d(c),
68
+ nn.ReLU(inplace=True),
69
+ nn.ReflectionPad2d(1),
70
+ nn.Conv2d(c, c, 3),
71
+ nn.InstanceNorm2d(c),
72
+ )
73
+ def forward(self, x):
74
+ return x + self.block(x)
75
+
76
+ class TransformNet(nn.Module):
77
+ def __init__(self):
78
+ super().__init__()
79
+ self.net = nn.Sequential(
80
+ conv_bn_relu(3, 32, 9, pad=4),
81
+ conv_bn_relu(32, 64, 3, stride=2, pad=1),
82
+ conv_bn_relu(64, 128, 3, stride=2, pad=1),
83
+ ResBlock(128), ResBlock(128), ResBlock(128),
84
+ ResBlock(128), ResBlock(128),
85
+ nn.Upsample(scale_factor=2, mode="nearest"),
86
+ conv_bn_relu(128, 64, 3, pad=1),
87
+ nn.Upsample(scale_factor=2, mode="nearest"),
88
+ conv_bn_relu(64, 32, 3, pad=1),
89
+ nn.ReflectionPad2d(4),
90
+ nn.Conv2d(32, 3, 9),
91
+ nn.Tanh(),
92
+ )
93
+ def forward(self, x):
94
+ return self.net(x)
95
+
96
+ # ── LOAD INPUT IMAGE ─────────────────────────────────────────
97
  if len(sys.argv) < 2:
98
  print("Usage: python inference.py path_to_input_image.jpg")
99
  sys.exit(1)
 
104
  transform = transforms.Compose([
105
  transforms.Resize((IMG_SIZE, IMG_SIZE)),
106
  transforms.ToTensor(),
107
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
 
108
  ])
109
 
110
  img = Image.open(input_path).convert("RGB")
111
  x = transform(img).unsqueeze(0).to(DEVICE)
112
 
113
+ # ── SECURE FILE DOWNLOAD & STATE LOAD ────────────────────────
114
+ print("Downloading weights from Hugging Face Hub...")
115
+ weights_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
116
+
117
+ model = TransformNet().to(DEVICE)
118
+ model.load_state_dict(torch.load(weights_path, map_location=DEVICE))
119
  model.eval()
120
+ print(f"Weights successfully loaded on: {DEVICE}")
121
 
122
  # ── RUN INFERENCE ────────────────────────────────────────────
123
+ print("Processing style transfer...")
124
  with torch.no_grad():
125
  out = model(x)
126
 
 
127
  save_image(out[0] * 0.5 + 0.5, output_path)
128
  print(f"Success! Styled image saved to: {output_path}")
129
+
130
  ```