Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
inovisao
pynovisao
Commits
9e4ef8cc
Commit
9e4ef8cc
authored
Oct 07, 2020
by
Diego André Sant'Ana
🤞
Browse files
fix classify with load saved model
parent
44a93341
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
34 deletions
+47
-34
examples/labels.txt
examples/labels.txt
+2
-4
src/classification/cnn_keras.py
src/classification/cnn_keras.py
+39
-27
src/pynovisao.py
src/pynovisao.py
+6
-3
No files found.
examples/labels.txt
View file @
9e4ef8cc
FolhasLargas
Solo
Soja
Gramineas
\ No newline at end of file
arvore
fundo
src/classification/cnn_keras.py
View file @
9e4ef8cc
...
...
@@ -29,7 +29,7 @@ from keras import backend as K
from
numpy
import
resize
,
expand_dims
from
keras.preprocessing.image
import
load_img
,
img_to_array
import
glob
from
interface.interface
import
InterfaceException
as
IException
from
classification.classifier
import
Classifier
...
...
@@ -96,6 +96,7 @@ class CNNKeras(Classifier):
self
.
model
=
None
self
.
trained
=
False
self
.
labels_file
=
Config
(
"LabelsFile"
,
'../examples/labels.txt'
,
str
)
def
get_config
(
self
):
"""Return configuration of classifier.
...
...
@@ -194,8 +195,9 @@ class CNNKeras(Classifier):
List of predicted classes for each instance in test data in ordered way.
"""
predict_directory
=
File
.
make_path
(
dataset
,
test_dir
)
predict_directory
=
File
.
make_path
(
dataset
,
test_dir
)
CLASS_NAMES
=
np
.
loadtxt
(
self
.
labels_file
.
value
,
str
)
# Create a Keras class
# Create a Keras class
if
not
os
.
path
.
exists
(
File
.
make_path
(
predict_directory
,
"png"
)):
os
.
makedirs
(
File
.
make_path
(
predict_directory
,
"png"
))
...
...
@@ -213,17 +215,9 @@ class CNNKeras(Classifier):
print
(
e
)
else
:
print
(
File
.
make_path
(
predict_directory
,
file
))
os
.
symlink
(
File
.
make_path
(
predict_directory
,
file
),
File
.
make_path
(
predict_directory
,
'png'
,
file
))
classify_datagen
=
ImageDataGenerator
()
classify_generator
=
classify_datagen
.
flow_from_directory
(
File
.
make_path
(
predict_directory
,
'png'
),
target_size
=
(
IMG_HEIGHT
,
IMG_WIDTH
),
batch_size
=
1
,
shuffle
=
False
,
class_mode
=
None
)
#os.symlink(File.make_path(predict_directory, file),File.make_path(predict_directory, 'png', file))
print
(
CLASS_NAMES
)
try
:
# self.model.load_weights(
...
...
@@ -231,29 +225,47 @@ class CNNKeras(Classifier):
K
.
clear_session
()
if
self
.
weight_path
is
not
None
:
self
.
model
=
load_model
(
self
.
weight_path
)
path_classes
=
self
.
weight_path
.
replace
(
"_model.h5"
,
"_classes.npy"
)
#path_classes = self.weight_path.replace( "_model.h5", "_classes.npy")
print
(
"Load Model H5:"
+
self
.
weight_path
)
np_load_old
=
np
.
load
#
modify the default parameters of np.load
np
.
load
=
lambda
*
a
,
**
k
:
np_load_old
(
*
a
,
allow_pickle
=
True
,
**
k
)
#
np_load_old = np.load
#
modify the default parameters of np.load
#
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
CLASS_NAMES
=
np
.
load
(
path_classes
).
item
().
keys
()
#
CLASS_NAMES = np.load(path_classes).item().keys()
except
Exception
as
e
:
raise
IException
(
"Can't load the model in "
+
str
(
e
))
output_classification
=
self
.
model
.
predict_generator
(
classify_generator
)
classify_datagen
=
ImageDataGenerator
(
rescale
=
1.
/
255
)
one_hot_output
=
np
.
argmax
(
output_classification
,
axis
=
1
)
classify_generator
=
classify_datagen
.
flow_from_directory
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
File
.
make_path
(
predict_directory
,
"png"
))),
target_size
=
(
IMG_HEIGHT
,
IMG_WIDTH
),
batch_size
=
1
,
shuffle
=
False
,
class_mode
=
"categorical"
)
print
(
"Examples:"
)
print
(
classify_generator
.
samples
)
output_classification
=
self
.
model
.
predict_generator
(
classify_generator
,
classify_generator
.
samples
,
verbose
=
2
)
one_hot_output
=
one_hot_output
.
tolist
()
one_hot_output
=
np
.
argmax
(
output_classification
,
axis
=
1
)
print
(
len
(
one_hot_output
))
print
(
one_hot_output
)
for
index
in
range
(
0
,
len
(
one_hot_output
)):
one_hot_output
[
index
]
=
CLASS_NAMES
[
one_hot_output
[
index
]]
return
one_hot_output
#output_classification = self.model.predict_generator(classify_generator)
#one_hot_output = np.argmax(output_classification, axis=1)
#one_hot_output = one_hot_output.tolist()
classes
=
[]
for
index
in
range
(
0
,
len
(
one_hot_output
)):
#print(index)
classes
.
append
(
CLASS_NAMES
[
one_hot_output
[
index
]])
#print(len(classes))
return
classes
def
train
(
self
,
dataset
,
training_data
,
force
=
False
):
"""Perform the training of classifier.
...
...
src/pynovisao.py
View file @
9e4ef8cc
...
...
@@ -12,6 +12,7 @@ import numpy as np
import
os
import
interface
import
types
import
cv2
from
interface.interface
import
InterfaceException
as
IException
from
PIL
import
Image
...
...
@@ -39,6 +40,7 @@ from multiprocessing import Process, Manager
import
threading
as
th
from
tqdm
import
tqdm
import
psutil
class
Act
(
object
):
"""Store all actions of Pynovisao."""
...
...
@@ -556,7 +558,7 @@ class Act(object):
self
.
_gt_segments
=
[
None
]
*
(
max
(
list_segments
)
+
1
)
# New and optimized classification
tmp
=
"
.
tmp"
tmp
=
"tmp"
File
.
remove_dir
(
File
.
make_path
(
self
.
dataset
,
tmp
))
self
.
tk
.
append_log
(
"Generating test images... (%0.3f seconds)"
,
(
TimeUtils
.
get_time
()
-
start_time
))
...
...
@@ -584,10 +586,11 @@ class Act(object):
# Get the label corresponding to predict class for each segment of image.
labels
=
self
.
classifier
.
classify
(
self
.
dataset
,
test_dir
=
tmp
,
test_data
=
"test.arff"
,
image
=
self
.
_const_image
)
print
(
labels
)
File
.
remove_dir
(
File
.
make_path
(
self
.
dataset
,
tmp
))
# Result is the class for each superpixel
if
type
(
labels
)
is
types
.
ListType
:
if
len
(
labels
)
>
0
:
self
.
tk
.
append_log
(
"Painting segments... (%0.3f seconds)"
,
(
TimeUtils
.
get_time
()
-
start_time
))
# If ground truth mode, show alternative results
...
...
@@ -615,7 +618,7 @@ class Act(object):
len_classes
=
sum
([
len_segments
[
idx
]
for
idx
in
idx_segment
])
popup_info
+=
"%-16s%-16s%0.2f%%
\n
"
%
(
cl
[
"name"
].
value
,
str
(
len_classes
),
(
len_classes
*
100.0
)
/
len_total
)
self
.
tk
.
refresh_image
(
self
.
_image
)
self
.
tk
.
popup
(
popup_info
)
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment