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.
 
 
 
 
 
 

91 lines
2.8 KiB

#include "crnnLayer.h"
#include "convolutionalLayer.h"
#include "../cuda/cuda.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
crnnLayer::crnnLayer(bool useGPU) : cortexLayer(CRNN, useGPU){
m_pState = NULL;
m_nState = 0;
m_pInputLayer = NULL;
m_pSelfLayer = NULL;
m_pOutputLayer = NULL;
};
crnnLayer::~crnnLayer(){
if (m_pInputLayer != NULL)delete(m_pInputLayer);
if (m_pSelfLayer != NULL)delete(m_pSelfLayer);
if (m_pOutputLayer != NULL)delete(m_pOutputLayer);
if (m_pState != NULL) free(m_pState);
m_nState = 0;
};
bool crnnLayer::create(uint32_t batch, uint32_t h, uint32_t w, uint32_t c, uint32_t hidden_filters, uint32_t output_filters, uint32_t steps, ACTIVATION activation, bool batch_normalize){
fprintf(stderr, "CRNN Layer: %d x %d x %d image, %d filters\n", h,w,c,output_filters);
batch = batch / steps;
m_nBatch = batch;
m_nSteps = steps;
m_nOut_Height = m_nIn_Height = h;
m_nOut_Width = m_nIn_Width = w;
m_nIn_Channels = c;
m_nOut_Channels = output_filters;
m_nInput = h * w * c;
m_nHidden = h * w * hidden_filters;
m_nOutput = m_nOut_Height * m_nOut_Height * m_nOut_Channels;
m_nOutputVol = m_nBatch * m_nOutput;
m_nState = m_nHidden*m_nBatch*(m_nSteps + 1);
m_pState = (float*)calloc(m_nState, sizeof(float));
m_pInputLayer = new convolutionalLayer(m_bUseGPU);
if (m_pInputLayer == NULL) return false;
m_pInputLayer->create(batch*steps, h, w, c, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
m_pInputLayer->m_nBatch = batch;
m_pSelfLayer = new convolutionalLayer(m_bUseGPU);
if (m_pSelfLayer == NULL){
delete(m_pInputLayer);
return false;
};
m_pSelfLayer->create(batch*steps, h, w, hidden_filters, hidden_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
m_pSelfLayer->m_nBatch = batch;
m_pOutputLayer = new convolutionalLayer(m_bUseGPU);
if (m_pOutputLayer == NULL){
delete(m_pInputLayer);
delete(m_pSelfLayer);
return false;
};
m_pOutputLayer->create(batch*steps, h, w, hidden_filters, output_filters, 3, 1, 1, activation, batch_normalize, 0, 0, 0);
m_pOutputLayer->m_nBatch = batch;
m_pOutput = m_pOutputLayer->m_pOutput;
m_pDelta = m_pOutputLayer->m_pDelta;
#ifdef GPU
m_pGpuState = m_pPU->makeFloatArray(m_nState);
m_pGpuOutput = m_pOutputLayer->m_pGpuOutput;
m_pGpuDelta = m_pOutputLayer->m_pDelta;
#endif
return true;
};
void crnnLayer::forward(cortexSTATE state){
m_pPU->forward_layer(this, state);
};
void crnnLayer::backward(cortexSTATE state){
m_pPU->backward_layer(this, state);
};
void crnnLayer::loadWeights(FILE *fp, bool trans){
m_pInputLayer->loadWeights(fp, trans);
m_pSelfLayer->loadWeights(fp, trans);
m_pOutputLayer->loadWeights(fp, trans);
};
void crnnLayer::update(uint32_t batch, float learning_rate, float momentum, float decay){
m_pPU->update_layer(this, batch, learning_rate, momentum, decay);
};