need to check what happens to im.data

This commit is contained in:
Dimitrii
2020-02-20 09:35:57 +03:00
parent 2b2f3f159c
commit 9d89162235
15 changed files with 54 additions and 64 deletions

3
.gitignore vendored
View File

@@ -10,3 +10,6 @@ example/yolov3.cfg
example/yolov3.weights example/yolov3.weights
darknet.h darknet.h
*.so *.so
predictions.png
predictions.jpg
main

View File

@@ -3,20 +3,16 @@
#include "detection.h" #include "detection.h"
detection * get_detection(detection *dets, int index, int dets_len) detection *get_detection(detection *dets, int index, int dets_len) {
{
if (index >= dets_len) { if (index >= dets_len) {
return NULL; return NULL;
} }
return dets + index; return dets + index;
} }
float get_detection_probability(detection *det, int index, int prob_len) float get_detection_probability(detection *det, int index, int prob_len) {
{
if (index >= prob_len) { if (index >= prob_len) {
return .0; return .0;
} }
return det->prob[index]; return det->prob[index];
} }

View File

@@ -26,15 +26,12 @@ type DetectionResult struct {
} }
func makeDetection(img *DarknetImage, det *C.detection, threshold float32, classes int, classNames []string) *Detection { func makeDetection(img *DarknetImage, det *C.detection, threshold float32, classes int, classNames []string) *Detection {
if det == nil { if det == nil {
return &Detection{} return &Detection{}
} }
dClassIDs := make([]int, 0) dClassIDs := make([]int, 0)
dClassNames := make([]string, 0) dClassNames := make([]string, 0)
dProbs := make([]float32, 0) dProbs := make([]float32, 0)
for i := 0; i < int(classes); i++ { for i := 0; i < int(classes); i++ {
dProb := float32(C.get_detection_probability(det, C.int(i), C.int(classes))) dProb := float32(C.get_detection_probability(det, C.int(i), C.int(classes)))
if dProb > threshold { if dProb > threshold {
@@ -44,7 +41,6 @@ func makeDetection(img *DarknetImage, det *C.detection, threshold float32, class
dProbs = append(dProbs, dProb*100) dProbs = append(dProbs, dProb*100)
} }
} }
fImgW := C.float(img.Width) fImgW := C.float(img.Width)
fImgH := C.float(img.Height) fImgH := C.float(img.Height)
halfRatioW := det.bbox.w / 2.0 halfRatioW := det.bbox.w / 2.0
@@ -65,7 +61,6 @@ func makeDetection(img *DarknetImage, det *C.detection, threshold float32, class
ClassNames: dClassNames, ClassNames: dClassNames,
Probabilities: dProbs, Probabilities: dProbs,
} }
return &out return &out
} }

View File

@@ -55,7 +55,6 @@ func main() {
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
_ = src
imgDarknet, err := darknet.Image2Float32(src) imgDarknet, err := darknet.Image2Float32(src)
if err != nil { if err != nil {
@@ -63,12 +62,11 @@ func main() {
} }
defer imgDarknet.Close() defer imgDarknet.Close()
dr, err := n.Detect(&imgDarknet) dr, err := n.Detect(imgDarknet)
if err != nil { if err != nil {
printError(err) printError(err)
return return
} }
_ = dr
log.Println("Network-only time taken:", dr.NetworkOnlyTimeTaken) log.Println("Network-only time taken:", dr.NetworkOnlyTimeTaken)
log.Println("Overall time taken:", dr.OverallTimeTaken, len(dr.Detections)) log.Println("Overall time taken:", dr.OverallTimeTaken, len(dr.Detections))

24
image.c
View File

@@ -1,22 +1,12 @@
#include <darknet.h> #include <darknet.h>
image new_darknet_image() { void fill_image_f32(image* im, int w, int h, int c, float* data) {
image img; int i;
img.w = 0; for (i = 0; i < w*h*c; i++) {
img.h = 0; im->data[i] = data[i];
img.c = 0; }
img.data = NULL;
return img;
} }
image prepare_image(image img, int w, int h, int c){ void set_data_f32_val(float* data, int index, float value) {
img.w=w; data[index] = value;
img.h=h;
img.c=c;
return img;
}
image new_image(int w, int h, int c){
image out = make_image(w, h, c);
return out;
} }

View File

@@ -26,34 +26,36 @@ func float_p(arr []float32) *C.float {
} }
// Image2Float32 Returns []float32 representation of image.Image // Image2Float32 Returns []float32 representation of image.Image
func Image2Float32(img image.Image) (DarknetImage, error) { func Image2Float32(img image.Image) (*DarknetImage, error) {
width := img.Bounds().Dx() width := img.Bounds().Dx()
height := img.Bounds().Dy() height := img.Bounds().Dy()
imgwh := width * height ans := []float32{}
imgSize := imgwh * 3 red := []float32{}
green := []float32{}
ans := make([]float32, imgSize) blue := []float32{}
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
r, g, b, _ := img.At(y, x).RGBA() r, g, b, _ := img.At(y, x).RGBA()
rpix, gpix, bpix := float32(r>>8)/float32(255.0), float32(g>>8)/float32(255.0), float32(b>>8)/float32(255.0) rpix, gpix, bpix := float32(r>>8)/float32(255.0), float32(g>>8)/float32(255.0), float32(b>>8)/float32(255.0)
ans[y+x*height] = rpix red = append(red, rpix)
ans[y+x*height+imgwh] = gpix green = append(green, gpix)
ans[y+x*height+imgwh+imgwh] = bpix blue = append(blue, bpix)
} }
} }
ans = append(ans, red...)
ans = append(ans, green...)
ans = append(ans, blue...)
imgDarknet := DarknetImage{ imgDarknet := &DarknetImage{
Width: width, Width: width,
Height: height, Height: height,
// image: C.new_darknet_image(), image: C.make_image(C.int(width), C.int(height), 3),
} }
// for i := range ans {
// C.set_data_f32_val(imgDarknet.image.data, C.int(i), C.float(ans[i]))
// }
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, float_p(ans))
// imgDarknet.image = C.prepare_image(imgDarknet.image, C.int(width), C.int(height), 3) // imgDarknet.image = C.load_image_color(C.CString("~/Downloads/mega.jpg"), 416, 416)
// imgDarknet.image.data = float_p(ans)
// imgDarknet.image = C.resize_image(imgDarknet.image, 416, 416) // Do we need resize? (detection function actually does it)
imgDarknet.image = C.load_image_color(C.CString("/home/dimitrii/Downloads/mega.jpg"), 416, 416)
return imgDarknet, nil return imgDarknet, nil
} }

View File

@@ -2,6 +2,5 @@
#include <darknet.h> #include <darknet.h>
extern image new_darknet_image(); extern void fill_image_f32(image *im, int w, int h, int c, float* data);
extern image prepare_image(image img, int w, int h, int c); extern void set_data_f32_val(float* data, int index, float value);
extern image new_image(int w, int h, int c);

BIN
main

Binary file not shown.

View File

@@ -6,13 +6,23 @@
#include "detection.h" #include "detection.h"
struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms) { struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box) {
image sized = letterbox_image(*img, n->w, n->h); image sized;
int ww = img->w;
int hh = img->h;
int cc = img->c;
if (letter_box) {
// printf("using letter %d %d %d %d %d %d\n", letter_box, n->w, n->h, img->w, img->h, img->c);
sized = letterbox_image(*img, n->w, n->h);
} else {
// printf("not using letter: %d %d %d %d %d %d\n", letter_box, n->w, n->h, img->w, img->h, img->c);
sized = resize_image(*img, n->w, n->h);
}
struct network_box_result result = { NULL }; struct network_box_result result = { NULL };
float *X = sized.data; float *X = sized.data;
int letter_box = 0;
network_predict_ptr(n, X); network_predict_ptr(n, X);
result.detections = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box); result.detections = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
// printf("Clang number of detections: %d\n", result.detections_len);
if (nms) { if (nms) {
do_nms_sort(result.detections, result.detections_len, classes, nms); do_nms_sort(result.detections, result.detections_len, classes, nms);
} }

View File

@@ -63,11 +63,8 @@ func (n *YOLONetwork) Init() error {
// C.set_batch_network(n.cNet, 1) // C.set_batch_network(n.cNet, 1)
C.srand(2222222) C.srand(2222222)
// Currently, hierarchal threshold is always 0.5. n.hierarchalThreshold = 0.5
n.hierarchalThreshold = .5 n.nms = 0.45
// Currently NMS is always 0.4.
n.nms = .45
metadata := C.get_metadata(nCfg) metadata := C.get_metadata(nCfg)
n.Classes = int(metadata.classes) n.Classes = int(metadata.classes)
@@ -93,7 +90,7 @@ func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) {
return nil, errNetworkNotInit return nil, errNetworkNotInit
} }
startTime := time.Now() startTime := time.Now()
result := C.perform_network_detect(n.cNet, &img.image, C.int(n.Classes), C.float(n.Threshold), C.float(n.hierarchalThreshold), C.float(n.nms)) result := C.perform_network_detect(n.cNet, &img.image, C.int(n.Classes), C.float(n.Threshold), C.float(n.hierarchalThreshold), C.float(n.nms), C.int(0))
endTime := time.Now() endTime := time.Now()
defer C.free_detections(result.detections, result.detections_len) defer C.free_detections(result.detections, result.detections_len)
ds := makeDetections(img, result.detections, int(result.detections_len), n.Threshold, n.Classes, n.ClassNames) ds := makeDetections(img, result.detections, int(result.detections_len), n.Threshold, n.Classes, n.ClassNames)

View File

@@ -8,4 +8,4 @@ struct network_box_result {
}; };
extern int get_network_layer_classes(network *n, int index); extern int get_network_layer_classes(network *n, int index);
extern struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms); extern struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB