-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_server.py
70 lines (51 loc) · 1.79 KB
/
inference_server.py
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
import io
import json
import torch
from PIL import Image
from generator import Generator
import numpy as np
from flask import Flask, jsonify, request
from PIL import Image
app = Flask(__name__)
torch.manual_seed(1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = 'cpu'
batch_size = 128
# Root directory for the dataset
model_path = "model\generator_epoch_950.pth"
img_save_pth = "images"
image_shape = (3, 128, 128)
image_dim = int(np.prod(image_shape))
latent_dim = 100
n_classes = 2
embedding_dim = 100
generator = Generator(n_classes, embedding_dim,latent_dim).to(device)
generator.load_state_dict(torch.load(model_path), strict=False)
print("Model loaded....................")
num_images = 1
generator.eval()
@app.route('/generate', methods=['POST'])
def generate():
if request.method == 'POST':
input = request.json
label = input.get('label')
# Generate random noise
z = torch.randn(num_images, latent_dim)
z = z.to(device)
with torch.no_grad():
labels = torch.ones(num_images) * label
labels = labels.to(device)
labels = labels.unsqueeze(1).long()
generated_images = generator((z, labels))
print(generated_images.shape)
image = generated_images[0].permute(1, 2, 0).cpu().numpy() # Change tensor to numpy array
image = (image + 1) / 2.0 * 255.0 # Rescale pixel values
image = image.astype('uint8')
im = Image.fromarray(image)
# Save the image as JPEG
im.save("static/image.jpg")
img_path = "http://127.0.0.1:5000/static/image.jpg"
print("New image generated ............... ", "label : ",label)
return jsonify({'imageUrl': img_path})
if __name__ == '__main__':
app.run()