DianLi/LandslideTrain/LandslideTrain.cpp

456 lines
13 KiB
C++
Raw Permalink Normal View History

2023-03-13 16:14:59 +08:00
#include "LandslideTrain.h"
#include <QSettings>
LandslideTrain::LandslideTrain()
{
//ui.setupUi(this);
}
QString LandslideTrain::PannelName()
{
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>");
}
QString LandslideTrain::CategoryName()
{
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>");
}
QString LandslideTrain::EnglishName()
{
return QString::fromLocal8Bit("LandslideTrain");
}
QString LandslideTrain::ChineseName()
{
return QString::fromLocal8Bit("ģ<EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>");
}
QString LandslideTrain::Information()
{
return QString::fromLocal8Bit("ģ<EFBFBD>͹<EFBFBD><EFBFBD><EFBFBD>");
}
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";
//<2F><><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
myWidget->activateWindow();
myWidget->raise();
return myWidget;
}
ui.setupUi(myWidget);
myWidget->setWindowTitle(QString::fromLocal8Bit("ģ<EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD>"));
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();//<2F><><EFBFBD><EFBFBD>wait<69><74><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>finished<65>źŶ<C5BA>Ӧ<EFBFBD>IJۺ<C4B2><DBBA><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mWorkObject->on_cancel();
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ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); //<2F><>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), 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);
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[SldTrain] <20><><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>port<72>ֶε<D6B6>ֵ
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();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void LandslideTrain::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[SldTrain] <20><><EFBFBD><EFBFBD>
configIni.beginGroup("SldTrain");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>·<EFBFBD><C2B7>
QString temp = ui.lineInModel->text();
if (temp != "")
configIni.setValue("RetrainModel", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM·<4D><C2B7>
temp = ui.lineInDataset->text();
if (temp != "")
configIni.setValue("SrcDom", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Label·<6C><C2B7>
temp = ui.lineInLabel->text();
if (temp != "")
configIni.setValue("SrcLabel", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
temp = ui.lineOutResult->text();
if (temp != "")
configIni.setValue("TrainResult", temp);
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
ui.progressBar->SetDoubleFormatValue(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), 0);
QDir inDatasetDir(inDataset);
if (!inDatasetDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir inLabelDir(inLabel);
if (!inLabelDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
QDir outModelDir(outResult);
if (!outModelDir.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
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)
{
//<2F>ж<EFBFBD>train<69>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><D1B5><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
QDir dirImage(outResult + "/Images");
QDir dirLabel(outResult + "/Labels");
if (!dirImage.exists() || !dirLabel.exists())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("δ<EFBFBD>ҵ<EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>\nȷ<EFBFBD><EFBFBD>Images<EFBFBD><EFBFBD>Labels<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: \n") + outResult);
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
//<2F>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD>ƥ<EFBFBD><C6A5>
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
if (imgList.size() != labelList.size())
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Images<EFBFBD><EFBFBD>Labels<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
}
if (ui.lineEpoch1->text() == "" || ui.lineEpoch2->text() == "")
{
QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><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("<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>\n") + ui.lineOutResult->text(),
QMessageBox::Ok, NULL);
mess.setWindowFlags(Qt::Drawer);
mess.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("ȷ<EFBFBD><EFBFBD>"));
int result = mess.exec();
pbCancel();
}
void LandslideTrain::pbCancel()
{
//qDebug() << "--pbtCancel";
delete myWidget;//<2F><><EFBFBD><EFBFBD>&QDialog::destroyed
}
void LandslideTrain::chooseInModel()
{
QString dirModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼѵ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>"), "", "*.pth");
if (dirModel != "")
ui.lineInModel->setText(dirModel);
}
void LandslideTrain::chooseInDataset()
{
QString dirDataset = QFileDialog::getExistingDirectory(ui.pbtInDataset, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dirDataset != "")
ui.lineInDataset->setText(dirDataset);
}
void LandslideTrain::chooseInLabel()
{
QString dirLabel = QFileDialog::getExistingDirectory(ui.pbtInLabel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dirLabel != "")
ui.lineInLabel->setText(dirLabel);
}
void LandslideTrain::chooseResultPath()
{
QString dirResult = QFileDialog::getExistingDirectory(ui.pbtOutResult, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
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";
}
}