You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda_runtime.h"
  2. #include "curand.h"
  3. #include "cublas_v2.h"
  4. //extern "C" {
  5. #include "../base/basicUtils.h"
  6. #include "cuda.h"
  7. #include "../base/basicImage.h"
  8. #include "../layers/cropLayer.h"
  9. //}
  10. __device__ float get_pixel_kernel(float *image, int w, int h, int x, int y, int c)
  11. {
  12. if(x < 0 || x >= w || y < 0 || y >= h) return 0;
  13. return image[x + w*(y + c*h)];
  14. }
  15. __device__ float3 rgb_to_hsv_kernel(float3 rgb)
  16. {
  17. float r = rgb.x;
  18. float g = rgb.y;
  19. float b = rgb.z;
  20. float h, s, v;
  21. float max = (r > g) ? ( (r > b) ? r : b) : ( (g > b) ? g : b);
  22. float min = (r < g) ? ( (r < b) ? r : b) : ( (g < b) ? g : b);
  23. float delta = max - min;
  24. v = max;
  25. if(max == 0){
  26. s = 0;
  27. h = -1;
  28. }else{
  29. s = delta/max;
  30. if(r == max){
  31. h = (g - b) / delta;
  32. } else if (g == max) {
  33. h = 2 + (b - r) / delta;
  34. } else {
  35. h = 4 + (r - g) / delta;
  36. }
  37. if (h < 0) h += 6;
  38. }
  39. return make_float3(h, s, v);
  40. }
  41. __device__ float3 hsv_to_rgb_kernel(float3 hsv)
  42. {
  43. float h = hsv.x;
  44. float s = hsv.y;
  45. float v = hsv.z;
  46. float r, g, b;
  47. float f, p, q, t;
  48. if (s == 0) {
  49. r = g = b = v;
  50. } else {
  51. int index = (int) floorf(h);
  52. f = h - index;
  53. p = v*(1-s);
  54. q = v*(1-s*f);
  55. t = v*(1-s*(1-f));
  56. if(index == 0){
  57. r = v; g = t; b = p;
  58. } else if(index == 1){
  59. r = q; g = v; b = p;
  60. } else if(index == 2){
  61. r = p; g = v; b = t;
  62. } else if(index == 3){
  63. r = p; g = q; b = v;
  64. } else if(index == 4){
  65. r = t; g = p; b = v;
  66. } else {
  67. r = v; g = p; b = q;
  68. }
  69. }
  70. r = (r < 0) ? 0 : ((r > 1) ? 1 : r);
  71. g = (g < 0) ? 0 : ((g > 1) ? 1 : g);
  72. b = (b < 0) ? 0 : ((b > 1) ? 1 : b);
  73. return make_float3(r, g, b);
  74. }
  75. __device__ float bilinear_interpolate_kernel(float *image, int w, int h, float x, float y, int c)
  76. {
  77. int ix = (int) floorf(x);
  78. int iy = (int) floorf(y);
  79. float dx = x - ix;
  80. float dy = y - iy;
  81. float val = (1-dy) * (1-dx) * get_pixel_kernel(image, w, h, ix, iy, c) +
  82. dy * (1-dx) * get_pixel_kernel(image, w, h, ix, iy+1, c) +
  83. (1-dy) * dx * get_pixel_kernel(image, w, h, ix+1, iy, c) +
  84. dy * dx * get_pixel_kernel(image, w, h, ix+1, iy+1, c);
  85. return val;
  86. }
  87. __global__ void levels_image_kernel(float *image, float *rand, int batch, int w, int h, int train, float saturation, float exposure, float translate, float scale, float shift)
  88. {
  89. int size = batch * w * h;
  90. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  91. if(id >= size) return;
  92. int x = id % w;
  93. id /= w;
  94. int y = id % h;
  95. id /= h;
  96. float rshift = rand[0];
  97. float gshift = rand[1];
  98. float bshift = rand[2];
  99. float r0 = rand[8*id + 0];
  100. float r1 = rand[8*id + 1];
  101. float r2 = rand[8*id + 2];
  102. float r3 = rand[8*id + 3];
  103. saturation = r0*(saturation - 1) + 1;
  104. saturation = (r1 > .5) ? 1./saturation : saturation;
  105. exposure = r2*(exposure - 1) + 1;
  106. exposure = (r3 > .5) ? 1./exposure : exposure;
  107. size_t offset = id * h * w * 3;
  108. image += offset;
  109. float r = image[x + w*(y + h*0)];
  110. float g = image[x + w*(y + h*1)];
  111. float b = image[x + w*(y + h*2)];
  112. float3 rgb = make_float3(r,g,b);
  113. if(train){
  114. float3 hsv = rgb_to_hsv_kernel(rgb);
  115. hsv.y *= saturation;
  116. hsv.z *= exposure;
  117. rgb = hsv_to_rgb_kernel(hsv);
  118. } else {
  119. shift = 0;
  120. }
  121. image[x + w*(y + h*0)] = rgb.x*scale + translate + (rshift - .5)*shift;
  122. image[x + w*(y + h*1)] = rgb.y*scale + translate + (gshift - .5)*shift;
  123. image[x + w*(y + h*2)] = rgb.z*scale + translate + (bshift - .5)*shift;
  124. }
  125. __global__ void forward_crop_layer_kernel(float *input, float *rand, int size, int c, int h, int w, int crop_height, int crop_width, int train, int flip, float angle, float *output)
  126. {
  127. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  128. if(id >= size) return;
  129. float cx = w/2.;
  130. float cy = h/2.;
  131. int count = id;
  132. int j = id % crop_width;
  133. id /= crop_width;
  134. int i = id % crop_height;
  135. id /= crop_height;
  136. int k = id % c;
  137. id /= c;
  138. int b = id;
  139. float r4 = rand[8*b + 4];
  140. float r5 = rand[8*b + 5];
  141. float r6 = rand[8*b + 6];
  142. float r7 = rand[8*b + 7];
  143. float dw = (w - crop_width)*r4;
  144. float dh = (h - crop_height)*r5;
  145. flip = (flip && (r6 > .5));
  146. angle = 2*angle*r7 - angle;
  147. if(!train){
  148. dw = (w - crop_width)/2.;
  149. dh = (h - crop_height)/2.;
  150. flip = 0;
  151. angle = 0;
  152. }
  153. input += w*h*c*b;
  154. float x = (flip) ? w - dw - j - 1 : j + dw;
  155. float y = i + dh;
  156. float rx = cos(angle)*(x-cx) - sin(angle)*(y-cy) + cx;
  157. float ry = sin(angle)*(x-cx) + cos(angle)*(y-cy) + cy;
  158. output[count] = bilinear_interpolate_kernel(input, w, h, rx, ry, k);
  159. }
  160. void gpuCUDA::forward_layer( cropLayer *layer, cortexSTATE state ){
  161. makeRandom( layer->m_pGpuRand, layer->m_nBatch << 3 );// *8);
  162. float rad_angle = layer->m_fAngle * 3.14159265f/180.f;
  163. float scale = 2.f;
  164. float translate = -1.f;
  165. if( layer->m_bNoAdjust ){
  166. scale = 1.f;
  167. translate = 0.f;
  168. };
  169. int size = layer->m_nBatch * layer->m_nIn_Width * layer->m_nIn_Height;
  170. levels_image_kernel<<<gridSize(size), BLOCK>>>(
  171. state.input,
  172. layer->m_pGpuRand,
  173. layer->m_nBatch,
  174. layer->m_nIn_Width,
  175. layer->m_nIn_Height,
  176. state.train,
  177. layer->m_fSaturation,
  178. layer->m_fExposure,
  179. translate,
  180. scale,
  181. layer->m_fShift
  182. );
  183. checkError();// cudaPeekAtLastError());
  184. size = layer->m_nBatch * layer->m_nIn_Channels * layer->m_nOut_Width * layer->m_nOut_Height;
  185. forward_crop_layer_kernel<<<gridSize(size), BLOCK>>>(
  186. state.input,
  187. layer->m_pGpuRand,
  188. size,
  189. layer->m_nIn_Channels,
  190. layer->m_nIn_Height,
  191. layer->m_nIn_Width,
  192. layer->m_nOut_Height,
  193. layer->m_nOut_Width,
  194. state.train,
  195. layer->m_bFlipped,
  196. rad_angle,
  197. layer->m_pGpuOutput
  198. );
  199. checkError();// cudaPeekAtLastError());
  200. /*
  201. cuda_pull_array(layer.output_gpu, layer.output, size);
  202. image im = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 0*(size/layer.batch));
  203. image im2 = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 1*(size/layer.batch));
  204. image im3 = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 2*(size/layer.batch));
  205. translate_image(im, -translate);
  206. scale_image(im, 1/scale);
  207. translate_image(im2, -translate);
  208. scale_image(im2, 1/scale);
  209. translate_image(im3, -translate);
  210. scale_image(im3, 1/scale);
  211. show_image(im, "cropped");
  212. show_image(im2, "cropped2");
  213. show_image(im3, "cropped3");
  214. cvWaitKey(0);
  215. */
  216. }