Upload src/config.py with huggingface_hub
Browse files- src/config.py +22 -18
src/config.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
-
"""Imager-1-Tiny - Model
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
-
from torchvision import models
|
| 6 |
from PIL import Image
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
class ImagerTiny(nn.Module):
|
| 10 |
-
"""ResNet-50 backbone with a single-logit head."""
|
| 11 |
|
| 12 |
-
def __init__(self, pretrained=False):
|
| 13 |
super().__init__()
|
| 14 |
self.backbone = models.resnet50(
|
| 15 |
weights="IMAGENET1K_V1" if pretrained else None
|
|
@@ -21,7 +23,7 @@ class ImagerTiny(nn.Module):
|
|
| 21 |
return self.backbone(x)
|
| 22 |
|
| 23 |
|
| 24 |
-
def load_model(weights_path=None, device="cpu"):
|
| 25 |
"""Load the trained Imager-1-Tiny model."""
|
| 26 |
if weights_path is None:
|
| 27 |
weights_path = os.path.join(
|
|
@@ -35,20 +37,22 @@ def load_model(weights_path=None, device="cpu"):
|
|
| 35 |
return model
|
| 36 |
|
| 37 |
|
| 38 |
-
def predict(image, model=None, device="cpu"):
|
| 39 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if model is None:
|
| 41 |
model = load_model(device=device)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
transforms.Resize(256),
|
| 45 |
-
transforms.CenterCrop(224),
|
| 46 |
-
transforms.ToTensor(),
|
| 47 |
-
transforms.Normalize(
|
| 48 |
-
mean=[0.485, 0.456, 0.406],
|
| 49 |
-
std=[0.229, 0.224, 0.225],
|
| 50 |
-
),
|
| 51 |
-
])
|
| 52 |
|
| 53 |
if isinstance(image, str):
|
| 54 |
image = Image.open(image)
|
|
@@ -56,7 +60,7 @@ def predict(image, model=None, device="cpu"):
|
|
| 56 |
image = Image.open(image)
|
| 57 |
image = image.convert("RGB")
|
| 58 |
|
| 59 |
-
tensor =
|
| 60 |
|
| 61 |
with torch.no_grad():
|
| 62 |
prob = torch.sigmoid(model(tensor)).item()
|
|
@@ -79,6 +83,6 @@ MODEL_CONFIG = {
|
|
| 79 |
"labels": {0: "Real", 1: "AI-Generated"},
|
| 80 |
"normalize": {
|
| 81 |
"mean": [0.485, 0.456, 0.406],
|
| 82 |
-
"std":
|
| 83 |
},
|
| 84 |
}
|
|
|
|
| 1 |
+
"""Imager-1-Tiny - Model architecture and inference."""
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
+
from torchvision import models
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
from .preprocess import get_transform
|
| 9 |
+
|
| 10 |
|
| 11 |
class ImagerTiny(nn.Module):
|
| 12 |
+
"""ResNet-50 backbone with a single-logit binary head."""
|
| 13 |
|
| 14 |
+
def __init__(self, pretrained: bool = False):
|
| 15 |
super().__init__()
|
| 16 |
self.backbone = models.resnet50(
|
| 17 |
weights="IMAGENET1K_V1" if pretrained else None
|
|
|
|
| 23 |
return self.backbone(x)
|
| 24 |
|
| 25 |
|
| 26 |
+
def load_model(weights_path: str = None, device: str = "cpu") -> ImagerTiny:
|
| 27 |
"""Load the trained Imager-1-Tiny model."""
|
| 28 |
if weights_path is None:
|
| 29 |
weights_path = os.path.join(
|
|
|
|
| 37 |
return model
|
| 38 |
|
| 39 |
|
| 40 |
+
def predict(image, model: ImagerTiny = None, device: str = "cpu") -> dict:
|
| 41 |
+
"""
|
| 42 |
+
Predict whether an image is Real or AI-Generated.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
image: PIL.Image, str path, or file-like object.
|
| 46 |
+
model: Pre-loaded ImagerTiny (optional).
|
| 47 |
+
device: "cpu" or "cuda".
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
dict with probability, label, confidence.
|
| 51 |
+
"""
|
| 52 |
if model is None:
|
| 53 |
model = load_model(device=device)
|
| 54 |
|
| 55 |
+
transform = get_transform(train=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if isinstance(image, str):
|
| 58 |
image = Image.open(image)
|
|
|
|
| 60 |
image = Image.open(image)
|
| 61 |
image = image.convert("RGB")
|
| 62 |
|
| 63 |
+
tensor = transform(image).unsqueeze(0).to(device)
|
| 64 |
|
| 65 |
with torch.no_grad():
|
| 66 |
prob = torch.sigmoid(model(tensor)).item()
|
|
|
|
| 83 |
"labels": {0: "Real", 1: "AI-Generated"},
|
| 84 |
"normalize": {
|
| 85 |
"mean": [0.485, 0.456, 0.406],
|
| 86 |
+
"std": [0.229, 0.224, 0.225],
|
| 87 |
},
|
| 88 |
}
|