PetaVision  Alpha
Factory.cpp
1 /*
2  * Factory.cpp
3  *
4  * Created on: Jan 20, 2016
5  * Author: pschultz
6  */
7 
8 #include "Factory.hpp"
9 #include "include/pv_common.h"
10 
11 #include "columns/HyPerCol.hpp"
12 
13 #include "layers/ANNErrorLayer.hpp"
14 #include "layers/ANNLayer.hpp"
15 #include "layers/ANNSquaredLayer.hpp"
16 #include "layers/BackgroundLayer.hpp"
17 #include "layers/BinningLayer.hpp"
18 #include "layers/CloneVLayer.hpp"
19 #include "layers/ConstantLayer.hpp"
20 #include "layers/DropoutLayer.hpp"
21 #include "layers/FilenameParsingGroundTruthLayer.hpp"
22 #include "layers/GapLayer.hpp"
23 #include "layers/HyPerLCALayer.hpp"
24 #include "layers/HyPerLayer.hpp"
25 #include "layers/ISTALayer.hpp"
26 #include "layers/ImageLayer.hpp"
27 #include "layers/InputRegionLayer.hpp"
28 #include "layers/LIF.hpp"
29 #include "layers/LIFGap.hpp"
30 #include "layers/LeakyIntegrator.hpp"
31 #include "layers/MaskLayer.hpp"
32 #include "layers/MomentumLCALayer.hpp"
33 #include "layers/PoolingIndexLayer.hpp"
34 #include "layers/PtwiseProductLayer.hpp"
35 #include "layers/PtwiseQuotientLayer.hpp"
36 #include "layers/PvpLayer.hpp"
37 #include "layers/RescaleLayer.hpp"
38 #include "layers/Retina.hpp"
39 #include "layers/SigmoidLayer.hpp"
40 
41 #include "deprecated/ANNWhitenedLayer.hpp"
42 #include "deprecated/ImageFromMemoryBuffer.hpp"
43 #include "deprecated/KmeansLayer.hpp"
44 #include "deprecated/LCALIFLayer.hpp"
45 #include "deprecated/LabelErrorLayer.hpp"
46 #include "deprecated/RunningAverageLayer.hpp"
47 #include "deprecated/ShuffleLayer.hpp"
48 #include "deprecated/WTALayer.hpp"
49 
50 #include "connections/CloneConn.hpp"
51 #include "connections/CopyConn.hpp"
52 #include "connections/FeedbackConn.hpp"
53 #include "connections/GapConn.hpp"
54 #include "connections/HyPerConn.hpp"
55 #include "connections/IdentConn.hpp"
56 #include "connections/MomentumConn.hpp"
57 #include "connections/PlasticCloneConn.hpp"
58 #include "connections/PoolingConn.hpp"
59 #include "connections/RescaleConn.hpp"
60 #include "connections/TransposeConn.hpp"
61 #include "connections/TransposePoolingConn.hpp"
62 #include "connections/WTAConn.hpp"
63 
64 #include "probes/AdaptiveTimeScaleProbe.hpp"
65 #include "probes/ColumnEnergyProbe.hpp"
66 #include "probes/FirmThresholdCostFnLCAProbe.hpp"
67 #include "probes/FirmThresholdCostFnProbe.hpp"
68 #include "probes/KernelProbe.hpp"
69 #include "probes/KneeTimeScaleProbe.hpp"
70 #include "probes/L0NormLCAProbe.hpp"
71 #include "probes/L0NormProbe.hpp"
72 #include "probes/L1NormLCAProbe.hpp"
73 #include "probes/L1NormProbe.hpp"
74 #include "probes/L2ConnProbe.hpp"
75 #include "probes/L2NormProbe.hpp"
76 #include "probes/LogTimeScaleProbe.hpp"
77 #include "probes/PointLIFProbe.hpp"
78 #include "probes/PointProbe.hpp"
79 #include "probes/QuotientColProbe.hpp"
80 #include "probes/RequireAllZeroActivityProbe.hpp"
81 #include "probes/StatsProbe.hpp"
82 
83 #include "initv/ConstantV.hpp"
84 #include "initv/GaussianRandomV.hpp"
85 #include "initv/InitVFromFile.hpp"
86 #include "initv/UniformRandomV.hpp"
87 #include "initv/ZeroV.hpp"
88 
89 #include "delivery/IdentDelivery.hpp"
90 #include "delivery/PostsynapticPerspectiveConvolveDelivery.hpp"
91 #include "delivery/PostsynapticPerspectiveStochasticDelivery.hpp"
92 #include "delivery/PresynapticPerspectiveConvolveDelivery.hpp"
93 #include "delivery/PresynapticPerspectiveStochasticDelivery.hpp"
94 #include "delivery/RescaleDelivery.hpp"
95 #include "delivery/WTADelivery.hpp"
96 
97 #ifdef PV_USE_CUDA
98 #include "delivery/PostsynapticPerspectiveGPUDelivery.hpp"
99 #include "delivery/PresynapticPerspectiveGPUDelivery.hpp"
100 #endif // PV_USE_CUDA
101 
102 #include "weightinit/InitCocircWeights.hpp"
103 #include "weightinit/InitGauss2DWeights.hpp"
104 #include "weightinit/InitGaussianRandomWeights.hpp"
105 #include "weightinit/InitIdentWeights.hpp"
106 #include "weightinit/InitOneToOneWeights.hpp"
107 #include "weightinit/InitOneToOneWeightsWithDelays.hpp"
108 #include "weightinit/InitSmartWeights.hpp"
109 #include "weightinit/InitSpreadOverArborsWeights.hpp"
110 #include "weightinit/InitUniformRandomWeights.hpp"
111 #include "weightinit/InitUniformWeights.hpp"
112 #include "weightinit/InitWeights.hpp"
113 
114 #include "weightupdaters/HebbianUpdater.hpp"
115 
116 #include "normalizers/NormalizeContrastZeroMean.hpp"
117 #include "normalizers/NormalizeGroup.hpp"
118 #include "normalizers/NormalizeL2.hpp"
119 #include "normalizers/NormalizeMax.hpp"
120 #include "normalizers/NormalizeNone.hpp"
121 #include "normalizers/NormalizeSum.hpp"
122 
123 namespace PV {
124 
126 
128  keywordHandlerList = std::vector<KeywordHandler *>();
129 
130  registerKeyword("ANNErrorLayer", Factory::create<ANNErrorLayer>);
131  registerKeyword("ANNLayer", Factory::create<ANNLayer>);
132  registerKeyword("ANNSquaredLayer", Factory::create<ANNSquaredLayer>);
133  registerKeyword("ANNWhitenedLayer", Factory::create<ANNWhitenedLayer>);
134  registerKeyword("BackgroundLayer", Factory::create<BackgroundLayer>);
135  registerKeyword("BinningLayer", Factory::create<BinningLayer>);
136  registerKeyword("CloneVLayer", Factory::create<CloneVLayer>);
137  registerKeyword("ConstantLayer", Factory::create<ConstantLayer>);
138  registerKeyword("DropoutLayer", Factory::create<DropoutLayer>);
140  "FilenameParsingGroundTruthLayer", Factory::create<FilenameParsingGroundTruthLayer>);
141  registerKeyword("GapLayer", Factory::create<GapLayer>);
142  registerKeyword("HyPerLayer", Factory::create<HyPerLayer>);
143  registerKeyword("HyPerLCALayer", Factory::create<HyPerLCALayer>);
144  registerKeyword("ISTALayer", Factory::create<ISTALayer>);
145 
146  registerKeyword("ImageLayer", Factory::create<ImageLayer>);
147  registerKeyword("InputRegionLayer", Factory::create<InputRegionLayer>);
148  registerKeyword("PvpLayer", Factory::create<PvpLayer>);
149  registerKeyword("ImageFromMemoryBuffer", Factory::create<ImageFromMemoryBuffer>);
150  registerKeyword("KmeansLayer", Factory::create<KmeansLayer>);
151  registerKeyword("LCALIFLayer", Factory::create<LCALIFLayer>);
152  registerKeyword("LIF", Factory::create<LIF>);
153  registerKeyword("LIFGap", Factory::create<LIFGap>);
154  registerKeyword("LabelErrorLayer", Factory::create<LabelErrorLayer>);
155  registerKeyword("LeakyIntegrator", Factory::create<LeakyIntegrator>);
156  registerKeyword("MaskLayer", Factory::create<MaskLayer>);
157  registerKeyword("MomentumLCALayer", Factory::create<MomentumLCALayer>);
158  registerKeyword("PoolingIndexLayer", Factory::create<PoolingIndexLayer>);
159  registerKeyword("PtwiseProductLayer", Factory::create<PtwiseProductLayer>);
160  registerKeyword("PtwiseQuotientLayer", Factory::create<PtwiseQuotientLayer>);
161  registerKeyword("RescaleLayer", Factory::create<RescaleLayer>);
162  registerKeyword("RunningAverageLayer", Factory::create<RunningAverageLayer>);
163  registerKeyword("Retina", Factory::create<Retina>);
164  registerKeyword("ShuffleLayer", Factory::create<ShuffleLayer>);
165  registerKeyword("SigmoidLayer", Factory::create<SigmoidLayer>);
166  registerKeyword("WTALayer", Factory::create<WTALayer>);
167 
168  registerKeyword("HyPerConn", Factory::create<HyPerConn>);
169  registerKeyword("CloneConn", Factory::create<CloneConn>);
170  // registerKeyword("ComponentsConn", Factory::create<ComponentsConn>);
171  registerKeyword("CopyConn", Factory::create<CopyConn>);
172  registerKeyword("FeedbackConn", Factory::create<FeedbackConn>);
173  registerKeyword("GapConn", Factory::create<GapConn>);
174  registerKeyword("IdentConn", Factory::create<IdentConn>);
175  registerKeyword("MomentumConn", Factory::create<MomentumConn>);
176  registerKeyword("PlasticCloneConn", Factory::create<PlasticCloneConn>);
177  registerKeyword("PoolingConn", Factory::create<PoolingConn>);
178  registerKeyword("RescaleConn", Factory::create<RescaleConn>);
179  registerKeyword("TransposeConn", Factory::create<TransposeConn>);
180  registerKeyword("TransposePoolingConn", Factory::create<TransposePoolingConn>);
181  registerKeyword("WTAConn", Factory::create<WTAConn>);
182 
183  registerKeyword("AdaptiveTimeScaleProbe", Factory::create<AdaptiveTimeScaleProbe>);
184  registerKeyword("KneeTimeScaleProbe", Factory::create<KneeTimeScaleProbe>);
185  registerKeyword("LogTimeScaleProbe", Factory::create<LogTimeScaleProbe>);
186  registerKeyword("ColumnEnergyProbe", Factory::create<ColumnEnergyProbe>);
187  registerKeyword("FirmThresholdCostFnLCAProbe", Factory::create<FirmThresholdCostFnLCAProbe>);
188  registerKeyword("FirmThresholdCostFnProbe", Factory::create<FirmThresholdCostFnProbe>);
189  registerKeyword("KernelProbe", Factory::create<KernelProbe>);
190  registerKeyword("L0NormLCAProbe", Factory::create<L0NormLCAProbe>);
191  registerKeyword("L0NormProbe", Factory::create<L0NormProbe>);
192  registerKeyword("L1NormLCAProbe", Factory::create<L1NormLCAProbe>);
193  registerKeyword("L1NormProbe", Factory::create<L1NormProbe>);
194  registerKeyword("L2ConnProbe", Factory::create<L2ConnProbe>);
195  registerKeyword("L2NormProbe", Factory::create<L2NormProbe>);
196  registerKeyword("PointLIFProbe", Factory::create<PointLIFProbe>);
197  registerKeyword("PointProbe", Factory::create<PointProbe>);
198  registerKeyword("QuotientColProbe", Factory::create<QuotientColProbe>);
199  registerKeyword("RequireAllZeroActivityProbe", Factory::create<RequireAllZeroActivityProbe>);
200  registerKeyword("StatsProbe", Factory::create<StatsProbe>);
201 
202  registerKeyword("ConstantV", Factory::create<ConstantV>);
203  registerKeyword("GaussianRandomV", Factory::create<GaussianRandomV>);
204  registerKeyword("InitVFromFile", Factory::create<InitVFromFile>);
205  registerKeyword("UniformRandomV", Factory::create<UniformRandomV>);
206  registerKeyword("ZeroV", Factory::create<ZeroV>);
207 
208  registerKeyword("IdentDelivery", Factory::create<IdentDelivery>);
210  "PostsynapticPerspectiveConvolveDelivery",
211  Factory::create<PostsynapticPerspectiveConvolveDelivery>);
213  "PostsynapticPerspectiveStochasticDelivery",
214  Factory::create<PostsynapticPerspectiveStochasticDelivery>);
216  "PresynapticPerspectiveConvolveDelivery",
217  Factory::create<PresynapticPerspectiveConvolveDelivery>);
219  "PresynapticPerspectiveStochasticDelivery",
220  Factory::create<PresynapticPerspectiveStochasticDelivery>);
221  registerKeyword("RescaleDelivery", Factory::create<RescaleDelivery>);
222  registerKeyword("WTADelivery", Factory::create<WTADelivery>);
223 #ifdef PV_USE_CUDA
225  "PostsynapticPerspectiveGPUDelivery", Factory::create<PostsynapticPerspectiveGPUDelivery>);
227  "PresynapticPerspectiveGPUDelivery", Factory::create<PresynapticPerspectiveGPUDelivery>);
228 #endif // PV_USE_CUDA
229 
230  registerKeyword("Gauss2DWeight", Factory::create<InitGauss2DWeights>);
231  registerKeyword("CoCircWeight", Factory::create<InitCocircWeights>);
232  registerKeyword("UniformWeight", Factory::create<InitUniformWeights>);
233  registerKeyword("SmartWeight", Factory::create<InitSmartWeights>);
234  registerKeyword("UniformRandomWeight", Factory::create<InitUniformRandomWeights>);
235  registerKeyword("GaussianRandomWeight", Factory::create<InitGaussianRandomWeights>);
236  registerKeyword("IdentWeight", Factory::create<InitIdentWeights>);
237  registerKeyword("OneToOneWeights", Factory::create<InitOneToOneWeights>);
238  registerKeyword("OneToOneWeightsWithDelays", Factory::create<InitOneToOneWeightsWithDelays>);
239  registerKeyword("SpreadOverArborsWeight", Factory::create<InitSpreadOverArborsWeights>);
240  registerKeyword("FileWeight", Factory::create<InitWeights>);
241 
242  registerKeyword("HebbianUpdater", Factory::create<HebbianUpdater>);
243 
244  registerKeyword("normalizeContrastZeroMean", Factory::create<NormalizeContrastZeroMean>);
245  registerKeyword("normalizeL2", Factory::create<NormalizeL2>);
246  registerKeyword("normalizeMax", Factory::create<NormalizeMax>);
247  registerKeyword("none", Factory::create<NormalizeNone>);
248  registerKeyword("normalizeSum", Factory::create<NormalizeSum>);
249  registerKeyword("normalizeGroup", Factory::create<NormalizeGroup>);
250 
251  return PV_SUCCESS;
252 }
253 
254 int Factory::copyKeywordHandlerList(std::vector<KeywordHandler *> const &orig) {
255  for (auto &kh : orig) {
256  registerKeyword(kh->getKeyword(), kh->getCreator());
257  }
258  return PV_SUCCESS;
259 }
260 
261 int Factory::registerKeyword(char const *keyword, ObjectCreateFn creator) {
262  KeywordHandler const *keywordHandler = getKeywordHandler(keyword);
263  if (keywordHandler != nullptr) {
264  return PV_FAILURE;
265  }
266  KeywordHandler *newKeyword = new KeywordHandler(keyword, creator);
267  keywordHandlerList.push_back(newKeyword);
268  return PV_SUCCESS;
269 }
270 
271 BaseObject *Factory::createByKeyword(char const *keyword, char const *name, HyPerCol *hc) const {
272  if (keyword == nullptr) {
273  return nullptr;
274  }
275  KeywordHandler const *keywordHandler = getKeywordHandler(keyword);
276  if (keywordHandler == nullptr) {
277  auto errorString = std::string(keyword).append(" \"").append(name).append("\": ");
278  errorString.append("keyword \"").append(keyword).append("\" is unrecognized.");
279  throw std::invalid_argument(errorString);
280  }
281  return keywordHandler ? keywordHandler->create(name, hc) : nullptr;
282 }
283 
284 KeywordHandler const *Factory::getKeywordHandler(char const *keyword) const {
285  pvAssert(keyword != nullptr);
286  for (auto &typeCreator : keywordHandlerList) {
287  if (!strcmp(typeCreator->getKeyword(), keyword)) {
288  return typeCreator;
289  }
290  }
291  return nullptr;
292 }
293 
295  for (auto &kh : keywordHandlerList) {
296  delete kh;
297  }
298  keywordHandlerList.clear();
299  return PV_SUCCESS;
300 }
301 
303 
304 } /* namespace PV */
int registerKeyword(char const *keyword, ObjectCreateFn creator)
Definition: Factory.cpp:261
int copyKeywordHandlerList(std::vector< KeywordHandler * > const &orig)
Definition: Factory.cpp:254
virtual ~Factory()
Definition: Factory.cpp:302
BaseObject * create(char const *name, HyPerCol *hc) const
KeywordHandler const * getKeywordHandler(char const *keyword) const
Definition: Factory.cpp:284
int registerCoreKeywords()
Definition: Factory.cpp:127
BaseObject * createByKeyword(char const *keyword, char const *name, HyPerCol *hc) const
Definition: Factory.cpp:271
int clearKeywordHandlerList()
Definition: Factory.cpp:294