sweetfelinity commited on
Commit
0935054
·
verified ·
1 Parent(s): 5a99680

Upload 18 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ CatClassifier.keras filter=lfs diff=lfs merge=lfs -text
CatBreedClassifier.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras import layers, models
3
+ import random
4
+
5
+ # Dataset used for training is a combination of the following datasets:
6
+ # Oxford IIIT Cats: https://www.kaggle.com/datasets/imbikramsaha/cat-breeds
7
+ # CatBreedsRefined-7k: https://www.kaggle.com/datasets/doctrinek/catbreedsrefined-7k
8
+
9
+ images_path = "CatsDataset"
10
+ batch_size = 32
11
+ img_size = 400 # Images will be converted to 400 x 400 before input into the model
12
+ seed = random.randrange(1, 100) # Or set manually
13
+ test_split = 0.2
14
+
15
+ print("Seed:", seed)
16
+
17
+ # Load dataset and split into test and train sets
18
+ train_set = tf.keras.utils.image_dataset_from_directory(
19
+ images_path,
20
+ validation_split=test_split,
21
+ subset="training",
22
+ seed=seed,
23
+ image_size=(img_size, img_size),
24
+ batch_size=batch_size
25
+ )
26
+
27
+ test_set = tf.keras.utils.image_dataset_from_directory(
28
+ images_path,
29
+ validation_split=test_split,
30
+ subset="validation",
31
+ seed=seed,
32
+ image_size=(img_size, img_size),
33
+ batch_size=batch_size
34
+ )
35
+
36
+ # Cache dataset to disk
37
+ train_set = train_set.cache().prefetch(buffer_size=tf.data.AUTOTUNE)
38
+ test_set = test_set.cache().prefetch(buffer_size=tf.data.AUTOTUNE)
39
+
40
+ num_classes = 12
41
+
42
+ # CNN Model
43
+ model = models.Sequential()
44
+ model.add(layers.RandomFlip("horizontal_and_vertical"))
45
+ model.add(layers.RandomRotation(0.2))
46
+ model.add(layers.RandomZoom((0, 0.2)))
47
+ model.add(layers.Rescaling(1./255))
48
+
49
+ model.add(layers.Conv2D(8, 3, activation="relu"))
50
+ model.add(layers.BatchNormalization())
51
+ model.add(layers.MaxPooling2D())
52
+
53
+ model.add(layers.Conv2D(16, 3, activation="relu"))
54
+ model.add(layers.BatchNormalization())
55
+ model.add(layers.MaxPooling2D())
56
+
57
+ model.add(layers.Conv2D(32, 3, activation="relu"))
58
+ model.add(layers.BatchNormalization())
59
+ model.add(layers.MaxPooling2D())
60
+
61
+ model.add(layers.Conv2D(64, 3, activation="relu"))
62
+ model.add(layers.BatchNormalization())
63
+ model.add(layers.MaxPooling2D())
64
+
65
+ model.add(layers.Conv2D(92, 3, activation="relu"))
66
+ model.add(layers.BatchNormalization())
67
+ model.add(layers.MaxPooling2D())
68
+
69
+ model.add(layers.BatchNormalization())
70
+
71
+ model.add(layers.Flatten())
72
+
73
+ model.add(layers.Dense(1024, activation="relu"))
74
+ model.add(layers.Dropout(0.5))
75
+ model.add(layers.Dense(512, activation="relu"))
76
+ model.add(layers.Dense(num_classes, activation="softmax"))
77
+
78
+ model.compile(optimizer="adam",
79
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(),
80
+ metrics=["accuracy"]
81
+ )
82
+
83
+ num_epochs = 50
84
+
85
+ # Save checkpoints after every 5 epochs
86
+ cp_callback = tf.keras.callbacks.ModelCheckpoint(
87
+ filepath="Checkpoint/cp.ckpt",
88
+ save_weights_only=True,
89
+ verbose=1,
90
+ save_freq="epoch",
91
+ period=5)
92
+
93
+ # Train model
94
+ model.fit(
95
+ train_set,
96
+ validation_data=test_set,
97
+ epochs=num_epochs,
98
+ callbacks=[cp_callback]
99
+ )
100
+
101
+ # Save model to disk
102
+ model.save("CatClassifier.keras")
103
+ model.save_weights('CatClassifierWeights.h5')
104
+ print("Model saved.")
105
+
106
+ # Load full model: model = tf.keras.models.load_model("CatClassifier.keras")
107
+ # Load model weights: Create model architecture as above then call model.load_weights("CatClassifierWeights.h5")
CatClassifier.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ed219bc27a1d57ddd90410e11587b204f9ec859b600092f86148b5a5590f1030
3
+ size 120515920
GradioApp.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import gradio as gr
3
+ import numpy as np
4
+ import cv2
5
+ import os
6
+
7
+ classes = ["Abyssinian", "Bengal", "Birman", "Bombay", "British Shorthair", "Egyptian Mau", "Maine Coon", "Persian", "Ragdoll", "Russian Blue", "Siamese", "Sphynx"]
8
+ example_images = ["examples/" + f for f in os.listdir("examples")]
9
+
10
+ img_size = 400
11
+ model = tf.keras.models.load_model("CatClassifier.keras")
12
+
13
+ def model_predict(image):
14
+ image = cv2.resize(image, (img_size, img_size))
15
+ image = np.expand_dims(image, axis=0)
16
+
17
+ predictions = model.predict(image)
18
+ predictions = predictions[0]
19
+
20
+ predicted_class_index = np.argmax(predictions)
21
+ predicted_class = classes[predicted_class_index]
22
+ pred_dict = {}
23
+
24
+ for i in range(len(classes)):
25
+ pred_dict[classes[i]] = predictions[i]
26
+
27
+ return predicted_class, pred_dict
28
+
29
+
30
+ def predict_breed(image):
31
+ if image is None:
32
+ return "Please attach an image first!", None
33
+
34
+ return model_predict(image)
35
+
36
+ with gr.Blocks() as demo:
37
+ with gr.Row():
38
+ with gr.Column():
39
+ image_input = gr.Image(label="Cat Image")
40
+ run_button = gr.Button(variant="primary")
41
+ examples = gr.Examples(example_images,inputs=image_input)
42
+ with gr.Column():
43
+ breed_output = gr.Text(label="Predicted Breed", interactive=False)
44
+ predict_labels = gr.Label(label="Class Probabilties")
45
+
46
+ run_button.click(fn=predict_breed, inputs=image_input, outputs=[breed_output, predict_labels])
47
+
48
+ if __name__ == "__main__":
49
+ demo.launch()
examples/RoyalNefertt_Serket_of_AchetAton.jpg ADDED
examples/animal-7242209_1920.jpg ADDED
examples/bengal-6003100_1920.jpg ADDED
examples/cat-8561309_1280.jpg ADDED
examples/cats-abyssinians-4915972_1920.jpg ADDED
examples/cats-abyssinians-4915993_1920.jpg ADDED
examples/depositphotos_347535116-stock-photo-bombay-black-cat-portrait-yellow.jpg ADDED
examples/istockphoto-1031592516-612x612.jpg ADDED
examples/istockphoto-1250476831-612x612.jpg ADDED
examples/istockphoto-1304538926-612x612.jpg ADDED
examples/istockphoto-140469307-612x612.jpg ADDED
examples/istockphoto-1443769180-612x612.jpg ADDED
examples/istockphoto-511011344-612x612.jpg ADDED
examples/kittens-2136803_1920.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ numpy
4
+ cv2