def evolve(n, debugging=False): if (debugging): debug = open("debug.txt", "w") else: debug = None config.load('configCIFAR10') # Create 2 separate populations (size is now defined explicitly, but config file can still be used) module_pop = population.Population(15, chromosome.ModuleChromo, debug=debug) # As the top hierarchical level, the blueprint population needs to be able to see the module population blueprint_pop = population.Population(10, chromosome.BlueprintChromo, module_pop, debug=debug) # Most of the actual evolving is now handled outside of the population, by CoDeepNEAT # Instead of requiring the user to overwrite the evaluation function, CoDeepNEAT evaluates the populations itself, # it simply requires a fitness function for the networks it creates passed in as an argument. codeepneat.epoch(n, blueprint_pop, module_pop, 25, fitness, data, save_best=True, name='CIFAR10', debug=debug) # It will still stop if fitness surpasses the max_fitness_threshold in config file # Plots the evolution of the best/average fitness visualize.plot_stats(module_pop.stats, name="CIFAR10mod_") visualize.plot_stats(blueprint_pop.stats, name="CIFAR10bp_")
def mutate_module(chromo_file): fp = open(chromo_file, "rb") #print(fp) chromo = pickle.load(fp) #print(chromo) fp.close() print("module:" + chromo_file) for connection in chromo._connections: print(str(connection)) counter = 0 config.load('configCIFAR10') inputs = keras.layers.Input(config.Config.input_nodes, name='input') module = chromo.decode(inputs) plot_model(module, to_file='mutate/module' + str(counter) + '.png', show_shapes=True) while True: counter += 1 mutate = input("Mutate? y/n: ") print() if mutate == "y": chromo._mutate_add_layer_debug() for connection in chromo._connections: print(str(connection)) config.load('configCIFAR10') inputs = keras.layers.Input(config.Config.input_nodes, name='input') module = chromo.decode(inputs) plot_model(module, to_file='mutate/module' + str(counter) + '.png', show_shapes=True) else: return
def eval_best(model_file, data): config.load('configCIFAR10') model = keras.models.load_model(model_file) datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization= False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range= 10, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(data[0]) csv_logger = CSVLogger(model_file + '_cifar10_new.csv') # Fit the model on the batches generated by datagen.flow(). model.fit_generator(datagen.flow(data[0], data[1], batch_size=batch_size), steps_per_epoch=data[0].shape[0] // batch_size, validation_data=(data[2], data[3]), epochs=epoch, verbose=1, max_queue_size=100, callbacks=[lr_reducer, early_stopper, csv_logger]) loss, fitness = model.evaluate(data[2], data[3]) print("fitness", fitness)
def visualize_module(chromo_file): #config.load('configCoverage') #chromosome.node_gene_type = genome.NodeGene fp = open(chromo_file, "rb") #print(fp) chromo = pickle.load(fp) print(chromo) fp.close() config.load('../configCIFAR10') inputs = keras.layers.Input(config.Config.input_nodes, name='input') module = chromo.decode(inputs) plot_model(module, to_file='module.png', show_shapes = True)