|
|
#include "deconvolutionalLayer.h"
#include "convolutionalLayer.h"
//#include "utils.h"
//#include "im2col.h"
//#include "col2im.h"
//#include "blas.h"
//#include "gemm.h"
/*
#include <stdio.h>
#include <time.h>
#include "../cuda/cuda.h"
#include "../cpu/activations.h"
#include "../cpu/im2col.h"
#include "../cpu/col2im.h"
#include "../cpu/blas.h"
#include "../cpu/gemm.h"
*/
#define LOG( X ) m_pLog->Log( "DECONVL", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "DECONVL", this, _LOG_ERROR, X )
deconvolutionalLayer::deconvolutionalLayer(bool useGPU) :cortexLayer(DECONVOLUTIONAL, useGPU){ m_nColImage = 0; m_pColImage = NULL; m_pGpuColImage = NULL;};
deconvolutionalLayer::~deconvolutionalLayer(){ if (m_pColImage)free(m_pColImage); if (m_pPU != NULL){ if (m_pGpuColImage != NULL)m_pPU->freeMem(m_pGpuColImage); };};
/*
int deconvolutional_out_height(deconvolutional_layer l){ int h = l.stride*(l.h - 1) + l.size; return h;}
int deconvolutional_out_width(deconvolutional_layer l){ int w = l.stride*(l.w - 1) + l.size; return w;}
int deconvolutional_out_size(deconvolutional_layer l){ return deconvolutional_out_height(l) * deconvolutional_out_width(l);}
image get_deconvolutional_image(deconvolutional_layer l){ int h,w,c; h = deconvolutional_out_height(l); w = deconvolutional_out_width(l); c = l.n; return float_to_image(w,h,c,l.output);}
image get_deconvolutional_delta(deconvolutional_layer l){ int h,w,c; h = deconvolutional_out_height(l); w = deconvolutional_out_width(l); c = l.n; return float_to_image(w,h,c,l.delta);}*/
bool deconvolutionalLayer::create(uint32_t batch, uint32_t h, uint32_t w, uint32_t c, uint32_t n, uint32_t size, uint32_t stride, ACTIVATION activation){ uint32_t i;
m_nIn_Height = h; m_nIn_Width = w; m_nIn_Channels = c; m_nOut_Channels = n; m_nBatch = batch; m_nStride = stride; m_nSize = size;
m_nWeight = c*n*size*size; m_pWeight = (float*)calloc(m_nWeight, sizeof(float)); m_pWeightUpdate = (float*)calloc(m_nWeight, sizeof(float));
m_nBias = n; m_pBias = (float*)calloc(m_nBias, sizeof(float)); m_pBiasUpdate = (float*)calloc(m_nBias, sizeof(float));
float scale = (float)(1.f/sqrt((float)(size*size*c))); for (i = 0; i < m_nWeight; ++i) m_pWeight[i] = scale*rand_normal(); for (i = 0; i < m_nBias; ++i) m_pBias[i] = scale;
//int out_h = deconvolutional_out_height(l);
//int out_w = deconvolutional_out_width(l);
m_nOut_Height = m_nStride * (m_nIn_Height - 1) + m_nSize; m_nOut_Width = m_nStride * (m_nIn_Width - 1) + m_nSize;
char tBuff[256]; sprintf(tBuff, "Deconvolutional Layer: %d x %d x %d image, %d filters -> %d x %d x %d image", h, w, c, n, m_nOut_Height, m_nOut_Width, n); LOG(tBuff);
//l.out_h = out_h;
//l.out_w = out_w;
//l.out_c = n;
//l.outputs = l.out_w * l.out_h * l.out_c;
//l.inputs = l.w * l.h * l.c;
m_nDelta = m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels; m_nDeltaVol = m_nOutputVol = m_nOutput*m_nBatch; m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels;
m_nColImage = m_nOut_Height*m_nOut_Width*m_nSize*m_nSize*m_nOut_Channels;
m_pOutput = (float*)calloc(m_nOutputVol, sizeof(float)); m_pDelta = (float*)calloc(m_nDeltaVol, sizeof(float)); m_pColImage = (float*)calloc(m_nColImage, sizeof(float));#ifdef GPU
m_pGpuWeight = m_pPU->makeFloatArray(m_nWeight); m_pGpuWeightUpdate = m_pPU->makeFloatArray(m_nWeight);
m_pGpuBias = m_pPU->makeFloatArray(m_nBias); m_pGpuBiasUpdate = m_pPU->makeFloatArray(m_nBias); m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol); m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol); /* Layer Specific */ m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage, m_pColImage); #endif
m_eActivation = activation; return true;};
bool deconvolutionalLayer::resize(uint32_t w, uint32_t h){ m_nIn_Height = h; m_nIn_Width = w; m_nOut_Height = m_nStride * (m_nIn_Height - 1) + m_nSize; m_nOut_Width = m_nStride * (m_nIn_Width - 1) + m_nSize; m_nInput = m_nIn_Height * m_nIn_Width * m_nIn_Channels; m_nOutput = m_nOut_Height * m_nOut_Width * m_nOut_Channels; m_nColImage = m_nOut_Height * m_nOut_Width * m_nSize * m_nSize * m_nOut_Channels; m_nDelta = m_nOut_Height * m_nOut_Width * m_nOut_Channels;
m_pColImage = (float*)realloc(m_pColImage, m_nColImage * sizeof(float)); m_pOutput = (float*)realloc(m_pOutput, m_nBatch * m_nOutput * sizeof(float)); m_pDelta = (float*)realloc(m_pDelta, m_nBatch * m_nDelta * sizeof(float)); #ifdef GPU
m_pPU->freeMem(m_pGpuColImage); m_pPU->freeMem(m_pGpuOutput); m_pPU->freeMem(m_pGpuDelta);
m_pGpuColImage = m_pPU->makeFloatArray(m_nColImage, m_pColImage); m_pGpuOutput = m_pPU->makeFloatArray(m_nOutput,m_pOutput); m_pGpuDelta = m_pPU->makeFloatArray(m_nDelta, m_pDelta); #endif
return true;};
void deconvolutionalLayer::forward(cortexSTATE state){ uint32_t i; //int out_h = deconvolutional_out_height(l);
//int out_w = deconvolutional_out_width(l);
uint32_t size = m_nOut_Height * m_nOut_Width;
uint32_t m = m_nSize*m_nSize*m_nOut_Channels; //this->n;
uint32_t n = m_nIn_Height * m_nIn_Width; uint32_t k = m_nIn_Channels;
m_pPU->makeFill(m_nOutput*m_nBatch, 0, m_pOutput, 1);
for(i = 0; i < m_nBatch; ++i){ float *a = m_pWeight; float *b = &state.input[ i * m_nIn_Channels * m_nIn_Height * m_nIn_Width ]; float *c = m_pColImage; m_pPU->gemm( 1, 0, m, n, k, 1, a, m, b, n, 0, c, n); m_pPU->col2im(c, m_nOut_Channels, m_nOut_Height, m_nOut_Width, m_nSize, m_nStride, 0, &m_pOutput[ i * m_nOut_Channels * m_nSize]); }; m_pPU->addBias(m_pOutput, m_pBias, m_nBatch, m_nOut_Channels, m_nSize); m_pPU->makeActivatedArray( m_pOutput, m_nBatch*m_nOut_Channels*m_nSize, m_eActivation);};
void deconvolutionalLayer::backward( cortexSTATE state){ float alpha = 1.f/m_nBatch; //int out_h = deconvolutional_out_height(l);
//int out_w = deconvolutional_out_width(l);
uint32_t size = m_nOut_Height * m_nOut_Width; uint32_t i;
m_pPU->makeGradientArray(m_pOutput, size*m_nOut_Channels*m_nBatch, m_eActivation, m_pDelta); m_pPU->makeBackwardBias(m_pBiasUpdate, m_pDelta, m_nBatch, m_nOut_Channels, size);
for(i = 0; i < m_nBatch; ++i){ uint32_t m = m_nIn_Channels; uint32_t n = m_nSize*m_nSize*m_nOut_Channels; uint32_t k = m_nIn_Height*m_nIn_Width;
float *a = &state.input[i*m*n]; float *b = m_pColImage; float *c = m_pWeightUpdate;
m_pPU->im2col(&m_pDelta[i*m_nOut_Channels*size], m_nOut_Channels, m_nOut_Height, m_nOut_Width, m_nSize, m_nStride, 0, b); m_pPU->gemm( 0, 1, m, n, k, alpha, a, k, b, k, 1, c, n );
if(state.delta){ uint32_t m = m_nIn_Channels; uint32_t n = m_nIn_Height*m_nIn_Width; uint32_t k = m_nSize*m_nSize*m_nOut_Channels; float *a = m_pWeight; float *b = m_pColImage; float *c = &state.delta[i*n*m]; m_pPU->gemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n); }; };};
void deconvolutionalLayer::update(int batch, float learning_rate, float momentum, float decay){// int size = m_nSize*m_nSize*m_nIn_Channels*m_nOut_Channels;
m_pPU->calcAxpy( m_nBias, learning_rate, m_pBiasUpdate, 1, m_pBias, 1 ); m_pPU->calcScal( m_nBias, momentum, m_pBiasUpdate, 1 ); m_pPU->calcAxpy(m_nWeight, -decay, m_pWeight, 1, m_pWeightUpdate, 1); m_pPU->calcAxpy(m_nWeight, learning_rate, m_pWeightUpdate, 1, m_pWeight, 1); m_pPU->calcScal(m_nWeight, momentum, m_pWeightUpdate, 1);};
#undef LOG
#undef LOG_ERROR
|