Skip to content

Commit

Permalink
[*] 学习
Browse files Browse the repository at this point in the history
  • Loading branch information
acgist committed Mar 24, 2024
1 parent 877a909 commit 7facd25
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 61 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ elseif(CMAKE_HOST_WIN32)
add_compile_options("/utf-8")
add_compile_options("/Zc:__cplusplus")
else()
message("需要安装MSVC")
message(WARNING "需要安装MSVC")
endif()
else()
message("不支持的系统:${CMAKE_HOST_SYSTEM_NAME}")
message(FATAL_ERROR "不支持的系统:${CMAKE_HOST_SYSTEM_NAME}")
endif()

string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
|json|v3.11.2|https://github.com/nlohmann/json|
|spdlog|v1.12.0|https://github.com/gabime/spdlog|
|OpenCV|4.8.0|https://github.com/opencv/opencv|
|yaml-cpp|0.8.0|https://github.com/jbeder/yaml-cpp|
|LibTorch|v2.2.1|https://github.com/pytorch/pytorch|
|yaml-cpp|0.8.0|https://github.com/jbeder/yaml-cpp|
|cpp-httplib|v0.15.3|https://github.com/yhirose/cpp-httplib|

## 模块
Expand Down
4 changes: 2 additions & 2 deletions boot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
add_subdirectory(main)

if(CMAKE_BUILD_TYPE MATCHES "Debug")
message("Debug模式打开测试")
message(STATUS "Debug模式打开测试")
add_subdirectory(test)
else()
message("非Debug模式关闭测试")
message(STATUS "非Debug模式关闭测试")
endif()
6 changes: 3 additions & 3 deletions boot/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ target_link_libraries(
lifurencore
)

add_executable(testOpenCVFace src/TestOpenCVFace.cpp src/source/opencv/OpenCV.cpp src/source/opencv/Face.cpp)
add_executable(testOpenCVFace src/TestOpenCVFace.cpp src/source/opencv/Face.cpp)
add_test(NAME testOpenCVFace COMMAND testOpenCVFace)
target_link_libraries(
testOpenCVFace PUBLIC
lifurencore
)

add_executable(testOpenCVColor src/TestOpenCVColor.cpp src/source/opencv/OpenCV.cpp src/source/opencv/Color.cpp)
add_executable(testOpenCVColor src/TestOpenCVColor.cpp src/source/opencv/Color.cpp)
add_test(NAME testOpenCVColor COMMAND testOpenCVColor)
target_link_libraries(
testOpenCVColor PUBLIC
lifurencore
)

add_executable(testOpenCVResize src/TestOpenCVResize.cpp src/source/opencv/OpenCV.cpp src/source/opencv/Resize.cpp)
add_executable(testOpenCVResize src/TestOpenCVResize.cpp src/source/opencv/Resize.cpp)
add_test(NAME testOpenCVResize COMMAND testOpenCVResize)
target_link_libraries(
testOpenCVResize PUBLIC
Expand Down
1 change: 0 additions & 1 deletion boot/test/src/TestOpenCVColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
int main(const int argc, const char * const argv[]) {
lifuren::logger::init();
SPDLOG_DEBUG("测试");
lifuren::shutdownOpenCVLogger();
lifuren::color("D:/tmp/logo.png");
// lifuren::color("D:/tmp/Dota2.jpg");
SPDLOG_DEBUG("完成");
Expand Down
1 change: 0 additions & 1 deletion boot/test/src/TestOpenCVFace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
int main(const int argc, const char * const argv[]) {
lifuren::logger::init();
SPDLOG_DEBUG("测试");
lifuren::shutdownOpenCVLogger();
lifuren::face("D:/gitee/lifuren/deps/opencv/etc/haarcascades/haarcascade_frontalface_default.xml", "D:/tmp/F4.jpg");
// lifuren::face("D:/gitee/lifuren/deps/opencv/etc/haarcascades/haarcascade_frontalface_default.xml", "D:/tmp/head.jpg");
SPDLOG_DEBUG("完成");
Expand Down
1 change: 0 additions & 1 deletion boot/test/src/TestOpenCVResize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
int main(const int argc, const char * const argv[]) {
lifuren::logger::init();
SPDLOG_DEBUG("测试");
lifuren::shutdownOpenCVLogger();
lifuren::resize("D:/tmp/Dota2.jpg");
SPDLOG_DEBUG("完成");
lifuren::logger::shutdown();
Expand Down
6 changes: 4 additions & 2 deletions boot/test/src/header/LibTorch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
#pragma once

#include <string>
#include <vector>

#include "torch/torch.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"

#include "Logger.hpp"
#include "config/Config.hpp"
#include "utils/Datasets.hpp"

LFR_LOG_FORMAT(at::Tensor);

namespace lifuren {

/**
* LibTorch Tensor测试
* 张量测试
*/
extern void testLibTorchTensor();

Expand All @@ -35,7 +37,7 @@ class GenderImpl : public torch::nn::Module {
// 池化层
torch::nn::AdaptiveAvgPool2d avgPool{ nullptr };
// 全连接层
torch::nn::Sequential classifier;
torch::nn::Sequential classifier{ nullptr };

public:
GenderImpl(std::vector<int>& cfg, int num_classes = 1000, bool batch_norm = false);
Expand Down
103 changes: 93 additions & 10 deletions boot/test/src/source/libtorch/Tensor.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,92 @@
#include "../../header/LibTorch.hpp"

// 测试初始化
void testInit();
// 测试拷贝
void testClone();
// 测试变形
void testResize();
// 测试切片
void testSlice();
// 测试运算
void testOperator();

void lifuren::testLibTorchTensor() {
// const torch::Tensor a = torch::randn({ 3, 2 });
// const torch::Tensor b = torch::randn({ 3, 2 });
SPDLOG_DEBUG("是否支持CUDA:{}", torch::cuda::is_available());
SPDLOG_DEBUG("是否支持CUDNN:{}", torch::cuda::cudnn_is_available());
// testInit();
// testClone();
testResize();
}

void testInit() {
auto a = torch::zeros({3, 4});
SPDLOG_DEBUG("a =\r\n{}", a);
a = torch::ones({3, 4});
SPDLOG_DEBUG("a =\r\n{}", a);
a = torch::eye(4);
SPDLOG_DEBUG("a =\r\n{}", a);
a = torch::full({3, 4}, 10);
SPDLOG_DEBUG("a =\r\n{}", a);
a = torch::tensor({33, 22, 11});
SPDLOG_DEBUG("a =\r\n{}", a);
// 随机
a = torch::rand({ 3, 4 });
SPDLOG_DEBUG("a =\r\n{}", a);
// 正态分布随机
a = torch::randn({ 3, 4 });
SPDLOG_DEBUG("a =\r\n{}", a);
a = torch::randint(0, 4, { 3, 4 });
SPDLOG_DEBUG("a =\r\n{}", a);
int array[10] = { 3, 4, 5, 1, 2, 3 };
a = torch::from_blob(array, { 3, 2 }, torch::kFloat);
SPDLOG_DEBUG("a =\r\n{}", a);
std::vector<float> vector{ 3, 4, 5, 1, 2, 3 };
a = torch::from_blob(vector.data(), { 3, 2 }, torch::kFloat);
SPDLOG_DEBUG("a =\r\n{}", a);
}

void testClone() {
auto a = torch::zeros({3, 4});
// 浅拷贝
auto b = a;
SPDLOG_DEBUG("a =\r\n{}", a);
SPDLOG_DEBUG("b =\r\n{}", b);
a[0][0] = 1;
SPDLOG_DEBUG("a =\r\n{}", a);
SPDLOG_DEBUG("b =\r\n{}", b);
// 深拷贝
auto c = a.clone();
SPDLOG_DEBUG("c =\r\n{}", a);
SPDLOG_DEBUG("c =\r\n{}", c);
a[0][0] = 2;
SPDLOG_DEBUG("a =\r\n{}", a);
SPDLOG_DEBUG("c =\r\n{}", c);
// 已有尺寸建立新的张量
// auto d = torch::zeros_like(b);
// auto d = torch::ones_like(b);
// auto d = torch::rand_like(b, torch::kFloat);
}

void testResize() {
// TODO:flatten、view、reshape、permute、transpose、squeeze、unsqueeze
int array[6] = { 4, 5, 6, 1, 2, 3 };
auto a = torch::from_blob(array, { 3, 2 }, torch::kInt);
SPDLOG_DEBUG("a =\r\n{}", a);
auto b = a.view({ 2, 3 });
// auto b = a.view({ 1, 2, -1 });
SPDLOG_DEBUG("b =\r\n{}", b);
SPDLOG_DEBUG("a =\r\n{}", a);
SPDLOG_DEBUG("a =\r\n{}", a.sizes());
SPDLOG_DEBUG("a =\r\n{}", a[0]);
SPDLOG_DEBUG("a =\r\n{}", a[0][0]);
}

void testSlice() {
// TODO:narrow、select、index、index_put_、index_select、slice
}

void testOperator() {
int arrayA[] = { 1, 2, 3, 4 };
int arrayB[] = { 1, 2, 3, 4 };
const torch::Tensor lineA = torch::tensor({ 1, 2, 3, 4});
Expand All @@ -11,14 +95,13 @@ void lifuren::testLibTorchTensor() {
const torch::Tensor b = torch::from_blob(arrayB, { 2, 2 }, torch::kInt);
SPDLOG_DEBUG("a =\r\n{}", a);
SPDLOG_DEBUG("b =\r\n{}", b);
SPDLOG_DEBUG("a + b =\r\n{}", (a + b));
SPDLOG_DEBUG("a - b =\r\n{}", (a - b));
SPDLOG_DEBUG("a * b =\r\n{}", (a * b));
SPDLOG_DEBUG("a / b =\r\n{}", (a / b));
SPDLOG_DEBUG("a % b =\r\n{}", (a % b));
SPDLOG_DEBUG("a + b =\r\n{}", (a + b));
SPDLOG_DEBUG("a - b =\r\n{}", (a - b));
SPDLOG_DEBUG("a * b =\r\n{}", (a * b));
SPDLOG_DEBUG("a / b =\r\n{}", (a / b));
SPDLOG_DEBUG("a % b =\r\n{}", (a % b));
SPDLOG_DEBUG("a == b =\r\n{}", (a == b));
SPDLOG_DEBUG("lineA dot lineB =\r\n{}", lineA.dot(lineB));
SPDLOG_DEBUG("lineA dot lineB =\r\n{}", lineA.dot(lineB));
SPDLOG_DEBUG("lineA dot lineB.t =\r\n{}", lineA.dot(lineB.t()));
SPDLOG_DEBUG("是否支持CUDA:{}", torch::cuda::is_available());
SPDLOG_DEBUG("是否支持CUDNN:{}", torch::cuda::cudnn_is_available());
// TODO:cat、stack
}
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(
${HEADER_DIR}/utils/Files.hpp
${HEADER_DIR}/utils/Jsons.hpp
${HEADER_DIR}/utils/Yamls.hpp
${HEADER_DIR}/utils/Layers.hpp
${HEADER_DIR}/utils/Strings.hpp
${HEADER_DIR}/utils/Datasets.hpp
${HEADER_DIR}/utils/Collections.hpp
Expand Down
8 changes: 6 additions & 2 deletions core/src/header/utils/Datasets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class FileDataset : public torch::data::Dataset<FileDataset> {
std::vector<std::string> paths;

public:
std::function<torch::Tensor(const std::string&)> fileTransform;
std::map<std::string, int> nameLabel;
std::map<int, std::string> labelName;
std::function<torch::Tensor(const std::string&)> fileTransform;

FileDataset(
const std::string& path,
Expand All @@ -56,9 +56,13 @@ inline auto loadImageDataset(int batch_size, const std::string& path, const std:
cv::Mat image = cv::imread(path);
cv::resize(image, image, cv::Size(224, 224));
torch::Tensor data_tensor = torch::from_blob(image.data, { image.rows, image.cols, 3 }, torch::kByte).permute({2, 0, 1});
// TODO:验证
// image.release();
// 不做正则
// auto data_tensor = torch::from_blob(image.data, { image.rows, image.cols, 3 }, torch::kByte).permute({ 2, 0, 1 }).unsqueeze(0).to(torch::kFloat32) / 225.0;
return data_tensor;
}).map(torch::data::transforms::Stack<>());
auto loader = torch::data::make_data_loader<torch::data::samplers::RandomSampler>(std::move(dataset), batch_size);
auto loader = torch::data::make_data_loader<torch::data::samplers::RandomSampler>(std::move(dataset), batch_size);
return std::move(loader);
}

Expand Down
23 changes: 23 additions & 0 deletions core/src/header/utils/Layers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* layer工具
*
* @author acgist
*/
#pragma once

#include "torch/torch.h"

namespace lifuren {
namespace layers {

// Linear
// BatchNorm1d
// Conv2d
// BatchNorm2d
// MaxPool2d
// AdaptiveAvgPool2d
// Conv2dReluBN
// torch::nn::LSTM

}
}
2 changes: 1 addition & 1 deletion core/src/source/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void lifuren::logger::init() {
logger->set_pattern("[%D %T] [%L] [%t] [%s:%#] %v");
spdlog::set_default_logger(logger);
// 关闭OpenCV日志
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_SILENT);
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_FATAL);
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_SILENT);
SPDLOG_DEBUG(R"(
北方有佳人,绝世而独立。
Expand Down
38 changes: 4 additions & 34 deletions docs/AI.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,40 +123,10 @@ T5/GPT/BERT/自注意力机制(Self-Attention)

## 模型精调

## 激活函数

## 优化算法

## 学习资料

* https://zh-v2.d2l.ai/
* https://github.com/hankcs/HanLP
* https://github.com/d2l-ai/d2l-zh
* https://github.com/Ewenwan/MVision
* https://github.com/NLP-LOVE/ML-NLP
* https://github.com/tangyudi/Ai-Learn
* https://github.com/apachecn/ailearning
* https://github.com/MorvanZhou/tutorials
* https://github.com/graykode/nlp-tutorial
* https://github.com/fighting41love/funNLP
* https://dataxujing.github.io/ASR-paper/#/
* https://dataxujing.github.io/NLP-paper/#/
* https://dataxujing.github.io/TTS-paper/#/
* https://dataxujing.github.io/CNN-paper2/#/
* https://dataxujing.github.io/AIGC-paper/#/
* https://github.com/Mikoto10032/DeepLearning
* https://github.com/microsoft/AI-For-Beginners
* https://github.com/microsoft/ML-For-Beginners
* https://github.com/DA-southampton/NLP_ability
* https://github.com/chenzomi12/DeepLearningSystem
* https://github.com/brightmart/nlp_chinese_corpus
* https://github.com/datawhalechina/leedl-tutorial
* https://scikit-learn.org/stable/modules/classes.html
* https://github.com/scutan90/DeepLearning-500-questions
* https://github.com/microsoft/generative-ai-for-beginners
* https://github.com/labmlai/annotated_deep_learning_paper_implementations

## 学习计划

* https://dataxujing.github.io/NLP-paper/#/
* https://dataxujing.github.io/CNN-paper2/#/
* https://dataxujing.github.io/AIGC-paper/#/
* https://github.com/tangyudi/Ai-Learn
* https://github.com/chenzomi12/DeepLearningSystem
* https://github.com/scutan90/DeepLearning-500-questions
2 changes: 1 addition & 1 deletion docs/Deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ CPU/GPU/CUDA/cuDNN
* https://developer.nvidia.com/cuda-11-8-0-download-archive

```
# https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
# https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_522.06_windows.exe
# https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-debian11-11-8-local_11.8.0-520.61.05-1_amd64.deb
# https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
# https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-ubuntu2004-11-8-local_11.8.0-520.61.05-1_amd64.deb
```

Expand Down

0 comments on commit 7facd25

Please sign in to comment.