@@ -0,0 +1,17 @@ | |||
cmake_minimum_required(VERSION 3.14.1) | |||
project(Ascend310Infer) | |||
find_package(OpenCV 2 REQUIRED) | |||
find_package(gflags REQUIRED) | |||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined | |||
-D_GLIBCXX_USE_CXX11_ABI=0") | |||
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) | |||
option(MINDSPORE_PATH "mindspore install path" "") | |||
include_directories(${OpenCV_INCLUDE_DIRS}) | |||
include_directories(${MINDSPORE_PATH}) | |||
message(STATUS "mindsporelibs:${MINDSPORE_PATH}") | |||
include_directories(${MINDSPORE_PATH}/include) | |||
include_directories(${PROJECT_SRC_ROOT}) | |||
find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) | |||
file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) | |||
add_executable(main src/main.cc src/utils.cc) | |||
target_link_libraries(main ${MS_LIB} ${MD_LIB} ${OpenCV_LIBS} gflags) |
@@ -1,3 +1,4 @@ | |||
#!/bin/bash | |||
# Copyright 2021 Huawei Technologies Co., Ltd | |||
# | |||
# Licensed under the Apache License, Version 2.0 (the "License"); | |||
@@ -12,25 +13,17 @@ | |||
# See the License for the specific language governing permissions and | |||
# limitations under the License. | |||
# ============================================================================ | |||
def _generate_steps_lr(lr_init, steps_per_epoch, total_epoch): | |||
""" | |||
Applies three steps decay to generate learning rate array. | |||
if [ -d out ]; then | |||
rm -rf out | |||
fi | |||
Args: | |||
lr_init(float): init learning rate. | |||
steps_per_epoch: per steps in an epoch. | |||
total_epoch: | |||
Returns: | |||
np.array, learning rate array. | |||
""" | |||
mkdir out | |||
cd out || exit | |||
total_steps = steps_per_epoch * total_epoch | |||
lr_each_step = [] | |||
for i in range(total_steps): | |||
a=i//steps_per_epoch | |||
if a <= 4: | |||
lr = lr_init | |||
elif a>4: | |||
lr= lr_init*0.01 | |||
lr_each_step.append(lr) | |||
return lr_each_step | |||
if [ -f "Makefile" ]; then | |||
make clean | |||
fi | |||
cmake .. \ | |||
-DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" | |||
make |
@@ -0,0 +1,42 @@ | |||
/** | |||
* Copyright 2021 Huawei Technologies Co., Ltd | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
#ifndef MINDSPORE_INFERENCE_UTILS_H_ | |||
#define MINDSPORE_INFERENCE_UTILS_H_ | |||
#include <opencv2/imgproc/types_c.h> | |||
#include <sys/stat.h> | |||
#include <dirent.h> | |||
#include <vector> | |||
#include <string> | |||
#include <memory> | |||
#include <opencv2/opencv.hpp> | |||
#include <opencv2/core/core.hpp> | |||
#include <opencv2/highgui/highgui.hpp> | |||
#include <opencv2/imgproc/imgproc.hpp> | |||
#include <opencv2/objdetect/objdetect.hpp> | |||
#include "include/api/types.h" | |||
std::vector<std::string> GetAllFiles(const std::string_view& dirName, const std::string& seq_name); | |||
DIR *OpenDir(const std::string_view& dirName); | |||
std::string RealPath(const std::string_view& path); | |||
mindspore::MSTensor ReadFileToTensor(const std::string &file); | |||
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs); | |||
cv::Mat BGRToRGB(const cv::Mat img); | |||
cv::Mat crop_and_pad(const cv::Mat &img, const float &cx, const float &cy, const float &size_z, const float &s_z); | |||
std::vector<double> Getpos(const std::string &dirName); | |||
float sumMat(const cv::Mat& inputImg); | |||
#endif |
@@ -0,0 +1,523 @@ | |||
/** | |||
* Copyright 2021 Huawei Technologies Co., Ltd | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
#include <dirent.h> | |||
#include <gflags/gflags.h> | |||
#include <opencv2/imgproc/types_c.h> | |||
#include <sys/time.h> | |||
#include <sstream> | |||
#include <string> | |||
#include <vector> | |||
#include <algorithm> | |||
#include <cmath> | |||
#include <fstream> | |||
#include <iosfwd> | |||
#include <iostream> | |||
#include <opencv2/core/core.hpp> | |||
#include <opencv2/highgui/highgui.hpp> | |||
#include <opencv2/imgproc/imgproc.hpp> | |||
#include <opencv2/objdetect/objdetect.hpp> | |||
#include <opencv2/opencv.hpp> | |||
#include "inc/utils.h" | |||
#include "include/api/context.h" | |||
#include "include/api/model.h" | |||
#include "include/api/serialization.h" | |||
#include "include/api/types.h" | |||
#include "include/dataset/execute.h" | |||
#include "include/dataset/transforms.h" | |||
#include "include/dataset/vision.h" | |||
#include "include/dataset/vision_ascend.h" | |||
using mindspore::Context; | |||
using mindspore::DataType; | |||
using mindspore::Graph; | |||
using mindspore::GraphCell; | |||
using mindspore::kSuccess; | |||
using mindspore::Model; | |||
using mindspore::ModelType; | |||
using mindspore::MSTensor; | |||
using mindspore::Serialization; | |||
using mindspore::Status; | |||
using mindspore::dataset::Execute; | |||
using mindspore::dataset::TensorTransform; | |||
using mindspore::dataset::transforms::TypeCast; | |||
using mindspore::dataset::vision::Decode; | |||
using mindspore::dataset::vision::HWC2CHW; | |||
using mindspore::dataset::vision::Normalize; | |||
using mindspore::dataset::vision::Resize; | |||
DEFINE_string(model_path1, "/home/siamfc/model1.mindir", "model path"); | |||
DEFINE_string(model_path2, "/home/siamfc/model2_change.mindir", "model path"); | |||
DEFINE_int32(device_id, 0, "device id"); | |||
DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision mode"); | |||
DEFINE_string(op_select_impl_mode, "", "op select impl mode"); | |||
DEFINE_string(aipp_path, "./aipp.cfg", "aipp path"); | |||
DEFINE_string(device_target, "Ascend310", "device target"); | |||
DEFINE_string(code_path, "/home/Siamfc/", "code path"); | |||
DEFINE_string(seq_root_path, "/home/siamfc/OTB2013/", "OTB route"); | |||
std::vector<std::string> all_videos = { | |||
"Basketball", "Bolt", "Boy", "Car4", "CarDark", | |||
"CarScale", "Coke", "Couple", "Crossing", "David", | |||
"David2", "David3", "Deer", "Dog1", "Doll", | |||
"Dudek", "FaceOcc1", "FaceOcc2", "Fish", "FleetFace", | |||
"Football", "Football1", "Football1", "Freeman1", "Freeman3", | |||
"Freeman4", "Girl", "Ironman", "Jogging", "Jumping", | |||
"Lemming", "Liquor", "Matrix", "Mhyang", "MotorRolling", | |||
"MountainBike", "Shaking", "Singer1", "Singer2", "Skating1", | |||
"Skiing", "Soccer", "Subway", "Suv", "Sylvester", | |||
"Tiger1", "Tiger2", "Trellis", "Walking", "Walking2", | |||
"Woman"}; | |||
struct param { | |||
const int* one = new int(1); | |||
size_t s_one = 4; | |||
size_t size_s; | |||
double init_x; | |||
double init_y; | |||
double init_w; | |||
double init_h; | |||
double target_position[2]; | |||
double target_sz[2]; | |||
double wc_z; | |||
double hc_z; | |||
double s_z; | |||
double scale_z; | |||
double penalty[3] = {0.9745, 1, 0.9745}; | |||
double scales[3] = {0.96385542, 1.00, 1.0375}; | |||
std::string dataset_path_txt; | |||
std::string record_name; | |||
std::string record_times; | |||
double s_x; | |||
double min_s_x; | |||
double max_s_x; | |||
double size_x_scales[3]; | |||
std::vector<double> box; | |||
std::vector<std::string> all_files; | |||
}; | |||
cv::Mat hwc2chw(Mat dst, size_t resize_detection) { | |||
std::vector<float> dst_data; | |||
std::vector<cv::Mat> bgrChannels(3); | |||
cv::split(dst, bgrChannels); | |||
for (size_t i = 0; i < bgrChannels.size(); i++) { | |||
std::vector<float> data = std::vector<float>(bgrChannels[i].reshape(1, 1)); | |||
dst_data.insert(dst_data.end(), data.begin(), data.end()); | |||
} | |||
cv::Mat srcMat; | |||
srcMat = cv::Mat(dst_data, true); | |||
cv::Mat dst_img = srcMat.reshape(3, resize_detection); | |||
return dst_img; | |||
} | |||
void pretreatment(const cv::Mat &src, cv::Mat *target, const param &config, const int &size, | |||
const double &s_x) { | |||
cv::Mat cropImg = crop_and_pad(src, config.target_position[0], | |||
config.target_position[1], size, s_x); | |||
cv::Mat exemplar_FLOAT; | |||
cropImg.convertTo(exemplar_FLOAT, CV_32FC3); | |||
*target = hwc2chw(exemplar_FLOAT, size); | |||
} | |||
void init_position(param *config, const std::string& temp_video) { | |||
config->all_files = GetAllFiles(FLAGS_seq_root_path, temp_video); | |||
config->box = Getpos(config->dataset_path_txt); | |||
config->size_s = config->all_files.size(); | |||
config->init_x = config->box[0] - 1; | |||
config->init_y = config->box[1] - 1; | |||
config->init_w = config->box[2]; | |||
config->init_h = config->box[3]; | |||
config->target_position[0] = config->init_x + (config->init_w - 1) / 2; | |||
config->target_position[1] = config->init_y + (config->init_h - 1) / 2; | |||
config->target_sz[0] = config->init_w; | |||
config->target_sz[1] = config->init_h; | |||
config->wc_z = config->init_w + 0.5 * (config->init_w + config->init_h); | |||
config->hc_z = config->init_h + 0.5 * (config->init_w + config->init_h); | |||
config->s_z = sqrt(config->wc_z * config->hc_z); | |||
config->scale_z = 127 / config->s_z; | |||
config->s_x = config->s_z + (255 - 127) / config->scale_z; | |||
config->min_s_x = 0.2 * config->s_x; | |||
config->max_s_x = 5 * config->s_x; | |||
} | |||
void getPath(param *config, const std::string& temp_video, int jogging_count) { | |||
config->dataset_path_txt = | |||
FLAGS_seq_root_path + "/" + temp_video + "/" + "groundtruth_rect.txt"; | |||
config->record_name = | |||
FLAGS_code_path + "/results/OTB2013/SiamFC/" + temp_video + ".txt"; | |||
config->record_times = FLAGS_code_path + "/results/OTB2013/SiamFC/times/" + | |||
temp_video + "_time.txt"; | |||
if (temp_video == "Jogging") { | |||
auto jogging_path = FLAGS_seq_root_path + "/" + temp_video + "/" + | |||
"groundtruth_rect" + "." + | |||
std::to_string(jogging_count) + ".txt"; | |||
auto jogging_record = FLAGS_code_path + "/results/OTB2013/SiamFC/" + | |||
temp_video + "." + std::to_string(jogging_count) + | |||
".txt"; | |||
config->dataset_path_txt = jogging_path; | |||
config->record_name = jogging_record; | |||
} | |||
} | |||
void getSizeScales(param *config) { | |||
for (int k = 0; k < 3; k++) { | |||
config->size_x_scales[k] = config->s_x * config->scales[k]; | |||
} | |||
} | |||
void getExemplar(const std::string& temp_video, std::vector<MSTensor> *outputs_exemplar, | |||
std::vector<MSTensor> *inputs_exemplar, Model *model1, | |||
param *config, int jogging_count) { | |||
getPath(config, temp_video, jogging_count); | |||
std::vector<MSTensor> model_inputs = model1->GetInputs(); | |||
init_position(config, temp_video); | |||
cv::Mat src = cv::imread(config->all_files[0], cv::IMREAD_COLOR); | |||
cv::Mat exemplar; | |||
pretreatment(src, &exemplar, config, 127, config->s_z); | |||
cout << "box :" << config->box[0] << " " << config->box[1] << " " | |||
<< config->box[2] << " " << config->box[3] << endl; | |||
size_t size_buffer = exemplar.size().width * exemplar.size().height * 4 * 3; | |||
mindspore::MSTensor image("x", mindspore::DataType::kNumberTypeFloat32, | |||
{static_cast<int64_t>(3), static_cast<int64_t>(127), | |||
static_cast<int64_t>(127)}, | |||
exemplar.data, size_buffer); | |||
inputs_exemplar->clear(); | |||
inputs_exemplar->emplace_back( | |||
model_inputs[0].Name(), model_inputs[0].DataType(), | |||
model_inputs[0].Shape(), image.Data().get(), image.DataSize()); | |||
inputs_exemplar->emplace_back( | |||
model_inputs[1].Name(), model_inputs[1].DataType(), | |||
model_inputs[1].Shape(), config->one, config->s_one); | |||
Status ret_instance; | |||
ret_instance = model1->Predict(inputs_exemplar, outputs_exemplar); // get exemplar img | |||
if (ret_instance != kSuccess) { | |||
cout << " Failed predict" << endl; | |||
} else { | |||
cout << " Success predict" << endl; | |||
} | |||
} | |||
void preInstance(std::vector<MSTensor> *input_exemplar, | |||
const std::vector<MSTensor>& outputs_exemplar, | |||
std::vector<MSTensor> *output_exemplar, | |||
const std::vector<MSTensor>& model_inputs_instance, Model *model2, | |||
const MSTensor& instance) { | |||
input_exemplar->clear(); | |||
input_exemplar->emplace_back( | |||
model_inputs_instance[0].Name(), model_inputs_instance[0].DataType(), | |||
model_inputs_instance[0].Shape(), outputs_exemplar[0].Data().get(), | |||
outputs_exemplar[0].DataSize()); | |||
input_exemplar->emplace_back(model_inputs_instance[1].Name(), model_inputs_instance[1].DataType(), | |||
model_inputs_instance[1].Shape(), instance.Data().get(), instance.DataSize()); | |||
model2->Predict(input_exemplar, output_exemplar); | |||
} | |||
void getRetInstance(int instance_num, const std::vector<MSTensor>& outputs_exemplar, const Mat &cos_window, | |||
param *config, Model *model2) { | |||
getSizeScales(config); | |||
std::vector<MSTensor> model_inputs_instance = model2->GetInputs(); | |||
cv::Mat instance_src; | |||
instance_src = cv::imread(config->all_files[instance_num], cv::IMREAD_COLOR); | |||
cv::Mat exemplar_img[3]; | |||
cv::Mat inputs_instance[3]; | |||
cv::Mat response_mapInit[3]; | |||
cv::Mat response_map[3]; | |||
double response_map_max[3]; | |||
std::vector<MSTensor> input_exemplar; | |||
std::vector<MSTensor> output_exemplar1; | |||
std::vector<MSTensor> output_exemplar2; | |||
std::vector<MSTensor> output_exemplar3; | |||
for (int n = 0; n < 3; n++) { | |||
pretreatment(instance_src, &exemplar_img[n], config, 255, config->size_x_scales[n]); | |||
} | |||
size_t size_buffer_instance = exemplar_img[0].size().width * exemplar_img[0].size().height * 3 * 4; | |||
mindspore::MSTensor instance1( | |||
"y", mindspore::DataType::kNumberTypeFloat32, | |||
{static_cast<int64_t>(3), static_cast<int64_t>(255), | |||
static_cast<int64_t>(255)}, | |||
exemplar_img[0].data, size_buffer_instance); | |||
mindspore::MSTensor instance2( | |||
"y", mindspore::DataType::kNumberTypeFloat32, | |||
{static_cast<int64_t>(3), static_cast<int64_t>(255), | |||
static_cast<int64_t>(255)}, | |||
exemplar_img[1].data, size_buffer_instance); | |||
mindspore::MSTensor instance3( | |||
"y", mindspore::DataType::kNumberTypeFloat32, | |||
{static_cast<int64_t>(3), static_cast<int64_t>(255), | |||
static_cast<int64_t>(255)}, | |||
exemplar_img[2].data, size_buffer_instance); | |||
preInstance(&input_exemplar, outputs_exemplar, &output_exemplar1, model_inputs_instance, model2, instance1); | |||
preInstance(&input_exemplar, outputs_exemplar, &output_exemplar2, model_inputs_instance, model2, instance2); | |||
preInstance(&input_exemplar, outputs_exemplar, &output_exemplar3, model_inputs_instance, model2, instance3); | |||
response_mapInit[0] = cv::Mat(17, 17, CV_32FC1, output_exemplar1[0].MutableData()); | |||
response_mapInit[1] = cv::Mat(17, 17, CV_32FC1, output_exemplar2[0].MutableData()); | |||
response_mapInit[2] = cv::Mat(17, 17, CV_32FC1, output_exemplar3[0].MutableData()); | |||
double minValue = 0; | |||
double maxValue = 0; | |||
for (int n = 0; n < 3; n++) { | |||
cv::resize(response_mapInit[n], response_map[n], Size(272, 272), 0, 0, | |||
cv::INTER_CUBIC); | |||
cv::minMaxIdx(response_map[n], &minValue, &maxValue, NULL, NULL); | |||
response_map_max[n] = maxValue * config->penalty[n]; | |||
} | |||
int scale_index = std::max_element(response_map_max, response_map_max + 3) - response_map_max; | |||
cv::Mat response_map_up = response_map[scale_index]; | |||
double minValue_response = 0; | |||
double maxValue_response = 0; | |||
cv::minMaxIdx(response_map_up, &minValue_response, &maxValue_response); | |||
response_map_up = response_map_up - minValue_response; | |||
Scalar sum_response = sum(response_map_up); | |||
response_map_up = response_map_up / sum_response[0]; | |||
response_map_up = (1 - 0.176) * response_map_up + 0.176 * cos_window; | |||
cv::minMaxIdx(response_map_up, &minValue_response, &maxValue_response); | |||
cv::Point maxLoc; | |||
cv::minMaxLoc(response_map_up, NULL, NULL, NULL, &maxLoc); | |||
double maxLoc_x = static_cast<double>(maxLoc.x); | |||
double maxLoc_y = static_cast<double>(maxLoc.y); | |||
maxLoc_x -= (271 / 2); | |||
maxLoc_y -= (271 / 2); | |||
maxLoc_x /= 2; | |||
maxLoc_y /= 2; | |||
double scale = config->scales[scale_index]; | |||
maxLoc_x = maxLoc_x * (config->s_x * scale) / 255; | |||
maxLoc_y = maxLoc_y * (config->s_x * scale) / 255; | |||
config->target_position[0] += maxLoc_x; | |||
config->target_position[1] += maxLoc_y; | |||
cout << " target_position[0]: " << config->target_position[0] | |||
<< " target_position[1]:" << config->target_position[1] << endl; | |||
config->s_x = (0.41 + 0.59 * scale) * config->s_x; | |||
config->s_x = max(config->min_s_x, min(config->max_s_x, config->s_x)); | |||
config->target_sz[0] = (0.41 + 0.59 * scale) * config->target_sz[0]; | |||
config->target_sz[1] = (0.41 + 0.59 * scale) * config->target_sz[1]; | |||
config->box[0] = config->target_position[0] + 1 - (config->target_sz[0]) / 2; | |||
config->box[1] = config->target_position[1] + 1 - (config->target_sz[1]) / 2; | |||
config->box[2] = config->target_sz[0]; | |||
config->box[3] = config->target_sz[1]; | |||
} | |||
void CreateHanningWindowWithCV_32F(Mat dst, const int &rows, const int &cols) { | |||
if (rows == 1 && cols == 1) { | |||
dst.at<float>(0, 0) = 1; | |||
} else if (rows == 1 && cols > 1) { | |||
float* dstData = dst.ptr<float>(0); | |||
for (int j = 0; j < cols; j++) { | |||
dstData[j] = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(j) / static_cast<double>(cols - 1))); | |||
} | |||
} else if (rows > 1 && cols == 1) { | |||
for (int i = 0; i < rows; i++) { | |||
float* dstData = dst.ptr<float>(i); | |||
dstData[0] = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(i) / static_cast<double>(rows - 1))); | |||
} | |||
} else { | |||
for (int i = 0; i < rows; i++) { | |||
float* dstData = dst.ptr<float>(i); | |||
double wr = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(i) / static_cast<double>(rows - 1))); | |||
for (int j = 0; j < cols; j++) { | |||
double wc = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(j) / static_cast<double>(cols - 1))); | |||
dstData[j] = static_cast<float>(wr * wc); | |||
} | |||
} | |||
sqrt(dst, dst); | |||
} | |||
} | |||
void CreateHanningWindowWithCV_64F(Mat dst, const int &rows, const int &cols) { | |||
if (rows == 1 && cols == 1) { | |||
dst.at<double>(0, 0) = 1; | |||
} else if (rows == 1 && cols > 1) { | |||
double* dstData = dst.ptr<double>(0); | |||
for (int j = 0; j < cols; j++) { | |||
dstData[j] = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(j) / static_cast<double>(cols - 1))); | |||
} | |||
} else if (rows > 1 && cols == 1) { | |||
for (int i = 0; i < rows; i++) { | |||
double* dstData = dst.ptr<double>(i); | |||
dstData[0] = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(i) / static_cast<double>(rows - 1))); | |||
} | |||
} else { | |||
for (int i = 0; i < rows; i++) { | |||
double* dstData = dst.ptr<double>(i); | |||
double wr = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(i) / static_cast<double>(rows - 1))); | |||
for (int j = 0; j < cols; j++) { | |||
double wc = | |||
0.5 * (1.0 - cos(2.0 * CV_PI * static_cast<double>(j) / static_cast<double>(cols - 1))); | |||
dstData[j] = static_cast<double>(wr * wc); | |||
} | |||
} | |||
sqrt(dst, dst); | |||
} | |||
} | |||
void myCreateHanningWindow(OutputArray _dst, cv::Size winSize, int type) { | |||
CV_Assert(type == CV_32FC1 || type == CV_64FC1); | |||
_dst.create(winSize, type); | |||
Mat dst = _dst.getMat(); | |||
int rows = dst.rows; | |||
int cols = dst.cols; | |||
if (dst.depth() == CV_32F) { | |||
CreateHanningWindowWithCV_32F(dst, rows, cols); | |||
} else { | |||
CreateHanningWindowWithCV_64F(dst, rows, cols); | |||
} | |||
} | |||
cv::Mat createMulHanningWindow(const cv::Size &winSize, int type) { | |||
int size1[2] = {1, winSize.width}; | |||
cv::Mat selfhanning1(1, size1, CV_32FC1, cv::Scalar(0)); | |||
myCreateHanningWindow(selfhanning1, cv::Size(1, winSize.width), CV_32FC1); | |||
int size2[2] = {winSize.height, 1}; | |||
cv::Mat selfhanning2(1, size2, CV_32FC1, cv::Scalar(0)); | |||
myCreateHanningWindow(selfhanning2, cv::Size(winSize.height, 1), CV_32FC1); | |||
cv::Mat mulHanning; | |||
mulHanning = selfhanning1 * selfhanning2; | |||
return mulHanning; | |||
} | |||
int Build(Model *model1, Model *model2) { | |||
if (RealPath(FLAGS_model_path1).empty()) { | |||
std::cout << "Invalid model" << std::endl; | |||
return 1; | |||
} | |||
auto context = std::make_shared<Context>(); | |||
auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>(); | |||
ascend310_info->SetDeviceID(FLAGS_device_id); | |||
context->MutableDeviceInfo().push_back(ascend310_info); | |||
// load graph1 | |||
Graph graph1; | |||
Status ret = Serialization::Load(FLAGS_model_path1, ModelType::kMindIR, &graph1); | |||
cout << "Load model success" << endl; | |||
if (ret != kSuccess) { | |||
std::cout << "Load model failed." << std::endl; | |||
return 1; | |||
} | |||
Status ret_build = model1->Build(GraphCell(graph1), context); | |||
if (ret_build != kSuccess) { | |||
std::cout << "ERROR: Build failed." << std::endl; | |||
return 1; | |||
} else { | |||
cout << "Build success " << endl; | |||
} | |||
// load graph2 | |||
Graph graph2; | |||
Status ret_graph2 = Serialization::Load(FLAGS_model_path2, ModelType::kMindIR, &graph2); | |||
if (ret_graph2 != kSuccess) { | |||
cout << " load graph2 failed" << endl; | |||
return 1; | |||
} else { | |||
cout << " load graph2 Success" << endl; | |||
} | |||
Status ret_build2 = model2->Build(GraphCell(graph2), context); | |||
if (ret_build2 != kSuccess) { | |||
cout << " build graph2 failed" << endl; | |||
return 1; | |||
} else { | |||
cout << " build graph2 Success" << endl; | |||
} | |||
auto all_files = GetAllFiles(FLAGS_seq_root_path, all_videos[0]); | |||
if (all_files.empty()) { | |||
std::cout << "ERROR: no input data." << std::endl; | |||
return 1; | |||
} | |||
return 0; | |||
} | |||
int main(int argc, char** argv) { | |||
gflags::ParseCommandLineFlags(&argc, &argv, true); | |||
Model model1; | |||
Model model2; | |||
if (Build(&model1, &model2) != 0) { | |||
cout << "Build model failed." << endl; | |||
return 1; | |||
} | |||
int jogging_count = 1; | |||
std::map<double, double> costTime_map; | |||
size_t size_v = all_videos.size(); | |||
for (size_t i = 0; i < size_v; ++i) { | |||
param config; | |||
std::vector<MSTensor> inputs_exemplar; | |||
std::vector<MSTensor> outputs_exemplar; | |||
struct timeval start, end; | |||
double startTime_ms, endTime_ms, useTime_ms; | |||
gettimeofday(&start, NULL); | |||
getExemplar(all_videos[i], &outputs_exemplar, &inputs_exemplar, &model1, &config, jogging_count); | |||
cout << "record:" << config.record_name << " " << config.record_times << endl; | |||
gettimeofday(&end, NULL); | |||
costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms)); | |||
ofstream outfile_record; | |||
ofstream outfile_times; | |||
outfile_times.open(config.record_times); | |||
outfile_record.open(config.record_name); | |||
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; | |||
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; | |||
useTime_ms = endTime_ms - startTime_ms; | |||
outfile_times << useTime_ms << std::endl; | |||
outfile_record << config.box[0] << "," << config.box[1] << "," << config.box[2] << "," << config.box[3] << endl; | |||
cv::Mat hann; | |||
hann = createMulHanningWindow(cv::Size(16 * 17, 16 * 17), CV_32FC1); | |||
Scalar sum_hann = sum(hann); | |||
cv::Mat cos_window = hann / sum_hann[0]; // create hanning | |||
// load graph2 | |||
for (size_t j = 1; j < config.size_s; j++) { | |||
gettimeofday(&start, NULL); | |||
getRetInstance(j, outputs_exemplar, cos_window, &config, model2); | |||
gettimeofday(&end, NULL); | |||
costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms)); | |||
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; | |||
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 100; | |||
useTime_ms = endTime_ms - startTime_ms; | |||
outfile_times << useTime_ms << std::endl; | |||
outfile_record << config.box[0] << "," << config.box[1] << "," << config.box[2] << "," << config.box[3] << endl; | |||
} | |||
if (all_videos[i] == "Jogging" && jogging_count == 1) { | |||
i--; | |||
jogging_count++; | |||
} | |||
} | |||
double average = 0.0; | |||
int infer_cnt = 0; | |||
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { | |||
double diff = 0.0; | |||
diff = iter->second - iter->first; | |||
average += diff; | |||
infer_cnt++; | |||
} | |||
average = average / infer_cnt; | |||
std::stringstream timeCost; | |||
timeCost << "NN inference cost average time: " << average << " ms of infer_count " << infer_cnt << std::endl; | |||
std::cout << "NN inference cost average time: " << average << "ms of infer_count " << infer_cnt << std::endl; | |||
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt"); | |||
std::ofstream file_stream(file_name.c_str(), std::ios::trunc); | |||
file_stream << timeCost.str(); | |||
file_stream.close(); | |||
costTime_map.clear(); | |||
return 0; | |||
} |
@@ -0,0 +1,256 @@ | |||
/** | |||
* Copyright 2021 Huawei Technologies Co., Ltd | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
#include "inc/utils.h" | |||
#include <dirent.h> | |||
#include <opencv2/imgproc/types_c.h> | |||
#include <std::string> | |||
#include <std::string_view> | |||
#include <sstream> | |||
#include <vector> | |||
#include <algorithm> | |||
#include <cmath> | |||
#include <fstream> | |||
#include <iostream> | |||
#include <opencv2/core/core.hpp> | |||
#include <opencv2/highgui/highgui.hpp> | |||
#include <opencv2/imgproc/imgproc.hpp> | |||
#include <opencv2/objdetect/objdetect.hpp> | |||
#include <opencv2/opencv.hpp> | |||
const int DAVID_DATA_SIZE = 471; | |||
const int DAVID_DATA_BEGIN = 299; | |||
const int DAVID_DATA_END = 770; | |||
const int FOOTBALL_DATA_SIZE = 74; | |||
const int FREEMAN3_DATA_SIZE = 460; | |||
const int FREEMAN4_DATA_SIZE = 283; | |||
const int DIVING_DATA_SIZE = 215; | |||
using mindspore::DataType; | |||
using mindspore::MSTensor; | |||
std::vector<std::string> GetAllFiles(const std::string_view& dirName, | |||
const std::string& seq_name) { | |||
struct dirent* filename; | |||
std::string seqName = std::string(dirName) + "/" + seq_name + "/img"; | |||
DIR* dir = OpenDir(seqName); | |||
if (dir == nullptr) { | |||
cout << "no dir" << endl; | |||
return {}; | |||
} | |||
std::vector<std::string> res; | |||
while ((filename = readdir(dir)) != nullptr) { | |||
std::string dName = std::string(filename->d_name); | |||
if (dName == "." || dName == ".." || filename->d_type != DT_REG) { | |||
continue; | |||
} | |||
res.emplace_back(std::string(dirName) + "/" + seq_name + "/img/" + | |||
filename->d_name); | |||
} | |||
std::sort(res.begin(), res.end()); | |||
std::vector<std::string> res_all; | |||
if (seq_name == "David") { | |||
res_all.resize(DAVID_DATA_SIZE); | |||
std::copy(res.begin() + DAVID_DATA_BEGIN, res.begin() + DAVID_DATA_END, | |||
res_all.begin()); | |||
} else if (seq_name == "Football1") { | |||
res_all.resize(FOOTBALL_DATA_SIZE); | |||
std::copy(res.begin(), res.begin() + FOOTBALL_DATA_SIZE, res_all.begin()); | |||
} else if (seq_name == "Freeman3") { | |||
res_all.resize(FREEMAN3_DATA_SIZE); | |||
std::copy(res.begin(), res.begin() + FREEMAN3_DATA_SIZE, res_all.begin()); | |||
} else if (seq_name == "Freeman4") { | |||
res_all.resize(FREEMAN4_DATA_SIZE); | |||
std::copy(res.begin(), res.begin() + FREEMAN4_DATA_SIZE, res_all.begin()); | |||
} else if (seq_name == "Diving") { | |||
res_all.resize(FREEMAN4_DATA_SIZE); | |||
std::copy(res.begin(), res.begin() + FREEMAN4_DATA_SIZE, res_all.begin()); | |||
} else { | |||
for (size_t i = 0; i < res.size(); i++) { | |||
res_all.emplace_back(res[i]); | |||
} | |||
} | |||
return res_all; | |||
} | |||
std::vector<double> Getpos(const std::string& dirName) { | |||
std::ifstream infile; | |||
infile.open(dirName.c_str()); | |||
std::string s; | |||
getline(infile, s); | |||
std::stringstream ss; | |||
ss << s; | |||
double temp; | |||
std::vector<double> data; | |||
while (ss >> temp) { | |||
data.push_back(temp); | |||
if (ss.peek() == ',' || ss.peek() == ' ' || ss.peek() == '\t') { | |||
ss.ignore(); | |||
} | |||
} | |||
infile.close(); | |||
return data; | |||
} | |||
int WriteResult(const std::string& imageFile, | |||
const std::vector<MSTensor>& outputs) { | |||
std::string homePath = "./result_Files"; | |||
for (size_t i = 0; i < outputs.size(); ++i) { | |||
size_t outputSize; | |||
std::shared_ptr<const void> netOutput = outputs[i].Data(); | |||
outputSize = outputs[i].DataSize(); | |||
int pos = imageFile.rfind('/'); | |||
std::string fileName(imageFile, pos + 1); | |||
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), | |||
'_' + std::to_std::string(i) + ".bin"); | |||
std::string outFileName = homePath + "/" + fileName; | |||
FILE* outputFile = fopen(outFileName.c_str(), "wb"); | |||
fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); | |||
fclose(outputFile); | |||
outputFile = nullptr; | |||
} | |||
return 0; | |||
} | |||
mindspore::MSTensor ReadFileToTensor(const std::string& file) { | |||
if (file.empty()) { | |||
std::cout << "Pointer file is nullptr" << std::endl; | |||
return mindspore::MSTensor(); | |||
} | |||
std::ifstream ifs(file); | |||
if (!ifs.good()) { | |||
std::cout << "File: " << file << " is not exist" << std::endl; | |||
return mindspore::MSTensor(); | |||
} | |||
if (!ifs.is_open()) { | |||
std::cout << "File: " << file << "open failed" << std::endl; | |||
return mindspore::MSTensor(); | |||
} | |||
ifs.seekg(0, std::ios::end); | |||
size_t size = ifs.tellg(); | |||
mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, | |||
{static_cast<int64_t>(size)}, nullptr, size); | |||
ifs.seekg(0, std::ios::beg); | |||
ifs.read(reinterpret_cast<char*>(buffer.MutableData()), size); | |||
ifs.close(); | |||
return buffer; | |||
} | |||
DIR* OpenDir(const std::string_view& dirName) { | |||
if (dirName.empty()) { | |||
std::cout << " dirName is null ! " << std::endl; | |||
return nullptr; | |||
} | |||
std::string realPath = RealPath(dirName); | |||
struct stat s; | |||
lstat(realPath.c_str(), &s); | |||
if (!S_ISDIR(s.st_mode)) { | |||
std::cout << "dirName is not a valid directory !" << std::endl; | |||
return nullptr; | |||
} | |||
DIR* dir = opendir(realPath.c_str()); | |||
if (dir == nullptr) { | |||
std::cout << "Can not open dir " << dirName << std::endl; | |||
return nullptr; | |||
} | |||
std::cout << "Successfully opened the dir " << dirName << std::endl; | |||
return dir; | |||
} | |||
std::string RealPath(const std::string_view& path) { | |||
char realPathMem[PATH_MAX] = {0}; | |||
char* realPathRet = nullptr; | |||
realPathRet = realpath(path.data(), realPathMem); | |||
if (realPathRet == nullptr) { | |||
std::cout << "File: " << path << " is not exist."; | |||
return ""; | |||
} | |||
std::string realPath(realPathMem); | |||
std::cout << path << " realpath is: " << realPath << std::endl; | |||
return realPath; | |||
} | |||
cv::Mat BGRToRGB(const cv::Mat& img) { | |||
cv::Mat image(img.rows, img.cols, CV_8UC3); | |||
for (int i = 0; i < img.rows; ++i) { | |||
cv::Vec3b* p1 = img.ptr<cv::Vec3b>(i); | |||
cv::Vec3b* p2 = image.ptr<cv::Vec3b>(i); | |||
for (int j = 0; j < img.cols; ++j) { | |||
p2[j][2] = p1[j][0]; | |||
p2[j][1] = p1[j][1]; | |||
p2[j][0] = p1[j][2]; | |||
} | |||
} | |||
return image; | |||
} | |||
cv::Mat crop_and_pad(const cv::Mat &img, const float &cx, const float &cy, const float &size_z, const float &s_z) { | |||
float xmin = cx - s_z / 2; | |||
float xmax = cx + s_z / 2; | |||
float ymin = cy - s_z / 2; | |||
float ymax = cy + s_z / 2; | |||
int w = img.cols; | |||
int h = img.rows; | |||
int left = 0; | |||
int right = 0; | |||
int top = 0; | |||
int bottom = 0; | |||
if (xmin < 0) left = static_cast<int>(abs(xmin)); | |||
if (xmax > w) right = static_cast<int>(xmax - w); | |||
if (ymin < 0) top = static_cast<int>(abs(ymin)); | |||
if (ymax > h) bottom = static_cast<int>(ymax - h); | |||
xmin = std::max(0, static_cast<int>(xmin)); | |||
xmax = std::min(w, static_cast<int>(xmax)); | |||
ymin = std::max(0, static_cast<int>(ymin)); | |||
ymax = std::min(h, static_cast<int>(ymax)); | |||
cv::Mat im_patch = img(cv::Range(ymin, ymax), cv::Range(xmin, xmax)); | |||
if (left != 0 || right != 0 || top != 0 || bottom != 0) { | |||
cv::Scalar tempVal = cv::mean(img); | |||
tempVal.val[0] = static_cast<int>(tempVal.val[0]); | |||
tempVal.val[1] = static_cast<int>(tempVal.val[1]); | |||
tempVal.val[2] = static_cast<int>(tempVal.val[2]); | |||
cv::copyMakeBorder(im_patch, im_patch, top, bottom, left, right, | |||
cv::BORDER_CONSTANT, tempVal); | |||
} | |||
if (size_z != s_z) { | |||
cv::resize(im_patch, im_patch, cv::Size(size_z, size_z)); | |||
} | |||
return im_patch; | |||
} | |||
float sumMat(const cv::Mat& inputImg) { | |||
float sum = 0.0; | |||
int rowNumber = inputImg.rows; | |||
int colNumber = inputImg.cols * inputImg.channels(); | |||
for (int i = 0; i < rowNumber; i++) { | |||
uchar* data = inputImg.ptr<uchar>(i); | |||
for (int j = 0; j < colNumber; j++) { | |||
sum = data[j] + sum; | |||
} | |||
} | |||
return sum; | |||
} |
@@ -1,137 +0,0 @@ | |||
# Copyright 2021 Huawei Technologies Co., Ltd | |||
# | |||
# Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | |||
# You may obtain a copy of the License at | |||
# | |||
# http://www.apache.org/licenses/LICENSE-2.0 | |||
# | |||
# Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | |||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
# See the License for the specific language governing permissions and | |||
# limitations under the License. | |||
# ============================================================================ | |||
import pickle | |||
import os | |||
import cv2 | |||
import functools | |||
import xml.etree.ElementTree as ET | |||
import sys | |||
import argparse | |||
import multiprocessing | |||
from multiprocessing import Pool | |||
from tqdm import tqdm | |||
from glob import glob | |||
from src import config,get_instance_image | |||
sys.path.append(os.getcwd()) | |||
multiprocessing.set_start_method('spawn', True) # 开启多线程 | |||
def worker(output_dir, video_dir): | |||
image_names = glob(os.path.join(video_dir, '*.JPEG')) # match所有video_dir下的符合的JPEG格式 | |||
# sort函数 | |||
# sorted()作用于任意可以迭代的对象,而sort()一般作用于列表; | |||
# sort()函数不需要复制原有列表,消耗的内存较少,效率也较高: b=sorted(a)并不改变a的排序,a.sort() 会改变a的排序 | |||
# sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) #key依据某一列为排序依据 | |||
#image_names = sorted(image_names, key=lambda x: int(x.split('/')[-1].split('.')[0])) # 从小到大进行排列 | |||
video_name = video_dir.split('/')[-1] | |||
save_folder = os.path.join(output_dir, video_name) | |||
if not os.path.exists(save_folder): | |||
os.mkdir(save_folder) | |||
trajs = {} | |||
for image_name in image_names: | |||
img = cv2.imread(image_name) | |||
# axis=0,表示shape第0个元素被压缩成1,即求每一列的平均值,axis=1,表示输出矩阵是1列(shape第一个元素被压缩成1),求每一行的均值,axis=(0,1)表示shape的第0个元素和第1个元素被压缩成了1 | |||
# 元组和列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组的创建很简单,只需要在括号中添加元素,并使用逗号间隔开 | |||
# map(int, img.mean(axis=(0, 1)))将数据全部转换为int类型列表 | |||
img_mean = tuple(map(int, img.mean(axis=(0, 1)))) | |||
anno_name = image_name.replace('Data', 'Annotations') # str.replace('a','b')将str中的a替换为字符串中的b | |||
anno_name = anno_name.replace('JPEG', 'xml') | |||
tree = ET.parse(anno_name) # 解析xml文件 | |||
root = tree.getroot() # 获取根节点; 作为一个元素,root有一个标签和一个属性字典,它也有子节点,for child in root | |||
bboxes = [] | |||
filename = root.find('filename').text # 查找指定标签的文本内容,对于任何标签都可以有三个特征,标签名root.tag,标签属性root.attrib,标签的文本内容root.text | |||
for obj in root.iter('object'): # 迭代所有的object属性 | |||
bbox = obj.find( | |||
'bndbox') # 找到objecet中的 boundbox 坐标值 | |||
bbox = list(map(int, [bbox.find('xmin').text, | |||
bbox.find('ymin').text, | |||
bbox.find('xmax').text, | |||
bbox.find('ymax').text])) | |||
trkid = int(obj.find('trackid').text) | |||
if trkid in trajs: | |||
trajs[trkid].append(filename) # 如果已经存在,就append | |||
else: # 添加 | |||
trajs[trkid] = [filename] | |||
instance_img, _, _ = get_instance_image(img, bbox, | |||
config.exemplar_size, config.instance_size, config.context_amount, | |||
img_mean) | |||
instance_img_name = os.path.join(save_folder, filename + ".{:02d}.x.jpg".format(trkid)) | |||
cv2.imwrite(instance_img_name, instance_img) | |||
return video_name, trajs | |||
def processing(data_dir, output_dir, num_threads=32): | |||
# get all 4417 videos | |||
video_dir = os.path.join(data_dir, 'Data/VID') # ./data/ILSVRC2015/Data/VID | |||
# 获取训练集和测试集的路径 from glob import glob = glob.glob | |||
all_videos = glob(os.path.join(video_dir, 'train/ILSVRC2015_VID_train_0000/*')) + \ | |||
glob(os.path.join(video_dir, 'train/ILSVRC2015_VID_train_0001/*')) + \ | |||
glob(os.path.join(video_dir, 'train/ILSVRC2015_VID_train_0002/*')) + \ | |||
glob(os.path.join(video_dir, 'train/ILSVRC2015_VID_train_0003/*')) + \ | |||
glob(os.path.join(video_dir, 'val/*')) | |||
#all_videos = sorted(all_videos,key=lambda x: int(x.split('/')[-1].split('_')[-1])) | |||
meta_data = [] | |||
if not os.path.exists(output_dir): | |||
os.makedirs(output_dir) | |||
# 开启并行运算 | |||
""" | |||
funtional.partial | |||
可调用的partial对象,使用方法是partial(func,*args,**kw),func是必须要传入的 | |||
imap和imap_unordered都用于对于大量数据遍历的多进程计算 | |||
imap返回结果顺序和输入相同,imap_unordered则为不保证顺序 imap_unordered的返回迭代器的结果的排序是任意的, | |||
example: | |||
logging.info(pool.map(fun, range(10))) #把10个数放进函数里面,函数返回一个列表的结果 | |||
pool.imap(fun,range(10))#把10个数放进fun里面,函数返回一个IMapIterator对象,每遍历一次,得到一个结果(进程由操作系统分配) | |||
pool.imap_unordered(fun,range(10)) #把10个数放进fun,函数返回一个IMapUnorderedIterator对象,每遍历一次得到一个结果 | |||
Pool使用完毕后必须关闭,否则进程不会退出。有两种写法: | |||
(1) | |||
iter = pool.imap(func, iter) | |||
for ret in iter: | |||
#do something | |||
(2)推荐 | |||
注意,第二种中,必须在with的块内使用iter | |||
with Pool() as pool: | |||
iter = pool.imap(func, iter) | |||
for ret in iter: | |||
#do something | |||
""" | |||
with Pool(processes=num_threads) as pool: | |||
for ret in tqdm(pool.imap_unordered(functools.partial(worker, output_dir), all_videos), | |||
total=len(all_videos)): | |||
meta_data.append(ret) # 通过这种方式可以使得进程退出 | |||
pickle.dump(meta_data, open(os.path.join(output_dir, "meta_data.pkl"), 'wb')) | |||
Data_dir = '/data/VID/ILSVRC2015' | |||
Output_dir = '/data/VID/ILSVRC_VID_CURATION_train' | |||
Num_threads = 32 # 原来设置为32 | |||
if __name__ == '__main__': | |||
# parse arguments | |||
parser = argparse.ArgumentParser(description="Demo SiamFC") | |||
parser.add_argument('--d', default=Data_dir, type=str, help="data_dir") | |||
parser.add_argument('--o', default=Output_dir, type=str, help="out put") | |||
parser.add_argument('--t', default=Num_threads, type=int, help="thread_num") | |||
args = parser.parse_args() | |||
processing(args.d, args.o, args.t) | |||
''' | |||
原来都是使用argparse库进行命令行解析,需要在python文件的开头需要大量的代码设定各个命令行参数。 | |||
而使用fire库不需要在python文件中设定命令行参数的代码,shell中指定函数名和对应参数即可 | |||
train.py | |||
def train(a,b): | |||
return a + b | |||
第一种调用方法 | |||
train 1 2 | |||
第二种调用方式 | |||
train --a 1 --b 2 | |||
''' |
@@ -12,63 +12,39 @@ | |||
# See the License for the specific language governing permissions and | |||
# limitations under the License. | |||
# ============================================================================ | |||
"""start eval """ | |||
from __future__ import absolute_import | |||
import argparse | |||
import os | |||
import sys | |||
sys.path.append(os.getcwd()) | |||
from got10k.experiments import * | |||
from src import SiamFCTracker | |||
from got10k.experiments import ExperimentOTB | |||
from mindspore import context | |||
import argparse | |||
from src import SiamFCTracker | |||
sys.path.append(os.getcwd()) | |||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser(description='siamfc tracking') | |||
parser.add_argument('--model_path', default='/root/HRBEU-MedAI/renwenhao2/models/siamfc_{}.ckpt/SiamFC_165-41_6650.ckpt' | |||
parser.add_argument('--device_id', type=int, default=7 | |||
, help='device id of GPU or Ascend') | |||
parser.add_argument('--model_path', default='/root/models/siamfc_{}.ckpt/SiamFC_177-47_6650.ckpt' | |||
, type=str, help='eval one special video') | |||
parser.add_argument('--dataset_path', default='/root/datasets/OTB2013', type=str) | |||
args = parser.parse_args() | |||
context.set_context( | |||
mode=context.PYNATIVE_MODE, | |||
device_id=5, | |||
mode=context.GRAPH_MODE, | |||
device_id=args.device_id, | |||
save_graphs=False, | |||
device_target='Ascend') | |||
tracker = SiamFCTracker(model_path=args.model_path) # 初始化一个追踪器 | |||
tracker = SiamFCTracker(model_path=args.model_path) | |||
root_dir = os.path.abspath('datasets/OTB2013') | |||
root_dir = os.path.abspath(args.dataset_path) | |||
e = ExperimentOTB(root_dir, version=2013) | |||
# root_dir = os.path.abspath('datasets/OTB') | |||
# e = ExperimentOTB(root_dir, version=2015) | |||
# root_dir = os.path.abspath('datasets/UAV123') | |||
# e = ExperimentUAV123(root_dir, version='UAV123') | |||
# root_dir = os.path.abspath('datasets/UAV123') | |||
# e = ExperimentUAV123(root_dir, version='UAV20L') | |||
# root_dir = os.path.abspath('datasets/DTB70') | |||
# e = ExperimentDTB70(root_dir) | |||
# root_dir = os.path.abspath('datasets/UAVDT') | |||
# e = ExperimentUAVDT(root_dir) | |||
# root_dir = os.path.abspath('datasets/VisDrone') | |||
# e = ExperimentVisDrone(root_dir) | |||
# root_dir = os.path.abspath('datasetssets/VOT2018') | |||
# e = ExperimentVOT(root_dir,version=2018) | |||
# root_dir = os.path.abspath('datasets/VOT2016') | |||
# e = ExperimentVOT(root_dir,version=2016) | |||
# root_dir = os.path.abspath('datasets/TColor128') | |||
# e = ExperimentTColor128(root_dir) | |||
# root_dir = os.path.abspath('datasets/Nfs') | |||
# e = ExperimentNfS(root_dir,fps=240) #高帧率 | |||
# root_dir = os.path.abspath('datasets/LaSOT') | |||
# e = ExperimentLaSOT(root_dir) | |||
e.run(tracker,visualize=False)#默认不开启可视化 | |||
e.run(tracker, visualize=False) | |||
prec_score, succ_score, succ_rate = e.report([tracker.name]) | |||
ss = '-prec_score:%.3f -succ_score:%.3f -succ_rate:%.3f' % (float(prec_score), float(succ_score), float(succ_rate)) | |||
ss = '-prec_score:%.3f -succ_score:%.3f -succ_rate:%.3f' % (float(prec_score), | |||
float(succ_score), | |||
float(succ_rate)) | |||
print(args.model_path.split('/')[-1], ss) |
@@ -0,0 +1,54 @@ | |||
# Copyright 2021 Huawei Technologies Co., Ltd | |||
# | |||
# Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | |||
# You may obtain a copy of the License at | |||
# | |||
# http://www.apache.org/licenses/LICENSE-2.0 | |||
# | |||
# Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | |||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
# See the License for the specific language governing permissions and | |||
# limitations under the License. | |||
# ============================================================================ | |||
"""export checkpoint file into models""" | |||
import argparse | |||
import numpy as np | |||
import mindspore as ms | |||
from mindspore import Tensor, context | |||
from mindspore.train.serialization import load_checkpoint, export, load_param_into_net | |||
from src.alexnet import SiameseAlexNet | |||
parser = argparse.ArgumentParser(description='siamfc export') | |||
parser.add_argument("--device_id", type=int, default=7, help="Device id") | |||
parser.add_argument('--model_path', default='/root/models/siamfc_{}.ckpt/SiamFC_177-47_6650.ckpt' | |||
, type=str, help='eval one special video') | |||
parser.add_argument('--file_name_export1', type=str, default='/root/SiamFC/models1', | |||
help='SiamFc output file name.') | |||
parser.add_argument('--file_name_export2', type=str, default='/root/SiamFC/models2', | |||
help='SiamFc output file name.') | |||
parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='MINDIR', help='file format') | |||
parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", | |||
help="device target") | |||
args = parser.parse_args() | |||
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) | |||
if args.device_target == "Ascend": | |||
context.set_context(device_id=args.device_id) | |||
if __name__ == "__main__": | |||
net1 = SiameseAlexNet(train=False) | |||
load_param_into_net(net1, load_checkpoint(args.model_path), strict_load=True) | |||
net1.set_train(False) | |||
net2 = SiameseAlexNet(train=False) | |||
load_param_into_net(net2, load_checkpoint(args.model_path), strict_load=True) | |||
net2.set_train(False) | |||
input_data_exemplar1 = Tensor(np.zeros([1, 3, 127, 127]), ms.float32) | |||
input_data_instance1 = Tensor(np.zeros(1), ms.float32) | |||
input_data_exemplar2 = Tensor(np.ones([1, 256, 6, 6]), ms.float32) | |||
input_data_instance2 = Tensor(np.ones([1, 3, 255, 255]), ms.float32) | |||
input1 = [input_data_exemplar1, input_data_instance1] | |||
input2 = [input_data_exemplar2, input_data_instance2] | |||
export(net1, *input1, file_name=args.file_name_export1, file_format=args.file_format) | |||
export(net2, *input2, file_name=args.file_name_export2, file_format=args.file_format) | |||
print("-- complete --") |
@@ -1,12 +0,0 @@ | |||
from __future__ import absolute_import | |||
from .got10k import GOT10k | |||
from .otb import OTB | |||
from .vot import VOT | |||
from .dtb70 import DTB70 | |||
from .tcolor128 import TColor128 | |||
from .uav123 import UAV123 | |||
from .nfs import NfS | |||
from .lasot import LaSOT | |||
from .trackingnet import TrackingNet | |||
from .vid import ImageNetVID |
@@ -1,67 +0,0 @@ | |||
from __future__ import absolute_import, print_function | |||
import os | |||
import glob | |||
import numpy as np | |||
import six | |||
class DTB70(object): | |||
"""`DTB70 <https://github.com/flyers/drone-tracking>`_ Dataset. | |||
Publication: | |||
``Visual object tracking for unmanned aerial vehicles: A benchmark and new motion models``, | |||
Y. Wu, J. Lim and M.-H. Yang, IEEE TPAMI 2015. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
""" | |||
def __init__(self, root_dir): | |||
super(DTB70, self).__init__() | |||
self.root_dir = root_dir | |||
self._check_integrity(root_dir) | |||
self.anno_files = sorted(glob.glob( | |||
os.path.join(root_dir, '*/groundtruth_rect.txt'))) | |||
self.seq_dirs = [os.path.dirname(f) for f in self.anno_files] | |||
self.seq_names = [os.path.basename(d) for d in self.seq_dirs] | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno), where ``img_files`` is a list of | |||
file names and ``anno`` is a N x 4 (rectangles) numpy array. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
img_files = sorted(glob.glob( | |||
os.path.join(self.seq_dirs[index], 'img/*.jpg'))) | |||
anno = np.loadtxt(self.anno_files[index], delimiter=',') | |||
assert len(img_files) == len(anno) | |||
assert anno.shape[1] == 4 | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
def _check_integrity(self, root_dir): | |||
seq_names = os.listdir(root_dir) | |||
seq_names = [n for n in seq_names if not n[0] == '.'] | |||
if os.path.isdir(root_dir) and len(seq_names) > 0: | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted.') |
@@ -1,107 +0,0 @@ | |||
from __future__ import absolute_import, print_function | |||
import os | |||
import glob | |||
import numpy as np | |||
import six | |||
class GOT10k(object): | |||
r"""`GOT-10K <http://got-10k.aitestunion.com//>`_ Dataset. | |||
Publication: | |||
``GOT-10k: A Large High-Diversity Benchmark for Generic Object | |||
Tracking in the Wild``, L. Huang, X. Zhao and K. Huang, ArXiv 2018. | |||
Args: | |||
root_dir (string): Root directory of dataset where ``train``, | |||
``val`` and ``test`` folders exist. | |||
subset (string, optional): Specify ``train``, ``val`` or ``test`` | |||
subset of GOT-10k. | |||
return_meta (string, optional): If True, returns ``meta`` | |||
of each sequence in ``__getitem__`` function, otherwise | |||
only returns ``img_files`` and ``anno``. | |||
""" | |||
def __init__(self, root_dir, subset='test', return_meta=False): | |||
super(GOT10k, self).__init__() | |||
assert subset in ['train', 'val', 'test'], 'Unknown subset.' | |||
self.root_dir = root_dir | |||
self.subset = subset | |||
self.return_meta = False if subset == 'test' else return_meta | |||
self._check_integrity(root_dir, subset) | |||
list_file = os.path.join(root_dir, subset, 'list.txt') #路径的拼接 | |||
with open(list_file, 'r') as f: | |||
self.seq_names = f.read().strip().split('\n') #按行读取所有的视频文件夹名 | |||
self.seq_dirs = [os.path.join(root_dir, subset, s) #获取所有视频文件的绝对路径 | |||
for s in self.seq_names] | |||
self.anno_files = [os.path.join(d, 'groundtruth.txt') | |||
for d in self.seq_dirs] | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno) if ``return_meta`` is False, otherwise | |||
(img_files, anno, meta), where ``img_files`` is a list of | |||
file names, ``anno`` is a N x 4 (rectangles) numpy array, while | |||
``meta`` is a dict contains meta information about the sequence. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
img_files = sorted(glob.glob(os.path.join( | |||
self.seq_dirs[index], '*.jpg'))) | |||
anno = np.loadtxt(self.anno_files[index], delimiter=',') | |||
if self.subset == 'test' and anno.ndim == 1: | |||
assert len(anno) == 4 | |||
anno = anno[np.newaxis, :] | |||
else: | |||
assert len(img_files) == len(anno) | |||
if self.return_meta: | |||
meta = self._fetch_meta(self.seq_dirs[index]) | |||
return img_files, anno, meta | |||
else: | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
def _check_integrity(self, root_dir, subset): | |||
assert subset in ['train', 'val', 'test'] | |||
list_file = os.path.join(root_dir, subset, 'list.txt') | |||
if os.path.isfile(list_file): | |||
with open(list_file, 'r') as f: | |||
seq_names = f.read().strip().split('\n') | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, subset, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted.') | |||
def _fetch_meta(self, seq_dir): | |||
# meta information | |||
meta_file = os.path.join(seq_dir, 'meta_info.ini') | |||
with open(meta_file) as f: | |||
meta = f.read().strip().split('\n')[1:] | |||
meta = [line.split(': ') for line in meta] | |||
meta = {line[0]: line[1] for line in meta} | |||
# attributes | |||
attributes = ['cover', 'absence', 'cut_by_image'] | |||
for att in attributes: | |||
meta[att] = np.loadtxt(os.path.join(seq_dir, att + '.label')) | |||
return meta |
@@ -1,121 +0,0 @@ | |||
from __future__ import absolute_import, print_function | |||
import os | |||
import glob | |||
import json | |||
import numpy as np | |||
import six | |||
class LaSOT(object): | |||
"""`LaSOT <https://cis.temple.edu/lasot/>`_ Datasets. | |||
Publication: | |||
``LaSOT: A High-quality Benchmark for Large-scale Single Object Tracking``, | |||
H. Fan, L. Lin, F. Yang, P. Chu, G. Deng, S. Yu, H. Bai | |||
Y. Xu, C. Liao, and H. Ling., CVPR 2019. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
subset (string, optional): Specify ``train`` or ``test`` | |||
subset of LaSOT. | |||
""" | |||
def __init__(self, root_dir, subset='train', return_meta=False): | |||
super(LaSOT, self).__init__() | |||
assert subset in ['train', 'test'], 'Unknown subset.' | |||
self.root_dir = root_dir | |||
self.subset = subset | |||
self.return_meta = return_meta | |||
self._check_integrity(root_dir) | |||
self.anno_files = sorted(glob.glob(os.path.join(root_dir, '*/*/groundtruth.txt'))) | |||
self.seq_dirs = [os.path.join(os.path.dirname(f), 'img') for f in self.anno_files] | |||
#self.seq_names = [os.path.basename(os.path.dirname(f) for f in self.anno_files)] | |||
# load subset sequence names | |||
split_file = os.path.join(os.path.dirname(__file__), 'lasot.json') #__file__ 代表当前文件所在的路径全名,包括当前文件名,而 dirname | |||
#os.path.dirname 为获取当前文件所在的路径名,不包括当前文件名 os.path.basename 返回path最后的文件名 | |||
with open(split_file, 'r') as f: | |||
splits = json.load(f) | |||
self.seq_names = splits[subset] | |||
# image and annotation paths | |||
self.seq_dirs = [os.path.join(root_dir, n[:n.rfind('-')], n, 'img') | |||
for n in self.seq_names] | |||
self.anno_files = [os.path.join(os.path.dirname(d), 'groundtruth.txt') | |||
for d in self.seq_dirs] | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno) if ``return_meta`` is False, otherwise | |||
(img_files, anno, meta), where ``img_files`` is a list of | |||
file names, ``anno`` is a N x 4 (rectangles) numpy array, while | |||
``meta`` is a dict contains meta information about the sequence. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
img_files = sorted(glob.glob(os.path.join( | |||
self.seq_dirs[index], '*.jpg'))) | |||
anno = np.loadtxt(self.anno_files[index], delimiter=',') | |||
if self.return_meta: | |||
meta = self._fetch_meta(self.seq_dirs[index]) | |||
return img_files, anno, meta | |||
else: | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
def _check_integrity(self, root_dir): | |||
seq_names = os.listdir(root_dir) | |||
seq_names = [n for n in seq_names if not n[0] == '.'] | |||
if os.path.isdir(root_dir) and len(seq_names) > 0: | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted.') | |||
def _fetch_meta(self, seq_dir): | |||
seq_dir = os.path.dirname(seq_dir) | |||
meta = {} | |||
# attributes | |||
for att in ['full_occlusion', 'out_of_view']: | |||
att_file = os.path.join(seq_dir, att + '.txt') | |||
meta[att] = np.loadtxt(att_file, delimiter=',') | |||
# nlp | |||
nlp_file = os.path.join(seq_dir, 'nlp.txt') | |||
with open(nlp_file, 'r') as f: | |||
meta['nlp'] = f.read().strip() | |||
return meta |
@@ -1,86 +0,0 @@ | |||
from __future__ import absolute_import, print_function, division | |||
import os | |||
import glob | |||
import numpy as np | |||
import six | |||
class NfS(object): | |||
"""`NfS <http://ci2cv.net/nfs/index.html>`_ Dataset. | |||
Publication: | |||
``Need for Speed: A Benchmark for Higher Frame Rate Object Tracking``, | |||
H. K. Galoogahi, A. Fagg, C. Huang, D. Ramanan and S. Lucey, ICCV 2017. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
fps (integer): Sequence frame rate. Two options ``30`` and ``240`` | |||
are available. Default is 240. | |||
""" | |||
def __init__(self, root_dir, fps=240): | |||
super(NfS, self).__init__() | |||
assert fps in [30, 240] | |||
self.fps = fps | |||
self.root_dir = root_dir | |||
self._check_integrity(root_dir) | |||
self.anno_files = sorted(glob.glob(os.path.join(root_dir, '*/%d/*.txt' % fps))) | |||
#self.anno_files = glob.glob(os.path.join(root_dir, '*/%d/*.txt' % fps)) | |||
self.seq_names = [ | |||
os.path.basename(f)[:-4] for f in self.anno_files] | |||
self.seq_dirs = [os.path.join( | |||
os.path.dirname(f), n) | |||
for f, n in zip(self.anno_files, self.seq_names)] | |||
def __getitem__(self, index): | |||
""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno), where ``img_files`` is a list of | |||
file names and ``anno`` is a N x 4 (rectangles) numpy array. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
img_files = sorted(glob.glob( | |||
os.path.join(self.seq_dirs[index], '*.jpg'))) | |||
anno = np.loadtxt(self.anno_files[index], dtype=str) | |||
anno = anno[:, 1:5].astype(float) # [left, top, right, bottom] | |||
anno[:, 2:] -= anno[:, :2] # [left, top, width, height] | |||
# handle inconsistent lengths | |||
if not len(img_files) == len(anno): | |||
if abs(len(anno) / len(img_files) - 8) < 1: | |||
anno = anno[0::8, :] | |||
diff = abs(len(img_files) - len(anno)) | |||
if diff > 0 and diff <= 1: | |||
n = min(len(img_files), len(anno)) | |||
anno = anno[:n] | |||
img_files = img_files[:n] | |||
assert len(img_files) == len(anno) | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
def _check_integrity(self, root_dir): | |||
seq_names = os.listdir(root_dir) | |||
seq_names = [n for n in seq_names if not n[0] == '.'] | |||
if os.path.isdir(root_dir) and len(seq_names) > 0: | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted.') |
@@ -1,196 +0,0 @@ | |||
from __future__ import absolute_import, print_function, unicode_literals | |||
import os | |||
import glob | |||
import numpy as np | |||
import io | |||
import six | |||
from itertools import chain | |||
from ..utils.ioutils import download, extract | |||
class OTB(object): | |||
r"""`OTB <http://cvlab.hanyang.ac.kr/tracker_benchmark/>`_ Datasets. | |||
Publication: | |||
``Object Tracking Benchmark``, Y. Wu, J. Lim and M.-H. Yang, IEEE TPAMI 2015. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
version (integer or string): Specify the benchmark version, specify as one of | |||
``2013``, ``2015``, ``tb50`` and ``tb100``. | |||
download (boolean, optional): If True, downloads the dataset from the internet | |||
and puts it in root directory. If dataset is downloaded, it is not | |||
downloaded again. | |||
""" | |||
__otb13_seqs = ['Basketball', 'Bolt', 'Boy', 'Car4', 'CarDark', | |||
'CarScale', 'Coke', 'Couple', 'Crossing', 'David', | |||
'David2', 'David3', 'Deer', 'Dog1', 'Doll', 'Dudek', | |||
'FaceOcc1', 'FaceOcc2', 'Fish', 'FleetFace', | |||
'Football', 'Football1', 'Freeman1', 'Freeman3', | |||
'Freeman4', 'Girl', 'Ironman', 'Jogging', 'Jumping', | |||
'Lemming', 'Liquor', 'Matrix', 'Mhyang', 'MotorRolling', | |||
'MountainBike', 'Shaking', 'Singer1', 'Singer2', | |||
'Skating1', 'Skiing', 'Soccer', 'Subway', 'Suv', | |||
'Sylvester', 'Tiger1', 'Tiger2', 'Trellis', 'Walking', | |||
'Walking2', 'Woman'] | |||
__tb50_seqs = ['Basketball', 'Biker', 'Bird1', 'BlurBody', 'BlurCar2', | |||
'BlurFace', 'BlurOwl', 'Bolt', 'Box', 'Car1', 'Car4', | |||
'CarDark', 'CarScale', 'ClifBar', 'Couple', 'Crowds', | |||
'David', 'Deer', 'Diving', 'DragonBaby', 'Dudek', | |||
'Football', 'Freeman4', 'Girl', 'Human3', 'Human4', | |||
'Human6', 'Human9', 'Ironman', 'Jump', 'Jumping', | |||
'Liquor', 'Matrix', 'MotorRolling', 'Panda', 'RedTeam', | |||
'Shaking', 'Singer2', 'Skating1', 'Skating2', 'Skiing', | |||
'Soccer', 'Surfer', 'Sylvester', 'Tiger2', 'Trellis', | |||
'Walking', 'Walking2', 'Woman'] | |||
__tb100_seqs = ['Bird2', 'BlurCar1', 'BlurCar3', 'BlurCar4', 'Board', | |||
'Bolt2', 'Boy', 'Car2', 'Car24', 'Coke', 'Coupon', | |||
'Crossing', 'Dancer', 'Dancer2', 'David2', 'David3', | |||
'Dog', 'Dog1', 'Doll', 'FaceOcc1', 'FaceOcc2', 'Fish', | |||
'FleetFace', 'Football1', 'Freeman1', 'Freeman3', | |||
'Girl2', 'Gym', 'Human2', 'Human5', 'Human7', 'Human8', | |||
'Jogging', 'KiteSurf', 'Lemming', 'Man', 'Mhyang', | |||
'MountainBike', 'Rubik', 'Singer1', 'Skater', | |||
'Skater2', 'Subway', 'Suv', 'Tiger1', 'Toy', 'Trans', | |||
'Twinnings', 'Vase'] + __tb50_seqs | |||
__otb15_seqs = __tb100_seqs | |||
__version_dict = { | |||
2013: __otb13_seqs, | |||
2015: __otb15_seqs, | |||
'otb2013': __otb13_seqs, | |||
'otb2015': __otb15_seqs, | |||
'tb50': __tb50_seqs, | |||
'tb100': __tb100_seqs} | |||
def __init__(self, root_dir, version=2015, download=True): | |||
super(OTB, self).__init__() | |||
assert version in self.__version_dict | |||
self.root_dir = root_dir | |||
self.version = version | |||
if download: | |||
self._download(root_dir, version) | |||
self._check_integrity(root_dir, version) | |||
#chain.from_iterable 接受一个可以迭代的对象作为参数,返回一个迭代器 | |||
valid_seqs = self.__version_dict[version] ##glob.glob获取指定路径下的所有.txt文件,查找符合特征规则的文件路径名 | |||
self.anno_files = sorted(list(chain.from_iterable(glob.glob( | |||
os.path.join(root_dir, s, 'groundtruth*.txt')) for s in valid_seqs))) | |||
# remove empty annotation files | |||
# (e.g., groundtruth_rect.1.txt of Human4) | |||
self.anno_files = self._filter_files(self.anno_files) | |||
self.seq_dirs = [os.path.dirname(f) for f in self.anno_files] | |||
self.seq_names = [os.path.basename(d) for d in self.seq_dirs] | |||
# rename repeated sequence names | |||
# (e.g., Jogging and Skating2) | |||
self.seq_names = self._rename_seqs(self.seq_names) | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno), where ``img_files`` is a list of | |||
file names and ``anno`` is a N x 4 (rectangles) numpy array. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
img_files = sorted(glob.glob( | |||
os.path.join(self.seq_dirs[index], 'img/*.jpg'))) | |||
# special sequences | |||
# (visit http://cvlab.hanyang.ac.kr/tracker_benchmark/index.html for detail) | |||
seq_name = self.seq_names[index] | |||
if seq_name.lower() == 'david': | |||
img_files = img_files[300-1:770] | |||
elif seq_name.lower() == 'football1': | |||
img_files = img_files[:74] | |||
elif seq_name.lower() == 'freeman3': | |||
img_files = img_files[:460] | |||
elif seq_name.lower() == 'freeman4': | |||
img_files = img_files[:283] | |||
elif seq_name.lower() == 'diving': | |||
img_files = img_files[:215] | |||
# to deal with different delimeters | |||
with open(self.anno_files[index], 'r') as f: | |||
anno = np.loadtxt(io.StringIO(f.read().replace(',', ' '))) | |||
assert len(img_files) == len(anno) | |||
assert anno.shape[1] == 4 | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
#过滤掉不符合条件的groundtruth文件 | |||
def _filter_files(self, filenames): | |||
filtered_files = [] | |||
for filename in filenames: | |||
with open(filename, 'r') as f: | |||
if f.read().strip() == '': | |||
print('Warning: %s is empty.' % filename) | |||
else: | |||
filtered_files.append(filename) | |||
return filtered_files | |||
def _rename_seqs(self, seq_names): | |||
# in case some sequences may have multiple targets | |||
renamed_seqs = [] | |||
for i, seq_name in enumerate(seq_names): | |||
if seq_names.count(seq_name) == 1: | |||
renamed_seqs.append(seq_name) | |||
else: | |||
ind = seq_names[:i + 1].count(seq_name) | |||
renamed_seqs.append('%s.%d' % (seq_name, ind)) | |||
return renamed_seqs | |||
#下载数据集 | |||
def _download(self, root_dir, version): | |||
assert version in self.__version_dict | |||
seq_names = self.__version_dict[version] | |||
if not os.path.isdir(root_dir): | |||
os.makedirs(root_dir) | |||
elif all([os.path.isdir(os.path.join(root_dir, s)) for s in seq_names]): | |||
print('Files already downloaded.') | |||
return | |||
url_fmt = 'http://cvlab.hanyang.ac.kr/tracker_benchmark/seq/%s.zip' | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if os.path.isdir(seq_dir): | |||
continue | |||
url = url_fmt % seq_name | |||
zip_file = os.path.join(root_dir, seq_name + '.zip') | |||
print('Downloading to %s...' % zip_file) | |||
download(url, zip_file) | |||
print('\nExtracting to %s...' % root_dir) | |||
extract(zip_file, root_dir) | |||
return root_dir | |||
#检查数据集的完整性 | |||
def _check_integrity(self, root_dir, version): | |||
assert version in self.__version_dict | |||
seq_names = self.__version_dict[version] | |||
if os.path.isdir(root_dir) and len(os.listdir(root_dir)) > 0: | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted. ' + | |||
'You can use download=True to download it.') |
@@ -1,98 +0,0 @@ | |||
from __future__ import absolute_import, print_function | |||
import os | |||
import glob | |||
import numpy as np | |||
import six | |||
from ..utils.ioutils import download, extract | |||
class TColor128(object): | |||
"""`TColor128 <http://www.dabi.temple.edu/~hbling/data/TColor-128/TColor-128.html>`_ Dataset. | |||
Publication: | |||
``Encoding color information for visual tracking: algorithms and benchmark``, | |||
P. Liang, E. Blasch and H. Ling, TIP, 2015. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
""" | |||
def __init__(self, root_dir, download=False): | |||
super(TColor128, self).__init__() | |||
self.root_dir = root_dir | |||
if download: | |||
self._download(root_dir) | |||
self._check_integrity(root_dir) | |||
self.anno_files = sorted(glob.glob( | |||
os.path.join(root_dir, '*/*_gt.txt'))) | |||
self.seq_dirs = [os.path.dirname(f) for f in self.anno_files] | |||
self.seq_names = [os.path.basename(d) for d in self.seq_dirs] | |||
# valid frame range for each sequence | |||
self.range_files = [glob.glob( | |||
os.path.join(d, '*_frames.txt'))[0] | |||
for d in self.seq_dirs] | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno), where ``img_files`` is a list of | |||
file names and ``anno`` is a N x 4 (rectangles) numpy array. | |||
""" | |||
if isinstance(index, six.string_types): | |||
if not index in self.seq_names: | |||
raise Exception('Sequence {} not found.'.format(index)) | |||
index = self.seq_names.index(index) | |||
# load valid frame range | |||
frames = np.loadtxt( | |||
self.range_files[index], dtype=int, delimiter=',') | |||
img_files = [os.path.join( | |||
self.seq_dirs[index], 'img/%04d.jpg' % f) | |||
for f in range(frames[0], frames[1] + 1)] | |||
# load annotations | |||
anno = np.loadtxt(self.anno_files[index], delimiter=',') | |||
assert len(img_files) == len(anno) | |||
assert anno.shape[1] == 4 | |||
return img_files, anno | |||
def __len__(self): | |||
return len(self.seq_names) | |||
def _download(self, root_dir): | |||
if not os.path.isdir(root_dir): | |||
os.makedirs(root_dir) | |||
elif len(os.listdir(root_dir)) > 100: | |||
print('Files already downloaded.') | |||
return | |||
url = 'http://www.dabi.temple.edu/~hbling/data/TColor-128/Temple-color-128.zip' | |||
zip_file = os.path.join(root_dir, 'Temple-color-128.zip') | |||
print('Downloading to %s...' % zip_file) | |||
download(url, zip_file) | |||
print('\nExtracting to %s...' % root_dir) | |||
extract(zip_file, root_dir) | |||
return root_dir | |||
def _check_integrity(self, root_dir): | |||
seq_names = os.listdir(root_dir) | |||
seq_names = [n for n in seq_names if not n[0] == '.'] | |||
if os.path.isdir(root_dir) and len(seq_names) > 0: | |||
# check each sequence folder | |||
for seq_name in seq_names: | |||
seq_dir = os.path.join(root_dir, seq_name) | |||
if not os.path.isdir(seq_dir): | |||
print('Warning: sequence %s not exists.' % seq_name) | |||
else: | |||
# dataset not exists | |||
raise Exception('Dataset not found or corrupted. ' + | |||
'You can use download=True to download it.') |
@@ -1,77 +0,0 @@ | |||
from __future__ import absolute_import, print_function | |||
import os | |||
import glob | |||
import six | |||
import numpy as np | |||
class TrackingNet(object): | |||
r"""`TrackingNet <https://tracking-net.org/>`_ Datasets. | |||
Publication: | |||
``TrackingNet: A Large-Scale Dataset and Benchmark for Object Tracking in the Wild.``, | |||
M. Muller, A. Bibi, S. Giancola, S. Al-Subaihi and B. Ghanem, ECCV 2018. | |||
Args: | |||
root_dir (string): Root directory of dataset where sequence | |||
folders exist. | |||
subset (string, optional): Specify ``train`` or ``test`` | |||
subset of TrackingNet. | |||
""" | |||
def __init__(self, root_dir, subset='test'): | |||
super(TrackingNet, self).__init__() | |||
assert subset in ['train', 'test'], 'Unknown subset.' | |||
self.root_dir = root_dir | |||
self.subset = subset | |||
if subset == 'test': | |||
self.subset_dirs = ['TEST'] | |||
elif subset == 'train': | |||
self.subset_dirs = ['TRAIN_%d' % c for c in range(12)] | |||
self._check_integrity(root_dir, self.subset_dirs) | |||
self.anno_files = [glob.glob(os.path.join( | |||
root_dir, c, 'anno/*.txt')) for c in self.subset_dirs] | |||
self.anno_files = sorted(sum(self.anno_files, [])) | |||
self.seq_dirs = [os.path.join( | |||
os.path.dirname(os.path.dirname(f)), | |||
os.path.basename(f)[:-4]) | |||
for f in self.anno_files] | |||
self.seq_names = [os.path.basename(d) for d in self.seq_dirs] | |||
def __getitem__(self, index): | |||
r""" | |||
Args: | |||
index (integer or string): Index or name of a sequence. | |||
Returns: | |||
tuple: (img_files, anno), where ``img_files`` is a list of | |||
file names and ``anno`` is a N x 4 (rectangles) numpy array. |