Image Class/Detection with DarkNet(EASY MODE)

Anthony Cazares

2024-03-11

Credits for project

Package Loading

library(tidyverse)
library(image.darknet)

1. Classification Model

Create Model

classmodel <- image_darknet_model(type="classify",
                                     model="tiny.cfg",
                                     weights = system.file(package="image.darknet", "models", "tiny.weights"),
                                     labels = system.file(package = "image.darknet", "include", "darknet", "data", "imagenet.shortnames.list"))

Test Model with Image

The image used is a google searched image of a beagle.

Model Results

image_darknet_classify(file = "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\Beagle.jpg",
                       object = classmodel)
## $file
## [1] "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\Beagle.jpg"
## 
## $type
##                    label probability
## 1       English foxhound 0.558082998
## 2                 beagle 0.187836483
## 3           Walker hound 0.120024040
## 4 Welsh springer spaniel 0.090224430
## 5                 Saluki 0.009521718

The model predicts it is an image of an english foxhound with probability of 55% and beagle only 18%.

Image 2

Google searched image of a house.

Image 2 Results

image_darknet_classify(file = "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\house.jpg",
                       object = classmodel)
## $file
## [1] "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\house.jpg"
## 
## $type
##         label probability
## 1   boathouse  0.74004233
## 2 mobile home  0.13946339
## 3  solar dish  0.02605881
## 4      beacon  0.01807578
## 5 planetarium  0.01064833

Probably guesses boathouse with 74% probability because of the reflective driveway tricking the model to think it is a body of water and not wet concrete.

2. Detection Model

Create Model

detectmodel <- image_darknet_model(type="detect",
                                     model="tiny-yolo-voc.cfg",
                                     weights = system.file(package="image.darknet", "models", "tiny-yolo-voc.weights"),
                                     labels = system.file(package = "image.darknet", "include", "darknet", "data", "voc.names"))

Test Model with Image 1

image_darknet_detect(file = "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\Beagle.jpg",
                       object = detectmodel)

The results give a probability of 89% that the object detected is a dog. The drawn detection box can be seen below.

Perfect!

Test Model with Image 2

image_darknet_detect(file = "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\house.jpg",
                       object = detectmodel)

Model did not detect an object. No change to image.

Tune

Change the threshold to get a detection. (Higher chance of incorrect detection)

image_darknet_detect(file = "C:\\Users\\Larem15\\Desktop\\R\\Rshowcase\\htmls\\Projects\\house.jpg",
                       object = detectmodel,
                     threshold = 0.1)

A car object was detected with 10% probability which is quite low but that is our threshold.

The bush was detected as a car.

Perfect!