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.
172 lines
4.2 KiB
172 lines
4.2 KiB
#include "avgpoolLayer.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "AVGpoolL", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "AVGpoolL", this, _LOG_ERROR, X )
|
|
|
|
avgpoolLayer::avgpoolLayer(bool useGPU) :cortexLayer(AVGPOOL, useGPU){
|
|
|
|
};
|
|
|
|
avgpoolLayer::~avgpoolLayer(){
|
|
|
|
};
|
|
|
|
bool avgpoolLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t c){
|
|
m_nBatch = batch;
|
|
|
|
m_nIn_Width = w;
|
|
m_nIn_Height = h;
|
|
m_nIn_Channels = c;
|
|
|
|
m_nOut_Width = 1;
|
|
m_nOut_Height = 1;
|
|
m_nOut_Channels = c;
|
|
|
|
m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
|
|
m_nOutput = m_nOut_Channels;
|
|
|
|
m_nOutputVol = m_nOutput*m_nBatch;
|
|
m_pOutput = (float*)calloc(m_nOutputVol, sizeof(float));
|
|
if (m_pOutput == NULL) return false;
|
|
m_nDelta = m_nOutput;
|
|
m_nDeltaVol = m_nDelta*m_nBatch;
|
|
m_pDelta = (float*)calloc(m_nDeltaVol, sizeof(float));
|
|
if (m_pDelta == NULL){
|
|
free(m_pOutput);
|
|
m_pOutput = NULL;
|
|
return false;
|
|
};
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol);
|
|
if (m_pGpuOutput = NULL){
|
|
free(m_pOutput); m_pOutput = NULL;
|
|
free(m_pDelta); m_pDelta = NULL;
|
|
return false;
|
|
};
|
|
m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol);
|
|
if (m_pGpuDelta = NULL){
|
|
free(m_pOutput); m_pOutput = NULL;
|
|
free(m_pDelta); m_pDelta = NULL;
|
|
m_pPU->freeMem(m_pGpuOutput); m_pGpuOutput = NULL;
|
|
return false;
|
|
};
|
|
};
|
|
#endif
|
|
char tBuff[256];
|
|
sprintf( tBuff, "avg %4dx%4dx%4d -> %4d\n", w, h, c, c);
|
|
LOG( tBuff );
|
|
return true;
|
|
};
|
|
|
|
void avgpoolLayer::forward(cortexSTATE state){
|
|
uint32_t Area = m_nIn_Width * m_nIn_Height;
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
for (uint32_t b = 0; b < m_nBatch; ++b){
|
|
for (uint32_t k = 0; k < m_nIn_Channels; ++k){
|
|
uint32_t out_index = k + b*m_nIn_Channels;
|
|
m_pGpuOutput[out_index] = 0;
|
|
for (uint32_t i = 0; i < Area; ++i){
|
|
uint32_t in_index = i + Area*out_index;
|
|
m_pGpuOutput[out_index] += state.input[in_index];
|
|
};
|
|
m_pGpuOutput[out_index] /= Area;
|
|
};
|
|
};
|
|
} else {
|
|
#endif
|
|
for (uint32_t b = 0; b < m_nBatch; ++b){
|
|
for (uint32_t k = 0; k < m_nIn_Channels; ++k){
|
|
uint32_t out_index = k + b*m_nIn_Channels;
|
|
m_pOutput[out_index] = 0;
|
|
for (uint32_t i = 0; i < Area; ++i){
|
|
uint32_t in_index = i + Area*out_index;
|
|
m_pOutput[out_index] += state.input[in_index];
|
|
};
|
|
m_pOutput[out_index] /= Area;
|
|
};
|
|
};
|
|
#ifdef GPU
|
|
};
|
|
#endif
|
|
};
|
|
|
|
void avgpoolLayer::backward(cortexSTATE state){
|
|
uint32_t Area = m_nIn_Width * m_nIn_Height;
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
for (uint32_t b = 0; b < m_nBatch; ++b){
|
|
for (uint32_t k = 0; k < m_nIn_Channels; ++k){
|
|
uint32_t out_index = k + b*m_nIn_Channels;
|
|
for (uint32_t i = 0; i < Area; ++i){
|
|
uint32_t in_index = i + Area*out_index;
|
|
state.delta[in_index] += m_pGpuDelta[out_index] / Area;
|
|
};
|
|
};
|
|
};
|
|
} else {
|
|
#endif
|
|
for (uint32_t b = 0; b < m_nBatch; ++b){
|
|
for (uint32_t k = 0; k < m_nIn_Channels; ++k){
|
|
uint32_t out_index = k + b*m_nIn_Channels;
|
|
for (uint32_t i = 0; i < Area; ++i){
|
|
uint32_t in_index = i + Area*out_index;
|
|
state.delta[in_index] += m_pDelta[out_index] / Area;
|
|
};
|
|
};
|
|
};
|
|
#ifdef GPU
|
|
};
|
|
#endif
|
|
};
|
|
|
|
// Pushes HOST Memory to GPU memory
|
|
void avgpoolLayer::push(void){
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
if (m_pOutput != NULL && m_pDelta != NULL){
|
|
if (m_pGpuOutput != NULL){
|
|
m_pPU->pushFloatArray(m_pGpuOutput, m_pOutput, m_nOutputVol);
|
|
} else {
|
|
m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol, m_pOutput);
|
|
};
|
|
if (m_pGpuDelta != NULL){
|
|
m_pPU->pushFloatArray(m_pGpuDelta, m_pDelta, m_nDeltaVol);
|
|
} else{
|
|
m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol, m_pDelta);
|
|
};
|
|
};
|
|
};
|
|
#endif
|
|
};
|
|
|
|
// Pulls GPU memory to a HOST memory
|
|
void avgpoolLayer::pull(void){
|
|
#ifdef GPU
|
|
if (m_bUseGPU){
|
|
if (m_pOutput != NULL && m_pDelta != NULL){
|
|
if (m_pGpuOutput != NULL){
|
|
m_pPU->pullFloatArray(m_pGpuOutput, m_pOutput, m_nOutputVol);
|
|
};
|
|
if (m_pGpuDelta != NULL){
|
|
m_pPU->pullFloatArray(m_pGpuDelta, m_pDelta, m_nDeltaVol);
|
|
};
|
|
};
|
|
};
|
|
#endif
|
|
};
|
|
|
|
bool avgpoolLayer::resize(uint32_t w, uint32_t h){
|
|
m_nIn_Width = w;
|
|
m_nIn_Height = h;
|
|
m_nInput = h*w*m_nIn_Channels;
|
|
return true;
|
|
};
|
|
|
|
cortexImage *avgpoolLayer::getImage(void){
|
|
return NULL;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|