LuisDarioHinojosa commited on
Commit
d0ce7c3
·
1 Parent(s): 9a8437d

initial commit

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ dogs_emotion_model_weights.h5 filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ from timeit import default_timer as timer
4
+ import cv2 as cv
5
+ import gradio as gr
6
+ from model_instance_function import get_pretrained_dog_emotion_classifier
7
+
8
+
9
+ # normalize function
10
+ def image_preprocessing(img):
11
+ img = np.array(img)
12
+ img = cv.resize(img,(224,224))
13
+ img = img.reshape(1,224,224,3)
14
+ return img / 255.0
15
+
16
+ # instance the model
17
+ model = get_pretrained_dog_emotion_classifier()
18
+
19
+ # gradio predict function
20
+ def predict(img):
21
+
22
+ # class to map the indices to the classes
23
+ class_2_index = {0: 'happy', 1: 'sad'}
24
+
25
+ # measure execution time
26
+ start_time = timer()
27
+
28
+ # preprocess the image
29
+ img = image_preprocessing(img)
30
+
31
+ # make a prediction (prob of sad dog)
32
+ pred_probability = model.predict(img)[0]
33
+
34
+ # convert to an index
35
+ pred_index = 1 if pred_probability > 0.5 else 0
36
+
37
+ # label
38
+ pred_label = class_2_index[pred_index]
39
+
40
+ end_time = timer()
41
+ total_time = end_time - start_time
42
+
43
+ return pred_probability, pred_label,round(total_time,5)
44
+
45
+
46
+ title = "Dog Emotions Vision Classifier"
47
+ description = "A vision classifier that distinguishes between sad and happy dogs."
48
+ article = "The model was trained in the [Dogs Emotions Dataset](https://huggingface.co/datasets/Q-b1t/Dogs_Emotions_Dataset) using the pretrained convolutional blocks of the VGG16 architecture and a custom classifier."
49
+
50
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
51
+
52
+ demo = gr.Interface(
53
+ fn = predict,
54
+ inputs = gr.Image(type = "pil"),
55
+ outputs = [gr.Number(label = "Probability of a sad dog"),gr.Textbox(max_lines = 2,label = "Most likely class"),gr.Number(label = "Prediction time (s)")],
56
+ examples = example_list,
57
+ title = title,
58
+ description = description,
59
+ article = article
60
+ )
61
+
62
+ demo.launch()
63
+
dogs_emotion_model_weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a371a36b8d1a9fec0cefe589899e8b26b1e5fd93a958a33b0c61f1752616a24c
3
+ size 125312320
examples/dog_happy_v0egS85G9RcCY5opp5uwNJzElpSZgr529.jpg ADDED
examples/dog_happy_yB059bZIQjikflYlf3RWAgdhGhX4cH761.jpg ADDED
examples/dog_sad_v4kht5PhHzFyHvbjuVsno4HkHzgi49559.jpg ADDED
examples/dog_sad_vbYCFnsQqu3WkAbNy67m9BdSMGBeNm832.jpg ADDED
model_instance_function.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow.keras.models import Model,Sequential
2
+ from tensorflow.keras.layers import Dense, Flatten, Dropout
3
+ from tensorflow.keras.applications import VGG16
4
+ from tensorflow.keras.regularizers import L2
5
+
6
+ def get_pretrained_dog_emotion_classifier(weights_path = "dogs_emotion_model_weights.h5"):
7
+ # images input shape
8
+ MODEL_INPUT_SHAPE = (224,224,3)
9
+
10
+ # instance the pretrained convolutional blocks
11
+ pretrained_vgg_model = VGG16(include_top = False, input_shape = MODEL_INPUT_SHAPE)
12
+
13
+ # freeze the first four pretrained layer
14
+ target_freeze_blocks = ["block1","block2","block3","block4"]
15
+ for layer in pretrained_vgg_model.layers:
16
+ if layer.name.split("_")[0] in target_freeze_blocks:
17
+ layer.trainable = False
18
+
19
+ # create the model's classifier
20
+ classifier = Sequential(
21
+ [
22
+ Flatten(),
23
+ Dense(32,activation = "relu",name = "classifier_dense1"),
24
+ Dropout(0.2,name = "classifier_dropout1"),
25
+ Dense(64,activation = "relu",kernel_regularizer = L2(),name = "classifier_dense2"),
26
+ Dense(64,activation = "relu",name = "classifier_dense3"),
27
+ Dropout(0.2,name = "classifier_dropout2"),
28
+ Dense(1,activation = "sigmoid",name = "classifier_dense4")
29
+ ],name = "classifier"
30
+ )
31
+
32
+ # connect the two models
33
+ output = classifier(pretrained_vgg_model.layers[-1].output)
34
+ model = Model(inputs = pretrained_vgg_model.layers[0].input,outputs = output)
35
+
36
+ # load the model_weights
37
+ model.load_weights(weights_path)
38
+
39
+ return model
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow==2.12.0
2
+ gradio==3.31.0
3
+ numpy==1.22.4
4
+ opencv-python==4.7.0.68