Repository navigation
Expand file tree
/
Copy pathInputAdapter.hpp
More file actions
executable file
·77 lines (64 loc) · 2.23 KB
/
Copy pathInputAdapter.hpp
File metadata and controls
executable file
·77 lines (64 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache 2.0 license.
// Please see LICENSE file in the project root for terms.
#ifndef CAFFE_DISTRI_INPUTADAPTER_HPP_
#define CAFFE_DISTRI_INPUTADAPTER_HPP_
#include <vector>
#include <map>
#include "common.hpp"
#include "caffe/caffe.hpp"
template<typename Dtype>
class InputAdapter {
protected:
int solver_mode_;
public:
/**
* Create a network data layer to be used by feedNet()
*/
InputAdapter(int solver_mode) {
solver_mode_ = solver_mode;
}
virtual ~InputAdapter() {}
/**
* feed a layer with data and label
*/
virtual void feed(vector< Blob<Dtype>* >& dataBlobs, Dtype* labelPtr) = 0;
};
struct cmp_str
{
bool operator()(const char *a, const char *b) const {
return std::strcmp(a, b) < 0;
}
};
template<typename Dtype>
class InputAdapterRegistry {
public:
typedef InputAdapter<Dtype>* (*AdapterCreator)(shared_ptr<Layer<Dtype> >& layer, int solver_mode);
typedef map<const char *, AdapterCreator, cmp_str> CreatorRegistry;
static void Set(const char* layertypename, AdapterCreator factory);
static InputAdapter<Dtype>* MakeAdapter(shared_ptr<Layer<Dtype> >& layer, int solver_mode);
private:
// AdapterFactory registry should never be instantiated - everything is done with its
// static variables.
InputAdapterRegistry() {}
static CreatorRegistry& Registry() {
static CreatorRegistry* g_registry_ = new CreatorRegistry();
return *g_registry_;
}
};
template <typename Dtype>
class InputAdapterRegisterer {
public:
InputAdapterRegisterer(const char * layerNm,
InputAdapter<Dtype>* (*creator)(shared_ptr<Layer<Dtype> >& layer, int solver_mode)) {
InputAdapterRegistry<Dtype>::Set(layerNm, creator);
}
};
#define REGISTER_INPUT_ADAPTER(layerNm, adapter) \
template<typename Dtype> \
InputAdapter<Dtype>* adapter##Creator(shared_ptr<Layer<Dtype> >& layer, int solver_mode) { \
return new adapter<Dtype>(layer, solver_mode); \
}; \
static InputAdapterRegisterer<float> g_f_creator_##adapter(layerNm, adapter##Creator<float>); \
static InputAdapterRegisterer<double> g_d_creator_##adapter(layerNm, adapter##Creator<double>)
#endif