🚀 AI與人類圖像分類模型
本項目是一個經過微調的圖像分類模型,在60,000張AI生成圖像和60,000張人類圖像上進行訓練。該模型能夠出色地檢測出由Midjourney v6.1、Flux 1.1 Pro、Stable Diffusion 3.5、GPT - 4o等前沿AI模型生成的高質量圖像。
詳細的訓練代碼可查看:blog/ai/fine - tuning - siglip2
🚀 快速開始
安裝依賴
pip install -q transformers torch Pillow accelerate
代碼示例
import torch
from PIL import Image as PILImage
from transformers import AutoImageProcessor, SiglipForImageClassification
MODEL_IDENTIFIER = r"Ateeqq/ai-vs-human-image-detector"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
try:
print(f"Loading processor from: {MODEL_IDENTIFIER}")
processor = AutoImageProcessor.from_pretrained(MODEL_IDENTIFIER)
print(f"Loading model from: {MODEL_IDENTIFIER}")
model = SiglipForImageClassification.from_pretrained(MODEL_IDENTIFIER)
model.to(device)
model.eval()
print("Model and processor loaded successfully.")
except Exception as e:
print(f"Error loading model or processor: {e}")
exit()
IMAGE_PATH = r"/content/images.jpg"
try:
print(f"Loading image: {IMAGE_PATH}")
image = PILImage.open(IMAGE_PATH).convert("RGB")
except FileNotFoundError:
print(f"Error: Image file not found at {IMAGE_PATH}")
exit()
except Exception as e:
print(f"Error opening image: {e}")
exit()
print("Preprocessing image...")
inputs = processor(images=image, return_tensors="pt").to(device)
print("Running inference...")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
predicted_label = model.config.id2label[predicted_class_idx]
probabilities = torch.softmax(logits, dim=-1)
predicted_prob = probabilities[0, predicted_class_idx].item()
print("-" * 30)
print(f"Image: {IMAGE_PATH}")
print(f"Predicted Label: {predicted_label}")
print(f"Confidence Score: {predicted_prob:.4f}")
print("-" * 30)
print("Scores per class:")
for i, label in model.config.id2label.items():
print(f" - {label}: {probabilities[0, i].item():.4f}")
輸出示例
Using device: cpu
Model and processor loaded successfully.
Loading image: /content/images.jpg
Preprocessing image...
Running inference...
------------------------------
Image: /content/images.jpg
Predicted Label: ai
Confidence Score: 0.9996
------------------------------
Scores per class:
- ai: 0.9996
- hum: 0.0004
✨ 主要特性
- 強大的檢測能力:能夠有效識別多種前沿AI模型生成的高質量圖像。
- 高準確率:在測試集上取得了高達0.9923的準確率。
📚 詳細文檔
評估指標

🏋️♂️ 訓練指標
- Epoch:5.0
- 總浮點運算次數(Total FLOPs):51,652,280,821 GF
- 訓練損失(Train Loss):0.0799
- 訓練運行時間(Train Runtime):2:39:49.46
- 訓練樣本每秒處理數(Train Samples/Sec):69.053
- 訓練步驟每秒處理數(Train Steps/Sec):4.316
📊 評估指標(微調模型在測試集上)
- Epoch:5.0
- 評估準確率(Eval Accuracy):0.9923
- 評估損失(Eval Loss):0.0551
- 評估運行時間(Eval Runtime):0:02:35.78
- 評估樣本每秒處理數(Eval Samples/Sec):212.533
- 評估步驟每秒處理數(Eval Steps/Sec):6.644
🔦 預測指標(在測試集上)
{
"test_loss": 0.05508904904127121,
"test_accuracy": 0.9923283699296264,
"test_runtime": 167.1844,
"test_samples_per_second": 198.039,
"test_steps_per_second": 6.191
}
- 最終測試準確率:0.9923
- 最終測試F1分數(Macro):0.9923
- 最終測試F1分數(Weighted):0.9923
📄 許可證
本項目採用Apache - 2.0許可證。