ana-bernal commited on
Commit
8f578aa
·
1 Parent(s): 2e1c089

updated app

Browse files
Files changed (2) hide show
  1. app.py +22 -29
  2. requirements.txt +1 -2
app.py CHANGED
@@ -11,6 +11,7 @@ import tensorflow as tf
11
  from tensorflow import keras
12
  from tensorflow.keras import layers
13
  from keras.applications.vgg16 import preprocess_input
 
14
 
15
  # Image processing
16
  import PIL
@@ -23,19 +24,19 @@ filename_model_VGG16 = 'vgg16_best.keras'
23
  example_images_path = './example_images'
24
 
25
  # Loading model
26
- model = keras.models.load_model(path + filename_model_VGG16)
27
 
28
  # Defining parameters
29
- breed_names_norm = [Chihuahua, papillon, beagle,
30
- Yorkshire_terrier, Australian_terrier,
31
- Scotch_terrier, golden_retriever,
32
- malinois, kelpie, Doberman, miniature_pinscher,
33
- Great_Dane, Pomeranian, standard_poodle, Mexican_hairless]
34
 
35
 
36
  # Importing stopwords
37
- with open('./stopwords/stopwords.txt') as file:
38
- my_stopwords = {line.rstrip() for line in file}
39
 
40
  # Function definitions
41
 
@@ -51,7 +52,7 @@ def load_img_path(img_path, target_size, show=False):
51
 
52
  return img
53
 
54
- def predict(model, img, show=False):
55
  """
56
  Returns a dictionnary: predicted_breeds, where the
57
  keys are [1,2,3] for the first, second and third more probable
@@ -61,36 +62,28 @@ def predict(model, img, show=False):
61
  Parameters:
62
  img: returned by the function load_img_path
63
  """
64
- img_array = keras.preprocessing.image.img_to_array(img)
65
  img_array = tf.expand_dims(img_array, 0) # Creates a batch axis
66
 
67
- predictions = model.predict(img_array, verbose=0)
68
- scores = predictions[0]
69
 
70
- # Keep the 3 more probable breeds
71
- predicted_breeds = {}
72
- for i in [1,2,3]:
73
- idx = scores.argsort()[-i] # First breed is the last proba when sorted
74
- name = breed_names_norm[idx]
75
- confidence = round(scores[idx]*100,2)
76
- predicted_breeds[i] = {'idx':idx,'name':name,'confidence':confidence}
77
-
78
- return predicted_breeds
79
 
80
  # --------------------------------------------------
81
 
82
  examples = [
83
- ["Jquery/Javascript Opacity animation with scroll <p>I'm looking to change the opacity on an object (and have the transition be animated) based on a users scroll.\nexample(http://davegamache.com/)</p>\n\n<p>I've searched everywhere\nlike here, but it ends up pointing me to the waypoints plugin (http://stackoverflow.com/questions/6316757/opacity-based-on-scroll-position)</p>\n\n<p>I've implemented the [waypoints][1] plugin and have the object fading once it's higher than 100px. [Using the offet attribute] but would like to basically control the opacity of an object and have the animation be visible like the above example.</p>\n\n<p>I've searched all over- this is my last resort.\nAny help is greatly appreciated.</p>\n"],
84
- ['Setting cross-domain cookies in Safari <p>I have to call domain A.com (which sets the cookies with http) from domain B.com.\nAll I do on domain B.com is (javascript): </p>\n\n<pre><code>var head = document.getElementsByTagName("head")[0];\nvar script = document.createElement("script");\nscript.src = "A.com/setCookie?cache=1231213123";\nhead.appendChild(script);\n</code></pre>\n\n<p>This sets the cookie on A.com on every browser I\'ve tested, except Safari.\nAmazingly this works in IE6, even without the P3P headers.</p>\n\n<p>Is there any way to make this work in Safari?</p>\n'],
85
- ['Database migrations for SQL Server <p>I need a database migration framework for SQL Server, capable of managing both schema changes and data migrations.</p>\n\n<p>I guess I am looking for something similar to django\'s <a href="http://south.aeracode.org/" rel="noreferrer">South</a> framework here.</p>\n\n<p>Given the fact that South is tightly coupled with django\'s ORM, and the fact that there\'s so many ORMs for SQL Server I guess having just a generic migration framework, enabling you to write and execute in controlled and sequential manner SQL data/schema change scripts should be sufficient.</p>\n'],
 
86
  ]
87
 
88
- demo = gr.Interface(fn=tag_suggestion,
89
- inputs="text",
90
- outputs=["text"],
91
  examples=examples)
92
 
93
 
94
  if __name__ == "__main__":
95
- demo.launch()
96
-
 
11
  from tensorflow import keras
12
  from tensorflow.keras import layers
13
  from keras.applications.vgg16 import preprocess_input
14
+ from huggingface_hub import from_pretrained_keras
15
 
16
  # Image processing
17
  import PIL
 
24
  example_images_path = './example_images'
25
 
26
  # Loading model
27
+ model = from_pretrained_keras('ana-bernal/keras_15_dog_breed_eff')
28
 
29
  # Defining parameters
30
+ breed_names_norm = ['Chihuahua', 'papillon', 'beagle',
31
+ 'Yorkshire_terrier', 'Australian_terrier',
32
+ 'Scotch_terrier', 'golden_retriever',
33
+ 'malinois', 'kelpie', 'Doberman', 'miniature_pinscher',
34
+ 'Great_Dane', 'Pomeranian', 'standard_poodle', 'Mexican_hairless']
35
 
36
 
37
  # Importing stopwords
38
+ # with open('./stopwords/stopwords.txt') as file:
39
+ # my_stopwords = {line.rstrip() for line in file}
40
 
41
  # Function definitions
42
 
 
52
 
53
  return img
54
 
55
+ def classify_image(inp):
56
  """
57
  Returns a dictionnary: predicted_breeds, where the
58
  keys are [1,2,3] for the first, second and third more probable
 
62
  Parameters:
63
  img: returned by the function load_img_path
64
  """
65
+ img_array = keras.preprocessing.image.img_to_array(inp)
66
  img_array = tf.expand_dims(img_array, 0) # Creates a batch axis
67
 
68
+ predictions = model.predict(img_array, verbose=0).flatten()
69
+ confidences = {breed_names_norm[i]: float(predictions[i]) for i in range(15)}
70
 
71
+ return confidences
 
 
 
 
 
 
 
 
72
 
73
  # --------------------------------------------------
74
 
75
  examples = [
76
+ ['example_images/01_test.jpg'],
77
+ ['example_images/02_test.jpg'],
78
+ ['example_images/03_test.jpg'],
79
+ ['example_images/04_test.jpg'],
80
  ]
81
 
82
+ demo = gr.Interface(fn=classify_image,
83
+ inputs=gr.Image(shape=(180, 180)),
84
+ outputs=gr.Label(num_top_classes=3),
85
  examples=examples)
86
 
87
 
88
  if __name__ == "__main__":
89
+ demo.launch()
 
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  gradio
2
  joblib
3
  tensorflow
4
- tensorflow_hub
5
- scikit-learn
 
1
  gradio
2
  joblib
3
  tensorflow
4
+ tensorflow_hub