From 98b363a84bdf0902f621181ab6c81523bbaec0a4 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Tue, 7 Nov 2017 16:27:06 -0800 Subject: [PATCH 1/5] local changes --- .gitignore | 3 +++ Makefile | 8 ++++---- examples/detector.py | 27 +++++++++++++++------------ python/darknet.py | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index bea19ff4..40b39e0c 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ ehthumbs.db Icon? Thumbs.db *.swp +*.weights +*.a +*.so diff --git a/Makefile b/Makefile index 5f6f6e2d..22b40a85 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -GPU=0 -CUDNN=0 -OPENCV=0 -OPENMP=0 +GPU=1 +CUDNN=1 +OPENCV=1 +OPENMP=1 DEBUG=0 ARCH= -gencode arch=compute_30,code=sm_30 \ diff --git a/examples/detector.py b/examples/detector.py index aa565d61..7ec86938 100644 --- a/examples/detector.py +++ b/examples/detector.py @@ -7,19 +7,22 @@ import sys, os sys.path.append(os.path.join(os.getcwd(),'python/')) import darknet as dn +import pdb -net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) -meta = dn.load_meta("cfg/coco.data") -r = dn.detect(net, meta, "data/dog.jpg") -print r +net = dn.load_net("cfg/yolo-tag.cfg", "yolo-tag_final.weights", 0) +meta = dn.load_meta("cfg/openimages.data") +pdb.set_trace() +rr = dn.detect(net, meta, 'data/dog.jpg') +print rr +pdb.set_trace() # And then down here you could detect a lot more images like: -r = dn.detect(net, meta, "data/eagle.jpg") -print r -r = dn.detect(net, meta, "data/giraffe.jpg") -print r -r = dn.detect(net, meta, "data/horses.jpg") -print r -r = dn.detect(net, meta, "data/person.jpg") -print r +rr = dn.detect(net, meta, "data/eagle.jpg") +print rr +rr = dn.detect(net, meta, "data/giraffe.jpg") +print rr +rr = dn.detect(net, meta, "data/horses.jpg") +print rr +rr = dn.detect(net, meta, "data/person.jpg") +print rr diff --git a/python/darknet.py b/python/darknet.py index c8ec9be5..4391525f 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -122,6 +122,6 @@ if __name__ == "__main__": net = load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = load_meta("cfg/coco.data") r = detect(net, meta, "data/dog.jpg") - print r + print(r) From 0158fb23b2e25436620ab8a452d6201bebf6b8f7 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Tue, 13 Mar 2018 11:25:44 -0700 Subject: [PATCH 2/5] python stuff --- examples/detector-scipy-opencv.py | 14 +++++++++----- examples/detector.py | 9 --------- python/darknet.py | 32 +++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/examples/detector-scipy-opencv.py b/examples/detector-scipy-opencv.py index 3bfc5913..88efaa24 100644 --- a/examples/detector-scipy-opencv.py +++ b/examples/detector-scipy-opencv.py @@ -38,14 +38,18 @@ import darknet as dn # Darknet net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = dn.load_meta("cfg/coco.data") -r = dn.detect(net, meta, "data/dog.jpg") -print r +import time +tStart = time.time() +for i in range(10): + r = dn.detect(net, meta, "data/dog.jpg") +print(time.time() - tStart) # scipy arr= imread('data/dog.jpg') -im = array_to_image(arr) -r = detect2(net, meta, im) -print r +tStart = time.time() +for i in range(10): + r = dn.detect(net, meta, arr) +print(time.time() - tStart) # OpenCV arr = cv2.imread('data/dog.jpg') diff --git a/examples/detector.py b/examples/detector.py index a9b19f29..b95ab71a 100644 --- a/examples/detector.py +++ b/examples/detector.py @@ -9,20 +9,11 @@ sys.path.append(os.path.join(os.getcwd(),'python/')) import darknet as dn import pdb -<<<<<<< HEAD -net = dn.load_net("cfg/yolo-tag.cfg", "yolo-tag_final.weights", 0) -meta = dn.load_meta("cfg/openimages.data") -pdb.set_trace() -rr = dn.detect(net, meta, 'data/dog.jpg') -print rr -pdb.set_trace() -======= dn.set_gpu(0) net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = dn.load_meta("cfg/coco.data") r = dn.detect(net, meta, "data/dog.jpg") print r ->>>>>>> 16686cec576580489ab3c7c78183e6efeafae780 # And then down here you could detect a lot more images like: rr = dn.detect(net, meta, "data/eagle.jpg") diff --git a/python/darknet.py b/python/darknet.py index a745eef5..d0c6e961 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -13,7 +13,10 @@ def sample(probs): return len(probs)-1 def c_array(ctype, values): - return (ctype * len(values))(*values) + arr = (ctype * len(values))() + arr[:] = values + return arr + class BOX(Structure): _fields_ = [("x", c_float), @@ -31,7 +34,7 @@ class METADATA(Structure): _fields_ = [("classes", c_int), ("names", POINTER(c_char_p))] - + #lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL) lib = CDLL("libdarknet.so", RTLD_GLOBAL) @@ -101,6 +104,18 @@ predict_image.restype = POINTER(c_float) network_detect = lib.network_detect network_detect.argtypes = [c_void_p, IMAGE, c_float, c_float, c_float, POINTER(BOX), POINTER(POINTER(c_float))] +import numpy +def array_to_image(arr): + arr = arr.copy() + arr = arr.transpose(2,0,1) + c = arr.shape[0] + h = arr.shape[1] + w = arr.shape[2] + arr = (arr.astype(numpy.float32)/255.0).flatten() + data = c_array(c_float, arr) + im = IMAGE(w,h,c,data) + return im + def classify(net, meta, im): out = predict_image(net, im) res = [] @@ -110,7 +125,10 @@ def classify(net, meta, im): return res def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): - im = load_image(image, 0, 0) + if type(image) == numpy.ndarray: + im = array_to_image(image) + else: + im = load_image(image, 0, 0) boxes = make_boxes(net) probs = make_probs(net) num = num_boxes(net) @@ -121,10 +139,12 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): if probs[j][i] > 0: res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h))) res = sorted(res, key=lambda x: -x[1]) - free_image(im) + + if type(image) != numpy.ndarray: + free_image(im) free_ptrs(cast(probs, POINTER(c_void_p)), num) return res - + if __name__ == "__main__": #net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0) #im = load_image("data/wolf.jpg", 0, 0) @@ -135,5 +155,5 @@ if __name__ == "__main__": meta = load_meta("cfg/coco.data") r = detect(net, meta, "data/dog.jpg") print(r) - + From 6fc177fa5ce47d204b51ac875ced1da8c2f5b6f8 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Thu, 15 Mar 2018 13:33:41 -0700 Subject: [PATCH 3/5] merged --- python/darknet.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/darknet.py b/python/darknet.py index 203096b5..ca3c388f 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -13,7 +13,7 @@ def sample(probs): return len(probs)-1 def c_array(ctype, values): - arr = (ctype * len(values))() + arr = (ctype*len(values))() arr[:] = values return arr @@ -42,7 +42,7 @@ class METADATA(Structure): _fields_ = [("classes", c_int), ("names", POINTER(c_char_p))] - + #lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL) lib = CDLL("libdarknet.so", RTLD_GLOBAL) @@ -143,7 +143,7 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): free_image(im) free_detections(dets, num) return res - + if __name__ == "__main__": #net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0) #im = load_image("data/wolf.jpg", 0, 0) @@ -153,4 +153,6 @@ if __name__ == "__main__": net = load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = load_meta("cfg/coco.data") r = detect(net, meta, "data/dog.jpg") - print r + print(r) + + From fee68e39ce3e33422ccd7807b0ba17296d24d3e8 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Thu, 15 Mar 2018 18:13:29 -0700 Subject: [PATCH 4/5] added real numpy interface to detect --- python/darknet.py | 65 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/python/darknet.py b/python/darknet.py index b14d2448..21301b46 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -13,9 +13,21 @@ def sample(probs): return len(probs)-1 def c_array(ctype, values): - arr = (ctype*len(values))() - arr[:] = values - return arr + new_values = values.ctypes.data_as(POINTER(ctype)) + return new_values + +def array_to_image(arr): + import numpy as np + # need to return old values to avoid python freeing memory + arr = arr.transpose(2,0,1) + c = arr.shape[0] + h = arr.shape[1] + w = arr.shape[2] + arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0 + data = arr.ctypes.data_as(POINTER(c_float)) + im = IMAGE(w,h,c,data) + return im, arr + class BOX(Structure): _fields_ = [("x", c_float), @@ -42,7 +54,6 @@ class METADATA(Structure): _fields_ = [("classes", c_int), ("names", POINTER(c_char_p))] - #lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL) lib = CDLL("libdarknet.so", RTLD_GLOBAL) @@ -141,7 +152,27 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): free_image(im) free_detections(dets, num) return res - + +def detect_numpy(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): + im, arr = array_to_image(image) + num = c_int(0) + pnum = pointer(num) + predict_image(net, im) + dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) + num = pnum[0] + if (nms): do_nms_obj(dets, num, meta.classes, nms); + + res = [] + for j in range(num): + for i in range(meta.classes): + if dets[j].prob[i] > 0: + b = dets[j].bbox + res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h))) + res = sorted(res, key=lambda x: -x[1]) + free_detections(dets, num) + return res + + if __name__ == "__main__": #net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0) #im = load_image("data/wolf.jpg", 0, 0) @@ -150,7 +181,27 @@ if __name__ == "__main__": #print r[:10] net = load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = load_meta("cfg/coco.data") - r = detect(net, meta, "data/dog.jpg") + import scipy.misc + import time + ''' + t_start = time.time() + for ii in range(100): + r = detect(net, meta, 'data/dog.jpg') + print(time.time() - t_start) + print(r) + + image = scipy.misc.imread('data/dog.jpg') + for ii in range(100): + scipy.misc.imsave('/tmp/image.jpg', image) + r = detect(net, meta, '/tmp/image.jpg') + print(time.time() - t_start) + print(r) + ''' + + image = scipy.misc.imread('data/dog.jpg') + t_start = time.time() + for ii in range(100): + r = detect_numpy(net, meta, image) + print(time.time() - t_start) print(r) - From 1d37c748249720a687eaf4ee7dae040545dd0143 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Thu, 15 Mar 2018 18:21:21 -0700 Subject: [PATCH 5/5] removing changes for pull request, but those files don't work well anymore anyways --- .gitignore | 1 - examples/detector-scipy-opencv.py | 17 ++++++----------- examples/detector.py | 19 +++++++++---------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 40b39e0c..e01738d4 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,5 @@ ehthumbs.db Icon? Thumbs.db *.swp -*.weights *.a *.so diff --git a/examples/detector-scipy-opencv.py b/examples/detector-scipy-opencv.py index 88efaa24..bcf6475c 100644 --- a/examples/detector-scipy-opencv.py +++ b/examples/detector-scipy-opencv.py @@ -38,23 +38,18 @@ import darknet as dn # Darknet net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = dn.load_meta("cfg/coco.data") -import time -tStart = time.time() -for i in range(10): - r = dn.detect(net, meta, "data/dog.jpg") -print(time.time() - tStart) +r = dn.detect(net, meta, "data/dog.jpg") +print(r) # scipy arr= imread('data/dog.jpg') -tStart = time.time() -for i in range(10): - r = dn.detect(net, meta, arr) -print(time.time() - tStart) +im = array_to_image(arr) +r = detect2(net, meta, im) +print(r) # OpenCV arr = cv2.imread('data/dog.jpg') im = array_to_image(arr) dn.rgbgr_image(im) r = detect2(net, meta, im) -print r - +print(r) diff --git a/examples/detector.py b/examples/detector.py index 61868112..039c0f20 100644 --- a/examples/detector.py +++ b/examples/detector.py @@ -13,15 +13,14 @@ dn.set_gpu(0) net = dn.load_net("cfg/yolo-thor.cfg", "/home/pjreddie/backup/yolo-thor_final.weights", 0) meta = dn.load_meta("cfg/thor.data") r = dn.detect(net, meta, "data/bedroom.jpg") -print r +print(r) # And then down here you could detect a lot more images like: -rr = dn.detect(net, meta, "data/eagle.jpg") -print rr -rr = dn.detect(net, meta, "data/giraffe.jpg") -print rr -rr = dn.detect(net, meta, "data/horses.jpg") -print rr -rr = dn.detect(net, meta, "data/person.jpg") -print rr - +r = dn.detect(net, meta, "data/eagle.jpg") +print(r) +r = dn.detect(net, meta, "data/giraffe.jpg") +print(r) +r = dn.detect(net, meta, "data/horses.jpg") +print(r) +r = dn.detect(net, meta, "data/person.jpg") +print(r)