-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image.py
More file actions
40 lines (25 loc) · 1.03 KB
/
Copy pathtest_image.py
File metadata and controls
40 lines (25 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = models.resnet18(pretrained=False)
model.fc = nn.Linear(model.fc.in_features, 2)
model = model.to(device)
model.load_state_dict(torch.load("../models/image_model.pth", map_location=device))
model.eval()
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor()
])
image_path = r"C:\Users\Isha\.ipython\ai_generated_content_detector\data\images\ai\IMG-20240917-WA0010.jpg"
image = Image.open(image_path).convert("RGB")
image = transform(image).unsqueeze(0).to(device)
import torch.nn.functional as F
with torch.no_grad():
output = model(image)
probabilities = F.softmax(output, dim=1)
confidence, predicted = torch.max(probabilities, 1)
classes = ["AI Generated", "Real"]
print("Prediction:", classes[predicted.item()])
print("Confidence:", round(confidence.item() * 100, 2), "%")