-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
200 lines (164 loc) · 6.34 KB
/
server.py
File metadata and controls
200 lines (164 loc) · 6.34 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from flask import Flask, request, send_file
import torch
from PIL import Image
import numpy as np
import io
import os
from albumentations import Compose, Resize, HorizontalFlip, Normalize
from albumentations.pytorch import ToTensorV2
from Training.model import Generator
from flask_cors import CORS
# Initialize Flask app
app = Flask(__name__)
CORS(app)
# Load your PyTorch models
VanGoghModel = Generator(3)
MonetModel = Generator(3)
MunchModel = Generator(3)
VanGoghModel.load_state_dict(torch.load("Models/vanGoghModel.pt", map_location=torch.device('cpu'), weights_only = True))
MonetModel.load_state_dict(torch.load("Models/monetModel.pt", map_location=torch.device('cpu'), weights_only = True))
MunchModel.load_state_dict(torch.load("Models/munchModel.pt", map_location=torch.device('cpu'), weights_only = True))
VanGoghModel.eval()
MonetModel.eval()
MunchModel.eval()
# Define image preprocessing
transforms = Compose(
[
Resize(width=256, height=256),
Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], max_pixel_value=255),
ToTensorV2(),
]
)
def preprocess_image(image_bytes):
"""
Preprocesses the image for model inference.
Args:
image_bytes: Byte data from the uploaded file.
Returns:
Preprocessed PyTorch tensor.
"""
# Open the image
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
# Convert to NumPy array and apply Albumentations
original_width, original_height = image.size
image_np = np.array(image)
transformed = transforms(image=image_np)
# Add batch dimension and return tensor
return transformed["image"].unsqueeze(0), (original_width, original_height) # Shape: [1, C, H, W]
def tensor_to_image(tensor):
"""
Converts a PyTorch tensor to a PIL image.
Args:
tensor: PyTorch tensor output from the model.
Returns:
PIL Image encoded in PNG format.
"""
tensor = tensor.squeeze(0) # Remove batch dimension
tensor = tensor.permute(1, 2, 0) # Change to (H, W, C)
tensor = tensor.clamp(0, 255) # Ensure the values are in [0, 255]
tensor = tensor.byte().cpu().numpy() # Convert to NumPy
image = Image.fromarray(tensor)
# Save to a byte buffer as PNG
buffer = io.BytesIO()
image.save(buffer, format="PNG")
buffer.seek(0)
return buffer
def denormalize_image(tensor):
"""
Denormalizes the image by reversing the normalization step.
Args:
tensor: The output tensor from the model.
Returns:
Denormalized image tensor.
"""
mean = torch.tensor([0.5, 0.5, 0.5])
std = torch.tensor([0.5, 0.5, 0.5])
tensor = tensor * std[:, None, None] + mean[:, None, None] # Reverse normalization
tensor = tensor * 255.0 # Scale to [0, 255]
return tensor.clamp(0, 255)
@app.route("/predictVanGogh", methods=["POST"])
def predict_vanGogh():
"""
Endpoint for image prediction.
Receives an image file, preprocesses it, runs inference, and returns an image.
"""
# Check if the request has a file
if "file" not in request.files:
return {"error": "No file uploaded"}, 400
# Read the file
file = request.files["file"]
image_bytes = file.read()
# Preprocess the image
input_tensor, original_dims = preprocess_image(image_bytes)
# Perform inference
with torch.no_grad():
output_tensor = VanGoghModel(input_tensor)
# Denormalize the output tensor
output_tensor = denormalize_image(output_tensor)
# Convert the output tensor to an image
output_image = tensor_to_image(output_tensor)
original_width, original_height = original_dims
resized_image = Image.open(output_image).resize((original_width, original_height))
buffer = io.BytesIO()
resized_image.save(buffer, format="PNG")
buffer.seek(0)
# Send the image back as a response
return send_file(buffer, mimetype="image/png")
@app.route("/predictMonet", methods=["POST"])
def predict_Monet():
"""
Endpoint for image prediction.
Receives an image file, preprocesses it, runs inference, and returns an image.
"""
# Check if the request has a file
if "file" not in request.files:
return {"error": "No file uploaded"}, 400
# Read the file
file = request.files["file"]
image_bytes = file.read()
# Preprocess the image
input_tensor, original_dims = preprocess_image(image_bytes)
# Perform inference
with torch.no_grad():
output_tensor = MonetModel(input_tensor)
# Denormalize the output tensor
output_tensor = denormalize_image(output_tensor)
# Convert the output tensor to an image
output_image = tensor_to_image(output_tensor)
original_width, original_height = original_dims
resized_image = Image.open(output_image).resize((original_width, original_height))
buffer = io.BytesIO()
resized_image.save(buffer, format="PNG")
buffer.seek(0)
# Send the image back as a response
return send_file(buffer, mimetype="image/png")
@app.route("/predictMunch", methods=["POST"])
def predict_Munch():
"""
Endpoint for image prediction.
Receives an image file, preprocesses it, runs inference, and returns an image.
"""
# Check if the request has a file
if "file" not in request.files:
return {"error": "No file uploaded"}, 400
# Read the file
file = request.files["file"]
image_bytes = file.read()
# Preprocess the image
input_tensor, original_dims = preprocess_image(image_bytes)
# Perform inference
with torch.no_grad():
output_tensor = MunchModel(input_tensor)
# Denormalize the output tensor
output_tensor = denormalize_image(output_tensor)
# Convert the output tensor to an image
output_image = tensor_to_image(output_tensor)
original_width, original_height = original_dims
resized_image = Image.open(output_image).resize((original_width, original_height))
buffer = io.BytesIO()
resized_image.save(buffer, format="PNG")
buffer.seek(0)
# Send the image back as a response
return send_file(buffer, mimetype="image/png")
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=False)