From: <bl...@us...> - 2008-09-14 22:06:37
|
Revision: 3425 http://hugin.svn.sourceforge.net/hugin/?rev=3425&view=rev Author: blimbo Date: 2008-09-14 22:06:33 +0000 (Sun, 14 Sep 2008) Log Message: ----------- tinysvm in foreign 3 Added Paths: ----------- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/CMakeLists.txt hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/Makefile hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_solver.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/cache.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/common.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/config.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/kernel.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/misc.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/model.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/model.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/oneclass_solver.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/oneclass_solver.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/param.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/param.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/q_matrix.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/q_matrix.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/qp_solver.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/qp_solver.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/svm_solver.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/svm_solver.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/svr_solver.cpp hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/svr_solver.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/timer.h hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/tinysvm.h Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/CMakeLists.txt =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/CMakeLists.txt (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/CMakeLists.txt 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,34 @@ + +# a list of all files belonging to tinysvm library + +SET(TINYSVM_SRC +config.h +common.h +param.cpp +param.h +kernel.h +classifier.h +classifier.cpp +q_matrix.h +q_matrix.cpp +cache.h +timer.h +base_example.cpp +base_example.h +example.cpp +example.h +model.cpp +model.h +base_solver.h +oneclass_solver.cpp +oneclass_solver.h +svm_solver.cpp +svm_solver.h +svr_solver.cpp +svr_solver.h +qp_solver.h +qp_solver.cpp +tinysvm.h +) + +add_library(tinysvm STATIC ${TINYSVM_SRC}) Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/Makefile =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/Makefile (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/Makefile 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,316 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 2.4 + +# Default target executed when no arguments are given to make. +default_target: all + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canoncical targets will work. +.SUFFIXES: + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E remove -f + +# The program to use to edit the cache. +CMAKE_EDIT_COMMAND = /usr/bin/ccmake + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/tnugent/celeste_gsoc_svn + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/tnugent/celeste_gsoc_svn + +# Include the progress variables for this target. +include CMakeFiles/progress.make + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache + +# Special rule for the target install +install: preinstall + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cmake -P cmake_install.cmake + +# Special rule for the target install +install/fast: preinstall/fast + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cmake -P cmake_install.cmake + +# Special rule for the target install/local +install/local: preinstall + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake + +# Special rule for the target install/local +install/local/fast: install/local + +# Special rule for the target list_install_components +list_install_components: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Only default component available" + +# Special rule for the target list_install_components +list_install_components/fast: list_install_components + +# Special rule for the target package +package: preinstall + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Run CPack packaging tool..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cpack --config /home/tnugent/celeste_gsoc_svn/CPackConfig.cmake + +# Special rule for the target package +package/fast: package + +# Special rule for the target package_source +package_source: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Run CPack packaging tool for source..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cpack --config /home/tnugent/celeste_gsoc_svn/CPackSourceConfig.cmake + +# Special rule for the target package_source +package_source/fast: package_source + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + cd /home/tnugent/celeste_gsoc_svn/src/foreign/tinysvm && /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache + +# The main all target +all: cmake_check_build_system + cd /home/tnugent/celeste_gsoc_svn && $(CMAKE_COMMAND) -E cmake_progress_start /home/tnugent/celeste_gsoc_svn/CMakeFiles $(CMAKE_ALL_PROGRESS) + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f CMakeFiles/Makefile2 src/foreign/tinysvm/all + $(CMAKE_COMMAND) -E cmake_progress_start /home/tnugent/celeste_gsoc_svn/CMakeFiles 0 + +# The main clean target +clean: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f CMakeFiles/Makefile2 src/foreign/tinysvm/clean + +# The main clean target +clean/fast: clean + +# Prepare targets for installation. +preinstall: all + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f CMakeFiles/Makefile2 src/foreign/tinysvm/preinstall + +# Prepare targets for installation. +preinstall/fast: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f CMakeFiles/Makefile2 src/foreign/tinysvm/preinstall + +# clear depends +depend: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 + +# Convenience name for target. +src/foreign/tinysvm/CMakeFiles/tinysvm.dir/rule: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f CMakeFiles/Makefile2 src/foreign/tinysvm/CMakeFiles/tinysvm.dir/rule + +# Convenience name for target. +tinysvm: src/foreign/tinysvm/CMakeFiles/tinysvm.dir/rule + +# fast build rule for target. +tinysvm/fast: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build + +# target to build an object file +base_example.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/base_example.o + +# target to preprocess a source file +base_example.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/base_example.i + +# target to generate assembly for a file +base_example.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/base_example.s + +# target to build an object file +classifier.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/classifier.o + +# target to preprocess a source file +classifier.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/classifier.i + +# target to generate assembly for a file +classifier.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/classifier.s + +# target to build an object file +example.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/example.o + +# target to preprocess a source file +example.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/example.i + +# target to generate assembly for a file +example.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/example.s + +# target to build an object file +model.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/model.o + +# target to preprocess a source file +model.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/model.i + +# target to generate assembly for a file +model.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/model.s + +# target to build an object file +oneclass_solver.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/oneclass_solver.o + +# target to preprocess a source file +oneclass_solver.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/oneclass_solver.i + +# target to generate assembly for a file +oneclass_solver.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/oneclass_solver.s + +# target to build an object file +param.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/param.o + +# target to preprocess a source file +param.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/param.i + +# target to generate assembly for a file +param.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/param.s + +# target to build an object file +q_matrix.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/q_matrix.o + +# target to preprocess a source file +q_matrix.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/q_matrix.i + +# target to generate assembly for a file +q_matrix.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/q_matrix.s + +# target to build an object file +qp_solver.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/qp_solver.o + +# target to preprocess a source file +qp_solver.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/qp_solver.i + +# target to generate assembly for a file +qp_solver.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/qp_solver.s + +# target to build an object file +svm_solver.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svm_solver.o + +# target to preprocess a source file +svm_solver.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svm_solver.i + +# target to generate assembly for a file +svm_solver.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svm_solver.s + +# target to build an object file +svr_solver.o: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svr_solver.o + +# target to preprocess a source file +svr_solver.i: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svr_solver.i + +# target to generate assembly for a file +svr_solver.s: + cd /home/tnugent/celeste_gsoc_svn && $(MAKE) -f src/foreign/tinysvm/CMakeFiles/tinysvm.dir/build.make src/foreign/tinysvm/CMakeFiles/tinysvm.dir/svr_solver.s + +# Help Target +help:: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... install" + @echo "... install/local" + @echo "... list_install_components" + @echo "... package" + @echo "... package_source" + @echo "... rebuild_cache" + @echo "... tinysvm" + @echo "... base_example.o" + @echo "... base_example.i" + @echo "... base_example.s" + @echo "... classifier.o" + @echo "... classifier.i" + @echo "... classifier.s" + @echo "... example.o" + @echo "... example.i" + @echo "... example.s" + @echo "... model.o" + @echo "... model.i" + @echo "... model.s" + @echo "... oneclass_solver.o" + @echo "... oneclass_solver.i" + @echo "... oneclass_solver.s" + @echo "... param.o" + @echo "... param.i" + @echo "... param.s" + @echo "... q_matrix.o" + @echo "... q_matrix.i" + @echo "... q_matrix.s" + @echo "... qp_solver.o" + @echo "... qp_solver.i" + @echo "... qp_solver.s" + @echo "... svm_solver.o" + @echo "... svm_solver.i" + @echo "... svm_solver.s" + @echo "... svr_solver.o" + @echo "... svr_solver.i" + @echo "... svr_solver.s" + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + cd /home/tnugent/celeste_gsoc_svn && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.cpp =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.cpp (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.cpp 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,475 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: base_example.cpp,v 1.5 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#include "base_example.h" +#include "common.h" + +// $Id: base_example.cpp,v 1.5 2002/08/20 06:31:16 taku-ku Exp $; + +// misc function +namespace TinySVM { + +int +inc_refcount_feature_node (feature_node *f) +{ + int i; + for (i = 0; f[i].index >= 0; i++); + return --f[i].index; +} + +int +dec_refcount_feature_node (feature_node *f) +{ + int i; + for (i = 0; f[i].index >= 0; i++); + return ++f[i].index; +} + +int +comp_feature_node (const void *x1, const void *x2) +{ + feature_node *p1 = (feature_node *) x1; + feature_node *p2 = (feature_node *) x2; + return (p1->index > p2->index); +} + +feature_node * +copy_feature_node (const feature_node * f) +{ + int i; + for (i = 0; f[i].index >= 0; i++); + + try { + feature_node *r = new feature_node[i + 1]; + for (i = 0; f[i].index >= 0; i++) { + r[i].index = f[i].index; + r[i].value = f[i].value; + } + r[i].index = -1; + return r; + } + + catch (...) { + fprintf (stderr, "copy_feature_node(): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +feature_node * +str2feature_node (const char *s) +{ + int elmnum = 0; + int len = strlen (s); + + for (int i = 0; i < len; i++) if (s[i] == ':') elmnum++; + + try { + feature_node *_x = new feature_node[elmnum + 1]; + + int j = 0; + for (int i = 0; j < elmnum && i < len;) { + while (i < len && isspace (s[i])) i++; + _x[j].index = atoi (s + i); + while (i + 1 < len && s[i] != ':') i++; + _x[j].value = atof (s + i + 1); + j++; + while (i < len && !isspace (s[i])) i++; + } + + // dumy index + _x[j].index = -1; + _x[j].value = 0; + return _x; + } + + catch (...) { + fprintf (stderr, "str2feature_node(): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +feature_node * +fix_feature_node (feature_node * _x) +{ + register int i; + register int cindex = -1; + register int sorted = 1; + + // check sort + for (i = 0; _x[i].index >= 0; i++) { + if (cindex >= _x[i].index) sorted = 0; + cindex = _x[i].index; + } + + // sort + if (!sorted) qsort ((void *) _x, i, sizeof (feature_node), comp_feature_node); + return _x; +} + +BaseExample::BaseExample () +{ + l = d = pack_d = strl = 0; + stre = 0; + x = 0; + y = 0; + alpha = 0; + G = 0; + feature_type = class_type = BINARY_FEATURE; +} + +BaseExample::~BaseExample () +{ + for (int i = 0; i < l; i++) { + if (x && dec_refcount_feature_node(x[i]) == -1) delete [] x[i]; + } + + delete [] x; + delete [] y; + delete [] alpha; + delete [] G; + delete [] stre; +} + +// copy constructor +BaseExample & +BaseExample::operator =(BaseExample & e) +{ + if (this != &e) { + clear (); + for (int i = 0; i < e.l; i++) { + inc_refcount_feature_node (e.x[i]); + add (e.y[i], e.x[i]); + } + l = e.l; + pack_d = e.pack_d; + svindex_size = e.svindex_size; + if (svindex_size) { + _clone (alpha, e.alpha, svindex_size); + _clone (G, e.G, svindex_size); + } + } + + return *this; +} + +int +BaseExample::clear () +{ + for (int i = 0; i < l; i++) { + if (x && dec_refcount_feature_node(x[i]) == -1) delete [] x[i]; + } + + delete [] x; + delete [] y; + delete [] alpha; + delete [] G; + + l = d = pack_d = 0; + x = 0; + y = 0; + alpha = 0; + G = 0; + return 0; +} + + +char * +BaseExample::readLine (FILE * fp) +{ + long len; + int c; + char *tstr; + + try { + if (! stre) { + strl = MAXLEN; + stre = new char[strl]; + } + + len = 0; + tstr = stre; + + while (1) { + if (len >= strl) { + tstr = _resize (tstr, strl, strl + MAXLEN, (char)0); + strl += MAXLEN; + stre = tstr; + } + + c = fgetc (fp); + if (c == '\n' || c == '\r') { + tstr[len] = '\0'; + break; + } + + if (c == EOF && feof (fp)) { + tstr[len] = '\0'; + if (feof (fp) && len == 0) tstr = 0; + break; + } + + tstr[len++] = c; + } + + return tstr; + } + + catch (...) { + fprintf (stderr, "BaseExample::readLine(): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +int +BaseExample::remove (int i) +{ + if (i < 0 || i >= l || ! x || ! y) { + fprintf(stderr, "BaseExample::set (): Out of range\n"); + return 0; + } + + if (dec_refcount_feature_node(x[i]) == -1) delete [] x[i]; + for (int j = i+1; j < l; j++) { + x[j-1] = x[j]; + y[j-1] = y[j]; + } + + return --l; +} + +int +BaseExample::get (int i, double &_y, feature_node *&_x) +{ + if (i < 0 || i >= l || ! x || ! y) { + fprintf(stderr, "BaseExample::set (): Out of range\n"); + return 0; + } + + _y = y[i]; + _x = x[i]; + return 1; +} + +const char * +BaseExample::get (int i) +{ + if (i < 0 || i >= l || ! x || ! y) { + fprintf(stderr, "BaseExample::get (): Out of range\n"); + return 0; + } + + try { + int elmnum; + feature_node *node = x[i]; + for (elmnum = 0; node[elmnum].index >= 0; elmnum++); + + int len = _min (strl + 1024, (elmnum + 1) * 32); + if (len > strl) { + stre = _resize (stre, strl, len, (char)0); + strl = len; + } + + sprintf (stre, "%.16g", y[i]); + char tmp[32]; + for (feature_node *node = x[i]; node->index >= 0; node++) { + sprintf (tmp, " %d:%.16g", node->index, node->value); + strcat(stre, tmp); + } + + return (const char*)stre; + } + + catch (...) { + fprintf (stderr, "BaseExample::get (): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +int +BaseExample::set (int i, const double _y, feature_node *_x) +{ + if (i < 0 || i >= l || ! x || ! y) { + fprintf(stderr, "BaseExample::set (): Out of range\n"); + return 0; + } + + if (dec_refcount_feature_node(x[i]) == -1) delete [] x[i]; + _x = fix_feature_node(_x); + inc_refcount_feature_node(_x); + + x[i] = _x; + y[i] = _y; + return 1; +} + +int +BaseExample::set (int i, const double _y, const char *s) +{ + return set(i, _y, (feature_node *) str2feature_node (s)); +} + +int +BaseExample::set (int _i, const char *s) +{ + double _y = 0; + int len = strlen (s); + + int i; + for (i = 0; i < len;) { + while (isspace (s[i])) i++; + _y = atof (s + i); + while (i < len && !isspace (s[i])) i++; + while (i < len && isspace (s[i])) i++; + break; + } + + return set (_i, _y, (const char *) (s + i)); +} + +int +BaseExample::add (const double _y, feature_node * _x) +{ + try { + int fnum = 0; + feature_node *node = fix_feature_node ((feature_node *) _x); + + // check contents + for (int i = 0; (node + i)->index >= 0; i++) { + if ((node + i)->value != 1) feature_type = DOUBLE_FEATURE; // check feature type + d = _max (d, (node + i)->index); // save max dimension + fnum++; + } + + // incriment refcount + inc_refcount_feature_node (node); + pack_d = _max (fnum, pack_d); + + // check class type + if (_y != +1 && _y != -1) class_type = DOUBLE_FEATURE; + + // resize + x = _append (x, l, node, (feature_node*)0); + y = _append (y, l, _y, 0.0); + l++; + + return 1; + } + + catch (...) { + fprintf (stderr, "BaseExample::add(): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +int +BaseExample::add (const double _y, const char *s) +{ + return add (_y, (feature_node *) str2feature_node (s)); +} + +int +BaseExample::add (const char *s) +{ + double _y = 0; + int len = strlen (s); + + int i; + for (i = 0; i < len;) { + while (isspace (s[i])) i++; + _y = atof (s + i); + while (i < len && !isspace (s[i])) i++; + while (i < len && isspace (s[i])) i++; + break; + } + + return add (_y, (const char *) (s + i)); +} + +int +BaseExample::writeSVindex (const char *filename, const char *mode, const int offset) +{ + if (!alpha || !G) return 0; + + FILE *fp = fopen (filename, mode); + if (!fp) return 0; + + for (int i = 0; i < svindex_size; i++) + fprintf (fp, "%.16g %.16g\n", alpha[i], G[i]); + + fclose (fp); + return 1; +} + +int +BaseExample::readSVindex (const char *filename, const char *mode, const int offset) +{ + if (l == 0) { + fprintf(stderr, "Fatal: size == 0, Read model/example file before reading .idx file\n"); + return 0; + } + + FILE *fp = fopen (filename, mode); + if (!fp) return 0; + + delete [] alpha; + delete [] G; + + int _l = 0; + char *buf; + + while ((buf = readLine (fp)) != NULL) { + double alpha_, G_; + if (2 != sscanf (buf, "%lf %lf\n", &alpha_, &G_)) { + fprintf(stderr, "Fatal: Format error %s, line %d\n", filename, _l); + fclose (fp); + return 0; + } + + alpha = _append (alpha, _l, alpha_, 0.0); + G = _append (G, _l, G_, 0.0); + _l++; + } + + fclose (fp); + + // check size of idx file + if (l < _l) { + fprintf(stderr, "Fatal: model/example size (%d) < idx size (%d)\n", l, _l); + delete [] alpha; + delete [] G; + alpha = 0; + G = 0; + return 0; + } + + svindex_size = _l; + return 1; +} +} Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_example.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,84 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: base_example.h,v 1.22 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _BASE_EXAMPLE_H +#define _BASE_EXAMPLE_H +#include "misc.h" +#include <stdio.h> + +// $Id: base_example.h,v 1.22 2002/08/20 06:31:16 taku-ku Exp $; + +namespace TinySVM { + +extern feature_node *str2feature_node (const char *); +extern feature_node *copy_feature_node (const feature_node *); +extern feature_node *fix_feature_node (feature_node *); + +class BaseExample +{ +private: + char *stre; + int strl; + int alloc_l; + +public: + int l; // number of SVs + int d; // dimension of SVs + int pack_d; // packed dimension of SVs + double *y; // class label, + feature_node **x; // training data + int feature_type; // feature type (bin or double) + int class_type; // class type (bin or double) + double *alpha; // alpha + double *G; // gradient + int svindex_size; // number of Support Vectors; + + int add (const double, feature_node *); + int add (const double, const char *); + int add (const char *); + int set (int i, const double, feature_node *); + int set (int i, const double, const char *); + int set (int i, const char *); + int get (int i, double &, feature_node *&); + const char *get (int i); + int remove (int i); + + int clear (); + int size () { return l; }; + + virtual int read (const char *, const char *mode = "r", const int offset = 0) = 0; + virtual int write (const char *, const char *mode = "w", const int offset = 0) = 0; + char *readLine (FILE *); + + int readSVindex (const char *, const char *mode = "r", const int offset = 0); + int writeSVindex (const char *, const char *mode = "w", const int offset = 0); + + BaseExample& operator=(BaseExample &); + BaseExample (); + virtual ~BaseExample(); +}; + +}; +#endif Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_solver.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_solver.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/base_solver.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,50 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: base_solver.h,v 1.9 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _SOLVER_H +#define _SOLVER_H +#include "model.h" + +// $Id: base_solver.h,v 1.9 2002/08/20 06:31:16 taku-ku Exp $; + +namespace TinySVM { + +class BaseSolver +{ + protected: + const BaseExample &example; + const Param param; + const int l; + + public: + virtual Model *learn() = 0; + BaseSolver(const BaseExample &e, const Param &p): + example (e), param (p), l (e.l) {}; + virtual ~BaseSolver() {}; +}; + +}; +#endif + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/cache.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/cache.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/cache.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,223 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: cache.h,v 1.22 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _CACHE_H +#define _CACHE_H +#include "common.h" + +// $Id: cache.h,v 1.22 2002/08/20 06:31:16 taku-ku Exp $; + +// Kernel Cache + +namespace TinySVM { + +template <class T = double> class Cache +{ +private: + int l; + int free_mem_size; + double cache_mem_size; + + struct head_t + { + head_t *prev, *next; // a cicular list + int index; + T* data; + }; + + head_t* lru_head; + head_t** index2head; + + inline int get_cache_size(const int _l, const double mem_size) + { + return _min (_l, _max(2, (int)(1024 * 1024 * mem_size/(sizeof(T) * _l)))); + } + + // delete h from current postion + inline void delete_lru(head_t *h) + { + h->prev->next = h->next; + h->next->prev = h->prev; + } + + // insert h to lru - 1, position + inline void insert_lru(head_t *h) + { + h->next = lru_head; + h->prev = lru_head->prev; + h->prev->next = h; + h->next->prev = h; + } + +public: + int size; + + Cache(int, double); + ~Cache(); + + void update(const int); + + void delete_index(const int index) + { + head_t *h = index2head[index]; + if (h) { + if (lru_head != h) { + delete_lru (h); + insert_lru (h); + } + lru_head = h; + } + } + + inline int getData(const int index, T **data) + { + head_t *h = index2head[index]; + if (h) { + if(lru_head == h) { + lru_head = lru_head->next; + } else { + delete_lru(h); + insert_lru(h); + } + + *data = h->data; + return 1; + } else { + h = lru_head; + lru_head = lru_head->next; + if (h->index!=-1) index2head[h->index] = 0; + h->index = index; + index2head[index] = h; + *data = h->data; + return 0; + } + } + + void swap_index(const int i, const int j) + { + _swap (index2head[i], index2head[j]); + for (head_t *h = lru_head;;h = h->next) { + if (h->index == i) h->index = j; + else if (h->index == j) h->index = i; // fixed, 2001-12-23, fatal bug .. + _swap(h->data[i], h->data[j]); + if (h == lru_head->prev) break; + } + } +}; + +template <class T> +Cache<T>::Cache(int _l, double _cache_mem_size): + l(_l), + cache_mem_size(_cache_mem_size) +{ + try { + size = get_cache_size(l, cache_mem_size); + + free_mem_size = 0; + + head_t *start = new head_t; + head_t *prev = start; + start->index = -1; + start->data = new T [l]; + + int tmp_size = 1; + + for(int i = 1; i < size; i++) { + tmp_size++; + head_t *head = new head_t; + head->prev = prev; + prev->next = head; + head->index = -1; + head->data = new T [l]; + prev = head; + } + + prev->next = start; + start->prev = prev; + lru_head = start; + + index2head = new head_t *[l]; + for(int i = 0; i < l; i++) index2head[i] = 0; + } + + catch (...) { + fprintf(stderr,"Cache::Cache(): Out of memory\n"); + exit(1); + } +} + +template <class T> +Cache<T>::~Cache() +{ + delete [] index2head; + + int flag = 1; + head_t *end = lru_head->prev; + head_t *h = lru_head; + + while (flag) { + delete [] h->data; + head_t *tmp = h->next; + if (h == end) flag = 0; + delete h; + h = tmp; + } +} + +// change the size of each elements to be _l +// if _l < l, allocate new erea for cache +template <class T> +void Cache<T>::update(const int _l) +{ + int new_size = get_cache_size(_l, cache_mem_size); + + if (100 * new_size/size >= 110 && new_size < _l) { + // realloc + for (head_t *h = lru_head;;h = h->next) { + T *new_data; + _clone(new_data, h->data, _l); + delete [] h->data; + h->data = new_data; + if (h == lru_head->prev) break; + } + + // new node + for (int i = 0; i < (new_size - size); i++) { + head_t *h = new head_t; + h->data = new T [_l]; + h->index = -1; + insert_lru(h); + lru_head = h; + } + + size = new_size; + } + + l = _l; +} + +}; +#endif + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.cpp =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.cpp (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.cpp 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,150 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: classifier.cpp,v 1.4 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#include "common.h" +#include "classifier.h" + +namespace TinySVM { + +Classifier::Classifier (const BaseExample & example, const Param & param): + Kernel (example, param) +{ + try { + x = example.x; + y = example.y; + model_bias = 0; + + if (feature_type == BINARY_FEATURE && dot_kernel) { + _getDistance = &Classifier::_getDistance_binary; + + dot_buf = new int[l]; + binary_kernel_cache = new double *[l]; + fi2si = new int *[d + 1]; + int *_fisize = new int[d + 1]; + + // init + for (int i = 0; i < (d + 1); i++) + _fisize[i] = 0; + + // 1st, detemin the size of each array + for (int i = 0; i < l; i++) { + feature_node *node = (feature_node *) x[i]; + int feature_num = 0; + while (node->index >= 0) { + _fisize[node->index]++; + node++; + feature_num++; + } + + // caluculate cache + model_bias += y[i] * (this->*_getKernel2) (0.0); + binary_kernel_cache[i] = new double[feature_num + 1]; + for (int j = 1; j <= feature_num; j++) { + binary_kernel_cache[i][j] = + y[i] * ((this->*_getKernel2) ((double) j) - + (this->*_getKernel2) ((double) (j - 1))); + } + } + + // 2nd memory allocate for each array + for (int i = 0; i < (d + 1); i++) { + fi2si[i] = new int[_fisize[i] + 1]; + _fisize[i] = 0; + } + + // 3rd, copy value + for (int i = 0; i < l; i++) { + feature_node *node = (feature_node *) x[i]; + while (node->index >= 0) { + fi2si[node->index][_fisize[node->index]++] = i; + node++; + } + } + + // final fill -1 (dumy field) + for (int i = 0; i < (d + 1); i++) + fi2si[i][_fisize[i]++] = -1; + + delete[]_fisize; + + } else { + _getDistance = &Classifier::_getDistance_normal; + } + } + + catch (...) { + fprintf (stderr, "Classifier::Classifier(): Out of memory\n"); + exit (EXIT_FAILURE); + } +} + +Classifier::~Classifier () +{ + if (feature_type == BINARY_FEATURE && dot_kernel) { + delete [] dot_buf; + for (int i = 0; i < l; i++) + delete [] binary_kernel_cache[i]; + delete [] binary_kernel_cache; + + for (int i = 0; i < (d + 1); i++) + delete [] fi2si[i]; + delete [] fi2si; + } +} + +double +Classifier::_getDistance_normal (const feature_node * _x) const +{ + register double result = 0.0; + register feature_node *node = fix_feature_node ((feature_node *) _x); + + for (register int i = 0; i < l; i++) + result += y[i] * (this->*_getKernel) (x[i], node); + + return result; +} + +double +Classifier::_getDistance_binary (const feature_node * node) const +{ + register double result = model_bias; + register int j, k; + register int *p; + + memset (dot_buf, 0, sizeof (int) * l); + + feature_node *org = (feature_node *) node; + while (node->index >= 0 && node->index <= d) { + if (node->value != 1) + return this->_getDistance_normal (org); + for (p = fi2si[node->index], j = 0; (k = p[j]) != -1; j++) + result += (binary_kernel_cache[k][++dot_buf[k]]); + node++; + } + + return result; +} + +}; Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/classifier.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,65 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: classifier.h,v 1.8 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _CLASSIFIER_H +#define _CLASSIFIER_H +#include "kernel.h" +#include "config.h" + +// $Id: classifier.h,v 1.8 2002/08/20 06:31:16 taku-ku Exp $; +namespace TinySVM { + +class Classifier: public Kernel +{ + private: + double model_bias; + int *dot_buf; + double **binary_kernel_cache; + int **fi2si; + + double (Classifier::*_getDistance) (const feature_node *) const; + double _getDistance_binary (const feature_node *) const; + double _getDistance_normal (const feature_node *) const; + + public: + Classifier (const BaseExample &, const Param &); + ~Classifier (); + + inline double getDistance (const char *s) + { + feature_node *node = str2feature_node (s); + double d = getDistance(node); + delete [] node; + return d; + } + + inline double getDistance (const feature_node *_x) + { + return (this->*_getDistance)(_x); + } +}; +}; +#endif + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/common.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/common.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/common.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,149 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: common.h,v 1.36 2002/08/20 06:31:16 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include "config.h" + +#ifndef _COMMON_H +#define _COMMON_H + +#define COPYRIGHT "TinySVM - tiny SVM package\nCopyright (C) 2000-2002 Taku Kudo, All rights reserved.\n" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef STDC_HEADERS +#include <stdio.h> +#endif + +#ifdef HAVE_STDIO_H +#include <stdio.h> +#endif + +#ifdef HAVE_STDLIB_H +#include <stdlib.h> +#endif + +#ifdef HAVE_CTYPE_H +#include <ctype.h> +#endif + +#ifdef HAVE_STRING_H +#include <string.h> +#endif + +#ifdef HAVE_MATH_H +#include <math.h> +#endif + +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + +#ifdef HAVE_SYS_TIMES_H +#include <sys/times.h> +#endif + +#ifdef HAVE_WINDOWS_H +#include <windows.h> +#endif + +#ifndef HAVE_MEMSET +#ifdef HAVE_BZERO +#define memset(a,b,c) bzero((a),(c)) +#else +template <class T> inline void memset(T* &a, int b, int c) +{ + for (int i = 0; i < c; i++) a[i] = b; +} +#endif +#endif + +#ifndef HAVE_MEMCPY +#ifdef HAVE_BCOPY +#define memcpy(a,b,c) bcopy((b),(a),(c)) +#else +template <class T> inline void memcpy(T* &a, T &b, int c) +{ + for (int i = 0; i < c; i++) a[i] = b[i]; +} +#endif +#endif + +#define EPS_A 1e-12 +#ifndef HUGE_VAL +#define HUGE_VAL 1e+37 +#endif + +#define INF HUGE_VAL +#define MAXLEN 1024 + +#ifndef EXIT_FAILURE +#define EXIT_FAILURE 1 +#endif + +#ifndef EXIT_SUCCESS +#define EXIT_SUCCESS 0 +#endif + +// Factory Functions +namespace TinySVM { + +template <class T> inline T _min (T x, T y) { return (x < y) ? x : y; } +template <class T> inline T _max (T x, T y) { return (x > y) ? x : y; } +template <class T> inline void _swap (T &x, T &y) { T z = x; x = y; y = z; } + +template <class S, class T> inline void _clone (T*& dst, S* src, int n) +{ + dst = new T [n]; + memcpy ((void *)dst, (void *)src, sizeof(T)*n); +} + +// resize ptr from n to l +template <class T> inline T* _resize (T* ptr, int n, int l, T v) +{ + T *dst = new T [l]; + + memcpy ((void *)dst, (void *)ptr, sizeof(T)*n); + for (int i = n; i < l; i++) dst[i] = v; + delete [] ptr; + return dst; +} + +// increse size of array automatically, simple implimentation +// you have to incriment N by yourself +template <class T> inline T* _append (T* ptr, int n, T v1, T v2) +{ + if (n % MAXLEN == 0) ptr = _resize (ptr, n, n + MAXLEN, v2); + ptr[n] = v1; + + return ptr; +} +} + +#endif Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/config.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/config.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/config.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,78 @@ +/* config.h. Generated automatically by configure. */ +/* config.h.in. Generated automatically from configure.in by autoheader. */ + +/* Define to `int' if <sys/types.h> doesn't define. */ +/* #undef pid_t */ + +/* Define to `unsigned' if <sys/types.h> doesn't define. */ +/* #undef size_t */ + +/* Define if you have the ANSI C header files. */ +/* #undef STDC_HEADERS */ + +#define HAVE_CXX_STD_NAMESPACE 1 +/* #undef HAVE_CXX_NAMESPACE */ + +/* Define if you have the bcopy function. */ +#define HAVE_BCOPY 1 + +/* Define if you have the bzero function. */ +#define HAVE_BZERO 1 + +/* Define if you have the getopt function. */ +#define HAVE_GETOPT 1 + +/* Define if you have the getopt_long function. */ +#define HAVE_GETOPT_LONG 1 + +/* Define if you have the isspace function. */ +#define HAVE_ISSPACE 1 + +/* Define if you have the memcpy function. */ +#define HAVE_MEMCPY 1 + +/* Define if you have the memset function. */ +#define HAVE_MEMSET 1 + +/* Define if you have the <ctype.h> header file. */ +#define HAVE_CTYPE_H 1 + +/* Define if you have the <dlfcn.h> header file. */ +#define HAVE_DLFCN_H 1 + +/* Define if you have the <getopt.h> header file. */ +#define HAVE_GETOPT_H 1 + +/* Define if you have the <math.h> header file. */ +#define HAVE_MATH_H 1 + +/* Define if you have the <stdio.h> header file. */ +#define HAVE_STDIO_H 1 + +/* Define if you have the <stdlib.h> header file. */ +#define HAVE_STDLIB_H 1 + +/* Define if you have the <string.h> header file. */ +#define HAVE_STRING_H 1 + +/* Define if you have the <sys/times.h> header file. */ +#define HAVE_SYS_TIMES_H 1 + +/* Define if you have the <sys/types.h> header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define if you have the <unistd.h> header file. */ +#define HAVE_UNISTD_H 1 + +/* Define if you have the <windows.h> header file. */ +/* #undef HAVE_WINDOWS_H */ + +/* Define if you have the m library (-lm). */ +#define HAVE_LIBM 1 + +/* Name of package */ +#define PACKAGE "TinySVM" + +/* Version number of package */ +#define VERSION "0.09" + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.cpp =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.cpp (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.cpp 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,152 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: example.cpp,v 1.5 2002/08/20 06:31:17 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#include "example.h" +#include "misc.h" +#include "common.h" +#include "base_solver.h" +#include "svm_solver.h" +#include "svr_solver.h" +#include "timer.h" +#include "oneclass_solver.h" + +// $Id: example.cpp,v 1.5 2002/08/20 06:31:17 taku-ku Exp $; + +namespace TinySVM { + +Example::Example() {}; +Example::~Example() {}; + +Model * +Example::learn (const Param & p) +{ + try { + if (l == 0) return 0; + BaseSolver *solver; + if (p.model[0]) { + char *tmp = new char [strlen(p.model) + 5]; + strcpy (tmp, p.model); + strcat (tmp, ".idx"); + fflush(stdout); + + if (readSVindex(tmp)) { + TinySVM::Model *m = new TinySVM::Model; + if (m->read (p.model)) rebuildSVindex (m); + else fprintf (stderr, "Example::learn() cannot open %s, ignored.\n", p.model); + delete m; + } else { + fprintf (stderr, "Example::learn() cannot open %s, ignored.\n", tmp); + } + delete [] tmp; + } + + switch (p.solver_type) { + case SVM: + solver = new SVM_Solver (*this, p); + break; + case SVR: + solver = new SVR_Solver (*this, p); + break; + case ONE_CLASS: + solver = new OneClass_Solver (*this, p); + break; + default: + fprintf (stderr, "Example::learn(): Unknown solver type [%d]\n", p.solver_type); + return 0; + } + + Timer timer; + Model *m = solver->learn (); + delete solver; + fprintf (stdout, "CPU Time:\t\t\t%s\n", timer.getDiff ()); + + return m; + } + + catch (...) { + fprintf (stderr, "Example::learn(): Out of memory\n"); + exit (EXIT_FAILURE); + return 0; + } +} + +int +Example::read (const char *filename, const char *mode, const int offset) +{ + FILE *fp = fopen (filename, mode); + if (!fp) return 0; + + fseek (fp, offset, SEEK_SET); + char *buf; + int line = 0; + while ((buf = readLine (fp)) != NULL) { + line++; + if (!add (buf)) { + fprintf (stderr, "Example::read(): Format error in file %s, line %d\n", + filename, line); + fclose (fp); + return 0; + } + } + fclose (fp); + svindex_size = l; + + return 1; +} + +int +Example::write (const char *filename, const char *mode, const int offset) +{ + FILE *fp = fopen (filename, mode); + if (!fp) return 0; + + for (int i = 0; i < l; i++) { + fprintf (fp, "%.16g", y[i]); + for (feature_node * node = x[i]; node->index >= 0; node++) + fprintf (fp, " %d:%.16g", node->index, node->value); + fprintf (fp, "\n"); + } + + fclose (fp); + return 1; +} + +int +Example::rebuildSVindex (Model *m) +{ + if (!m ) return 0; + + alpha = _resize (alpha, svindex_size, l, 0.0); + G = _resize (G, svindex_size, l, 0.0); + + for (int i = svindex_size; i < l; i++) { + G[i] = y[i] * (m->classify(x[i]) + m->b) - 1; + alpha[i] = 0; + } + + svindex_size = l; + return l; +} +} Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/example.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,58 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: example.h,v 1.16 2002/08/20 06:31:17 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _EXAMPLE_H +#define _EXAMPLE_H +#include "param.h" +#include "model.h" +#include "base_example.h" + +// $Id: example.h,v 1.16 2002/08/20 06:31:17 taku-ku Exp $; + +namespace TinySVM { + +class Example: public BaseExample +{ +public: + int read (const char *, const char *mode = "r", const int offset = 0); + int write (const char *, const char *mode = "w", const int offset = 0); + int rebuildSVindex (Model *); + int rebuildSVindex (Model &m) { return rebuildSVindex(&m); }; + + Example(); + ~Example(); + Model *learn(const Param &); + Model *learn(const Param *p) { return this->learn(*p); }; + Model *learn(const char *s) + { + Param p; + if (p.set(s)) return this->learn(p); + else return NULL; + } +}; + +}; +#endif + Added: hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/kernel.h =================================================================== --- hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/kernel.h (rev 0) +++ hugin/branches/gsoc2008_sky_identification/src/foreign/tinysvm/kernel.h 2008-09-14 22:06:33 UTC (rev 3425) @@ -0,0 +1,249 @@ +/* + TinySVM -- Yet Another Tiny SVM Package + + $Id: kernel.h,v 1.18 2002/08/20 06:31:17 taku-ku Exp $; + + Copyright (C) 2001-2002 Taku Kudo <ta...@is...> + All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later verjsion. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +#ifndef _KERNEL_H +#define _KERNEL_H +#include "misc.h" +#include "param.h" +#include "base_example.h" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +// $Id: kernel.h,v 1.18 2002/08/20 06:31:17 taku-ku Exp $; +namespace TinySVM { + +class Kernel +{ +protected: + // constant param + const int l; + const int d; + const int pack_d; + const int kernel_type; + const int feature_type; + const int dot_kernel; + const int degree; + const double param_g; + const double param_r; + const double param_s; + + double (Kernel::*_getKernel)(const feature_node *, const feature_node *) const; + double (Kernel::*_getKernel2)(const double) const; + + // linear + inline double _getKernel_linear(const feature_node *x1, const feature_node *x2) const + { + return dot_normal(x1,x2); + } + + inline double _getKernel_linear2(const double _x) const + { + return _x; + } + + // polynomial + inline double _getKernel_poly(const feature_node *x1, const feature_node *x2) const + { + return pow (param_s * dot_normal(x1,x2) + param_r, degree); + } + + inline double _getKernel_poly2(const double _x) const + { + return pow (param_s * _x + param_r, degree); + } + + // neural + inline double _getKernel_neural(const feature_node *x1, const feature_node *x2) const + { + return tanh (param_s * dot_normal(x1,x2) + param_r); + } + + inline double _getKernel_neural2(const double _x) const + { + return tanh (param_s * _x + param_r); + } + + // RBF + inline double _getKernel_rbf(const feature_node *x1, const feature_node *x2) const + { + return exp (-param_s * norm2(x1,x2)); + } + + inline double _getKernel_rbf2(const double _x) const + { + fprintf (stderr, "Kernel::getKernel_rbf() cannot obtain kernel value only with dot.\n"); + exit (-1); + return 0.0; + } + + // ANOVA + inline double _getKernel_anova(const feature_node *x1, const feature_node *x2) const + { + register double sum = 0; + register int zero = d; + + while (x1->index >= 0 && x2->index >= 0) { + if (x1->index == x2->i... [truncated message content] |