Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
af88b0d5
Commit
af88b0d5
authored
Aug 10, 2017
by
Davis King
Browse files
Options
Browse Files
Download
Plain Diff
merged
parents
d9f93548
d62c5a7c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
170 additions
and
39 deletions
+170
-39
cmake_swig_jni
dlib/java/cmake_swig_jni
+19
-1
java_array.h
dlib/java/java_array.h
+0
-0
jvector.h
dlib/java/jvector.h
+0
-0
swig_api.h
dlib/java/swig_api.h
+58
-33
swig_test.java
dlib/java/swig_test.java
+15
-0
matrix_blas_bindings.h
dlib/matrix/matrix_blas_bindings.h
+2
-2
matrix_mat.h
dlib/matrix/matrix_mat.h
+31
-2
matrix_mat_abstract.h
dlib/matrix/matrix_mat_abstract.h
+29
-0
dnn.cpp
dlib/test/dnn.cpp
+1
-1
matrix.cpp
dlib/test/matrix.cpp
+15
-0
No files found.
dlib/java/cmake_swig_jni
View file @
af88b0d5
...
@@ -119,8 +119,26 @@ FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/swig.i
...
@@ -119,8 +119,26 @@ FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/swig.i
%module global
%module global
%{
%{
#include \"swig_api.h\"
#include <exception>
#include <exception>
#include <stdexcept>
static JavaVM *cached_jvm = 0;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
cached_jvm = jvm;
return JNI_VERSION_1_6;
}
static JNIEnv * JNI_GetEnv() {
JNIEnv *env;
jint rc = cached_jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
if (rc == JNI_EDETACHED)
throw std::runtime_error(\"current thread not attached\");
if (rc == JNI_EVERSION)
throw std::runtime_error(\"jni version not supported\");
return env;
}
#include \"swig_api.h\"
%}
%}
// Convert all C++ exceptions into java.lang.Exception
// Convert all C++ exceptions into java.lang.Exception
...
...
dlib/java/java_array.h
0 → 100644
View file @
af88b0d5
This diff is collapsed.
Click to expand it.
dlib/java/jvector.h
deleted
100644 → 0
View file @
d9f93548
This diff is collapsed.
Click to expand it.
dlib/java/swig_api.h
View file @
af88b0d5
#ifndef EXAMPLE_SWIG_ApI_H_
#ifndef EXAMPLE_SWIG_ApI_H_
#define EXAMPLE_SWIG_ApI_H_
#define EXAMPLE_SWIG_ApI_H_
// This file is essentially a small unit test for the swig cmake scripts and the j
vector
// This file is essentially a small unit test for the swig cmake scripts and the j
ava array
// classes. All it does it define a few simple functions for writing to and summing
// classes. All it does it define a few simple functions for writing to and summing
// arrays. The swig_test.java file then calls these C++ functions and checks if they work
// arrays. The swig_test.java file then calls these C++ functions and checks if they work
// correctly.
// correctly.
// Let's use the jvector, a tool for efficiently binding java native arrays to C++ function
// Let's use java_array.h, a tool for efficiently binding java native arrays to C++
// arguments. You do this by putting this pair of include statements in your swig_api.h
// function arguments. You do this by putting this pair of include statements in your
// file. Then after that you can use the jvector and jvector_crit classes.
// swig_api.h file. Then after that you can use the java::array, java::array_view, and
#include "jvector.h"
// java::array_view_crit classes.
#include "java_array.h"
#ifdef SWIG
#ifdef SWIG
%
include
"j
vector
.h"
%
include
"j
ava_array
.h"
#endif
#endif
// ----------------------------------------------------------------------------------------
using
namespace
java
;
// SWIG can't expose templated functions to java. We declare these here as helper
// SWIG can't expose templated functions to java. We declare these here as helper
// functions to make the non-templated routines swig will expose easier to write. You can
// functions to make the non-templated routines swig will expose easier to write. You can
// see these java exposed methods below (i.e. sum(), sum_crit(), assign(), and
// see these java exposed methods below (i.e. sum(), sum_crit(), assign(), and
// assign_crit()).
// assign_crit()).
template
<
typename
T
>
template
<
typename
T
>
T
tsum
(
const
jvector
_crit
<
T
>&
arr
)
T
tsum
(
const
array_view
_crit
<
T
>&
arr
)
{
{
T
s
=
0
;
T
s
=
0
;
for
(
auto
&
v
:
arr
)
for
(
auto
&
v
:
arr
)
...
@@ -32,7 +34,7 @@ T tsum(const jvector_crit<T>& arr)
...
@@ -32,7 +34,7 @@ T tsum(const jvector_crit<T>& arr)
return
s
;
return
s
;
}
}
template
<
typename
T
>
template
<
typename
T
>
T
tsum
(
const
jvector
<
T
>&
arr
)
T
tsum
(
const
array_view
<
T
>&
arr
)
{
{
T
s
=
0
;
T
s
=
0
;
for
(
auto
&
v
:
arr
)
for
(
auto
&
v
:
arr
)
...
@@ -56,41 +58,64 @@ void tassign(T& arr)
...
@@ -56,41 +58,64 @@ void tassign(T& arr)
// "global", which is where these sum and assign routines will appear. You can see
// "global", which is where these sum and assign routines will appear. You can see
// examples of java code that calls them in swig_test.java.
// examples of java code that calls them in swig_test.java.
inline
int
sum_crit
(
const
jvector_crit
<
int16_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum_crit
(
const
array_view_crit
<
int16_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
jvector
<
int16_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
array_view
<
int16_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
jvector_crit
<
int16_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign_crit
(
array_view_crit
<
int16_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
jvector
<
int16_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array_view
<
int16_t
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
array_view_crit
<
int32_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
array_view
<
int32_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
array_view_crit
<
int32_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array_view
<
int32_t
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
array_view_crit
<
int64_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
array_view
<
int64_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
array_view_crit
<
int64_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array_view
<
int64_t
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
jvector_crit
<
int32_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
jvector
<
int32_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
jvector_crit
<
int32_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
jvector
<
int32_t
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
array_view_crit
<
char
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
array_view
<
char
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
array_view_crit
<
char
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array_view
<
char
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
jvector_crit
<
int64_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
jvector
<
int64_t
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
jvector_crit
<
int64_t
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
jvector
<
int64_t
>&
arr
)
{
tassign
(
arr
);
}
inline
int
sum_crit
(
const
jvector_crit
<
char
>&
arr
)
{
return
tsum
(
arr
);
}
inline
double
sum_crit
(
const
array_view_crit
<
double
>&
arr
)
{
return
tsum
(
arr
);
}
inline
int
sum
(
const
jvector
<
char
>&
arr
)
{
return
tsum
(
arr
);
}
inline
double
sum
(
const
array_view
<
double
>&
arr
)
{
return
tsum
(
arr
);
}
inline
void
assign_crit
(
jvector_crit
<
char
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign_crit
(
array_view_crit
<
double
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
jvector
<
char
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array_view
<
double
>&
arr
)
{
tassign
(
arr
);
}
inline
float
sum_crit
(
array
<
float
>
arr
)
{
array_view_crit
<
float
>
a
(
arr
);
return
tsum
(
a
);
}
inline
float
sum
(
const
array
<
float
>&
arr
)
{
array_view
<
float
>
a
(
arr
);
return
tsum
(
a
);
}
inline
void
assign_crit
(
array_view_crit
<
float
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
array
<
float
>&
arr
)
{
array_view
<
float
>
a
(
arr
);
tassign
(
a
);
}
inline
double
sum_crit
(
const
jvector_crit
<
double
>&
arr
)
{
return
tsum
(
arr
);
}
array
<
int32_t
>
make_an_array
(
size_t
s
)
inline
double
sum
(
const
jvector
<
double
>&
arr
)
{
return
tsum
(
arr
);
}
{
inline
void
assign_crit
(
jvector_crit
<
double
>&
arr
)
{
tassign
(
arr
);
}
array
<
int32_t
>
arr
(
s
);
inline
void
assign
(
jvector
<
double
>&
arr
)
{
tassign
(
arr
);
}
array_view_crit
<
int32_t
>
a
(
arr
);
for
(
size_t
i
=
0
;
i
<
a
.
size
();
++
i
)
a
[
i
]
=
i
;
inline
float
sum_crit
(
const
jvector_crit
<
float
>&
arr
)
{
return
tsum
(
arr
);
}
return
arr
;
inline
float
sum
(
const
jvector
<
float
>&
arr
)
{
return
tsum
(
arr
);
}
}
inline
void
assign_crit
(
jvector_crit
<
float
>&
arr
)
{
tassign
(
arr
);
}
inline
void
assign
(
jvector
<
float
>&
arr
)
{
tassign
(
arr
);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
...
...
dlib/java/swig_test.java
View file @
af88b0d5
...
@@ -69,6 +69,14 @@ public class swig_test
...
@@ -69,6 +69,14 @@ public class swig_test
}
}
}
}
public
static
void
assertIsEqual
(
int
val1
,
int
val2
)
{
if
(
val1
!=
val2
)
{
throw
new
RuntimeException
(
"Test failed "
+
val1
+
" should be equal to "
+
val2
);
}
}
public
static
double
sum
(
double
[]
arr
)
public
static
double
sum
(
double
[]
arr
)
{
{
double
s
=
0
;
double
s
=
0
;
...
@@ -233,6 +241,13 @@ public class swig_test
...
@@ -233,6 +241,13 @@ public class swig_test
assertIs28
(
global
.
sum_crit
(
arr
));
assertIs28
(
global
.
sum_crit
(
arr
));
}
}
}
}
{
int
[]
a
=
global
.
make_an_array
(
4
);
for
(
int
i
=
0
;
i
<
a
.
length
;
++
i
)
{
assertIsEqual
(
a
[
i
],
i
);
}
}
System
.
out
.
println
(
"\n\n ALL TESTS COMPLETED SUCCESSFULLY\n"
);
System
.
out
.
println
(
"\n\n ALL TESTS COMPLETED SUCCESSFULLY\n"
);
}
}
...
...
dlib/matrix/matrix_blas_bindings.h
View file @
af88b0d5
...
@@ -424,7 +424,7 @@ namespace dlib
...
@@ -424,7 +424,7 @@ namespace dlib
template
<
typename
T
>
template
<
typename
T
>
int
get_ld
(
const
matrix_op
<
op_pointer_to_col_vect
<
T
>
>&
m
)
{
return
m
.
nc
();
}
int
get_ld
(
const
matrix_op
<
op_pointer_to_col_vect
<
T
>
>&
m
)
{
return
m
.
nc
();
}
template
<
typename
T
>
template
<
typename
T
>
int
get_ld
(
const
matrix_op
<
op_pointer_to_mat
<
T
>
>&
m
)
{
return
m
.
nc
()
;
}
int
get_ld
(
const
matrix_op
<
op_pointer_to_mat
<
T
>
>&
m
)
{
return
m
.
op
.
stride
;
}
// --------
// --------
...
@@ -443,7 +443,7 @@ namespace dlib
...
@@ -443,7 +443,7 @@ namespace dlib
template
<
typename
T
>
template
<
typename
T
>
int
get_inc
(
const
matrix_op
<
op_pointer_to_col_vect
<
T
>
>&
)
{
return
1
;
}
int
get_inc
(
const
matrix_op
<
op_pointer_to_col_vect
<
T
>
>&
)
{
return
1
;
}
template
<
typename
T
>
template
<
typename
T
>
int
get_inc
(
const
matrix_op
<
op_pointer_to_mat
<
T
>
>&
)
{
return
1
;
}
int
get_inc
(
const
matrix_op
<
op_pointer_to_mat
<
T
>
>&
m
)
{
return
m
.
op
.
stride
==
m
.
op
.
cols
?
1
:
0
;
}
template
<
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
template
<
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
int
get_inc
(
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
)
{
return
1
;
}
int
get_inc
(
const
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
)
{
return
1
;
}
...
...
dlib/matrix/matrix_mat.h
View file @
af88b0d5
...
@@ -319,11 +319,19 @@ namespace dlib
...
@@ -319,11 +319,19 @@ namespace dlib
const
T
*
ptr_
,
const
T
*
ptr_
,
const
long
nr_
,
const
long
nr_
,
const
long
nc_
const
long
nc_
)
:
ptr
(
ptr_
),
rows
(
nr_
),
cols
(
nc_
){}
)
:
ptr
(
ptr_
),
rows
(
nr_
),
cols
(
nc_
),
stride
(
nc_
){}
op_pointer_to_mat
(
const
T
*
ptr_
,
const
long
nr_
,
const
long
nc_
,
const
long
stride_
)
:
ptr
(
ptr_
),
rows
(
nr_
),
cols
(
nc_
),
stride
(
stride_
){}
const
T
*
ptr
;
const
T
*
ptr
;
const
long
rows
;
const
long
rows
;
const
long
cols
;
const
long
cols
;
const
long
stride
;
const
static
long
cost
=
1
;
const
static
long
cost
=
1
;
const
static
long
NR
=
0
;
const
static
long
NR
=
0
;
...
@@ -333,7 +341,7 @@ namespace dlib
...
@@ -333,7 +341,7 @@ namespace dlib
typedef
default_memory_manager
mem_manager_type
;
typedef
default_memory_manager
mem_manager_type
;
typedef
row_major_layout
layout_type
;
typedef
row_major_layout
layout_type
;
const_ret_type
apply
(
long
r
,
long
c
)
const
{
return
ptr
[
r
*
cols
+
c
];
}
const_ret_type
apply
(
long
r
,
long
c
)
const
{
return
ptr
[
r
*
stride
+
c
];
}
long
nr
()
const
{
return
rows
;
}
long
nr
()
const
{
return
rows
;
}
long
nc
()
const
{
return
cols
;
}
long
nc
()
const
{
return
cols
;
}
...
@@ -419,6 +427,27 @@ namespace dlib
...
@@ -419,6 +427,27 @@ namespace dlib
return
matrix_op
<
op
>
(
op
(
ptr
,
nr
,
nc
));
return
matrix_op
<
op
>
(
op
(
ptr
,
nr
,
nc
));
}
}
template
<
typename
T
>
const
matrix_op
<
op_pointer_to_mat
<
T
>
>
mat
(
const
T
*
ptr
,
long
nr
,
long
nc
,
long
stride
)
{
DLIB_ASSERT
(
nr
>=
0
&&
nc
>=
0
&&
stride
>
0
,
"
\t
const matrix_exp mat(ptr, nr, nc, stride)"
<<
"
\n\t
nr and nc must be >= 0 while stride > 0"
<<
"
\n\t
nr: "
<<
nr
<<
"
\n\t
nc: "
<<
nc
<<
"
\n\t
stride: "
<<
stride
);
typedef
op_pointer_to_mat
<
T
>
op
;
return
matrix_op
<
op
>
(
op
(
ptr
,
nr
,
nc
,
stride
));
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
dlib/matrix/matrix_mat_abstract.h
View file @
af88b0d5
...
@@ -153,6 +153,35 @@ namespace dlib
...
@@ -153,6 +153,35 @@ namespace dlib
the pointer and thus will not delete or free it.
the pointer and thus will not delete or free it.
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
const
matrix_exp
mat
(
const
T
*
ptr
,
long
nr
,
long
nc
,
long
stride
);
/*!
requires
- nr >= 0
- nc >= 0
- stride > 0
- ptr == a pointer to at least (nr-1)*stride+nc T objects (or the NULL pointer if nr*nc==0)
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == nc
- for all valid r and c:
M(r,c) == ptr[r*stride + c]
(i.e. the pointer is interpreted as a matrix laid out in memory
in row major order, with a row stride of the given stride amount.)
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/test/dnn.cpp
View file @
af88b0d5
...
@@ -870,7 +870,7 @@ namespace
...
@@ -870,7 +870,7 @@ namespace
conv2
.
get_gradient_for_filters
(
true
,
gi
,
data
,
filter_gradient2
);
conv2
.
get_gradient_for_filters
(
true
,
gi
,
data
,
filter_gradient2
);
dlog
<<
LINFO
<<
"filter gradient error: "
<<
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
)));
dlog
<<
LINFO
<<
"filter gradient error: "
<<
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
)));
DLIB_TEST_MSG
(
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
)))
<
1
e-3
,
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
))));
DLIB_TEST_MSG
(
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
)))
<
2
e-3
,
max
(
abs
(
mat
(
filter_gradient1
)
-
mat
(
filter_gradient2
))));
}
}
}
}
...
...
dlib/test/matrix.cpp
View file @
af88b0d5
...
@@ -1350,6 +1350,21 @@ namespace
...
@@ -1350,6 +1350,21 @@ namespace
DLIB_TEST
(
mm
(
3
)
==
4
);
DLIB_TEST
(
mm
(
3
)
==
4
);
}
}
{
const
long
n
=
5
;
matrix
<
double
>
m1
,
m2
,
m3
,
truth
;
m1
=
randm
(
n
,
n
);
m2
=
randm
(
n
,
n
);
rectangle
rect1
(
1
,
1
,
3
,
3
);
rectangle
rect2
(
2
,
1
,
4
,
3
);
truth
=
subm
(
m1
,
rect1
)
*
subm
(
m2
,
rect2
);
m3
=
mat
(
&
m1
(
0
,
0
)
+
6
,
3
,
3
,
m1
.
nc
())
*
mat
(
&
m2
(
0
,
0
)
+
7
,
3
,
3
,
m2
.
nc
());
DLIB_TEST
(
max
(
abs
(
truth
-
m3
))
<
1e-13
);
}
{
{
const
long
n
=
5
;
const
long
n
=
5
;
matrix
<
double
>
m1
,
m2
,
m3
,
truth
;
matrix
<
double
>
m1
,
m2
,
m3
,
truth
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment