Spaces:
Build error
Build error
Anirudh Bhalekar
commited on
Commit
·
1fc8c87
1
Parent(s):
2aa0bff
working v1
Browse files- .gradio/flagged/dataset1.csv +2 -0
- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/inference.cpython-311.pyc +0 -0
- app.py +22 -4
- inference.py +30 -9
- requirements.txt +0 -0
- test.ipynb +50 -3
.gradio/flagged/dataset1.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
seismic,Select Task,output,timestamp
|
| 2 |
+
,Fault,,2025-08-20 13:37:37.675220
|
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
__pycache__/inference.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/inference.cpython-311.pyc and b/__pycache__/inference.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -6,8 +6,26 @@ from PIL import Image
|
|
| 6 |
import torch
|
| 7 |
from inference import predict, random_sample
|
| 8 |
|
| 9 |
-
|
| 10 |
def main():
|
| 11 |
-
gr.
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
from inference import predict, random_sample
|
| 8 |
|
|
|
|
| 9 |
def main():
|
| 10 |
+
with gr.Blocks() as demo:
|
| 11 |
+
# Button to select task
|
| 12 |
+
|
| 13 |
+
seismic_data = gr.State()
|
| 14 |
+
gr.Markdown("## SFM Inference Demo")
|
| 15 |
+
with gr.Row():
|
| 16 |
+
task = gr.Radio(choices=['Fault', 'Facies'], label="Select Task", value='Fault')
|
| 17 |
+
gr.Markdown("### Upload your seismic data or sample from dataset")
|
| 18 |
+
|
| 19 |
+
with gr.Row():
|
| 20 |
+
seismic_image = gr.Image(label="Upload Seismic Data")
|
| 21 |
+
prediction_image = gr.Image(label="Prediction Result")
|
| 22 |
+
|
| 23 |
+
with gr.Row():
|
| 24 |
+
random_sample_button = gr.Button("Upload Random Sample", elem_id="random-sample-button")
|
| 25 |
+
random_sample_button.click(fn=random_sample, inputs=[task], outputs=[seismic_image, seismic_data])
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
predict_button = gr.Button("Run Inference", elem_id="predict-button")
|
| 29 |
+
predict_button.click(fn=predict, inputs=[seismic_data, task], outputs=[prediction_image])
|
| 30 |
+
|
| 31 |
+
demo.launch()
|
inference.py
CHANGED
|
@@ -4,9 +4,20 @@ import timm
|
|
| 4 |
from util.datasets import ThebeSet, P3DFaciesSet
|
| 5 |
from util.pos_embed import interpolate_pos_embed
|
| 6 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if task == 'Fault':
|
| 11 |
model = models_Fault.__dict__[model_type](
|
| 12 |
img_size=768,
|
|
@@ -14,19 +25,21 @@ def predict(seis: torch.Tensor, finetune: str, task='Fault', model_type='vit_lar
|
|
| 14 |
drop_path_rate=0.1,
|
| 15 |
in_chans=1,
|
| 16 |
)
|
|
|
|
| 17 |
elif task == 'Facies':
|
| 18 |
model = models_Facies.__dict__[model_type](
|
| 19 |
-
img_size=
|
| 20 |
num_classes=6,
|
| 21 |
drop_path_rate=0.1,
|
| 22 |
in_chans=1,
|
| 23 |
)
|
|
|
|
| 24 |
else:
|
| 25 |
raise ValueError(f"Task not configured yet: {task}")
|
| 26 |
|
| 27 |
model.to(device)
|
| 28 |
-
checkpoint = torch.load(
|
| 29 |
-
|
| 30 |
checkpoint_model = checkpoint['model']
|
| 31 |
state_dict = model.state_dict()
|
| 32 |
|
|
@@ -40,10 +53,10 @@ def predict(seis: torch.Tensor, finetune: str, task='Fault', model_type='vit_lar
|
|
| 40 |
print(msg)
|
| 41 |
|
| 42 |
|
| 43 |
-
print("Seismic data shape:",
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
-
output = model(
|
| 47 |
output = output.squeeze(0)
|
| 48 |
|
| 49 |
if task in ['Fault']:
|
|
@@ -55,19 +68,27 @@ def predict(seis: torch.Tensor, finetune: str, task='Fault', model_type='vit_lar
|
|
| 55 |
output = output.detach().cpu().numpy()
|
| 56 |
|
| 57 |
print("Model output shape:", output.shape)
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
return output
|
| 60 |
|
| 61 |
|
| 62 |
-
def random_sample(
|
| 63 |
if task == 'Fault':
|
|
|
|
| 64 |
dataset = ThebeSet(data_path, [768, 768], 'test')
|
| 65 |
elif task == 'Facies':
|
|
|
|
| 66 |
dataset = P3DFaciesSet(data_path, mode = 'train')
|
| 67 |
else:
|
| 68 |
raise ValueError(f"Task not configured yet: {task}")
|
| 69 |
|
| 70 |
index = random.randint(0, len(dataset) - 1)
|
| 71 |
seis, label = dataset[index]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
return
|
|
|
|
| 4 |
from util.datasets import ThebeSet, P3DFaciesSet
|
| 5 |
from util.pos_embed import interpolate_pos_embed
|
| 6 |
import random
|
| 7 |
+
import huggingface_hub
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import numpy as np
|
| 11 |
+
from matplotlib import cm
|
| 12 |
|
| 13 |
|
| 14 |
+
HFACE_FAULTS = "checkpoint-24.pth"
|
| 15 |
+
HFACE_FACIES = "checkpoint-49.pth"
|
| 16 |
+
|
| 17 |
+
FAULT_DATA_PATH = "C:\\Users\\abhalekar\\Desktop\\DATASETS\\Thebe_DATASET\\crossline_combined_data"
|
| 18 |
+
FACIES_DATA_PATH = "C:\\Users\\abhalekar\\Desktop\\DATASETS\\P3D_Vol_DATASET"
|
| 19 |
+
|
| 20 |
+
def predict(seismic: torch.Tensor, task='Fault', model_type='vit_large_patch16', device = 'cpu', hface = True, thresh = 0.5):
|
| 21 |
if task == 'Fault':
|
| 22 |
model = models_Fault.__dict__[model_type](
|
| 23 |
img_size=768,
|
|
|
|
| 25 |
drop_path_rate=0.1,
|
| 26 |
in_chans=1,
|
| 27 |
)
|
| 28 |
+
checkpoint_path = hf_hub_download(repo_id="Ani24/SFM_Finetuned", filename=HFACE_FAULTS, subfolder="ckpts-Tversky-Neut")
|
| 29 |
elif task == 'Facies':
|
| 30 |
model = models_Facies.__dict__[model_type](
|
| 31 |
+
img_size=128,
|
| 32 |
num_classes=6,
|
| 33 |
drop_path_rate=0.1,
|
| 34 |
in_chans=1,
|
| 35 |
)
|
| 36 |
+
checkpoint_path = hf_hub_download(repo_id="Ani24/SFM_Finetuned", filename=HFACE_FACIES, subfolder="ckpts-RSVSFacies-P3D")
|
| 37 |
else:
|
| 38 |
raise ValueError(f"Task not configured yet: {task}")
|
| 39 |
|
| 40 |
model.to(device)
|
| 41 |
+
checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=False)
|
| 42 |
+
|
| 43 |
checkpoint_model = checkpoint['model']
|
| 44 |
state_dict = model.state_dict()
|
| 45 |
|
|
|
|
| 53 |
print(msg)
|
| 54 |
|
| 55 |
|
| 56 |
+
print("Seismic data shape:", seismic.shape)
|
| 57 |
|
| 58 |
with torch.no_grad():
|
| 59 |
+
output = model(seismic.unsqueeze(0))
|
| 60 |
output = output.squeeze(0)
|
| 61 |
|
| 62 |
if task in ['Fault']:
|
|
|
|
| 68 |
output = output.detach().cpu().numpy()
|
| 69 |
|
| 70 |
print("Model output shape:", output.shape)
|
| 71 |
+
output = output/ output.max() # Normalize output to [0, 1] range
|
| 72 |
+
# output is numpy 2d array - convert to pil RGB image
|
| 73 |
+
output = Image.fromarray((output * 255).astype(np.uint8)).convert("RGB")
|
| 74 |
return output
|
| 75 |
|
| 76 |
|
| 77 |
+
def random_sample(task = 'Fault', data_path = None, batch_size=1, num_workers=0):
|
| 78 |
if task == 'Fault':
|
| 79 |
+
data_path = FAULT_DATA_PATH
|
| 80 |
dataset = ThebeSet(data_path, [768, 768], 'test')
|
| 81 |
elif task == 'Facies':
|
| 82 |
+
data_path = FACIES_DATA_PATH
|
| 83 |
dataset = P3DFaciesSet(data_path, mode = 'train')
|
| 84 |
else:
|
| 85 |
raise ValueError(f"Task not configured yet: {task}")
|
| 86 |
|
| 87 |
index = random.randint(0, len(dataset) - 1)
|
| 88 |
seis, label = dataset[index]
|
| 89 |
+
|
| 90 |
+
seis_image = seis.detach().cpu().numpy().squeeze(0)
|
| 91 |
+
seis_image = (seis_image - seis_image.min()) / (seis_image.max() - seis_image.min()) # Normalize to [0, 1] range
|
| 92 |
+
seis_image = Image.fromarray(np.uint8(cm.seismic(seis_image) * 255)) # Convert to PIL Image
|
| 93 |
|
| 94 |
+
return seis_image, seis
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
test.ipynb
CHANGED
|
@@ -2,13 +2,52 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
-
"execution_count":
|
| 6 |
"id": "aaadf81b",
|
| 7 |
"metadata": {},
|
| 8 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"source": [
|
| 10 |
-
"import app\n"
|
|
|
|
|
|
|
| 11 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
],
|
| 14 |
"metadata": {
|
|
@@ -18,7 +57,15 @@
|
|
| 18 |
"name": "python3"
|
| 19 |
},
|
| 20 |
"language_info": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"name": "python",
|
|
|
|
|
|
|
| 22 |
"version": "3.11.9"
|
| 23 |
}
|
| 24 |
},
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
"id": "aaadf81b",
|
| 7 |
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stderr",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"C:\\Users\\abhalekar\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 14 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"* Running on local URL: http://127.0.0.1:7860\n",
|
| 22 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"data": {
|
| 27 |
+
"text/html": [
|
| 28 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 29 |
+
],
|
| 30 |
+
"text/plain": [
|
| 31 |
+
"<IPython.core.display.HTML object>"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
"metadata": {},
|
| 35 |
+
"output_type": "display_data"
|
| 36 |
+
}
|
| 37 |
+
],
|
| 38 |
"source": [
|
| 39 |
+
"import app\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"app.main()"
|
| 42 |
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "code",
|
| 46 |
+
"execution_count": null,
|
| 47 |
+
"id": "5f4991c2",
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"outputs": [],
|
| 50 |
+
"source": []
|
| 51 |
}
|
| 52 |
],
|
| 53 |
"metadata": {
|
|
|
|
| 57 |
"name": "python3"
|
| 58 |
},
|
| 59 |
"language_info": {
|
| 60 |
+
"codemirror_mode": {
|
| 61 |
+
"name": "ipython",
|
| 62 |
+
"version": 3
|
| 63 |
+
},
|
| 64 |
+
"file_extension": ".py",
|
| 65 |
+
"mimetype": "text/x-python",
|
| 66 |
"name": "python",
|
| 67 |
+
"nbconvert_exporter": "python",
|
| 68 |
+
"pygments_lexer": "ipython3",
|
| 69 |
"version": "3.11.9"
|
| 70 |
}
|
| 71 |
},
|