Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake based windows port #62

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/SimpleOpenCV/deepbeliefopencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
#include "deepbeliefopencv.h"

#include <cctype>
#include <cstdint>
#include <iostream>
#include <iterator>

#include <stdio.h>
#include <assert.h>

#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/highgui/highgui_c.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui_c.h>

#include "libjpcnn.h"

Expand Down
2 changes: 1 addition & 1 deletion examples/SimpleOpenCV/deepbeliefopencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <string>

#include "opencv2/core/utility.hpp"
#include "opencv2/core/core.hpp"

namespace DeepBelief {

Expand Down
4 changes: 2 additions & 2 deletions examples/SimpleOpenCV/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "opencv2/highgui.hpp"
#include "opencv2/core/utility.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

#include <string>

Expand Down
109 changes: 109 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
project(DeepBeliefSDK)
cmake_minimum_required(VERSION 2.8)

if ( WIN32 )
add_definitions(-DUSE_EIGEN_GEMM=1 -D_WIN32_WINNT=0x0501 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
# for <vs2015:
add_definitions(-Dsnprintf=_snprintf -Dstrcasecmp=lstrcmpiA)
include_directories(${EIGEN3_INCLUDE_DIR})
else ()
add_definitions(-Ofast -g)
endif()
message(STATUS "Current list dir: ${CMAKE_CURRENT_LIST_DIR}")

include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/include")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/graph")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/math")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/third_party")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/svm")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/utility")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/opengl")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib/windows")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/include")
include_directories("${CMAKE_CURRENT_LIST_DIR}/src/lib")

option(USE_GEMM "Use GEMM BLAS" OFF)

set(LIBSRCS
${CMAKE_CURRENT_LIST_DIR}/src/lib/libjpcnn.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/basenode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/buffer.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/convnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/dropoutnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/flatnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/gconvnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/graph.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/maxnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/neuronnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/nodefactory.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/normalizenode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/poolnode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/prepareinput.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/graph/relunode.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_add.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_channels.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_correlate.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_dot.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_gemm.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_local_response.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_margin.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_max.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_scale.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/math/matrix_softmax.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/opengl/glbuffer.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/opengl/glcontext.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/opengl/glgemm.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/opengl/glprogram.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pi/mailbox.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/pi/qpu_gemm.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/svm/svm.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/svm/svmutils.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/third_party/stb_image.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/utility/binary_format.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/utility/cstring_helpers.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/utility/os_image_load.cpp
${CMAKE_CURRENT_LIST_DIR}/src/lib/utility/os_image_save.cpp
)

if ( WIN32 )
set(LIBSRCS ${LIBSRCS}
${CMAKE_CURRENT_LIST_DIR}/src/lib/windows/mmap.cpp
)
endif ()

#message(STATUS "lib sources: ${LIBSRCS})
add_library(libjpcnn ${LIBSRCS})

set(TOOLSRCS
${CMAKE_CURRENT_LIST_DIR}/src/tool/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/tool/timer.cpp
)
if ( WIN32 )
set(TOOLSRCS ${TOOLSRCS}
${CMAKE_CURRENT_LIST_DIR}/src/lib/windows/getopt.cpp
)
endif ()

add_executable(jpcnn ${TOOLSRCS})
target_link_libraries(jpcnn libjpcnn)

set(simple_example_src
${CMAKE_CURRENT_LIST_DIR}/../examples/SimpleLinux/main.c
)
add_executable(simple_example ${simple_example_src})
target_link_libraries(simple_example libjpcnn)

if ( OpenCV_DIR )
find_package( OpenCV REQUIRED ${OPENCV_REQUIRED_MODULES})
endif ()

if ( OpenCV_FOUND )
include_directories(${OpenCV_INCLUDE_DIRS})
set(opencv_example_src
${CMAKE_CURRENT_LIST_DIR}/../examples/SimpleOpenCV/main.cpp
${CMAKE_CURRENT_LIST_DIR}/../examples/SimpleOpenCV/deepbeliefopencv.cpp
${CMAKE_CURRENT_LIST_DIR}/../examples/SimpleOpenCV/deepbeliefopencv.h
)
add_executable(opencv_example ${opencv_example_src})
target_link_libraries(opencv_example libjpcnn ${OpenCV_LIBS})
endif ()
2 changes: 1 addition & 1 deletion source/src/lib/graph/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
//#include <sys/time.h>

#include "buffer.h"
#include "binary_format.h"
Expand Down
8 changes: 4 additions & 4 deletions source/src/lib/libjpcnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "libjpcnn.h"

#include <stdio.h>
#include <sys/time.h>
//#include <sys/time.h>

#include "buffer.h"
#include "prepareinput.h"
Expand Down Expand Up @@ -111,8 +111,8 @@ void* jpcnn_create_image_buffer_from_uint8_data(unsigned char* pixelData, int wi

void jpcnn_classify_image(void* networkHandle, void* inputHandle, unsigned int flags, int layerOffset, float** outPredictionsValues, int* outPredictionsLength, char*** outPredictionsNames, int* outPredictionsNamesLength) {

const bool doMultiSample = (flags & JPCNN_MULTISAMPLE);
const bool doRandomSample = (flags & JPCNN_RANDOM_SAMPLE);
const bool doMultiSample = (flags & JPCNN_MULTISAMPLE) != 0;
const bool doRandomSample = (flags & JPCNN_RANDOM_SAMPLE) != 0;

Graph* graph = (Graph*)(networkHandle);
Buffer* input = (Buffer*)(inputHandle);
Expand Down Expand Up @@ -231,7 +231,7 @@ float jpcnn_predict(void* predictorHandle, float* predictions, int predictionsLe
svm_predict_probability(model, nodes, probabilityEstimates);
const double predictionValue = probabilityEstimates[0];
destroy_node_list(nodes);
return predictionValue;
return (float)predictionValue;
}


Expand Down
5 changes: 3 additions & 2 deletions source/src/lib/math/matrix_max.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <assert.h>
#include <math.h>
#include <float.h>
#include <algorithm>

#include "buffer.h"

Expand Down Expand Up @@ -72,9 +73,9 @@ Buffer* matrix_max_patch(Buffer* input, int patchWidth, int stride) {
for (int outputChannel = 0; outputChannel < outputChannels; outputChannel += 1) {
jpfloat_t patchMax = -FLT_MAX;
for (int patchY = 0; patchY < patchWidth; patchY += 1) {
const int inputY = (int)fmin((inputHeight - 1), (inputOriginY + patchY));
const int inputY = (int)std::min((inputHeight - 1), (inputOriginY + patchY));
for (int patchX = 0; patchX < patchWidth; patchX += 1) {
const int inputX = (int)fmin((inputWidth - 1), (inputOriginX + patchX));
const int inputX = (int)std::min((inputWidth - 1), (inputOriginX + patchX));
const int inputOffset = inputDims.offset(imageIndex, inputY, inputX, outputChannel);
const jpfloat_t inputValue = *(input->_data + inputOffset);
patchMax = fmaxf(patchMax, inputValue);
Expand Down
17 changes: 14 additions & 3 deletions source/src/lib/utility/binary_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifndef _WIN32
#include <sys/mman.h>
#endif

#include <sys/stat.h>
#include <fcntl.h>

Expand All @@ -22,14 +26,18 @@ SBinaryTag* read_tag_from_file(const char* filename, bool useMemoryMap) {
return NULL;
}


SBinaryTag* result;
#ifndef _WIN32
if (useMemoryMap) {
const int fileHandle = open(filename, O_RDONLY);
struct stat statBuffer;
fstat(fileHandle, &statBuffer);
const size_t bytesInFile = (size_t)(statBuffer.st_size);
result = (SBinaryTag*)(mmap(NULL, bytesInFile, PROT_READ, MAP_SHARED, fileHandle, 0));
} else {
} else
#endif
{
FILE* file = fopen(filename, "rb");
if (file == NULL) {
fprintf(stderr, "read_tag_from_file() - couldn't open '%s'\n", filename);
Expand All @@ -53,12 +61,15 @@ SBinaryTag* read_tag_from_file(const char* filename, bool useMemoryMap) {
}

void deallocate_file_tag(SBinaryTag* fileTag, bool useMemoryMap) {
if (useMemoryMap) {
#ifndef _WIN32
if ( useMemoryMap ) {
// This assumes that there's only a single root tag in a binary file, so we can
// calculate the file length based on the tag length.
const size_t tagTotalBytes = get_total_sizeof_tag(fileTag);
munmap(fileTag, tagTotalBytes);
} else {
} else
#endif
{
free(fileTag);
}
}
Expand Down
Loading