AJain1234 commited on
Commit
26e20fb
·
verified ·
1 Parent(s): dbd520e

Update Model/centroid_app.py

Browse files
Files changed (1) hide show
  1. Model/centroid_app.py +80 -80
Model/centroid_app.py CHANGED
@@ -1,80 +1,80 @@
1
-
2
- import pickle
3
- import random
4
- import numpy as np
5
- import cv2
6
- from tensorflow.keras import models, layers
7
-
8
- # Load the trained model architecture
9
- def create_resnet18():
10
- model = models.Sequential()
11
- model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)))
12
- model.add(layers.MaxPooling2D((2, 2)))
13
- model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
14
- model.add(layers.MaxPooling2D((2, 2)))
15
- model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
16
- model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
17
- model.add(layers.MaxPooling2D((2, 2)))
18
- model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
19
- model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
20
- model.add(layers.MaxPooling2D((2, 2)))
21
- model.add(layers.Flatten())
22
- model.add(layers.Dense(512, activation='relu'))
23
- model.add(layers.Dense(10, activation='softmax'))
24
- return model
25
-
26
- # Load the pretrained weights
27
- def load_pretrained_weights(model, weights_path):
28
- model.load_weights(weights_path)
29
-
30
- # Function to unpickle a file
31
- def unpickle(file):
32
- with open(file, 'rb') as fo:
33
- dict = pickle.load(fo, encoding='bytes')
34
- return dict
35
-
36
- # Function to load images from the unpickled data batch file of a specific class
37
- def load_class_images(class_index, train_batches):
38
- images = []
39
- for batch in train_batches:
40
- if b'data' in batch and b'labels' in batch:
41
- data = batch[b'data']
42
- labels = batch[b'labels']
43
- for i, label in enumerate(labels):
44
- if label == class_index:
45
- img = data[i].reshape(3, 32, 32).transpose(1, 2, 0) # Reshape and transpose the image
46
- images.append(img)
47
- return images
48
-
49
- # Function to calculate the distance of the mean embeddings with a query image
50
- def classify_query(query_image, model, mean_embeddings):
51
- query_embedding = model.predict(np.expand_dims(query_image, axis=0))
52
- distances = [np.linalg.norm(query_embedding.flatten() - mean_embedding) for mean_embedding in mean_embeddings]
53
- predicted_class = np.argmin(distances)
54
- return predicted_class
55
-
56
-
57
- def retrieve(query_image,k=3):
58
- model = create_resnet18()
59
- load_pretrained_weights(model, 'Model\pretrained_model_weights.h5')
60
-
61
- mean_embeddings = pickle.load(open('Model\data\mean_embeddings.pkl', 'rb'))
62
-
63
- # query_image_path = '/content/airplane_8925.png'
64
- # query_image = cv2.imread(query_image_path)
65
- query_image = cv2.resize(query_image, (32, 32)) / 255.0 # Resize and normalize the image
66
-
67
- predicted_class = classify_query(query_image, model, mean_embeddings)
68
- # print("Predicted Class:", predicted_class)
69
-
70
- # Load random images of the predicted class
71
- train_batches = [unpickle(rf"Model\data\data_batch_{i}") for i in range(1,6)]
72
- class_images = load_class_images(predicted_class, train_batches)
73
-
74
- if class_images:
75
- random_images = random.sample(class_images, k) # Select 3 random images
76
- return random_images
77
- else:
78
- print("No images found for the predicted class.")
79
-
80
-
 
1
+
2
+ import pickle
3
+ import random
4
+ import numpy as np
5
+ import cv2
6
+ from tensorflow.keras import models, layers
7
+
8
+ # Load the trained model architecture
9
+ def create_resnet18():
10
+ model = models.Sequential()
11
+ model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)))
12
+ model.add(layers.MaxPooling2D((2, 2)))
13
+ model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
14
+ model.add(layers.MaxPooling2D((2, 2)))
15
+ model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
16
+ model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
17
+ model.add(layers.MaxPooling2D((2, 2)))
18
+ model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
19
+ model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
20
+ model.add(layers.MaxPooling2D((2, 2)))
21
+ model.add(layers.Flatten())
22
+ model.add(layers.Dense(512, activation='relu'))
23
+ model.add(layers.Dense(10, activation='softmax'))
24
+ return model
25
+
26
+ # Load the pretrained weights
27
+ def load_pretrained_weights(model, weights_path):
28
+ model.load_weights(weights_path)
29
+
30
+ # Function to unpickle a file
31
+ def unpickle(file):
32
+ with open(file, 'rb') as fo:
33
+ dict = pickle.load(fo, encoding='bytes')
34
+ return dict
35
+
36
+ # Function to load images from the unpickled data batch file of a specific class
37
+ def load_class_images(class_index, train_batches):
38
+ images = []
39
+ for batch in train_batches:
40
+ if b'data' in batch and b'labels' in batch:
41
+ data = batch[b'data']
42
+ labels = batch[b'labels']
43
+ for i, label in enumerate(labels):
44
+ if label == class_index:
45
+ img = data[i].reshape(3, 32, 32).transpose(1, 2, 0) # Reshape and transpose the image
46
+ images.append(img)
47
+ return images
48
+
49
+ # Function to calculate the distance of the mean embeddings with a query image
50
+ def classify_query(query_image, model, mean_embeddings):
51
+ query_embedding = model.predict(np.expand_dims(query_image, axis=0))
52
+ distances = [np.linalg.norm(query_embedding.flatten() - mean_embedding) for mean_embedding in mean_embeddings]
53
+ predicted_class = np.argmin(distances)
54
+ return predicted_class
55
+
56
+
57
+ def retrieve(query_image,k=3):
58
+ model = create_resnet18()
59
+ load_pretrained_weights(model, 'Model/pretrained_model_weights.h5')
60
+
61
+ mean_embeddings = pickle.load(open('Model/data/mean_embeddings.pkl', 'rb'))
62
+
63
+ # query_image_path = '/content/airplane_8925.png'
64
+ # query_image = cv2.imread(query_image_path)
65
+ query_image = cv2.resize(query_image, (32, 32)) / 255.0 # Resize and normalize the image
66
+
67
+ predicted_class = classify_query(query_image, model, mean_embeddings)
68
+ # print("Predicted Class:", predicted_class)
69
+
70
+ # Load random images of the predicted class
71
+ train_batches = [unpickle(rf"Model/data/data_batch_{i}") for i in range(1,6)]
72
+ class_images = load_class_images(predicted_class, train_batches)
73
+
74
+ if class_images:
75
+ random_images = random.sample(class_images, k) # Select 3 random images
76
+ return random_images
77
+ else:
78
+ print("No images found for the predicted class.")
79
+
80
+