DianLi/LandslideTrain/LandslideTrain.cpp
2023-03-13 16:14:59 +08:00

456 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "LandslideTrain.h"
#include <QSettings>
LandslideTrain::LandslideTrain()
{
//ui.setupUi(this);
}
QString LandslideTrain::PannelName()
{
return QString::fromLocal8Bit("華窐耀輸");
}
QString LandslideTrain::CategoryName()
{
return QString::fromLocal8Bit("華窐耀輸");
}
QString LandslideTrain::EnglishName()
{
return QString::fromLocal8Bit("LandslideTrain");
}
QString LandslideTrain::ChineseName()
{
return QString::fromLocal8Bit("耀倰凳膘");
}
QString LandslideTrain::Information()
{
return QString::fromLocal8Bit("耀倰凳膘");
}
QString LandslideTrain::IconPath()
{
return ":/LandslideTrain/resources/dem_vec.svg";
}
QWidget* LandslideTrain::CenterWidget()
{
//QString gdal_path = qApp->applicationDirPath().toLocal8Bit() + "/share/gdal";
//QString pro_lib_path = qApp->applicationDirPath().toLocal8Bit() + "/share/proj";
//qputenv("GDAL_DATA", gdal_path.toLocal8Bit());
//qputenv("PROJ_LIB", pro_lib_path.toLocal8Bit());
bool showWin = false;
if (myWidget == nullptr)
{
myWidget = new QDialog();
showWin = true;
qDebug() << "new QDialog()";
}
else
{
qDebug() << "already have myWidget";
//慾魂敦諳甜枑汔祫階脯
myWidget->activateWindow();
myWidget->raise();
return myWidget;
}
ui.setupUi(myWidget);
myWidget->setWindowTitle(QString::fromLocal8Bit("耀倰捄褶"));
myWidget->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
myWidget->setWindowIcon(QIcon(":/LandslideTrain/resources/dem_vec.svg"));
myWidget->setAttribute(Qt::WA_QuitOnClose, false);
myWidget->setAttribute(Qt::WA_DeleteOnClose);
connect(myWidget, &QDialog::destroyed, this, [=] {
qDebug() << "----Landslide train window close----";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\SldModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
WriteConfigPaths(strConfigPath);
f.close();
}
}
if (mWorkThread != nullptr)
{
mWorkThread->requestInterruption();
mWorkThread->quit();
mWorkThread->wait();//覃蚚wait綴珂覃蚚finished陓瘍勤茼腔羞滲杅ㄛ硒俴俇傖綴婬厘狟軗
mWorkObject->on_cancel();
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLaterㄛ祥剒猁delete
mWorkObject = nullptr;//mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater
}
myWidget->close();
myWidget = nullptr;
});
connect(ui.pbtInModel, &QPushButton::clicked, this, &LandslideTrain::chooseInModel);
connect(ui.pbtInDataset, &QPushButton::clicked, this, &LandslideTrain::chooseInDataset);
connect(ui.pbtInLabel, &QPushButton::clicked, this, &LandslideTrain::chooseInLabel);
connect(ui.pbtOutResult, &QPushButton::clicked, this, &LandslideTrain::chooseResultPath);
connect(ui.pushButton_ok, &QPushButton::clicked, this, &LandslideTrain::readAndStart);
connect(ui.pushButton_cancel, &QPushButton::clicked, this, &LandslideTrain::pbCancel);
ui.lineEpoch1->setValidator(new QIntValidator(0, 999, this));
ui.lineEpoch2->setValidator(new QIntValidator(0, 999, this));
ui.lineEpoch1->setText("100");
ui.lineEpoch2->setText("100");
ui.pbtInModel->setFocus();
connect(ui.checkBoxGenData, &QCheckBox::clicked, this, [=](bool checked) {
executeGenData = checked;
if (!executeGenData && !executeTrain)
ui.pushButton_ok->setEnabled(false);
else
ui.pushButton_ok->setEnabled(true);
});
connect(ui.checkBoxTrain, &QCheckBox::clicked, this, [=](bool checked) {
executeTrain = checked;
if (!executeGenData && !executeTrain)
ui.pushButton_ok->setEnabled(false);
else
ui.pushButton_ok->setEnabled(true);
});
ui.progressBar->setTextVisible(true);
ui.progressBar->setRange(0, 100);
QFile qssFile(":/LandslideTrain/LandslideTrain.qss");
qssFile.open(QFile::ReadOnly); //眕硐黍源宒湖羲
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
myWidget->setStyleSheet(qss);
qssFile.close();
}
else
qDebug() << "-- no qssFile";
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (pluginsDir.cd("srsplugins\\SldModel"))
{
QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini");
QFile f(strConfigPath);
if (f.exists())
{
ReadConfigHistoryPaths(strConfigPath);
f.close();
}
}
if (showWin)
myWidget->show();
return myWidget;
}
void LandslideTrain::startWorkThread()
{
if (mWorkObject != nullptr)
{
return;
}
mWorkThread = new QThread();
mWorkObject = new WorkObject();
mWorkObject->moveToThread(mWorkThread);
connect(mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater);
connect(mWorkThread, &QThread::finished, mWorkObject, &QObject::deleteLater);
connect(mWorkObject, &WorkObject::progress, myWidget, [=](double val) {ui.progressBar->SetDoubleFormatValue(QString::fromLocal8Bit("輛僅"), val); });
connect(mWorkObject, &WorkObject::trainFinished, this, &LandslideTrain::finished);
connect(this, &LandslideTrain::start, mWorkObject, &WorkObject::runTrainWork);
mWorkThread->start();
}
void LandslideTrain::ReadConfigHistoryPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
//湖羲梓枙峈ㄩ[SldTrain] 腔郪ㄛ甜黍龰堤port趼僇腔硉
configIni.beginGroup("SldTrain");
ui.lineInModel->setText(configIni.value("RetrainModel").toString());
ui.lineInDataset->setText(configIni.value("SrcDom").toString());
ui.lineInLabel->setText(configIni.value("SrcLabel").toString());
ui.lineOutResult->setText(configIni.value("TrainResult").toString());
configIni.endGroup();//壽敕郪
}
void LandslideTrain::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//湖羲梓枙峈ㄩ[SldTrain] 腔郪
configIni.beginGroup("SldTrain");
//載陔怀⻌耀倰繚噤
QString temp = ui.lineInModel->text();
if (temp != "")
configIni.setValue("RetrainModel", temp);
//載陔怀⻌DOM繚噤
temp = ui.lineInDataset->text();
if (temp != "")
configIni.setValue("SrcDom", temp);
//載陔怀⻌Label繚噤
temp = ui.lineInLabel->text();
if (temp != "")
configIni.setValue("SrcLabel", temp);
//載陔怀堤賦彆繚噤
temp = ui.lineOutResult->text();
if (temp != "")
configIni.setValue("TrainResult", temp);
configIni.endGroup();//壽敕郪
}
void LandslideTrain::readAndStart()
{
QString inDataset = ui.lineInDataset->text();
QString inLabel=ui.lineInLabel->text();
QString inModel=ui.lineInModel->text();
QString outResult=ui.lineOutResult->text();
if (inDataset == "" || inLabel == "" || inModel == "" || outResult == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("ワ潰脤怀⻌怀堤繚噤"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
ui.progressBar->SetDoubleFormatValue(QString::fromLocal8Bit("輛僅"), 0);
QDir inDatasetDir(inDataset);
if (!inDatasetDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("捄褶杅擂恅璃標祥湔婓"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir inLabelDir(inLabel);
if (!inLabelDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("梓キ杅擂恅璃標祥湔婓"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir outModelDir(outResult);
if (!outModelDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("耀倰怀堤恅璃標祥湔婓"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (executeGenData && executeTrain)
qDebug() << "executeGenData & executeTrain";
if (executeGenData && (!executeTrain))
qDebug() << "executeGenData, not executeTrain";
if ((!executeGenData) && executeTrain)
{
//瓚剿train恅璃標狟衄拸捄褶摩恅璃標
QDir dirImage(outResult + "/Images");
QDir dirLabel(outResult + "/Labels");
if (!dirImage.exists() || !dirLabel.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"),
QString::fromLocal8Bit("帤梑善捄褶摩恅璃標\n<EFBFBD>悵Images睿Labels婓眕狟繚噤笢: \n") + outResult);
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
//瓚剿恅璃岆瘁⑤饜
QStringList imgList = getAllFiles(outResult + "/Images", "tif");
QStringList labelList = getAllFiles(outResult + "/Labels", "tif");
if (imgList.size() == 0 || labelList.size() == 0)
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"),
QString::fromLocal8Bit("捄褶摩恅璃標囀峈諾"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (imgList.size() != labelList.size())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"),
QString::fromLocal8Bit("捄褶摩Images﹜Labels恅璃標囀恅璃祥⑤饜"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
}
if (ui.lineEpoch1->text() == "" || ui.lineEpoch2->text() == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("渣昫"), QString::fromLocal8Bit("ワ怀⻌淏<EFBFBD>腔捄褶謫杅"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QString epoch1 = ui.lineEpoch1->text();
QString epoch2 = ui.lineEpoch2->text();
if (mWorkThread == nullptr)
{
qDebug() << "--startThread";
startWorkThread();
}
emit start(inModel, inDataset, inLabel, outResult, executeGenData, executeTrain, epoch1, epoch2);
}
QStringList LandslideTrain::getAllFiles(QString path, QString fileType)
{
QDir dir(path);
if (!dir.exists())
return QStringList();
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
int file_count = list.count();
if (file_count <= 0)
return QStringList();
QStringList files;
for (int i = 0; i < file_count; i++)
{
QFileInfo file_info = list.at(i);
QString suffix = file_info.suffix();
if (QString::compare(suffix, QString(fileType), Qt::CaseInsensitive) == 0)
{
QString absolute_file_path = file_info.absoluteFilePath();
files.append(absolute_file_path);
}
}
return files;
}
void LandslideTrain::finished()
{
QMessageBox mess(QMessageBox::NoIcon,
QString::fromLocal8Bit("堍俴賦旰"),
QString::fromLocal8Bit("賦彆恅璃汜傖繚噤\n") + ui.lineOutResult->text(),
QMessageBox::Ok, NULL);
mess.setWindowFlags(Qt::Drawer);
mess.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("<EFBFBD>"));
int result = mess.exec();
pbCancel();
}
void LandslideTrain::pbCancel()
{
//qDebug() << "--pbtCancel";
delete myWidget;//覃お&QDialog::destroyed
}
void LandslideTrain::chooseInModel()
{
QString dirModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("恁寁怀⻌場宎捄褶耀倰恅璃"), "", "*.pth");
if (dirModel != "")
ui.lineInModel->setText(dirModel);
}
void LandslideTrain::chooseInDataset()
{
QString dirDataset = QFileDialog::getExistingDirectory(ui.pbtInDataset, QString::fromLocal8Bit("恁寁怀⻌捄褶杅擂繚噤"), "");
if (dirDataset != "")
ui.lineInDataset->setText(dirDataset);
}
void LandslideTrain::chooseInLabel()
{
QString dirLabel = QFileDialog::getExistingDirectory(ui.pbtInLabel, QString::fromLocal8Bit("恁寁怀⻌梓キ杅擂繚噤"), "");
if (dirLabel != "")
ui.lineInLabel->setText(dirLabel);
}
void LandslideTrain::chooseResultPath()
{
QString dirResult = QFileDialog::getExistingDirectory(ui.pbtOutResult, QString::fromLocal8Bit("恁寁怀堤耀倰恅璃繚噤"), "");
if (dirResult != "")
ui.lineOutResult->setText(dirResult);
}
void WorkObject::runTrainWork(QString inModel, QString dataset, QString label, QString outModel, bool gen, bool train, QString epoch1, QString epoch2)
{
QDir pluginsDir = QDir(qApp->applicationDirPath());
if (!pluginsDir.cd("models\\envs"))
{
qDebug() << "no folder models\\envs";
return;
}
QString exeDirName = pluginsDir.absoluteFilePath("train_3c_landslide.exe");
QString inDom = " --dom_path " + dataset + "/";
QString inLabel = " --label_path " + label + "/";
QString trainedModel = " --retrained_model " + inModel;
QString save_model = " --save_model " + outModel + "/";
QString strMid, strTrain;
if (gen)
strMid = " --exe_mid True ";
else
strMid = " --exe_mid False ";
if (train)
strTrain = " --exe_train True";
else
strTrain = " --exe_train False";
QString strEpoch1, strEpoch2;
strEpoch1 = " --epoch1 " + epoch1;
strEpoch2 = " --epoch2 " + epoch2;
QString ss = exeDirName + inDom + inLabel + trainedModel + save_model + strMid + strTrain + strEpoch1 + strEpoch2;
qDebug() << ss;
QProcess* pProces = new QProcess(this);
connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(on_read()));
pProces->start(ss);
}
void WorkObject::on_read()
{
mProcess = (QProcess*)sender();
QString output = QString::fromLocal8Bit(mProcess->readAllStandardOutput());
if (output.toDouble() > 0)
{
qDebug() << "exe out:" << output.toDouble();
emit progress(output.toDouble());
if (output.toDouble() == 100.0)
{
delete mProcess;
mProcess = nullptr;
emit trainFinished();
}
}
else
qDebug() << "Unresolved exe out:" << output;
}
void WorkObject::on_cancel()
{
if (mProcess == nullptr)
{
qDebug() << "--mProcess null";
}
else
{
QString KillStr = "taskkill /f /im train_3c_landslide.exe";
mProcess->startDetached(KillStr);
qDebug() << "--kill Process";
}
}