Commit ec22f9ee authored by Felipe Silveira's avatar Felipe Silveira
Browse files

Adicionados novos parametros para setar as configurações

parent 1f838135
......@@ -29,12 +29,12 @@ from util.config import Config
_classifier_list = OrderedDict( [
["weka_classifiers", Config("Invalid" if WekaClassifiers is None else WekaClassifiers.__name__,
WekaClassifiers is not None, bool, meta=WekaClassifiers, hidden=WekaClassifiers is None)],
["cnn_caffe", Config("Invalid" if CNNCaffe is None else CNNCaffe.__name__,
WekaClassifiers is None and CNNCaffe is not None, bool, meta=CNNCaffe, hidden=CNNCaffe is None)],
["cnn_keras", Config("Invalid" if CNNKeras is None else CNNKeras.__name__,
CNNKeras is not None, bool, meta=CNNKeras, hidden=CNNKeras is None)],
["weka_classifiers", Config("Invalid" if WekaClassifiers is None else WekaClassifiers.__name__,
WekaClassifiers is not None, bool, meta=WekaClassifiers, hidden=WekaClassifiers is None)]
CNNKeras is not None, bool, meta=CNNKeras, hidden=CNNKeras is None)]
] )
def get_classifier_config():
......
......@@ -37,8 +37,6 @@ START_TIME = time.time()
# =========================================================
IMG_WIDTH, IMG_HEIGHT = 256, 256
BATCH_SIZE = 16
EPOCHS = 1
CLASS_NAMES = ['ferrugemAsiatica', 'folhaSaudavel',
'fundo', 'manchaAlvo', 'mildio', 'oidio']
......@@ -46,13 +44,19 @@ CLASS_NAMES = ['ferrugemAsiatica', 'folhaSaudavel',
class CNNKeras(Classifier):
""" Class for CNN classifiers based on Keras applications """
def __init__(self, architecture = "VGG16", fine_tuning_rate = 100):
def __init__(self, architecture="VGG16", learning_rate=0.0001, momentum=0.9, batch_size=16, epochs=10, fine_tuning_rate=100, transfer_learning=False, save_weights=False):
"""
Constructor of CNNKeras
"""
self.architecture = Config("architecture", architecture, str)
self.fine_tuning_rate = Config("fineTuningRate", fine_tuning_rate, int)
self.architecture = Config("Architecture", architecture, str)
self.learning_rate = Config("Learning rate", learning_rate, float)
self.momentum = Config("Momentum", momentum, float)
self.batch_size = Config("Batch size", batch_size, int)
self.epochs = Config("Epochs", epochs, int)
self.fine_tuning_rate = Config("Fine Tuning rate", fine_tuning_rate, int)
self.transfer_learning = Config("Transfer Learning", transfer_learning, bool)
self.save_weights = Config("Save weights", save_weights, bool)
self.file_name = "kerasCNN"
......@@ -75,7 +79,7 @@ class CNNKeras(Classifier):
# compile the model
self.model.compile(loss="categorical_crossentropy",
optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
optimizer=optimizers.SGD(lr=self.learning_rate.value, momentum=self.momentum.value),
metrics=["accuracy"])
def get_config(self):
......@@ -88,8 +92,14 @@ class CNNKeras(Classifier):
"""
keras_config = OrderedDict()
keras_config["architecture"] = self.architecture
keras_config["fine_tuning_rate"] = self.fine_tuning_rate
keras_config["Architecture"] = self.architecture
keras_config["Learning rate"] = self.learning_rate
keras_config["Momentum"] = self.momentum
keras_config["Batch size"] = self.batch_size
keras_config["Epochs"] = self.epochs
keras_config["Fine Tuning rate"] = self.fine_tuning_rate
keras_config["Transfer Learning"] = self.transfer_learning
keras_config["Save weights"] = self.save_weights
return keras_config
......@@ -103,11 +113,29 @@ class CNNKeras(Classifier):
"""
keras_config = OrderedDict()
keras_config["architecture"] = Config.nvl_config(
configs["architecture"], self.architecture)
keras_config["Architecture"] = Config.nvl_config(
configs["Architecture"], self.architecture)
keras_config["fine_tuning_rate"] = Config.nvl_config(
configs["fine_tuning_rate"], self.fine_tuning_rate)
keras_config["Learning rate"] = Config.nvl_config(
configs["Learning rate"], self.learning_rate)
keras_config["Momentum"] = Config.nvl_config(
configs["Momentum"], self.momentum)
keras_config["Batch size"] = Config.nvl_config(
configs["Batch size"], self.batch_size)
keras_config["Epochs"] = Config.nvl_config(
configs["Epochs"], self.epochs)
keras_config["Fine Tuning rate"] = Config.nvl_config(
configs["Fine Tuning rate"], self.fine_tuning_rate)
keras_config["Transfer Learning"] = Config.nvl_config(
configs["Transfer Learning"], self.transfer_learning)
keras_config["Save weights"] = Config.nvl_config(
configs["Save weights"], self.save_weights)
def get_summary_config(self):
"""Return fomatted summary of configuration.
......@@ -118,14 +146,20 @@ class CNNKeras(Classifier):
Formatted string with summary of configuration.
"""
keras_config = OrderedDict()
keras_config[self.architecture.label] = self.architecture.value
keras_config[self.learning_rate.label] = self.learning_rate.value
keras_config[self.momentum.label] = self.momentum.value
keras_config[self.batch_size.label] = self.batch_size.value
keras_config[self.epochs.label] = self.epochs.value
keras_config[self.fine_tuning_rate.label] = self.fine_tuning_rate.value
keras_config[self.transfer_learning.label] = self.transfer_learning.value
keras_config[self.save_weights.label] = self.save_weights.value
summary = ''
for config in keras_config:
summary += "%s: %s\n" % (config, str(keras_config[config]))
return summary
def classify(self, dataset, test_dir, test_data):
......@@ -171,7 +205,7 @@ class CNNKeras(Classifier):
classify_generator = classify_datagen.flow_from_directory(
predict_directory,
target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=BATCH_SIZE,
batch_size=self.batch_size,
shuffle=False,
class_mode=None)
......@@ -203,7 +237,7 @@ class CNNKeras(Classifier):
train_generator = train_datagen.flow_from_directory(
dataset,
target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=BATCH_SIZE,
batch_size=self.batch_size,
shuffle=True,
class_mode="categorical")
......@@ -231,8 +265,8 @@ class CNNKeras(Classifier):
# Train the model
self.model.fit_generator(
train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=EPOCHS,
steps_per_epoch=train_generator.samples // self.batch_size,
epochs=self.epochs,
callbacks=[checkpoint])
def must_train(self):
......
......@@ -76,7 +76,7 @@ if __name__ == "__main__":
tk.add_separator()
tk.add_command("Execute", act.run_extractors, 'F')
tk.add_menu("Classification")
tk.add_menu("Training")
tk.add_command("Choose classifier", act.select_classifier)
tk.add_command("Configure", act.configure_classifier)
tk.add_separator()
......@@ -87,6 +87,7 @@ if __name__ == "__main__":
tk.add_separator()
tk.add_command("Execute", act.run_classifier, 'C')
tk.add_menu("Classification")
tk.add_menu("Help")
tk.add_command("About Pynovisao", act.about, 'b')
......
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