00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "CUDA/CudaHmaxCBCL.H"
00038 #include "Image/Image.H"
00039 #include "Raster/Raster.H"
00040 #include "Util/log.H"
00041
00042 #include <fstream>
00043 #include <iostream>
00044 #include <iomanip>
00045 #include <string>
00046 #include <unistd.h>
00047 #include <cstdlib>
00048 #include <vector>
00049 #include <dirent.h>
00050
00051
00052
00053 CudaHmaxCBCL::CudaHmaxCBCL()
00054 {
00055 nc0patches = 0;
00056 nc1patches = 0;
00057 }
00058
00059 CudaHmaxCBCL::CudaHmaxCBCL(std::string c0patchFile, std::string c1patchFile)
00060 {
00061 loadC0(c0patchFile);
00062 loadC1(c1patchFile);
00063 }
00064
00065 CudaHmaxCBCL::~CudaHmaxCBCL()
00066 {
00067 if(nc0patches > 0)
00068 cpu_release_images(&c0patches,nc0patches);
00069 if(nc1patches > 0)
00070 cpu_release_images(&c1patches,nc1patches);
00071 }
00072
00073 void CudaHmaxCBCL::loadC0(std::string c0patchFile)
00074 {
00075 readFilters(c0patchFile,&c0patches,&nc0patches);
00076 }
00077
00078 void CudaHmaxCBCL::loadC1(std::string c1patchFile)
00079 {
00080 readFilters(c1patchFile,&c1patches,&nc1patches);
00081 }
00082
00083 float *CudaHmaxCBCL::getC2Features()
00084 {
00085 return c2b;
00086 }
00087
00088 int CudaHmaxCBCL::numC2Features()
00089 {
00090 return nc2units;
00091 }
00092
00093 void CudaHmaxCBCL::writeOutFeatures(std::string c2FileName, int id, bool clearFile)
00094 {
00095 std::ofstream c2File;
00096 if(clearFile)
00097 c2File.open(c2FileName.c_str(),std::ios::out);
00098 else
00099 c2File.open(c2FileName.c_str(),std::ios::app);
00100 if (c2File.is_open()) {
00101 c2File << id << " ";
00102 for(int i=0;i<nc2units;i++) {
00103 c2File << std::setiosflags(std::ios::fixed) << std::setprecision(10) <<
00104 (i+1) << ":" << c2b[i] << " ";
00105 }
00106 c2File << std::endl;
00107 }
00108 c2File.close();
00109 }
00110
00111 void CudaHmaxCBCL::extractC1Patch(Image<float> &stim, int index, int patchSize)
00112 {
00113 getC1(stim.getArrayPtr(),stim.getWidth(),stim.getHeight(),true);
00114
00115 int targC1Band = nc1bands/2;
00116 int w = cpu_get_width(&c1,nc1bands,targC1Band);
00117 int h = cpu_get_height(&c1,nc1bands,targC1Band);
00118 int wStart = floor((rand()-1.0F)/RAND_MAX*(w-patchSize));
00119 int hStart = floor((rand()-1.0F)/RAND_MAX*(h-patchSize));
00120 assert(wStart>=0);
00121 assert(hStart>=0);
00122
00123 int xPatch=0;
00124 for(int x=wStart;x<wStart+patchSize;x++)
00125 {
00126 int yPatch=0;
00127 for(int y=hStart;y<hStart+patchSize;y++)
00128 {
00129 cpu_copy_filter(&c1patches,nc1patches,index,xPatch,yPatch,&c1,targC1Band,x,y);
00130 yPatch++;
00131 }
00132 xPatch++;
00133 }
00134 clearC1();
00135 printf("Loaded patch %d\n",index);
00136
00137 }
00138
00139 void CudaHmaxCBCL::initC1Patches(int numPatches, int patchSize, int patchDepth)
00140 {
00141 cpu_create_filters(&c1patches,numPatches,patchSize,patchSize,patchDepth);
00142 nc1patches = numPatches;
00143 }
00144
00145 void CudaHmaxCBCL::extractC1Patches(std::vector<std::string> files, int numPatches, int patchSize, int patchDepth)
00146 {
00147 int numPosTrainImages = files.size();
00148 Image<float> stim;
00149
00150 std::srand(time(0));
00151
00152 initC1Patches(numPatches,patchSize,patchDepth);
00153
00154 for(int i=0;i<numPatches;i++){
00155
00156 unsigned int imInd = static_cast<unsigned int>(floor((rand()-1.0F)/RAND_MAX*numPosTrainImages));
00157 stim = Raster::ReadGrayNTSC(files[imInd].c_str());
00158 extractC1Patch(stim,i,patchSize);
00159 }
00160
00161 }
00162
00163 void CudaHmaxCBCL::writeOutC1Patches(std::string filename)
00164 {
00165 writeFilters(c1patches,nc1patches,filename.c_str());
00166 }
00167
00168 void CudaHmaxCBCL::readFilters(std::string filename,band_info** ppfilt,int* pnfilts)
00169 {
00170 cpu_read_filters(filename.c_str(),ppfilt,pnfilts);
00171 }
00172
00173 void CudaHmaxCBCL::writeFilters(band_info* pfilt,int nfilts, std::string filename)
00174 {
00175 cpu_write_filters(pfilt,nfilts,filename.c_str());
00176 }
00177
00178
00179 void CudaHmaxCBCL::getC1(const float *pimg, int width, int height, bool copy)
00180 {
00181 cpu_create_c0(pimg,width,height,&c0,&nc0bands,1.113,12);
00182 gpu_s_norm_filter(c0,nc0bands,c0patches,nc0patches,&s1,&ns1bands,false);
00183 gpu_c_local(s1,ns1bands,8,3,2,2,&c1,&nc1bands,copy);
00184 }
00185
00186 void CudaHmaxCBCL::clearC1()
00187 {
00188 cpu_release_images(&c0,nc0bands);
00189 cpu_release_images(&s1,ns1bands);
00190 cpu_release_images(&c1,nc1bands);
00191 }
00192
00193 void CudaHmaxCBCL::getC2(const float *pimg, int width, int height)
00194 {
00195 getC1(pimg,width,height,false);
00196 gpu_s_rbf(c1,nc1bands,c1patches,nc1patches,sqrtf(0.5),&s2,&ns2bands);
00197 cpu_c_global(s2,ns2bands,&c2b,&nc2units);
00198 clearC1();
00199 cpu_release_images(&s2,ns2bands);
00200
00201
00202 }
00203
00204 void CudaHmaxCBCL::clearC2()
00205 {
00206 delete [] c2b;
00207 }
00208
00209
00210 std::vector<std::string> CudaHmaxCBCL::readDir(std::string inName)
00211 {
00212 DIR *dp = opendir(inName.c_str());
00213 if(dp == NULL)
00214 {
00215 LFATAL("Directory does not exist %s",inName.c_str());
00216 }
00217 dirent *dirp;
00218 std::vector<std::string> fList;
00219 while ((dirp = readdir(dp)) != NULL ) {
00220 if (dirp->d_name[0] != '.')
00221 fList.push_back(inName + '/' + std::string(dirp->d_name));
00222 }
00223 LINFO("%"ZU" files in the directory\n", fList.size());
00224 LINFO("file list : \n");
00225 for (unsigned int i=0; i<fList.size(); i++)
00226 LINFO("\t%s", fList[i].c_str());
00227 std::sort(fList.begin(),fList.end());
00228 return fList;
00229 }
00230
00231
00232 std::vector<std::string> CudaHmaxCBCL::readList(std::string inName)
00233 {
00234 std::ifstream inFile;
00235 inFile.open(inName.c_str(),std::ios::in);
00236 if(!inFile){
00237 LFATAL("Unable to open image path list file: %s",inName.c_str());
00238 }
00239 std::string sLine;
00240 std::vector<std::string> fList;
00241 while (std::getline(inFile, sLine)) {
00242 std::cout << sLine << std::endl;
00243 fList.push_back(sLine);
00244 }
00245 LINFO("%"ZU" paths in the file\n", fList.size());
00246 LINFO("file list : \n");
00247 for (unsigned int i=0; i<fList.size(); i++)
00248 LINFO("\t%s", fList[i].c_str());
00249 inFile.close();
00250 return fList;
00251 }
00252
00253