keremberke commited on
Commit
307beef
1 Parent(s): 853c33d

Upload 7 files

Browse files
Files changed (7) hide show
  1. README.md +8 -10
  2. app.py +177 -0
  3. cls_models.txt +2 -0
  4. det_models.txt +8 -0
  5. requirements.txt +2 -0
  6. seg_models.txt +3 -0
  7. utils.py +14 -0
README.md CHANGED
@@ -1,13 +1,11 @@
1
  ---
2
- title: Awesome Yolov8 Models
3
- emoji:
4
- colorFrom: blue
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.19.1
8
  app_file: app.py
9
- pinned: false
10
- license: openrail
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Awesome YOLOv8 Models
3
+ emoji: 💯
4
+ colorFrom: green
5
+ colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.17.1
8
  app_file: app.py
9
+ pinned: true
10
+ license: mit
11
+ ---
 
 
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ import gradio as gr
4
+ from datasets import load_dataset
5
+ from ultralyticsplus import YOLO, render_result, postprocess_classify_output
6
+
7
+ from utils import load_models_from_txt_files
8
+
9
+ EXAMPLE_IMAGE_DIR = 'example_images'
10
+
11
+ DEFAULT_DET_MODEL_ID = 'keremberke/yolov8m-valorant-detection'
12
+ DEFAULT_DET_DATASET_ID = 'keremberke/valorant-object-detection'
13
+ DEFAULT_SEG_MODEL_ID = 'keremberke/yolov8s-building-segmentation'
14
+ DEFAULT_SEG_DATASET_ID = 'keremberke/satellite-building-segmentation'
15
+ DEFAULT_CLS_MODEL_ID = 'keremberke/yolov8m-chest-xray-classification'
16
+ DEFAULT_CLS_DATASET_ID = 'keremberke/chest-xray-classification'
17
+
18
+ # load model ids and default models
19
+ det_model_ids, seg_model_ids, cls_model_ids = load_models_from_txt_files()
20
+ det_model = YOLO(DEFAULT_DET_MODEL_ID)
21
+ det_model_id = DEFAULT_DET_MODEL_ID
22
+ seg_model = YOLO(DEFAULT_SEG_MODEL_ID)
23
+ seg_model_id = DEFAULT_SEG_MODEL_ID
24
+ cls_model = YOLO(DEFAULT_CLS_MODEL_ID)
25
+ cls_model_id = DEFAULT_CLS_MODEL_ID
26
+
27
+
28
+ def get_examples(model_id, dataset_id, task):
29
+ examples = []
30
+ ds = load_dataset(dataset_id, name="mini")["validation"]
31
+ Path(EXAMPLE_IMAGE_DIR).mkdir(parents=True, exist_ok=True)
32
+ for ind in range(min(5, len(ds))):
33
+ jpeg_image_file = ds[ind]["image"]
34
+ image_file_path = str(Path(EXAMPLE_IMAGE_DIR) / f"{task}_example_{ind}.jpg")
35
+ jpeg_image_file.save(image_file_path, format='JPEG', quality=100)
36
+ image_path = os.path.abspath(image_file_path)
37
+ examples.append([image_path, model_id, 0.25])
38
+ return examples
39
+
40
+ # load default examples using default datasets
41
+ det_examples = get_examples(DEFAULT_DET_MODEL_ID, DEFAULT_DET_DATASET_ID, 'detect')
42
+ seg_examples = get_examples(DEFAULT_SEG_MODEL_ID, DEFAULT_SEG_DATASET_ID, 'segment')
43
+ cls_examples = get_examples(DEFAULT_CLS_MODEL_ID, DEFAULT_CLS_DATASET_ID, 'classification')
44
+
45
+
46
+ def predict(image, model_id, threshold):
47
+ """Perform inference on image."""
48
+ # set task
49
+ if model_id in det_model_ids:
50
+ task = 'detect'
51
+ elif model_id in seg_model_ids:
52
+ task = 'segment'
53
+ elif model_id in cls_model_ids:
54
+ task = 'classify'
55
+ else:
56
+ raise ValueError(f"Invalid model_id: {model_id}")
57
+
58
+ # set model
59
+ if task == 'detect':
60
+ global det_model
61
+ global det_model_id
62
+ if model_id != det_model_id:
63
+ det_model = YOLO(model_id)
64
+ det_model_id = model_id
65
+ model = det_model
66
+ elif task == 'segment':
67
+ global seg_model
68
+ global seg_model_id
69
+ if model_id != seg_model_id:
70
+ seg_model = YOLO(model_id)
71
+ seg_model_id = model_id
72
+ model = seg_model
73
+ elif task == 'classify':
74
+ global cls_model
75
+ global cls_model_id
76
+ if model_id != cls_model_id:
77
+ cls_model = YOLO(model_id)
78
+ cls_model_id = model_id
79
+ model = cls_model
80
+ else:
81
+ raise ValueError(f"Invalid task: {task}")
82
+
83
+ # set model parameters
84
+ model.overrides['conf'] = threshold
85
+
86
+ # perform inference
87
+ results = model.predict(image)
88
+ print(model_id)
89
+ print(task)
90
+
91
+ if task in ['detect', 'segment']:
92
+ # draw predictions
93
+ output = render_result(model=model, image=image, result=results[0])
94
+ elif task == 'classify':
95
+ # postprocess classification output
96
+ output = postprocess_classify_output(model, result=results[0])
97
+ else:
98
+ raise ValueError(f"Invalid task: {task}")
99
+
100
+ return output
101
+
102
+
103
+ with gr.Blocks() as demo:
104
+ gr.Markdown("""# <p align='center'><img width='500px' src='https://user-images.githubusercontent.com/34196005/215836968-fb54e066-a524-4caf-b469-92bbaa96f921.gif' /></p>
105
+ <p style='text-align: center'>
106
+ <br> <a href='https://yolov8.xyz' target='_blank'>project website</a> | <a href='https://github.com/keremberke/awesome-yolov8-models' target='_blank'>project github</a>
107
+ </p>
108
+ <p style='text-align: center'>
109
+ Follow me for more!
110
+ <br> <a href='https://twitter.com/_keremberke' target='_blank'>twitter</a> | <a href='https://github.com/keremberke' target='_blank'>github</a> | <a href='https://www.linkedin.com/in/kerem-berke-bba6a5204/' target='_blank'>linkedin</a>
111
+ </p>
112
+ """)
113
+ with gr.Tab("Detection"):
114
+ with gr.Row():
115
+ with gr.Column():
116
+ detect_input = gr.Image()
117
+ detect_model_id = gr.Dropdown(choices=det_model_ids, label="Model:", value=DEFAULT_DET_MODEL_ID, interactive=True)
118
+ detect_threshold = gr.Slider(maximum=1, step=0.01, value=0.25, label="Threshold:", interactive=True)
119
+ detect_button = gr.Button("Detect!")
120
+ with gr.Column():
121
+ detect_output = gr.Image(label="Predictions:", interactive=False)
122
+ with gr.Row():
123
+ gr.Examples(
124
+ det_examples,
125
+ inputs=[detect_input, detect_model_id, detect_threshold],
126
+ outputs=detect_output,
127
+ fn=predict,
128
+ cache_examples=True,
129
+ )
130
+ with gr.Tab("Segmentation"):
131
+ with gr.Row():
132
+ with gr.Column():
133
+ segment_input = gr.Image()
134
+ segment_model_id = gr.Dropdown(choices=seg_model_ids, label="Model:", value=DEFAULT_SEG_MODEL_ID, interactive=True)
135
+ segment_threshold = gr.Slider(maximum=1, step=0.01, value=0.25, label="Threshold:", interactive=True)
136
+ segment_button = gr.Button("Segment!")
137
+ with gr.Column():
138
+ segment_output = gr.Image(label="Predictions:", interactive=False)
139
+ with gr.Row():
140
+ gr.Examples(
141
+ seg_examples,
142
+ inputs=[segment_input, segment_model_id, segment_threshold],
143
+ outputs=segment_output,
144
+ fn=predict,
145
+ cache_examples=False,
146
+ )
147
+ with gr.Tab("Classification"):
148
+ with gr.Row():
149
+ with gr.Column():
150
+ classify_input = gr.Image()
151
+ classify_model_id = gr.Dropdown(choices=cls_model_ids, label="Model:", value=DEFAULT_CLS_MODEL_ID, interactive=True)
152
+ classify_threshold = gr.Slider(maximum=1, step=0.01, value=0.25, label="Threshold:", interactive=True)
153
+ classify_button = gr.Button("Classify!")
154
+ with gr.Column():
155
+ classify_output = gr.Label(
156
+ label="Predictions:", show_label=True, num_top_classes=5
157
+ )
158
+ with gr.Row():
159
+ gr.Examples(
160
+ cls_examples,
161
+ inputs=[classify_input, classify_model_id, classify_threshold],
162
+ outputs=classify_output,
163
+ fn=predict,
164
+ cache_examples=False,
165
+ )
166
+
167
+ detect_button.click(
168
+ predict, inputs=[detect_input, detect_model_id, detect_threshold], outputs=detect_output
169
+ )
170
+ segment_button.click(
171
+ predict, inputs=[segment_input, segment_model_id, segment_threshold], outputs=segment_output
172
+ )
173
+ classify_button.click(
174
+ predict, inputs=[classify_input, classify_model_id, classify_threshold], outputs=classify_output
175
+ )
176
+
177
+ demo.launch(server_port=8080)
cls_models.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ keremberke/yolov8m-shoe-classification
2
+ keremberke/yolov8m-chest-xray-classification
det_models.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ keremberke/yolov8m-valorant-detection
2
+ keremberke/yolov8m-csgo-player-detection
3
+ keremberke/yolov8m-forklift-detection
4
+ keremberke/yolov8m-blood-cell-detection
5
+ keremberke/yolov8m-plane-detection
6
+ keremberke/yolov8m-nlf-head-detection
7
+ keremberke/yolov8m-hard-hat-detection
8
+ keremberke/yolov8m-table-extraction
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ ultralyticsplus==0.0.25
seg_models.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ keremberke/yolov8m-pcb-defect-segmentation
2
+ keremberke/yolov8s-building-segmentation
3
+ keremberke/yolov8n-pothole-segmentation
utils.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DET_MODELS_FILENAME = 'det_models.txt'
2
+ SEG_MODELS_FILENAME = 'seg_models.txt'
3
+ CLS_MODELS_FILENAME = 'cls_models.txt'
4
+
5
+
6
+ def load_models_from_txt_files():
7
+ """Load models from txt files."""
8
+ with open(DET_MODELS_FILENAME, 'r') as file:
9
+ det_models = [line.strip() for line in file]
10
+ with open(SEG_MODELS_FILENAME, 'r') as file:
11
+ seg_models = [line.strip() for line in file]
12
+ with open(CLS_MODELS_FILENAME, 'r') as file:
13
+ cls_models = [line.strip() for line in file]
14
+ return det_models, seg_models, cls_models