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.

523 lines
16 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "convolutionalLayer.h"
  2. #define LOG( X ) m_pLog->Log( "CONV", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "CONV", this, _LOG_ERROR, X )
  4. convolutionalLayer::convolutionalLayer(bool useGPU) :cortexLayer(CONVOLUTIONAL, useGPU){
  5. };
  6. convolutionalLayer::~convolutionalLayer(){
  7. };
  8. void convolutionalLayer::swapBinary(void){
  9. float *tSwap = m_pWeight;
  10. m_pWeight = m_pBinaryWeight;
  11. m_pBinaryWeight = tSwap;
  12. #ifdef GPU
  13. tSwap = m_pGpuWeight;
  14. m_pGpuWeight = m_pGpuBinaryWeight;
  15. m_pGpuBinaryWeight = tSwap;
  16. #endif
  17. };
  18. void convolutionalLayer::binarizeWeights(float *weights, uint32_t n, uint32_t size, float *binary){
  19. for (uint32_t f = 0; f < n; f++){
  20. float mean = 0;
  21. for (uint32_t i = 0; i < size; ++i){
  22. mean += (float)(fabs(weights[f*size + i]));
  23. };
  24. mean = mean / size;
  25. for (uint32_t i = 0; i < size; ++i){
  26. binary[f*size + i] = (weights[f*size + i] > 0) ? mean : -mean;
  27. };
  28. };
  29. };
  30. void convolutionalLayer::binarize(float *input, uint32_t n, float *binary){
  31. for (uint32_t i = 0; i < n; ++i){
  32. binary[i] = (input[i] > 0) ? 1.f : -1.f;
  33. };
  34. };
  35. void convolutionalLayer::binarizeInput(float *input, uint32_t n, uint32_t size, float *binary){
  36. for (uint32_t s = 0; s < size; ++s){
  37. float mean = 0;
  38. for (uint32_t i = 0; i < n; ++i){
  39. mean += (float)fabs(input[i*size + s]);
  40. };
  41. mean = mean / n;
  42. for (uint32_t i = 0; i < n; ++i){
  43. binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean;
  44. };
  45. };
  46. };
  47. //uint32_t convolutionalLayer::getOutputHeight(void){
  48. // return (m_nIn_Height + 2 * m_nPad - m_nSize) / m_nStride + 1; //?
  49. //};
  50. //uint32_t convolutionalLayer::getOutputWidth(void){
  51. // return (m_nIn_Width + 2 * m_nPad - m_nSize) / m_nStride + 1;
  52. //};
  53. cortexImage *convolutionalLayer::getOutputImage(void){
  54. cortexImage *tImage = NULL;
  55. tImage = new cortexImage();
  56. if (tImage != NULL){
  57. tImage->setData( m_nOut_Width, m_nOut_Height, m_nOut_Channels, m_pOutput);
  58. };
  59. return tImage;
  60. };
  61. cortexImage *convolutionalLayer::getDeltaImage(void){
  62. cortexImage *tImage = NULL;
  63. tImage = new cortexImage();
  64. if (tImage != NULL){
  65. tImage->setData( m_nOut_Width, m_nOut_Height, m_nOut_Channels, m_pDelta);
  66. };
  67. return tImage;
  68. };
  69. uint32_t convolutionalLayer::calcWorkspaceSize(){
  70. #ifdef GPU
  71. #ifdef CUDNN
  72. if( m_bUseGPU ){
  73. uint32_t most = 0;
  74. uint32_t s = 0;
  75. cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(),
  76. l.srcTensorDesc,
  77. l.weightDesc,
  78. l.convDesc,
  79. l.dstTensorDesc,
  80. l.fw_algo,
  81. &s);
  82. if (s > most) most = s;
  83. cudnnGetConvolutionBackwardFilterWorkspaceSize(cudnn_handle(),
  84. l.srcTensorDesc,
  85. l.ddstTensorDesc,
  86. l.convDesc,
  87. l.dweightDesc,
  88. l.bf_algo,
  89. &s);
  90. if (s > most) most = s;
  91. cudnnGetConvolutionBackwardDataWorkspaceSize(cudnn_handle(),
  92. l.weightDesc,
  93. l.ddstTensorDesc,
  94. l.convDesc,
  95. l.dsrcTensorDesc,
  96. l.bd_algo,
  97. &s);
  98. if (s > most) most = s;
  99. return most;
  100. };
  101. #endif // CUDNN
  102. return (uint32_t)(m_nOut_Height * m_nOut_Width * m_nSize * m_nSize * m_nIn_Channels * sizeof(float));
  103. #else // GPU
  104. return (uint32_t)(m_nOut_Height * m_nOut_Width * m_nSize * m_nSize * m_nIn_Channels * sizeof(float));
  105. #endif
  106. }
  107. bool convolutionalLayer::create(uint32_t batch, uint32_t h, uint32_t w, uint32_t c, uint32_t n, uint32_t size, uint32_t stride, uint32_t padding, ACTIVATION activation, bool batch_normalize, bool binary, bool xnor, bool adam){
  108. uint32_t i;
  109. m_nIn_Height = h;
  110. m_nIn_Width = w;
  111. m_nIn_Channels = c;
  112. m_nN = n;
  113. m_bBinary = binary;
  114. m_bXNOR = xnor;
  115. m_nBatch = batch;
  116. m_nStride = stride;
  117. m_nSize = size;
  118. m_nPad = padding;
  119. m_bBatchNormalize = batch_normalize;
  120. m_nOut_Height = (m_nIn_Height + 2 * m_nPad - m_nSize) / m_nStride + 1;
  121. m_nOut_Width = (m_nIn_Width + 2 * m_nPad - m_nSize) / m_nStride + 1;
  122. m_nOut_Channels = n;
  123. m_bAdam = adam;
  124. m_nWeight = m_nOut_Channels * m_nIn_Channels * m_nSize * m_nSize;
  125. m_pWeight = (float*)calloc(m_nWeight, sizeof(float));
  126. if ( m_pWeight == NULL ) return false;
  127. m_pWeightUpdate = (float*)calloc(m_nWeight, sizeof(float));
  128. if ( m_pWeightUpdate == NULL ) return false;
  129. m_nBias = m_nOut_Channels;
  130. m_pBias = (float*)calloc(m_nBias, sizeof(float));
  131. if ( m_pBias == NULL ) return false;
  132. m_pBiasUpdate = (float*)calloc(m_nBias, sizeof(float));
  133. if ( m_pBiasUpdate == NULL ) return false;
  134. //float scale = 1./sqrt(size*size*c);
  135. //float scale = (float)sqrt(2. / (size*size*c));
  136. float scale = (float)sqrt( 2.f / (m_nIn_Channels * m_nSize * m_nSize));
  137. for (i = 0; i < m_nWeight; ++i) m_pWeight[i] = scale*rand_uniform(-1, 1);
  138. m_nDelta = m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  139. m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
  140. m_nDeltaVol = m_nOutputVol = m_nBatch * m_nOutput;
  141. m_pOutput = (float*)calloc(m_nOutputVol, sizeof(float));
  142. if ( m_pOutput == NULL )return false;
  143. m_pDelta = (float*)calloc(m_nDeltaVol, sizeof(float));
  144. if ( m_pDelta == NULL )return false;
  145. if (m_bBinary){
  146. m_pBinaryWeight = (float*)calloc(m_nWeight, sizeof(float));
  147. if ( m_pBinaryWeight == NULL )return false;
  148. m_pCharWeight = (char*)calloc(m_nWeight, sizeof(char));
  149. if ( m_pCharWeight == NULL ) return false;
  150. m_nScale = m_nOut_Channels;
  151. m_pScale = (float*)calloc(m_nScale, sizeof(float));
  152. if ( m_pScale == NULL ) return false;
  153. m_pGpuBinaryWeight = m_pPU->makeFloatArray(m_nWeight, m_pWeight);
  154. if (m_pGpuBinaryWeight == NULL) return false;
  155. };
  156. if (m_bXNOR){
  157. if (!m_pBinaryWeight){
  158. m_pBinaryWeight = (float*)calloc(m_nWeight, sizeof(float));
  159. if (m_pBinaryWeight==NULL)return false;
  160. };
  161. m_pBinaryInput = (float*)calloc(m_nBatch*m_nInput, sizeof(float));
  162. if (m_pBinaryInput == NULL)return false;
  163. #ifdef GPU
  164. if (m_bUseGPU){
  165. if (m_pGpuBinaryWeight == NULL){
  166. m_pGpuBinaryWeight = m_pPU->makeFloatArray(m_nWeight, m_pWeight);
  167. if (m_pGpuBinaryWeight == NULL)return false;
  168. };
  169. m_pGpuBinaryInput = m_pPU->makeFloatArray(m_nInput*m_nBatch);
  170. if (m_pGpuBinaryInput == NULL)return false;
  171. };
  172. #endif
  173. };
  174. if (m_bBatchNormalize){
  175. if (!m_pScale){
  176. m_nScale = m_nOut_Channels;
  177. m_pScale = (float*)calloc(m_nScale, sizeof(float));
  178. if (!m_pScale)return false;
  179. };
  180. m_pScaleUpdate = (float*)calloc(m_nScale, sizeof(float));
  181. if (!m_pScaleUpdate)return false;
  182. for (i = 0; i < m_nScale; ++i) m_pScale[i] = 1.f;
  183. m_pMean = (float*)calloc(m_nScale, sizeof(float));
  184. m_pVariance = (float*)calloc(m_nScale, sizeof(float));
  185. m_pMeanDelta = (float*)calloc(m_nScale, sizeof(float));
  186. m_pVarianceDelta = (float*)calloc(m_nScale, sizeof(float));
  187. m_pRollingMean = (float*)calloc(m_nScale, sizeof(float));
  188. m_pRollingVariance = (float*)calloc(m_nScale, sizeof(float));
  189. m_pX = (float*)calloc(m_nOutputVol, sizeof(float));
  190. m_pXNorm = (float*)calloc(m_nOutputVol, sizeof(float));
  191. #ifdef GPU
  192. if (m_bUseGPU){
  193. m_pGpuMean = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pMean);
  194. m_pGpuVariance = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pVariance);
  195. m_pGpuRollingMean = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pRollingMean);
  196. m_pGpuRollingVariance = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pRollingVariance);
  197. m_pGpuMeanDelta = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pMeanDelta);
  198. m_pGpuVarianceDelta = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pVarianceDelta);
  199. m_pGpuX = m_pPU->makeFloatArray(m_nOutputVol);//, m_pOutput);
  200. m_pGpuXNorm = m_pPU->makeFloatArray(m_nOutputVol);//, m_pOutput);
  201. };
  202. #endif
  203. };
  204. if (m_bAdam){
  205. m_pHostM = (float*)calloc(m_nWeight, sizeof(float));
  206. m_pHostV = (float*)calloc(m_nWeight, sizeof(float));
  207. #ifdef GPU
  208. if (m_bUseGPU){
  209. m_pGpuM = m_pPU->makeFloatArray(m_nWeight);//, m_pHostM);
  210. m_pGpuV = m_pPU->makeFloatArray(m_nWeight);//, m_pHostV);
  211. };
  212. #endif
  213. };
  214. #ifdef GPU
  215. if (m_bUseGPU){
  216. m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight);//, m_pWeight);
  217. m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);//, m_pWeightUpdate);
  218. m_pGpuBias = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pBias);
  219. m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pBiasUpdate);
  220. m_pGpuDelta = m_pPU->makeFloatArray(m_nBatch * m_nOutput);//, m_pDelta);
  221. m_pGpuOutput = m_pPU->makeFloatArray(m_nBatch * m_nOutput);//, m_pOutput);
  222. m_pGpuScale = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pScale);
  223. m_pGpuScaleUpdate = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pScaleUpdate);
  224. #ifdef CUDNN
  225. cudnnCreateTensorDescriptor(&l.srcTensorDesc);
  226. cudnnCreateTensorDescriptor(&l.dstTensorDesc);
  227. cudnnCreateFilterDescriptor(&l.weightDesc);
  228. cudnnCreateTensorDescriptor(&l.dsrcTensorDesc);
  229. cudnnCreateTensorDescriptor(&l.ddstTensorDesc);
  230. cudnnCreateFilterDescriptor(&l.dweightDesc);
  231. cudnnCreateConvolutionDescriptor(&l.convDesc);
  232. cudnn_convolutional_setup(&l);
  233. #endif // CUDNN
  234. };
  235. #endif // GPU
  236. m_nWorkspace = calcWorkspaceSize();
  237. m_eActivation = activation;
  238. char tBuff[256];
  239. sprintf(tBuff, "\t%5d\t%2dx%2d\t%2d\t%4dx%4dx%4d -> %4dx%4dx%4d", n, size, size, stride, w, h, c, m_nOut_Width, m_nOut_Height, m_nOut_Channels);
  240. LOG(tBuff);
  241. return true;
  242. };
  243. void convolutionalLayer::denormalize(void){
  244. uint32_t i, j;
  245. for (i = 0; i < m_nOut_Channels; ++i){
  246. float scale = (float)(m_pScale[i] / sqrt(m_pRollingVariance[i] + .00001f));
  247. for (j = 0; j < m_nIn_Channels*m_nSize*m_nSize; ++j){
  248. m_pWeight[i*m_nIn_Channels*m_nSize*m_nSize + j] *= scale;
  249. };
  250. m_pBias[i] -= m_pRollingMean[i] * scale;
  251. m_pScale[i] = 1.f;
  252. m_pRollingMean[i] = .0f;
  253. m_pRollingVariance[i] = 1.f;
  254. };
  255. };
  256. void convolutionalLayer::test(void){
  257. create(1, 5, 5, 3, 2, 5, 2, 1, LEAKY, true, false, false, false);
  258. float data[] = {
  259. 1, 1, 1, 1, 1,
  260. 1, 1, 1, 1, 1,
  261. 1, 1, 1, 1, 1,
  262. 1, 1, 1, 1, 1,
  263. 1, 1, 1, 1, 1,
  264. 2, 2, 2, 2, 2,
  265. 2, 2, 2, 2, 2,
  266. 2, 2, 2, 2, 2,
  267. 2, 2, 2, 2, 2,
  268. 2, 2, 2, 2, 2,
  269. 3, 3, 3, 3, 3,
  270. 3, 3, 3, 3, 3,
  271. 3, 3, 3, 3, 3,
  272. 3, 3, 3, 3, 3,
  273. 3, 3, 3, 3, 3 };
  274. cortexSTATE state = { 0 };
  275. state.input = data;
  276. forward(state);
  277. };
  278. bool convolutionalLayer::resize(uint32_t w, uint32_t h){
  279. m_nIn_Width = w;
  280. m_nIn_Height = h;
  281. m_nOut_Width = (m_nIn_Width + 2 * m_nPad - m_nSize) / m_nStride + 1;
  282. m_nOut_Height = (m_nIn_Height + 2 * m_nPad - m_nSize) / m_nStride + 1;
  283. m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  284. m_nInput = m_nIn_Width * m_nIn_Height * m_nIn_Channels;
  285. m_pOutput = (float*)realloc(m_pOutput, m_nBatch*m_nOutput*sizeof(float));
  286. m_pDelta = (float*)realloc(m_pDelta, m_nBatch*m_nOutput*sizeof(float));
  287. if (m_bBatchNormalize){
  288. m_pX = (float*)realloc(m_pX, m_nBatch*m_nOutput*sizeof(float));
  289. m_pXNorm = (float*)realloc(m_pXNorm, m_nBatch*m_nOutput*sizeof(float));
  290. };
  291. #ifdef GPU
  292. m_pPU->freeMem( m_pGpuDelta );
  293. m_pPU->freeMem( m_pGpuOutput );
  294. m_pGpuDelta = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pDelta);
  295. m_pGpuOutput = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  296. if (m_bBatchNormalize){
  297. m_pPU->freeMem( m_pGpuX );
  298. m_pPU->freeMem( m_pGpuXNorm );
  299. m_pGpuX = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  300. m_pGpuXNorm = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  301. };
  302. #ifdef CUDNN
  303. cudnn_setup();
  304. #endif
  305. #endif
  306. m_nWorkspace = calcWorkspaceSize();
  307. return true;
  308. };
  309. #ifdef GPU
  310. #ifdef CUDNN
  311. void convolutionalLayer::cudnn_setup(void){
  312. cudnnSetTensor4dDescriptor(l->dsrcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  313. cudnnSetTensor4dDescriptor(l->ddstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  314. cudnnSetFilter4dDescriptor(l->dweightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  315. cudnnSetTensor4dDescriptor(l->srcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  316. cudnnSetTensor4dDescriptor(l->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  317. cudnnSetFilter4dDescriptor(l->weightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  318. #if(CUDNN_MAJOR >= 6)
  319. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION, CUDNN_DATA_FLOAT); // cudnn 6.0
  320. #else
  321. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION); // cudnn 5.1
  322. #endif
  323. cudnnGetConvolutionForwardAlgorithm(cudnn_handle(),
  324. l->srcTensorDesc,
  325. l->weightDesc,
  326. l->convDesc,
  327. l->dstTensorDesc,
  328. CUDNN_CONVOLUTION_FWD_PREFER_FASTEST,
  329. 0,
  330. &l->fw_algo);
  331. cudnnGetConvolutionBackwardDataAlgorithm(cudnn_handle(),
  332. l->weightDesc,
  333. l->ddstTensorDesc,
  334. l->convDesc,
  335. l->dsrcTensorDesc,
  336. CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST,
  337. 0,
  338. &l->bd_algo);
  339. cudnnGetConvolutionBackwardFilterAlgorithm(cudnn_handle(),
  340. l->srcTensorDesc,
  341. l->ddstTensorDesc,
  342. l->convDesc,
  343. l->dweightDesc,
  344. CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST,
  345. 0,
  346. &l->bf_algo);
  347. };
  348. #endif
  349. #endif
  350. void convolutionalLayer::forward(cortexSTATE state){
  351. m_pPU->forward_layer( this, state );
  352. };
  353. void convolutionalLayer::backward( cortexSTATE state ){
  354. m_pPU->backward_layer( this, state );
  355. };
  356. void convolutionalLayer::loadWeights(FILE *fp, bool trans){
  357. if (m_bBinary){
  358. fread(m_pBias, sizeof(float), m_nN, fp);
  359. if (m_bBatchNormalize && (!m_bDontLoadScales)){
  360. fread(m_pScale, sizeof(float), m_nN, fp);
  361. fread(m_pRollingMean, sizeof(float), m_nN, fp);
  362. fread(m_pRollingVariance, sizeof(float), m_nN, fp);
  363. };
  364. int size = m_nIn_Channels*m_nSize*m_nSize;// l.c*l.size*l.size;
  365. int i, j, k;
  366. for (i = 0; i < m_nN; ++i){
  367. float mean = 0;
  368. fread(&mean, sizeof(float), 1, fp);
  369. for (j = 0; j < (size >> 3); ++j){
  370. int index = i*size + (j << 3);
  371. unsigned char c = 0;
  372. fread(&c, sizeof(char), 1, fp);
  373. for (k = 0; k < 8; ++k){
  374. if (j * 8 + k >= size) break;
  375. m_pWeight[index + k] = (c & 1 << k) ? mean : -mean;
  376. };
  377. };
  378. };
  379. #ifdef GPU
  380. m_pPU->push_layer(this);
  381. #endif
  382. } else {
  383. int num = m_nN*m_nIn_Channels*m_nSize*m_nSize;//l.n*l.c*l.size*l.size;
  384. fread(m_pBias, sizeof(float), m_nN, fp);
  385. if (m_bBatchNormalize && (!m_bDontLoadScales)){
  386. fread(m_pScale, sizeof(float), m_nN, fp);
  387. fread(m_pRollingMean, sizeof(float), m_nN, fp);
  388. fread(m_pRollingVariance, sizeof(float), m_nN, fp);
  389. if (0){
  390. int i;
  391. for (i = 0; i < m_nN; ++i){
  392. printf("%g, ", m_pRollingMean[i]);
  393. };
  394. printf("\n");
  395. for (i = 0; i < m_nN; ++i){
  396. printf("%g, ", m_pRollingVariance[i]);
  397. };
  398. printf("\n");
  399. };
  400. if (0){
  401. m_pPU->makeFill(m_nN, 0, m_pRollingMean, 1);
  402. m_pPU->makeFill(m_nN, 0, m_pRollingVariance, 1);
  403. };
  404. };
  405. fread(m_pWeight, sizeof(float), num, fp);
  406. if (m_bAdam){
  407. fread(m_pHostM, sizeof(float), num, fp);
  408. fread(m_pHostV, sizeof(float), num, fp);
  409. };
  410. //if(l.c == 3) scal_cpu(num, 1./256, l.weights, 1);
  411. if (m_bFlipped) {
  412. m_pPU->transposeMatrix(m_pWeight, m_nIn_Channels*m_nSize*m_nSize, m_nN);
  413. };
  414. //if (l.binary) binarize_weights(l.weights, l.n, l.c*l.size*l.size, l.weights);
  415. #ifdef GPU
  416. m_pPU->push_layer(this);
  417. #endif
  418. };
  419. };
  420. void convolutionalLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
  421. //uint32_t size = m_nSize * m_nSize * m_nIn_Channels * m_nOut_Channels;
  422. m_pPU->calcAxpy(m_nBias, learning_rate / batch, m_pBiasUpdate, 1, m_pBias, 1);
  423. m_pPU->calcScal(m_nBias, momentum, m_pBiasUpdate, 1);
  424. if(m_pScale){
  425. m_pPU->calcAxpy(m_nScale, learning_rate / batch, m_pScaleUpdate, 1, m_pScale, 1);
  426. m_pPU->calcScal(m_nScale, momentum, m_pScaleUpdate, 1);
  427. };
  428. m_pPU->calcAxpy(m_nWeight, -decay*batch, m_pWeight, 1, m_pWeightUpdate, 1);
  429. m_pPU->calcAxpy(m_nWeight, learning_rate / batch, m_pWeightUpdate, 1, m_pWeight, 1);
  430. m_pPU->calcScal(m_nWeight, momentum, m_pWeightUpdate, 1);
  431. };
  432. /*
  433. image get_convolutional_weight(convolutional_layer l, int i)
  434. {
  435. int h = l.size;
  436. int w = l.size;
  437. int c = l.c;
  438. return float_to_image(w, h, c, l.weights + i*h*w*c);
  439. }
  440. void rgbgr_weights(){
  441. int i;
  442. for (i = 0; i < l.n; ++i){
  443. image im = get_convolutional_weight(l, i);
  444. if (im.c == 3) {
  445. rgbgr_image(im);
  446. }
  447. }
  448. }
  449. void rescale_weights(convolutional_layer l, float scale, float trans)
  450. {
  451. int i;
  452. for (i = 0; i < l.n; ++i){
  453. image im = get_convolutional_weight(l, i);
  454. if (im.c == 3) {
  455. scale_image(im, scale);
  456. float sum = sum_array(im.data, im.w*im.h*im.c);
  457. l.biases[i] += sum*trans;
  458. }
  459. }
  460. }
  461. image *get_weights(convolutional_layer l)
  462. {
  463. image *weights = (image*)calloc(l.n, sizeof(image));
  464. int i;
  465. for (i = 0; i < l.n; ++i){
  466. weights[i] = copy_image(get_convolutional_weight(l, i));
  467. //normalize_image(weights[i]);
  468. }
  469. return weights;
  470. }
  471. image *visualize(convolutional_layer l, char *window, image *prev_weights)
  472. {
  473. image *single_weights = get_weights(l);
  474. show_images(single_weights, l.n, window);
  475. image delta = get_convolutional_image(l);
  476. image dc = collapse_image_layers(delta, 1);
  477. char buff[256];
  478. sprintf(buff, "%s: Output", window);
  479. //show_image(dc, buff);
  480. //save_image(dc, buff);
  481. free_image(dc);
  482. return single_weights;
  483. }
  484. */
  485. #undef LOG
  486. #undef LOG_ERROR