forked from nextgis/qgis_molusce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralnetworkwidget.py
227 lines (186 loc) · 8.76 KB
/
neuralnetworkwidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
#******************************************************************************
#
# MOLUSCE
# ---------------------------------------------------------
# Modules for Land Use Change Simulations
#
# Copyright (C) 2012-2013 NextGIS ([email protected])
#
# This source is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 2 of the License, or (at your option)
# any later version.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
#******************************************************************************
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import numpy
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
try:
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
except ImportError:
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib import rcParams
from algorithms.models.mlp.manager import MlpManager
from ui.ui_neuralnetworkwidgetbase import Ui_Widget
import molusceutils as utils
class NeuralNetworkWidget(QWidget, Ui_Widget):
def __init__(self, plugin, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.plugin = plugin
self.inputs = plugin.inputs
self.settings = QSettings("NextGIS", "MOLUSCE")
# init plot for learning curve
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.figure.suptitle(self.tr("Neural Network learning curve"))
self.canvas = FigureCanvas(self.figure)
self.mpltoolbar = NavigationToolbar(self.canvas, None)
lstActions = self.mpltoolbar.actions()
self.mpltoolbar.removeAction(lstActions[7])
self.layoutPlot.addWidget(self.canvas)
self.layoutPlot.addWidget(self.mpltoolbar)
# and configure matplotlib params
rcParams['font.serif'] = "Verdana, Arial, Liberation Serif"
rcParams['font.sans-serif'] = "Tahoma, Arial, Liberation Sans"
rcParams['font.cursive'] = "Courier New, Arial, Liberation Sans"
rcParams['font.fantasy'] = "Comic Sans MS, Arial, Liberation Sans"
rcParams['font.monospace'] = "Courier New, Liberation Mono"
self.btnTrainNetwork.clicked.connect(self.trainNetwork)
self.manageGui()
def manageGui(self):
self.spnNeigbourhood.setValue(int(self.settings.value("ui/ANN/neighborhood", 1)))
self.spnLearnRate.setValue(float(self.settings.value("ui/ANN/learningRate", 0.1)))
self.spnMaxIterations.setValue(int(self.settings.value("ui/ANN/maxIterations", 1000)))
self.leTopology.setText(self.settings.value("ui/ANN/topology", "10"))
self.spnMomentum.setValue(float(self.settings.value("ui/ANN/momentum", 0.05)))
self.btnStop.setEnabled(False)
def trainNetwork(self):
if not utils.checkInputRasters(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Initial or final raster is not set. Please specify input data and try again")
)
return
if not utils.checkFactors(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Factors rasters is not set. Please specify them and try again")
)
return
if not utils.checkChangeMap(self.inputs):
QMessageBox.warning(self.plugin,
self.tr("Missed input data"),
self.tr("Change map raster is not set. Please create it try again")
)
return
if self.leTopology.text() == "":
QMessageBox.warning(self.plugin,
self.tr("Wrong network topology"),
self.tr("Network topology is undefined. Please define it and try again")
)
return
self.settings.setValue("ui/ANN/neighborhood", self.spnNeigbourhood.value())
self.settings.setValue("ui/ANN/learningRate", self.spnLearnRate.value())
self.settings.setValue("ui/ANN/maxIterations", self.spnMaxIterations.value())
self.settings.setValue("ui/ANN/topology", self.leTopology.text())
self.settings.setValue("ui/ANN/momentum", self.spnMomentum.value())
self.btnStop.setEnabled(True)
self.btnTrainNetwork.setEnabled(False)
self.plugin.logMessage(self.tr("Init ANN model"))
model = MlpManager(ns=self.spnNeigbourhood.value())
self.inputs["model"] = model
model.createMlp(self.inputs["initial"],
self.inputs["factors"].values(),
self.inputs["changeMap"],
[int(n) for n in self.leTopology.text().split(" ")]
)
self.plugin.logMessage(self.tr("Set training data"))
model.setTrainingData(self.inputs["initial"],
self.inputs["factors"].values(),
self.inputs["changeMap"],
mode=self.inputs["samplingMode"],
samples=self.plugin.spnSamplesCount.value()
)
model.setEpochs(self.spnMaxIterations.value())
model.setValPercent(20)
model.setLRate(self.spnLearnRate.value())
model.setMomentum(self.spnMomentum.value())
model.setContinueTrain()
self.axes.cla()
self.axes.grid(True)
self.dataTrain = []
self.dataVal = []
self.plotTrain = self.axes.plot(self.dataTrain,
linewidth=1,
color="green", marker='o'
)[0]
self.plotVal = self.axes.plot(self.dataVal,
linewidth=1,
color="red",
)[0]
leg = self.axes.legend(('Train', 'Validation'), 'upper right', shadow=False)
for t in leg.get_texts():
t.set_fontsize('small')
model.moveToThread(self.plugin.workThread)
self.plugin.workThread.started.connect(model.startTrain)
self.btnStop.clicked.connect(model.stopTrain)
model.updateGraph.connect(self.__updateGraph)
model.updateDeltaRMS.connect(self.__updateRMS)
model.updateMinValErr.connect(self.__updateValidationError)
model.updateKappa.connect(self.__updateKappa)
model.processInterrupted.connect(self.__trainInterrupted)
model.rangeChanged.connect(self.plugin.setProgressRange)
model.updateProgress.connect(self.plugin.showProgress)
model.errorReport.connect(self.plugin.logErrorReport)
model.processFinished.connect(self.__trainFinished)
model.processFinished.connect(self.plugin.workThread.quit)
self.plugin.logMessage(self.tr("Start trainig ANN model"))
self.plugin.workThread.start()
def __trainFinished(self):
model = self.inputs["model"]
self.plugin.workThread.started.disconnect(model.startTrain)
model.rangeChanged.disconnect(self.plugin.setProgressRange)
model.updateProgress.disconnect(self.plugin.showProgress)
model.errorReport.disconnect(self.plugin.logErrorReport)
self.plugin.restoreProgressState()
self.btnStop.setEnabled(False)
self.btnTrainNetwork.setEnabled(True)
self.plugin.logMessage(self.tr("ANN model trained"))
def __trainInterrupted(self):
self.plugin.workThread.quit()
self.btnStop.setEnabled(False)
self.btnTrainNetwork.setEnabled(True)
self.plugin.logMessage(self.tr("ANN model training interrupted"))
def __updateRMS(self, dRMS):
self.leDeltaRMS.setText("%6.5f" % (dRMS))
def __updateValidationError(self, error):
self.leValidationError.setText("%6.5f" % (error))
def __updateKappa(self, kappa):
self.leKappa.setText("%6.5f" % (kappa))
def __updateGraph(self, errTrain, errVal):
self.dataTrain.append(errTrain)
self.dataVal.append(errVal)
ymin = min([min(self.dataTrain), min(self.dataVal)])
ymax = max([max(self.dataTrain), max(self.dataVal)])
self.axes.set_xbound(lower=0, upper=len(self.dataVal))
self.axes.set_ybound(lower=ymin, upper=ymax)
self.plotTrain.set_xdata(numpy.arange(len(self.dataTrain)))
self.plotTrain.set_ydata(numpy.array(self.dataTrain))
self.plotVal.set_xdata(numpy.arange(len(self.dataVal)))
self.plotVal.set_ydata(numpy.array(self.dataVal))
self.canvas.draw()