File size: 1,886 Bytes
578f570 |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import torch
from efficientnet_pytorch import EfficientNet
import torch.nn.functional as F
# ๋ชจ๋ธ ํด๋์ค ์ ์ ๋ฐ ๋ชจ๋ธ ๋ก๋
model_name = 'efficientnet-b0' # ์ฌ์ฉํ EfficientNet ๋ชจ๋ธ์ ์ด๋ฆ์ ๋ง๊ฒ ์์
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()
# ์์ ๋ฐ์ดํฐ (์
๋ ฅ ๋ฐ์ดํฐ์ ํํ์ ๋ง๊ฒ ์์ ํด์ผ ํฉ๋๋ค)
# EfficientNet ๋ชจ๋ธ์ ์
๋ ฅ๋๋ ๋ฐ์ดํฐ๋ 224x224 ํฌ๊ธฐ์ ์ด๋ฏธ์ง์
๋๋ค.
# ์ฌ๊ธฐ์๋ ์์์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํฉ๋๋ค.
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_image = Image.open('real_test.jpg')
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # ๋ฐฐ์น ์ฐจ์ ์ถ๊ฐ
# CUDA ์ฌ์ฉ (GPU๊ฐ ์๋ ๊ฒฝ์ฐ)
if torch.cuda.is_available():
model = model.to('cuda')
input_batch = input_batch.to('cuda')
# ์์ธก ์ํ
with torch.no_grad():
output = model(input_batch)
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
# print(output)
probabilities = F.softmax(output, dim=1)
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
# print(probabilities)
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค ํ์ธ
_, predicted_class = torch.max(probabilities, 1)
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
print('Predicted class:')
print('Fake' if predicted_class.item()==0 else 'Real') |