| import torch
|
| from PIL import Image
|
| from torchvision import transforms
|
| from model import model as Model
|
|
|
| def predict_image(image_path, model_path='./models/Network_best.pth'):
|
|
|
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
|
| net = Model(pretrain=False).to(device)
|
| net.load_state_dict(torch.load(model_path, map_location=device))
|
| net.eval()
|
|
|
|
|
| transform = transforms.Compose([
|
| transforms.Resize((224, 224)),
|
| transforms.ToTensor()
|
| ])
|
|
|
|
|
| img = Image.open(image_path).convert('RGB')
|
| x = transform(img).unsqueeze(0).to(device)
|
|
|
|
|
| with torch.no_grad():
|
| prob = torch.sigmoid(net(x)).item()
|
|
|
|
|
| result = "REAL" if prob > 0.5 else "AI-GENERATED"
|
| print(f"๐ผ๏ธ Image: {image_path}")
|
| print(f"๐ Prediction: {result}")
|
| print(f"๐ Confidence: {prob:.3f}")
|
|
|
|
|
| if __name__ == "__main__":
|
|
|
| image_path = "sample.jpg"
|
| predict_image(image_path)
|
|
|