Upload test-example
Browse filespromotion_model.pt : EfficientNet ๋ชจ๋ธ์ PyTorch๋ก ํ์ตํ ๊ฒฐ๊ณผ
predict.py : ์์ ๋ถ๋ฅ ์ฝ๋
- model/fake_test.webp +0 -0
- model/predict.py +61 -0
- model/promotion_model.pt +3 -0
- model/real_test.jpg +0 -0
model/fake_test.webp
ADDED
model/predict.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from efficientnet_pytorch import EfficientNet
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
# ๋ชจ๋ธ ํด๋์ค ์ ์ ๋ฐ ๋ชจ๋ธ ๋ก๋
|
6 |
+
model_name = 'efficientnet-b0' # ์ฌ์ฉํ EfficientNet ๋ชจ๋ธ์ ์ด๋ฆ์ ๋ง๊ฒ ์์
|
7 |
+
model = EfficientNet.from_name(model_name)
|
8 |
+
|
9 |
+
num_classes = 2
|
10 |
+
model._fc = torch.nn.Linear(model._fc.in_features, num_classes)
|
11 |
+
|
12 |
+
# ๋ชจ๋ธ ๊ฐ์ค์น ๋ก๋
|
13 |
+
model.load_state_dict(torch.load('promotion_model.pt', map_location=torch.device('cpu')))
|
14 |
+
|
15 |
+
# ๋ชจ๋ธ์ ํ๊ฐ ๋ชจ๋๋ก ์ค์
|
16 |
+
model.eval()
|
17 |
+
|
18 |
+
# ์์ ๋ฐ์ดํฐ (์
๋ ฅ ๋ฐ์ดํฐ์ ํํ์ ๋ง๊ฒ ์์ ํด์ผ ํฉ๋๋ค)
|
19 |
+
# EfficientNet ๋ชจ๋ธ์ ์
๋ ฅ๋๋ ๋ฐ์ดํฐ๋ 224x224 ํฌ๊ธฐ์ ์ด๋ฏธ์ง์
๋๋ค.
|
20 |
+
# ์ฌ๊ธฐ์๋ ์์์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํฉ๋๋ค.
|
21 |
+
from PIL import Image
|
22 |
+
from torchvision import transforms
|
23 |
+
|
24 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
25 |
+
preprocess = transforms.Compose([
|
26 |
+
transforms.Resize(224),
|
27 |
+
transforms.CenterCrop(224),
|
28 |
+
transforms.ToTensor(),
|
29 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
30 |
+
])
|
31 |
+
|
32 |
+
# ์์ ์ด๋ฏธ์ง ๋ก๋
|
33 |
+
input_image = Image.open('fake_test.webp') # ์ค์ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ก ์์
|
34 |
+
# input_image = Image.open('real_test.jpg')
|
35 |
+
|
36 |
+
input_tensor = preprocess(input_image)
|
37 |
+
input_batch = input_tensor.unsqueeze(0) # ๋ฐฐ์น ์ฐจ์ ์ถ๊ฐ
|
38 |
+
|
39 |
+
# CUDA ์ฌ์ฉ (GPU๊ฐ ์๋ ๊ฒฝ์ฐ)
|
40 |
+
if torch.cuda.is_available():
|
41 |
+
model = model.to('cuda')
|
42 |
+
input_batch = input_batch.to('cuda')
|
43 |
+
|
44 |
+
# ์์ธก ์ํ
|
45 |
+
with torch.no_grad():
|
46 |
+
output = model(input_batch)
|
47 |
+
|
48 |
+
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
49 |
+
# print(output)
|
50 |
+
|
51 |
+
probabilities = F.softmax(output, dim=1)
|
52 |
+
|
53 |
+
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
54 |
+
# print(probabilities)
|
55 |
+
|
56 |
+
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค ํ์ธ
|
57 |
+
_, predicted_class = torch.max(probabilities, 1)
|
58 |
+
|
59 |
+
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
60 |
+
print('Predicted class:')
|
61 |
+
print('Fake' if predicted_class.item()==0 else 'Real')
|
model/promotion_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:743c9fa37ddb444c4692b7bea81b20c4f3c63e0b1ece9812a5e24895590301ab
|
3 |
+
size 16335682
|
model/real_test.jpg
ADDED