cyberai-1 commited on
Commit
833f32e
·
1 Parent(s): a018d0d
Files changed (1) hide show
  1. app.py +61 -25
app.py CHANGED
@@ -8,6 +8,11 @@ import numpy as np
8
  from flask import Flask, jsonify, render_template, request
9
  from PIL import Image
10
 
 
 
 
 
 
11
  app = Flask(__name__)
12
 
13
  CLASSES = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
@@ -23,31 +28,62 @@ def load_pytorch():
23
  if _pytorch_model is not None:
24
  return _pytorch_model
25
 
26
- import torch
27
- import torch.nn as nn
28
- import torch.nn.functional as F
29
- from torchvision import transforms
30
-
31
- class CNN_Torch(nn.Module):
32
- def __init__(self, num_classes=6):
33
- super().__init__()
34
- self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
35
- self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
36
- self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
37
- self.conv3_drop = nn.Dropout2d(p=0.25)
38
- self.pool = nn.MaxPool2d(2, 2)
39
- self.fc1 = nn.Linear(128 * 18 * 18, 256)
40
- self.fc2 = nn.Linear(256, num_classes)
41
-
42
- def forward(self, x):
43
- x = self.pool(F.relu(self.conv1(x)))
44
- x = self.pool(F.relu(self.conv2(x)))
45
- x = self.pool(F.relu(self.conv3_drop(self.conv3(x))))
46
- x = x.view(-1, 128 * 18 * 18)
47
- x = F.relu(self.fc1(x))
48
- x = F.dropout(x, training=self.training)
49
- x = self.fc2(x)
50
- return F.log_softmax(x, dim=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
53
  model = CNN_Torch(6).to(device)
 
8
  from flask import Flask, jsonify, render_template, request
9
  from PIL import Image
10
 
11
+ import torch
12
+ import torch.nn as nn
13
+ import torch.nn.functional as F
14
+ from torchvision import transforms
15
+
16
  app = Flask(__name__)
17
 
18
  CLASSES = ["buildings", "forest", "glacier", "mountain", "sea", "street"]
 
28
  if _pytorch_model is not None:
29
  return _pytorch_model
30
 
31
+
32
+ class CNN_Torch(nn.Module):
33
+ """
34
+ CNN PyTorch allégé pour images RGB (3 canaux).
35
+ Entrée : (B, 3, 150, 150)
36
+ Sortie : (B, num_classes) — log-softmax
37
+
38
+ Architecture :
39
+ Block 1 : Conv2d(332) + BN + ReLU + MaxPool2d(2) → 75×75
40
+ Block 2 : Conv2d(3264) + BN + ReLU + MaxPool2d(2) + Drop2d → 37×37
41
+ Block 3 : Conv2d(64128)+ BN + ReLU + MaxPool2d(2) + Drop2d → 18×18
42
+ GAP : AdaptiveAvgPool2d(1) → (B,128)
43
+ Head : Linear(128→256) + ReLU + Dropout + Linear(256→C)
44
+
45
+ Paramètre `dropout` contrôlé depuis l'extérieur → utilisé dans le CV.
46
+ """
47
+ def __init__(self, num_classes: int = 6, dropout: float = 0.5):
48
+ super().__init__()
49
+
50
+ self.features = nn.Sequential(
51
+ # Block 1 150 75
52
+ nn.Conv2d(3, 32, kernel_size=3, padding=1, bias=False),
53
+ nn.BatchNorm2d(32),
54
+ nn.ReLU(inplace=True),
55
+ nn.MaxPool2d(2),
56
+
57
+ # Block 2 — 75 → 37
58
+ nn.Conv2d(32, 64, kernel_size=3, padding=1, bias=False),
59
+ nn.BatchNorm2d(64),
60
+ nn.ReLU(inplace=True),
61
+ nn.MaxPool2d(2),
62
+ nn.Dropout2d(0.1),
63
+
64
+ # Block 3 — 37 → 18
65
+ nn.Conv2d(64, 128, kernel_size=3, padding=1, bias=False),
66
+ nn.BatchNorm2d(128),
67
+ nn.ReLU(inplace=True),
68
+ nn.MaxPool2d(2),
69
+ nn.Dropout2d(0.2),
70
+ )
71
+
72
+ self.gap = nn.AdaptiveAvgPool2d(1) # (B, 128, 18, 18) → (B, 128, 1, 1)
73
+
74
+ self.classifier = nn.Sequential(
75
+ nn.Flatten(),
76
+ nn.Linear(128, 256),
77
+ nn.ReLU(inplace=True),
78
+ nn.Dropout(dropout),
79
+ nn.Linear(256, num_classes),
80
+ )
81
+
82
+ def forward(self, x):
83
+ x = self.features(x)
84
+ x = self.gap(x)
85
+ x = self.classifier(x)
86
+ return F.log_softmax(x, dim=1)
87
 
88
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
89
  model = CNN_Torch(6).to(device)