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.

456 lines
15 KiB

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_pScale = (float*)calloc(m_nOut_Channels, sizeof(float));
  151. if ( m_pScale == NULL ) return false;
  152. m_pGpuBinaryWeight = m_pPU->makeFloatArray(m_nWeight, m_pWeight);
  153. if (m_pGpuBinaryWeight == NULL) return false;
  154. };
  155. if (m_bXNOR){
  156. if (!m_pBinaryWeight){
  157. m_pBinaryWeight = (float*)calloc(m_nWeight, sizeof(float));
  158. if (m_pBinaryWeight==NULL)return false;
  159. };
  160. m_pBinaryInput = (float*)calloc(m_nBatch*m_nInput, sizeof(float));
  161. if (m_pBinaryInput == NULL)return false;
  162. #ifdef GPU
  163. if (m_bUseGPU){
  164. if (m_pGpuBinaryWeight == NULL){
  165. m_pGpuBinaryWeight = m_pPU->makeFloatArray(m_nWeight, m_pWeight);
  166. if (m_pGpuBinaryWeight == NULL)return false;
  167. };
  168. m_pGpuBinaryInput = m_pPU->makeFloatArray(m_nInput*m_nBatch);
  169. if (m_pGpuBinaryInput == NULL)return false;
  170. };
  171. #endif
  172. };
  173. if (m_bBatchNormalize){
  174. if (!m_pScale){
  175. m_pScale = (float*)calloc(m_nOut_Channels, sizeof(float));
  176. if (!m_pScale)return false;
  177. };
  178. m_pScaleUpdate = (float*)calloc(m_nOut_Channels, sizeof(float));
  179. if (!m_pScaleUpdate)return false;
  180. for (i = 0; i < m_nOut_Channels; ++i) m_pScale[i] = 1.f;
  181. m_pMean = (float*)calloc(m_nOut_Channels, sizeof(float));
  182. m_pVariance = (float*)calloc(m_nOut_Channels, sizeof(float));
  183. m_pMeanDelta = (float*)calloc(m_nOut_Channels, sizeof(float));
  184. m_pVarianceDelta = (float*)calloc(m_nOut_Channels, sizeof(float));
  185. m_pRollingMean = (float*)calloc(m_nOut_Channels, sizeof(float));
  186. m_pRollingVariance = (float*)calloc(m_nOut_Channels, sizeof(float));
  187. m_pX = (float*)calloc(m_nOutputVol, sizeof(float));
  188. m_pXNorm = (float*)calloc(m_nOutputVol, sizeof(float));
  189. #ifdef GPU
  190. if (m_bUseGPU){
  191. m_pGpuMean = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pMean);
  192. m_pGpuVariance = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pVariance);
  193. m_pGpuRollingMean = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pRollingMean);
  194. m_pGpuRollingVariance = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pRollingVariance);
  195. m_pGpuMeanDelta = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pMeanDelta);
  196. m_pGpuVarianceDelta = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pVarianceDelta);
  197. m_pGpuX = m_pPU->makeFloatArray(m_nOutputVol);//, m_pOutput);
  198. m_pGpuXNorm = m_pPU->makeFloatArray(m_nOutputVol);//, m_pOutput);
  199. };
  200. #endif
  201. };
  202. if (m_bAdam){
  203. m_pHostM = (float*)calloc(m_nWeight, sizeof(float));
  204. m_pHostV = (float*)calloc(m_nWeight, sizeof(float));
  205. #ifdef GPU
  206. if (m_bUseGPU){
  207. m_pGpuM = m_pPU->makeFloatArray(m_nWeight);//, m_pHostM);
  208. m_pGpuV = m_pPU->makeFloatArray(m_nWeight);//, m_pHostV);
  209. };
  210. #endif
  211. };
  212. #ifdef GPU
  213. if (m_bUseGPU){
  214. m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight);//, m_pWeight);
  215. m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);//, m_pWeightUpdate);
  216. m_pGpuBias = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pBias);
  217. m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pBiasUpdate);
  218. m_pGpuDelta = m_pPU->makeFloatArray(m_nBatch * m_nOutput);//, m_pDelta);
  219. m_pGpuOutput = m_pPU->makeFloatArray(m_nBatch * m_nOutput);//, m_pOutput);
  220. m_pGpuScale = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pScale);
  221. m_pGpuScaleUpdate = m_pPU->makeFloatArray(m_nOut_Channels);//, m_pScaleUpdate);
  222. #ifdef CUDNN
  223. cudnnCreateTensorDescriptor(&l.srcTensorDesc);
  224. cudnnCreateTensorDescriptor(&l.dstTensorDesc);
  225. cudnnCreateFilterDescriptor(&l.weightDesc);
  226. cudnnCreateTensorDescriptor(&l.dsrcTensorDesc);
  227. cudnnCreateTensorDescriptor(&l.ddstTensorDesc);
  228. cudnnCreateFilterDescriptor(&l.dweightDesc);
  229. cudnnCreateConvolutionDescriptor(&l.convDesc);
  230. cudnn_convolutional_setup(&l);
  231. #endif // CUDNN
  232. };
  233. #endif // GPU
  234. m_nWorkspace = calcWorkspaceSize();
  235. m_eActivation = activation;
  236. char tBuff[256];
  237. 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);
  238. LOG(tBuff);
  239. return true;
  240. };
  241. void convolutionalLayer::denormalize(void){
  242. uint32_t i, j;
  243. for (i = 0; i < m_nOut_Channels; ++i){
  244. float scale = (float)(m_pScale[i] / sqrt(m_pRollingVariance[i] + .00001f));
  245. for (j = 0; j < m_nIn_Channels*m_nSize*m_nSize; ++j){
  246. m_pWeight[i*m_nIn_Channels*m_nSize*m_nSize + j] *= scale;
  247. };
  248. m_pBias[i] -= m_pRollingMean[i] * scale;
  249. m_pScale[i] = 1.f;
  250. m_pRollingMean[i] = .0f;
  251. m_pRollingVariance[i] = 1.f;
  252. };
  253. };
  254. void convolutionalLayer::test(void){
  255. create(1, 5, 5, 3, 2, 5, 2, 1, LEAKY, true, false, false, false);
  256. float data[] = {
  257. 1, 1, 1, 1, 1,
  258. 1, 1, 1, 1, 1,
  259. 1, 1, 1, 1, 1,
  260. 1, 1, 1, 1, 1,
  261. 1, 1, 1, 1, 1,
  262. 2, 2, 2, 2, 2,
  263. 2, 2, 2, 2, 2,
  264. 2, 2, 2, 2, 2,
  265. 2, 2, 2, 2, 2,
  266. 2, 2, 2, 2, 2,
  267. 3, 3, 3, 3, 3,
  268. 3, 3, 3, 3, 3,
  269. 3, 3, 3, 3, 3,
  270. 3, 3, 3, 3, 3,
  271. 3, 3, 3, 3, 3 };
  272. cortexSTATE state = { 0 };
  273. state.input = data;
  274. forward(state);
  275. };
  276. bool convolutionalLayer::resize(uint32_t w, uint32_t h){
  277. m_nIn_Width = w;
  278. m_nIn_Height = h;
  279. m_nOut_Width = (m_nIn_Width + 2 * m_nPad - m_nSize) / m_nStride + 1;
  280. m_nOut_Height = (m_nIn_Height + 2 * m_nPad - m_nSize) / m_nStride + 1;
  281. m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
  282. m_nInput = m_nIn_Width * m_nIn_Height * m_nIn_Channels;
  283. m_pOutput = (float*)realloc(m_pOutput, m_nBatch*m_nOutput*sizeof(float));
  284. m_pDelta = (float*)realloc(m_pDelta, m_nBatch*m_nOutput*sizeof(float));
  285. if (m_bBatchNormalize){
  286. m_pX = (float*)realloc(m_pX, m_nBatch*m_nOutput*sizeof(float));
  287. m_pXNorm = (float*)realloc(m_pXNorm, m_nBatch*m_nOutput*sizeof(float));
  288. };
  289. #ifdef GPU
  290. m_pPU->freeMem( m_pGpuDelta );
  291. m_pPU->freeMem( m_pGpuOutput );
  292. m_pGpuDelta = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pDelta);
  293. m_pGpuOutput = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  294. if (m_bBatchNormalize){
  295. m_pPU->freeMem( m_pGpuX );
  296. m_pPU->freeMem( m_pGpuXNorm );
  297. m_pGpuX = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  298. m_pGpuXNorm = m_pPU->makeFloatArray(m_nBatch*m_nOutput, m_pOutput);
  299. };
  300. #ifdef CUDNN
  301. cudnn_setup();
  302. #endif
  303. #endif
  304. m_nWorkspace = calcWorkspaceSize();
  305. return true;
  306. };
  307. #ifdef GPU
  308. #ifdef CUDNN
  309. void convolutionalLayer::cudnn_setup(void){
  310. cudnnSetTensor4dDescriptor(l->dsrcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  311. cudnnSetTensor4dDescriptor(l->ddstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  312. cudnnSetFilter4dDescriptor(l->dweightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  313. cudnnSetTensor4dDescriptor(l->srcTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->c, l->h, l->w);
  314. cudnnSetTensor4dDescriptor(l->dstTensorDesc, CUDNN_TENSOR_NCHW, CUDNN_DATA_FLOAT, l->batch, l->out_c, l->out_h, l->out_w);
  315. cudnnSetFilter4dDescriptor(l->weightDesc, CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, l->n, l->c, l->size, l->size);
  316. #if(CUDNN_MAJOR >= 6)
  317. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION, CUDNN_DATA_FLOAT); // cudnn 6.0
  318. #else
  319. cudnnSetConvolution2dDescriptor(l->convDesc, l->pad, l->pad, l->stride, l->stride, 1, 1, CUDNN_CROSS_CORRELATION); // cudnn 5.1
  320. #endif
  321. cudnnGetConvolutionForwardAlgorithm(cudnn_handle(),
  322. l->srcTensorDesc,
  323. l->weightDesc,
  324. l->convDesc,
  325. l->dstTensorDesc,
  326. CUDNN_CONVOLUTION_FWD_PREFER_FASTEST,
  327. 0,
  328. &l->fw_algo);
  329. cudnnGetConvolutionBackwardDataAlgorithm(cudnn_handle(),
  330. l->weightDesc,
  331. l->ddstTensorDesc,
  332. l->convDesc,
  333. l->dsrcTensorDesc,
  334. CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST,
  335. 0,
  336. &l->bd_algo);
  337. cudnnGetConvolutionBackwardFilterAlgorithm(cudnn_handle(),
  338. l->srcTensorDesc,
  339. l->ddstTensorDesc,
  340. l->convDesc,
  341. l->dweightDesc,
  342. CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST,
  343. 0,
  344. &l->bf_algo);
  345. };
  346. #endif
  347. #endif
  348. void convolutionalLayer::forward(cortexSTATE state){
  349. m_pPU->forward_layer( this, state );
  350. };
  351. void convolutionalLayer::backward( cortexSTATE state ){
  352. m_pPU->backward_layer( this, state );
  353. };
  354. void convolutionalLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
  355. //uint32_t size = m_nSize * m_nSize * m_nIn_Channels * m_nOut_Channels;
  356. m_pPU->calcAxpy(m_nBias, learning_rate / batch, m_pBiasUpdate, 1, m_pBias, 1);
  357. m_pPU->calcScal(m_nBias, momentum, m_pBiasUpdate, 1);
  358. if(m_pScale){
  359. m_pPU->calcAxpy(m_nScale, learning_rate / batch, m_pScaleUpdate, 1, m_pScale, 1);
  360. m_pPU->calcScal(m_nScale, momentum, m_pScaleUpdate, 1);
  361. };
  362. m_pPU->calcAxpy(m_nWeight, -decay*batch, m_pWeight, 1, m_pWeightUpdate, 1);
  363. m_pPU->calcAxpy(m_nWeight, learning_rate / batch, m_pWeightUpdate, 1, m_pWeight, 1);
  364. m_pPU->calcScal(m_nWeight, momentum, m_pWeightUpdate, 1);
  365. };
  366. /*
  367. image get_convolutional_weight(convolutional_layer l, int i)
  368. {
  369. int h = l.size;
  370. int w = l.size;
  371. int c = l.c;
  372. return float_to_image(w, h, c, l.weights + i*h*w*c);
  373. }
  374. void rgbgr_weights(){
  375. int i;
  376. for (i = 0; i < l.n; ++i){
  377. image im = get_convolutional_weight(l, i);
  378. if (im.c == 3) {
  379. rgbgr_image(im);
  380. }
  381. }
  382. }
  383. void rescale_weights(convolutional_layer l, float scale, float trans)
  384. {
  385. int i;
  386. for (i = 0; i < l.n; ++i){
  387. image im = get_convolutional_weight(l, i);
  388. if (im.c == 3) {
  389. scale_image(im, scale);
  390. float sum = sum_array(im.data, im.w*im.h*im.c);
  391. l.biases[i] += sum*trans;
  392. }
  393. }
  394. }
  395. image *get_weights(convolutional_layer l)
  396. {
  397. image *weights = (image*)calloc(l.n, sizeof(image));
  398. int i;
  399. for (i = 0; i < l.n; ++i){
  400. weights[i] = copy_image(get_convolutional_weight(l, i));
  401. //normalize_image(weights[i]);
  402. }
  403. return weights;
  404. }
  405. image *visualize(convolutional_layer l, char *window, image *prev_weights)
  406. {
  407. image *single_weights = get_weights(l);
  408. show_images(single_weights, l.n, window);
  409. image delta = get_convolutional_image(l);
  410. image dc = collapse_image_layers(delta, 1);
  411. char buff[256];
  412. sprintf(buff, "%s: Output", window);
  413. //show_image(dc, buff);
  414. //save_image(dc, buff);
  415. free_image(dc);
  416. return single_weights;
  417. }
  418. */
  419. #undef LOG
  420. #undef LOG_ERROR