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.

107 lines
2.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "crop_layer.h"
  2. #include "dn_cuda.h"
  3. #include <stdio.h>
  4. namespace DN{
  5. image get_crop_image(crop_layer l)
  6. {
  7. int h = l.out_h;
  8. int w = l.out_w;
  9. int c = l.out_c;
  10. return float_to_image(w, h, c, l.output);
  11. }
  12. void backward_crop_layer(const crop_layer l, network net){}
  13. void backward_crop_layer_gpu(const crop_layer l, network net){}
  14. crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure)
  15. {
  16. fprintf(stderr, "Crop Layer: %d x %d -> %d x %d x %d image\n", h, w, crop_height, crop_width, c);
  17. crop_layer l;
  18. memset(&l, 0, sizeof(crop_layer));
  19. l.type = CROP;
  20. l.batch = batch;
  21. l.h = h;
  22. l.w = w;
  23. l.c = c;
  24. l.scale = (float)crop_height / h;
  25. l.flip = flip;
  26. l.angle = angle;
  27. l.saturation = saturation;
  28. l.exposure = exposure;
  29. l.out_w = crop_width;
  30. l.out_h = crop_height;
  31. l.out_c = c;
  32. l.inputs = l.w * l.h * l.c;
  33. l.outputs = l.out_w * l.out_h * l.out_c;
  34. l.output = (float*)calloc(l.outputs*batch, sizeof(float));
  35. l.forward = forward_crop_layer;
  36. l.backward = backward_crop_layer;
  37. #ifdef GPU
  38. l.forward_gpu = forward_crop_layer_gpu;
  39. l.backward_gpu = backward_crop_layer_gpu;
  40. l.output_gpu = cuda_make_array(l.output, l.outputs*batch);
  41. l.rand_gpu = cuda_make_array(0, l.batch * 8);
  42. #endif
  43. return l;
  44. }
  45. void resize_crop_layer(layer *l, int w, int h)
  46. {
  47. l->w = w;
  48. l->h = h;
  49. l->out_w = (int)(l->scale*w);
  50. l->out_h = (int)(l->scale*h);
  51. l->inputs = l->w * l->h * l->c;
  52. l->outputs = l->out_h * l->out_w * l->out_c;
  53. l->output = (float*)realloc(l->output, l->batch*l->outputs*sizeof(float));
  54. #ifdef GPU
  55. cuda_free(l->output_gpu);
  56. l->output_gpu = cuda_make_array(l->output, l->outputs*l->batch);
  57. #endif
  58. }
  59. void forward_crop_layer(const crop_layer l, network net)
  60. {
  61. int i, j, c, b, row, col;
  62. int index;
  63. int count = 0;
  64. int flip = (l.flip && rand() % 2);
  65. int dh = rand() % (l.h - l.out_h + 1);
  66. int dw = rand() % (l.w - l.out_w + 1);
  67. float scale = 2;
  68. float trans = -1;
  69. if (l.noadjust){
  70. scale = 1;
  71. trans = 0;
  72. }
  73. if (!net.train){
  74. flip = 0;
  75. dh = (l.h - l.out_h) / 2;
  76. dw = (l.w - l.out_w) / 2;
  77. }
  78. for (b = 0; b < l.batch; ++b){
  79. for (c = 0; c < l.c; ++c){
  80. for (i = 0; i < l.out_h; ++i){
  81. for (j = 0; j < l.out_w; ++j){
  82. if (flip){
  83. col = l.w - dw - j - 1;
  84. }
  85. else{
  86. col = j + dw;
  87. }
  88. row = i + dh;
  89. index = col + l.w*(row + l.h*(c + l.c*b));
  90. l.output[count++] = net.input[index] * scale + trans;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. };