Upload folder using huggingface_hub
Browse files- convnext_ai_detector_final.pth +3 -0
- model_config.json +1 -0
- predictor.py +42 -0
convnext_ai_detector_final.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b593b8598ef03b1f39308f3c5a8032b38ac11d1b45d8323e629facc839cd9c73
|
| 3 |
+
size 785073417
|
model_config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model_name": "convnext_large.fb_in22k_ft_in1k", "img_size": 384, "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225], "labels": {"0": "AI Generated", "1": "Human / Real"}}
|
predictor.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import timm
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
|
| 7 |
+
class AIDetector:
|
| 8 |
+
def __init__(self, model_path, config_path):
|
| 9 |
+
with open(config_path, 'r') as f:
|
| 10 |
+
self.config = __import__('json').load(f)
|
| 11 |
+
|
| 12 |
+
# Load Architecture
|
| 13 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
self.model = timm.create_model(self.config['model_name'], pretrained=False, num_classes=2)
|
| 15 |
+
|
| 16 |
+
# Load Weights
|
| 17 |
+
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
|
| 18 |
+
self.model.to(self.device)
|
| 19 |
+
self.model.eval()
|
| 20 |
+
|
| 21 |
+
# Setup Transforms
|
| 22 |
+
self.transform = transforms.Compose([
|
| 23 |
+
transforms.Resize((self.config['img_size'], self.config['img_size'])),
|
| 24 |
+
transforms.ToTensor(),
|
| 25 |
+
transforms.Normalize(mean=self.config['mean'], std=self.config['std']),
|
| 26 |
+
])
|
| 27 |
+
|
| 28 |
+
def predict(self, image_path):
|
| 29 |
+
img = Image.open(image_path).convert('RGB')
|
| 30 |
+
img_t = self.transform(img).unsqueeze(0).to(self.device)
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = self.model(img_t)
|
| 34 |
+
probs = torch.nn.functional.softmax(outputs, dim=1)
|
| 35 |
+
conf, pred = torch.max(probs, 1)
|
| 36 |
+
|
| 37 |
+
label = self.config['labels'][str(pred.item())]
|
| 38 |
+
return {"prediction": label, "confidence": conf.item()}
|
| 39 |
+
|
| 40 |
+
# Example usage:
|
| 41 |
+
# detector = AIDetector('convnext_ai_detector_final.pth', 'model_config.json')
|
| 42 |
+
# print(detector.predict('test_image.jpg'))
|