cyberai-1 commited on
Commit
a8cdf96
·
1 Parent(s): 2fc2d09

Update model

Browse files
Files changed (1) hide show
  1. app.py +50 -97
app.py CHANGED
@@ -13,12 +13,12 @@ import torch
13
  import torch.nn as nn
14
  import torch.nn.functional as F
15
  from torchvision import transforms
16
- from tensorflow.keras import layers, models
17
 
18
  app = Flask(__name__)
19
 
20
  CLASSES = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
21
- IMG_SIZE = 150
 
22
 
23
  _pytorch_model = None
24
  _tf_model = None
@@ -26,78 +26,64 @@ _tf_model = None
26
 
27
  class CNN_Torch(nn.Module):
28
  """
29
- CNN amélioré 4 blocs
30
- Entrée : (B, 3, 150, 150)
31
- Sortie : (B, num_classes)
 
 
 
 
 
 
 
 
32
  """
33
-
34
- def __init__(self, num_classes=6):
35
  super().__init__()
36
 
37
  self.features = nn.Sequential(
38
- # Block 1: 150 -> 75
39
- nn.Conv2d(3, 32, kernel_size=3, padding=1),
40
- nn.BatchNorm2d(32),
41
- nn.ReLU(inplace=True),
42
-
43
- nn.Conv2d(32, 32, kernel_size=3, padding=1),
44
- nn.BatchNorm2d(32),
45
- nn.ReLU(inplace=True),
46
-
47
- nn.MaxPool2d(2),
48
-
49
- # Block 2: 75 -> 37
50
- nn.Conv2d(32, 64, kernel_size=3, padding=1),
51
- nn.BatchNorm2d(64),
52
- nn.ReLU(inplace=True),
53
-
54
- nn.Conv2d(64, 64, kernel_size=3, padding=1),
55
- nn.BatchNorm2d(64),
56
- nn.ReLU(inplace=True),
57
-
58
- nn.MaxPool2d(2),
59
- nn.Dropout2d(0.10),
60
-
61
- # Block 3: 37 -> 18
62
- nn.Conv2d(64, 128, kernel_size=3, padding=1),
63
- nn.BatchNorm2d(128),
64
- nn.ReLU(inplace=True),
65
-
66
- nn.Conv2d(128, 128, kernel_size=3, padding=1),
67
- nn.BatchNorm2d(128),
68
- nn.ReLU(inplace=True),
69
-
70
  nn.MaxPool2d(2),
71
- nn.Dropout2d(0.15),
72
 
73
- # Block 4: 18 -> 9
74
- nn.Conv2d(128, 256, kernel_size=3, padding=1),
75
- nn.BatchNorm2d(256),
76
- nn.ReLU(inplace=True),
77
-
78
- nn.Conv2d(256, 256, kernel_size=3, padding=1),
79
- nn.BatchNorm2d(256),
80
- nn.ReLU(inplace=True),
81
-
82
- nn.MaxPool2d(2),
83
- nn.Dropout2d(0.20),
 
 
 
 
 
 
 
 
 
84
  )
85
 
 
86
  self.gap = nn.AdaptiveAvgPool2d(1)
87
 
88
  self.classifier = nn.Sequential(
89
  nn.Flatten(),
90
  nn.Linear(256, 256),
91
  nn.ReLU(inplace=True),
92
- nn.Dropout(0.3),
93
- nn.Linear(256, num_classes)
94
  )
95
 
96
  def forward(self, x):
97
- x = self.features(x)
98
- x = self.gap(x)
99
- x = self.classifier(x)
100
- return x
101
 
102
 
103
  def load_pytorch():
@@ -113,7 +99,7 @@ def load_pytorch():
113
  model.eval()
114
 
115
  tf_transform = transforms.Compose([
116
- transforms.Resize((IMG_SIZE, IMG_SIZE)),
117
  transforms.ToTensor(),
118
  transforms.Normalize(
119
  [0.485, 0.456, 0.406],
@@ -125,44 +111,11 @@ def load_pytorch():
125
  return _pytorch_model
126
 
127
 
128
-
129
- def build_cnn_tf(num_classes: int = 6, input_shape: tuple = (228, 228, 3)):
130
- return models.Sequential([
131
- layers.Input(shape=input_shape),
132
-
133
- layers.Conv2D(32, (5, 5), activation="relu"),
134
- layers.MaxPooling2D(2, 2),
135
-
136
- layers.Conv2D(32, (5, 5), activation="relu"),
137
- layers.MaxPooling2D(2, 2),
138
-
139
- layers.Conv2D(32, (3, 3), activation="relu"),
140
- layers.MaxPooling2D(2, 2),
141
-
142
- layers.Conv2D(64, (3, 3), activation="relu"),
143
- layers.MaxPooling2D(2, 2),
144
-
145
- layers.Conv2D(64, (3, 3), activation="relu"),
146
- layers.MaxPooling2D(2, 2),
147
-
148
- layers.Flatten(),
149
- layers.Dense(1024, activation="relu"),
150
- layers.Dropout(0.20),
151
- layers.Dense(124, activation="relu"),
152
- layers.Dropout(0.20),
153
- layers.Dense(num_classes, activation="softmax"),
154
- ])
155
-
156
-
157
  def load_tensorflow():
158
  global _tf_model
159
- if _tf_model is not None:
160
- return _tf_model
161
-
162
- model = build_cnn_tf(num_classes=6, input_shape=(228, 228, 3))
163
- model.load_weights("parfait_model.keras")
164
-
165
- _tf_model = model
166
  return _tf_model
167
 
168
 
@@ -205,9 +158,9 @@ def predict():
205
  elif framework == "tensorflow":
206
  model = load_tensorflow()
207
  arr = np.array(
208
- pil_img.resize((IMG_SIZE, IMG_SIZE)),
209
  dtype=np.float32
210
- )
211
  arr = np.expand_dims(arr, axis=0)
212
  probs = model.predict(arr, verbose=0)[0]
213
 
@@ -234,4 +187,4 @@ def predict():
234
 
235
  if __name__ == "__main__":
236
  port = int(os.environ.get("PORT", 5000))
237
- app.run(host="0.0.0.0", port=port, debug=False)
 
13
  import torch.nn as nn
14
  import torch.nn.functional as F
15
  from torchvision import transforms
 
16
 
17
  app = Flask(__name__)
18
 
19
  CLASSES = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
20
+ PYTORCH_IMG_SIZE = 150
21
+ TF_IMG_SIZE = 228
22
 
23
  _pytorch_model = None
24
  _tf_model = None
 
26
 
27
  class CNN_Torch(nn.Module):
28
  """
29
+ CNN PyTorch 4 blocs pour images RGB (3 canaux, 150×150).
30
+ Entrée : (B, 3, 150, 150) — normalisée ImageNet (mean/std)
31
+ Sortie : (B, num_classes) — logits bruts (CrossEntropyLoss)
32
+
33
+ Architecture :
34
+ Block 1 : Conv(3→32)×2 + BN + ReLU + MaxPool(2) 150→75
35
+ Block 2 : Conv(32→64)×2 + BN + ReLU + MaxPool(2) + Drop2d 75→37
36
+ Block 3 : Conv(64→128)×2 + BN + ReLU + MaxPool(2) + Drop2d 37→18
37
+ Block 4 : Conv(128→256)×2+ BN + ReLU + MaxPool(2) + Drop2d 18→9
38
+ GAP : AdaptiveAvgPool2d(1) →(B,256)
39
+ Head : Linear(256→256) + ReLU + Dropout + Linear(256→C)
40
  """
41
+ def __init__(self, num_classes: int = 6):
 
42
  super().__init__()
43
 
44
  self.features = nn.Sequential(
45
+ # Block 1 150×150 75×75
46
+ nn.Conv2d(3, 32, kernel_size=3, padding=1, bias=False),
47
+ nn.BatchNorm2d(32), nn.ReLU(inplace=True),
48
+ nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False),
49
+ nn.BatchNorm2d(32), nn.ReLU(inplace=True),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  nn.MaxPool2d(2),
 
51
 
52
+ # Block 2 75×75 → 37×37
53
+ nn.Conv2d(32, 64, kernel_size=3, padding=1, bias=False),
54
+ nn.BatchNorm2d(64), nn.ReLU(inplace=True),
55
+ nn.Conv2d(64, 64, kernel_size=3, padding=1, bias=False),
56
+ nn.BatchNorm2d(64), nn.ReLU(inplace=True),
57
+ nn.MaxPool2d(2), nn.Dropout2d(0.10),
58
+
59
+ # Block 3 — 37×37 → 18×18
60
+ nn.Conv2d(64, 128, kernel_size=3, padding=1, bias=False),
61
+ nn.BatchNorm2d(128), nn.ReLU(inplace=True),
62
+ nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
63
+ nn.BatchNorm2d(128), nn.ReLU(inplace=True),
64
+ nn.MaxPool2d(2), nn.Dropout2d(0.15),
65
+
66
+ # Block 4 — 18×18 → 9×9
67
+ nn.Conv2d(128, 256, kernel_size=3, padding=1, bias=False),
68
+ nn.BatchNorm2d(256), nn.ReLU(inplace=True),
69
+ nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False),
70
+ nn.BatchNorm2d(256), nn.ReLU(inplace=True),
71
+ nn.MaxPool2d(2), nn.Dropout2d(0.20),
72
  )
73
 
74
+ # (B,256,9,9) → (B,256,1,1) → (B,256)
75
  self.gap = nn.AdaptiveAvgPool2d(1)
76
 
77
  self.classifier = nn.Sequential(
78
  nn.Flatten(),
79
  nn.Linear(256, 256),
80
  nn.ReLU(inplace=True),
81
+ nn.Dropout(0.30),
82
+ nn.Linear(256, num_classes),
83
  )
84
 
85
  def forward(self, x):
86
+ return self.classifier(self.gap(self.features(x)))
 
 
 
87
 
88
 
89
  def load_pytorch():
 
99
  model.eval()
100
 
101
  tf_transform = transforms.Compose([
102
+ transforms.Resize((PYTORCH_IMG_SIZE, PYTORCH_IMG_SIZE)),
103
  transforms.ToTensor(),
104
  transforms.Normalize(
105
  [0.485, 0.456, 0.406],
 
111
  return _pytorch_model
112
 
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def load_tensorflow():
115
  global _tf_model
116
+ if _tf_model is None:
117
+ import tensorflow as tf
118
+ _tf_model = tf.keras.models.load_model("parfait_model.keras", compile=False)
 
 
 
 
119
  return _tf_model
120
 
121
 
 
158
  elif framework == "tensorflow":
159
  model = load_tensorflow()
160
  arr = np.array(
161
+ pil_img.resize((TF_IMG_SIZE, TF_IMG_SIZE)),
162
  dtype=np.float32
163
+ ) / 255.0
164
  arr = np.expand_dims(arr, axis=0)
165
  probs = model.predict(arr, verbose=0)[0]
166
 
 
187
 
188
  if __name__ == "__main__":
189
  port = int(os.environ.get("PORT", 5000))
190
+ app.run(host="0.0.0.0", port=port, debug=False)