Spaces:
Sleeping
Sleeping
File size: 9,254 Bytes
92d14a2 f1c6a3d 432e6ee f1c6a3d 432e6ee f1c6a3d 432e6ee f1c6a3d 432e6ee f1c6a3d 7896f53 432e6ee f1c6a3d 92d14a2 164b7b4 92d14a2 77dfadf 730f5a5 f1c6a3d 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 730f5a5 92d14a2 78d19ee 92d14a2 78d19ee 92d14a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import subprocess
import os
import sys
import importlib.metadata
def is_pkg_installed(pkg_name):
try:
importlib.metadata.distribution(pkg_name)
return True
except importlib.metadata.PackageNotFoundError:
return False
packages_to_install = [
('gradio',None),
('numpy',None),
('tensorflow', '2.9'),
('keras', '2.9'),
('opencv-python-headless', '4.5.5.64'),
('python-dotenv', None),
('torch', None),
('torchvision', None),
('xplique', None),
]
env_name = os.path.basename(sys.prefix)
if env_name == 'fossil': # in case pkgs installed to unexpected env during local dev
for package, version in packages_to_install:
package_spec = f"{package}=={version}" if version else package
if not is_pkg_installed(package):
subprocess.call(f"pip install {package_spec}".split())
else:
print(f"{package_spec} is already installed.")
else:
print("Please use venv named 'fossil' in case pkgs installed to other unexpected envs or locations.")
#print(os.getenv('SYSTEM'))
'''
if os.getenv('SYSTEM') == 'spaces':
subprocess.call('pip install tensorflow==2.9'.split())
subprocess.call('pip install keras==2.9'.split())
subprocess.call('pip install git+https://github.com/facebookresearch/segment-anything.git'.split())
subprocess.call('pip install opencv-python-headless==4.5.5.64'.split())
subprocess.call('pip install git+https://github.com/cocodataset/panopticapi.git'.split())
subprocess.call('pip install python-dotenv'.split())
subprocess.call('pip install torch torchvision '.split())
subprocess.call('pip install xplique'.split())
'''
import gradio as gr
from huggingface_hub import snapshot_download
import cv2
import dotenv
dotenv.load_dotenv()
import numpy as np
import gradio as gr
import glob
from inference_sam import segmentation_sam
from explanations import explain
from inference_resnet import get_triplet_model
import pathlib
import tensorflow as tf
from closest_sample import get_images
if not os.path.exists('images'):
REPO_ID='Serrelab/image_examples_gradio'
snapshot_download(repo_id=REPO_ID, token=os.environ.get('READ_TOKEN'),repo_type='dataset',local_dir='images')
def get_model(model_name):
if model_name=='Mummified 170':
n_classes = 170
model = get_triplet_model(input_shape = (600, 600, 3),
embedding_units = 256,
embedding_depth = 2,
backbone_class=tf.keras.applications.ResNet50V2,
nb_classes = n_classes,load_weights=False,finer_model=True,backbone_name ='Resnet50v2')
model.load_weights('model_classification/mummified-170.h5')
elif model_name=='Rock 170':
n_classes = 171
model = get_triplet_model(input_shape = (600, 600, 3),
embedding_units = 256,
embedding_depth = 2,
backbone_class=tf.keras.applications.ResNet50V2,
nb_classes = n_classes,load_weights=False,finer_model=True,backbone_name ='Resnet50v2')
model.load_weights('model_classification/rock-170.h5')
else:
return 'Error'
return model,n_classes
def segment_image(input_image):
img = segmentation_sam(input_image)
return img
def classify_image(input_image, model_name):
if 'Rock 170' ==model_name:
from inference_resnet import inference_resnet_finer
model,n_classes= get_model(model_name)
result = inference_resnet_finer(input_image,model,size=600,n_classes=n_classes)
return result
elif 'Mummified 170' ==model_name:
from inference_resnet import inference_resnet_finer
model, n_classes= get_model(model_name)
result = inference_resnet_finer(input_image,model,size=600,n_classes=n_classes)
return result
if 'Fossils 19' ==model_name:
from inference_beit import inference_dino
model,n_classes = get_model(model_name)
return inference_dino(input_image,model_name)
return None
def get_embeddings(input_image,model_name):
if 'Rock 170' ==model_name:
from inference_resnet import inference_resnet_embedding
model,n_classes= get_model(model_name)
result = inference_resnet_embedding(input_image,model,size=600,n_classes=n_classes)
return result
elif 'Mummified 170' ==model_name:
from inference_resnet import inference_resnet_embedding
model, n_classes= get_model(model_name)
result = inference_resnet_embedding(input_image,model,size=600,n_classes=n_classes)
return result
if 'Fossils 19' ==model_name:
from inference_beit import inference_dino
model,n_classes = get_model(model_name)
return inference_dino(input_image,model_name)
return None
def find_closest(input_image,model_name):
embedding = get_embeddings(input_image,model_name)
paths = get_images(embedding)
return paths
def explain_image(input_image,model_name):
model,n_classes= get_model(model_name)
saliency, integrated, smoothgrad = explain(model,input_image,n_classes=n_classes)
#original = saliency + integrated + smoothgrad
print('done')
return saliency, integrated, smoothgrad,
with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
with gr.Tab(" Florrissant Fossils"):
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input")
classify_image_button = gr.Button("Classify Image")
with gr.Column():
segmented_image = gr.outputs.Image(label="SAM output",type='numpy')
segment_button = gr.Button("Segment Image")
#classify_segmented_button = gr.Button("Classify Segmented Image")
with gr.Column():
model_name = gr.Dropdown(
["Mummified 170", "Rock 170"],
multiselect=False,
value="Rock 170",
label="Model",
interactive=True,
)
class_predicted = gr.Label(label='Class Predicted',num_top_classes=10)
with gr.Row():
paths = sorted(pathlib.Path('images/').rglob('*.jpg'))
samples=[[path.as_posix()] for path in paths if 'fossils' in str(path) ][:19]
examples_fossils = gr.Examples(samples, inputs=input_image,examples_per_page=10,label='Fossils Examples from the dataset')
samples=[[path.as_posix()] for path in paths if 'leaves' in str(path) ][:19]
examples_leaves = gr.Examples(samples, inputs=input_image,examples_per_page=5,label='Leaves Examples from the dataset')
# with gr.Accordion("Using Diffuser"):
# with gr.Column():
# prompt = gr.Textbox(lines=1, label="Prompt")
# output_image = gr.Image(label="Output")
# generate_button = gr.Button("Generate Leave")
# with gr.Column():
# class_predicted2 = gr.Label(label='Class Predicted from diffuser')
# classify_button = gr.Button("Classify Image")
with gr.Accordion("Explanations "):
gr.Markdown("Computing Explanations from the model")
with gr.Row():
#original_input = gr.Image(label="Original Frame")
saliency = gr.Image(label="saliency")
gradcam = gr.Image(label='integraged gradients')
guided_gradcam = gr.Image(label='gradcam')
#guided_backprop = gr.Image(label='guided backprop')
generate_explanations = gr.Button("Generate Explanations")
with gr.Accordion('Closest Images'):
gr.Markdown("Finding the closest images in the dataset")
with gr.Row():
closest_image_0 = gr.Image(label='Closest Image')
closest_image_1 = gr.Image(label='Second Closest Image')
closest_image_2 = gr.Image(label='Third Closest Image')
closest_image_3 = gr.Image(label='Forth Closest Image')
closest_image_4 = gr.Image(label='Fifth Closest Image')
find_closest_btn = gr.Button("Find Closest Images")
segment_button.click(segment_image, inputs=input_image, outputs=segmented_image)
classify_image_button.click(classify_image, inputs=[input_image,model_name], outputs=class_predicted)
generate_explanations.click(explain_image, inputs=[input_image,model_name], outputs=[saliency,gradcam,guided_gradcam])
find_closest_btn.click(find_closest, inputs=[input_image,model_name], outputs=[closest_image_0,closest_image_1,closest_image_2,closest_image_3,closest_image_4])
#classify_segmented_button.click(classify_image, inputs=[segmented_image,model_name], outputs=class_predicted)
demo.queue()
if os.getenv('SYSTEM') == 'spaces':
demo.launch(width='40%',auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD')))
else:
demo.launch()
|