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.
100 lines
3.3 KiB
100 lines
3.3 KiB
#include "localLayer.h"
|
|
/*
|
|
#include "utils.h"
|
|
#include "im2col.h"
|
|
#include "col2im.h"
|
|
#include "blas.h"
|
|
#include "gemm.h"
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
*/
|
|
|
|
localLayer::localLayer(bool useGPU) :cortexLayer(LOCAL, useGPU){
|
|
m_nColImage = 0;
|
|
m_pColImage = NULL;
|
|
m_pGpuColImage = NULL;
|
|
};
|
|
|
|
localLayer::~localLayer(){
|
|
if (m_pPU != NULL){
|
|
if (m_pGpuColImage != NULL)m_pPU->freeMem(m_pGpuColImage);
|
|
};
|
|
if (m_pColImage != NULL) free(m_pColImage);
|
|
};
|
|
|
|
bool localLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t c, uint32_t n, uint32_t size, uint32_t stride, uint32_t pad, ACTIVATION activation){
|
|
m_nIn_Height = h;
|
|
m_nIn_Width = w;
|
|
m_nIn_Channels = c;
|
|
m_nN = n;
|
|
m_nBatch = batch;
|
|
m_nStride = stride;
|
|
m_nSize = size;
|
|
m_nPad = pad;
|
|
|
|
uint32_t out_h = (m_nPad == 0) ? ((m_nIn_Height - m_nSize) / (m_nStride + 1)) : ((m_nIn_Height - 1) / (m_nStride + 1));
|
|
uint32_t out_w = (m_nPad == 0) ? ((m_nIn_Width - m_nSize) / (m_nStride + 1)) : ((m_nIn_Width - 1) / (m_nStride + 1));
|
|
|
|
int locations = out_h*out_w;
|
|
m_nOut_Height = out_h;
|
|
m_nOut_Width = out_w;
|
|
m_nOut_Channels = n;
|
|
m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
|
|
m_nInput = m_nIn_Width * m_nIn_Height * m_nIn_Channels;
|
|
|
|
m_nWeight = c*n*size*size*locations;
|
|
m_pWeight = (float*)calloc(m_nWeight, sizeof(float));
|
|
m_pWeightUpdate = (float*)calloc(m_nWeight, sizeof(float));
|
|
m_nBias = m_nOutput;
|
|
m_pBias = (float*)calloc(m_nBias, sizeof(float));
|
|
m_pBiasUpdate = (float*)calloc(m_nBias, sizeof(float));
|
|
|
|
// float scale = 1./sqrt(size*size*c);
|
|
float scale = (float)sqrt(2.f / (size*size*c));
|
|
for ( uint32_t i = 0; i < c*n*size*size; ++i) m_pWeight[i] = scale*rand_uniform(-1, 1);
|
|
|
|
m_nColImage = out_h*out_w*size*size*c;
|
|
m_pColImage = (float*)calloc(m_nColImage, sizeof(float));
|
|
m_pOutput = (float*)calloc(m_nOutput, sizeof(float));
|
|
m_nDelta = m_nOutput;
|
|
m_pDelta = (float*)calloc(m_nDelta, sizeof(float));
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight);// , m_pWeight);
|
|
m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);//, m_pWeightUpdate);
|
|
m_pGpuBias = m_pPU->makeFloatArray(m_nOutput);//, m_pBias);
|
|
m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nOutput);//, m_pBiasUpdate);
|
|
m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage);//, m_pColImage);
|
|
m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta);//, m_pDelta);
|
|
m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput);//,m_pOutput);
|
|
};
|
|
#endif
|
|
m_eActivation = activation;
|
|
fprintf(stderr, "Local Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h, w, c, n, out_h, out_w, n);
|
|
return true;
|
|
};
|
|
|
|
void localLayer::forward(cortexSTATE state){
|
|
m_pPU->forward_layer(this, state);
|
|
};
|
|
|
|
void localLayer::backward(cortexSTATE state){
|
|
m_pPU->backward_layer(this, state);
|
|
};
|
|
|
|
void localLayer::loadWeights(FILE *fp, bool trans){
|
|
//int locations = m_nOut_Width*m_nOut_Height;
|
|
//int size = m_nSize*m_nSize*m_nIn_Channels*m_nN*locations;
|
|
//fread(m_pBias, sizeof(float), m_nOutput, fp);
|
|
//fread(m_pWeight, sizeof(float), size, fp);
|
|
fread(m_pBias, sizeof(float), m_nBias, fp);
|
|
fread(m_pWeight, sizeof(float), m_nWeight, fp);
|
|
#ifdef GPU
|
|
m_pPU->push_layer(this);
|
|
#endif
|
|
};
|
|
|
|
void localLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
|
|
m_pPU->update_layer(this, batch, learning_rate, momentum, decay);
|
|
};
|
|
|