-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
73 lines (59 loc) · 2.13 KB
/
server.py
File metadata and controls
73 lines (59 loc) · 2.13 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
import io
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
from flask import Flask, request, jsonify
from flask_cors import CORS
import traceback
#모델
class CustomResNet(nn.Module):
def __init__(self, num_classes):
super(CustomResNet, self).__init__()
self.resnet = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)
# 기존 fc 레이어의 출력 크기를 확인하여 새로운 레이어 추가
num_ftrs = self.resnet.fc.in_features
# 원래 ResNet50의 마지막 fc 레이어를 제거하고 새로운 레이어 추가
self.resnet.fc = nn.Sequential(
nn.Linear(num_ftrs, 512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, num_classes)
)
def forward(self, x):
return self.resnet(x)
app = Flask(__name__)
CORS(app)
# 모델 불러오는 부분
# 모델 경로 설정해주기
num_classes = 7
model = CustomResNet(num_classes=num_classes)
model.load_state_dict(torch.load('./myModel/model.pth', map_location=torch.device('cpu')))
model.eval()
# 사진 전처리(224 x 224로 만들어주기)
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
@app.route('/face', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
try:
img = Image.open(file.stream).convert('RGB')
img = transform(img).unsqueeze(0)
with torch.no_grad():
outputs = model(img)
_, predicted = torch.max(outputs, 1)
predicted_class = predicted.item() + 1
print(f'Predicted class: {predicted_class}')
return jsonify({'predicted_class': predicted_class})
except Exception as e:
traceback.print_exc()
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3333)