|
import torch
|
|
from efficientnet_pytorch import EfficientNet
|
|
import torch.nn.functional as F
|
|
|
|
|
|
model_name = 'efficientnet-b0'
|
|
model = EfficientNet.from_name(model_name)
|
|
|
|
num_classes = 2
|
|
model._fc = torch.nn.Linear(model._fc.in_features, num_classes)
|
|
|
|
|
|
model.load_state_dict(torch.load('promotion_model.pt', map_location=torch.device('cpu')))
|
|
|
|
|
|
model.eval()
|
|
|
|
|
|
|
|
|
|
from PIL import Image
|
|
from torchvision import transforms
|
|
|
|
|
|
preprocess = transforms.Compose([
|
|
transforms.Resize(224),
|
|
transforms.CenterCrop(224),
|
|
transforms.ToTensor(),
|
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
|
])
|
|
|
|
|
|
input_image = Image.open('fake_test.webp')
|
|
|
|
|
|
input_tensor = preprocess(input_image)
|
|
input_batch = input_tensor.unsqueeze(0)
|
|
|
|
|
|
if torch.cuda.is_available():
|
|
model = model.to('cuda')
|
|
input_batch = input_batch.to('cuda')
|
|
|
|
|
|
with torch.no_grad():
|
|
output = model(input_batch)
|
|
|
|
|
|
|
|
|
|
probabilities = F.softmax(output, dim=1)
|
|
|
|
|
|
|
|
|
|
|
|
_, predicted_class = torch.max(probabilities, 1)
|
|
|
|
|
|
print('Predicted class:')
|
|
print('Fake' if predicted_class.item()==0 else 'Real') |