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.

47 lines
1.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda.h"
  2. #include "../layers/regionLayer.h"
  3. void gpuCUDA::forward_layer(regionLayer *l, cortexSTATE state){
  4. ///*
  5. if(!state.train){
  6. makeCopy( l->m_nBatch*l->m_nInput, state.input, 1, l->m_pGpuOutput, 1);
  7. return;
  8. };
  9. //*/
  10. makeFlatten(state.input, l->m_nIn_Height*l->m_nIn_Width, l->m_nN*(l->m_nCoords + l->m_nClasses + 1), l->m_nBatch, 1, l->m_pGpuOutput);
  11. if (l->m_pSoftmaxTree != NULL){
  12. uint32_t count = 5;
  13. for (uint32_t i = 0; i < l->m_pSoftmaxTree->groups; ++i) {
  14. int group_size = l->m_pSoftmaxTree->group_size[i];
  15. makeSoftMax(l->m_pGpuOutput + count, group_size, l->m_nClasses + 5, l->m_nIn_Height*l->m_nIn_Width*l->m_nN*l->m_nBatch, 1, l->m_pGpuOutput + count);
  16. count += group_size;
  17. };
  18. } else if (l->m_bSoftMax){
  19. makeSoftMax(l->m_pGpuOutput + 5, l->m_nClasses, l->m_nClasses + 5, l->m_nIn_Height*l->m_nIn_Width*l->m_nN*l->m_nBatch, 1, l->m_pGpuOutput + 5);
  20. };
  21. float *in_cpu = (float *)calloc(l->m_nOutput, sizeof(float));
  22. float *truth_cpu = NULL;
  23. if (state.truth){
  24. int num_truth = l->m_nBatch*l->m_nTruth;
  25. truth_cpu = (float *)calloc(num_truth, sizeof(float));
  26. pullFloatArray(state.truth, truth_cpu, num_truth);
  27. };
  28. pullFloatArray(l->m_pGpuOutput, in_cpu, l->m_nOutput);
  29. cortexSTATE cpu_state = state;
  30. cpu_state.train = state.train;
  31. cpu_state.truth = truth_cpu;
  32. cpu_state.input = in_cpu;
  33. forward_layer(l, cpu_state);
  34. pushFloatArray(l->m_pGpuOutput, l->m_pOutput, l->m_nOutput);
  35. free(cpu_state.input);
  36. if (!state.train) return;
  37. pushFloatArray(l->m_pGpuDelta, l->m_pDelta, l->m_nDelta);
  38. if (cpu_state.truth != NULL)free(cpu_state.truth);
  39. };
  40. void gpuCUDA::backward_layer(regionLayer *l, cortexSTATE state){
  41. makeFlatten(l->m_pGpuDelta, l->m_nIn_Height*l->m_nIn_Width, l->m_nN*(l->m_nCoords + l->m_nClasses + 1), l->m_nBatch, 0, state.delta);
  42. };