Skip to content

Commit 626d511

Browse files
author
lintaowx
committed
add new file
1 parent d5b4233 commit 626d511

6 files changed

Lines changed: 381 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
COMMON= -DGPU -I/usr/local/cuda/include/
2+
COMMON+= -DCUDNN
3+
ARCH= -gencode arch=compute_52,code=[sm_52,compute_52]
4+
CFLAGS=-Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas
5+
LDFLAGS= -lm -pthread
6+
7+
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
8+
LDFLAGS+= -lcudnn
9+
LDFLAGS+= -lstdc++
10+
11+
CFLAGS+= -DGPU
12+
CFLAGS+= -DCUDNN -I/usr/local/cudnn/include
13+
CFLAGS+= -O2
14+
15+
SLIB=images.so
16+
OBJDIR=./obj/
17+
OBJ=images.o images_kernel.o
18+
OBJS = $(addprefix $(OBJDIR), $(OBJ))
19+
20+
all:obj $(SLIB)
21+
22+
$(SLIB): $(OBJS)
23+
gcc $(COMMON) $(CFLAGS) -fPIC -shared -o $@ $^ $(LDFLAGS)
24+
25+
$(OBJDIR)%.o: %.c
26+
gcc $(COMMON) $(CFLAGS) -fpic -c $< -o $@
27+
28+
$(OBJDIR)%.o: %.cu
29+
nvcc $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -Xcompiler -fpic -o $@
30+
31+
32+
33+
obj:
34+
mkdir -p obj
35+
36+
clean:
37+
rm -f *.so ./obj/*.o

‎images.c‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include "images.h"
2+
3+
Image* init_cuda_memory(int srcWidth,int srcHeight,int dstWidth,int dstHeight){
4+
5+
Image* im = (Image*)calloc(1,sizeof(Image));
6+
im->data = (float*)calloc(1,dstWidth*dstHeight*3*sizeof(float));
7+
im->dstHeight = dstHeight;
8+
im->dstWidth = dstWidth;
9+
im->srcWidth = srcWidth;
10+
im->srcHeight = srcHeight;
11+
cudaMalloc((void**)&(im->cu_src),srcWidth*srcHeight*3*sizeof(unsigned char));
12+
// cudaMemset(im->cu_src,0,srcWidth*srcHeight*3*sizeof(unsigned char));
13+
cudaMalloc((void**)&(im->cu_dst_resize),dstWidth*dstHeight*3*sizeof(unsigned char));
14+
// cudaMemset(im->cu_dst_resize,0,dstWidth*dstHeight*3*sizeof(unsigned char));
15+
cudaMalloc((void**)&(im->cu_dst),dstWidth*dstHeight*3*sizeof(float));
16+
// cudaMemset(im->cu_dst,0,dstWidth*dstHeight*3*sizeof(unsigned char));
17+
return im;
18+
}
19+
20+
21+
float overlap(float x1, float w1, float x2, float w2)
22+
{
23+
float l1 = x1;
24+
float l2 = x2;
25+
float left = l1 > l2 ? l1 : l2;
26+
float r1 = x1 + w1;
27+
float r2 = x2 + w2;
28+
float right = r1 < r2 ? r1 : r2;
29+
return right - left;
30+
}
31+
32+
float box_intersection(box a, box b)
33+
{
34+
float w = overlap(a.x, a.w, b.x, b.w);
35+
float h = overlap(a.y, a.h, b.y, b.h);
36+
if(w < 0 || h < 0) return 0;
37+
float area = w*h;
38+
return area;
39+
}
40+
41+
int nms_comparator(const void *pa, const void *pb)
42+
{
43+
box a = *(box *)pa;
44+
box b = *(box *)pb;
45+
float diff = a.conf - b.conf;
46+
if(diff < 0) return 1;
47+
else if(diff > 0) return -1;
48+
return 0;
49+
}
50+
51+
float box_union(box a, box b)
52+
{
53+
float i = box_intersection(a, b);
54+
float u = a.w*a.h + b.w*b.h - i;
55+
return u;
56+
}
57+
58+
float box_iou(box a, box b)
59+
{
60+
return box_intersection(a, b)/box_union(a, b);
61+
}
62+
63+
void do_nms_sort(float* dets, BBOX* bbox,int classes, float nms){
64+
int i,j,k;
65+
box det_arr[bbox->nums];
66+
for (i = 0; i < bbox->nums; i++){
67+
box a;
68+
a.x = dets[i*bbox->cols];
69+
a.y = dets[i*bbox->cols + 1];
70+
a.w = dets[i*bbox->cols + 2];
71+
a.h = dets[i*bbox->cols + 3];
72+
a.conf = dets[i*bbox->cols + 4];
73+
a.classify = dets[i*bbox->cols + 5];
74+
a.prob = 1;
75+
det_arr[i] = a;
76+
}
77+
for(k = 0; k < classes; ++k){
78+
qsort(det_arr,bbox->nums,sizeof(box),nms_comparator);
79+
for(i = 0; i < bbox->nums; ++i){
80+
if(det_arr[i].prob == 0) continue;
81+
box a = det_arr[i];
82+
for(j = i+1; j < bbox->nums; ++j){
83+
box b = det_arr[j];
84+
if (box_iou(a, b) > nms){
85+
det_arr[j].prob = 0;
86+
}
87+
}
88+
}
89+
}
90+
int count = 0;
91+
j = 0;
92+
for (i = 0; i < bbox->nums; ++i){
93+
if (det_arr[i].prob != 0){
94+
count++;
95+
k = j * bbox->cols;
96+
*(bbox->result + k) = det_arr[i].x;
97+
*(bbox->result + k+1) = det_arr[i].y;
98+
*(bbox->result + k+2) = det_arr[i].w;
99+
*(bbox->result + k+3) = det_arr[i].h;
100+
*(bbox->result + k+4) = det_arr[i].conf;
101+
*(bbox->result + k+5) = det_arr[i].classify;
102+
}
103+
else{
104+
j --;
105+
}
106+
j ++;
107+
108+
}
109+
bbox->nums = count;
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+

‎images.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<stdio.h>
2+
#include <stdlib.h>
3+
typedef struct Image{
4+
float* data; // output data
5+
int srcWidth; // width of original image
6+
int srcHeight; // height of original image
7+
int dstWidth; // width of target image
8+
int dstHeight; // height of target image
9+
float* cu_dst; // output image by cuda
10+
unsigned char* cu_src; // accept original by cuda
11+
unsigned char* cu_dst_resize; // save the resize image by cuda
12+
}Image;
13+
14+
typedef struct{
15+
int prob;
16+
int classify;
17+
float x, y, w, h,conf;
18+
} box;
19+
20+
typedef struct{
21+
int nums;
22+
int cols;
23+
float* result;
24+
} BBOX;
25+
26+
Image* init_cuda_memory(int srcWidth,int srcHeight,int dstWidth,int dstHeight);
27+
//Image* resize(const unsigned char*src,int srcWidth,int srcHeight,int dstWidth,int dstHeight);
28+
Image* resize_cu(const unsigned char*src,Image* img);

‎images_kernel.cu‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
extern "C" {
2+
#include "images.h"
3+
}
4+
5+
__global__ void resizeGPU(const unsigned char*src,int srcWidth,int srcHeight,
6+
unsigned char *dst_resize,int dstWidth,int dstHeight,
7+
float w_ratio,float h_ratio,float *dst)
8+
{
9+
const int x = blockIdx.x*blockDim.x+threadIdx.x;
10+
const int y = blockIdx.y*blockDim.y+threadIdx.y;
11+
if(x < dstWidth && y < dstHeight){
12+
float srcXf= x * w_ratio;
13+
float srcYf = y * h_ratio;
14+
int srcX = (int)srcXf;
15+
int srcY = (int)srcYf;
16+
float u= srcXf - srcX;
17+
float v = srcYf - srcY;
18+
int dstOffset = (y*dstWidth + x);
19+
int offset;
20+
//resize(inter_linear)
21+
for(int i = 0;i < 3;++i){
22+
offset = dstOffset*3 + i;
23+
dst_resize[offset] = (1-u)*(1-v)*src[(srcY*srcWidth+srcX)*3 + i];
24+
dst_resize[offset] += (1-u)*v*src[((srcY+1)*srcWidth+srcX)*3 + i];
25+
dst_resize[offset] += u*(1-v)*src[(srcY*srcWidth+srcX+1)*3 + i];
26+
dst_resize[offset] += u*v*src[((srcY+1)*srcWidth+srcX+1)*3 + i];
27+
}
28+
29+
// BGR_RGB and channel split.
30+
int image_patch = dstWidth*dstHeight;
31+
offset = dstOffset;
32+
// BGR2RGB
33+
// dst[offset*3 + 2] = dst_resize[offset*3];
34+
// dst[offset*3 + 1] = dst_resize[offset*3+1];
35+
// dst[offset*3] = dst_resize[offset*3+2];
36+
//merge
37+
38+
dst[offset] = dst_resize[offset*3+2] / 255.0f;
39+
dst[offset + image_patch] = dst_resize[offset*3+1] / 255.0f;
40+
dst[offset + image_patch*2] = dst_resize[offset*3] / 255.0f;
41+
42+
}
43+
}
44+
45+
Image* resize_cu(const unsigned char*src,Image* img){
46+
47+
cudaMemcpy(img->cu_src,src,img->srcWidth*img->srcHeight*3*sizeof(unsigned char),cudaMemcpyHostToDevice);
48+
int uint = 16;
49+
dim3 grid((img->dstWidth+uint-1)/uint,(img->dstHeight+uint-1)/uint);
50+
dim3 block(uint,uint);
51+
float w_ratio = (float)img->srcWidth/img->dstWidth;
52+
float h_ratio = (float)img->srcHeight/img->dstHeight;
53+
54+
resizeGPU<<<grid,block>>>(img->cu_src, img->srcWidth,img->srcHeight,
55+
img->cu_dst_resize,img->dstWidth,img->dstHeight,w_ratio,h_ratio,img->cu_dst);
56+
cudaMemcpy(img->data,img->cu_dst,img->dstHeight*img->dstWidth*3*sizeof(float),cudaMemcpyDeviceToHost);
57+
return img;
58+
59+
}
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+

‎main.py‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# coding:utf-8
2+
import cv2
3+
import time
4+
from ctypes import *
5+
import numpy as np
6+
7+
8+
class Image(Structure):
9+
_fields_ = [
10+
("data", POINTER(c_float)),
11+
("srcWidth", c_int),
12+
("srcHeight", c_int),
13+
("dstWidth", c_int),
14+
("dstHeight", c_int),
15+
("cu_dst", POINTER(c_float)),
16+
("cu_src", POINTER(c_ubyte)),
17+
("cu_dst_resize", POINTER(c_ubyte))
18+
]
19+
20+
21+
class BBOX(Structure):
22+
_fields_ = [
23+
("nums", c_int),
24+
("cols", c_int),
25+
("result", POINTER(c_float))
26+
]
27+
28+
29+
_so_path = './images.so'
30+
images_lib = CDLL(_so_path)
31+
32+
init_cuda_memory = images_lib.init_cuda_memory
33+
init_cuda_memory.argtypes = (c_int,c_int,c_int,c_int)
34+
init_cuda_memory.restype = POINTER(Image)
35+
36+
resize_cu = images_lib.resize_cu
37+
resize_cu.argtypes = [POINTER(c_ubyte),POINTER(Image)]
38+
resize_cu.restype = POINTER(Image)
39+
40+
do_nms_sort = images_lib.do_nms_sort
41+
do_nms_sort.argtypes = [POINTER(c_float),POINTER(BBOX),c_int,c_float]
42+
do_nms_sort.restype = None
43+
44+
45+
def test_nms():
46+
# nms by C and python invoke.
47+
data = np.array([[603.8855, 215.5790, 20.1108, 54.5654, 0.9998, 1.0000],
48+
[951.5989, 544.6505, 26.7356, 75.6343, 1.0000, 1.0000],
49+
[830.8037, 695.8022, 27.9502, 72.3551, 1.0000, 1.0000],
50+
[830.8037, 695.8022, 27.9502, 72.3551, 1.0000, 1.0000],
51+
[604.3386, 215.3251, 19.3738, 54.6253, 1.0000, 1.0000]], dtype=np.float32)
52+
time1 = time.time()
53+
bbox = BBOX()
54+
bbox.nums = data.shape[0]
55+
bbox.cols = data.shape[1]
56+
a = np.zeros(data.shape, dtype=np.float32)
57+
bbox.result = a.ctypes.data_as(POINTER(c_float))
58+
do_nms_sort(data.ctypes.data_as(POINTER(c_float)), bbox, 1, 0.5)
59+
bbox = np.ctypeslib.as_array(bbox.result, (bbox.nums, 6))
60+
time2 = time.time()
61+
print (time2 - time1)*1000
62+
print data
63+
print '============================='
64+
print bbox
65+
66+
67+
def test_images_cuda():
68+
im = cv2.imread('test.png')
69+
srcHeight,srcWidth,channel = im.shape
70+
dstHeight = 300
71+
dstWidth = 300
72+
struct_im = images_lib.init_cuda_memory(srcWidth, srcHeight, dstWidth, dstHeight)
73+
time1 = time.time()
74+
img = resize_cu(im.ctypes.data_as(POINTER(c_ubyte)), struct_im)
75+
image = np.ctypeslib.as_array(img.contents.data, (1, 3, dstHeight, dstWidth))
76+
time2 = time.time()
77+
print (time2 - time1)*1000
78+
print image.shape
79+
80+
81+
if __name__ == '__main__':
82+
# test_nms()
83+
test_images_cuda()
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+

‎test.png‎

339 KB
Loading

0 commit comments

Comments
 (0)