[Phission-cvs] phission/examples/cpp/MLImageNeuralNetwork/src ml_class_stats.c,NONE,1.1 phANNSystem.
Brought to you by:
pthoren
From: Philip T. <pt...@us...> - 2005-12-20 23:35:47
|
Update of /cvsroot/phission/phission/examples/cpp/MLImageNeuralNetwork/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3911/src Modified Files: phANNSystem.cpp Added Files: ml_class_stats.c Log Message: forgot to add the stats.c files fixed the bug in phANNSystem, was overruning a buffer; I can classify through the ANN now Index: phANNSystem.cpp =================================================================== RCS file: /cvsroot/phission/phission/examples/cpp/MLImageNeuralNetwork/src/phANNSystem.cpp,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** phANNSystem.cpp 19 Dec 2005 17:56:10 -0000 1.14 --- phANNSystem.cpp 20 Dec 2005 23:35:38 -0000 1.15 *************** *** 1514,1519 **** int32_t b = 0; int32_t bright_level = 0; ! const int32_t nbright_levels = 8; ! const int32_t bright_offset = -3; /* start darker */ const int32_t initial_bright = (nbright_levels / 2) + bright_offset; --- 1514,1519 ---- int32_t b = 0; int32_t bright_level = 0; ! const int32_t nbright_levels = 5; ! const int32_t bright_offset = -4; /* start darker */ const int32_t initial_bright = (nbright_levels / 2) + bright_offset; *************** *** 2084,2091 **** /* print the string to a buffer */ ! snprintf(buf,255,"%s:%0.1f_",tags[i],output_nodes[i]); len += strlen(buf); ! if (*string_size < len) { /* +1 for the NULL BYTE */ --- 2084,2091 ---- /* print the string to a buffer */ ! snprintf(buf,255,"%s_",tags[i]); len += strlen(buf); ! if (*string_size < (len+pos+1)) { /* +1 for the NULL BYTE */ *************** *** 2097,2101 **** sprintf(&((*string)[pos]),"%s",buf); ! pos += strlen((*string)); } } --- 2097,2101 ---- sprintf(&((*string)[pos]),"%s",buf); ! pos = strlen((*string)); } } *************** *** 2120,2123 **** --- 2120,2125 ---- phFUNCTION("phANNSystem::classify") + int retrc = phSUCCESS; + /* input and output are allocated and populated by this program; * actual_output is returned from fann_run and compared against the *************** *** 2448,2455 **** /* Crop the blob out from the input image */ rc = crop_image.crop(*input_image, ! blobinfo.x1, ! blobinfo.y1, ! blobinfo.x2, ! blobinfo.y2 ); phPRINT_RC(rc,NULL,"crop_image.crop"); --- 2450,2454 ---- /* Crop the blob out from the input image */ rc = crop_image.crop(*input_image, ! blobinfo.x1, blobinfo.y1, blobinfo.x2, blobinfo.y2 ); phPRINT_RC(rc,NULL,"crop_image.crop"); *************** *** 2457,2464 **** ratio = crop_image.getWidth() / crop_image.getHeight(); rc = crop_image.resize(this->m_width,this->m_height); phPRINT_RC(rc,NULL,"crop_image.resize(%u,%u)", this->m_width,this->m_height); ! rc = this->setInputImage( crop_image, ratio ); phPRINT_RC(rc,NULL,"this->setInputImage(crop_image,ratio)"); --- 2456,2465 ---- ratio = crop_image.getWidth() / crop_image.getHeight(); + /* don't resize the image */ + #if 0 rc = crop_image.resize(this->m_width,this->m_height); phPRINT_RC(rc,NULL,"crop_image.resize(%u,%u)", this->m_width,this->m_height); ! #endif rc = this->setInputImage( crop_image, ratio ); phPRINT_RC(rc,NULL,"this->setInputImage(crop_image,ratio)"); *************** *** 2480,2486 **** snprintf(filename,512,"blobs/file%05d_blob%06d_%s.ppm", ! filecount, ! i, ! class_string); crop_image.save(filename); --- 2481,2485 ---- snprintf(filename,512,"blobs/file%05d_blob%06d_%s.ppm", ! filecount, i, class_string); crop_image.save(filename); *************** *** 2511,2514 **** --- 2510,2518 ---- } + goto success; + error: + retrc = phFAIL; + success: + rc = this->shutdown(); phPRINT_RC(rc,NULL,"this->shutdown"); *************** *** 2518,2522 **** rc = system->shutdown(); ! phCHECK_RC(rc,NULL,"system->shutdown()"); phFree(input_nodes); --- 2522,2526 ---- rc = system->shutdown(); ! phPRINT_RC(rc,NULL,"system->shutdown()"); phFree(input_nodes); *************** *** 2529,2534 **** phDelete(med); phDelete(sobel); - phDelete(canny); - phDelete(sub); phDelete(grey_convert); phDelete(threshold_grey); --- 2533,2536 ---- *************** *** 2536,2569 **** phDelete(thresholdg); phDelete(thresholdb); - phDelete(orig); - phDelete(inverse); - phDelete(hsvconvert); - phDelete(hsvthresh); - phDelete(blob); - phDelete(blob_data); - phDelete(pipeline); - phDelete(display); - phDelete(monitor_display); - phFree(filename); - - return phSUCCESS; - error: - phFree(input_nodes); - - phDelete(input_image); - phDelete(system); - phDelete(gauss); - phDelete(mean); - phDelete(med); - phDelete(sobel); phDelete(canny); phDelete(sub); - phDelete(grey_convert); - phDelete(threshold_grey); - phDelete(thresholdr); - phDelete(thresholdg); - phDelete(thresholdb); phDelete(orig); - phDelete(inverse); phDelete(hsvconvert); phDelete(hsvthresh); --- 2538,2545 ---- phDelete(thresholdg); phDelete(thresholdb); phDelete(canny); + phDelete(inverse); phDelete(sub); phDelete(orig); phDelete(hsvconvert); phDelete(hsvthresh); *************** *** 2575,2579 **** phFree(filename); ! return phFAIL; } --- 2551,2555 ---- phFree(filename); ! return retrc; } --- NEW FILE: ml_class_stats.c --- /* --------------------------------------------------------------------------- Phission : Realtime Vision Processing System Copyright (C) 2003-2005 Philip D.S. Thoren (pt...@cs...) University of Massachusetts at Lowell, Laboratory for Artificial Intelligence and Robotics This file is part of Phission. Phission is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Phission is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Phission; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------*/ #include <ml_class_stats.h> #include <phission.h> #ifdef __cplusplus extern "C" { #endif /* ------------------------------------------------------------------------ */ int ml_train_print_log_headers( FILE *log_fp, char **tags, uint32_t ntags ) { uint32_t k = 0; if ((ntags == 0) || (log_fp == NULL) || (tags == NULL)) { return phFAIL; } /* Print out the row headers for the data being logged, to the logfile */ fprintf(log_fp, "%s %s %s %s %s %s %s %s", "Epoch", "TrainMSE", "TrainSetMSE", "TrainSetSize", "TrainSetClassified", "TrainSetIncorrectClassified", "TrainSetPercentClassified", "TrainSetPercentIncorrectClassified" ); /* Print out a row header for each of the output node statistics */ for (k = 0; k < ntags; k++ ) { fprintf(log_fp," %sCorrect_train %sIncorrect_train", tags[k], tags[k] ); fprintf(log_fp," %sPrctCorrect_train %sPrctIncorrect_train", tags[k], tags[k] ); } fprintf(log_fp, " %s %s %s %s %s %s", "TestSetMSE", "TestSetSize", "TestSetClassified", "TestSetIncorrectClassified", "TestSetPercentClassified", "TestSetPercentIncorrectClassified"); for (k = 0; k < ntags; k++ ) { fprintf(log_fp," %sCorrect_test %sIncorrect_test", tags[k], tags[k] ); fprintf(log_fp," %sPrctCorrect_test %sPrctIncorrect_test", tags[k], tags[k] ); } fprintf(log_fp,"\n"); /* flush the headers out NOW! Don't wait for the data to be * automatically flushed by the system. */ fflush(log_fp); return phSUCCESS; } /* ------------------------------------------------------------------------ */ int ml_class_stats_new( uint32_t num_outputs, ml_class_stats **pstats ) { phFUNCTION("ml_class_stats_new") if (pstats == NULL) return phFAIL; *pstats = (ml_class_stats *)phCalloc(num_outputs, sizeof(ml_class_stats)); phCHECK_PTR(*pstats,"phCalloc","phCalloc failed."); (*pstats)->num_outputs = num_outputs; (*pstats)->node_correctly_classified = (uint32_t *)phCalloc(num_outputs,sizeof(uint32_t)); phCHECK_PTR((*pstats),"phCalloc","phCalloc failed."); (*pstats)->node_incorrectly_classified = (uint32_t *)phCalloc(num_outputs,sizeof(uint32_t)); phCHECK_PTR((*pstats),"phCalloc","phCalloc failed."); return phSUCCESS; error: return phFAIL; } /* ------------------------------------------------------------------------ */ int ml_class_stats_free( ml_class_stats **pstats ) { if (pstats == NULL) return phSUCCESS; phFree((*pstats)->node_correctly_classified); phFree((*pstats)->node_incorrectly_classified); phFree(*pstats); return phSUCCESS; } /* ------------------------------------------------------------------------ */ int ml_class_stats_reset( ml_class_stats *stats ) { uint32_t i = 0; if (stats == NULL) return phSUCCESS; stats->output_matches_active = 0; stats->output_matches_inactive = 0; stats->correctly_classified = 0; stats->incorrectly_classified = 0; stats->output_nodes_active = 0; stats->output_nodes_inactive = 0; stats->nclassifications = 0; for (i = 0; i < stats->num_outputs; i++ ) { stats->node_correctly_classified[i] = 0; stats->node_incorrectly_classified[i] = 0; } return phSUCCESS; } /* ------------------------------------------------------------------------ */ int ml_class_stats_classify( ml_class_stats *stats, fann_type *expected_output, fann_type *actual_output ) { uint32_t k = 0; /* TODO: make this a function */ /* correctly classified */ stats->output_matches_active = 0; stats->output_matches_inactive = 0; stats->output_nodes_active = 0; stats->output_nodes_inactive = 0; for (k = 0; k < stats->num_outputs; k++ ) { if (expected_output[k] > 0) stats->output_nodes_active++; if (expected_output[k] < 1) stats->output_nodes_inactive++; /* if the actual output falls within the threshold * (1.0 +/- thresh) and the output node value is * also active (node is either 1 or 0), then the * node is correctly classified. */ //if ((((actual_output[k] >= (1.0-thresh)) && (actual_output[k] <= (1.0+thresh))) && (output_node_values[k] > 0))) if ((phNODE_ACTIVE(actual_output[k])) && (expected_output[k] > 0.0)) { stats->output_matches_active++; stats->node_correctly_classified[k]++; } /* if the actual output isn't within the threshold * of 1.0 and the node is inactive, then the node * is correctly classified */ else if ((phNODE_INACTIVE(actual_output[k])) && (expected_output[k] < 1.0)) { stats->output_matches_inactive++; stats->node_correctly_classified[k]++; } /* otherwise the node is incorrectly classified */ else { stats->node_incorrectly_classified[k]++; } } if ((stats->output_matches_active == stats->output_nodes_active) && (stats->output_matches_inactive == stats->output_nodes_inactive)) { stats->correctly_classified++; } else { stats->incorrectly_classified++; } stats->nclassifications++; return phSUCCESS; } /* ------------------------------------------------------------------------ */ int ml_class_stats_print_row( FILE *fp, float mse, ml_class_stats *stats ) { uint32_t k = 0; float ratio_correct = 0.0; float ratio_incorrect = 0.0; if (fp == NULL) return phSUCCESS; ratio_correct = ((float)stats->correctly_classified / (float)(stats->nclassifications)), ratio_incorrect = ((float)stats->incorrectly_classified / (float)(stats->nclassifications)); /* print the training set MSE to the log file */ fprintf(fp," %0.13f %u %u %u %0.13f %0.13f", mse, stats->nclassifications, stats->correctly_classified, stats->incorrectly_classified, ratio_correct, ratio_incorrect ); for (k = 0; k < stats->num_outputs; k++ ) { ratio_correct = ((float)stats->node_correctly_classified[k] / (float)(stats->nclassifications)); ratio_incorrect= ((float)stats->node_incorrectly_classified[k] / (float)(stats->nclassifications)); fprintf(fp," %u %u %0.13f %0.13f", stats->node_correctly_classified[k], stats->node_incorrectly_classified[k], ratio_correct, ratio_incorrect ); } fflush(fp); return phSUCCESS; } #ifdef __cplusplus } #endif |