diff --git a/.gitignore b/.gitignore index 53c1fb056bb..4e99c067660 100644 --- a/.gitignore +++ b/.gitignore @@ -93,3 +93,11 @@ LOCK LOG* CURRENT MANIFEST-* + + +# Windows dev files +Release/ +Debug/ +3rdparty/ +*.*sdf +*.deps \ No newline at end of file diff --git a/MSVC/caffe.vcxproj b/MSVC/caffe.vcxproj new file mode 100644 index 00000000000..6875b69c75a --- /dev/null +++ b/MSVC/caffe.vcxproj @@ -0,0 +1,129 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {537EF951-4B02-4103-A310-52A6F7C8D748} + Win32Proj + caffe + Caffe + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + + true + $(SolutionDir)bin\$(Configuration)\ + .exe + $(ProjectName)D + obj\$(ProjectName)\$(Configuration)\ + + + false + $(SolutionDir)bin\$(Configuration)\ + .exe + obj\$(ProjectName)\$(Configuration)\ + + + + + + Level3 + Disabled + WIN32;USE_CUDNN;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + Console + true + lib\$(Configuration);..\3rdparty\lib;..\3rdparty\lib;$(CUDA_PATH)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + caffelib.lib;cudnn.lib;opencv_core249d.lib;opencv_calib3d249d.lib;opencv_contrib249d.lib;opencv_flann249d.lib;opencv_highgui249d.lib;opencv_imgproc249d.lib;opencv_legacy249d.lib;opencv_ml249d.lib;opencv_gpu249d.lib;opencv_objdetect249d.lib;opencv_photo249d.lib;opencv_features2d249d.lib;opencv_nonfree249d.lib;opencv_stitching249d.lib;opencv_video249d.lib;opencv_videostab249d.lib;cudart.lib;cuda.lib;nppi.lib;cufft.lib;cublas.lib;curand.lib;gflagsd.lib;libglog.lib;libopenblas.dll.a;libprotobufd.lib;libprotoc.lib;leveldbd.lib;lmdbd.lib;libhdf5_D.lib;libhdf5_hl_D.lib;Shlwapi.lib;%(AdditionalDependencies) + + + + robocopy ..\3rdparty\bin $(SolutionDir)bin\$(Configuration) /xo /xn +IF %ERRORLEVEL% GEQ 8 exit 1 +exit 0 + Copy dlls to app folder + + + + + Level3 + + + MaxSpeed + true + true + WIN32;USE_CUDNN;NDEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + Console + true + true + true + lib\$(Configuration);..\3rdparty\lib;..\3rdparty\lib;$(CUDA_PATH)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + caffelib.lib;cudnn.lib;opencv_core249.lib;opencv_flann249.lib;opencv_imgproc249.lib;opencv_highgui249.lib;opencv_legacy249.lib;opencv_video249.lib;opencv_ml249.lib;opencv_calib3d249.lib;opencv_objdetect249.lib;opencv_stitching249.lib;opencv_gpu249.lib;opencv_nonfree249.lib;opencv_features2d249.lib;cudart.lib;cuda.lib;nppi.lib;cufft.lib;cublas.lib;curand.lib;gflags.lib;libglog.lib;libopenblas.dll.a;libprotobuf.lib;libprotoc.lib;leveldb.lib;lmdb.lib;libhdf5.lib;libhdf5_hl.lib;Shlwapi.lib;%(AdditionalDependencies) + + + robocopy ..\3rdparty\bin $(SolutionDir)bin\$(Configuration) /xo /xn +IF %ERRORLEVEL% GEQ 8 exit 1 +exit 0 + Copy dlls to app folder + + + true + + + + + + + + {7f271127-d68a-4b9b-acce-1369394ed6e5} + false + true + false + true + true + + + + + + \ No newline at end of file diff --git a/MSVC/caffe.vcxproj.filters b/MSVC/caffe.vcxproj.filters new file mode 100644 index 00000000000..b94d5bc6849 --- /dev/null +++ b/MSVC/caffe.vcxproj.filters @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MSVC/caffelib.vcxproj b/MSVC/caffelib.vcxproj new file mode 100644 index 00000000000..7813e41ae7b --- /dev/null +++ b/MSVC/caffelib.vcxproj @@ -0,0 +1,271 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {7F271127-D68A-4B9B-ACCE-1369394ED6E5} + Win32Proj + caffe + CaffeLib + + + + StaticLibrary + true + v120 + Unicode + + + StaticLibrary + false + v120 + true + Unicode + + + + + + + + + + + + + + true + lib\$(Configuration)\ + .lib + $(ProjectName) + obj\$(ProjectName)\$(Configuration)\ + + + false + lib\$(Configuration)\ + .lib + obj\$(ProjectName)\$(Configuration)\ + + + + + + Level3 + Disabled + WIN32;USE_CUDNN;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + ../scripts/GeneratePB.bat + + + + + + + $(OutDir)$(TargetName)$(TargetExt) + + + compute_35,sm_35;compute_52,sm_52;compute_52,compute_52 + + + + + Level3 + + + MaxSpeed + true + true + WIN32;USE_CUDNN;NDEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + ../scripts/GeneratePB.bat + + + compute_35,sm_35;compute_52,sm_52;compute_52,compute_52 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MSVC/caffelib.vcxproj.filters b/MSVC/caffelib.vcxproj.filters new file mode 100644 index 00000000000..0c081aafb5b --- /dev/null +++ b/MSVC/caffelib.vcxproj.filters @@ -0,0 +1,494 @@ + + + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\util + + + Source Files\util + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files\util + + + Source Files + + + Source Files\util + + + Source Files\layers + + + Source Files\layers + + + Source Files\layers + + + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + Head Files + + + + + {93c9d8fe-d851-4bff-951e-ca0a847fa914} + + + {96337b54-9395-4d43-8842-41c1449332e9} + + + {5b7591ff-b0b4-4618-9be6-d8642730c68a} + + + {e7bb9e91-e346-4c51-a426-223799f528b7} + + + + + + \ No newline at end of file diff --git a/MSVC/pycaffe.vcxproj b/MSVC/pycaffe.vcxproj new file mode 100644 index 00000000000..550a88f3c62 --- /dev/null +++ b/MSVC/pycaffe.vcxproj @@ -0,0 +1,117 @@ + + + + + Debug + x64 + + + Release + x64 + + + + + + + + + + {C209DCB1-C214-4182-B340-F171BB50B45B} + Win32Proj + caffe + pycaffe + SAK + SAK + SAK + SAK + + + + DynamicLibrary + true + v120 + Unicode + + + DynamicLibrary + false + v120 + true + Unicode + + + + + + + + + + + + + + true + $(SolutionDir)../bin + .pyd + + + false + $(SolutionDir)../bin + .pyd + + + + + + Level3 + Disabled + WIN32;USE_CUDNN;_DEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + Console + true + caffelibd.lib;cudnn.lib;opencv_core249d.lib;opencv_calib3d249d.lib;opencv_contrib249d.lib;opencv_flann249d.lib;opencv_highgui249d.lib;opencv_imgproc249d.lib;opencv_legacy249d.lib;opencv_ml249d.lib;opencv_gpu249d.lib;opencv_objdetect249d.lib;opencv_photo249d.lib;opencv_features2d249d.lib;opencv_nonfree249d.lib;opencv_stitching249d.lib;opencv_video249d.lib;opencv_videostab249d.lib;cudart.lib;cuda.lib;nppi.lib;cufft.lib;cublas.lib;curand.lib;gflagsd.lib;libglog.lib;libopenblas.dll.a;libprotobufd.lib;libprotoc.lib;leveldbd.lib;lmdbd.lib;libhdf5_D.lib;libhdf5_hl_D.lib;Shlwapi.lib;%(AdditionalDependencies) + ..\3rdparty\lib;..\3rdparty\lib;$(CUDA_PATH)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + + + ../scripts/GeneratePB.bat + + + copy /y "..\bin\caffe.lib" "..\bin\caffed.lib" + + + + + Level3 + + + MaxSpeed + true + true + WIN32;USE_CUDNN;NDEBUG;_CONSOLE;_LIB;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + ..\include;..\src;..\3rdparty\include;..\3rdparty\include\openblas;..\3rdparty\include\hdf5;..\3rdparty\include\lmdb;..\3rdparty\include\opencv;$(CUDA_PATH)\include;%(AdditionalIncludeDirectories) + -D_SCL_SECURE_NO_WARNINGS %(AdditionalOptions) + + + Console + true + true + true + caffelib.lib;cudnn.lib;opencv_core249.lib;opencv_flann249.lib;opencv_imgproc249.lib;opencv_highgui249.lib;opencv_legacy249.lib;opencv_video249.lib;opencv_ml249.lib;opencv_calib3d249.lib;opencv_objdetect249.lib;opencv_stitching249.lib;opencv_gpu249.lib;opencv_nonfree249.lib;opencv_features2d249.lib;cudart.lib;cuda.lib;nppi.lib;cufft.lib;cublas.lib;curand.lib;gflags.lib;libglog.lib;libopenblas.dll.a;libprotobuf.lib;libprotoc.lib;leveldb.lib;lmdb.lib;libhdf5.lib;libhdf5_hl.lib;Shlwapi.lib;python27.lib;%(AdditionalDependencies) + ..\3rdparty\lib;..\3rdparty\lib;$(CUDA_PATH)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + + + ../scripts/GeneratePB.bat + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index ebec286d550..61eae4c553f 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,10 @@ Check out the [project site](http://caffe.berkeleyvision.org) for all the detail and step-by-step examples. -[![Join the chat at https://gitter.im/BVLC/caffe](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/BVLC/caffe?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +## Setup +`git clone git@github.com:MSRDL/caffe.git` -Please join the [caffe-users group](https://groups.google.com/forum/#!forum/caffe-users) or [gitter chat](https://gitter.im/BVLC/caffe) to ask questions and talk about methods and models. -Framework development discussions and thorough bug reports are collected on [Issues](https://github.com/BVLC/caffe/issues). - -Happy brewing! +For development in Windows, also run `git clone git@github.com:MSRDL/wincaffe-3rdparty.git 3rdparty` from the `caffe` folder. ## License and Citation diff --git a/caffex.sln b/caffex.sln new file mode 100644 index 00000000000..439c25dbb3b --- /dev/null +++ b/caffex.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.40629.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CaffeLib", "MSVC\caffelib.vcxproj", "{7F271127-D68A-4B9B-ACCE-1369394ED6E5}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Caffe", "MSVC\caffe.vcxproj", "{537EF951-4B02-4103-A310-52A6F7C8D748}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7F271127-D68A-4B9B-ACCE-1369394ED6E5}.Debug|x64.ActiveCfg = Debug|x64 + {7F271127-D68A-4B9B-ACCE-1369394ED6E5}.Debug|x64.Build.0 = Debug|x64 + {7F271127-D68A-4B9B-ACCE-1369394ED6E5}.Release|x64.ActiveCfg = Release|x64 + {7F271127-D68A-4B9B-ACCE-1369394ED6E5}.Release|x64.Build.0 = Release|x64 + {537EF951-4B02-4103-A310-52A6F7C8D748}.Debug|x64.ActiveCfg = Debug|x64 + {537EF951-4B02-4103-A310-52A6F7C8D748}.Debug|x64.Build.0 = Debug|x64 + {537EF951-4B02-4103-A310-52A6F7C8D748}.Release|x64.ActiveCfg = Release|x64 + {537EF951-4B02-4103-A310-52A6F7C8D748}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/include/caffe/common_layers.hpp b/include/caffe/common_layers.hpp index 89bab8d6f3a..20d203dca7b 100644 --- a/include/caffe/common_layers.hpp +++ b/include/caffe/common_layers.hpp @@ -673,6 +673,30 @@ class TileLayer : public Layer { unsigned int axis_, tiles_, outer_dim_, inner_dim_; }; +template +class MILLayer : public Layer { + public: + explicit MILLayer(const LayerParameter& param) + : Layer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + virtual void Reshape(const vector*>& bottom, + const vector*>& top); + virtual inline const char* type() const { return "MIL"; } + virtual inline int ExactNumBottomBlobs() const { return 1; } + virtual inline int ExactNumTopBlobs() const { return 1; } + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + // virtual Dtype Forward_gpu(const vector*>& bottom, + // vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); + // virtual void Backward_gpu(const vector*>& top, + // const vector& propagate_down, vector*>* bottom); + + int channels_, height_, width_, num_images_; +}; } // namespace caffe #endif // CAFFE_COMMON_LAYERS_HPP_ diff --git a/include/caffe/data_layers.hpp b/include/caffe/data_layers.hpp index 90fd0d19917..08c8c8a36d1 100644 --- a/include/caffe/data_layers.hpp +++ b/include/caffe/data_layers.hpp @@ -339,6 +339,39 @@ class WindowDataLayer : public BasePrefetchingDataLayer { vector > image_database_cache_; }; +/** + * @brief Uses a text file which specifies the image names, and a hdf5 file for the labels. + * Note that each image can have multiple positive labels. + * + * TODO(dox): thorough documentation for Forward and proto params. + */ +template +class MILDataLayer : public BasePrefetchingDataLayer { + public: + explicit MILDataLayer(const LayerParameter& param); + virtual ~MILDataLayer(); + virtual void DataLayerSetUp(const vector*>& bottom, + const vector*>& top) override; + + virtual const char* type() const override; + + virtual int ExactNumBottomBlobs() const override; + + virtual inline int ExactNumTopBlobs() const override; + +protected: + virtual void load_batch(Batch* batch) override; + virtual unsigned int PrefetchRand(); + int num_images_; + unsigned int counter_; + shared_ptr prefetch_rng_; + vector< std::pair > image_database_; + hid_t label_file_id_; + + vector mean_value_; + Blob label_blob_; +}; + } // namespace caffe #endif // CAFFE_DATA_LAYERS_HPP_ diff --git a/include/caffe/loss_layers.hpp b/include/caffe/loss_layers.hpp index 8d41af34e88..26652acaf13 100644 --- a/include/caffe/loss_layers.hpp +++ b/include/caffe/loss_layers.hpp @@ -657,6 +657,23 @@ class SigmoidCrossEntropyLossLayer : public LossLayer { vector*> sigmoid_top_vec_; }; +/* CrossEntropyLossLayer +*/ +template +class CrossEntropyLossLayer : public LossLayer { + public: + explicit CrossEntropyLossLayer(const LayerParameter& param) + : LossLayer(param) {} + virtual void LayerSetUp(const vector*>& bottom, + const vector*>& top); + + protected: + virtual void Forward_cpu(const vector*>& bottom, + const vector*>& top); + virtual void Backward_cpu(const vector*>& top, + const vector& propagate_down, const vector*>& bottom); +}; + // Forward declare SoftmaxLayer for use in SoftmaxWithLossLayer. template class SoftmaxLayer; diff --git a/include/caffe/util/cudnn.hpp b/include/caffe/util/cudnn.hpp index b531dd5fa7a..17a4ef551e6 100644 --- a/include/caffe/util/cudnn.hpp +++ b/include/caffe/util/cudnn.hpp @@ -14,7 +14,7 @@ << cudnnGetErrorString(status); \ } while (0) -inline const char* cudnnGetErrorString(cudnnStatus_t status) { +inline const char* CUDNNWINAPI cudnnGetErrorString(cudnnStatus_t status) { switch (status) { case CUDNN_STATUS_SUCCESS: return "CUDNN_STATUS_SUCCESS"; diff --git a/include/caffe/util/hdf5.hpp b/include/caffe/util/hdf5.hpp index ce568c5eb0d..725fcfdd45a 100644 --- a/include/caffe/util/hdf5.hpp +++ b/include/caffe/util/hdf5.hpp @@ -4,7 +4,6 @@ #include #include "hdf5.h" -#include "hdf5_hl.h" #include "caffe/blob.hpp" diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index 6070b4c7f3a..e2560808473 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -9,6 +9,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/msvc.hpp" namespace caffe { @@ -22,7 +23,11 @@ inline void MakeTempFilename(string* temp_filename) { strcpy(temp_filename_cstr, temp_filename->c_str()); int fd = mkstemp(temp_filename_cstr); CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename; - close(fd); +#ifndef _MSC_VER + close(fd); +#else + _close(fd); +#endif *temp_filename = temp_filename_cstr; delete[] temp_filename_cstr; } @@ -33,7 +38,11 @@ inline void MakeTempDir(string* temp_dirname) { char* temp_dirname_cstr = new char[temp_dirname->size() + 1]; // NOLINT_NEXT_LINE(runtime/printf) strcpy(temp_dirname_cstr, temp_dirname->c_str()); - char* mkdtemp_result = mkdtemp(temp_dirname_cstr); +#ifndef _MSC_VER + char* mkdtemp_result = mkdtemp(temp_dirname_cstr); +#else + errno_t mkdtemp_result = _mktemp_s(temp_dirname_cstr, sizeof(temp_dirname_cstr)); +#endif CHECK(mkdtemp_result != NULL) << "Failed to create a temporary directory at: " << *temp_dirname; *temp_dirname = temp_dirname_cstr; diff --git a/include/caffe/util/msvc.hpp b/include/caffe/util/msvc.hpp new file mode 100644 index 00000000000..7dae60aeb4c --- /dev/null +++ b/include/caffe/util/msvc.hpp @@ -0,0 +1,25 @@ +#pragma once + +#ifdef _MSC_VER + +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include + +#define snprintf _snprintf_s +#define getpid _getpid + +#define mkdir(str, mode) _mkdir(str) + +#define __builtin_popcount __popcnt +#define __builtin_popcountl __popcnt64 + +static const char letters[] = +"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +/* Generate a temporary file name based on TMPL. TMPL must match the +rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed +does not exist at the time of the call to mkstemp. TMPL is +overwritten with the result. */ +int mkstemp(char *tmpl); +#endif // _MSC_VER \ No newline at end of file diff --git a/scripts/GeneratePB.bat b/scripts/GeneratePB.bat new file mode 100644 index 00000000000..c0b32412c0f --- /dev/null +++ b/scripts/GeneratePB.bat @@ -0,0 +1,8 @@ +if exist "../src/caffe/proto/caffe.pb.h" ( + echo caffe.pb.h remains the same as before +) else ( + echo caffe.pb.h is being generated + "../3rdparty/tools/protoc" -I="../src/caffe/proto" --cpp_out="../src/caffe/proto" "../src/caffe/proto/caffe.proto" + copy "..\src\caffe\proto\caffe.pb.h" "..\include\caffe\caffe.pb.h" +) + diff --git a/src/caffe/common.cpp b/src/caffe/common.cpp index 299d67d4bec..4d65062f764 100644 --- a/src/caffe/common.cpp +++ b/src/caffe/common.cpp @@ -3,8 +3,12 @@ #include #include #include +#ifdef _MSC_VER +#include +#endif #include "caffe/common.hpp" +#include "caffe/util/msvc.hpp" #include "caffe/util/rng.hpp" namespace caffe { @@ -46,7 +50,9 @@ void GlobalInit(int* pargc, char*** pargv) { // Google logging. ::google::InitGoogleLogging(*(pargv)[0]); // Provide a backtrace on segfault. +#ifndef _MSC_VER // InstallFailureSignalHandler is not defined windows glog ::google::InstallFailureSignalHandler(); +#endif } #ifdef CPU_ONLY // CPU-only Caffe. diff --git a/src/caffe/layers/base_data_layer.cpp b/src/caffe/layers/base_data_layer.cpp index b90bd4e0caf..2796078cf9f 100644 --- a/src/caffe/layers/base_data_layer.cpp +++ b/src/caffe/layers/base_data_layer.cpp @@ -1,3 +1,5 @@ +#pragma warning ( disable : 4703 ) //REVIEW ktran: make caffe warnings free + #include #include #include diff --git a/src/caffe/layers/bnll_layer.cu b/src/caffe/layers/bnll_layer.cu index d963d0687d2..6c4fa42326e 100644 --- a/src/caffe/layers/bnll_layer.cu +++ b/src/caffe/layers/bnll_layer.cu @@ -6,7 +6,7 @@ namespace caffe { -const float kBNLL_THRESHOLD = 50.; +__device__ __constant__ float kBNLL_THRESHOLD = 50.; template __global__ void BNLLForward(const int n, const Dtype* in, Dtype* out) { diff --git a/src/caffe/layers/contrastive_loss_layer.cpp b/src/caffe/layers/contrastive_loss_layer.cpp index 25e167819d3..db31aec790a 100644 --- a/src/caffe/layers/contrastive_loss_layer.cpp +++ b/src/caffe/layers/contrastive_loss_layer.cpp @@ -53,7 +53,7 @@ void ContrastiveLossLayer::Forward_cpu( if (legacy_version) { loss += std::max(margin - dist_sq_.cpu_data()[i], Dtype(0.0)); } else { - Dtype dist = std::max(margin - sqrt(dist_sq_.cpu_data()[i]), 0.0); + Dtype dist = std::max(margin - sqrt(dist_sq_.cpu_data()[i]), 0.0); loss += dist*dist; } } diff --git a/src/caffe/layers/cross_entropy_loss_layer.cpp b/src/caffe/layers/cross_entropy_loss_layer.cpp new file mode 100644 index 00000000000..37cfe93a639 --- /dev/null +++ b/src/caffe/layers/cross_entropy_loss_layer.cpp @@ -0,0 +1,75 @@ +// Copyright 2014 BVLC and contributors. + +#include +#include +#include +#include + +#include "caffe/layer.hpp" +#include "caffe/vision_layers.hpp" +#include "caffe/util/math_functions.hpp" +#include "caffe/util/io.hpp" + +using std::max; + +namespace caffe { + + template + void CrossEntropyLossLayer::LayerSetUp(const vector*>& bottom, const vector*>& top) {} + + template + void CrossEntropyLossLayer::Forward_cpu(const vector*>& bottom, const vector*>& top) { + const Dtype* bottom_data = bottom[0]->cpu_data(); + const Dtype* bottom_label = bottom[1]->cpu_data(); + int count = bottom[0]->count(); + int num = bottom[0]->num(); + Dtype loss = 0; Dtype l; + for (int i = 0; i < count; ++i) { + if(bottom_label[i] != 0){ + l = -bottom_label[i]*log(max(bottom_data[i],Dtype(0.00001))); + loss += l; + } + + if(bottom_label[i] != 1){ + l = -(1-bottom_label[i])*log(max((1-bottom_data[i]),Dtype(0.00001))); + loss += l; + } + CHECK_GE(l, 0.) << "loss is not >= 0, loss: " << l << " bottom_label: " << bottom_label[i] << " bottom_data: " << bottom_data[i]; + } + top[0]->mutable_cpu_data()[0] = loss / count; + } + +template +void CrossEntropyLossLayer::Backward_cpu( + const vector*>& top, const vector& propagate_down, + const vector*>& bottom) { + + if (propagate_down[1]) { + LOG(FATAL) << this->type() + << " Layer cannot backpropagate to label inputs."; + } + if (propagate_down[0]) { + const Dtype* bottom_data = bottom[0]->cpu_data(); + const Dtype* bottom_label = bottom[1]->cpu_data(); + Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); + int count = bottom[0]->count(); + int num = bottom[0]->num(); + memset(bottom_diff, 0, sizeof(Dtype) * bottom[0]->count()); + for (int i = 0; i < count; ++i) { + Dtype val = 0; + if(bottom_label[i] != 0) + val = val - bottom_label[i]/max(bottom_data[i], Dtype(0.00001))/count; + if(bottom_label[i] != 1) + val = val - (-(1-bottom_label[i])/max(1-bottom_data[i], Dtype(0.00001))/count); + + bottom_diff[i] = val; + } + const Dtype loss_weight = top[0]->cpu_diff()[0]; + caffe_scal(count, loss_weight, bottom_diff); + } +} + +INSTANTIATE_CLASS(CrossEntropyLossLayer); +REGISTER_LAYER_CLASS(CrossEntropyLoss); + +} // namespace caffe diff --git a/src/caffe/layers/mil_data_layer.cpp b/src/caffe/layers/mil_data_layer.cpp new file mode 100644 index 00000000000..b314a10b3a4 --- /dev/null +++ b/src/caffe/layers/mil_data_layer.cpp @@ -0,0 +1,228 @@ +#include + +#include +#include +#include +#include +#include + +#include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" + +#include "caffe/common.hpp" +#include "caffe/data_layers.hpp" +#include "caffe/layer.hpp" +#include "caffe/util/math_functions.hpp" +#include "caffe/util/rng.hpp" +#include "caffe/util/benchmark.hpp" +#include "caffe/util/hdf5.hpp" + +namespace caffe { + // ReSharper disable once CppPossiblyUninitializedMember + template + MILDataLayer::MILDataLayer(const LayerParameter& param): BasePrefetchingDataLayer(param) {} + + template + MILDataLayer::~MILDataLayer() { + this->StopInternalThread(); + } + + template + void MILDataLayer::DataLayerSetUp(const vector*>& bottom, const vector*>& top) { + string label_file = this->layer_param_.mil_data_param().label_file().c_str(); + + LOG(INFO) << "Loading labels from: "<< label_file; + label_file_id_ = H5Fopen(label_file.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); + + LOG(INFO) << "MIL Data layer:" << std::endl; + std::ifstream infile(this->layer_param_.mil_data_param().source().c_str()); + CHECK(infile.good()) << "Failed to open window file " + << this->layer_param_.mil_data_param().source() << std::endl; + + const int channels = this->layer_param_.mil_data_param().channels(); + const int img_size = this->transform_param_.crop_size(); + const int scale = this->transform_param_.scale(); + const int images_per_batch = this->layer_param_.mil_data_param().images_per_batch(); + const int n_classes = this->layer_param_.mil_data_param().n_classes(); + const int num_scales = this->layer_param_.mil_data_param().num_scales(); + const float scale_factor = this->layer_param_.mil_data_param().scale_factor(); + mean_value_.clear(); + std::copy(this->transform_param_.mean_value().begin(), + this->transform_param_.mean_value().end(), + back_inserter(mean_value_)); + + if (mean_value_.size() == 0) + mean_value_ = vector(channels, 128); + + CHECK_EQ(mean_value_.size(), channels); + + LOG(INFO) << "MIL Data Layer: "<< "channels: " << channels; + LOG(INFO) << "MIL Data Layer: "<< "img_size: " << img_size; + LOG(INFO) << "MIL Data Layer: "<< "scale: " << scale; + LOG(INFO) << "MIL Data Layer: "<< "n_classes: " << n_classes; + LOG(INFO) << "MIL Data Layer: "<< "num_scales: " << num_scales; + LOG(INFO) << "MIL Data Layer: "<< "scale_factor: " << scale_factor; + LOG(INFO) << "MIL Data Layer: "<< "images_per_batch: " << images_per_batch; + for(int i = 0; i < mean_value_.size(); i++) + LOG(INFO) << "MIL Data Layer: "<< "mean_value[" << i << "]: " << mean_value_[i]; + + const bool prefetch_needs_rand = this->transform_param_.mirror() || this->transform_param_.crop_size(); + if (prefetch_needs_rand) { + const unsigned int prefetch_rng_seed = caffe_rng_rand(); + prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed)); + } else { + prefetch_rng_.reset(); + } + + string im_name; + int count = 0; + while (infile >> im_name) { + string full_im_name = string(this->layer_param_.mil_data_param().root_dir() + "/" + im_name + "." + this->layer_param_.mil_data_param().ext()); + image_database_.push_back(make_pair(im_name, full_im_name)); + ++count; + } + num_images_ = count; + LOG(INFO) << "Number of images: " << count; + + top[0]->Reshape(images_per_batch*num_scales, channels, img_size, img_size); + top[1]->Reshape(images_per_batch, n_classes, 1, 1); + for (auto& prefetch : this->prefetch_) + { + prefetch.data_.Reshape(top[0]->shape()); + prefetch.label_.Reshape(top[1]->shape()); + } + LOG(INFO) << "output data size: " << top[0]->num() << "," << top[0]->channels() << "," << top[0]->height() << "," << top[0]->width(); + + this->counter_ = 0; + } + + template + const char* MILDataLayer::type() const { + return "MILData"; + } + + template + unsigned int MILDataLayer::PrefetchRand() { + CHECK(prefetch_rng_); + auto prefetch_rng = static_cast(prefetch_rng_->generator()); + return (*prefetch_rng)(); + } + + cv::Mat Transform_IDL(cv::Mat cv_img, int img_size, bool do_mirror) { + cv::Size cv_size; //(width, height) + if (cv_img.cols > cv_img.rows){ + cv::Size tmp(img_size, round(cv_img.rows*img_size / cv_img.cols)); + cv_size = tmp; + } + else{ + cv::Size tmp(round(cv_img.cols*img_size / cv_img.rows), img_size); + cv_size = tmp; + } + + cv::Mat cv_resized_img; + cv::resize(cv_img, cv_resized_img, + cv_size, 0, 0, cv::INTER_LINEAR); + + // horizontal flip at random + if (do_mirror) { + cv::flip(cv_resized_img, cv_resized_img, 1); + } + return cv_resized_img; + } + + template + int MILDataLayer::ExactNumBottomBlobs() const + { return 0; } + + template + int MILDataLayer::ExactNumTopBlobs() const + { return 2; } + + // This function is called on prefetch thread + template + void MILDataLayer::load_batch(Batch* batch) { + CPUTimer timer; + timer.Start(); + CHECK(batch->data_.count()); + + //Dtype* top_data = this->prefetch_data_.mutable_cpu_data(); + //Dtype* top_label = this->prefetch_label_.mutable_cpu_data(); + Dtype* top_data = batch->data_.mutable_cpu_data(); + Dtype* top_label = batch->label_.mutable_cpu_data(); + const int img_size = this->transform_param_.crop_size(); + const int channels = this->layer_param_.mil_data_param().channels(); + const int scale = this->transform_param_.scale(); + const bool mirror = this->transform_param_.mirror(); + + const int images_per_batch = this->layer_param_.mil_data_param().images_per_batch(); + const int n_classes = this->layer_param_.mil_data_param().n_classes(); + const int num_scales = this->layer_param_.mil_data_param().num_scales(); + const float scale_factor = this->layer_param_.mil_data_param().scale_factor(); + + // zero out batch + //caffe_set(this->prefetch_data_.count(), Dtype(0), top_data); + caffe_set(batch->data_.count(), Dtype(0), top_data); + int item_id; + for(int i_image = 0; i_image < images_per_batch; i_image++){ + // Sample which image to read + unsigned int index = counter_; counter_ = counter_ + 1; + const unsigned int rand_index = this->PrefetchRand(); + if(this->layer_param_.mil_data_param().randomize()) + index = rand_index; + + // LOG(INFO) << index % this->num_images_ << ", " << this->num_images_; + pair p = this->image_database_[index % this->num_images_]; + string im_name = p.first; + string full_im_name = p.second; + + cv::Mat cv_img = cv::imread(full_im_name, CV_LOAD_IMAGE_COLOR); + if (!cv_img.data) { + LOG(ERROR) << "Could not open or find file " << full_im_name; + return; + } + + //REVIEW ktran: do not hardcode dataset name (or its prefix "/labels-") + //REVIEW ktran: also do not use deep dataset name so that we don't have to modify the core caffe code + //(ref: https://github.com/BVLC/caffe/commit/a0787631a27ca6478f70341462aafdcf35dabb19) + hdf5_load_nd_dataset(this->label_file_id_, string("/labels-"+im_name).c_str(), 4, 4, &this->label_blob_); + const Dtype* label = label_blob_.mutable_cpu_data(); + + CHECK_EQ(label_blob_.width(), 1) << "Expected width of label to be 1." ; + CHECK_EQ(label_blob_.height(), n_classes) << "Expected height of label to be " << n_classes; + CHECK_EQ(label_blob_.channels(), 1) << "Expected channels of label to be 1." ; + CHECK_EQ(label_blob_.num(), 1) << "Expected num of label to be 1." ; + + float img_size_i = img_size; + for(int i_scales = 0; i_scales < num_scales; i_scales++){ + // Resize such that the image is of size img_size, img_size + item_id = i_image*num_scales + i_scales; + // LOG(INFO) << "MIL Data Layer: scale: " << (int) round(img_size_i); + cv::Mat cv_cropped_img = Transform_IDL(cv_img, static_cast(round(img_size_i)), mirror); + for (int c = 0; c < channels; ++c) { + for (int h = 0; h < cv_cropped_img.rows; ++h) { + for (int w = 0; w < cv_cropped_img.cols; ++w) { + Dtype pixel = + static_cast(cv_cropped_img.at(h, w)[c]); + top_data[((item_id * channels + c) * img_size + h) + * img_size + w] + = (pixel - static_cast(mean_value_[c]))*scale; + } + } + } + img_size_i = std::max(static_cast(1.), img_size_i*scale_factor); + } + + for(int i_label = 0; i_label < n_classes; i_label++){ + top_label[i_image*n_classes + i_label] = + label[i_label]; + } + } + + timer.Stop(); + DLOG(INFO) << "Prefetch batch: " << timer.MilliSeconds() << " ms."; + } + + INSTANTIATE_CLASS(MILDataLayer); + REGISTER_LAYER_CLASS(MILData); +} // namespace caffe diff --git a/src/caffe/layers/mil_layer.cpp b/src/caffe/layers/mil_layer.cpp new file mode 100644 index 00000000000..e8c459333c1 --- /dev/null +++ b/src/caffe/layers/mil_layer.cpp @@ -0,0 +1,117 @@ +// Copyright 2014 BVLC and contributors. + +#include +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/filler.hpp" +#include "caffe/layer.hpp" +#include "caffe/vision_layers.hpp" +#include "caffe/util/math_functions.hpp" + +using std::max; +using std::min; + +namespace caffe { + +template +void MILLayer::LayerSetUp(const vector*>& bottom, + const vector*>& top) { + CHECK_EQ(bottom.size(), 1) << "MIL Layer takes a single blob as input."; + CHECK_EQ(top.size(), 1) << "MIL Layer takes a single blob as output."; +} +template +void MILLayer::Reshape(const vector*>& bottom, + const vector*>& top){ + top[0]->Reshape(bottom[0]->num(), bottom[0]->channels(), 1, 1); +} + +template +void MILLayer::Forward_cpu(const vector*>& bottom, + const vector*>& top) { + const Dtype* bottom_data = bottom[0]->cpu_data(); + Dtype* top_data = top[0]->mutable_cpu_data(); + int offset; + channels_ = bottom[0]->channels(); + num_images_ = bottom[0]->num(); + height_ = bottom[0]->height(); + width_ = bottom[0]->width(); + + for(int j = 0; j < channels_; j++){ + for(int i = 0; i < num_images_; i++){ + Dtype prob, max_prob; + + switch (this->layer_param_.mil_param().type()) { + case MILParameter_MILType_MAX: + prob = -FLT_MAX; + offset = bottom[0]->offset(i, j); + for(int k = 0; k < height_; k++){ + for(int l = 0; l < width_; l++) { + prob = max(prob, bottom_data[offset]); + offset = offset + 1; + } + } + top_data[i*channels_ + j] = prob; + break; + + case MILParameter_MILType_NOR: + prob = 1.; max_prob = -FLT_MAX; + offset = bottom[0]->offset(i, j); + for(int k = 0; k < height_; k++) { + for(int l = 0; l < width_; l++) { + assert(0 <= bottom_data[offset] && bottom_data[offset] <= 1); + prob = prob*(1. - bottom_data[offset]); + max_prob = max(max_prob, bottom_data[offset]); + offset = offset + 1; + } + } + top_data[i*channels_ + j] = max(Dtype(1.) - prob, max_prob); + assert(0 <= top_data[i*channels_ + j] && top_data[i*channels_ + j] <= 1); + break; + } + } + } +} + +template +void MILLayer::Backward_cpu(const vector*>& top, + const vector& propagate_down, + const vector*>& bottom) { + + const Dtype* top_diff = top[0]->cpu_diff(); + const Dtype* top_data = top[0]->cpu_data(); + const Dtype* bottom_data = bottom[0]->cpu_data(); + Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); + int offset; + + if(propagate_down[0]){ + for(int j = 0; j < channels_; j++){ + for(int i = 0; i < num_images_; i++){ + offset = bottom[0]->offset(i, j); + + for(int k = 0; k < height_; k++){ + for(int l = 0; l < width_; l++){ + switch (this->layer_param_.mil_param().type()) { + case MILParameter_MILType_MAX: + bottom_diff[offset] = + top_diff[i*channels_ + j] * (top_data[i*channels_ + j] == bottom_data[offset]); + break; + case MILParameter_MILType_NOR: + bottom_diff[offset] = top_diff[i*channels_ + j] * + min(Dtype(1.),((1-top_data[i*channels_ + j])/(1-bottom_data[offset]))); + break; + } + offset = offset + 1; + } + } + + } + } + } +} + +INSTANTIATE_CLASS(MILLayer); +REGISTER_LAYER_CLASS(MIL); +} // namespace caffe diff --git a/src/caffe/parallel.cpp b/src/caffe/parallel.cpp index a6d154e168e..e3da6df40e2 100644 --- a/src/caffe/parallel.cpp +++ b/src/caffe/parallel.cpp @@ -3,8 +3,6 @@ #endif #include #include -#include -#include #include #include diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index f52c941b05e..35cd647f47e 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -385,6 +385,8 @@ message LayerParameter { optional ThresholdParameter threshold_param = 128; optional TileParameter tile_param = 138; optional WindowDataParameter window_data_param = 129; + optional MILDataParameter mil_data_param = 0x004d4944; //"MID" + optional MILParameter mil_param = 0x004d494c; //"MIL" } // Message that stores parameters used to apply transformation @@ -958,6 +960,16 @@ message ThresholdParameter { optional float threshold = 1 [default = 0]; // Strictly positive values } +// Message that stores parameters used by MILLayer +message MILParameter { + enum MILType { + MAX = 0; + NOR = 1; + } + optional MILType type = 1 [default = MAX]; // The MIL method +} + + message WindowDataParameter { // Specify the data source. optional string source = 1; @@ -991,6 +1003,37 @@ message WindowDataParameter { optional string root_folder = 13 [default = ""]; } +message MILDataParameter { + // Specify the data source. + optional string source = 1; + + // Number of scales for each image + optional uint32 num_scales = 2 [default = 1]; + + // Side length ratio between neighbouring scales + optional float scale_factor = 6 [default = 1]; + + // Number of channels in the image + optional uint32 channels = 4 [default = 3]; + + // Specify the number of images per batch + optional uint32 images_per_batch = 3; + // Specify the number of classes + optional uint32 n_classes = 5; + // specify the box_dir and label_dir + optional string label_file = 7; + + // Root directory which contains all the images + optional string root_dir = 11; + // Extention for the file + optional string ext = 12; + + // To randomize or not + optional bool randomize = 13 [default = true]; +} + + + message SPPParameter { enum PoolMethod { MAX = 0; diff --git a/src/caffe/util/db.cpp b/src/caffe/util/db.cpp index ccda054d881..100b592098d 100644 --- a/src/caffe/util/db.cpp +++ b/src/caffe/util/db.cpp @@ -18,6 +18,7 @@ DB* GetDB(DataParameter::DB backend) { #endif // USE_LMDB default: LOG(FATAL) << "Unknown database backend"; + return nullptr; } } @@ -33,6 +34,7 @@ DB* GetDB(const string& backend) { } #endif // USE_LMDB LOG(FATAL) << "Unknown database backend"; + return nullptr; } } // namespace db diff --git a/src/caffe/util/db_lmdb.cpp b/src/caffe/util/db_lmdb.cpp index 78dd880ac41..24effb31eef 100644 --- a/src/caffe/util/db_lmdb.cpp +++ b/src/caffe/util/db_lmdb.cpp @@ -1,8 +1,11 @@ #ifdef USE_LMDB #include "caffe/util/db_lmdb.hpp" +#ifdef _MSC_VER +#include +#define mkdir(str, mode) _mkdir(str) +#endif #include - #include namespace caffe { namespace db { diff --git a/src/caffe/util/hdf5.cpp b/src/caffe/util/hdf5.cpp index 7730e76ab87..eec6524bb34 100644 --- a/src/caffe/util/hdf5.cpp +++ b/src/caffe/util/hdf5.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace caffe { @@ -10,9 +11,9 @@ template void hdf5_load_nd_dataset_helper( hid_t file_id, const char* dataset_name_, int min_dim, int max_dim, Blob* blob) { + // REVIEW ktran: fix the logic in mil_data_layer and uncomment this check // Verify that the dataset exists. - CHECK(H5LTfind_dataset(file_id, dataset_name_)) - << "Failed to find HDF5 dataset " << dataset_name_; + ////CHECK(H5LTfind_dataset(file_id, dataset_name_)) << "Failed to find HDF5 dataset " << dataset_name_; // Verify that the number of dimensions is in the accepted range. herr_t status; int ndims; @@ -29,11 +30,15 @@ void hdf5_load_nd_dataset_helper( CHECK_GE(status, 0) << "Failed to get dataset info for " << dataset_name_; switch (class_) { case H5T_FLOAT: + { LOG_FIRST_N(INFO, 1) << "Datatype class: H5T_FLOAT"; break; + } case H5T_INTEGER: + { LOG_FIRST_N(INFO, 1) << "Datatype class: H5T_INTEGER"; break; + } case H5T_TIME: LOG(FATAL) << "Unsupported datatype class: H5T_TIME"; case H5T_STRING: diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index f2b1dd98423..34f0cb04f09 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -2,23 +2,23 @@ #include #include #include -#include #ifdef USE_OPENCV #include #include #include #endif // USE_OPENCV -#include -#include #include // NOLINT(readability/streams) #include -#include #include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" #include "caffe/util/io.hpp" +#ifdef _MSC_VER +#define open _open +#endif + const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte. namespace caffe { @@ -37,7 +37,12 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) { FileInputStream* input = new FileInputStream(fd); bool success = google::protobuf::TextFormat::Parse(input, proto); delete input; +#ifdef _MSC_VER + _close(fd); +#else close(fd); +#endif + return success; } @@ -46,11 +51,22 @@ void WriteProtoToTextFile(const Message& proto, const char* filename) { FileOutputStream* output = new FileOutputStream(fd); CHECK(google::protobuf::TextFormat::Print(proto, output)); delete output; +#ifdef _MSC_VER + _close(fd); +#else close(fd); +#endif } bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { - int fd = open(filename, O_RDONLY); +#ifndef _MSC_VER + int flags = O_RDONLY; +#else + //When loading a binary file in Windows, you have to specify that it’s binary + int flags = O_RDONLY | O_BINARY; +#endif + int fd = open(filename, flags); + CHECK_NE(fd, -1) << "File not found: " << filename; ZeroCopyInputStream* raw_input = new FileInputStream(fd); CodedInputStream* coded_input = new CodedInputStream(raw_input); @@ -60,7 +76,12 @@ bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { delete coded_input; delete raw_input; +#ifdef _MSC_VER + _close(fd); +#else close(fd); +#endif + return success; } diff --git a/src/caffe/util/math_functions.cpp b/src/caffe/util/math_functions.cpp index 0aab6b17b85..b3601f43548 100644 --- a/src/caffe/util/math_functions.cpp +++ b/src/caffe/util/math_functions.cpp @@ -6,6 +6,7 @@ #include "caffe/common.hpp" #include "caffe/util/math_functions.hpp" #include "caffe/util/rng.hpp" +#include "caffe/util/msvc.hpp" namespace caffe { diff --git a/src/caffe/util/signal_handler.cpp b/src/caffe/util/signal_handler.cpp index 5d764ec524f..46fb20f7829 100644 --- a/src/caffe/util/signal_handler.cpp +++ b/src/caffe/util/signal_handler.cpp @@ -13,9 +13,11 @@ namespace { void handle_signal(int signal) { switch (signal) { +#ifndef _MSC_VER case SIGHUP: got_sighup = true; break; +#endif case SIGINT: got_sigint = true; break; @@ -28,6 +30,8 @@ namespace { } already_hooked_up = true; + //REVIEW ktran: make signal handler work in Windows +#ifndef _MSC_VER struct sigaction sa; // Setup the handler sa.sa_handler = &handle_signal; @@ -42,11 +46,14 @@ namespace { if (sigaction(SIGINT, &sa, NULL) == -1) { LOG(FATAL) << "Cannot install SIGINT handler."; } +#endif } // Set the signal handlers to the default. void UnhookHandler() { if (already_hooked_up) { + //REVIEW ktran: make signal handler work in Windows +#ifndef _MSC_VER struct sigaction sa; // Setup the sighub handler sa.sa_handler = SIG_DFL; @@ -63,6 +70,7 @@ namespace { } already_hooked_up = false; +#endif } } diff --git a/src/caffe/util/upgrade_proto.cpp b/src/caffe/util/upgrade_proto.cpp index ac379e50f4f..f5f2e979f26 100644 --- a/src/caffe/util/upgrade_proto.cpp +++ b/src/caffe/util/upgrade_proto.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include #include