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 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)
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 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 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 test_FitnessFunction(): """Unit test for FitnessFunction function. Checks fitness value is as expected for a variety of extreme cases.""" # create test image ground_truth = np.zeros((20, 20)) ground_truth[4:10, 4:10] = 1 inferred = np.zeros((20, 20)) inferred[4:10, 4:6] = 1 inferred[4:10, 6:10] = 2 assert Segmentors.FitnessFunction(inferred, ground_truth) == [ 2**np.log(3), ] inferred = np.zeros((20, 20)) inferred[4:10, 3:6] = 1 inferred[4:10, 6:10] = 2 assert Segmentors.FitnessFunction(inferred, ground_truth) == [ 8**np.log(3), ] inferred = np.zeros((20, 20)) inferred[4:10, 3:6] = 1 inferred[4:10, 6:10] = 2 inferred[3:5, 3:6] = 3 assert Segmentors.FitnessFunction(inferred, ground_truth) == [ 9**np.log(4), ] inferred = np.zeros((20, 20)) assert Segmentors.FitnessFunction(inferred, ground_truth) == [ sys.maxsize, ] inferred = np.arange(400).reshape(ground_truth.shape) assert Segmentors.FitnessFunction(inferred, ground_truth) == [ 2**np.log(400), ] inferred = np.zeros((20, 20)) inferred[1:19, 1:19] = 1 assert Segmentors.FitnessFunction(inferred, ground_truth) == [ sys.maxsize, ]
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 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