Spaces:
Running
Running
added requirements
Browse files- .history/app_20240617181401.py +72 -0
- .history/app_20240617181448.py +72 -0
- .history/requirements_20240617181314.txt +0 -0
- .history/requirements_20240617181322.txt +1 -0
- .history/requirements_20240617181330.txt +2 -0
- .history/requirements_20240617181331.txt +2 -0
- .history/requirements_20240617181334.txt +3 -0
- app.py +2 -2
- requirements.txt +3 -0
.history/app_20240617181401.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
6 |
+
from datasets import load_dataset
|
7 |
+
|
8 |
+
# Model and processor configuration
|
9 |
+
model_name_or_path = "google/vit-base-patch16-224-in21k"
|
10 |
+
processor = ViTImageProcessor.from_pretrained(model_name_or_path)
|
11 |
+
|
12 |
+
# Load dataset (adjust dataset_path accordingly)
|
13 |
+
dataset_path = "pawlo2013/chest_xray"
|
14 |
+
train_dataset = load_dataset(dataset_path, split="train")
|
15 |
+
class_names = train_dataset.features["label"].names
|
16 |
+
|
17 |
+
# Load ViT model
|
18 |
+
model = ViTForImageClassification.from_pretrained(
|
19 |
+
"./models",
|
20 |
+
num_labels=len(class_names),
|
21 |
+
id2label={str(i): label for i, label in enumerate(class_names)},
|
22 |
+
label2id={label: i for i, label in enumerate(class_names)},
|
23 |
+
)
|
24 |
+
|
25 |
+
# Set model to evaluation mode
|
26 |
+
model.eval()
|
27 |
+
|
28 |
+
|
29 |
+
# Define the classification function
|
30 |
+
def classify_image(img_path):
|
31 |
+
img = Image.open(img_path)
|
32 |
+
processed_input = processor(images=img, return_tensors="pt")
|
33 |
+
with torch.no_grad():
|
34 |
+
outputs = model(**processed_input)
|
35 |
+
logits = outputs.logits
|
36 |
+
probabilities = torch.softmax(logits, dim=1)[0].tolist()
|
37 |
+
|
38 |
+
result = {class_name: prob for class_name, prob in zip(class_names, probabilities)}
|
39 |
+
filename = os.path.basename(img_path).split(".")[0]
|
40 |
+
return {"filename": filename, "probabilities": result}
|
41 |
+
|
42 |
+
|
43 |
+
def format_output(output):
|
44 |
+
return f"{output['filename']}", output["probabilities"]
|
45 |
+
|
46 |
+
|
47 |
+
# Function to load examples from a folder
|
48 |
+
def load_examples_from_folder(folder_path):
|
49 |
+
examples = []
|
50 |
+
for file in os.listdir(folder_path):
|
51 |
+
if file.endswith((".png", ".jpg", ".jpeg")):
|
52 |
+
examples.append(os.path.join(folder_path, file))
|
53 |
+
return examples
|
54 |
+
|
55 |
+
|
56 |
+
# Define the path to the examples folder
|
57 |
+
examples_folder = "./examples"
|
58 |
+
examples = load_examples_from_folder(examples_folder)
|
59 |
+
|
60 |
+
# Create the Gradio interface
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=lambda img: format_output(classify_image(img)),
|
63 |
+
inputs=gr.Image(type="filepath"),
|
64 |
+
outputs=[gr.Textbox(label="True Label (from filename)"), gr.Label()],
|
65 |
+
examples=examples,
|
66 |
+
title="Pneumonia X-Ray 3-Class Classification with Vision Transformer (ViT) using data augmentation",
|
67 |
+
description="Upload an X-ray image to classify it as normal, viral or bacterial pneumonia.",
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the app
|
71 |
+
if __name__ == "__main__":
|
72 |
+
iface.launch()
|
.history/app_20240617181448.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
6 |
+
from datasets import load_dataset
|
7 |
+
|
8 |
+
# Model and processor configuration
|
9 |
+
model_name_or_path = "google/vit-base-patch16-224-in21k"
|
10 |
+
processor = ViTImageProcessor.from_pretrained(model_name_or_path)
|
11 |
+
|
12 |
+
# Load dataset (adjust dataset_path accordingly)
|
13 |
+
dataset_path = "pawlo2013/chest_xray"
|
14 |
+
train_dataset = load_dataset(dataset_path, split="train")
|
15 |
+
class_names = train_dataset.features["label"].names
|
16 |
+
|
17 |
+
# Load ViT model
|
18 |
+
model = ViTForImageClassification.from_pretrained(
|
19 |
+
"./models",
|
20 |
+
num_labels=len(class_names),
|
21 |
+
id2label={str(i): label for i, label in enumerate(class_names)},
|
22 |
+
label2id={label: i for i, label in enumerate(class_names)},
|
23 |
+
)
|
24 |
+
|
25 |
+
# Set model to evaluation mode
|
26 |
+
model.eval()
|
27 |
+
|
28 |
+
|
29 |
+
# Define the classification function
|
30 |
+
def classify_image(img_path):
|
31 |
+
img = Image.open(img_path)
|
32 |
+
processed_input = processor(images=img, return_tensors="pt")
|
33 |
+
with torch.no_grad():
|
34 |
+
outputs = model(**processed_input)
|
35 |
+
logits = outputs.logits
|
36 |
+
probabilities = torch.softmax(logits, dim=1)[0].tolist()
|
37 |
+
|
38 |
+
result = {class_name: prob for class_name, prob in zip(class_names, probabilities)}
|
39 |
+
filename = os.path.basename(img_path).split(".")[0]
|
40 |
+
return {"filename": filename, "probabilities": result}
|
41 |
+
|
42 |
+
|
43 |
+
def format_output(output):
|
44 |
+
return f"{output['filename']}", output["probabilities"]
|
45 |
+
|
46 |
+
|
47 |
+
# Function to load examples from a folder
|
48 |
+
def load_examples_from_folder(folder_path):
|
49 |
+
examples = []
|
50 |
+
for file in os.listdir(folder_path):
|
51 |
+
if file.endswith((".png", ".jpg", ".jpeg")):
|
52 |
+
examples.append(os.path.join(folder_path, file))
|
53 |
+
return examples
|
54 |
+
|
55 |
+
|
56 |
+
# Define the path to the examples folder
|
57 |
+
examples_folder = "./examples"
|
58 |
+
examples = load_examples_from_folder(examples_folder)
|
59 |
+
|
60 |
+
# Create the Gradio interface
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=lambda img: format_output(classify_image(img)),
|
63 |
+
inputs=gr.Image(type="filepath"),
|
64 |
+
outputs=[gr.Textbox(label="True Label (from filename)"), gr.Label()],
|
65 |
+
examples=examples,
|
66 |
+
title="Pneumonia X-Ray 3-Class Classification with Vision Transformer (ViT) using data augmentation",
|
67 |
+
description="Upload an X-ray image to classify it as normal, viral or bacterial pneumonia. Checkout the model in more details at https://huggingface.co/pawlo2013/vit-pneumonia-x-ray_3_class",
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the app
|
71 |
+
if __name__ == "__main__":
|
72 |
+
iface.launch()
|
.history/requirements_20240617181314.txt
ADDED
File without changes
|
.history/requirements_20240617181322.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
torch
|
.history/requirements_20240617181330.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
.history/requirements_20240617181331.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
.history/requirements_20240617181334.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
datasets
|
app.py
CHANGED
@@ -63,8 +63,8 @@ iface = gr.Interface(
|
|
63 |
inputs=gr.Image(type="filepath"),
|
64 |
outputs=[gr.Textbox(label="True Label (from filename)"), gr.Label()],
|
65 |
examples=examples,
|
66 |
-
title="Pneumonia X-Ray 3-Class Classification with Vision Transformer (ViT)",
|
67 |
-
description="Upload an X-ray image to classify it as normal, viral or bacterial pneumonia.",
|
68 |
)
|
69 |
|
70 |
# Launch the app
|
|
|
63 |
inputs=gr.Image(type="filepath"),
|
64 |
outputs=[gr.Textbox(label="True Label (from filename)"), gr.Label()],
|
65 |
examples=examples,
|
66 |
+
title="Pneumonia X-Ray 3-Class Classification with Vision Transformer (ViT) using data augmentation",
|
67 |
+
description="Upload an X-ray image to classify it as normal, viral or bacterial pneumonia. Checkout the model in more details at https://huggingface.co/pawlo2013/vit-pneumonia-x-ray_3_class",
|
68 |
)
|
69 |
|
70 |
# Launch the app
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
datasets
|