Spaces:
Build error
Build error
pablorodriper
commited on
Commit
•
d50da4e
1
Parent(s):
8061319
Upload 2 files
Browse files- app.py +2 -2
- predict.py +20 -18
app.py
CHANGED
@@ -7,7 +7,7 @@ from huggingface_hub import from_pretrained_keras
|
|
7 |
from predict import predict_label
|
8 |
|
9 |
##Create list of examples to be loaded
|
10 |
-
example_list = glob.glob("
|
11 |
example_list = list(map(lambda el:[el], example_list))
|
12 |
|
13 |
demo = gr.Blocks()
|
@@ -31,7 +31,7 @@ with demo:
|
|
31 |
gr.Markdown("The model is trained to classify videos belonging to the following classes: liver, kidney-right, kidney-left, femur-right, femur-left, bladder, heart, lung-right, lung-left, spleen, pancreas")
|
32 |
|
33 |
with gr.Column():
|
34 |
-
gr.Examples(example_list, [input_video], [output_label], predict_label, cache_examples=
|
35 |
|
36 |
submit_button.click(predict_label, inputs=input_video, outputs=output_label)
|
37 |
|
|
|
7 |
from predict import predict_label
|
8 |
|
9 |
##Create list of examples to be loaded
|
10 |
+
example_list = glob.glob("*.mp4")
|
11 |
example_list = list(map(lambda el:[el], example_list))
|
12 |
|
13 |
demo = gr.Blocks()
|
|
|
31 |
gr.Markdown("The model is trained to classify videos belonging to the following classes: liver, kidney-right, kidney-left, femur-right, femur-left, bladder, heart, lung-right, lung-left, spleen, pancreas")
|
32 |
|
33 |
with gr.Column():
|
34 |
+
gr.Examples(example_list, [input_video], [output_label], predict_label, cache_examples=False)
|
35 |
|
36 |
submit_button.click(predict_label, inputs=input_video, outputs=output_label)
|
37 |
|
predict.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import cv2
|
|
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
from huggingface_hub import from_pretrained_keras
|
@@ -6,12 +7,31 @@ from tensorflow.keras.optimizers import Adam
|
|
6 |
|
7 |
from constants import LEARNING_RATE
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
model = get_model()
|
|
|
10 |
|
11 |
def predict_label(path):
|
12 |
frames = load_video(path)
|
13 |
prediction = model.predict(tf.expand_dims(frames, axis=0))[0]
|
14 |
label = np.argmax(prediction, axis=0)
|
|
|
15 |
|
16 |
return label
|
17 |
|
@@ -33,21 +53,3 @@ def load_video(path):
|
|
33 |
finally:
|
34 |
cap.release()
|
35 |
return np.array(frames)
|
36 |
-
|
37 |
-
|
38 |
-
def get_model():
|
39 |
-
"""
|
40 |
-
Download the model from the Hugging Face Hub and compile it.
|
41 |
-
"""
|
42 |
-
model = from_pretrained_keras("pablorodriper/video-vision-transformer")
|
43 |
-
|
44 |
-
model.compile(
|
45 |
-
optimizer=Adam(learning_rate=LEARNING_RATE),
|
46 |
-
loss="sparse_categorical_crossentropy",
|
47 |
-
# metrics=[
|
48 |
-
# keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
|
49 |
-
# keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
|
50 |
-
# ],
|
51 |
-
)
|
52 |
-
|
53 |
-
return model
|
|
|
1 |
import cv2
|
2 |
+
# import imageio
|
3 |
import numpy as np
|
4 |
import tensorflow as tf
|
5 |
from huggingface_hub import from_pretrained_keras
|
|
|
7 |
|
8 |
from constants import LEARNING_RATE
|
9 |
|
10 |
+
def get_model():
|
11 |
+
"""
|
12 |
+
Download the model from the Hugging Face Hub and compile it.
|
13 |
+
"""
|
14 |
+
model = from_pretrained_keras("pablorodriper/video-vision-transformer")
|
15 |
+
|
16 |
+
model.compile(
|
17 |
+
optimizer=Adam(learning_rate=LEARNING_RATE),
|
18 |
+
loss="sparse_categorical_crossentropy",
|
19 |
+
# metrics=[
|
20 |
+
# keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
|
21 |
+
# keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
|
22 |
+
# ],
|
23 |
+
)
|
24 |
+
|
25 |
+
return model
|
26 |
+
|
27 |
model = get_model()
|
28 |
+
labels = ['liver', 'kidney-right', 'kidney-left', 'femur-right', 'femur-left', 'bladder', 'heart', 'lung-right', 'lung-left', 'spleen', 'pancreas']
|
29 |
|
30 |
def predict_label(path):
|
31 |
frames = load_video(path)
|
32 |
prediction = model.predict(tf.expand_dims(frames, axis=0))[0]
|
33 |
label = np.argmax(prediction, axis=0)
|
34 |
+
label = labels[label]
|
35 |
|
36 |
return label
|
37 |
|
|
|
53 |
finally:
|
54 |
cap.release()
|
55 |
return np.array(frames)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|