-
Notifications
You must be signed in to change notification settings - Fork 0
/
lakera_integration.py
129 lines (110 loc) · 4.02 KB
/
lakera_integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
""" Copyright 2023 Lakera AI. All Rights Reserved.
Run Lakera's MLTest on a Roboflow dataset and model.
"""
import os
from typing import Iterable
import uuid
import yaml
import numpy as np
from PIL import Image
from lakera import (
Batch,
DataMapper,
Predictor,
Runner,
RunnerOptions,
Sample,
TargetType,
)
from roboflow import Roboflow
#Input your Roboflow API key
ROBOFLOW_API_KEY = ""
# Name of your public Roboflow project
ROBOFLOW_PROJECT = "site-safety-lk"
# Model version to use
ROBOFLOW_MODEL_VERSION = 1
# Path to your Roboflow dataset.
PATH_TO_DATASET = "safety"
class RoboflowDataset(DataMapper):
def __init__(self, options: RunnerOptions):
self.options = options
self.path_to_dataset = f"{PATH_TO_DATASET}/test"
with open(f"{PATH_TO_DATASET}/data.yaml") as f:
names = yaml.safe_load(f)["names"]
self.id_to_label = {i: name for i, name in enumerate(names)}
def get_input_identifiers(self) -> Iterable[str]:
"""Returns a list of all the image names in the dataset."""
return os.listdir(os.path.join(self.path_to_dataset, "images"))
def identifier_to_sample(self, identifier: str) -> Sample:
"""Given an image name, loads the image and its annotations."""
# Load the image from disk.
image = Image.open(
os.path.join(self.path_to_dataset, "images", identifier)
)
# Create a Sample object and indicate we are working with object detection.
sample = Sample(
target_type=TargetType.BOUNDING_BOX,
identifier=identifier,
)
# Add the image to the sample.
sample.add_input(np.array(image))
# Load the labels from disk.
labels_file = identifier.replace(".jpg", ".txt")
predictions = []
with open(
os.path.join(self.path_to_dataset, "labels", labels_file)
) as f:
predictions = f.readlines()
for p in predictions:
class_id, x, y, w, h = p.split()
label = self.id_to_label[int(class_id)]
# Add the bounding box to the sample.
sample.add_yolo_target_bbox(
float(x), float(y), float(w), float(h), label
)
return sample
class RoboflowPredictorAPI(Predictor):
def __init__(self, options: RunnerOptions):
self.options = options
self.rf = Roboflow(api_key=ROBOFLOW_API_KEY)
project = self.rf.workspace().project(ROBOFLOW_PROJECT)
self.model = project.version(ROBOFLOW_MODEL_VERSION).model
def predict(self, batch: Batch) -> Batch:
"""Makes predictions on an input batch of images."""
for sample in batch:
h, w, _ = sample.input.shape
# Save the image to disk to be uploaded to Roboflow.
path_to_image = f"/tmp/rf/{uuid.uuid4()}.jpg"
Image.fromarray(sample.input).save(path_to_image)
# Compute predictions by making a request to Roboflow.
pred = self.model.predict(
path_to_image, confidence=40, overlap=30
).json()
if "predictions" not in pred:
continue
# Add the model predictions to the sample.
for p in pred["predictions"]:
sample.add_yolo_prediction_bbox(
p["x"] / float(w),
p["y"] / float(h),
p["width"] / float(w),
p["height"] / float(h),
p["class"],
p["confidence"],
)
return batch
if __name__ == "__main__":
path_to_options = "./options.yaml"
# Where to store the MLTest results.
path_to_output = "./mltest_results"
# Load your test suite.
options = RunnerOptions(
path_to_yaml=path_to_options,
)
# Create a Lakera Runner
runner = Runner(
RoboflowDataset, RoboflowPredictorAPI, options, path_to_output
)
# Run the runner to get your results!
runner_results = runner.run()
runner_results.print_summary()