Skip to content

Commit

Permalink
Merge pull request #285 from mingmingtasd/example
Browse files Browse the repository at this point in the history
Refactor C++ examples
  • Loading branch information
fujunwei authored Aug 2, 2022
2 parents 6b27f08 + a0a2a48 commit 525a858
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 48 deletions.
7 changes: 0 additions & 7 deletions examples/MobileNetV2/MobileNetV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ bool MobileNetV2::ParseAndCheckExampleOptions(int argc, const char* argv[]) {
return true;
}

const wnn::Operand MobileNetV2::BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path) {
const cnpy::NpyArray data = cnpy::npy_load(path);
mConstants.push_back(data.data_holder);
return utils::BuildConstant(builder, data.shape, data.data<float>(), data.num_bytes());
}

const wnn::Operand MobileNetV2::BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
int32_t convIndex,
Expand Down
5 changes: 0 additions & 5 deletions examples/MobileNetV2/MobileNetV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class MobileNetV2 : public ExampleBase {
const wnn::Operand LoadNchw(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand LoadNhwc(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand LoadBatchNormNchw(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path);
const wnn::Operand BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
int32_t convIndex,
Expand Down Expand Up @@ -64,7 +62,4 @@ class MobileNetV2 : public ExampleBase {
const wnn::Operand BuildGemm(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
int32_t gemmIndex);

private:
std::vector<SHARED_DATA_TYPE> mConstants;
};
7 changes: 0 additions & 7 deletions examples/ResNet/ResNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ bool ResNet::ParseAndCheckExampleOptions(int argc, const char* argv[]) {
return true;
}

const wnn::Operand ResNet::BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path) {
const cnpy::NpyArray data = cnpy::npy_load(path);
mConstants.push_back(data.data_holder);
return utils::BuildConstant(builder, data.shape, data.data<float>(), data.num_bytes());
}

const wnn::Operand ResNet::BuildNchwConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
const std::string& name,
Expand Down
5 changes: 0 additions & 5 deletions examples/ResNet/ResNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class ResNet : public ExampleBase {
bool ParseAndCheckExampleOptions(int argc, const char* argv[]) override;
const wnn::Operand LoadNchw(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand LoadNhwc(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path);
const wnn::Operand BuildNchwConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
const std::string& name,
Expand Down Expand Up @@ -62,7 +60,4 @@ class ResNet : public ExampleBase {
const wnn::Operand loop(const wnn::GraphBuilder& builder,
const wnn::Operand node,
uint32_t num);

private:
std::vector<SHARED_DATA_TYPE> mConstants;
};
7 changes: 7 additions & 0 deletions examples/SampleUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ bool ExampleBase::ParseAndCheckExampleOptions(int argc, const char* argv[]) {
return true;
}

const wnn::Operand ExampleBase::BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path) {
const cnpy::NpyArray data = cnpy::npy_load(path);
mConstants.push_back(data.data_holder);
return utils::BuildConstant(builder, data.shape, data.data<float>(), data.num_bytes());
}

bool Expected(float output, float expected) {
return (fabs(output - expected) < 0.005f);
}
Expand Down
3 changes: 3 additions & 0 deletions examples/SampleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ExampleBase {
virtual ~ExampleBase() = default;

virtual bool ParseAndCheckExampleOptions(int argc, const char* argv[]);
const wnn::Operand BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path);

std::string mImagePath;
std::string mWeightsPath;
Expand All @@ -51,6 +53,7 @@ class ExampleBase {
std::string mDevicePreference = "default";
std::string mPowerPreference = "default";
bool mFused = true;
std::vector<SHARED_DATA_TYPE> mConstants;
};

wnn::Context CreateCppContext(wnn::ContextOptions const* options = nullptr);
Expand Down
7 changes: 0 additions & 7 deletions examples/SqueezeNet/SqueezeNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ bool SqueezeNet::ParseAndCheckExampleOptions(int argc, const char* argv[]) {
return true;
}

const wnn::Operand SqueezeNet::BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path) {
const cnpy::NpyArray data = cnpy::npy_load(path);
mConstants.push_back(data.data_holder);
return utils::BuildConstant(builder, data.shape, data.data<float>(), data.num_bytes());
}

const wnn::Operand SqueezeNet::BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
const std::string& name,
Expand Down
5 changes: 0 additions & 5 deletions examples/SqueezeNet/SqueezeNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class SqueezeNet : public ExampleBase {
bool ParseAndCheckExampleOptions(int argc, const char* argv[]) override;
const wnn::Operand LoadNchw(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand LoadNhwc(const wnn::GraphBuilder& builder, bool softmax = true);
const wnn::Operand BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path);
const wnn::Operand BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
const std::string& name,
Expand All @@ -36,7 +34,4 @@ class SqueezeNet : public ExampleBase {
const std::string& convName,
const std::string& conv1x1Name,
const std::string& conv3x3Name);

private:
std::vector<SHARED_DATA_TYPE> mConstants;
};
7 changes: 0 additions & 7 deletions examples/SuperResolution/SuperResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
SuperResolution::SuperResolution() : ExampleBase() {
}

const wnn::Operand SuperResolution::BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path) {
const cnpy::NpyArray data = cnpy::npy_load(path);
mConstants.push_back(data.data_holder);
return utils::BuildConstant(builder, data.shape, data.data<float>(), data.num_bytes());
}

const wnn::Operand SuperResolution::BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
int32_t convIndex,
Expand Down
5 changes: 0 additions & 5 deletions examples/SuperResolution/SuperResolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ class SuperResolution : public ExampleBase {
const wnn::Operand LoadNchw(const wnn::GraphBuilder& builder, bool softmax);

private:
const wnn::Operand BuildConstantFromNpy(const wnn::GraphBuilder& builder,
const std::string& path);

const wnn::Operand BuildConv(const wnn::GraphBuilder& builder,
const wnn::Operand& input,
int32_t convIndex,
bool relu6,
utils::Conv2dOptions* options,
const std::string& biasName = "");

std::vector<SHARED_DATA_TYPE> mConstants;
};

0 comments on commit 525a858

Please sign in to comment.