Spaces:
Sleeping
Sleeping
adjust to perform inference with tensorflow
Browse files- app.py +44 -11
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import coremltools as ct
|
3 |
import numpy as np
|
4 |
import requests
|
5 |
import huggingface_hub as hf
|
6 |
from huggingface_hub import hf_hub_download
|
|
|
7 |
from huggingface_hub import login
|
8 |
import os
|
9 |
import PIL
|
@@ -11,26 +13,57 @@ import PIL
|
|
11 |
#login()
|
12 |
|
13 |
|
14 |
-
read_key = os.environ.get('HF_TOKEN',
|
15 |
-
|
|
|
16 |
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)
|
17 |
|
18 |
|
19 |
print(f"Loading extractor...{extractor_path}")
|
20 |
-
extractor =
|
|
|
|
|
21 |
print(f"Loading classifier...{classifier_path}")
|
22 |
classifier = ct.models.MLModel(classifier_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def classify_image(image):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
features =
|
29 |
-
|
30 |
-
print(
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return {'ding': isDing["ding"]}
|
33 |
-
|
34 |
image = gr.Image(type='pil')
|
35 |
label = gr.Label(num_top_classes=3)
|
36 |
|
|
|
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 PIL
|
|
|
13 |
#login()
|
14 |
|
15 |
|
16 |
+
read_key = os.environ.get('HF_TOKEN', True)
|
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 |
+
image = tf.image.convert_image_dtype(resized, tf.float32)
|
50 |
+
image = tf.reshape(image, [1,480,480,3])
|
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 |
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
coremltools==
|
|
|
|
1 |
+
coremltools==6.0
|
2 |
+
tensorflow==2.15
|