+#include "FillCommand.h"
#include "EncodeCommand.h"
#include <nncc/foundation/Memory.h>
{
std::map<std::string, std::unique_ptr<Command>> commands;
+ commands["fill"] = nncc::foundation::make_unique<FillCommand>();
commands["encode"] = nncc::foundation::make_unique<EncodeCommand>();
return commands.at(argv[1])->run();
--- /dev/null
+#include "FillCommand.h"
+#include "LayerResolver.h"
+#include "NetworkBuilder.h"
+#include "ParameterRandomizePass.h"
+
+#include <caffe.pb.h>
+
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/text_format.h>
+
+#include <chrono>
+#include <random>
+#include <iostream>
+
+int FillCommand::run(void) const
+{
+ caffe::NetParameter param;
+
+ // Read from standard input
+ google::protobuf::io::FileInputStream is{0};
+ if (!google::protobuf::TextFormat::Parse(&is, ¶m))
+ {
+ std::cerr << "ERROR: Failed to parse prototxt" << std::endl;
+ return 255;
+ }
+
+ auto net = NetworkBuilder{LayerResolver{}}.build(¶m);
+
+ uint32_t seed = std::chrono::system_clock::now().time_since_epoch().count();
+
+ // Allow users to override seed
+ {
+ char *env = std::getenv("SEED");
+
+ if (env)
+ {
+ seed = std::stoi(env);
+ }
+ }
+
+ std::cerr << "Use '" << seed << "' as seed" << std::endl;
+
+ // Create a random number generator
+ std::default_random_engine generator{seed};
+
+ // Randomize parameters
+ for (uint32_t n = 0; n < net->layers().size(); ++n)
+ {
+ net->layers().at(n).accept(ParameterRandomizePass{generator});
+ }
+
+ // Write to standard output
+ google::protobuf::io::FileOutputStream output(1);
+ google::protobuf::TextFormat::Print(param, &output);
+
+ return 0;
+}
--- /dev/null
+#ifndef __FILL_COMMAND_H__
+#define __FILL_COMMAND_H__
+
+#include "Command.h"
+
+struct FillCommand final : public Command
+{
+ int run(void) const override;
+};
+
+#endif // __FILL_COMMAND_H__
--- /dev/null
+#include "ParameterRandomizePass.h"
+
+ParameterRandomizePass::ParameterRandomizePass(std::default_random_engine &generator)
+ : _generator{generator}
+{
+ // DO NOTHING
+}
+
+void ParameterRandomizePass::visit(InputLayer &)
+{
+ // InputLayer has no parameter to be randomized
+ return;
+}
--- /dev/null
+#ifndef __PARAMETER_RANDOMIZE_PASS_H__
+#define __PARAMETER_RANDOMIZE_PASS_H__
+
+#include "LayerTransformPass.h"
+
+#include <random>
+
+class ParameterRandomizePass : public LayerTransformPass
+{
+public:
+ ParameterRandomizePass(std::default_random_engine &generator);
+
+public:
+ void visit(InputLayer &) override;
+
+private:
+ std::default_random_engine &_generator;
+};
+
+#endif // __PARAMETER_RANDOMIZE_PASS_H__