mralamdari commited on
Commit
0e7f880
·
1 Parent(s): acb820c

Delete my_classifier.py

Browse files
Files changed (1) hide show
  1. my_classifier.py +0 -88
my_classifier.py DELETED
@@ -1,88 +0,0 @@
1
- import tensorflow as tf
2
- from tensorflow import keras
3
- from tensorflow.keras import layers
4
- import numpy as np
5
- import cv2
6
-
7
-
8
- class ImageClassifier:
9
- def __init__(self):
10
- self.model = None
11
-
12
- def preprocess_image(self, image):
13
- # Resize the image to (32, 32)
14
- resized_image = cv2.resize(image, (28, 28, 1))
15
-
16
- # # Convert the image to grayscale
17
- # gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
18
-
19
- # # # Normalize the pixel values between 0 and 1
20
- # normalized_image = gray_image.astype("float32") / 255.0
21
-
22
- # # # Transpose the dimensions to match the model's input shape
23
- # transposed_image = np.transpose(normalized_image, (1, 2, 0))
24
-
25
- # # # Expand dimensions to match model input shape (add batch dimension)
26
- # img_array = np.expand_dims(transposed_image, axis=0)
27
- return resized_image
28
-
29
- def load_dataset(self):
30
- # Set up the dataset
31
- (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
32
-
33
- # Normalize pixel values between 0 and 1
34
- x_train = x_train.astype("float32") / 255.0
35
- x_test = x_test.astype("float32") / 255.0
36
-
37
- return (x_train, y_train), (x_test, y_test)
38
-
39
- def build_model(self, x_train):
40
- # Define the model architecture
41
- model = tf.keras.models.Sequential([
42
- tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
43
- tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu'),
44
- tf.keras.layers.BatchNormalization(),
45
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu'),
46
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu'),
47
- tf.keras.layers.BatchNormalization(),
48
- tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same', activation='relu'),
49
- tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same', activation='relu'),
50
- tf.keras.layers.BatchNormalization(),
51
- tf.keras.layers.GlobalAveragePooling2D(),
52
- tf.keras.layers.Dense(10, activation='softmax')
53
- ])
54
-
55
- # Compile the model
56
- optimizer = keras.optimizers.RMSprop(learning_rate=0.001)
57
- model.compile(loss="sparse_categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"])
58
-
59
- self.model = model
60
-
61
- def train_model(self, x_train, y_train, batch_size, epochs, validation_split):
62
- # Train the model
63
- self.model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_split=validation_split)
64
-
65
- def evaluate_model(self, x_test, y_test):
66
- # Evaluate the model on the test set
67
- score = self.model.evaluate(x_test, y_test, verbose=0)
68
- print("Test loss:", score[0])
69
- print("Test accuracy:", score[1])
70
-
71
- def save_model(self, filepath):
72
- # Save the trained model
73
- self.model.save(filepath)
74
-
75
- def load_model(self, filepath):
76
- # Load the trained model
77
- self.model = keras.models.load_model(filepath)
78
-
79
- def classify_image(self, image, top_k=3):
80
- # Preprocess the image
81
- preprocessed_image = self.preprocess_image(image)
82
-
83
- # Perform inference
84
- predicted_probs = self.model.predict(np.array([preprocessed_image]))
85
- top_classes = np.argsort(predicted_probs[0])[-top_k:][::-1]
86
- top_probs = predicted_probs[0][top_classes]
87
-
88
- return top_classes, top_probs