Files changed (1) hide show
  1. app.py +72 -5
app.py CHANGED
@@ -10,6 +10,68 @@ from sklearn.ensemble import RandomForestClassifier
10
  from geopy.distance import geodesic
11
  import json
12
  import csv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Read data
15
  pd.set_option('display.max_columns', None)
@@ -75,12 +137,17 @@ def geo_plot_ml_recommendation():
75
  img = Image.open(file)
76
  st.image(img, caption="Uploaded Image", use_column_width=True)
77
 
78
- # Image Classification Logic (Replace this with your model)
79
- # Placeholder logic: random category
80
- categories = ['Museum', 'University', 'Hotel']
81
- prediction = np.random.choice(categories)
 
 
 
 
82
 
83
- swiped_images = swiped_images.append({'image_category': prediction}, ignore_index=True)
 
84
 
85
  # Enter Current Location
86
  current_latitude = st.number_input("Enter current latitude", value=54.898291)
 
10
  from geopy.distance import geodesic
11
  import json
12
  import csv
13
+ import os
14
+ import torch
15
+ import torch.nn as nn
16
+ import torch.nn.functional as F
17
+ from torch_optimizer import Lookahead
18
+ from torchvision import transforms
19
+
20
+ class GSNet(nn.Module):
21
+ def __init__(self, nclasses):
22
+ super(GSNet, self).__init__()
23
+ self.conv1 = nn.Conv2d(3, 100, 7)
24
+ self.conv1_bn = nn.BatchNorm2d(100)
25
+ self.pool = nn.MaxPool2d(2, 2)
26
+
27
+ self.conv2 = nn.Conv2d(100, 150, 5)
28
+ self.conv2_bn = nn.BatchNorm2d(150)
29
+
30
+ self.conv3 = nn.Conv2d(150, 250, 3)
31
+ self.conv3_bn = nn.BatchNorm2d(250)
32
+
33
+ self.fc1 = nn.Linear(250 * 3 * 3, 350)
34
+ self.fc1_bn = nn.BatchNorm1d(350)
35
+
36
+ self.fc2 = nn.Linear(350, 400)
37
+ self.fc2_bn = nn.BatchNorm1d(400)
38
+
39
+ self.fc3 = nn.Linear(350, 450)
40
+ self.fc3_bn = nn.BatchNorm1d(450)
41
+
42
+ self.fc4 = nn.Linear(450, nclasses)
43
+
44
+ self.dropout = nn.Dropout(p=0.6)
45
+
46
+ def forward(self, x):
47
+ # convolutional layer
48
+ x = self.pool(F.elu(self.conv1(x)))
49
+ x = self.dropout(self.conv1_bn(x))
50
+
51
+ # convolutional layer
52
+ x = self.pool(F.elu(self.conv2(x)))
53
+ x = self.dropout(self.conv2_bn(x))
54
+
55
+ # convolutional layer
56
+ x = self.pool(F.elu(self.conv3(x)))
57
+ x = self.dropout(self.conv3_bn(x))
58
+
59
+ #fully connected layer
60
+ x = x.view(x.size(0), -1)
61
+ x = F.elu(self.fc1(x))
62
+
63
+ #fully connected layer
64
+ x = self.dropout(self.fc1_bn(x))
65
+ x = self.fc3(x)
66
+ return x
67
+
68
+
69
+ nclasses = 9
70
+ my_pretrained_model = GSNet(nclasses)
71
+ model_path = "CI_model.pth"
72
+ my_pretrained_model.load_state_dict(torch.load(model_path))
73
+ my_pretrained_model.eval()
74
+
75
 
76
  # Read data
77
  pd.set_option('display.max_columns', None)
 
137
  img = Image.open(file)
138
  st.image(img, caption="Uploaded Image", use_column_width=True)
139
 
140
+ # Image Classification using Pre-trained Model
141
+ img_array = np.array(img)
142
+ img_tensor = transform(Image.fromarray(img_array)).unsqueeze(0)
143
+ img_tensor = img_tensor.to(device)
144
+
145
+ with torch.no_grad():
146
+ my_pretrained_model.eval()
147
+ logits = my_pretrained_model(img_tensor)
148
 
149
+ predicted_category = label_encoder.classes_[logits.argmax()]
150
+ swiped_images = swiped_images.append({'image_category': predicted_category}, ignore_index=True)
151
 
152
  # Enter Current Location
153
  current_latitude = st.number_input("Enter current latitude", value=54.898291)