File size: 6,496 Bytes
ab51159 4d2ac45 ab51159 3131a8e 4d2ac45 ac05c13 4d2ac45 ab51159 4d2ac45 ab51159 4d2ac45 ab51159 833f32e 001319d 66d25ea 833f32e 001319d 66d25ea 833f32e 001319d 833f32e 001319d 66d25ea 833f32e 001319d 833f32e 001319d 833f32e 001319d 66d25ea 833f32e 001319d 833f32e 001319d 833f32e 4d2ac45 833f32e 001319d 833f32e 001319d 833f32e 001319d ab51159 4d2ac45 ab51159 4d2ac45 ab51159 4d2ac45 ab51159 4d2ac45 ab51159 4d2ac45 ab51159 ac05c13 001319d 3131a8e 001319d 3131a8e ac05c13 3131a8e 001319d b899fa4 4d2ac45 b899fa4 4d2ac45 ab51159 4d2ac45 ab51159 b899fa4 ab51159 4d2ac45 ab51159 4d2ac45 cc8203a 4d2ac45 ab51159 4d2ac45 ab51159 4d2ac45 cc8203a dfe8843 4d2ac45 dfe8843 4d2ac45 ab51159 4d2ac45 ab51159 ac05c13 | 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | """
Intel Scene Classifier — Flask App
"""
import io
import os
import urllib.request
import numpy as np
from flask import Flask, jsonify, render_template, request
from PIL import Image
import tensorflow as tf
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from tensorflow.keras import layers, models
app = Flask(__name__)
CLASSES = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
IMG_SIZE = 150
_pytorch_model = None
_tf_model = None
class CNN_Torch(nn.Module):
"""
CNN amélioré 4 blocs
Entrée : (B, 3, 150, 150)
Sortie : (B, num_classes)
"""
def __init__(self, num_classes=6):
super().__init__()
self.features = nn.Sequential(
# Block 1: 150 -> 75
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
# Block 2: 75 -> 37
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Dropout2d(0.10),
# Block 3: 37 -> 18
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Dropout2d(0.15),
# Block 4: 18 -> 9
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Dropout2d(0.20),
)
self.gap = nn.AdaptiveAvgPool2d(1)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(256, 256),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(256, num_classes)
)
def forward(self, x):
x = self.features(x)
x = self.gap(x)
x = self.classifier(x)
return x
def load_pytorch():
global _pytorch_model
if _pytorch_model is not None:
return _pytorch_model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = CNN_Torch(num_classes=6).to(device)
state_dict = torch.load("parfait_model.pth", map_location=device)
model.load_state_dict(state_dict)
model.eval()
tf_transform = transforms.Compose([
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]
),
])
_pytorch_model = (model, device, tf_transform)
return _pytorch_model
def build_cnn_tf(num_classes: int = 6, input_shape: tuple = (228, 228, 3)):
return models.Sequential([
layers.Input(shape=input_shape),
layers.Conv2D(32, (5, 5), activation="relu"),
layers.MaxPooling2D(2, 2),
layers.Conv2D(32, (5, 5), activation="relu"),
layers.MaxPooling2D(2, 2),
layers.Conv2D(32, (3, 3), activation="relu"),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation="relu"),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation="relu"),
layers.MaxPooling2D(2, 2),
layers.Flatten(),
layers.Dense(1024, activation="relu"),
layers.Dropout(0.20),
layers.Dense(124, activation="relu"),
layers.Dropout(0.20),
layers.Dense(num_classes, activation="softmax"),
])
def load_tensorflow():
global _tf_model
if _tf_model is not None:
return _tf_model
model = build_cnn_tf(num_classes=6, input_shape=(228, 228, 3))
model.load_weights("parfait_model.keras")
_tf_model = model
return _tf_model
def read_input_image():
if "image" in request.files and request.files["image"].filename:
return Image.open(io.BytesIO(request.files["image"].read())).convert("RGB")
image_url = request.form.get("image_url", "").strip()
if image_url:
with urllib.request.urlopen(image_url) as response:
return Image.open(io.BytesIO(response.read())).convert("RGB")
raise ValueError("No image provided")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/predict", methods=["POST"])
def predict():
framework = request.form.get("model", "pytorch")
try:
pil_img = read_input_image()
except Exception:
return jsonify({"error": "Fichier image invalide"}), 400
try:
if framework == "pytorch":
model, device, tf_transform = load_pytorch()
tensor = tf_transform(pil_img).unsqueeze(0).to(device)
with torch.no_grad():
out = model(tensor)
probs = torch.softmax(out, dim=1).cpu().numpy()[0]
#probs = torch.exp(out).cpu().numpy()[0]
elif framework == "tensorflow":
model = load_tensorflow()
arr = np.array(
pil_img.resize((IMG_SIZE, IMG_SIZE)),
dtype=np.float32
)
arr = np.expand_dims(arr, axis=0)
probs = model.predict(arr, verbose=0)[0]
else:
return jsonify({"error": "Framework non supporté"}), 400
pred_idx = int(np.argmax(probs))
return jsonify({
"class": CLASSES[pred_idx],
#"confidence": float(probs[pred_idx]),
"confidence": float(probs[pred_idx]),
"probabilities": {
c: float(p) for c, p in zip(CLASSES, probs)
},
})
except FileNotFoundError as e:
return jsonify({
"error": f"Modèle introuvable : {e}. Placez les fichiers .pth et .keras à la racine."
}), 500
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=False)
|