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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
use crate::{
prelude::{AnnotatedImage, AnnotatedImageSet, Annotation, BBox, Detector},
types::Class,
};
use image::{DynamicImage, GenericImageView};
use rand::{thread_rng, Rng};
/// Generates random annotations for the provided annotated image set.
///
/// # Arguments
///
/// * `annotated_image_set` - The set of annotated images to add the annotations to.
/// * `count` - The number of annotations to generate for each annotated image.
/// * `max_count` - An optional maximum number of annotations to generate for all annotated images.
/// * `width` - The width of the generated bounding boxes.
/// * `height` - The height of the generated bounding boxes.
pub fn generate_annotations(
annotated_image_set: &mut dyn AnnotatedImageSet,
count: usize,
max_count: Option<usize>,
width: u32,
height: u32,
) {
let max_count = max_count.unwrap_or(std::usize::MAX);
let mut annotation_count = 0;
let mut rng = thread_rng();
let images: Vec<DynamicImage> = annotated_image_set
.annotated_images()
.map(|annotated_image| annotated_image.image.clone())
.collect();
for image in images {
let image_size = image.dimensions();
let mut annotations = Vec::new();
for _ in 0..count {
if annotation_count == max_count {
break;
}
let x = rng.gen_range(0..image_size.0 - width);
let y = rng.gen_range(0..image_size.1 - height);
let bbox = BBox {
x: x as i32,
y: y as i32,
width,
height,
};
let annotation = Annotation { bbox, class: 0 };
annotations.push(annotation);
annotation_count += 1;
}
let new_annotated_image = AnnotatedImage { image, annotations };
annotated_image_set.add_annotated_image(new_annotated_image);
}
}
/// generates hard negative samples, see: [Hard Negative Mining](https://openaccess.thecvf.com/content_ECCV_2018/papers/SouYoung_Jin_Unsupervised_Hard-Negative_Mining_ECCV_2018_paper.pdf)
///
/// # Arguments
///
/// * `annotated_image_set` - The set of annotated images to add the annotations to.
/// * `detector` - The hard negative mining detector.
/// * `class`- The class to assign to the hard negative samples.
/// * `max_count` - An optional maximum number of annotations to generate for all annotated images.
/// * `width` - The width of the generated bounding boxes.
/// * `height` - The height of the generated bounding boxes.
pub fn add_hard_negative_samples(
annotated_image_set: &mut dyn AnnotatedImageSet,
detector: &dyn Detector,
class: Class,
max_count: Option<usize>,
width: u32,
height: u32,
) {
let annotated_images = generate_negative_samples(
annotated_image_set,
detector,
class,
max_count,
width,
height,
);
annotated_images
.into_iter()
.for_each(|annotated_image| annotated_image_set.add_annotated_image(annotated_image));
}
/// generates negative samples
/// ///
/// # Arguments
///
/// * `annotated_image_set` - The set of annotated images to add the annotations to.
/// * `detector` - The hard negative mining detector.
/// * `class`- The class to assign to the hard negative samples.
/// * `max_count` - An optional maximum number of annotations to generate for all annotated images.
/// * `width` - The width of the generated bounding boxes.
/// * `height` - The height of the generated bounding boxes.
///
/// # Returns
///
/// Vec<AnnotatedImage> - The generated negative samples.
///
pub fn generate_negative_samples(
annotated_image_set: &dyn AnnotatedImageSet,
detector: &dyn Detector,
class: Class,
max_count: Option<usize>,
width: u32,
height: u32,
) -> Vec<AnnotatedImage> {
let mut annotations_counter = 0;
let mut generated_annotated_images: Vec<AnnotatedImage> = Vec::new();
for annotated_image in annotated_image_set.annotated_images() {
let detections = detector.detect(&annotated_image.image);
let mut false_pos_annotations = Vec::new();
detections.iter().for_each(|detection| {
if max_count.is_some() && max_count.unwrap() >= annotations_counter {
return;
}
if detection.class == class {
let mut false_pos = true;
annotated_image.annotations.iter().for_each(|annotation| {
if class == annotation.class {
if annotation.bbox.overlap(&detection.bbox) > 0.1 {
false_pos = false;
}
}
});
if false_pos {
let false_pos_bbox = detection.bbox.clone();
let false_pos_annotation: Annotation = Annotation {
bbox: BBox {
x: false_pos_bbox.x,
y: false_pos_bbox.y,
width,
height,
},
class: 0,
};
false_pos_annotations.push(false_pos_annotation);
annotations_counter += 1;
}
}
});
generated_annotated_images.push(AnnotatedImage {
image: annotated_image.image.clone(),
annotations: false_pos_annotations,
});
}
generated_annotated_images
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bbox::BBox;
use crate::prelude::{DataSet, Detection, MemoryDataSet};
use image::DynamicImage;
use mockall::predicate::*;
use mockall::*;
// Define the mock Detector trait
mock! {
DummyDetector {}
impl Detector for DummyDetector {
fn detect(&self, image: &DynamicImage) -> Vec<Detection>;
}
}
// Set up the mock Detector with default return values
fn mock_detector() -> MockDummyDetector {
let mut mock = MockDummyDetector::new();
mock.expect_detect().returning(|_| {
vec![Detection {
bbox: BBox::new(10, 10, 5, 5),
class: 1,
confidence: 1.0,
}]
});
mock
}
#[test]
fn test_generate_annotations() {
let mut dataset = MemoryDataSet::new();
// Create some annotated images and add them to the dataset
let annotated_image_1 = AnnotatedImage {
image: DynamicImage::new_rgba8(100, 100),
annotations: vec![Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 1,
height: 1,
},
class: 0,
}],
};
let annotated_image_2 = AnnotatedImage {
image: DynamicImage::new_rgba8(100, 100),
annotations: vec![
Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 1,
height: 1,
},
class: 1,
},
Annotation {
bbox: BBox {
x: 1,
y: 1,
width: 1,
height: 1,
},
class: 2,
},
],
};
dataset.add_annotated_image(annotated_image_1.clone());
dataset.add_annotated_image(annotated_image_2.clone());
// Generate 10 random annotations for each image with a maximum of 50 total annotations
generate_annotations(&mut dataset, 10, Some(15), 10, 10);
// Assert that the annotated image set has the correct number of annotated images
assert_eq!(dataset.annotated_images_len(), 4);
assert_eq!(dataset.len(), 1 + 2 + 10 + 5);
}
#[test]
fn test_generate_negative_samples() {
// Set up the mock Detector
let mock_detector = mock_detector();
// Set up the MemoryDataSet and add some test annotated images
let mut dataset = MemoryDataSet::new();
let annotated_image_1 = AnnotatedImage {
image: DynamicImage::new_rgba8(20, 20),
annotations: vec![
Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 5,
height: 5,
},
class: 0,
},
Annotation {
bbox: BBox {
x: 5,
y: 5,
width: 5,
height: 5,
},
class: 1,
},
],
};
let annotated_image_2 = AnnotatedImage {
image: DynamicImage::new_rgba8(20, 20),
annotations: vec![
Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 5,
height: 5,
},
class: 1,
},
Annotation {
bbox: BBox {
x: 10,
y: 10,
width: 10,
height: 10,
},
class: 1,
},
],
};
dataset.add_annotated_image(annotated_image_1);
dataset.add_annotated_image(annotated_image_2);
// Call generate_negative_samples with the mock Detector and test dataset
let negative_samples = generate_negative_samples(&dataset, &mock_detector, 1, None, 5, 5);
// Verify that the correct number of negative samples were generated
assert_eq!(negative_samples.len(), 2);
assert_eq!(negative_samples[0].annotations.len(), 1);
assert_eq!(negative_samples[1].annotations.len(), 0);
}
#[test]
fn test_add_hard_negative_samples() {
// Set up the mock Detector
let mock_detector = mock_detector();
// Set up the MemoryDataSet and add some test annotated images
let mut dataset = MemoryDataSet::new();
let annotated_image_1 = AnnotatedImage {
image: DynamicImage::new_rgba8(20, 20),
annotations: vec![
Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 5,
height: 5,
},
class: 0,
},
Annotation {
bbox: BBox {
x: 5,
y: 5,
width: 5,
height: 5,
},
class: 1,
},
],
};
let annotated_image_2 = AnnotatedImage {
image: DynamicImage::new_rgba8(20, 20),
annotations: vec![
Annotation {
bbox: BBox {
x: 0,
y: 0,
width: 5,
height: 5,
},
class: 1,
},
Annotation {
bbox: BBox {
x: 10,
y: 10,
width: 10,
height: 10,
},
class: 1,
},
],
};
dataset.add_annotated_image(annotated_image_1);
dataset.add_annotated_image(annotated_image_2);
// Call generate_negative_samples with the mock Detector and test dataset
add_hard_negative_samples(&mut dataset, &mock_detector, 1, None, 5, 5);
assert_eq!(dataset.annotated_images_len(), 4);
assert_eq!(
dataset
.annotated_images()
.skip(2)
.next()
.unwrap()
.annotations
.len(),
1
);
assert_eq!(
dataset.annotated_images().last().unwrap().annotations.len(),
0
);
}
}