Commit b45dc089 authored by Gilberto Astolfi's avatar Gilberto Astolfi
Browse files

Merge branch 'feature-graphic-confusion-matrix'

parents 1c4e93d9 8390e5c3
......@@ -522,3 +522,9 @@ class CNNKeras(Classifier):
class_mode="categorical")
return train_generator, validation_generator, test_generator
def single_classify(self, image_path, directory, extractors, dict_classes):
from matplotlib.pylab import imread
image = imread(image_path)
predict = self.model.predict([image])[0]
return dict_classes[predict]
\ No newline at end of file
......@@ -269,3 +269,18 @@ class WekaClassifiers(Classifier):
"""
self.data = None
self.classifier = None
def single_classify(self, image_path, directory, extractors, dict_classes):
'''
'''
from extraction import FeatureExtractor
from os import remove
test_file = 'temp'
fextractor=FeatureExtractor(extractors)
fextractor.extract_one_file(directory, image_path, output_file = test_file)
predicted = self.classify(directory, test_dir='.tmp', test_data=test_file+'.arff', image=None)
remove(directory+'/'+test_file+'.arff')
return predicted[0]
\ No newline at end of file
......@@ -89,10 +89,12 @@ if __name__ == "__main__":
tk.add_command("Load h5 weight (only for CNNs)", act.open_weight)
tk.add_command("Execute", act.run_classifier, 'C')
tk.add_command("Execute folder", act.run_classifier_folder)
tk.add_menu("Experimenter")
tk.add_check_button("Ground Truth", act.toggle_ground_truth, default_state = False)
tk.add_separator()
tk.add_command("Execute Graphical Confusion Matrix", act.run_grafic_confusion_matrix)
tk.add_separator()
tk.add_command("Cross Validation", act.cross_validation, 'X')
tk.add_command("Experimenter All", act.experimenter_all, 'p')
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
This file must contain the implementation code for all actions of pynovisao.
......@@ -45,6 +43,7 @@ class Act(object):
Arguments of program.
"""
self.tk = tk
self.has_trained = False
self.segmenter = [segmentation._segmenter_list[segmenter].meta for segmenter in segmentation._segmenter_list
if segmentation._segmenter_list[segmenter].value == True ][0]()
......@@ -329,6 +328,7 @@ class Act(object):
self.tk.refresh_panel_classes(self.classes)
if self.classifier: self.classifier.reset()
self.has_trained=False
def toggle_dataset_generator(self):
"""Enable/disable the dataset generator on click in image.
......@@ -463,7 +463,7 @@ class Act(object):
def process_config():
"""Update the current classifier."""
new_config = self.tk.get_config_and_destroy()
self.classifier = [new_config[classifier].meta for classifier in new_config
if new_config[classifier].value == True ][0]()
......@@ -628,6 +628,7 @@ class Act(object):
self.tk.append_log("DONE (%0.3f seconds)", (TimeUtils.get_time() - start_time))
self._image = self._const_image
self.has_trained=True
def _show_ground_truth(self, list_segments, len_segments, labels, start_time):
......@@ -941,3 +942,84 @@ class Act(object):
np.savetxt(f, all_frequency_weighted_IU, fmt='%.5f')
f.close()
def run_grafic_confusion_matrix(self):
'''
Generate a a graphical confusion matrix where images are classified and according to classification go to the wrong or right folder.
'''
from classification import WekaClassifiers
if not isinstance(self.classifier, WekaClassifiers):
message='Only available to Weka Classifiers.'
IException(message)
if not self.has_trained:
message='Dataset Must Be Trained.'
IException(message)
from os.path import abspath
directory = self.tk.utils.ask_directory()
if not directory:
message = 'No directory selected.'
IException(message)
return
directory = abspath(directory)
dataset = abspath(self.dataset)
if directory == self.dataset:
title = 'Same Dataset'
message = 'The dataset selected is the same of the trained. Are you sure that is right?'
option=self.tk.ask_ok_cancel(title, message)
if not option:
return
from shutil import rmtree
from os import mkdir, listdir
from os.path import isdir
from os import symlink
def create_folder_struct(matrix_path, class_names, human, computer):
try:
rmtree(matrix_path)
except Exception as e:
print str(e)
pass
mkdir(matrix_path, 0777)
for class_ in class_names:
real=matrix_path+human+class_+'/'
mkdir(real, 0777)
for _class in class_names:
mkdir(real+computer+_class, 0777)
index=directory[-2::-1].index('/')
matrix_path=directory[:-(index+1)]+'folder_confusion_matrix'
class_names, classes=listdir(directory), {}
for i in range(len(class_names)-1,-1,-1):
if isdir(dataset+'/'+class_names[i]):
if class_names[i][0] is not '.':
continue
del class_names[i]
for i, name in enumerate(class_names):
classes[name], classes[i]=i, name
images=[]
for classe in class_names:
image_names=listdir(directory+'/'+classe)
for i in range(len(image_names)):
image_names[i]=directory+'/',classe ,'/'+image_names[i]
images.extend(image_names)
human, computer = '/human_', '/computer_'
create_folder_struct(matrix_path, class_names, human, computer)
for image_path in images:
original=reduce(lambda a,b:a+b, image_path)
real_class_path=matrix_path+human+image_path[1]
predicted=self.classifier.single_classify(original, directory, self.extractors, classes)
predicted_class_path = real_class_path+computer+predicted
name_predicted=predicted_class_path+image_path[2]
symlink(original, name_predicted)
message = 'Graphical Confusion Matrix saved in '+matrix_path
self.tk.append_log(message)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment