Spaces:
Sleeping
Sleeping
Eric P. Nusbaum
commited on
Commit
·
123d436
1
Parent(s):
ecce323
List tensors
Browse files
app.py
CHANGED
@@ -2,69 +2,36 @@
|
|
2 |
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
5 |
-
import numpy as np
|
6 |
-
from PIL import Image
|
7 |
import os
|
8 |
|
9 |
# Suppress TensorFlow logging for cleaner logs
|
10 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
with tf.io.gfile.GFile(pb_file_path, 'rb') as f:
|
19 |
graph_def = tf.compat.v1.GraphDef()
|
20 |
graph_def.ParseFromString(f.read())
|
21 |
|
22 |
with tf.Graph().as_default() as graph:
|
23 |
tf.import_graph_def(graph_def, name='')
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
MODEL_DIR = 'tensorflow/'
|
28 |
-
graph = load_frozen_graph(os.path.join(MODEL_DIR, 'model.pb'))
|
29 |
-
sess = tf.compat.v1.Session(graph=graph)
|
30 |
-
|
31 |
-
# Identify input and output tensor names
|
32 |
-
# You may need to adjust these based on your model's actual tensor names
|
33 |
-
input_tensor = graph.get_tensor_by_name('input:0') # Replace 'input:0' with your actual input tensor name
|
34 |
-
output_tensor = graph.get_tensor_by_name('output:0') # Replace 'output:0' with your actual output tensor name
|
35 |
-
|
36 |
-
def preprocess_image(image):
|
37 |
-
# Resize image to the size expected by the model
|
38 |
-
target_size = (320, 320) # Replace with your model's expected input size
|
39 |
-
image = image.resize(target_size)
|
40 |
-
image = np.array(image)
|
41 |
-
image = image / 255.0 # Normalize if required
|
42 |
-
return image
|
43 |
-
|
44 |
-
def predict(image):
|
45 |
-
image = preprocess_image(image)
|
46 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
47 |
-
predictions = sess.run(output_tensor, feed_dict={input_tensor: image})
|
48 |
-
predicted_index = np.argmax(predictions, axis=1)[0]
|
49 |
-
predicted_label = labels[predicted_index]
|
50 |
-
confidence = predictions[0][predicted_index] * 100
|
51 |
-
return {predicted_label: round(confidence, 2)}
|
52 |
|
53 |
# Define Gradio interface
|
54 |
-
title = "JunkWaxHero 🦸♂️ -
|
55 |
-
description = "
|
56 |
|
57 |
iface = gr.Interface(
|
58 |
-
fn=
|
59 |
-
inputs=
|
60 |
-
outputs=
|
61 |
title=title,
|
62 |
description=description,
|
63 |
-
examples=[
|
64 |
-
["examples/card1.jpg"],
|
65 |
-
["examples/card2.jpg"],
|
66 |
-
["examples/card3.jpg"]
|
67 |
-
],
|
68 |
allow_flagging="never"
|
69 |
)
|
70 |
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
|
|
|
|
5 |
import os
|
6 |
|
7 |
# Suppress TensorFlow logging for cleaner logs
|
8 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
9 |
|
10 |
+
def list_tensor_names():
|
11 |
+
# Load the frozen TensorFlow graph
|
12 |
+
MODEL_DIR = 'tensorflow/'
|
13 |
+
MODEL_PATH = os.path.join(MODEL_DIR, 'model.pb')
|
14 |
+
|
15 |
+
with tf.io.gfile.GFile(MODEL_PATH, 'rb') as f:
|
|
|
16 |
graph_def = tf.compat.v1.GraphDef()
|
17 |
graph_def.ParseFromString(f.read())
|
18 |
|
19 |
with tf.Graph().as_default() as graph:
|
20 |
tf.import_graph_def(graph_def, name='')
|
21 |
+
|
22 |
+
tensor_names = [op.name for op in graph.get_operations()]
|
23 |
+
return "\n".join(tensor_names)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Define Gradio interface
|
26 |
+
title = "JunkWaxHero 🦸♂️ - Tensor Names Inspector"
|
27 |
+
description = "This interface lists all tensor names in the TensorFlow model to help identify the correct input and output tensors."
|
28 |
|
29 |
iface = gr.Interface(
|
30 |
+
fn=list_tensor_names,
|
31 |
+
inputs=None,
|
32 |
+
outputs="text",
|
33 |
title=title,
|
34 |
description=description,
|
|
|
|
|
|
|
|
|
|
|
35 |
allow_flagging="never"
|
36 |
)
|
37 |
|