mirror of
https://github.com/lltcggie/waifu2x-caffe.git
synced 2025-06-28 06:32:47 +00:00
パラメータファイルの読み込みを高速化
This commit is contained in:
parent
097a3c8da3
commit
138e9f1c43
@ -255,6 +255,17 @@ Waifu2x::eWaifu2xError Waifu2x::CreateZoomColorImage(const cv::Mat &float_image,
|
|||||||
|
|
||||||
// 学習したパラメータをファイルから読み込む
|
// 学習したパラメータをファイルから読み込む
|
||||||
Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float>> net, const std::string ¶m_path)
|
Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float>> net, const std::string ¶m_path)
|
||||||
|
{
|
||||||
|
const std::string caffemodel_path = param_path + ".caffemodel";
|
||||||
|
|
||||||
|
FILE *fp = fopen(caffemodel_path.c_str(), "rb");
|
||||||
|
const bool isModelExist = fp != nullptr;
|
||||||
|
if (fp) fclose(fp);
|
||||||
|
|
||||||
|
caffe::NetParameter param;
|
||||||
|
if (isModelExist && caffe::ReadProtoFromBinaryFile(caffemodel_path, ¶m))
|
||||||
|
net->CopyTrainedLayersFrom(param);
|
||||||
|
else
|
||||||
{
|
{
|
||||||
rapidjson::Document d;
|
rapidjson::Document d;
|
||||||
std::vector<char> jsonBuf;
|
std::vector<char> jsonBuf;
|
||||||
@ -295,6 +306,9 @@ Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
std::vector<float> weightList;
|
||||||
|
std::vector<float> biasList;
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (auto it = d.Begin(); it != d.End(); ++it)
|
for (auto it = d.Begin(); it != d.End(); ++it)
|
||||||
{
|
{
|
||||||
@ -334,8 +348,10 @@ Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float
|
|||||||
if (!(b1->count() == bias.Size()))
|
if (!(b1->count() == bias.Size()))
|
||||||
return eWaifu2xError_FailedConstructModel;
|
return eWaifu2xError_FailedConstructModel;
|
||||||
|
|
||||||
|
weightList.resize(0);
|
||||||
|
biasList.resize(0);
|
||||||
|
|
||||||
size_t weightCount = 0;
|
size_t weightCount = 0;
|
||||||
std::vector<float> weightList;
|
|
||||||
for (auto it2 = weight.Begin(); it2 != weight.End(); ++it2)
|
for (auto it2 = weight.Begin(); it2 != weight.End(); ++it2)
|
||||||
{
|
{
|
||||||
for (auto it3 = (*it2).Begin(); it3 != (*it2).End(); ++it3)
|
for (auto it3 = (*it2).Begin(); it3 != (*it2).End(); ++it3)
|
||||||
@ -350,7 +366,6 @@ Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float
|
|||||||
|
|
||||||
caffe::caffe_copy(b0->count(), weightList.data(), b0Ptr);
|
caffe::caffe_copy(b0->count(), weightList.data(), b0Ptr);
|
||||||
|
|
||||||
std::vector<float> biasList;
|
|
||||||
for (auto it2 = bias.Begin(); it2 != bias.End(); ++it2)
|
for (auto it2 = bias.Begin(); it2 != bias.End(); ++it2)
|
||||||
biasList.push_back((float)it2->GetDouble());
|
biasList.push_back((float)it2->GetDouble());
|
||||||
|
|
||||||
@ -358,11 +373,16 @@ Waifu2x::eWaifu2xError Waifu2x::LoadParameter(boost::shared_ptr<caffe::Net<float
|
|||||||
|
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
net->ToProto(¶m);
|
||||||
|
|
||||||
|
caffe::WriteProtoToBinaryFile(param, caffemodel_path);
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
return eWaifu2xError_FailedConstructModel;
|
return eWaifu2xError_FailedConstructModel;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return eWaifu2xError_OK;
|
return eWaifu2xError_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user