Spaces:
Runtime error
Runtime error
Trying cods classif
Browse files- app copy.py +29 -0
- app.py +29 -4
- requirements.txt +3 -2
app copy.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load a pretrained YOLOv8n model
|
6 |
+
model = YOLO("yolov8n.pt")
|
7 |
+
|
8 |
+
|
9 |
+
def main_function(lbd, img):
|
10 |
+
results = model(img) # predict on an image
|
11 |
+
r = results[0]
|
12 |
+
im_bgr = r.plot() # BGR-order numpy array
|
13 |
+
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
|
14 |
+
new_img = im_rgb
|
15 |
+
# res = results[0].save(filename="output.jpg") # save the image
|
16 |
+
# # load image
|
17 |
+
# new_img = Image.open("output.jpg")
|
18 |
+
return new_img
|
19 |
+
|
20 |
+
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=main_function,
|
23 |
+
inputs=["slider", gr.Image(type="pil")],
|
24 |
+
outputs=gr.Image(type="pil"),
|
25 |
+
examples=[
|
26 |
+
[0, "bus.jpg"],
|
27 |
+
],
|
28 |
+
)
|
29 |
+
iface.launch()
|
app.py
CHANGED
@@ -1,6 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load a pretrained YOLOv8n model
|
6 |
model = YOLO("yolov8n.pt")
|
@@ -19,11 +44,11 @@ def main_function(lbd, img):
|
|
19 |
|
20 |
|
21 |
iface = gr.Interface(
|
22 |
-
fn=main_function,
|
23 |
-
inputs=["slider", gr.Image(type="pil")],
|
24 |
-
outputs=gr.Image(type="pil"),
|
25 |
examples=[
|
26 |
-
[0, "bus.jpg"],
|
27 |
],
|
28 |
)
|
29 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image
|
4 |
+
from cods.classif.data import ClassificationDataset
|
5 |
+
from cods.classif.models import ClassificationModel
|
6 |
+
from cods.classif.cp import ClassificationConformalizer
|
7 |
+
|
8 |
+
|
9 |
+
def classif(img):
|
10 |
+
model_name = "resnet34"
|
11 |
+
pretrained_resnet_34 = timm.create_model(model_name, pretrained=True)
|
12 |
+
classifier = ClassificationModel(model=pretrained_resnet_34, model_name=model_name)
|
13 |
+
|
14 |
+
val_dataset = ClassificationDataset(...) # path to imagenet validation set
|
15 |
+
|
16 |
+
val_preds = classifier.build_predictions(
|
17 |
+
val_dataset,
|
18 |
+
dataset_name="imagenet",
|
19 |
+
split_name="cal",
|
20 |
+
batch_size=512,
|
21 |
+
shuffle=False,
|
22 |
+
)
|
23 |
+
|
24 |
+
cc = ClassificationConformalizer(method="lac", preprocess="softmax")
|
25 |
+
cc.lbd = 0.9
|
26 |
+
conf_cls = cc.conformalize(val_preds)
|
27 |
+
return str(conf_cls)
|
28 |
+
|
29 |
|
30 |
# Load a pretrained YOLOv8n model
|
31 |
model = YOLO("yolov8n.pt")
|
|
|
44 |
|
45 |
|
46 |
iface = gr.Interface(
|
47 |
+
fn=classif, # main_function,
|
48 |
+
inputs=gr.Image(type="pil"), # ["slider", gr.Image(type="pil")],
|
49 |
+
outputs=gr.Textbox(), # Image(type="pil"),
|
50 |
examples=[
|
51 |
+
"bus.jpg", # [0, "bus.jpg"],
|
52 |
],
|
53 |
)
|
54 |
iface.launch()
|
requirements.txt
CHANGED
@@ -4,6 +4,7 @@ gradio
|
|
4 |
opencv-python
|
5 |
Pillow
|
6 |
matplotlib
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
ultralytics
|
|
|
4 |
opencv-python
|
5 |
Pillow
|
6 |
matplotlib
|
7 |
+
timm
|
8 |
+
cods @ git+https://github.com/leoandeol/cods@main
|
9 |
|
10 |
+
#ultralytics
|
|