Skip to content

Commit

Permalink
[*] gender
Browse files Browse the repository at this point in the history
  • Loading branch information
acgist committed Oct 22, 2024
1 parent 30f06d9 commit 47fdcca
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 97 deletions.
4 changes: 2 additions & 2 deletions boot/main/src/cli/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool lifuren::cli(const int argc, const char* const argv[]) {
SPDLOG_DEBUG("命令参数:{}", argv[i]);
}
const char* const command = argv[0];
if(command == "poetize") {
if(std::strcmp(command, "poetize") == 0) {
if(argc <= 2) {
SPDLOG_WARN("缺少提示内容");
return true;
Expand All @@ -23,7 +23,7 @@ bool lifuren::cli(const int argc, const char* const argv[]) {
prompt.push_back(argv[i]);
}
// TODO: 实现
} else if(command == "embedding") {
} else if(std::strcmp(command, "embedding") == 0) {
// TODO: 实现
} else {
SPDLOG_WARN("不支持的命令:{}", command);
Expand Down
1 change: 0 additions & 1 deletion boot/test/src/cv/TestImage.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "lifuren/Test.hpp"

#include "lifuren/CV.hpp"
#include "lifuren/File.hpp"
#include "lifuren/Image.hpp"

Expand Down
14 changes: 6 additions & 8 deletions boot/test/src/model/TestGender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GenderModuleImpl : public torch::nn::Module {
torch::Tensor forward(torch::Tensor x) {
x = this->features->forward(x);
x = this->avgPool->forward(x);
x = x.flatten();
x = x.flatten(1);
x = this->classifier->forward(x);
return torch::log_softmax(x, 1);
}
Expand All @@ -67,14 +67,16 @@ class GenderModuleImpl : public torch::nn::Module {

TORCH_MODULE(GenderModule);

class GenderModel : public lifuren::Model<lifuren::dataset::ImageFileDatasetLoader, float, std::string, torch::nn::CrossEntropyLoss, GenderModule> {
class GenderModel : public lifuren::Model<lifuren::dataset::ImageFileDatasetLoader, float, std::string, torch::nn::CrossEntropyLoss, GenderModule, torch::optim::Adam> {

public:
GenderModel(lifuren::ModelParams params = {
.batch_size = 10LL,
.epoch_count = 10LL,
.classify = true
}) : Model(torch::nn::CrossEntropyLoss{}, GenderModule{}, params) {
}) : Model(params, [](auto tensor) {
return tensor.squeeze().to(torch::kInt64);
}) {
}
virtual ~GenderModel() {
}
Expand All @@ -92,12 +94,8 @@ class GenderModel : public lifuren::Model<lifuren::dataset::ImageFileDatasetLoad
this->trainDataset = std::move(lifuren::dataset::loadImageFileDataset(200, 200, this->params.batch_size, path_train, ".jpg", mapping));
return true;
}
std::shared_ptr<torch::optim::Optimizer> defineOptimizer() override {
// return std::make_shared<torch::optim::SGD>(this->model->parameters(), this->params.lr);
return std::make_shared<torch::optim::Adam>(this->model->parameters(), this->params.lr);
}
float eval(std::string i) {
cv::Mat image = cv::imread(i);
cv::Mat image = cv::imread(i);
cv::resize(image, image, cv::Size(200, 200));
torch::Tensor image_tensor = torch::from_blob(image.data, { image.rows, image.cols, 3 }, torch::kByte).permute({ 2, 0, 1 }).unsqueeze(0).to(torch::kF32).div(255.0);
auto prediction = this->model->forward(image_tensor);
Expand Down
6 changes: 4 additions & 2 deletions boot/test/src/model/TestLibtorch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ LFR_FORMAT_LOG_STREAM(at::Tensor);
// torch::Tensor a = torch::rand({4, 6});
SPDLOG_DEBUG("\n{}", a);
SPDLOG_DEBUG("\n{}", a.t());
SPDLOG_DEBUG("\n{}", a.numel());
SPDLOG_DEBUG("\n{}", a.element_size());
SPDLOG_DEBUG("\n{}", a.flatten());
SPDLOG_DEBUG("\n{}", a.reshape({6, 4}));
SPDLOG_DEBUG("\n{}", a.permute({1, 0}));
Expand Down Expand Up @@ -52,6 +54,6 @@ LFR_FORMAT_LOG_STREAM(at::Tensor);

LFR_TEST(
// testPrint();
// testTensor();
testNorm();
testTensor();
// testNorm();
);
28 changes: 13 additions & 15 deletions boot/test/src/model/TestLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class LinearModuleImpl : public torch::nn::Module {

TORCH_MODULE(LinearModule);

class LinearModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, float, torch::Tensor, torch::nn::MSELoss, LinearModule> {
class LinearModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, float, torch::Tensor, torch::nn::MSELoss, LinearModule, torch::optim::SGD> {

public:
LinearModel(lifuren::ModelParams params = {
.batch_size = 10LL
}) : Model(torch::nn::MSELoss{}, LinearModule{}, params) {
.lr = 0.001F,
.batch_size = 10,
.epoch_count = 256
}) : Model(params) {
}
virtual ~LinearModel() {
}
Expand All @@ -46,21 +48,17 @@ class LinearModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, fl
std::normal_distribution<float> b(0.5, 0.2);
std::vector<float> labels;
std::vector<std::vector<float>> features;
labels.reserve(100);
features.reserve(100);
labels.reserve(200);
features.reserve(200);
// w * 15.4 + 4 + r
for(int index = 0; index < 100; ++index) {
for(int index = 0; index < 200; ++index) {
float f = w(rand);
features.push_back(std::vector<float>{ f });
labels.push_back(15.4 * f + 4 + b(rand));
features.push_back(std::vector<float>{ f });
}
this->trainDataset = std::move(lifuren::dataset::loadRawDataset(this->params.batch_size, labels, features));
return true;
}
std::shared_ptr<torch::optim::Optimizer> defineOptimizer() override {
return std::make_shared<torch::optim::SGD>(this->model->parameters(), this->params.lr);
// return std::make_shared<torch::optim::Adam>(this->model->parameters(), this->params.lr);
}
float eval(torch::Tensor i) {
auto o = this->model->forward(i);
return o.template item<float>();
Expand All @@ -72,7 +70,7 @@ class LinearModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, fl
LinearModel linear;
linear.define();
linear.trainValAndTest(false, false);
float pred = linear.eval(torch::tensor({3.0F}, torch::kFloat32));
float pred = linear.eval(torch::tensor({ 3.0F }, torch::kFloat32));
SPDLOG_DEBUG("当前预测:{}", pred);
linear.print();
linear.save();
Expand All @@ -85,11 +83,11 @@ class LinearModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, fl
// model.load(lifuren::config::CONFIG.tmp);
model.print();
// w * 15.4 + 4 + r
float pred = model.eval(torch::tensor({3.0F}, torch::kFloat32));
float pred = model.eval(torch::tensor({ 3.0F }, torch::kFloat32));
SPDLOG_DEBUG("当前预测:{}", pred);
}

LFR_TEST(
// testLine();
testLoad();
testLine();
// testLoad();
);
7 changes: 2 additions & 5 deletions boot/test/src/model/TestModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class SimpleModuleImpl : public torch::nn::Module {

TORCH_MODULE(SimpleModule);

class SimpleModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, float, torch::Tensor, torch::nn::MSELoss, SimpleModule> {
class SimpleModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, float, torch::Tensor, torch::nn::MSELoss, SimpleModule, torch::optim::Adam> {

public:
SimpleModel(lifuren::ModelParams params = {}) : Model(torch::nn::MSELoss{}, SimpleModule{}, params) {
SimpleModel(lifuren::ModelParams params = {}) : Model(params) {
}
virtual ~SimpleModel() {
}
Expand All @@ -53,9 +53,6 @@ class SimpleModel : public lifuren::Model<lifuren::dataset::RawDatasetLoader, fl
this->trainDataset = std::move(lifuren::dataset::loadRawDataset(5LL, labels, features));
return true;
}
std::shared_ptr<torch::optim::Optimizer> defineOptimizer() override {
return std::make_shared<torch::optim::Adam>(this->model->parameters(), this->params.lr);
}
float eval(torch::Tensor i) {
auto o = this->model->forward(i);
return o.template item<float>();
Expand Down
2 changes: 1 addition & 1 deletion cv/src/OpenCV.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "lifuren/Logger.hpp"

#include "opencv2/opencv.hpp"
#include "opencv2/core/utils/logger.hpp"

void lifuren::logger::opencv::init() {
#if defined(_DEBUG) || !defined(NDEBUG)
Expand Down
Loading

0 comments on commit 47fdcca

Please sign in to comment.