3.6 MiB
Vendored
3.6 MiB
Vendored
In [51]:
!nvidia-smiIn [52]:
import os
HOME = os.getcwd()
print(HOME)In [53]:
!mkdir {HOME}/imagesIn [54]:
%cd {HOME}/images
!wget -q https://media.roboflow.com/notebooks/examples/dog.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-2.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-3.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-4.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-5.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-6.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-7.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-8.jpegThu Jul 11 20:48:14 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 72C P0 32W / 70W | 473MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
+---------------------------------------------------------------------------------------+
/content/datasets/images
/content/datasets/images/images
In [55]:
!pip install -q supervision
import supervision as sv
print(sv.__version__)In [96]:
import cv2
IMAGE_PATH = f"{HOME}/images/dog.jpeg"
image = cv2.imread(IMAGE_PATH)In [57]:
!pip install -q super-gradients0.21.0
In [58]:
from super_gradients.training import models
model = models.get("yolo_nas_l", pretrained_weights="coco")
result = model.predict(image)
detections = sv.Detections.from_yolo_nas(result)[2024-07-11 20:48:37] WARNING - checkpoint_utils.py - :warning: The pre-trained models provided by SuperGradients may have their own licenses or terms and conditions derived from the dataset used for pre-training. It is your responsibility to determine whether you have permission to use the models for your use case. The model you have requested was pre-trained on the coco dataset, published under the following terms: https://cocodataset.org/#termsofuse [2024-07-11 20:48:37] INFO - checkpoint_utils.py - License Notification: YOLO-NAS pre-trained weights are subjected to the specific license terms and conditions detailed in https://github.com/Deci-AI/super-gradients/blob/master/LICENSE.YOLONAS.md By downloading the pre-trained weight files you agree to comply with these terms. [2024-07-11 20:48:38] INFO - checkpoint_utils.py - Successfully loaded pretrained weights for architecture yolo_nas_l [2024-07-11 20:48:38] INFO - pipelines.py - Fusing some of the model's layers. If this takes too much memory, you can deactivate it by setting `fuse_model=False`
In [59]:
"detections", len(detections)Out [59]:
('detections', 7)In [60]:
!pip install -q ultralyticsIn [97]:
from ultralytics import YOLO
model = YOLO("yolov8m.pt")
result = model(image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(result)Downloading https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8m.pt to 'yolov8m.pt'...
0%| | 0.00/49.7M [00:00<?, ?B/s] 59%|█████▊ | 29.1M/49.7M [00:00<00:00, 305MB/s] 100%|██████████| 49.7M/49.7M [00:00<00:00, 363MB/s]
In [62]:
model = YOLO("yolov8m-seg.pt")
result = model(image, verbose=False)[0]
detections_segmentation = sv.Detections.from_ultralytics(result)Downloading https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8m-seg.pt to 'yolov8m-seg.pt'...
100%|██████████| 52.4M/52.4M [00:00<00:00, 69.6MB/s]
In [63]:
"detections", len(detections)Out [63]:
('detections', 4)In [64]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections)
annotated_image = label_annotator.annotate(annotated_image, detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [64]:
In [65]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = [
f"{model.model.names[class_id]} {confidence:.2f}"
for class_id, confidence in zip(detections.class_id, detections.confidence)
]
annotated_image = box_annotator.annotate(
image.copy(),
detections=detections,
)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [66]:
mask_annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
annotated_image = mask_annotator.annotate(
image.copy(), detections=detections_segmentation
)
sv.plot_image(image=annotated_image, size=(8, 8))In [67]:
detections_index = detections[0]
detections_index_list = detections[[0, 1, 3]]
detections_index_slice = detections[:2]In [68]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
images = []
for d in [detections_index, detections_index_list, detections_index_slice]:
annotated_image = box_annotator.annotate(image.copy(), detections=d)
annotated_image = label_annotator.annotate(annotated_image, detections=d)
images.append(annotated_image)
titles = [
"by index - detections[0]",
"by index list - detections[[0, 1, 3]]",
"by index slice - detections[:2]",
]
sv.plot_images_grid(images=images, titles=titles, grid_size=(1, 3))In [69]:
detections_filtered = detections[detections.class_id == 0]In [70]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections_filtered)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered
)
sv.plot_image(image=annotated_image, size=(8, 8))In [100]:
detections_filtered = detections[detections.confidence > 0.7]In [101]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = []
for class_id, confidence in zip(
detections_filtered.class_id, detections_filtered.confidence
):
labels.append(f"{model.model.names[class_id]} {confidence:.2f}")
annotated_image = box_annotator.annotate(
image.copy(),
detections=detections_filtered,
)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [103]:
detections_filtered = detections[
(detections.class_id != 0) & (detections.confidence > 0.7)
]In [104]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = [
f"{class_id} {confidence:.2f}"
for class_id, confidence in zip(
detections_filtered.class_id, detections_filtered.confidence
)
]
annotated_image = box_annotator.annotate(
image.copy(),
detections=detections_filtered,
)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [75]:
!pip install -q supervision[assets]In [76]:
!mkdir {HOME}/videosIn [77]:
%cd {HOME}/videosIn [78]:
from supervision.assets import VideoAssets, download_assets
download_assets(VideoAssets.VEHICLES)
VIDEO_PATH = VideoAssets.VEHICLES.value/content/datasets/images/videos Downloading vehicles.mp4 assets
0%| | 0/35345757 [00:00<?, ?it/s]
In [79]:
sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)Out [79]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
In [80]:
frame_generator = sv.get_video_frames_generator(source_path=VIDEO_PATH)In [81]:
frame = next(iter(frame_generator))
sv.plot_image(image=frame, size=(8, 8))In [82]:
RESULT_VIDEO_PATH = f"{HOME}/videos/vehicle-counting-result.mp4"In [83]:
video_info = sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)
with sv.VideoSink(target_path=RESULT_VIDEO_PATH, video_info=video_info) as sink:
for frame in sv.get_video_frames_generator(source_path=VIDEO_PATH, stride=2):
sink.write_frame(frame=frame)In [84]:
sv.VideoInfo.from_video_path(video_path=RESULT_VIDEO_PATH)Out [84]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=269)
In [85]:
!pip install -q roboflowIn [86]:
!mkdir {HOME}/datasets
%cd {HOME}/datasets
import roboflow
from roboflow import Roboflow
roboflow.login()
rf = Roboflow()
project = rf.workspace("roboflow-jvuqo").project("fashion-assistant-segmentation")
dataset = project.version(5).download("yolov8")/content/datasets/images/datasets visit https://app.roboflow.com/auth-cli to get your authentication token. Paste the authentication token here: ·········· loading Roboflow workspace... loading Roboflow project... Dependency ultralytics==8.0.196 is required but found version=8.2.54, to fix: `pip install ultralytics==8.0.196`
Downloading Dataset Version Zip in fashion-assistant-segmentation-5 to yolov8:: 100%|██████████| 122509/122509 [00:03<00:00, 37319.95it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 15%|█▍ | 187/1254 [00:00<00:00, 1860.52it/s]
Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 30%|██▉ | 374/1254 [00:00<00:00, 1609.45it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 43%|████▎ | 538/1254 [00:00<00:00, 1529.93it/s]
In [87]:
ds = sv.DetectionDataset.from_yolo(
images_directory_path=f"{dataset.location}/train/images",
annotations_directory_path=f"{dataset.location}/train/labels",
data_yaml_path=f"{dataset.location}/data.yaml",
)Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 79%|███████▉ | 989/1254 [00:00<00:00, 2606.19it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 100%|██████████| 1254/1254 [00:00<00:00, 2505.30it/s]
In [88]:
len(ds)Out [88]:
573
In [89]:
ds.classesOut [89]:
['baseball cap', 'hoodie', 'jacket', 'pants', 'shirt', 'shorts', 'sneaker', 'sunglasses', 'sweatshirt', 't-shirt']
In [90]:
IMAGE_NAME = next(iter(ds.images.keys()))
image = ds.images[IMAGE_NAME]
annotations = ds.annotations[IMAGE_NAME]
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
mask_annotator = sv.MaskAnnotator()
labels = [f"{ds.classes[class_id]}" for class_id in annotations.class_id]
annotated_image = mask_annotator.annotate(image.copy(), detections=annotations)
annotated_image = box_annotator.annotate(annotated_image, detections=annotations)
annotated_image = label_annotator.annotate(
annotated_image, detections=annotations, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [91]:
ds_train, ds_test = ds.split(split_ratio=0.8)In [92]:
"ds_train", len(ds_train), "ds_test", len(ds_test)Out [92]:
('ds_train', 458, 'ds_test', 115)In [ ]:
ds_train.as_pascal_voc(
images_directory_path=f"{HOME}/datasets/result/images",
annotations_directory_path=f"{HOME}/datasets/result/labels",
)