011nuyha commited on
Commit
578f570
โ€ข
1 Parent(s): bfec04d

Upload test-example

Browse files

promotion_model.pt : EfficientNet ๋ชจ๋ธ์„ PyTorch๋กœ ํ•™์Šตํ•œ ๊ฒฐ๊ณผ
predict.py : ์˜ˆ์‹œ ๋ถ„๋ฅ˜ ์ฝ”๋“œ

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