-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation.py
191 lines (158 loc) · 7.6 KB
/
segmentation.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
import os
from pathlib import Path
import glob
import shutil
import shlex
from qgis.core import QgsVectorFileWriter
from .setup import check, getTempdir, cleanTempdir
from .common import getInterpreter, getScript, getInputLayer, runCommand
def seg_generate_tiles(backend, inputX, inputY, inputTileSize, inputClassification, inputShuffle, inputTrainSize, inputValSize, inputTestSize, inputXNodata, inputYNodata, inputXDtype, inputYDtype, outputDest, feedback):
#copying data to temp dir
tempdir = getTempdir(feedback)
cleanTempdir(tempdir, feedback)
feedback.pushInfo('Copying data to ' + str(tempdir))
os.mkdir(os.path.join(tempdir, 'x'))
for i in inputX:
shutil.copy(i, os.path.join(tempdir, 'x'))
feedback.setProgress(int(5))
shutil.copy(inputY, tempdir)
inputY = getInputLayer(inputY, tempdir, backend)
feedback.setProgress(int(10))
#creating output folder
os.mkdir(os.path.join(tempdir, 'output'))
ready, started = check(backend, feedback)
feedback.setProgress(int(15))
if ready and started:
feedback.pushInfo('Starting tiles generation')
interpreter = getInterpreter(backend)
script = getScript('seg_generate_tiles.py', backend)
if backend == 'docker':
td = '/mnt/temp/'
else:
td = tempdir
cmd = [interpreter, '-u', script, \
shlex.quote(td), shlex.quote(inputY), str(inputTileSize), str(int(inputClassification)), \
str(int(inputShuffle)), str(inputTrainSize), str(inputValSize), str(inputTestSize),\
str(inputXNodata), str(inputYNodata), str(inputXDtype), str(inputYDtype)]
runCommand(cmd, backend, interpreter, tempdir, feedback)
feedback.setProgress(int(90))
feedback.pushInfo('Copying result to ' + outputDest)
results = glob.glob(os.path.join(tempdir, 'output') + '/*')
for result in results:
shutil.copy(result, outputDest)
feedback.setProgress(int(95))
cleanTempdir(tempdir, feedback)
feedback.setProgress(int(100))
def seg_train(backend, inputXTrain, inputYTrain, inputXVal, inputYVal, model, backbone, inputCheckpoint, inputWeights, inputEpochs, inputBatchSize, inputLessMetrics, inputLR, inputMultiprocessing, outputDest, feedback):
#copying data to temp dir
tempdir = getTempdir(feedback)
cleanTempdir(tempdir, feedback)
feedback.pushInfo('Copying data to ' + str(tempdir))
os.mkdir(os.path.join(tempdir, 'x_train'))
for i in inputXTrain:
shutil.copy(i, os.path.join(tempdir, 'x_train'))
os.mkdir(os.path.join(tempdir, 'y_train'))
for i in inputYTrain:
shutil.copy(i, os.path.join(tempdir, 'y_train'))
os.mkdir(os.path.join(tempdir, 'x_val'))
if not inputXVal == ['']:
for i in inputXVal:
shutil.copy(i, os.path.join(tempdir, 'x_val'))
os.mkdir(os.path.join(tempdir, 'y_val'))
if not inputYVal == ['']:
for i in inputYVal:
shutil.copy(i, os.path.join(tempdir, 'y_val'))
if inputCheckpoint is not None and inputCheckpoint != '':
shutil.copy(inputCheckpoint, tempdir)
inputCheckpoint = getInputLayer(inputCheckpoint, tempdir, backend)
feedback.setProgress(int(10))
#creating output folder
os.mkdir(os.path.join(tempdir, 'output'))
out_name = os.path.basename(outputDest)
ready, started = check(backend, feedback)
feedback.setProgress(int(15))
if ready and started:
feedback.pushInfo('Starting model training')
interpreter = getInterpreter(backend)
script = getScript('seg_train.py', backend)
if backend == 'docker':
td = '/mnt/temp/'
else:
td = tempdir
cmd = [interpreter, '-u', script, \
shlex.quote(td), str(model), str(backbone), shlex.quote(str(inputCheckpoint)), \
shlex.quote(str(inputWeights)), str(inputEpochs), str(inputBatchSize), \
str(int(inputLessMetrics)), str(inputLR), str(int(inputMultiprocessing)), \
shlex.quote(str(out_name))]
runCommand(cmd, backend, interpreter, tempdir, feedback)
feedback.setProgress(int(90))
feedback.pushInfo('Copying result to ' + outputDest)
shutil.copytree(os.path.join(tempdir, 'output'), os.path.dirname(outputDest), dirs_exist_ok = True)
feedback.setProgress(int(95))
cleanTempdir(tempdir, feedback)
feedback.setProgress(int(100))
def seg_test(backend, inputXTest, inputYTest, inputModel, inputBatchSize, inputMultiprocessing, feedback):
#copying data to temp dir
tempdir = getTempdir(feedback)
cleanTempdir(tempdir, feedback)
feedback.pushInfo('Copying data to ' + str(tempdir))
os.mkdir(os.path.join(tempdir, 'x_test'))
for i in inputXTest:
shutil.copy(i, os.path.join(tempdir, 'x_test'))
os.mkdir(os.path.join(tempdir, 'y_test'))
for i in inputYTest:
shutil.copy(i, os.path.join(tempdir, 'y_test'))
shutil.copy(inputModel, tempdir)
inputModel = getInputLayer(inputModel, tempdir, backend)
feedback.setProgress(int(10))
ready, started = check(backend, feedback)
feedback.setProgress(int(15))
if ready and started:
feedback.pushInfo('Starting model testing')
interpreter = getInterpreter(backend)
script = getScript('seg_test.py', backend)
if backend == 'docker':
td = '/mnt/temp/'
else:
td = tempdir
cmd = [interpreter, '-u', script, \
shlex.quote(td), shlex.quote(str(inputModel)), str(inputBatchSize), \
str(int(inputMultiprocessing)) ]
runCommand(cmd, backend, interpreter, tempdir, feedback)
feedback.setProgress(int(90))
cleanTempdir(tempdir, feedback)
feedback.setProgress(int(100))
def seg_map(backend, inputLayers, inputReference, inputModel, inputSamplesFile, inputBatchSize, inputNodata, inputMultiprocessing, outputDest, feedback):
#copying data to temp dir
tempdir = getTempdir(feedback)
cleanTempdir(tempdir, feedback)
feedback.pushInfo('Copying data to ' + str(tempdir))
layers = []
for i in inputLayers:
shutil.copy(i, tempdir)
layers.append(getInputLayer(i, tempdir, backend))
shutil.copy(inputReference, tempdir)
inputReference = getInputLayer(inputReference, tempdir, backend)
shutil.copy(inputModel, tempdir)
inputModel = getInputLayer(inputModel, tempdir, backend)
shutil.copy(inputSamplesFile, tempdir)
inputSamplesFile = getInputLayer(inputSamplesFile, tempdir, backend)
outfile = getInputLayer(outputDest, tempdir, backend)
feedback.setProgress(int(10))
ready, started = check(backend, feedback)
feedback.setProgress(int(15))
if ready and started:
feedback.pushInfo('Starting model training')
interpreter = getInterpreter(backend)
script = getScript('seg_map.py', backend)
cmd = [interpreter, '-u', script, \
shlex.quote(inputReference), shlex.quote(inputModel), shlex.quote(inputSamplesFile), \
str(inputNodata), str(inputBatchSize), str(int(inputMultiprocessing)), \
shlex.quote(str(outfile))] + [shlex.quote(str(i)) for i in layers]
runCommand(cmd, backend, interpreter, tempdir, feedback)
feedback.setProgress(int(90))
feedback.pushInfo('Copying result to ' + outputDest)
shutil.copy(outfile, os.path.dirname(outputDest))
feedback.setProgress(int(95))
cleanTempdir(tempdir, feedback)
feedback.setProgress(int(100))