#include "regionLayer.h" #include "../base/basicTree.h" #include "../base/basicBox.h" #include "../base/basicActivations.h" #include "../base/basicData.h" #include "../base/basicCortex.h" #define LOG( X ) m_pLog->Log( "REGION", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "REGION", this, _LOG_ERROR, X ) #define DOABS 1 regionLayer::regionLayer(bool useGPU) : cortexLayer(REGION,useGPU){ m_pSoftmaxTree = NULL; }; regionLayer::~regionLayer(){ if (m_pSoftmaxTree != NULL){ //ToDo: Cleanup Tree }; }; bool regionLayer::create(uint32_t batch, uint32_t w, uint32_t h, uint32_t n, uint32_t classes, uint32_t coords){ m_nN = n; m_nBatch = batch; m_nIn_Height = h; m_nIn_Width = w; m_nClasses = classes; m_nCoords = coords; m_nBias = m_nN * m_nN; m_pBias = (float *)calloc(m_nBias, sizeof(float)); if (m_pBias == NULL) return false; m_pBiasUpdate = (float *)calloc(m_nBias, sizeof(float)); if (m_pBiasUpdate == NULL) return false; m_nInput = h*w*n*(classes + coords + 1); m_nDelta = m_nOutput = m_nInput; m_nDeltaVol = m_nOutputVol = m_nOutput * m_nBatch; m_pOutput = (float *)calloc(m_nOutputVol, sizeof(float)); if (m_pOutput == NULL)return false; m_nTruth = 30 * (5); m_pDelta = (float *)calloc(m_nDeltaVol, sizeof(float)); if (m_pDelta == NULL)return false; for (uint32_t i = 0; i < m_nBias; ++i){ m_pBias[i] = .5; }; #ifdef GPU if (m_bUseGPU){ m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol); m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol); }; #endif char tBuff[256]; sprintf(tBuff, "\t%5d\t%2dx%2d\t%2d\t%4dx%4dx%4d -> %4dx%4dx%4d", n, m_nSize, m_nSize, m_nStride, w, h, m_nIn_Channels, m_nOut_Width, m_nOut_Height, m_nOut_Channels); LOG(tBuff); srand(0); return true; }; bool regionLayer::resize(uint32_t w, uint32_t h){ m_nIn_Width = w; m_nIn_Height = h; m_nDelta = m_nOutput = m_nInput = h*w*m_nN*(m_nClasses + m_nCoords + 1); m_nDeltaVol = m_nOutputVol = m_nOutput *m_nBatch; m_pOutput = (float *)realloc(m_pOutput, m_nOutputVol*sizeof(float)); if (m_pOutput == NULL) return false; m_nDelta = m_nOutput; m_pDelta = (float *)realloc(m_pDelta, m_nDeltaVol*sizeof(float)); if (m_pDelta == NULL) return false; #ifdef GPU if (m_bUseGPU){ m_pPU->freeMem(m_pGpuDelta); m_pPU->freeMem(m_pGpuOutput); m_pGpuDelta = m_pPU->makeFloatArray(m_nDeltaVol, m_pDelta);// cuda_make_array(l->delta, l->batch*l->outputs); m_pGpuOutput = m_pPU->makeFloatArray(m_nOutputVol, m_pOutput); }; #endif return true; }; cortexBOXLABEL get_region_box(float *x, float *biases, uint32_t n, uint32_t index, uint32_t i, uint32_t j, uint32_t w, uint32_t h){ cortexBOXLABEL b; b.x = (i + logistic_activate(x[index + 0])) / w; b.y = (j + logistic_activate(x[index + 1])) / h; b.w = (float)exp(x[index + 2]) * biases[2 * n]; b.h = (float)exp(x[index + 3]) * biases[2 * n + 1]; if (DOABS){ b.w = (float)exp(x[index + 2]) * biases[2 * n] / w; b.h = (float)exp(x[index + 3]) * biases[2 * n + 1] / h; }; return b; } float delta_region_box(cortexBOXLABEL truth, float *x, float *biases, uint32_t n, uint32_t index, uint32_t i, uint32_t j, uint32_t w, uint32_t h, float *delta, float scale){ cortexBOXLABEL pred = get_region_box(x, biases, n, index, i, j, w, h); float iou = pred._iou(truth); float tx = (truth.x*w - i); float ty = (truth.y*h - j); float tw = (float)log(truth.w / biases[2 * n]); float th = (float)log(truth.h / biases[2 * n + 1]); if (DOABS){ tw = (float)log(truth.w*w / biases[2 * n]); th = (float)log(truth.h*h / biases[2 * n + 1]); }; delta[index + 0] = scale * (tx - logistic_activate(x[index + 0])) * logistic_gradient(logistic_activate(x[index + 0])); delta[index + 1] = scale * (ty - logistic_activate(x[index + 1])) * logistic_gradient(logistic_activate(x[index + 1])); delta[index + 2] = scale * (tw - x[index + 2]); delta[index + 3] = scale * (th - x[index + 3]); return iou; }; void delta_region_class(float *output, float *delta, uint32_t index, uint32_t cls, uint32_t classes, cortexTREE *hier, float scale, float *avg_cat){ uint32_t i, n; if (hier){ float pred = 1; while (cls >= 0){ pred *= output[index + cls]; int g = hier->group[cls]; int offset = hier->group_offset[g]; for (i = 0; i < hier->group_size[g]; ++i){ delta[index + offset + i] = scale * (0 - output[index + offset + i]); }; delta[index + cls] = scale * (1 - output[index + cls]); cls = hier->parent[cls]; }; *avg_cat += pred; } else { for (n = 0; n < classes; ++n){ delta[index + n] = scale * (((n == cls) ? 1 : 0) - output[index + n]); if (n == cls) *avg_cat += output[index + n]; }; }; }; float logit(float x){ return (float)log(x / (1. - x)); }; float tisnan(float x){ return (float)(x != x); }; void softmax_tree(float *input, int batch, int inputs, float temp, cortexTREE *hierarchy, float *output); void regionLayer::forward(cortexSTATE state){ uint32_t i, j, b, t, n; uint32_t size = m_nCoords + m_nClasses + 1; memcpy( m_pOutput, state.input, m_nOutput*sizeof(float) ); #ifndef GPU flatten(l.output, l.w*l.h, size*l.n, l.batch, 1); #endif for (b = 0; b < m_nBatch; ++b){ for (i = 0; i < m_nIn_Height*m_nIn_Width*m_nN; ++i){ int index = size * i + b * m_nInput; m_pOutput[index + 4] = logistic_activate(m_pOutput[index + 4]); }; }; #ifndef GPU if (l.softmax_tree){ for (b = 0; b < l.batch; ++b){ for (i = 0; i < l.h*l.w*l.n; ++i){ int index = size*i + b*l.outputs; softmax_tree(l.output + index + 5, 1, 0, 1, l.softmax_tree, l.output + index + 5); } } } else if (l.softmax){ for (b = 0; b < l.batch; ++b){ for (i = 0; i < l.h*l.w*l.n; ++i){ int index = size*i + b*l.outputs; softmax(l.output + index + 5, l.classes, 1, l.output + index + 5); } } } #endif if (!state.train) return; memset( m_pDelta, 0, m_nDelta * sizeof(float)); float avg_iou = 0; float recall = 0; float avg_cat = 0; float avg_obj = 0; float avg_anyobj = 0; uint32_t count = 0; uint32_t class_count = 0; m_fCost = 0.f; for (b = 0; b < m_nBatch; ++b) { if ( m_pSoftmaxTree != NULL ){ bool onlyclass = false; for (t = 0; t < 30; ++t){ cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths); truth._float( state.truth + t * 5 + b * m_nTruth ); if (truth.x == 0) break; int cls = (int)state.truth[ t * 5 + b * m_nTruth + 4]; float maxp = 0; int maxi = 0; if (truth.x > 100000 && truth.y > 100000){ for (n = 0; n < m_nN*m_nIn_Width*m_nIn_Height; ++n){ int index = size*n + b*m_nInput + 5; float scale = m_pOutput[index - 1]; float p = scale*get_hierarchy_probability(m_pOutput + index, m_pSoftmaxTree, cls); if (p > maxp){ maxp = p; maxi = n; }; }; int index = size*maxi + b*m_nInput + 5; delta_region_class(m_pOutput, m_pDelta, index, cls, m_nClasses, m_pSoftmaxTree, m_fClassScale, &avg_cat); ++class_count; onlyclass = true; break; }; }; if (onlyclass) continue; }; for (j = 0; j < m_nIn_Height; ++j) { for (i = 0; i < m_nIn_Width; ++i) { for (n = 0; n < m_nN; ++n) { int index = size*(j*m_nIn_Width*m_nN + i*m_nN + n) + b*m_nOutput; cortexBOXLABEL pred = get_region_box(m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height); float best_iou = 0; int best_class = -1; for (t = 0; t < 30; ++t){ cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths); truth._float(state.truth + t * 5 + b * m_nTruth); if(truth.x == 0) break; float iou = pred._iou(truth); if (iou > best_iou) { best_class = (int)state.truth[t * 5 + b*m_nTruth + 4]; best_iou = iou; }; }; avg_anyobj += m_pOutput[index + 4]; m_pDelta[index + 4] = m_fNoObjectScale * ((0 - m_pOutput[index + 4]) * logistic_gradient(m_pOutput[index + 4])); if( m_sClassFix == -1 ){ m_pDelta[index + 4] = m_fNoObjectScale * ((best_iou - m_pOutput[index + 4]) * logistic_gradient(m_pOutput[index + 4])); } else { if (best_iou > m_fThreshold) { m_pDelta[index + 4] = 0; if( m_sClassFix > 0 ){ delta_region_class( m_pOutput, m_pDelta, index + 5, best_class, m_nClasses, m_pSoftmaxTree, m_fClassScale*(m_sClassFix == 2 ? m_pOutput[index + 4] : 1), &avg_cat); ++class_count; }; }; }; if( state.net->getSeen() < 12800 ){ cortexBOXLABEL truth = { 0 }; truth.x = (i + .5f) / m_nIn_Width; truth.y = (j + .5f) / m_nIn_Height; truth.w = m_pBias[2 * n]; truth.h = m_pBias[2 * n + 1]; if (DOABS){ truth.w = m_pBias[2 * n] / m_nIn_Width; truth.h = m_pBias[2 * n + 1] / m_nIn_Height; }; delta_region_box(truth, m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height, m_pDelta, .01f); }; }; }; }; for (t = 0; t < 30; ++t){ cortexBOXLABEL truth;// = float_to_box(state.truth + t * 5 + b*l.truths); truth._float( state.truth + t * 5 + b * m_nTruth ); if (truth.x == 0) break; float best_iou = 0; uint32_t best_index = 0; uint32_t best_n = 0; i = (int)(truth.x * m_nIn_Width); j = (int)(truth.y * m_nIn_Height); //printf("%d %f %d %f\n", i, truth.x*l.w, j, truth.y*l.h); cortexBOXLABEL truth_shift = truth; truth_shift.x = 0; truth_shift.y = 0; //printf("index %d %d\n",i, j); for (n = 0; n < m_nN; ++n){ int index = size*(j*m_nIn_Width*m_nN + i*m_nN + n) + b * m_nInput; cortexBOXLABEL pred = get_region_box(m_pOutput, m_pBias, n, index, i, j, m_nIn_Width, m_nIn_Height); if (m_bBiasMatch){ pred.w = m_pBias[2 * n]; pred.h = m_pBias[2 * n + 1]; if (DOABS){ pred.w = m_pBias[2 * n] / m_nIn_Width; pred.h = m_pBias[2 * n + 1] / m_nIn_Height; }; }; //printf("pred: (%f, %f) %f x %f\n", pred.x, pred.y, pred.w, pred.h); pred.x = 0; pred.y = 0; float iou = pred._iou(truth_shift); if (iou > best_iou){ best_index = index; best_iou = iou; best_n = n; }; }; //printf("%d %f (%f, %f) %f x %f\n", best_n, best_iou, truth.x, truth.y, truth.w, truth.h); float iou = delta_region_box(truth, m_pOutput, m_pBias, best_n, best_index, i, j, m_nIn_Width, m_nIn_Height, m_pDelta, m_fCoordScale); if (iou > .5) recall += 1; avg_iou += iou; //l.delta[best_index + 4] = iou - l.output[best_index + 4]; avg_obj += m_pOutput[best_index + 4]; m_pDelta[best_index + 4] = m_fObjectScale * (1 - m_pOutput[best_index + 4]) * logistic_gradient(m_pOutput[best_index + 4]); if (m_bRescore) { m_pDelta[best_index + 4] = m_fObjectScale * (iou - m_pOutput[best_index + 4]) * logistic_gradient(m_pOutput[best_index + 4]); }; int cls = (int)state.truth[t * 5 + b * m_nTruth + 4]; if( m_pMap != NULL ) cls = m_pMap[cls]; delta_region_class(m_pOutput, m_pDelta, best_index + 5, cls, m_nClasses, m_pSoftmaxTree, m_fClassScale, &avg_cat); ++count; ++class_count; }; }; //printf("\n"); #ifndef GPU flatten(m_pDelta, m_nIn_Width*m_nIn_Height, size*m_nN, m_nBatch, 0); #endif m_fCost = (float)pow( mag_array(m_pDelta, m_nDelta), 2 ); printf("Region Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, Avg Recall: %f, count: %d\n", avg_iou / count, avg_cat / class_count, avg_obj / count, avg_anyobj / (m_nIn_Width*m_nIn_Height*m_nN*m_nBatch), recall / count, count); }; void regionLayer::backward(cortexSTATE state){ m_pPU->calcAxpy( m_nDelta, 1, m_pDelta, 1, state.delta, 1 ); }; void regionLayer::get_region_boxes(uint32_t w, uint32_t h, float thresh, float **probs, cortexBOXLABEL *boxes, bool only_objectness, uint32_t *map){ uint32_t i, j, n; float *predictions = m_pOutput; for (i = 0; i < m_nIn_Width*m_nIn_Height; ++i){ uint32_t row = i / m_nIn_Width; uint32_t col = i % m_nIn_Width; for (n = 0; n < m_nN; ++n){ int index = i*m_nN + n; int p_index = index * (m_nClasses + 5) + 4; float scale = predictions[p_index]; if (m_sClassFix == -1 && scale < .5) scale = 0; int box_index = index * (m_nClasses + 5); boxes[index] = get_region_box(predictions, m_pBias, n, box_index, col, row, m_nIn_Width, m_nIn_Height); boxes[index].x *= w; boxes[index].y *= h; boxes[index].w *= w; boxes[index].h *= h; int class_index = index * (m_nClasses + 5) + 5; if( m_pSoftmaxTree != NULL ){ hierarchy_predictions(predictions + class_index, m_nClasses, m_pSoftmaxTree, 0); int found = 0; if (map){ for (j = 0; j < 200; ++j){ float prob = scale*predictions[class_index + map[j]]; probs[index][j] = (prob > thresh) ? prob : 0; }; } else { for (j = m_nClasses - 1; j >= 0; --j){ if (!found && predictions[class_index + j] > .5){ found = 1; } else { predictions[class_index + j] = 0; }; float prob = predictions[class_index + j]; probs[index][j] = (scale > thresh) ? prob : 0; }; }; } else { for (j = 0; j < m_nClasses; ++j){ float prob = scale*predictions[class_index + j]; probs[index][j] = (prob > thresh) ? prob : 0; }; }; if (only_objectness){ probs[index][0] = scale; }; }; }; }; uint32_t regionLayer::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 regionLayer::getClassesCount(void){ return m_nClasses; }; #undef LOG #undef LOG_ERROR