Update model.py
Browse files
model.py
CHANGED
@@ -1,66 +1,40 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
from flask_cors import CORS
|
3 |
-
from PIL import Image
|
4 |
import torch
|
5 |
-
from torchvision import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
model
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"bluebell", "buttercup", "colts_foot", "corn_poppy", "cowslip",
|
33 |
-
"crocus", "daffodil", "daisy", "dandelion", "foxglove",
|
34 |
-
"fritillary", "geranium", "hibiscus", "iris", "lily_valley",
|
35 |
-
"pansy", "petunia", "rose", "snowdrop", "sunflower",
|
36 |
-
"tigerlily", "tulip", "wallflower", "water_lily", "wild_tulip",
|
37 |
-
"windflower"
|
38 |
-
]
|
39 |
-
|
40 |
-
@app.route("/predict", methods=["POST"])
|
41 |
-
def predict():
|
42 |
-
if "file" not in request.files:
|
43 |
-
return jsonify({"error": "No file uploaded"}), 400
|
44 |
-
|
45 |
-
file = request.files["file"]
|
46 |
-
|
47 |
-
try:
|
48 |
-
# Load and preprocess the image
|
49 |
-
image = Image.open(file.stream).convert("RGB")
|
50 |
-
input_tensor = preprocess(image).unsqueeze(0).to(device)
|
51 |
-
|
52 |
-
# Perform inference
|
53 |
with torch.no_grad():
|
54 |
-
outputs = model(
|
55 |
-
_,
|
56 |
-
|
57 |
-
|
58 |
-
print(f"Predicted class: {predicted_label}")
|
59 |
-
return jsonify({"predicted_class": predicted_label})
|
60 |
|
61 |
-
except Exception as e:
|
62 |
-
return jsonify({"error": f"Error during prediction: {str(e)}"}), 500
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from torchvision import models, transforms
|
3 |
+
from PIL import Image
|
4 |
+
import json
|
5 |
+
|
6 |
+
# Load model
|
7 |
+
class CustomResNet:
|
8 |
+
def __init__(self, model_path, num_classes):
|
9 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
self.model = models.resnet152(pretrained=False)
|
11 |
+
self.model.fc = torch.nn.Linear(self.model.fc.in_features, num_classes)
|
12 |
+
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
|
13 |
+
self.model.to(self.device)
|
14 |
+
self.model.eval()
|
15 |
+
|
16 |
+
# Preprocessing
|
17 |
+
self.preprocess = transforms.Compose([
|
18 |
+
transforms.Resize((224, 224)),
|
19 |
+
transforms.ToTensor(),
|
20 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
21 |
+
])
|
22 |
+
|
23 |
+
def predict(self, image_bytes):
|
24 |
+
# Load and preprocess image
|
25 |
+
image = Image.open(image_bytes).convert("RGB")
|
26 |
+
tensor = self.preprocess(image).unsqueeze(0).to(self.device)
|
27 |
+
|
28 |
+
# Make prediction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with torch.no_grad():
|
30 |
+
outputs = self.model(tensor)
|
31 |
+
_, predicted = torch.max(outputs, 1)
|
32 |
+
|
33 |
+
return predicted.item()
|
|
|
|
|
34 |
|
|
|
|
|
35 |
|
36 |
+
# API function
|
37 |
+
def load_model():
|
38 |
+
with open("config.json", "r") as f:
|
39 |
+
config = json.load(f)
|
40 |
+
return CustomResNet("trained_model.pth", config["num_labels"])
|