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.
 
 
 
 
 
 

280 lines
8.7 KiB

#include "detectionLayer.h"
#include "../base/basicCortex.h"
#define LOG( X ) m_pLog->Log( "DETECTL", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "DETECTL", this, _LOG_ERROR, X )
detectionLayer::detectionLayer(bool useGPU) : cortexLayer(DETECTION, useGPU){
};
detectionLayer::~detectionLayer(){
};
bool detectionLayer::create(uint32_t batch, uint32_t inputs, uint32_t n, uint32_t side, uint32_t classes, uint32_t coords, uint32_t rescore){
//l.n = n;
m_nIn_Channels = n;
m_nBatch = batch;
m_nInput = inputs;
m_nClasses = classes;
m_nCoords = coords;
m_nRescore = rescore;
m_nSide = side;
m_nIn_Width = side;
m_nIn_Height = side;
assert(m_nSide*m_nSide*((1 + m_nCoords)*m_nIn_Channels + m_nClasses) == inputs);
m_nOutput = m_nInput;
m_nTruth = m_nSide*m_nSide*(1 + m_nCoords + m_nClasses);
m_pOutput = (float*)calloc(batch*m_nOutput, sizeof(float));
m_pDelta = (float*)calloc(batch*m_nOutput, sizeof(float));
m_pGpuOutput = NULL;// cuda_make_array(l.output, batch*l.outputs);
m_pGpuDelta = NULL;// cuda_make_array(l.delta, batch*l.outputs);
char tBuff[256];
sprintf(tBuff, "Detection Layer");
LOG(tBuff);
srand(0);
return true;
};
/*
void activationLayer::forward(cortexSTATE state){
#ifdef GPU
if (m_bUseGPU){
}
else {
#endif
#ifdef GPU
};
#endif
};
void activationLayer::backward(cortexSTATE state){
#ifdef GPU
if (m_bUseGPU){
}
else {
#endif
#ifdef GPU
};
#endif
};
*/
void detectionLayer::forward(cortexSTATE state){
uint32_t locations = m_nSide*m_nSide;
uint32_t i, j, b;
memcpy(m_pOutput, state.input, m_nOutput*m_nBatch*sizeof(float));
//if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
if (m_bSoftMax){
for (b = 0; b < m_nBatch; ++b){
int index = b*m_nInput;
for (i = 0; i < locations; ++i) {
int offset = i*m_nClasses;
m_pPU->makeSoftMax(m_pOutput + index + offset, m_nClasses, 1, m_pOutput + index + offset);
};
};
};
if (state.train){
float avg_iou = 0;
float avg_cat = 0;
float avg_allcat = 0;
float avg_obj = 0;
float avg_anyobj = 0;
uint32_t count = 0;
m_fCost = 0.f;
uint32_t size = m_nInput * m_nBatch;
memset(m_pDelta, 0, size * sizeof(float));
for (b = 0; b < m_nBatch; ++b){
uint32_t index = b*m_nInput;
for (i = 0; i < locations; ++i) {
uint32_t truth_index = (b*locations + i)*(1 + m_nCoords + m_nClasses);
uint32_t is_obj = (int)state.truth[truth_index];
for (j = 0; j < m_nIn_Channels; ++j) {
uint32_t p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
m_pDelta[p_index] = m_fNoObjectScale*(0 - m_pOutput[p_index]);
m_fCost += (float)(m_fNoObjectScale*pow(m_pOutput[p_index], 2));
avg_anyobj += m_pOutput[p_index];
};
int best_index = -1;
float best_iou = 0.f;
float best_rmse = 20.f;
if (!is_obj){
continue;
};
uint32_t class_index = index + i* m_nClasses;
for (j = 0; j < m_nClasses; ++j) {
m_pDelta[class_index + j] = m_fClassScale * (state.truth[truth_index + 1 + j] - m_pOutput[class_index + j]);
m_fCost += (float)(m_fClassScale * pow(state.truth[truth_index + 1 + j] - m_pOutput[class_index + j], 2));
if (state.truth[truth_index + 1 + j]) avg_cat += m_pOutput[class_index + j];
avg_allcat += m_pOutput[class_index + j];
}
cortexBOXLABEL truth;
truth._float(state.truth + truth_index + 1 + m_nClasses);
truth.x /= m_nSide;
truth.y /= m_nSide;
for (j = 0; j < m_nIn_Channels; ++j){
int box_index = index + locations*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + j) * m_nCoords;
cortexBOXLABEL out;
out._float(m_pOutput + box_index);
out.x /= m_nSide;
out.y /= m_nSide;
if ( m_bSQRT ){
out.w = out.w*out.w;
out.h = out.h*out.h;
};
float iou = out._iou(truth);
//iou = 0;
float rmse = out._rmse(truth);
if (best_iou > 0 || iou > 0){
if (iou > best_iou){
best_iou = iou;
best_index = j;
};
} else{
if (rmse < best_rmse){
best_rmse = rmse;
best_index = j;
};
};
};
if(m_bForced){
if (truth.w*truth.h < .1){
best_index = 1;
} else{
best_index = 0;
};
};
if ( m_bRandom && ( state.net->getSeen() < 64000 ) ){
best_index = rand() % m_nIn_Channels;
};
int box_index = index + locations*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + best_index) * m_nCoords;
int tbox_index = truth_index + 1 + m_nClasses;
cortexBOXLABEL out;
out._float( m_pOutput + box_index );
out.x /= m_nSide;
out.y /= m_nSide;
if ( m_bSQRT ) {
out.w = out.w*out.w;
out.h = out.h*out.h;
};
float iou = out._iou(truth);
//printf("%d,", best_index);
int p_index = index + locations*m_nClasses + i*m_nIn_Channels + best_index;
m_fCost -= (float)(m_fNoObjectScale * pow(m_pOutput[p_index], 2));
m_fCost += (float)(m_fObjectScale * pow(1 - m_pOutput[p_index], 2));
avg_obj += m_pOutput[p_index];
m_pDelta[p_index] = m_fObjectScale * (1.f - m_pOutput[p_index]);
if (m_bRescore){
m_pDelta[p_index] = m_fObjectScale * (iou - m_pOutput[p_index]);
}
m_pDelta[box_index + 0] = m_fCoordScale*(state.truth[tbox_index + 0] - m_pOutput[box_index + 0]);
m_pDelta[box_index + 1] = m_fCoordScale*(state.truth[tbox_index + 1] - m_pOutput[box_index + 1]);
m_pDelta[box_index + 2] = m_fCoordScale*(state.truth[tbox_index + 2] - m_pOutput[box_index + 2]);
m_pDelta[box_index + 3] = m_fCoordScale*(state.truth[tbox_index + 3] - m_pOutput[box_index + 3]);
if ( m_bSQRT ){
m_pDelta[box_index + 2] = (float)(m_fCoordScale*(sqrt(state.truth[tbox_index + 2]) - m_pOutput[box_index + 2]));
m_pDelta[box_index + 3] = (float)(m_fCoordScale*(sqrt(state.truth[tbox_index + 3]) - m_pOutput[box_index + 3]));
};
m_fCost += (float)pow(1 - iou, 2);
avg_iou += iou;
++count;
};
};
if (0){
float *costs = (float*)calloc(m_nBatch*locations*m_nIn_Channels, sizeof(float));
for (b = 0; b < m_nBatch; ++b) {
int index = b*m_nInput;
for (i = 0; i < locations; ++i) {
for (j = 0; j < m_nIn_Channels; ++j) {
int p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
costs[b*locations*m_nIn_Channels + i*m_nIn_Channels + j] = m_pDelta[p_index] * m_pDelta[p_index];
};
};
};
int indexes[100];
top_k(costs, m_nBatch*locations*m_nIn_Channels, 100, indexes);
float cutoff = costs[indexes[99]];
for (b = 0; b < m_nBatch; ++b) {
int index = b*m_nInput;
for (i = 0; i < locations; ++i) {
for (j = 0; j < m_nIn_Channels; ++j) {
int p_index = index + locations*m_nClasses + i*m_nIn_Channels + j;
if (m_pDelta[p_index] * m_pDelta[p_index] < cutoff) m_pDelta[p_index] = 0;
};
};
};
free(costs);
};
//m_fCost = (float)pow( m_pPU->mag_array(m_pDelta, m_nOutput * m_nBatch), 2);
m_fCost = m_pPU->mag2_array(m_pDelta, m_nOutput * m_nBatch);
char tBuff[256];
sprintf(tBuff, "Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d", avg_iou / count, avg_cat / count, avg_allcat / (count*m_nClasses), avg_obj / count, avg_anyobj / (m_nBatch*locations*m_nIn_Channels), count);
LOG(tBuff);
//if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
};
};
void detectionLayer::backward(cortexSTATE state){
m_pPU->calcAxpy(m_nBatch*m_nInput, 1, m_pDelta, 1, state.delta, 1);
};
void detectionLayer::getDetectionBoxes(uint32_t w, uint32_t h, float thresh, float **probs, cortexBOXLABEL *boxes, bool only_objectness){
uint32_t i, j, n;
float *predictions = m_pOutput;
//int per_cell = 5*num+classes;
for (i = 0; i < m_nSide*m_nSide; ++i){
int row = i / m_nSide;
int col = i % m_nSide;
for (n = 0; n < m_nIn_Channels; ++n){
int index = i*m_nIn_Channels + n;
int p_index = m_nSide*m_nSide*m_nClasses + i*m_nIn_Channels + n;
float scale = predictions[p_index];
int box_index = m_nSide*m_nSide*(m_nClasses + m_nIn_Channels) + (i*m_nIn_Channels + n) * 4;
boxes[index].x = (predictions[box_index + 0] + col) / m_nSide * w;
boxes[index].y = (predictions[box_index + 1] + row) / m_nSide * h;
boxes[index].w = (float)pow(predictions[box_index + 2], (m_bSQRT? 2 : 1)) * w;
boxes[index].h = (float)pow(predictions[box_index + 3], (m_bSQRT? 2 : 1)) * h;
for (j = 0; j < m_nClasses; ++j){
int class_index = i*m_nClasses;
float prob = scale*predictions[class_index + j];
probs[index][j] = (prob > thresh) ? prob : 0;
};
if (only_objectness){
probs[index][0] = scale;
};
};
};
};
uint32_t detectionLayer::getBoxesCount(void){
if (m_nIn_Height == m_nIn_Width) m_nSide = m_nIn_Height;
return (m_nIn_Width*m_nIn_Height*m_nN);
};
uint32_t detectionLayer::getClassesCount(void){
return m_nClasses;
};
#undef LOG
#undef LOG_ERROR