水水水水水水水水水水水水水水水水
立即下载
资源介绍:
水水水水水水水水水水水水水水水水
// Copyright (c) 2020 Horizon Robotics.All Rights Reserved.
//
// The material in this file is confidential and contains trade secrets
// of Horizon Robotics Inc. This is proprietary information owned by
// Horizon Robotics Inc. No part of this work may be disclosed,
// reproduced, copied, transmitted, or used in any way for any purpose,
// without the express written permission of Horizon Robotics Inc.
#include "layer/dequantize.h"
#include "test_op.h"
#include "gtest/gtest.h"
using hobot::dnn::NDArray;
using hobot::dnn::TShape;
using hobot::dnn::TypeFlag;
TEST(Dequantize, Dequantize_case1) {
std::cout << "input(24,24), int8->float32, scale" << std::endl;
hobot::dnn::Dequantize DequantizeOp;
hobot::dnn::VarientMap attr_dict;
attr_dict["num_args"] = 2;
DequantizeOp.Init(attr_dict);
std::vector bottom_blobs;
std::vector top_blobs;
std::vector x(24 * 24);
//std::srand(static_cast(std::time(nullptr)));
for (size_t i = 0; i < x.size(); ++i) {
x[i] = static_cast(static_cast(std::rand()) / static_cast(RAND_MAX) * 255 - 128);
}
std::vector scale = {1.5f};
hobot::dnn::TShape shape{24, 24};
hobot::dnn::TShape shape1{1};
std::unique_ptr x_nd(
new NDArray(x.data(), shape, TypeFlag::kInt8));
std::unique_ptr scale_nd(
new NDArray(scale.data(), shape1, TypeFlag::kFloat32));
std::unique_ptr y_nd(new NDArray(shape, TypeFlag::kFloat32));
bottom_blobs.push_back(x_nd.get());
bottom_blobs.push_back(scale_nd.get());
top_blobs.push_back(y_nd.get());
clock_t start_time = clock();
DequantizeOp.Forward(bottom_blobs, top_blobs);
clock_t end_time = clock();
double cost_time = (double) (end_time - start_time) / CLOCKS_PER_SEC * 1000;
std::cout << "cost time:" << cost_time << "ms" << std::endl;
#ifdef DNN_X86
std::string output_path = "./Output/Dequantize_case1.txt";
std::ofstream output_file(output_path);
ASSERT_TRUE(output_file.is_open()) << "Failed to open file " << output_path << " for writing.";
for (size_t i = 0; i < y_nd->Size(); ++i) {
output_file << std::to_string(y_nd->Dptr()[i]) << "\n";
//std::cout << std::to_string(y_nd->Dptr()[i]) << std::endl;
}
output_file.close();
std::cout << "Data saved to " << output_path << std::endl;
#else
std::string input_path = "./Output/Dequantize_case1.txt";
std::ifstream input_file(input_path);
ASSERT_TRUE(input_file.is_open()) << "Failed to open file " << input_path << " for reading.";
std::vector saved_data;
std::string value;
while (input_file >> value) {
saved_data.push_back(std::stof(value));
}
input_file.close();
std::vector current_data(y_nd->Dptr(), y_nd->Dptr() + y_nd->Size());
for (size_t i = 0; i < current_data.size(); ++i) {
ASSERT_NEAR(saved_data[i], current_data[i], 0.0001) << "Difference at index " << i << ": saved_data = " << saved_data[i]
<< ", current_data = " << current_data[i];
}
#endif
}
TEST(Dequantize, Dequantize_case2) {
std::cout << "input(24,24), int8->float32, scale_zero" << std::endl;
hobot::dnn::Dequantize DequantizeOp;
hobot::dnn::VarientMap attr_dict;
attr_dict["num_args"] = 3;
DequantizeOp.Init(attr_dict);
std::vector bottom_blobs;
std::vector top_blobs;
std::vector x(24 * 24);
//std::srand(static_cast(std::time(nullptr)));
for (size_t i = 0; i < x.size(); ++i) {
x[i] = static_cast(static_cast(std::rand()) / static_cast(RAND_MAX) * 255 - 128);
}
std::vector scale = {1.5f};
std::vector zero_point = {2};
hobot::dnn::TShape shape{24, 24};
hobot::dnn::TShape shape1{1};
std::unique_ptr x_nd(
new NDArray(x.data(), shape, TypeFlag::kInt8));
std::unique_ptr scale_nd(
new NDArray(scale.data(), shape1, TypeFlag::kFloat32));
std::unique_ptr zero_point_nd(
new NDArray(zero_point.data(), shape1, TypeFlag::kInt8));
std::unique_ptr y_nd(new NDArray(shape, TypeFlag::kFloat32));
bottom_blobs.push_back(x_nd.get());
bottom_blobs.push_back(scale_nd.get());
bottom_blobs.push_back(zero_point_nd.get());
top_blobs.push_back(y_nd.get());
clock_t start_time = clock();
DequantizeOp.Forward(bottom_blobs, top_blobs);
clock_t end_time = clock();
double cost_time = (double) (end_time - start_time) / CLOCKS_PER_SEC * 1000;
std::cout << "cost time:" << cost_time << "ms" << std::endl;
#ifdef DNN_X86
std::string output_path = "./Output/Dequantize_case2.txt";
std::ofstream output_file(output_path);
ASSERT_TRUE(output_file.is_open()) << "Failed to open file " << output_path << " for writing.";
for (size_t i = 0; i < y_nd->Size(); ++i) {
output_file << std::to_string(y_nd->Dptr()[i]) << "\n";
//std::cout << std::to_string(y_nd->Dptr()[i]) << std::endl;
}
output_file.close();
std::cout << "Data saved to " << output_path << std::endl;
#else
std::string input_path = "./Output/Dequantize_case2.txt";
std::ifstream input_file(input_path);
ASSERT_TRUE(input_file.is_open()) << "Failed to open file " << input_path << " for reading.";
std::vector saved_data;
std::string value;
while (input_file >> value) {
saved_data.push_back(std::stof(value));
}
input_file.close();
std::vector current_data(y_nd->Dptr(), y_nd->Dptr() + y_nd->Size());
for (size_t i = 0; i < current_data.size(); ++i) {
ASSERT_NEAR(saved_data[i], current_data[i], 0.0001) << "Difference at index " << i << ": saved_data = " << saved_data[i]
<< ", current_data = " << current_data[i];
}
#endif
}
TEST(Dequantize, Dequantize_case3) {
std::cout << "input(3,224,224), int16->float32, scale" << std::endl;
hobot::dnn::Dequantize DequantizeOp;
hobot::dnn::VarientMap attr_dict;
attr_dict["num_args"] = 2;
DequantizeOp.Init(attr_dict);
std::vector bottom_blobs;
std::vector top_blobs;
std::vector x(3 * 224 * 224);
//std::srand(static_cast(std::time(nullptr)));
for (size_t i = 0; i < x.size(); ++i) {
x[i] = static_cast(static_cast(std::rand()) / static_cast(RAND_MAX) * 2000 - 1000);
}
std::vector scale = {1.5f};
hobot::dnn::TShape shape{3, 224, 224};
hobot::dnn::TShape shape1{1};
std::unique_ptr x_nd(
new NDArray(x.data(), shape, TypeFlag::kInt16));
std::unique_ptr scale_nd(
new NDArray(scale.data(), shape1, TypeFlag::kFloat32));
std::unique_ptr y_nd(new NDArray(shape, TypeFlag::kFloat32));
bottom_blobs.push_back(x_nd.get());
bottom_blobs.push_back(scale_nd.get());
top_blobs.push_back(y_nd.get());
clock_t start_time = clock();
DequantizeOp.Forward(bottom_blobs, top_blobs);
clock_t end_time = clock();
double cost_time = (double) (end_time - start_time) / CLOCKS_PER_SEC * 1000;
std::cout << "cost time:" << cost_time << "ms" << std::endl;
#ifdef DNN_X86
std::string output_path = "./Output/Dequantize_case3.txt";
std::ofstream output_file(output_path);
ASSERT_TRUE(output_file.is_open()) << "Failed to open file " << output_path << " for writing.";
for (size_t i = 0; i < y_nd->Size(); ++i) {
output_file << std::to_string(y_nd->Dptr()[i]) << "\n";
//std::cout << std::to_string(y_nd->Dptr()[i]) << std::endl;
}
output_file.close();
std::cout << "Data saved to " << output_path << std::endl;
#else
std::string input_path = "./Output/Dequantize_case3.txt";
std::ifstream input_file(input_path);
ASSERT_TRUE(input_file.is_open()) << "Failed to open file " << input_path << " for reading.";
std::vector saved_data;
std::string value;
while (input_file >> value) {
saved_data.push_back(std::stof(value));
}
input_file.