Update geneticAlgorithm.py
Browse files- geneticAlgorithm.py +19 -32
geneticAlgorithm.py
CHANGED
|
@@ -3,10 +3,10 @@ import pygad
|
|
| 3 |
import numpy
|
| 4 |
from imageMulticlassClassification import ImageMulticlassClassification
|
| 5 |
|
| 6 |
-
def fitness_func(solution, solution_idx):
|
| 7 |
try:
|
| 8 |
-
print("solution_idx :",solution_idx)
|
| 9 |
-
print("solution :",solution)
|
| 10 |
neuronDense1 = [16, 32, 64, 128, 256, 512, 1024, 2048]
|
| 11 |
neuronDense2 = [16, 32, 64, 128, 256, 512, 1024, 2048]
|
| 12 |
Dropout1 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
|
|
@@ -15,30 +15,22 @@ def fitness_func(solution, solution_idx):
|
|
| 15 |
Activations = ["relu", "sigmoid", "softplus", "softsign", "tanh", "selu", "gelu", "linear"]
|
| 16 |
Optimizers = ["Adam", "RMSprop", "SGD", "Adadelta", "Adagrad", "Adamax", "Ftrl", "Nadam"]
|
| 17 |
LossFunction = ["SparseCategoricalCrossentropy", "CategoricalCrossentropy", "BinaryCrossentropy", "MeanAbsoluteError", "MeanSquaredError", "SquaredHinge", "CategoricalHinge", "CosineSimilarity"]
|
|
|
|
|
|
|
| 18 |
usedNeuronDense1 = neuronDense1[solution[0]]
|
| 19 |
-
print("==================================")
|
| 20 |
-
print(f"usedNeuronDense1 : {usedNeuronDense1}")
|
| 21 |
usedNeuronDense2 = neuronDense2[solution[1]]
|
| 22 |
-
print(f"usedNeuronDense2 : {usedNeuronDense2}")
|
| 23 |
usedDropout1 = Dropout1[solution[2]]
|
| 24 |
-
print(f"usedDropout1 : {usedDropout1}")
|
| 25 |
usedDropout2 = Dropout2[solution[3]]
|
| 26 |
-
print(f"usedDropout2 : {usedDropout2}")
|
| 27 |
usedBatchs = Batchs[solution[4]]
|
| 28 |
-
print(f"usedBatchs : {usedBatchs}")
|
| 29 |
usedActivations = Activations[solution[5]]
|
| 30 |
-
print(f"usedActivations : {usedActivations}")
|
| 31 |
usedOptimizers = Optimizers[solution[6]]
|
| 32 |
-
print(f"usedOptimizers : {usedOptimizers}")
|
| 33 |
usedLossFunction = LossFunction[solution[7]]
|
| 34 |
-
print(f"usedLossFunction : {usedLossFunction}")
|
| 35 |
-
print("==================================")
|
| 36 |
|
| 37 |
-
imgWidth=50
|
| 38 |
-
imgHeight=50
|
| 39 |
-
batchSize=usedBatchs
|
| 40 |
-
IMC = ImageMulticlassClassification(imgWidth,imgHeight,batchSize)
|
| 41 |
-
IMC.data_MakeDataset(datasetUrl="https://huggingface.co/datasets/S1223/HandGestureDataset/resolve/main/HandGestureDataset.tgz",datasetDirectoryName="HandGestureDataset", ratioValidation=0.20)
|
| 42 |
IMC.data_PreprocessingDataset()
|
| 43 |
customModel = tf.keras.Sequential()
|
| 44 |
customModel.add(tf.keras.layers.Conv2D(16, (3, 3), input_shape=(imgWidth, imgHeight, 3), activation=usedActivations))
|
|
@@ -51,28 +43,22 @@ def fitness_func(solution, solution_idx):
|
|
| 51 |
customModel.add(tf.keras.layers.Dense(usedNeuronDense2, activation=usedActivations))
|
| 52 |
customModel.add(tf.keras.layers.Dropout(usedDropout2))
|
| 53 |
customModel.add(tf.keras.layers.Dense(10, activation="softmax"))
|
| 54 |
-
IMC.model_make(customModel)
|
| 55 |
modelName = ""
|
| 56 |
for x in solution:
|
| 57 |
modelName += f"{str(x)}_"
|
| 58 |
-
IMC.training_model(epochs=
|
| 59 |
-
IMC.evaluation(labelName=["0","1","2","3","4","5","6","7","8","9"])
|
| 60 |
output = float(IMC.history.history["val_accuracy"][-1])
|
| 61 |
-
# output = numpy.max(solution)
|
| 62 |
-
# print(output)
|
| 63 |
-
# print(type(output))
|
| 64 |
-
print("fitness :",output)
|
| 65 |
fitness = output
|
| 66 |
return fitness
|
| 67 |
except Exception as e:
|
| 68 |
print(str(e))
|
| 69 |
return 0.00001
|
| 70 |
|
| 71 |
-
function_inputs = [1,2,3,4,5,6,7,8]
|
| 72 |
desired_output = 5
|
| 73 |
|
| 74 |
-
fitness_function = fitness_func
|
| 75 |
-
|
| 76 |
num_generations = 1
|
| 77 |
num_parents_mating = 4
|
| 78 |
|
|
@@ -92,7 +78,7 @@ mutation_percent_genes = 'default'
|
|
| 92 |
|
| 93 |
ga_instance = pygad.GA(num_generations=num_generations,
|
| 94 |
num_parents_mating=num_parents_mating,
|
| 95 |
-
fitness_func=
|
| 96 |
sol_per_pop=sol_per_pop,
|
| 97 |
num_genes=num_genes,
|
| 98 |
init_range_low=init_range_low,
|
|
@@ -103,12 +89,13 @@ ga_instance = pygad.GA(num_generations=num_generations,
|
|
| 103 |
mutation_type=mutation_type,
|
| 104 |
mutation_percent_genes=mutation_percent_genes,
|
| 105 |
gene_type=[int, int, int, int, int, int, int, int],
|
| 106 |
-
allow_duplicate_genes=False,
|
| 107 |
-
save_best_solutions=False,
|
| 108 |
save_solutions=False)
|
|
|
|
| 109 |
print("Initial Population")
|
| 110 |
print(ga_instance.initial_population)
|
| 111 |
print(ga_instance.run())
|
| 112 |
solution, solution_fitness, solution_idx = ga_instance.best_solution()
|
| 113 |
print("Parameters of the best solution : {solution}".format(solution=solution))
|
| 114 |
-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
|
|
|
|
| 3 |
import numpy
|
| 4 |
from imageMulticlassClassification import ImageMulticlassClassification
|
| 5 |
|
| 6 |
+
def fitness_func(ga_instance, solution, solution_idx):
|
| 7 |
try:
|
| 8 |
+
print("solution_idx :", solution_idx)
|
| 9 |
+
print("solution :", solution)
|
| 10 |
neuronDense1 = [16, 32, 64, 128, 256, 512, 1024, 2048]
|
| 11 |
neuronDense2 = [16, 32, 64, 128, 256, 512, 1024, 2048]
|
| 12 |
Dropout1 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
|
|
|
|
| 15 |
Activations = ["relu", "sigmoid", "softplus", "softsign", "tanh", "selu", "gelu", "linear"]
|
| 16 |
Optimizers = ["Adam", "RMSprop", "SGD", "Adadelta", "Adagrad", "Adamax", "Ftrl", "Nadam"]
|
| 17 |
LossFunction = ["SparseCategoricalCrossentropy", "CategoricalCrossentropy", "BinaryCrossentropy", "MeanAbsoluteError", "MeanSquaredError", "SquaredHinge", "CategoricalHinge", "CosineSimilarity"]
|
| 18 |
+
|
| 19 |
+
# Use the 'solution' array to access the genes.
|
| 20 |
usedNeuronDense1 = neuronDense1[solution[0]]
|
|
|
|
|
|
|
| 21 |
usedNeuronDense2 = neuronDense2[solution[1]]
|
|
|
|
| 22 |
usedDropout1 = Dropout1[solution[2]]
|
|
|
|
| 23 |
usedDropout2 = Dropout2[solution[3]]
|
|
|
|
| 24 |
usedBatchs = Batchs[solution[4]]
|
|
|
|
| 25 |
usedActivations = Activations[solution[5]]
|
|
|
|
| 26 |
usedOptimizers = Optimizers[solution[6]]
|
|
|
|
| 27 |
usedLossFunction = LossFunction[solution[7]]
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
imgWidth = 50
|
| 30 |
+
imgHeight = 50
|
| 31 |
+
batchSize = usedBatchs
|
| 32 |
+
IMC = ImageMulticlassClassification(imgWidth, imgHeight, batchSize)
|
| 33 |
+
IMC.data_MakeDataset(datasetUrl="https://huggingface.co/datasets/S1223/HandGestureDataset/resolve/main/HandGestureDataset.tgz", datasetDirectoryName="HandGestureDataset", ratioValidation=0.20)
|
| 34 |
IMC.data_PreprocessingDataset()
|
| 35 |
customModel = tf.keras.Sequential()
|
| 36 |
customModel.add(tf.keras.layers.Conv2D(16, (3, 3), input_shape=(imgWidth, imgHeight, 3), activation=usedActivations))
|
|
|
|
| 43 |
customModel.add(tf.keras.layers.Dense(usedNeuronDense2, activation=usedActivations))
|
| 44 |
customModel.add(tf.keras.layers.Dropout(usedDropout2))
|
| 45 |
customModel.add(tf.keras.layers.Dense(10, activation="softmax"))
|
| 46 |
+
IMC.model_make(customModel)
|
| 47 |
modelName = ""
|
| 48 |
for x in solution:
|
| 49 |
modelName += f"{str(x)}_"
|
| 50 |
+
IMC.training_model(epochs=50, modelName=modelName, optimizer=usedOptimizers, lossFunction=usedLossFunction)
|
| 51 |
+
IMC.evaluation(labelName=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
|
| 52 |
output = float(IMC.history.history["val_accuracy"][-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
fitness = output
|
| 54 |
return fitness
|
| 55 |
except Exception as e:
|
| 56 |
print(str(e))
|
| 57 |
return 0.00001
|
| 58 |
|
| 59 |
+
function_inputs = [1, 2, 3, 4, 5, 6, 7, 8]
|
| 60 |
desired_output = 5
|
| 61 |
|
|
|
|
|
|
|
| 62 |
num_generations = 1
|
| 63 |
num_parents_mating = 4
|
| 64 |
|
|
|
|
| 78 |
|
| 79 |
ga_instance = pygad.GA(num_generations=num_generations,
|
| 80 |
num_parents_mating=num_parents_mating,
|
| 81 |
+
fitness_func=fitness_func,
|
| 82 |
sol_per_pop=sol_per_pop,
|
| 83 |
num_genes=num_genes,
|
| 84 |
init_range_low=init_range_low,
|
|
|
|
| 89 |
mutation_type=mutation_type,
|
| 90 |
mutation_percent_genes=mutation_percent_genes,
|
| 91 |
gene_type=[int, int, int, int, int, int, int, int],
|
| 92 |
+
allow_duplicate_genes=False,
|
| 93 |
+
save_best_solutions=False,
|
| 94 |
save_solutions=False)
|
| 95 |
+
|
| 96 |
print("Initial Population")
|
| 97 |
print(ga_instance.initial_population)
|
| 98 |
print(ga_instance.run())
|
| 99 |
solution, solution_fitness, solution_idx = ga_instance.best_solution()
|
| 100 |
print("Parameters of the best solution : {solution}".format(solution=solution))
|
| 101 |
+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
|