def run(self, ngen=10, startfile=None, checkpoint=None): """Run the genetic algorithm, updating the population over ngen number of generations. Keywork arguments: ngen -- number of generations to run the genetic algorithm. startfile -- File containing existing population (default None) checkpoint -- File containing existing checkpoint (default None) Output: population -- Resulting population after ngen generations. """ if startfile: population = self.readpop(startfile) else: population = self.newpopulation() if checkpoint: self.writepop(population, filename=f"0_{checkpoint}") for cur_g in range(1, ngen + 1): print(f"generation {cur_g} of population size {len(population)}") population = self.nextgen(population) seg = Segmentors.algoFromParams(self.hof[0]) mask = seg.evaluate(self.img) fitness = Segmentors.FitnessFunction(self.mask, mask) print(f"#BEST - {fitness[0]} - {self.hof[0]}") if checkpoint: print(f"Writing Checkpoint file - {checkpoint}") self.writepop(population, filename=f"{checkpoint}_{cur_g}") for cur_p in range(len(population)): logging.getLogger().info(population[cur_p]) return population
def monitor(self): html_path = os.path.join(os.getcwd(), "web_pages", "monitor.html") if self.best_fit != -1: # If there is a segmentor then display the images segmentation img = imageio.imread(self.rgb_filename) gmask = imageio.imread(self.label_filename) print(self.best_ind["params"]) self.best_ind["params"] seg = Segmentors.algoFromParams(self.best_ind["params"]) mask = seg.evaluate(img) static_dir = os.path.join(os.getcwd(), "public") imageio.imwrite(os.path.join(static_dir, "mask.jpg"), mask) code = GeneticSearch.print_best_algorithm_code(self.best_ind["params"]) # Calculate progress bar precentage percentage = (1 - self.best_ind["fitness"]) * 100 rounded_fitness = float("{0:.2f}".format(self.best_ind["fitness"])) data = ["", code, self.best_ind["params"], rounded_fitness, percentage] else: data = ['style="display:none;"', "", "", "",""] return fill_html_template(html_path, data)
def periodic_update(n_intevals, src): if update_best_individual(): img = "Chameleon.jpg" segmenter = Segmentors.algoFromParams((results[0].get())["params"]) return "/static/" + tasks.evaluate_segmentation.delay(segmenter, img).get() return src
def run(self, ngen=10, population=None,startfile=None, checkpoint=None, cp_freq=1): """Run the genetic algorithm, updating the population over ngen number of generations. Keywork arguments: ngen -- number of generations to run the genetic algorithm. startfile -- File containing existing population (default None) checkpoint -- File containing existing checkpoint (default None) Output: population -- Resulting population after ngen generations. """ if startfile: try: print(f"Reading in {startfile}") population = self.readpop(startfile) except FileNotFoundError: print("WARNING: Start file not found") except: raise if not population: print(f"Inicializing new randome population") population = self.newpopulation() if checkpoint: self.writepop(population, filename=f"{checkpoint}") for cur_g in range(0, ngen+1): print(f"generation {cur_g} of population size {len(population)}") _, population = self.popfitness(population) bestsofar = self.hof[0] seg = Segmentors.algoFromParams(bestsofar) mask = seg.evaluate(self.img) fitness = Segmentors.FitnessFunction(mask, self.mask) print(f"#BEST - {fitness} - {bestsofar}") if checkpoint and cur_g%cp_freq == 0: print(f"Writing Checkpoint file - {checkpoint}") copyfile(f"{checkpoint}", f"{checkpoint}.prev") self.writepop(population, filename=f"{checkpoint}") for cur_p in range(len(population)): logging.getLogger().info(population[cur_p]) if cur_g < ngen+1: population = self.mutate(population) if checkpoint: print(f"Writing Checkpoint file - {checkpoint}") copyfile(f"{checkpoint}", f"{checkpoint}.prev") self.writepop(population, filename=f"{checkpoint}") for cur_p in range(len(population)): logging.getLogger().info(population[cur_p]) return population
def segmentwidget(img, gmask, params=None, alg=None): """Generate GUI. Produce slider for each parameter for the current segmentor. Show both options for the masked image. Keyword arguments: img -- original image gmask -- ground truth segmentation mask for the image params -- list of parameter options alg -- algorithm to search parameters over """ if params is None and alg is None: alg = 'FB' params = [alg, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\ (1, 1), 0, 'checkerboard', 'checkerboard', 0, 0, 0, 0, 0, 0] elif params is None and alg is not None: params = [alg, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,\ (1, 1), 0, 'checkerboard', 'checkerboard', 0, 0, 0, 0, 0, 0] elif params is not None and alg is not None: params[0] = alg seg = Segmentors.algoFromParams(params) widg = dict() widglist = [] for ppp in seg.paramindexes: thislist = eval(seg.params.ranges[ppp]) thiswidg = widgets.SelectionSlider(options=tuple(thislist), disabled=False, description=ppp, value=seg.params[ppp], continuous_update=False, orientation='horizontal', readout=True) widglist.append(thiswidg) widg[ppp] = thiswidg def func(img=img, mask=gmask, **kwargs): """Find mask and fitness for current algorithm. Show masked image.""" print(seg.params["algorithm"]) for k in kwargs: seg.params[k] = kwargs[k] mask = seg.evaluate(img) fit = Segmentors.FitnessFunction(mask, gmask) fig = showtwo(img, mask) plt.title('Fitness Value: ' + str(fit[0])) layout = widgets.Layout(grid_template_columns='1fr 1fr 1fr') u_i = widgets.GridBox(widglist, layout=layout) out = widgets.interactive_output(func, widg) display(u_i, out) return seg.params
def continuous_search(input_file, input_mask, startfile='', checkpoint='checkpoint.txt', best_mask_file="temp_mask.png", pop_size=10): img = imageio.imread(input_file) gmask = imageio.imread(input_mask) fid_out = open(f"{input_file}.txt", "w+") #TODO: Read this file in and set population first #Run the search my_evolver = GeneticSearch.Evolver(img, gmask, pop_size=pop_size) best_fitness = 2.0 iteration = 0 while (best_fitness > 0.0): print(f"running {iteration} iteration") if (startfile): population = my_evolver.run(ngen=1, startfile=None) startfile = None else: population = my_evolver.run(ngen=1) #Get the best algorithm so far params = my_evolver.hof[0] #Generate mask of best so far. seg = Segmentors.algoFromParams(params) mask = seg.evaluate(img) fitness = Segmentors.FitnessFunction(mask, gmask)[0] if (fitness < best_fitness): best_fitness = fitness print( f"\n\n\n\nIteration {iteration} Finess Improved to {fitness}") my_evolver.writepop(population, filename="checkpoint.pop") #imageio.imwrite(best_mask_file,mask); fid_out.write(f"[{iteration}, {fitness}, {params}]\n") fid_out.flush() ###TODO Output [fitness, seg] iteration += 1
def print_best_algorithm_code(individual): """Print usable code to run segmentation algorithm based on an individual's genetic representation vector.""" ind_algo = Segmentors.algoFromParams(individual) original_function = inspect.getsource(ind_algo.evaluate) function_contents = original_function[original_function.find(' '):\ original_function.find('return')] while function_contents.find('self.params') != -1: function_contents = function_contents.replace( function_contents[function_contents.find('self.params'):function_contents.find(']')+1],\ str(ind_algo.params[function_contents[function_contents.find('self.params') + 13:\ function_contents.find(']')-1]])) function_contents = function_contents.replace(' ', '') function_contents = function_contents[function_contents.find('\n\"\"\"') + 5:] print(function_contents) return function_contents
def conduct_genetic_search(img, gmask, num_gen, pop_size): """ Note: this task could be sped up by rewriting it to send the evaluation and fitness function calls to other workers as tasks. """ # Only needed on some images # Convert the RGB 3-channel image into a 1-channel image # gmask = (np.sum(gmask, axis=2) > 0) download_and_store_image(img) download_and_store_image(gmask) img = imageio.imread(get_path(img)) gmask = imageio.imread(get_path(gmask)) my_evolver = GeneticSearch.Evolver(img, gmask, pop_size=pop_size) # Conduct the genetic search population = None for _ in range(num_gen): # if population is uninitialized if population is None: population = my_evolver.run(ngen=1) else: # Simulate a generation and store population in population variable population = my_evolver.run(ngen=1, population=population) # Take the best segmentor from the hof and use it to segment the rgb image seg = Segmentors.algoFromParams(my_evolver.hof[0]) mask = seg.evaluate(img) # Calculate and print the fitness value of the segmentor fitness = Segmentors.FitnessFunction(mask, gmask)[0] params = my_evolver.hof[0] # Combine data into a single object data = {} data["fitness"] = fitness data["params"] = params return data
def print_best_algorithm_code(individual): """Print usable code to run segmentation algorithm based on an individual's genetic representation vector.""" ind_algo = Segmentors.algoFromParams(individual) original_function = inspect.getsource(ind_algo.evaluate) # Get the body of the function function_contents = original_function[original_function.find(' '):\ original_function.find('return')] while function_contents.find('self.params') != -1: # Find the index of the 's' at the start of self.params params_index = function_contents.find('self.params') # Find the index of the ']' at the end of self.params["<SOME_TEXT>"] end_bracket_index = function_contents.find(']', params_index) + 1 # Find the first occurance of self.params["<SOME_TEXT>"] and store it code_to_replace = function_contents[params_index:end_bracket_index] # These offset will be used to access only the params_key offset = len('self.params["') offset2 = len('"]') # Get the params key params_key = function_contents[params_index + offset:end_bracket_index - offset2] # Use the params_key to access the params_value param_value = str(ind_algo.params[params_key]) # Replace self.params["<SOME_TEXT>"] with the value of self.params["<SOME_TEXT>"] function_contents = function_contents.replace(code_to_replace, param_value) function_contents = function_contents.replace(' ', '') function_contents = function_contents[function_contents.find('\n\"\"\"') + 5:] print(function_contents) return function_contents
args = parser.parse_args() print(args) random.seed(args.seed) logging.basicConfig(stream=sys.stdout, level=logging.ERROR) #logging.basicConfig(stream=sys.stdout, level=logging.INFO) img = imageio.imread(args.image) gmask = imageio.imread(args.mask) if len(gmask.shape) > 2: gmask = color.rgb2gray(gmask) ee = GeneticSearch.Evolver(img, gmask, pop_size=args.pop) ee.run(args.generations, checkpoint=args.checkpointfile) seg = Segmentors.algoFromParams(ee.hof[0]) mask = seg.evaluate(img) imageio.imwrite(args.outputfolder+"file.jpg", mask) fitness,_ = Segmentors.FitnessFunction(mask,gmask) print(f"{fitness} {ee.hof[0]}")
def segmentwidget(img, gmask, params=None, alg=None): """Generate GUI. Produce slider for each parameter for the current segmentor. Show both options for the masked image. Keyword arguments: img -- original image gmask -- ground truth segmentation mask for the image params -- list of parameter options alg -- algorithm to search parameters over """ if params: if alg: params[0] = alg; seg = Segmentors.algoFromParams(params) else: if alg: algorithm_gen = Segmentors.algorithmspace[alg] seg = algorithm_gen() else: seg = Segmentors.segmentor() widg = dict() widglist = [] for ppp, ind in zip(seg.paramindexes, range(len(seg.paramindexes))): thislist = eval(seg.params.ranges[ppp]) name = ppp current_value = seg.params[ppp] if not current_value in thislist: #TODO: We should find the min distance between current_value and this list and use that instead. current_value = thislist[0] thiswidg = widgets.SelectionSlider(options=tuple(thislist), disabled=False, description=name, value=current_value, continuous_update=False, orientation='horizontal', readout=True ) widglist.append(thiswidg) widg[ppp] = thiswidg # algorithms = list(Segmentors.algorithmspace.keys()) # w = widgets.Dropdown( # options=algorithms, # value=algorithms[0], # description='Choose Algorithm:', # ) def func(img=img, mask=gmask, **kwargs): """Find mask and fitness for current algorithm. Show masked image.""" print(seg.params["algorithm"]) for k in kwargs: seg.params[k] = kwargs[k] mask = seg.evaluate(img) fit = Segmentors.FitnessFunction(mask, gmask) fig = showtwo(img, mask) # I like the idea of printing the sharepython but it should be below the figures. #print(seg.sharepython(img)) # plt.title('Fitness Value: ' + str(fit[0])) layout = widgets.Layout(grid_template_columns='1fr 1fr 1fr') u_i = widgets.GridBox(widglist, layout=layout) out = widgets.interactive_output(func, widg) display(u_i, out) return seg.params
my_evolver = GeneticSearch.Evolver(img, gmask, pop_size=pop_size) # Conduct the genetic search population = None for i in range(num_gen): # if population is uninitialized if population is None: population = my_evolver.run(ngen=1) else: # Simulate a generation and store population in population variable population = my_evolver.run(ngen=1, population=population) # Take the best segmentor from the hof and use it to segment the rgb image seg = Segmentors.algoFromParams(my_evolver.hof[0]) mask = seg.evaluate(img) # Calculate and print the fitness value of the segmentor fitness = Segmentors.FitnessFunction(mask, gmask)[0] params = my_evolver.hof[0] # Combine data into a single object data = {} data["fitness"] = fitness data["params"] = params # Convert the data to json format data = json.dumps(data) print(i, data, "\n\n")
def run(self, ngen=10, population=None,startfile=None, checkpoint=None, cp_freq=1): """Run the genetic algorithm, updating the population over ngen number of generations. Keywork arguments: ngen -- number of generations to run the genetic algorithm. startfile -- File containing existing population (default None) checkpoint -- File containing existing checkpoint (default None) Output: population -- Resulting population after ngen generations. """ if startfile: try: print(f"Reading in {startfile}") population = self.readpop(startfile) except FileNotFoundError: print("WARNING: Start file not found") except: raise if not population: print(f"Initializing a new random population") population = self.newpopulation() if checkpoint: self.writepop(population, filename=f"{checkpoint}") for cur_g in range(0, ngen+1): print(f"Generation {cur_g} of population size {len(population)}") histogram = Segmentors.popCounts(population) print(f"#HIST - {histogram}") _, population = self.popfitness(population) bestsofar = self.hof[0] seg = Segmentors.algoFromParams(bestsofar) mask = seg.evaluate(self.img) fitness = Segmentors.FitnessFunction(mask, self.mask) print(f"#BEST - {fitness} - {bestsofar}") if checkpoint and cur_g%cp_freq == 0: print(f"Writing Checkpoint file - {checkpoint}") copyfile(f"{checkpoint}", f"{checkpoint}.prev") self.writepop(population, filename=f"{checkpoint}") for cur_p in range(len(population)): logging.getLogger().info(population[cur_p]) if cur_g < ngen+1: if bestsofar.fitness.values[0] >= 0.95: population = self.newpopulation() # if the best fitness value is at or above the # threshold of 0.95, discard the entire current # population and randomly select a new population # for the next generation # note: setting keep_prob = 0 and mutate_prob = 1 # as mutate arguments # should have same result as self.new_population() else: population = self.mutate(population) # if the best fitness value is below this threshold, # proceed as normal, mutating the current population # to get the next generation if checkpoint: print(f"Writing Checkpoint file - {checkpoint}") copyfile(f"{checkpoint}", f"{checkpoint}.prev") self.writepop(population, filename=f"{checkpoint}") for cur_p in range(len(population)): logging.getLogger().info(population[cur_p]) return population
img = imageio.imread(imagefile) gmask = imageio.imread(maskfile) #Run random Search random.seed(args.seed) ee = GeneticSearch.Evolver(img, gmask, pop_size=args.pop) population = ee.run(args.generations, startfile=startfile, checkpoint=args.checkpoint, cp_freq=9999) params = ee.hof[0] #Create segmentor from params file = open(f"{args.outputfolder}params.txt", "w") file.write(str(params) + "\n") seg = Segmentors.algoFromParams(params) #Loop though images for imagename, maskname, outputname in zip(imagefiles, maskfiles, outputfiles): # Loop over image files img = imageio.imread(imagename) gmask = imageio.imread(maskname) if len(gmask.shape) > 2: gmask = color.rgb2gray(gmask) #Evaluate image mask = seg.evaluate(img) #Save Mask to output