Commit 0886042c authored by lishen's avatar lishen
Browse files

dlib from github, version=19.24

parent 5b127120
Pipeline #262 failed with stages
in 0 seconds
**/.idea
*~
*.swp
*.o
*.so
*.pyc
build
build2
dist
*.egg-info/
docs/release/
docs/docs/web/
docs/docs/chm/
docs/docs/cache/
docs/docs/git-logs.xml
docs/docs/python/classes.txt
docs/docs/python/functions.txt
docs/docs/python/constants.txt
**/.vscode
cmake_minimum_required(VERSION 3.8.0)
project(dlib_project)
#############################################################################
# #
# READ examples/CMakeLists.txt TO SEE HOW TO USE DLIB FROM C++ WITH CMAKE #
# #
#############################################################################
get_directory_property(has_parent PARENT_DIRECTORY)
if(NOT has_parent)
# When you call add_subdirectory(dlib) from a parent CMake project dlib's
# CMake scripts will assume you want to statically compile dlib into
# whatever you are building rather than create a standalone copy of dlib.
# This means CMake will build dlib as a static library, disable dlib's
# install targets so they don't clutter your project, and adjust a few other
# minor things that are convenient when statically building dlib as part of
# your own projects.
#
# On the other hand, if there is no parent CMake project or if
# DLIB_IN_PROJECT_BUILD is set to false, CMake will compile dlib as a normal
# standalone library (either shared or static, based on the state of CMake's
# BUILD_SHARED_LIBS flag), and include the usual install targets so you can
# install dlib on your computer via `make install`. Since the only reason
# to build this CMakeLists.txt (the one you are reading right now) by itself
# is if you want to install dlib, we indicate as such by setting
# DLIB_IN_PROJECT_BUILD to false.
set(DLIB_IN_PROJECT_BUILD false)
endif()
add_subdirectory(dlib)
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
\ No newline at end of file
#
# MANIFEST.in
#
# Manifest template for creating the dlib source distribution.
include MANIFEST.in
include setup.py
include README.md
# sources
recursive-include dlib **
recursive-include python_examples *.txt *.py
recursive-include tools/python **
prune tools/python/build*
prune dlib/cmake_utils/*/build*
prune dlib/test
global-exclude *.pyc
# dlib C++ library [![GitHub Actions C++ Status](https://github.com/davisking/dlib/actions/workflows/build_cpp.yml/badge.svg)](https://github.com/davisking/dlib/actions/workflows/build_cpp.yml) [![GitHub Actions Python Status](https://github.com/davisking/dlib/actions/workflows/build_python.yml/badge.svg)](https://github.com/davisking/dlib/actions/workflows/build_python.yml)
Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real world problems. See [http://dlib.net](http://dlib.net) for the main project documentation and API reference.
## Compiling dlib C++ example programs
Go into the examples folder and type:
```bash
mkdir build; cd build; cmake .. ; cmake --build .
```
That will build all the examples.
If you have a CPU that supports AVX instructions then turn them on like this:
```bash
mkdir build; cd build; cmake .. -DUSE_AVX_INSTRUCTIONS=1; cmake --build .
```
Doing so will make some things run faster.
Finally, Visual Studio users should usually do everything in 64bit mode. By default Visual Studio is 32bit, both in its outputs and its own execution, so you have to explicitly tell it to use 64bits. Since it's not the 1990s anymore you probably want to use 64bits. Do that with a cmake invocation like this:
```bash
cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64
```
## Compiling your own C++ programs that use dlib
The examples folder has a [CMake tutorial](https://github.com/davisking/dlib/blob/master/examples/CMakeLists.txt) that tells you what to do. There are also additional instructions on the [dlib web site](http://dlib.net/compile.html).
Alternatively, if you are using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager you can download and install dlib with CMake integration in a single command:
```bash
vcpkg install dlib
```
## Compiling dlib Python API
Before you can run the Python example programs you must compile dlib. Type:
```bash
python setup.py install
```
## Running the unit test suite
Type the following to compile and run the dlib unit test suite:
```bash
cd dlib/test
mkdir build
cd build
cmake ..
cmake --build . --config Release
./dtest --runall
```
Note that on windows your compiler might put the test executable in a subfolder called `Release`. If that's the case then you have to go to that folder before running the test.
This library is licensed under the Boost Software License, which can be found in [dlib/LICENSE.txt](https://github.com/davisking/dlib/blob/master/dlib/LICENSE.txt). The long and short of the license is that you can use dlib however you like, even in closed source commercial software.
## dlib sponsors
This research is based in part upon work supported by the Office of the Director of National Intelligence (ODNI), Intelligence Advanced Research Projects Activity (IARPA) under contract number 2014-14071600010. The views and conclusions contained herein are those of the authors and should not be interpreted as necessarily representing the official policies or endorsements, either expressed or implied, of ODNI, IARPA, or the U.S. Government.
This diff is collapsed.
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
\ No newline at end of file
This diff is collapsed.
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_ALL_SOURCe_
#define DLIB_ALL_SOURCe_
#if defined(DLIB_ALGs_) || defined(DLIB_PLATFORm_)
#include "../dlib_basic_cpp_build_tutorial.txt"
#endif
// ISO C++ code
#include "../base64/base64_kernel_1.cpp"
#include "../bigint/bigint_kernel_1.cpp"
#include "../bigint/bigint_kernel_2.cpp"
#include "../bit_stream/bit_stream_kernel_1.cpp"
#include "../entropy_decoder/entropy_decoder_kernel_1.cpp"
#include "../entropy_decoder/entropy_decoder_kernel_2.cpp"
#include "../entropy_encoder/entropy_encoder_kernel_1.cpp"
#include "../entropy_encoder/entropy_encoder_kernel_2.cpp"
#include "../md5/md5_kernel_1.cpp"
#include "../tokenizer/tokenizer_kernel_1.cpp"
#include "../unicode/unicode.cpp"
#include "../test_for_odr_violations.cpp"
#ifndef DLIB_ISO_CPP_ONLY
// Code that depends on OS specific APIs
// include this first so that it can disable the older version
// of the winsock API when compiled in windows.
#include "../sockets/sockets_kernel_1.cpp"
#include "../bsp/bsp.cpp"
#include "../dir_nav/dir_nav_kernel_1.cpp"
#include "../dir_nav/dir_nav_kernel_2.cpp"
#include "../dir_nav/dir_nav_extensions.cpp"
#include "../linker/linker_kernel_1.cpp"
#include "../logger/extra_logger_headers.cpp"
#include "../logger/logger_kernel_1.cpp"
#include "../logger/logger_config_file.cpp"
#include "../misc_api/misc_api_kernel_1.cpp"
#include "../misc_api/misc_api_kernel_2.cpp"
#include "../sockets/sockets_extensions.cpp"
#include "../sockets/sockets_kernel_2.cpp"
#include "../sockstreambuf/sockstreambuf.cpp"
#include "../sockstreambuf/sockstreambuf_unbuffered.cpp"
#include "../server/server_kernel.cpp"
#include "../server/server_iostream.cpp"
#include "../server/server_http.cpp"
#include "../threads/multithreaded_object_extension.cpp"
#include "../threads/threaded_object_extension.cpp"
#include "../threads/threads_kernel_1.cpp"
#include "../threads/threads_kernel_2.cpp"
#include "../threads/threads_kernel_shared.cpp"
#include "../threads/thread_pool_extension.cpp"
#include "../threads/async.cpp"
#include "../timer/timer.cpp"
#include "../stack_trace.cpp"
#ifdef DLIB_PNG_SUPPORT
#include "../image_loader/png_loader.cpp"
#include "../image_saver/save_png.cpp"
#endif
#ifdef DLIB_JPEG_SUPPORT
#include "../image_loader/jpeg_loader.cpp"
#include "../image_saver/save_jpeg.cpp"
#endif
#ifndef DLIB_NO_GUI_SUPPORT
#include "../gui_widgets/fonts.cpp"
#include "../gui_widgets/widgets.cpp"
#include "../gui_widgets/drawable.cpp"
#include "../gui_widgets/canvas_drawing.cpp"
#include "../gui_widgets/style.cpp"
#include "../gui_widgets/base_widgets.cpp"
#include "../gui_core/gui_core_kernel_1.cpp"
#include "../gui_core/gui_core_kernel_2.cpp"
#endif // DLIB_NO_GUI_SUPPORT
#include "../cuda/cpu_dlib.cpp"
#include "../cuda/tensor_tools.cpp"
#include "../data_io/image_dataset_metadata.cpp"
#include "../data_io/mnist.cpp"
#include "../data_io/cifar.cpp"
#include "../svm/auto.cpp"
#include "../global_optimization/global_function_search.cpp"
#include "../filtering/kalman_filter.cpp"
#endif // DLIB_ISO_CPP_ONLY
#define DLIB_ALL_SOURCE_END
#endif // DLIB_ALL_SOURCe_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_
#define DLIB_AnY_
#include "any/any.h"
#include "any/any_trainer.h"
#include "any/any_decision_function.h"
#include "any/any_function.h"
#endif // DLIB_AnY_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_H_
#define DLIB_AnY_H_
#include "any_abstract.h"
#include <memory>
#include "storage.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class any
{
public:
any() = default;
any(const any& other) = default;
any& operator=(const any& other) = default;
any(any&& other) = default;
any& operator=(any&& other) = default;
template<
typename T,
std::enable_if_t<!std::is_same<std::decay_t<T>, any>::value, bool> = true
>
any(T&& item)
: storage{std::forward<T>(item)}
{
}
template<
typename T,
typename T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same<T_, any>::value, bool> = true
>
any& operator=(T&& item)
{
if (contains<T_>())
storage.unsafe_get<T_>() = std::forward<T>(item);
else
*this = std::move(any{std::forward<T>(item)});
return *this;
}
bool is_empty() const { return storage.is_empty(); }
void clear() { storage.clear(); }
void swap (any& item) { std::swap(*this, item); }
template<typename T> bool contains() const { return storage.contains<T>();}
template <typename T> T& cast_to() { return storage.cast_to<T>(); }
template <typename T> const T& cast_to() const { return storage.cast_to<T>(); }
template <typename T> T& get() { return storage.get<T>(); }
private:
te::storage_heap storage;
};
// ----------------------------------------------------------------------------------------
template <typename T> T& any_cast(any& a) { return a.cast_to<T>(); }
template <typename T> const T& any_cast(const any& a) { return a.cast_to<T>(); }
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_ABSTRACT_H_
#ifdef DLIB_AnY_ABSTRACT_H_
#include <typeinfo>
namespace dlib
{
// ----------------------------------------------------------------------------------------
class bad_any_cast : public std::bad_cast
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is the exception class used by the any object.
It is used to indicate when someone attempts to cast an any
object into a type which isn't contained in the any object.
!*/
public:
virtual const char* what() const throw() { return "bad_any_cast"; }
};
// ----------------------------------------------------------------------------------------
class any
{
/*!
INITIAL VALUE
- is_empty() == true
- for all T: contains<T>() == false
WHAT THIS OBJECT REPRESENTS
This object is basically a type-safe version of a void*. In particular,
it is a container which can contain only one object but the object may
be of any type.
It is somewhat like the type_safe_union except you don't have to declare
the set of possible content types beforehand. So in some sense this is
like a less type-strict version of the type_safe_union.
!*/
public:
any(
);
/*!
ensures
- this object is properly initialized
!*/
any (
const any& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
any_function (
any_function&& item
);
/*!
ensures
- #item.is_empty() == true
- moves item into *this.
!*/
template < typename T >
any (
const T& item
);
/*!
ensures
- #contains<T>() == true
- #cast_to<T>() == item
(i.e. a copy of item will be stored in *this)
!*/
void clear (
);
/*!
ensures
- #*this will have its default value. I.e. #is_empty() == true
!*/
template <typename T>
bool contains (
) const;
/*!
ensures
- if (this object currently contains an object of type T) then
- returns true
- else
- returns false
!*/
bool is_empty(
) const;
/*!
ensures
- if (this object contains any kind of object) then
- returns false
- else
- returns true
!*/
template <typename T>
T& cast_to(
);
/*!
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
const T& cast_to(
) const;
/*!
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
T& get(
);
/*!
ensures
- #is_empty() == false
- #contains<T>() == true
- if (contains<T>() == true)
- returns a non-const reference to the object contained in *this.
- else
- Constructs an object of type T inside *this
- Any previous object stored in this any object is destructed and its
state is lost.
- returns a non-const reference to the newly created T object.
!*/
any& operator= (
const any& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
void swap (
any& item
);
/*!
ensures
- swaps *this and item
- does not invalidate pointers or references to the object contained
inside *this or item. Moreover, a pointer or reference to the object in
*this will now refer to the contents of #item and vice versa.
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T
>
T& any_cast(
any& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const T& any_cast(
const any& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_ABSTRACT_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_DECISION_FUNCTION_Hh_
#define DLIB_AnY_DECISION_FUNCTION_Hh_
#include "any_decision_function_abstract.h"
#include "any_function.h"
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <class sample_type, class result_type = double>
using any_decision_function = any_function<result_type(const sample_type&)>;
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_DECISION_FUNCTION_Hh_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
#ifdef DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
#include "any_function_abstract.h"
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <class sample_type, class result_type = double>
using any_decision_function = any_function<result_type(const sample_type&)>;
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_DECISION_FUNCTION_ABSTRACT_H_
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_FUNCTION_Hh_
#define DLIB_AnY_FUNCTION_Hh_
#include "../assert.h"
#include "../functional.h"
#include "any.h"
#include "any_function_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
class Storage,
class F
>
class any_function_basic;
template <
class Storage,
class R,
class... Args
>
class any_function_basic<Storage, R(Args...)>
{
private:
template<class F>
using is_valid = std::enable_if_t<!std::is_same<std::decay_t<F>, any_function_basic>::value &&
dlib::is_invocable_r<R, F, Args...>::value,
bool>;
template<typename Func>
static auto make_invoker()
{
return [](void* self, Args... args) -> R {
return dlib::invoke(*reinterpret_cast<std::add_pointer_t<Func>>(self),
std::forward<Args>(args)...);
};
}
Storage str;
R (*func)(void*, Args...) = nullptr;
public:
using result_type = R;
constexpr any_function_basic(std::nullptr_t) noexcept {}
constexpr any_function_basic() = default;
constexpr any_function_basic(const any_function_basic& other) = default;
constexpr any_function_basic& operator=(const any_function_basic& other) = default;
constexpr any_function_basic(any_function_basic&& other)
: str{std::move(other.str)},
func{std::exchange(other.func, nullptr)}
{
}
constexpr any_function_basic& operator=(any_function_basic&& other)
{
if (this != &other)
{
str = std::move(other.str);
func = std::exchange(other.func, nullptr);
}
return *this;
}
template<class F, is_valid<F> = true>
any_function_basic(
F&& f
) : str{std::forward<F>(f)},
func{make_invoker<F&&>()}
{
}
template<class F, is_valid<F> = true>
any_function_basic(
F* f
) : str{f},
func{make_invoker<F*>()}
{
}
R operator()(Args... args) const {
return func(const_cast<void*>(str.get_ptr()), std::forward<Args>(args)...);
}
void clear() { str.clear(); }
void swap (any_function_basic& item) { std::swap(*this, item); }
bool is_empty() const noexcept { return str.is_empty() || func == nullptr; }
bool is_set() const noexcept { return !is_empty(); }
explicit operator bool() const noexcept { return is_set(); }
template <typename T> bool contains() const { return str.template contains<T>();}
template <typename T> T& cast_to() { return str.template cast_to<T>(); }
template <typename T> const T& cast_to() const { return str.template cast_to<T>(); }
template <typename T> T& get() { return str.template get<T>(); }
};
// ----------------------------------------------------------------------------------------
template <class T, class Storage, class F>
T& any_cast(any_function_basic<Storage, F>& a) { return a.template cast_to<T>(); }
template <class T, class Storage, class F>
const T& any_cast(const any_function_basic<Storage, F>& a) { return a.template cast_to<T>(); }
// ----------------------------------------------------------------------------------------
template <class F>
using any_function = any_function_basic<te::storage_sbo<16>, F>;
template <class F>
using any_function_view = any_function_basic<te::storage_view, F>;
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_FUNCTION_Hh_
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_FUNCTION_ABSTRACT_H_
#ifdef DLIB_AnY_FUNCTION_ABSTRACT_H_
#include "any_abstract.h"
#include "../algs.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
class Storage,
typename function_type
>
class any_function_basic
{
/*!
REQUIREMENTS ON Storage
This must be one of the storage types from dlib/any/storage.hh
E.g. storage_heap, storage_stack, etc.
It determines the method by which any_function_basic holds onto the function it uses.
REQUIREMENTS ON function_type
This type should be a function signature. Some examples are:
void (int,int) // a function returning nothing and taking two ints
void () // a function returning nothing and taking no arguments
char (double&) // a function returning a char and taking a reference to a double
The number of arguments in the function must be no greater than 10.
INITIAL VALUE
- is_empty() == true
- for all T: contains<T>() == false
WHAT THIS OBJECT REPRESENTS
This object is a version of dlib::any that is restricted to containing
elements which are some kind of function object with an operator() which
matches the function signature defined by function_type.
Here is an example:
#include <iostream>
#include <string>
#include "dlib/any.h"
using namespace std;
void print_message(string str) { cout << str << endl; }
int main()
{
dlib::any_function<void(string)> f;
f = print_message;
f("hello world"); // calls print_message("hello world")
}
Note that any_function_basic objects can be used to store general function
objects (i.e. defined by a class with an overloaded operator()) in
addition to regular global functions.
!*/
public:
// This is the type of object returned by function_type functions.
typedef result_type_for_function_type result_type;
any_function_basic(
);
/*!
ensures
- this object is properly initialized
!*/
any_function_basic (
const any_function_basic& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
any_function_basic (
any_function_basic&& item
);
/*!
ensures
- moves item into *this.
- The exact move semantics are determined by which Storage type is used. E.g.
storage_heap will result in #item.is_empty()==true but storage_view would result
in #item.is_empty() == false
!*/
template < typename Funct >
any_function_basic (
Funct&& funct
);
/*!
ensures
- #contains<T>() == true
- #cast_to<T>() == item
(i.e. calling operator() will invoke funct())
!*/
void clear (
);
/*!
ensures
- #*this will have its default value. I.e. #is_empty() == true
!*/
template <typename T>
bool contains (
) const;
/*!
ensures
- if (this object currently contains an object of type T) then
- returns true
- else
- returns false
!*/
bool is_empty(
) const;
/*!
ensures
- if (this object contains any kind of object) then
- returns false
- else
- returns true
!*/
bool is_set (
) const;
/*!
ensures
- returns !is_empty()
!*/
explicit operator bool(
) const;
/*!
ensures
- returns is_set()
!*/
result_type operator(Args... args) (
) const;
/*!
requires
- is_empty() == false
- the signature defined by function_type takes no arguments
ensures
- Let F denote the function object contained within *this. Then
this function performs:
return F(std::forward<Args>(args)...)
!*/
template <typename T>
T& cast_to(
);
/*!
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
const T& cast_to(
) const;
/*!
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
T& get(
);
/*!
ensures
- #is_empty() == false
- #contains<T>() == true
- if (contains<T>() == true)
- returns a non-const reference to the object contained in *this.
- else
- Constructs an object of type T inside *this
- Any previous object stored in this any_function_basic object is destructed and its
state is lost.
- returns a non-const reference to the newly created T object.
!*/
any_function_basic& operator= (
const any_function_basic& item
);
/*!
ensures
- copies the state of item into *this.
- Note that the type of copy is determined by the Storage template argument. E.g.
storage_sbo will result in a deep copy, while storage_view would result in *this
and item referring to the same underlying function.
!*/
void swap (
any_function_basic& item
);
/*!
ensures
- swaps *this and item
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename function_type
>
T& any_cast(
any_function_basic<function_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename function_type
>
const T& any_cast(
const any_function_basic<function_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
/*!A any_function
A version of any_function_basic (defined above) that owns the function it contains. Uses
the small buffer optimization to make working with small lambdas faster.
!*/
template <class F>
using any_function = any_function_basic<te::storage_sbo<16>, F>;
/*!A any_function_view
A version of any_function_basic (defined above) that *DOES NOT* own the function it
contains. It merely holds a pointer to the function given to its constructor.
!*/
template <class F>
using any_function_view = any_function_basic<te::storage_view, F>;
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_FUNCTION_ABSTRACT_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AnY_TRAINER_H_
#define DLIB_AnY_TRAINER_H_
#include "any.h"
#include "any_decision_function.h"
#include "any_trainer_abstract.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_trainer
{
public:
using sample_type = sample_type_;
using scalar_type = scalar_type_;
using mem_manager_type = default_memory_manager;
using trained_function_type = any_decision_function<sample_type, scalar_type>;
any_trainer() = default;
any_trainer(const any_trainer& other) = default;
any_trainer& operator=(const any_trainer& other) = default;
any_trainer(any_trainer&& other) = default;
any_trainer& operator=(any_trainer&& other) = default;
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same<T_,any_trainer>::value, bool> = true
>
any_trainer (
T&& item
) : storage{std::forward<T>(item)},
train_func{[](
const void* ptr,
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) -> trained_function_type {
const T_& f = *reinterpret_cast<const T_*>(ptr);
return f.train(samples, labels);
}}
{
}
template <
class T,
class T_ = std::decay_t<T>,
std::enable_if_t<!std::is_same<T_,any_trainer>::value, bool> = true
>
any_trainer& operator= (
T&& item
)
{
if (contains<T_>())
storage.unsafe_get<T_>() = std::forward<T>(item);
else
*this = std::move(any_trainer{std::forward<T>(item)});
return *this;
}
trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(is_empty() == false,
"\t trained_function_type any_trainer::train()"
<< "\n\t You can't call train() on an empty any_trainer"
<< "\n\t this: " << this
);
return train_func(storage.get_ptr(), samples, labels);
}
bool is_empty() const { return storage.is_empty(); }
void clear() { storage.clear(); }
void swap (any_trainer& item) { std::swap(*this, item); }
template <typename T> bool contains() const { return storage.contains<T>();}
template <typename T> T& cast_to() { return storage.cast_to<T>(); }
template <typename T> const T& cast_to() const { return storage.cast_to<T>(); }
template <typename T> T& get() { return storage.get<T>(); }
private:
te::storage_heap storage;
trained_function_type (*train_func) (
const void* self,
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) = nullptr;
};
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename V>
T& any_cast(any_trainer<U,V>& a) { return a.template cast_to<T>(); }
template <typename T, typename U, typename V>
const T& any_cast(const any_trainer<U,V>& a) { return a.template cast_to<T>(); }
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_TRAINER_H_
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_AnY_TRAINER_ABSTRACT_H_
#ifdef DLIB_AnY_TRAINER_ABSTRACT_H_
#include "any_abstract.h"
#include "../algs.h"
#include "any_decision_function_abstract.h"
#include <vector>
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename sample_type_,
typename scalar_type_ = double
>
class any_trainer
{
/*!
INITIAL VALUE
- is_empty() == true
- for all T: contains<T>() == false
WHAT THIS OBJECT REPRESENTS
This object is a version of dlib::any that is restricted to containing
elements which are some kind of object with a .train() method compatible
with the following signature:
decision_function train(
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
Where decision_function is a type capable of being stored in an
any_decision_function<sample_type,scalar_type> object.
any_trainer is intended to be used to contain objects such as the svm_nu_trainer
and other similar types which represent supervised machine learning algorithms.
It allows you to write code which contains and processes these trainer objects
without needing to know the specific types of trainer objects used.
!*/
public:
typedef sample_type_ sample_type;
typedef scalar_type_ scalar_type;
typedef default_memory_manager mem_manager_type;
typedef any_decision_function<sample_type, scalar_type> trained_function_type;
any_trainer(
);
/*!
ensures
- this object is properly initialized
!*/
any_trainer (
const any_trainer& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
any_trainer (
any_trainer&& item
);
/*!
ensures
- #item.is_empty() == true
- moves item into *this.
!*/
template < typename T >
any_trainer (
const T& item
);
/*!
ensures
- #contains<T>() == true
- #cast_to<T>() == item
(i.e. a copy of item will be stored in *this)
!*/
void clear (
);
/*!
ensures
- #*this will have its default value. I.e. #is_empty() == true
!*/
template <typename T>
bool contains (
) const;
/*!
ensures
- if (this object currently contains an object of type T) then
- returns true
- else
- returns false
!*/
bool is_empty(
) const;
/*!
ensures
- if (this object contains any kind of object) then
- returns false
- else
- returns true
!*/
trained_function_type train (
const std::vector<sample_type>& samples,
const std::vector<scalar_type>& labels
) const
/*!
requires
- is_empty() == false
ensures
- Let TRAINER denote the object contained within *this. Then
this function performs:
return TRAINER.train(samples, labels)
!*/
template <typename T>
T& cast_to(
);
/*!
ensures
- if (contains<T>() == true) then
- returns a non-const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
const T& cast_to(
) const;
/*!
ensures
- if (contains<T>() == true) then
- returns a const reference to the object contained within *this
- else
- throws bad_any_cast
!*/
template <typename T>
T& get(
);
/*!
ensures
- #is_empty() == false
- #contains<T>() == true
- if (contains<T>() == true)
- returns a non-const reference to the object contained in *this.
- else
- Constructs an object of type T inside *this
- Any previous object stored in this any_trainer object is destructed and its
state is lost.
- returns a non-const reference to the newly created T object.
!*/
any_trainer& operator= (
const any_trainer& item
);
/*!
ensures
- copies the state of item into *this.
- Note that *this and item will contain independent copies of the
contents of item. That is, this function performs a deep
copy and therefore does not result in *this containing
any kind of reference to item.
!*/
void swap (
any_trainer& item
);
/*!
ensures
- swaps *this and item
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename sample_type,
typename scalar_type
>
inline void swap (
any_trainer<sample_type,scalar_type>& a,
any_trainer<sample_type,scalar_type>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
T& any_cast(
any_trainer<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename sample_type,
typename scalar_type
>
const T& any_cast(
const any_trainer<sample_type,scalar_type>& a
) { return a.cast_to<T>(); }
/*!
ensures
- returns a.cast_to<T>()
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AnY_TRAINER_ABSTRACT_H_
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment