Spaces:
Sleeping
Sleeping
Refactor for cleanup and add more samples
Browse files
app.py
CHANGED
@@ -1,71 +1,28 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
import gradio as gr
|
3 |
-
import coremltools as ct
|
4 |
-
import numpy as np
|
5 |
-
import requests
|
6 |
-
import huggingface_hub as hf
|
7 |
-
from huggingface_hub import hf_hub_download
|
8 |
-
from huggingface_hub import snapshot_download
|
9 |
-
from huggingface_hub import login
|
10 |
import os
|
11 |
-
import
|
|
|
12 |
|
13 |
-
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
#extractor2_path = hf_hub_download(repo_id="crossprism/efficientnetv221k-M", filename="efficientnetV2M21kExtractor.mlmodel", use_auth_token = read_key)
|
18 |
-
extractor_path = snapshot_download(repo_id="crossprism/efficientnetv2-21k-fv-m", use_auth_token = read_key)
|
19 |
-
classifier_path = hf_hub_download(repo_id="crossprism/tesla_sentry_dings", filename="tesla_sentry_door_ding.mlpackage/Data/com.apple.CoreML/tesla_door_dings.mlmodel", use_auth_token = read_key)
|
20 |
-
|
21 |
-
|
22 |
-
print(f"Loading extractor...{extractor_path}")
|
23 |
-
extractor = tf.saved_model.load(extractor_path+"/efficientnetv2-21k-fv-m")
|
24 |
-
|
25 |
-
#extractor2 = ct.models.MLModel(extractor2_path)
|
26 |
-
print(f"Loading classifier...{classifier_path}")
|
27 |
-
classifier = ct.models.MLModel(classifier_path)
|
28 |
-
spec = classifier.get_spec()
|
29 |
-
labels = spec.neuralNetworkClassifier.stringClassLabels.vector
|
30 |
-
image = PIL.Image.open('test.jpg')
|
31 |
-
|
32 |
-
def makeKerasModel(labels, coreml_classifier):
|
33 |
-
input = tf.keras.Input(shape = (1280))
|
34 |
-
x = tf.keras.layers.Dense(len(labels), activation = "sigmoid")(input)
|
35 |
-
model = tf.keras.Model(input,x, trainable = False)
|
36 |
-
weights = np.array(coreml_classifier.layers[0].innerProduct.weights.floatValue)
|
37 |
-
weights = weights.reshape((len(labels),1280))
|
38 |
-
#weights = weights.reshape((1280,len(labels)))
|
39 |
-
weights = weights.T
|
40 |
-
bias = np.array(coreml_classifier.layers[0].innerProduct.bias.floatValue)
|
41 |
-
model.set_weights([weights,bias])
|
42 |
-
return model
|
43 |
-
|
44 |
-
#Only MacOS can run inference on CoreML models. Convert it to tensorflow to match the tf feature extractor
|
45 |
-
tf_classifier = makeKerasModel(labels, spec.neuralNetworkClassifier)
|
46 |
|
47 |
def classify_image(image):
|
48 |
resized = image.resize((480,480))
|
49 |
-
|
50 |
-
|
51 |
-
features = extractor.signatures['serving_default'](image)
|
52 |
-
#features2 = extractor2.predict({"image":resized})
|
53 |
-
#print(features)
|
54 |
-
#print(features2)
|
55 |
-
#features2 = features2["Identity"]
|
56 |
-
#isDing = classifier.predict({"features":features2[0]})
|
57 |
-
#isDing = isDing["Identity"]
|
58 |
-
input = {"input_1":features["output_1"]}
|
59 |
-
p = tf_classifier.predict(input)
|
60 |
-
#print(p)
|
61 |
-
isDing = {}
|
62 |
-
for i,label in enumerate(labels):
|
63 |
-
isDing[label] = p[i]
|
64 |
-
#print(isDing)
|
65 |
-
return {'ding': isDing["ding"]}
|
66 |
-
#classify_image(image)
|
67 |
image = gr.Image(type='pil')
|
68 |
label = gr.Label(num_top_classes=3)
|
69 |
|
70 |
-
gr.Interface(fn=classify_image, inputs=image, outputs=label, examples = [["test.jpg","test2.jpg","test3.jpg"]]).launch()
|
71 |
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
import platform
|
4 |
+
from helper import CoreMLPipeline
|
5 |
|
6 |
+
force_tf = os.environ.get('FORCE_TF', False)
|
7 |
+
auth_key = os.environ.get('HF_TOKEN', True)
|
8 |
|
9 |
+
config = { "coreml_extractor_repoid":"crossprism/efficientnetv2-21k-fv-m",
|
10 |
+
"coreml_extractor_path":"efficientnetV2M21kExtractor.mlmodel",
|
11 |
+
"tf_extractor_repoid":"crossprism/efficientnetv2-21k-fv-m-tf",
|
12 |
+
"tf_extractor_path":"efficientnetv2-21k-fv-m",
|
13 |
+
"coreml_classifier_repoid":"crossprism/tesla_sentry_dings",
|
14 |
+
"coreml_classifier_path":"tesla_sentry_door_ding.mlpackage/Data/com.apple.CoreML/tesla_door_dings.mlmodel"
|
15 |
+
}
|
16 |
+
use_tf = force_tf or (platform.system() != 'Darwin')
|
17 |
|
18 |
+
helper = CoreMLPipeline(config, auth_key, use_tf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def classify_image(image):
|
21 |
resized = image.resize((480,480))
|
22 |
+
return helper.classify(resized)
|
23 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
image = gr.Image(type='pil')
|
25 |
label = gr.Label(num_top_classes=3)
|
26 |
|
27 |
+
gr.Interface(fn=classify_image, inputs=image, outputs=label, examples = [["test.jpg"],["test2.jpg"],["test3.jpg"]]).launch()
|
28 |
|
helper.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import coremltools as ct
|
3 |
+
import numpy as np
|
4 |
+
import PIL
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
from huggingface_hub import snapshot_download
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Helper class to extract features from one model, and then feed those features into a classification head
|
10 |
+
# Because coremltools will only perform inference on OSX, an alternative tensorflow inference pipeline uses
|
11 |
+
# a tensorflow feature extractor and feeds the features into a dynamically created keras model based on the coreml classification head.
|
12 |
+
class CoreMLPipeline:
|
13 |
+
def __init__(self, config, auth_key, use_tf):
|
14 |
+
self.config = config
|
15 |
+
self.use_tf = use_tf
|
16 |
+
if use_tf:
|
17 |
+
extractor_path = snapshot_download(repo_id=config["tf_extractor_repoid"], use_auth_token = auth_key)
|
18 |
+
else:
|
19 |
+
extractor_path = hf_hub_download(repo_id=config["coreml_extractor_repoid"],
|
20 |
+
filename=config["coreml_extractor_path"], use_auth_token = auth_key)
|
21 |
+
|
22 |
+
classifier_path = hf_hub_download(repo_id=config["coreml_classifier_repoid"], filename=config["coreml_classifier_path"],
|
23 |
+
use_auth_token = auth_key)
|
24 |
+
|
25 |
+
print(f"Loading extractor...{extractor_path}")
|
26 |
+
if use_tf:
|
27 |
+
self.extractor = tf.saved_model.load(os.path.join(extractor_path, config["tf_extractor_path"]))
|
28 |
+
else:
|
29 |
+
self.extractor = ct.models.MLModel(extractor_path)
|
30 |
+
|
31 |
+
print(f"Loading classifier...{classifier_path}")
|
32 |
+
self.classifier = ct.models.MLModel(classifier_path)
|
33 |
+
|
34 |
+
if use_tf:
|
35 |
+
self.make_keras_model()
|
36 |
+
|
37 |
+
#Only MacOS can run inference on CoreML models. Convert it to tensorflow to match the tf feature extractor
|
38 |
+
def make_keras_model(self):
|
39 |
+
spec = self.classifier.get_spec()
|
40 |
+
nnClassifier = spec.neuralNetworkClassifier
|
41 |
+
labels = nnClassifier.stringClassLabels.vector
|
42 |
+
input = tf.keras.Input(shape = (1280))
|
43 |
+
activation = "sigmoid" if len(labels) == 1 else "softmax"
|
44 |
+
x = tf.keras.layers.Dense(len(labels), activation = activation)(input)
|
45 |
+
model = tf.keras.Model(input,x, trainable = False)
|
46 |
+
weights = np.array(nnClassifier.layers[0].innerProduct.weights.floatValue)
|
47 |
+
weights = weights.reshape((len(labels),1280))
|
48 |
+
weights = weights.T
|
49 |
+
bias = np.array(nnClassifier.layers[0].innerProduct.bias.floatValue)
|
50 |
+
model.set_weights([weights,bias])
|
51 |
+
self.tf_model = model
|
52 |
+
self.labels = labels
|
53 |
+
|
54 |
+
def classify(self,resized):
|
55 |
+
if self.use_tf:
|
56 |
+
image = tf.image.convert_image_dtype(resized, tf.float32)
|
57 |
+
image = tf.expand_dims(image, 0)
|
58 |
+
features = self.extractor.signatures['serving_default'](image)
|
59 |
+
input = {"input_1":features["output_1"]}
|
60 |
+
output = self.tf_model.predict(input)
|
61 |
+
results = {}
|
62 |
+
for i,label in enumerate(self.labels):
|
63 |
+
results[label] = output[i]
|
64 |
+
else:
|
65 |
+
features = self.extractor.predict({"image":resized})
|
66 |
+
features = features["Identity"]
|
67 |
+
output = self.classifier.predict({"features":features[0]})
|
68 |
+
results = output["Identity"]
|
69 |
+
return results
|
70 |
+
|