upd readme

This commit is contained in:
Dimitrii
2020-02-21 15:47:21 +03:00
parent e486364a2b
commit feb1a0353d
16 changed files with 88 additions and 185 deletions

View File

@@ -3,17 +3,19 @@
[![GoDoc](https://godoc.org/github.com/LdDl/go-darknet?status.svg)](https://godoc.org/github.com/LdDl/go-darknet)
go-darknet is a Go package, which uses Cgo to enable Go applications to use
YOLO in [Darknet].
go-darknet is a Go package, which uses Cgo to enable Go applications to use YOLO in [Darknet].
## License
## Table of Contents
go-darknet follows [Darknet]'s [license].
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Documentation](#documentation)
- [License](#license)
## Requirements
For proper codebase please use fork of [darknet](https://github.com/AlexeyAB/darknet)
There are instructions for defining GPU/CPU + function for loading image from memory.
For proper codebase please use fork of [darknet](https://github.com/AlexeyAB/darknet). Latest commit I've tested [here](https://github.com/AlexeyAB/darknet/commit/64fb042c63637038671ae9d53c06165599b28912)
In order to use go-darknet, `libdarknet.so` should be available in one of
the following locations:
@@ -26,53 +28,82 @@ Also, [darknet.h] should be available in one of the following locations:
* /usr/include
* /usr/local/include
## Install
To achieve it, after Darknet compilation (via make) execute following command:
```shell
sudo cp libdarknet.so /usr/lib/libdarknet.so && sudo cp include/darknet.h /usr/local/include/darknet.h
```
## Installation
```shell
go get github.com/LdDl/go-darknet
```
The package name is `darknet`.
## Usage
## Use
Example Go program is provided in the [example] directory. Please refer to the code on how to use this Go package.
Example Go code/program is provided in the [example] directory. Please
refer to the code on how to use this Go package.
Building and running the example program is easy:
Building and running program:
Navigate to [example] folder
```shell
cd $GOPATH/github.com/LdDl/go-darknet/example
#download dataset (coco.names, coco.data, weights and configuration file)
```
Download dataset (sample of image, coco.names, yolov3.cfg, yolov3.weights).
```shell
./download_data.sh
#build program
go build main.go
#run it
./main -configFile yolov3.cfg --dataConfigFile coco.data -imageFile sample.jpg -weightsFile yolov3.weights
```
Note: you don't need *coco.data* file anymore, because script below does insert *coco.names* into 'names' filed in *yolov3.cfg* file (so AlexeyAB's fork can deal with it properly)
So last rows in yolov3.cfg file will look like:
```bash
......
[yolo]
mask = 0,1,2
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=80
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
names = coco.names # this is path to coco.names file
```
Build and run program
```
go build main.go && ./main --configFile=yolov3.cfg --weightsFile=yolov3.weights --imageFile=sample.jpg
```
Output should be something like this:
```shell
truck (7): 95.6232% | start point: (78,69) | end point: (222, 291)
truck (7): 81.5451% | start point: (0,114) | end point: (90, 329)
car (2): 99.8129% | start point: (269,192) | end point: (421, 323)
car (2): 99.6615% | start point: (567,188) | end point: (743, 329)
car (2): 99.5795% | start point: (425,196) | end point: (544, 309)
car (2): 96.5765% | start point: (678,185) | end point: (797, 320)
car (2): 91.5156% | start point: (391,209) | end point: (441, 291)
car (2): 88.1737% | start point: (507,193) | end point: (660, 324)
car (2): 83.6209% | start point: (71,199) | end point: (102, 281)
bicycle (1): 59.4000% | start point: (183,276) | end point: (257, 407)
person (0): 96.3393% | start point: (142,119) | end point: (285, 356)
truck (7): 49.5197% | start point: (0,136) | end point: (85, 311)
car (2): 36.3747% | start point: (95,152) | end point: (186, 283)
truck (7): 48.4384% | start point: (95,152) | end point: (186, 283)
truck (7): 45.6590% | start point: (694,178) | end point: (798, 310)
car (2): 76.8379% | start point: (1,145) | end point: (84, 324)
truck (7): 25.5731% | start point: (107,89) | end point: (215, 263)
car (2): 99.8783% | start point: (511,185) | end point: (748, 328)
car (2): 99.8194% | start point: (261,189) | end point: (427, 322)
car (2): 99.6408% | start point: (426,197) | end point: (539, 311)
car (2): 74.5610% | start point: (692,186) | end point: (796, 316)
car (2): 72.8053% | start point: (388,206) | end point: (437, 276)
bicycle (1): 72.2932% | start point: (178,270) | end point: (268, 406)
person (0): 97.3026% | start point: (143,135) | end point: (268, 343)
```
## Documentation
See go-darknet's API documentation at [GoDoc].
## License
go-darknet follows [Darknet]'s [license].
[Darknet]: https://github.com/pjreddie/darknet
[license]: https://github.com/pjreddie/darknet/blob/master/LICENSE
[darknet.h]: https://github.com/pjreddie/darknet/blob/master/include/darknet.h
[include/darknet.h]: https://github.com/pjreddie/darknet/blob/master/include/darknet.h
[darknet.h]: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h
[include/darknet.h]: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h
[Makefile]: https://github.com/alexeyab/darknet/blob/master/Makefile
[example]: /example
[GoDoc]: https://godoc.org/github.com/LdDl/go-darknet

View File

@@ -8,8 +8,7 @@ func makeClassNames(names **C.char, classes int) []string {
out := make([]string, classes)
for i := 0; i < classes; i++ {
n := C.get_class_name(names, C.int(i), C.int(classes))
s := C.GoString(n)
out[i] = s
out[i] = C.GoString(n)
}
return out
}

View File

@@ -1,5 +1,5 @@
package darknet
// #cgo CFLAGS: -I/usr/local/include -I/usr/local/cuda/include
// #cgo LDFLAGS: -L./lib -ldarknet -lm
// #cgo LDFLAGS: -L/usr/lib -ldarknet -lm
import "C"

View File

@@ -1,5 +0,0 @@
person
car
motorcycle
bus
truck

View File

@@ -2,7 +2,6 @@
#include "detection.h"
detection *get_detection(detection *dets, int index, int dets_len) {
if (index >= dets_len) {
return NULL;

View File

@@ -2,6 +2,5 @@
#include <darknet.h>
extern detection *get_detection(detection *dets, int index, int dets_len);
extern float get_detection_probability(detection *det, int index, int prob_len);

View File

@@ -1,6 +1,5 @@
wget --output-document=sample.jpg https://cdn-images-1.medium.com/max/800/1*EYFejGUjvjPcc4PZTwoufw.jpeg
wget --output-document=coco.names https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names
wget --output-document=coco.data https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/coco.data
sed -i 's#data/coco.names#coco.names#g' coco.data
wget --output-document=yolov3.cfg https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg
wget --output-document=coco.names https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names
wget --output-document=yolov3.cfg https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3.cfg
sed -i -e "\$anames = coco.names" yolov3.cfg
wget --output-document=yolov3.weights https://pjreddie.com/media/files/yolov3.weights

View File

@@ -12,8 +12,6 @@ import (
darknet "github.com/LdDl/go-darknet"
)
var dataConfigFile = flag.String("dataConfigFile", "",
"Path to data configuration file. Example: cfg/coco.data")
var configFile = flag.String("configFile", "",
"Path to network layer configuration file. Example: cfg/yolov3.cfg")
var weightsFile = flag.String("weightsFile", "",
@@ -28,7 +26,7 @@ func printError(err error) {
func main() {
flag.Parse()
if *dataConfigFile == "" || *configFile == "" || *weightsFile == "" ||
if *configFile == "" || *weightsFile == "" ||
*imageFile == "" {
flag.Usage()
@@ -37,7 +35,6 @@ func main() {
n := darknet.YOLONetwork{
GPUDeviceIndex: 0,
DataConfigurationFile: *dataConfigFile,
NetworkConfigurationFile: *configFile,
WeightsFile: *weightsFile,
Threshold: .25,
@@ -58,18 +55,6 @@ func main() {
panic(err.Error())
}
// bytes <<<<<<<<<<<<<
// imgBytes, err := imageToBytes(src)
// if err != nil {
// panic(err.Error())
// }
// imgDarknet, err := darknet.ImageFromMemory(imgBytes, 4032, 3024)
// if err != nil {
// panic(err.Error())
// }
// defer imgDarknet.Close()
// bytes >>>>>>>>>>>>>
imgDarknet, err := darknet.Image2Float32(src)
if err != nil {
panic(err.Error())

View File

@@ -1,18 +0,0 @@
[
{
"frame_id":1,
"filename":"/home/dimitrii/Downloads/mega.jpg",
"objects": [
{"class_id":3, "name":"bus", "relative_coordinates":{"center_x":0.475497, "center_y":0.317034, "width":0.175112, "height":0.104746}, "confidence":0.686032},
{"class_id":3, "name":"bus", "relative_coordinates":{"center_x":0.278355, "center_y":0.302800, "width":0.087061, "height":0.052067}, "confidence":0.562420},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.799558, "center_y":0.643484, "width":0.388328, "height":0.539759}, "confidence":0.942335},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.365836, "center_y":0.431383, "width":0.267126, "height":0.156932}, "confidence":0.776083},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.513763, "center_y":0.475799, "width":0.301966, "height":0.240549}, "confidence":0.684380},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.717148, "center_y":0.516788, "width":0.456320, "height":0.313704}, "confidence":0.584288},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.217955, "center_y":0.379872, "width":0.212185, "height":0.101100}, "confidence":0.468769},
{"class_id":1, "name":"car", "relative_coordinates":{"center_x":0.126970, "center_y":0.337752, "width":0.035609, "height":0.022990}, "confidence":0.452899},
{"class_id":0, "name":"person", "relative_coordinates":{"center_x":0.051506, "center_y":0.369395, "width":0.056224, "height":0.175025}, "confidence":0.978385},
{"class_id":0, "name":"person", "relative_coordinates":{"center_x":0.097309, "center_y":0.370574, "width":0.048715, "height":0.142128}, "confidence":0.801452}
]
}
]

25
image.c
View File

@@ -11,28 +11,3 @@ void set_data_f32_val(float* data, int index, float value) {
data[index] = value;
}
image resize_image_golang(image im, int w, int h) {
return resize_image(im, w, h);
}
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
image float_to_image(int w, int h, int c, float *data)
{
image out = make_empty_image(w,h,c);
fill_image_f32(&out, w, h, c, data);
// for (i = 0; i < w*h*c; i++) {
// out.data[i] = data[i];
// }
return out;
}

View File

@@ -23,24 +23,20 @@ func (img *DarknetImage) Close() error {
return nil
}
func float_p(arr []float32) *C.float {
return (*C.float)(unsafe.Pointer(&arr[0]))
}
// https://stackoverflow.com/questions/33186783/get-a-pixel-array-from-from-golang-image-image/59747737#59747737
func image_2_array_pix(src image.Image) []float32 {
func imgTofloat32(src image.Image) []float32 {
bounds := src.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
src_rgba := image.NewRGBA(src.Bounds())
draw.Copy(src_rgba, image.Point{}, src, src.Bounds(), draw.Src, nil)
srcRGBA := image.NewRGBA(src.Bounds())
draw.Copy(srcRGBA, image.Point{}, src, src.Bounds(), draw.Src, nil)
ans := []float32{}
red := []float32{}
green := []float32{}
blue := []float32{}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idx_s := (y*width + x) * 4
pix := src_rgba.Pix[idx_s : idx_s+4]
idxSource := (y*width + x) * 4
pix := srcRGBA.Pix[idxSource : idxSource+4]
rpix, gpix, bpix := float32(pix[0])/257.0, float32(pix[1])/257.0, float32(pix[2])/257.0
red = append(red, rpix)
green = append(green, gpix)
@@ -55,7 +51,7 @@ func image_2_array_pix(src image.Image) []float32 {
// Image2Float32 Returns []float32 representation of image.Image
func Image2Float32(img image.Image) (*DarknetImage, error) {
ans := image_2_array_pix(img)
ans := imgTofloat32(img)
width := img.Bounds().Dx()
height := img.Bounds().Dy()
imgDarknet := &DarknetImage{
@@ -63,10 +59,6 @@ func Image2Float32(img image.Image) (*DarknetImage, error) {
Height: height,
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.load_image_color(C.CString("/home/dimitrii/Downloads/mega.jpg"), 4032, 3024)
C.fill_image_f32(&imgDarknet.image, C.int(width), C.int(height), 3, (*C.float)(unsafe.Pointer(&ans[0])))
return imgDarknet, nil
}
}

View File

@@ -3,7 +3,4 @@
#include <darknet.h>
extern void fill_image_f32(image *im, int w, int h, int c, float* data);
extern void set_data_f32_val(float* data, int index, float value);
extern image resize_image_golang(image im, int w, int h);
extern image make_empty_image(int w, int h, int c);
extern image float_to_image(int w, int h, int c, float *data);
extern void set_data_f32_val(float* data, int index, float value);

View File

@@ -6,52 +6,23 @@
#include "detection.h"
struct network_box_result perform_network_detect(network *n, image img, int classes, float thresh, float hier_thresh, float nms, int letter_box) {
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;
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);
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);
sized = resize_image(*img, n->w, n->h);
}
// printf("\n>>>>>>>>>>>>>>Fourth Print (Golang)\n");
// for (int i = 0; i< 50; i++) {
// printf("%f\n", sized.data[i]);
// }
// printf("\n<<<<<<<<<<<<<<Done\n");
// sized.data = float*{0.815686, 0.592875, 0.645386, 0.731689, 0.659976};
// newImg := resize_image_golang(imgDarknet.image, 416, 416)
struct network_box_result result = { NULL };
float *X = sized.data;
float *outnwt = network_predict_ptr(n, X);
// printf("\n>>>>>>>>>>>>>>Golang out\n");
// for (int i = 0; i< 100; i++) {
// printf("%f ", outnwt[i]);
// }
// printf("\n<<<<<<<<<<<<<<Done\n");
network_predict_ptr(n, X);
int nboxes = 0;
detection *dets = get_network_boxes(n, img.w, img.h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
printf("Clang number of detections: %d %d %d %f %f %d\n", nboxes, img.w, img.h, thresh, hier_thresh, letter_box);
result.detections = get_network_boxes(n, img.w, img.h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
printf("Golang number of detections: %d\n", result.detections_len);
// printf("Clang number of detections: %d\n", result.detections_len);
detection *dets = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &nboxes, letter_box);
result.detections = get_network_boxes(n, img->w, img->h, thresh, hier_thresh, 0, 1, &result.detections_len, letter_box);
if (nms) {
do_nms_sort(result.detections, result.detections_len, classes, nms);
}
free_image(sized);
return result;
}

View File

@@ -15,7 +15,6 @@ import (
// YOLONetwork represents a neural network using YOLO.
type YOLONetwork struct {
GPUDeviceIndex int
DataConfigurationFile string
NetworkConfigurationFile string
WeightsFile string
Threshold float32
@@ -29,47 +28,28 @@ type YOLONetwork struct {
}
var (
errNetworkNotInit = errors.New("network not initialised")
errUnableToInitNetwork = errors.New("unable to initialise")
errNetworkNotInit = errors.New("Network not initialised")
errUnableToInitNetwork = errors.New("Unable to initialise")
)
// Init the network.
func (n *YOLONetwork) Init() error {
nCfg := C.CString(n.NetworkConfigurationFile)
defer C.free(unsafe.Pointer(nCfg))
wFile := C.CString(n.WeightsFile)
defer C.free(unsafe.Pointer(wFile))
// data := C.CString(n.DataConfigurationFile)
// defer C.free(unsafe.Pointer(data))
// inptim := C.CString("/home/dimitrii/Downloads/mega.jpg")
// defer C.free(unsafe.Pointer(inptim))
// outputim := C.CString("/home/dimitrii/work/src/github.com/LdDl/go-darknet/example/out-sample.png")
// defer C.free(unsafe.Pointer(outputim))
// C.test_detector(data, nCfg, wFile, inptim, 0.4, 0.5, 1, 1, 0, outputim, 0, 0)
// // GPU device ID must be set before `load_network()` is invoked.
// GPU device ID must be set before `load_network()` is invoked.
C.cuda_set_device(C.int(n.GPUDeviceIndex))
n.cNet = C.load_network(nCfg, wFile, 0)
if n.cNet == nil {
return errUnableToInitNetwork
}
// C.set_batch_network(n.cNet, 1)
C.srand(2222222)
n.hierarchalThreshold = 0.5
n.nms = 0.45
metadata := C.get_metadata(nCfg)
n.Classes = int(metadata.classes)
n.ClassNames = makeClassNames(metadata.names, n.Classes)
return nil
}
@@ -78,7 +58,6 @@ func (n *YOLONetwork) Close() error {
if n.cNet == nil {
return errNetworkNotInit
}
C.free_network(*n.cNet)
n.cNet = nil
return nil
@@ -90,7 +69,7 @@ func (n *YOLONetwork) Detect(img *DarknetImage) (*DetectionResult, error) {
return nil, errNetworkNotInit
}
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), C.int(0))
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()
defer C.free_detections(result.detections, result.detections_len)
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 struct network_box_result perform_network_detect(network *n, image img, int classes, float thresh, float hier_thresh, float nms, int letter_box);
extern struct network_box_result perform_network_detect(network *n, image *img, int classes, float thresh, float hier_thresh, float nms, int letter_box);