once-for-all-GM/evolution_ws.py
def run_evolution_search(self, constraint_lower, constraint_upper, input_size=224, verbose=False, **kwargs):
		"""Run a single roll-out of regularized evolution to a fixed time budget."""
		self.update_hyper_params(kwargs)

		mutation_numbers = int(round(self.mutation_ratio * self.population_size))
		parents_size = int(round(self.parent_ratio * self.population_size))

		best_valids = [-100]
		population = []  # (validation, sample, latency) tuples
		child_pool = []
		efficiency_pool = []
		best_info = None
		if verbose:
			print('Generate random population...')
		for _ in range(self.population_size):
			sample, efficiency = self.random_valid_sample(constraint_lower, constraint_upper, input_size=input_size)
			child_pool.append(sample)
			efficiency_pool.append(efficiency)

		accs = self.accuracy_predictor.get_arch_acc(child_pool, efficiency_pool)
		print('finish calculate the accuracy of models')
		for i in range(self.population_size):
			population.append((accs[i], child_pool[i], efficiency_pool[i]))

		if verbose:
			print('Start Evolution...')
		# After the population is seeded, proceed with evolving the population.
		with tqdm(total=self.max_time_budget, desc='Searching with constraint (%s)' % constraint_upper,
		          disable=(not verbose)) as t:
			for i in range(self.max_time_budget):
				parents = sorted(population, key=lambda x: x[0])[::-1][:parents_size]
				acc = parents[0][0]
				t.set_postfix({
					'acc': parents[0][0]
				})
				if not verbose and (i + 1) % 100 == 0:
					print('Iter: {} Acc: {}'.format(i + 1, parents[0][0]))

				if acc > best_valids[-1]:
					best_valids.append(acc)
					best_info = parents[0]
				else:
					best_valids.append(best_valids[-1])

				population = parents
				child_pool = []
				efficiency_pool = []

				print('perfrom mutation')
				for j in range(mutation_numbers):
					par_sample = population[np.random.randint(parents_size)][1]
					# Mutate
					new_sample, efficiency = self.mutate_sample(par_sample, constraint_lower, constraint_upper, input_size=input_size)
					child_pool.append(new_sample)
					efficiency_pool.append(efficiency)

				print('perfrom crossover')
				for j in range(self.population_size - mutation_numbers):
					par_sample1 = population[np.random.randint(parents_size)][1]
					par_sample2 = population[np.random.randint(parents_size)][1]
					# Crossover
					new_sample, efficiency = self.crossover_sample(par_sample1, par_sample2, constraint_lower, constraint_upper, input_size=input_size)
					child_pool.append(new_sample)
					efficiency_pool.append(efficiency)

				print('get model acc')
				accs = self.accuracy_predictor.get_arch_acc(child_pool, efficiency_pool)
				for j in range(self.population_size):
					population.append((accs[j], child_pool[j], efficiency_pool[j]))

				t.update(1)

		return best_valids, best_info
Run a single roll-out of regularized evolution to a fixed time budget.
Run a single roll-out of regularized evolution to a fixed time budget.
Run a single roll - out of regularized evolution to a fixed time budget .
def run_evolution_search(self, constraint_lower, constraint_upper, input_size=224, verbose=False, **kwargs):
		self.update_hyper_params(kwargs)
		mutation_numbers = int(round(self.mutation_ratio * self.population_size))
		parents_size = int(round(self.parent_ratio * self.population_size))
		best_valids = [-100]
		population = []  
		child_pool = []
		efficiency_pool = []
		best_info = None
		if verbose:
			print('Generate random population...')
		for _ in range(self.population_size):
			sample, efficiency = self.random_valid_sample(constraint_lower, constraint_upper, input_size=input_size)
			child_pool.append(sample)
			efficiency_pool.append(efficiency)
		accs = self.accuracy_predictor.get_arch_acc(child_pool, efficiency_pool)
		print('finish calculate the accuracy of models')
		for i in range(self.population_size):
			population.append((accs[i], child_pool[i], efficiency_pool[i]))
		if verbose:
			print('Start Evolution...')
		with tqdm(total=self.max_time_budget, desc='Searching with constraint (%s)' % constraint_upper,
		          disable=(not verbose)) as t:
			for i in range(self.max_time_budget):
				parents = sorted(population, key=lambda x: x[0])[::-1][:parents_size]
				acc = parents[0][0]
				t.set_postfix({
					'acc': parents[0][0]
				})
				if not verbose and (i + 1) % 100 == 0:
					print('Iter: {} Acc: {}'.format(i + 1, parents[0][0]))
				if acc > best_valids[-1]:
					best_valids.append(acc)
					best_info = parents[0]
				else:
					best_valids.append(best_valids[-1])
				population = parents
				child_pool = []
				efficiency_pool = []
				print('perfrom mutation')
				for j in range(mutation_numbers):
					par_sample = population[np.random.randint(parents_size)][1]
					new_sample, efficiency = self.mutate_sample(par_sample, constraint_lower, constraint_upper, input_size=input_size)
					child_pool.append(new_sample)
					efficiency_pool.append(efficiency)
				print('perfrom crossover')
				for j in range(self.population_size - mutation_numbers):
					par_sample1 = population[np.random.randint(parents_size)][1]
					par_sample2 = population[np.random.randint(parents_size)][1]
					new_sample, efficiency = self.crossover_sample(par_sample1, par_sample2, constraint_lower, constraint_upper, input_size=input_size)
					child_pool.append(new_sample)
					efficiency_pool.append(efficiency)
				print('get model acc')
				accs = self.accuracy_predictor.get_arch_acc(child_pool, efficiency_pool)
				for j in range(self.population_size):
					population.append((accs[j], child_pool[j], efficiency_pool[j]))
				t.update(1)
		return best_valids, best_info
def run_evolution_search ( self , constraint_lower , constraint_upper , input_size = 224 , verbose = False , ** kwargs ) : self . update_hyper_params ( kwargs ) mutation_numbers = int ( round ( self . mutation_ratio * self . population_size ) ) parents_size = int ( round ( self . parent_ratio * self . population_size ) ) best_valids = [ - 100 ] population = [ ] child_pool = [ ] efficiency_pool = [ ] best_info = None if verbose : print ( 'Generate random population...' ) for _ in range ( self . population_size ) : sample , efficiency = self . random_valid_sample ( constraint_lower , constraint_upper , input_size = input_size ) child_pool . append ( sample ) efficiency_pool . append ( efficiency ) accs = self . accuracy_predictor . get_arch_acc ( child_pool , efficiency_pool ) print ( 'finish calculate the accuracy of models' ) for i in range ( self . population_size ) : population . append ( ( accs [ i ] , child_pool [ i ] , efficiency_pool [ i ] ) ) if verbose : print ( 'Start Evolution...' ) with tqdm ( total = self . max_time_budget , desc = 'Searching with constraint (%s)' % constraint_upper , disable = ( not verbose ) ) as t : for i in range ( self . max_time_budget ) : parents = sorted ( population , key = lambda x : x [ 0 ] ) [ : : - 1 ] [ : parents_size ] acc = parents [ 0 ] [ 0 ] t . set_postfix ( { 'acc' : parents [ 0 ] [ 0 ] } ) if not verbose and ( i + 1 ) % 100 == 0 : print ( 'Iter: {} Acc: {}' . format ( i + 1 , parents [ 0 ] [ 0 ] ) ) if acc > best_valids [ - 1 ] : best_valids . append ( acc ) best_info = parents [ 0 ] else : best_valids . append ( best_valids [ - 1 ] ) population = parents child_pool = [ ] efficiency_pool = [ ] print ( 'perfrom mutation' ) for j in range ( mutation_numbers ) : par_sample = population [ np . random . randint ( parents_size ) ] [ 1 ] new_sample , efficiency = self . mutate_sample ( par_sample , constraint_lower , constraint_upper , input_size = input_size ) child_pool . append ( new_sample ) efficiency_pool . append ( efficiency ) print ( 'perfrom crossover' ) for j in range ( self . population_size - mutation_numbers ) : par_sample1 = population [ np . random . randint ( parents_size ) ] [ 1 ] par_sample2 = population [ np . random . randint ( parents_size ) ] [ 1 ] new_sample , efficiency = self . crossover_sample ( par_sample1 , par_sample2 , constraint_lower , constraint_upper , input_size = input_size ) child_pool . append ( new_sample ) efficiency_pool . append ( efficiency ) print ( 'get model acc' ) accs = self . accuracy_predictor . get_arch_acc ( child_pool , efficiency_pool ) for j in range ( self . population_size ) : population . append ( ( accs [ j ] , child_pool [ j ] , efficiency_pool [ j ] ) ) t . update ( 1 ) return best_valids , best_info
Run a single roll-out of regularized evolution to a fixed time budget.
Run a single roll - out of regularized evolution to a fixed time budget .
"""Run a single roll-out of regularized evolution to a fixed time budget.""" # (validation, sample, latency) tuples # After the population is seeded, proceed with evolving the population. # Mutate # Crossover
self
constraint_lower
constraint_upper
input_size
verbose