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.

237 lines
6.2 KiB

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