2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// revertMNNModel.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/01/31.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2020-03-26 14:11:19 +08:00
|
|
|
#include <random>
|
2019-04-17 10:49:11 +08:00
|
|
|
#include <ctime>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <string.h>
|
2020-02-26 21:24:38 +08:00
|
|
|
#include <stdlib.h>
|
2019-12-27 22:16:57 +08:00
|
|
|
#include <MNN/MNNDefine.h>
|
2019-04-17 10:49:11 +08:00
|
|
|
#include "revertMNNModel.hpp"
|
|
|
|
|
|
|
|
const float MIN_VALUE = -2.0;
|
|
|
|
const float MAX_VALUE = 2.0;
|
|
|
|
|
|
|
|
Revert::Revert(const char* originalModelFileName) {
|
|
|
|
std::ifstream inputFile(originalModelFileName, std::ios::binary);
|
|
|
|
inputFile.seekg(0, std::ios::end);
|
|
|
|
const auto size = inputFile.tellg();
|
|
|
|
inputFile.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
char* buffer = new char[size];
|
|
|
|
inputFile.read(buffer, size);
|
|
|
|
inputFile.close();
|
|
|
|
mMNNNet = MNN::UnPackNet(buffer);
|
|
|
|
delete[] buffer;
|
|
|
|
MNN_ASSERT(mMNNNet->oplists.size() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Revert::~Revert() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Revert::getBuffer() const {
|
|
|
|
return reinterpret_cast<void*>(mBuffer.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t Revert::getBufferSize() const {
|
|
|
|
return mBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Revert::packMNNNet() {
|
|
|
|
flatbuffers::FlatBufferBuilder builder(1024);
|
|
|
|
auto offset = MNN::Net::Pack(builder, mMNNNet.get());
|
|
|
|
builder.Finish(offset);
|
|
|
|
mBufferSize = builder.GetSize();
|
2020-07-02 18:29:10 +08:00
|
|
|
mBuffer.reset(new uint8_t[mBufferSize], std::default_delete<uint8_t[]>());
|
2019-04-17 10:49:11 +08:00
|
|
|
::memcpy(mBuffer.get(), builder.GetBufferPointer(), mBufferSize);
|
|
|
|
mMNNNet.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Revert::initialize() {
|
|
|
|
if (mMNNNet->bizCode == "benchmark") {
|
|
|
|
randStart();
|
|
|
|
for (auto& op : mMNNNet->oplists) {
|
|
|
|
const auto opType = op->type;
|
|
|
|
switch (opType) {
|
|
|
|
case MNN::OpType_Convolution:
|
|
|
|
case MNN::OpType_Deconvolution:
|
|
|
|
case MNN::OpType_ConvolutionDepthwise: {
|
|
|
|
auto param = op->main.AsConvolution2D();
|
|
|
|
auto& convCommon = param->common;
|
|
|
|
const int weightSize = convCommon->kernelX * convCommon->kernelY * convCommon->outputCount *
|
|
|
|
convCommon->inputCount / convCommon->group;
|
|
|
|
param->weight.resize(weightSize);
|
|
|
|
::memset(param->weight.data(), 0, param->weight.size() * sizeof(float));
|
|
|
|
param->bias.resize(convCommon->outputCount);
|
|
|
|
::memset(param->bias.data(), 0, param->bias.size() * sizeof(float));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MNN::OpType_Scale: {
|
|
|
|
auto param = op->main.AsScale();
|
|
|
|
param->biasData.resize(param->channels);
|
|
|
|
param->scaleData.resize(param->channels);
|
|
|
|
for (int i = 0; i < param->channels; ++i) {
|
|
|
|
param->scaleData[i] = getRandValue();
|
|
|
|
param->biasData[i] = getRandValue();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
packMNNNet();
|
|
|
|
}
|
2020-03-26 14:11:19 +08:00
|
|
|
static std::random_device gDevice;
|
2019-04-17 10:49:11 +08:00
|
|
|
float Revert::getRandValue() {
|
2020-03-26 14:11:19 +08:00
|
|
|
return MIN_VALUE + (MAX_VALUE - MIN_VALUE) * gDevice() / RAND_MAX;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Revert::randStart() {
|
|
|
|
}
|