Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- hed.py +79 -0
- vit_onnx.onnx +3 -0
hed.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""HED.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1sxAQYKOi_hJozIVNX80ChnznWExGtWXd
|
| 8 |
+
|
| 9 |
+
# IMPORTS
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import tensorflow as tf # For tensorflow
|
| 13 |
+
import numpy as np # For mathematical computations
|
| 14 |
+
import matplotlib.pyplot as plt # For plotting and Visualization
|
| 15 |
+
import seaborn as sns
|
| 16 |
+
from tensorflow.keras.layers import Input, Layer, Resizing, Rescaling, InputLayer, Conv2D, BatchNormalization, MaxPooling2D, Dropout, Flatten, Dense, RandomRotation, RandomFlip, RandomContrast, ReLU, Add, GlobalAveragePooling2D, Permute
|
| 17 |
+
from tensorflow.keras import Model
|
| 18 |
+
from tensorflow.keras.regularizers import L2
|
| 19 |
+
from tensorflow.keras.losses import CategoricalCrossentropy
|
| 20 |
+
from tensorflow.keras.metrics import CategoricalAccuracy, TopKCategoricalAccuracy
|
| 21 |
+
from tensorflow.keras.optimizers import Adam
|
| 22 |
+
from sklearn.metrics import confusion_matrix
|
| 23 |
+
from tensorflow.keras.callbacks import ModelCheckpoint, Callback
|
| 24 |
+
import cv2
|
| 25 |
+
|
| 26 |
+
"""# ONNX"""
|
| 27 |
+
|
| 28 |
+
!pip install onnx
|
| 29 |
+
!pip install onnxruntime
|
| 30 |
+
|
| 31 |
+
import onnx
|
| 32 |
+
import onnxruntime as ort
|
| 33 |
+
|
| 34 |
+
# from google.colab import drive
|
| 35 |
+
# drive.mount('/content/drive')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
"""## Predicting Using Onnx Model"""
|
| 40 |
+
|
| 41 |
+
!pip install onnx
|
| 42 |
+
!pip install onnxruntime
|
| 43 |
+
|
| 44 |
+
import onnxruntime as rt
|
| 45 |
+
import onnx
|
| 46 |
+
|
| 47 |
+
"""# Creating Web Interface Using Gradio"""
|
| 48 |
+
|
| 49 |
+
!pip install gradio
|
| 50 |
+
|
| 51 |
+
import gradio as gr
|
| 52 |
+
|
| 53 |
+
!pip install onnx
|
| 54 |
+
!pip install onnxruntime
|
| 55 |
+
|
| 56 |
+
import onnxruntime as rt
|
| 57 |
+
|
| 58 |
+
# !cp -r /content/drive/MyDrive/vit_onnx.onnx /content/vit_onnx.onnx
|
| 59 |
+
|
| 60 |
+
import onnx
|
| 61 |
+
model = onnx.load("vit_onnx.onnx")
|
| 62 |
+
|
| 63 |
+
import onnxruntime as ort
|
| 64 |
+
session = ort.InferenceSession("vit_onnx.onnx")
|
| 65 |
+
|
| 66 |
+
input_name = session.get_inputs()[0].name
|
| 67 |
+
output_name = session.get_outputs()[0].name
|
| 68 |
+
|
| 69 |
+
CLASS_NAMES = ["Angry", "Happy", "Sad"]
|
| 70 |
+
def predict_image(im):
|
| 71 |
+
im = tf.expand_dims(tf.cast(im, tf.float32), axis=0).numpy()
|
| 72 |
+
prediction = session.run([output_name], {input_name: im})
|
| 73 |
+
return {CLASS_NAMES[i]: float(prediction[0][0][i]) for i in range(3)}
|
| 74 |
+
|
| 75 |
+
image = gr.inputs.Image(shape=(224, 224))
|
| 76 |
+
label = gr.outputs.Label(num_top_classes=3)
|
| 77 |
+
iface = gr.Interface(fn=predict_image, inputs=image, outputs=label, capture_session=True)
|
| 78 |
+
iface.launch(debug="True")
|
| 79 |
+
|
vit_onnx.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a24c81c8da0f4c831414b6bca27320ed6f632681eddace226611db5a276e5fa2
|
| 3 |
+
size 343610599
|