def __init__(self): MastermindServerTCP.__init__(self, 0.5, 0.5, 300.0) self.players = { } # all the Players() that exist in the world whether connected or not. self.localmaps = {} # the localmaps for each player. self.overmaps = {} # the dict of all overmaps by player.name self.options = Options() self.calendar = Calendar(0, 0, 0, 0, 0, 0) # all zeros is the epoch self.options.save() self.worldmap = Worldmap( 13 ) # create this many chunks in x and y (z is always 1 (level 0) for genning the world. we will build off that for caverns and ant stuff and z level buildings. self.starting_locations = [ Position(48, 48, 0) ] #TODO: starting locations should be loaded dynamically from secenarios self.RecipeManager = RecipeManager()
def main(_): np.random.seed(0) opts = Options(FLAGS) print("{") print(" \"corpus\": \"{}\"".format(opts.data_path)) corpus_save_path = "../data/corpus.npz" load_exist = False if load_exist and os.path.exists(corpus_save_path): corp = joblib.load(corpus_save_path) with open("../data/corpus.txt", 'r') as f: for u, line in enumerate(f): origin_size = len(corp.pos_per_user[u]) corp.pos_per_user[u] = [] assert (origin_size == len(line.split())) for item in line.split(): corp.pos_per_user[u].append({ "item": int(item.split('|')[0]), "time": int(item.split('|')[1]) }) else: corp = Corpus() corp.load_data(opts.data_path, opts.user_min, opts.item_min) # joblib.dump(corp, corpus_save_path) with tf.Graph().as_default(), tf.Session() as session: tf.set_random_seed(0) # go_poprec(opts, corp, session) go_transrec(opts, corp, session) print("}")
def decompile_and_capture_output(output_path: Path, asm_file_path: Path) -> str: out_string = io.StringIO() with contextlib.redirect_stdout(out_string): returncode = decompile( Options( filename=str(asm_file_path), debug=False, void=False, ifs=True, andor_detection=True, goto_patterns=["GOTO"], rodata_files=[], stop_on_error=True, print_assembly=False, visualize_flowgraph=False, c_context=None, dump_typemap=False, preproc_defines={}, coding_style=CodingStyle( newline_after_function=True, newline_after_if=True, newline_before_else=True, ), ), "test", ) if returncode == 0: return out_string.getvalue() else: return CRASH_STRING
def rep(iterations, profile): options = Options() options.serial = True options.accperclass = True options.convlayers = 0 options.step = 20 options.combine = True options.representatives = True options.post_init() x_train, y_train, x_test, y_test = get_dataset(options) # pylint: disable=unused-variable model = train_network(get_model(x_test, options), options) only_convolutional, _ = split_network(model, options.convlayers) # pylint: disable=unused-variable cmd_string = """run_experiment(iterations, options, x_train, y_train, x_test, y_test, model, only_convolutional, EXPERIMENT_DIR + '/representatives.csv')""" if profile: cProfile.run(cmd_string, PROFILING_DIR + '/representatives') else: exec(cmd_string)
def test_split(self): options = Options() _, _, x_test, _ = get_dataset(options) model = train_network(get_model(x_test, options), options) part1, part2 = split_network(model, 3) model_output = model(x_test[0:5]) split_output = part2(part1(x_test[0:5])) equality = tf.math.reduce_all(tf.equal(model_output, split_output)) self.assertEqual(equality, True)
def start(cls, interval = None, unit = 'minutes'): """ Restart the schedule on a new interval Arguments: interval (int): The time between wallpaper change unit (string)['minutes' | 'hours' | 'days' | 'weeks']: Unit of time used """ if interval == None: settings = options.get('schedule') interval = settings.get('interval') unit = settings.get('unit') print('Updating wall every ' + str(interval) + ' ' + unit) schedule.clear(WALL_SCHEDULE) # TODO: Dynaimcally choose unit to schedule on job = getattr(schedule.every(interval), unit) job.do(Options.set_wallpaper_random).tag(WALL_SCHEDULE) Options.set_schedule(interval, unit)
def test_compose_rotation(self): """Test whether rotation -> convolution == convolution -> rotation It isn't. This has implications for our project. This is not as much a test as a demonstration of fact""" options = Options() _, _, x_test, _ = get_dataset(options) model = train_network(get_model(x_test, options), options) part1, _ = split_network(model, 8) image = tf.expand_dims(x_test[0], 0) rotated_image = tfa.image.rotate(image, math.pi) out = tfa.image.rotate(part1(image), math.pi) rotated_out = part1(rotated_image) equality = tf.math.reduce_all( tf.equal(tfa.image.rotate(out, math.pi), rotated_out)) self.assertEqual(equality, False)
def set_selected_albums(selected_albums): # User has changed the album selection Options.set_selected_albums(selected_albums)
log = f'Process rank:{opt.global_rank}, {i+1} / {len(dataloader)}' log += f' -- average = {np.mean(exactmatch):.3f}' logger.warning(log) logger.warning( f'Process rank:{opt.global_rank}, total {total} -- average = {np.mean(exactmatch):.3f}' ) if opt.is_distributed: torch.distributed.barrier() score, total = src.util.weighted_average(np.mean(exactmatch), total, opt) return score, total if __name__ == "__main__": options = Options() options.add_reader_options() options.add_eval_options() opt = options.parse() src.slurm.init_distributed_mode(opt) src.slurm.init_signal_handler() opt.train_batch_size = opt.per_gpu_batch_size * max(1, opt.world_size) dir_path = Path(opt.checkpoint_dir) / opt.name directory_exists = dir_path.exists() if opt.is_distributed: torch.distributed.barrier() dir_path.mkdir(parents=True, exist_ok=True) if opt.write_results: (dir_path / 'test_results').mkdir(parents=True, exist_ok=True) logger = src.util.init_logger(
src.evaluation.eval_batch(scores, inversions, avg_topk, idx_topk) total += question_ids.size(0) inversions = src.util.weighted_average(np.mean(inversions), total, opt)[0] for k in avg_topk: avg_topk[k] = src.util.weighted_average(np.mean(avg_topk[k]), total, opt)[0] idx_topk[k] = src.util.weighted_average(np.mean(idx_topk[k]), total, opt)[0] return loss, inversions, avg_topk, idx_topk if __name__ == "__main__": options = Options() options.add_retriever_options() options.add_optim_options() opt = options.parse() torch.manual_seed(opt.seed) src.slurm.init_distributed_mode(opt) src.slurm.init_signal_handler() dir_path = Path(opt.checkpoint_dir) / opt.name directory_exists = dir_path.exists() if opt.is_distributed: torch.distributed.barrier() dir_path.mkdir(parents=True, exist_ok=True) if not directory_exists and opt.is_main: options.print_options(opt)
class Server(MastermindServerTCP): def __init__(self): MastermindServerTCP.__init__(self, 0.5, 0.5, 300.0) self.players = { } # all the Players() that exist in the world whether connected or not. self.localmaps = {} # the localmaps for each player. self.overmaps = {} # the dict of all overmaps by player.name self.options = Options() self.calendar = Calendar(0, 0, 0, 0, 0, 0) # all zeros is the epoch self.options.save() self.worldmap = Worldmap( 26 ) # create this many chunks in x and y (z is always 1 (level 0) for genning the world. we will build off that for caverns and ant stuff and z level buildings. self.starting_locations = [ Position(23, 23, 0) ] #TODO: starting locations should be loaded dynamically from secenarios for i in range(1, 13): self.starting_locations.append(Position(23 * i, 23, 0)) self.RecipeManager = RecipeManager() def calculate_route( self, pos0, pos1, consider_impassable=True ): # normally we will want to consider impassable terrain in movement calculations. creatures that don't can walk or break through walls. #print('----------------Calculating Route---------------------') #print('pos0: ' + str(pos0)) #print('pos1: ' + str(pos1)) reachable = [pos0] explored = [] while len(reachable) > 0: position = random.choice( reachable ) # get a random reachable position #TODO: be a little more intelligent about picking the best reachable position. # If we just got to the goal node. return the path. if position == pos1: path = [] while position != pos0: path.append(position) position = position.previous ret_path = [] for step in path: ret_path.insert(0, step) return ret_path # Don't repeat ourselves. reachable.remove(position) explored.append(position) new_reachable = self.worldmap.get_adjacent_positions_non_impassable( position) for adjacent in new_reachable: if (abs(adjacent.x - pos0.x) > 10 or abs(adjacent.y - pos0.y) > 10): continue if adjacent not in reachable and adjacent not in explored: adjacent.previous = position # Remember how we got there. reachable.append(adjacent) return None def callback_client_handle(self, connection_object, data): #print("Server: Recieved data \""+str(data)+"\" from client \""+str(connection_object.address)+"\".") # use the data to determine what player is giving the command and if they are logged in yet. if (isinstance( data, Command)): # the data we recieved was a command. process it. if (data.command == 'login'): if (data.args[0] == 'password' ): # TODO: put an actual password system in. print('password accepted for ' + str(data.ident)) if (not data.ident in self.players ): # this player doesn't exist in the world yet. # check and see if the players has logged in before. tmp_player = self.worldmap.get_player( data.ident) # by 'name' if (tmp_player is not None): # player exists print('player exists. loading.') self.players[data.ident] = tmp_player self.players[ data.ident].position = tmp_player.position self.localmaps[ data. ident] = self.worldmap.get_chunks_near_position( self.players[data.ident].position) else: # new player print('new player joined.') self.players[data.ident] = Player(data.ident) self.players[data.ident].position = random.choice( self.starting_locations) self.worldmap.put_object_at_position( self.players[data.ident], self.players[data.ident].position) self.localmaps[ data. ident] = self.worldmap.get_chunks_near_position( self.players[data.ident].position) print('Player ' + str(data.ident) + ' entered the world at position ' + str(self.players[data.ident].position)) self.callback_client_send(connection_object, self.players[data.ident]) else: print('password not accepted.') connection_object.disconnect() if (data.command == 'request_player_update'): self.callback_client_send(connection_object, self.players[data.ident]) if (data.command == 'request_localmap_update'): self.localmaps[ data.ident] = self.worldmap.get_chunks_near_position( self.players[data.ident].position) self.callback_client_send(connection_object, self.localmaps[data.ident]) # all the commands that are actions need to be put into the command_queue then we will loop through the queue each turn and process the actions. if (data.command == 'move'): self.players[data.ident].command_queue.append( Action(self.players[data.ident], 'move', [data.args[0]])) if (data.command == 'bash'): self.players[data.ident].command_queue.append( Action(self.players[data.ident], 'bash', [data.args[0]])) if (data.command == 'create_blueprint'): # [result, direction]) # args 0 is ident args 1 is direction. print('creating blueprint ' + str(data.args[0]) + ' for player ' + str(self.players[data.ident])) # blueprint rules # * there should be blueprints for terrain, furniture, items, and anything else that takes a slot up in the Worldmap. # * they act as placeholders and then 'transform' into the type they are once completed. # Blueprint(type, recipe) position_to_create_at = None if (data.args[1] == 'south'): position_to_create_at = Position( self.players[data.ident].position.x, self.players[data.ident].position.y + 1, self.players[data.ident].position.z) elif (data.args[1] == 'north'): position_to_create_at = Position( self.players[data.ident].position.x, self.players[data.ident].position.y - 1, self.players[data.ident].position.z) elif (data.args[1] == 'east'): sposition_to_create_at = Position( self.players[data.ident].position.x + 1, self.players[data.ident].position.y, self.players[data.ident].position.z) elif (data.args[1] == 'west'): position_to_create_at = Position( self.players[data.ident].position.x - 1, self.players[data.ident].position.y, self.players[data.ident].position.z) _recipe = server.RecipeManager.RECIPE_TYPES[data.args[0]] type_of = _recipe['type_of'] bp_to_create = Blueprint(type_of, _recipe) self.worldmap.put_object_at_position(bp_to_create, position_to_create_at) if (data.command == 'calculated_move'): print( 'Recieved calculated_move action. let\'s build a path for ' + str(data.ident)) _position = Position(data.args[0], data.args[1], data.args[2]) _route = self.calculate_route( self.players[data.ident].position, _position ) # returns a route from point 0 to point 1 as a series of Position(s) print(_route) # fill the queue with move commands to reach the tile. _x = self.players[data.ident].position.x _y = self.players[data.ident].position.y _z = self.players[data.ident].position.z action = None if (_route is None): print('No _route possible.') return for step in _route: _next_x = step.x _next_y = step.y _next_z = step.z if (_x > _next_x): action = Action(self.players[data.ident], 'move', ['west']) elif (_x < _next_x): action = Action(self.players[data.ident], 'move', ['east']) elif (_y > _next_y): action = Action(self.players[data.ident], 'move', ['north']) elif (_y < _next_y): action = Action(self.players[data.ident], 'move', ['south']) elif (_z < _next_z): action = Action(self.players[data.ident], 'move', ['up']) elif (_z > _next_z): action = Action(self.players[data.ident], 'move', ['down']) self.players[data.ident].command_queue.append(action) # pretend as if we are in the next position. _x = _next_x _y = _next_y _z = _next_z if (data.command == 'move_item_to_player_storage' ): # when the player clicked on a ground item. print('RECIEVED: move_item_to_player_storage', str(data)) _player = self.players[data.ident] _from_pos = Position(data.args[0], data.args[1], data.args[2]) _item_ident = data.args[3] print(_player, _from_pos, _item_ident) # first check if the player can carry that item. # then find a spot for it to go (open_containers) # then send the player the updated version of themselves so they can refresh. if (data.command == 'move_item'): # client sends 'hey server. can you move this item from this to that?' _player_requesting = self.players[data.ident] _item = data.args[0] # the item we are moving. _from_type = data.args[ 1] # creature.held_item, creature.held_item.container, bodypart.equipped, bodypart.equipped.container, position, blueprint _from_list = [ ] # the object list that contains the item. parse the type and fill this properly. _to_list = data.args[ 2] # the list the item will end up. passed from command. _position = Position( data.args[3], data.args[4], data.args[5] ) # pass the position even if we may not need it. # need to parse where it's coming from and where it's going. if (_from_type == 'bodypart.equipped'): for bodypart in _player_requesting.body_parts[:]: # iterate a copy to remove properly. if (_item in bodypart.equipped): _from_list = bodypart.equipped _from_list.remove(_item) _to_list.append(_item) print('moved correctly.') return elif (_from_type == 'bodypart.equipped.container'): for bodypart in _player_requesting.body_parts[:]: # iterate a copy to remove properly. for item in bodypart.equipped: #could be a container or not. if (isinstance(item, Container)): # if it's a container. for item2 in item.contained_items[:]: # check every item in the container. if (item2 is _item): from_list = item.contained_items _from_list.remove(_item) _to_list.append(_item) print('moved correctly.') return elif (_from_type == 'position'): _from_list = self.worldmap.get_tile_by_position( _position)['items'] if (_item in _from_list): _from_list.remove(_item) _to_list.append(_item) print('moved correctly.') return elif ( _from_type == 'blueprint' ): # a blueprint is a type of container but can't be moved from it's world position. for item in self.worldmap.get_tile_by_position( _position)['items']: if (isinstance(item) == Blueprint ): # only one blueprint allowed per space. _from_list = item.contained_items _from_list.remove(_item) _to_list.append(_item) print('moved correctly.') return ### possible move types ### # creature(held) to creature(held) (give to another player) # creature(held) to position(ground) (drop) # creature(held) to bodypart (equip) # bodypart to creature(held) (unequip) # bodypart to position (drop) # position to creature(held) (pick up from ground) # position to bodypart (equip from ground) # position to position (move from here to there) # creature to blueprint (fill blueprint) # blueprint to position (empty blueprint on ground) # blueprint to creature (grab from blueprint) return super(Server, self).callback_client_handle(connection_object, data) def callback_client_send(self, connection_object, data, compression=None): #print("Server: Sending data \""+str(data)+"\" to client \""+str(connection_object.address)+"\" with compression \""+str(compression)+"\"!") return super(Server, self).callback_client_send(connection_object, data, compression) def callback_connect_client(self, connection_object): print("Server: Client from \"" + str(connection_object.address) + "\" connected.") return super(Server, self).callback_connect_client(connection_object) def callback_disconnect_client(self, connection_object): print("Server: Client from \"" + str(connection_object.address) + "\" disconnected.") return super(Server, self).callback_disconnect_client(connection_object) def process_creature_command_queue(self, creature): actions_to_take = creature.actions_per_turn for action in creature.command_queue[:]: # iterate a copy so we can remove on the fly. if (actions_to_take == 0): return # this creature is out of action points. if (creature.next_action_available > 0): # this creature can't act until x turns from now. creature.next_action_available = creature.next_action_available - 1 return # if we get here we can process a single action if (action.action_type == 'move'): actions_to_take = actions_to_take - 1 # moving costs 1 ap. if (action.args[0] == 'south'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x, self.players[creature.name].position.y + 1, self.players[creature.name].position.z))): self.players[creature.name].position = Position( self.players[creature.name].position.x, self.players[creature.name].position.y + 1, self.players[creature.name].position.z) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'north'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x, self.players[creature.name].position.y - 1, self.players[creature.name].position.z))): self.players[creature.name].position = Position( self.players[creature.name].position.x, self.players[creature.name].position.y - 1, self.players[creature.name].position.z) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'east'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x + 1, self.players[creature.name].position.y, self.players[creature.name].position.z))): self.players[creature.name].position = Position( self.players[creature.name].position.x + 1, self.players[creature.name].position.y, self.players[creature.name].position.z) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'west'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x - 1, self.players[creature.name].position.y, self.players[creature.name].position.z))): self.players[creature.name].position = Position( self.players[creature.name].position.x - 1, self.players[creature.name].position.y, self.players[creature.name].position.z) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'up'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z + 1))): self.players[creature.name].position = Position( self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z + 1) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'down'): if (self.worldmap.move_object_from_position_to_position( self.players[creature.name], self.players[creature.name].position, Position( self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z - 1))): self.players[creature.name].position = Position( self.players[creature.name].position.x, self.players[creature.name].position.y, self.players[creature.name].position.z - 1) creature.command_queue.remove( action) # remove the action after we process it. elif (action.action_type == 'bash'): actions_to_take = actions_to_take - 1 # bashing costs 1 ap. if (action.args[0] == 'south'): self.worldmap.bash( self.players[creature.name], Position(self.players[creature.name].position.x, self.players[creature.name].position.y + 1, self.players[creature.name].position.z)) self.localmaps[ creature. name] = self.worldmap.get_chunks_near_position( self.players[creature.name].position) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'north'): self.worldmap.bash( self.players[creature.name], Position(self.players[creature.name].position.x, self.players[creature.name].position.y - 1, self.players[creature.name].position.z)) self.localmaps[ creature. name] = self.worldmap.get_chunks_near_position( self.players[creature.name].position) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'east'): self.worldmap.bash( self.players[creature.name], Position(self.players[creature.name].position.x + 1, self.players[creature.name].position.y, self.players[creature.name].position.z)) self.localmaps[ creature. name] = self.worldmap.get_chunks_near_position( self.players[creature.name].position) creature.command_queue.remove( action) # remove the action after we process it. if (action.args[0] == 'west'): self.worldmap.bash( self.players[creature.name], Position(self.players[creature.name].position.x - 1, self.players[creature.name].position.y, self.players[creature.name].position.z)) self.localmaps[ creature. name] = self.worldmap.get_chunks_near_position( self.players[creature.name].position) creature.command_queue.remove( action) # remove the action after we process it. # this function handles overseeing all creature movement, attacks, and interactions def compute_turn(self): for player, chunks in self.localmaps.items(): for chunk in chunks: # players typically get 9 chunks for tile in chunk.tiles: tile['lumens'] = 0 # reset light levels. creatures_to_process = [ ] # we want a list that contains all the non-duplicate creatures on all localmaps around players. for player, chunks in self.localmaps.items(): for chunk in chunks: # players typically get 9 chunks for tile in chunk.tiles: if (tile['creature'] is not None): if (tile['creature'] not in creatures_to_process ): # avoid duplicates creatures_to_process.append(tile['creature']) for creature in creatures_to_process: if (len(creature.command_queue) > 0): print('doing actions for: ' + str(creature.name)) self.process_creature_command_queue(creature) for tile in self.worldmap.get_all_tiles(): if (tile['creature'] is not None): #TODO: don't just draw a light around every creature. we need to check for all lights. We also need to have light blocked by walls. for tile, distance in self.worldmap.get_tiles_near_position( tile['position'], 8): tile['lumens'] = tile['lumens'] + int(8 - distance) # now that we've processed what everything wants to do we can return. def generate_and_apply_city_layout(self, city_size): #city_size = 1 city_layout = self.worldmap.generate_city(city_size) # for every 1 city size it's 12 tiles across and high for j in range(city_size * 12): for i in range(city_size * 12): if (city_layout[i][j] == 'r'): json_file = random.choice( os.listdir('./data/json/mapgen/residential/')) server.worldmap.build_json_building_at_position( './data/json/mapgen/residential/' + json_file, Position(i * server.worldmap.chunk_size + 1, j * server.worldmap.chunk_size + 1, 0)) elif (city_layout[i][j] == 'c'): json_file = random.choice( os.listdir('./data/json/mapgen/commercial/')) server.worldmap.build_json_building_at_position( './data/json/mapgen/commercial/' + json_file, Position(i * server.worldmap.chunk_size + 1, j * server.worldmap.chunk_size + 1, 0)) elif (city_layout[i][j] == 'i'): json_file = random.choice( os.listdir('./data/json/mapgen/industrial/')) server.worldmap.build_json_building_at_position( './data/json/mapgen/industrial/' + json_file, Position(i * server.worldmap.chunk_size + 1, j * server.worldmap.chunk_size + 1, 0)) elif (city_layout[i][j] == 'R' ): # complex enough to choose the right rotation. attached_roads = 0 try: if (city_layout[int(i - 1)][int(j)] == 'R'): attached_roads = attached_roads + 1 if (city_layout[int(i + 1)][int(j)] == 'R'): attached_roads = attached_roads + 1 if (city_layout[int(i)][int(j - 1)] == 'R'): attached_roads = attached_roads + 1 if (city_layout[int(i)][int(j + 1)] == 'R'): attached_roads = attached_roads + 1 if (attached_roads == 4): json_file = './data/json/mapgen/road/city_road_4_way.json' elif (attached_roads == 3 ): #TODO: make sure the roads line up right. if (city_layout[int(i + 1)][int(j)] != 'R'): json_file = './data/json/mapgen/road/city_road_3_way_s0.json' elif (city_layout[int(i - 1)][int(j)] != 'R'): json_file = './data/json/mapgen/road/city_road_3_way_p0.json' elif (city_layout[int(i)][int(j + 1)] != 'R'): json_file = './data/json/mapgen/road/city_road_3_way_u0.json' elif (city_layout[int(i)][int(j - 1)] != 'R'): json_file = './data/json/mapgen/road/city_road_3_way_d0.json' elif (attached_roads <= 2): if (city_layout[int(i + 1)][int(j)] == 'R'): json_file = './data/json/mapgen/road/city_road_h.json' elif (city_layout[int(i - 1)][int(j)] == 'R'): json_file = './data/json/mapgen/road/city_road_h.json' elif (city_layout[int(i)][int(j + 1)] == 'R'): json_file = './data/json/mapgen/road/city_road_v.json' elif (city_layout[int(i)][int(j - 1)] == 'R'): json_file = './data/json/mapgen/road/city_road_v.json' server.worldmap.build_json_building_at_position( json_file, Position(i * server.worldmap.chunk_size + 1, j * server.worldmap.chunk_size + 1, 0)) except: #TODO: fix this blatant hack to account for coordinates outside the city layout. pass
def constraint(iterations, profile): delete = True if delete: remove_caches() options = Options() options.serial = True options.convlayers = 3 options.step = 20 options.combine = False options.representatives = False options.post_init() options.model_step = 20 x_train, y_train, x_test, y_test = get_dataset(options) # pylint: disable=unused-variable model = train_network(get_model(x_test, options), options) options.convlayers = get_last_conv_layer(model) + 1 only_convolutional, _ = split_network(model, options.convlayers) cmd_string = """run_experiment(iterations, options, x_train, y_train, x_test, y_test, model, only_convolutional, EXPERIMENT_DIR + '/constraint.csv')""" if profile: cProfile.run(cmd_string, PROFILING_DIR + '/constraint') else: exec(cmd_string)
def set_wallpaper_by_direction(direction): return Options.set_wallpaper_by_direction(direction)
self.cls_photos[index])) fname_ske = np.random.choice([ fname for fname in fname_sketch_list if self.fnames_photos[index].split('.')[0] in fname ], 1)[0] fname_sketch = os.path.join(self.sketch_dir, random_sub_dir_sk, self.cls_photos[index], fname_ske) return self.transform_photo(self.loader( fname_photo, True)), self.transform_photo(self.loader(fname_sketch, False)) def __len__(self): return len(self.fnames_photos) if __name__ == '__main__': args = Options().parse() testdemo = GAN_DataGeneratorPaired_for_sketchy(args.photo_dir, args.sketch_dir, 'train') d = DataLoader(dataset=testdemo, batch_size=32, shuffle=True, num_workers=2) for epoch in range(2): for i, data in enumerate(d): img, sketch = data img = Variable(img) sketch = Variable(sketch) print(epoch, i, img.data.size(), sketch.data.size())
import threading import time import schedule from src.options import Options WALL_SCHEDULE = 'WALL_SCHEDULE' options = Options.get_user_options() # Start schedule check loop def start_loop(): while True: schedule.run_pending() time.sleep(1) thread = threading.Thread(target = start_loop) thread.start() def test(): print('job') class Scheduler(): @classmethod def start(cls, interval = None, unit = 'minutes'): """ Restart the schedule on a new interval Arguments: interval (int): The time between wallpaper change unit (string)['minutes' | 'hours' | 'days' | 'weeks']: Unit of time used """
def setup(): options = Options() options.serial = True parser = argparse.ArgumentParser(description='Run the experiments') parser.add_argument('--profile', dest='profile', action='store_true', help='Profile the experiments') parser.add_argument('--nobasic', dest='basic', action='store_false', help='Skip the basic experiment') parser.add_argument('--norep', dest='rep', action='store_false', help='Skip the representatives experiment') parser.add_argument('--noconv', dest='conv', action='store_false', help='Skip the convolution experiment') parser.add_argument('--norot', dest='rot_first', action='store_false', help='Skip the rotate first experiment') parser.add_argument('--nocon', dest='constraint', action='store_false', help='Skip the constraint experiment') parser.add_argument('--iterations', type=int, default=501, nargs='?', help='How many iterations to run') args = parser.parse_args() try: shutil.rmtree(EXPERIMENT_DIR) except OSError: pass os.mkdir(EXPERIMENT_DIR) try: shutil.rmtree(PROFILING_DIR) except OSError: pass os.mkdir(PROFILING_DIR) remove_caches() if args.basic: basic(args.iterations, args.profile) if args.rep: rep(args.iterations, args.profile) if args.conv: conv(args.iterations, args.profile) if args.rot_first: rot_first(args.iterations, args.profile) if args.constraint: constraint(args.iterations, args.profile)
def get_user_options(): return Options.get_user_options()
def set_wallpaper(media_item): Options.set_current_wallpaper(media_item) return True
def get_current_wallpaper(): # Get current wall from user options return Options.get_current_wallpaper()
def set_wallpaper_random(): return Options.set_wallpaper_random()
attention_mask=context_mask.cuda(), max_length=50 ) for k, o in enumerate(outputs): ans = tokenizer.decode(o, skip_special_tokens=True) gold = dataset.get_example(idx[k])['answers'] score = src.evaluation.ems(ans, gold) total += 1 exactmatch.append(score) exactmatch, total = src.util.weighted_average(np.mean(exactmatch), total, opt) return exactmatch if __name__ == "__main__": options = Options() options.add_reader_options() options.add_optim_options() opt = options.parse() #opt = options.get_options(use_reader=True, use_optim=True) torch.manual_seed(opt.seed) src.slurm.init_distributed_mode(opt) src.slurm.init_signal_handler() checkpoint_path = Path(opt.checkpoint_dir)/opt.name checkpoint_exists = checkpoint_path.exists() if opt.is_distributed: torch.distributed.barrier() checkpoint_path.mkdir(parents=True, exist_ok=True) #if not checkpoint_exists and opt.is_main:
#!/usr/bin/env python3 """ main module file run the script by './main.py' or 'python3 main.py'""" import sys import os from src.runner import check_some from src.options import Options from src.training_sample_finder import get_training_samples from src.network import train_network, get_dataset, get_model, split_network sys.path.append(os.path.join(os.path.dirname(__file__), 'src')) if __name__ == "__main__": # Initialize the options class. options = Options() # Parse arguments options.parse_args(10000) # Get the MNIST dataset x_train, y_train, x_test, y_test = get_dataset(options) # Gets the model and the conv part of model, the function also trains the # network, if there doesn't exist a saved network model = train_network(get_model(x_test, options), options) only_convolutional, _ = split_network(model, options.convlayers) model.summary() if only_convolutional is not None: only_convolutional.summary() training_samples = get_training_samples(only_convolutional, x_train, y_train, options) # Run the checksome function, which tests a chosen algorithm to find if it # can properly rotate pictures back
def main(): ## testing IMAGE ## Prepare data args = Options().parse() train_dataset = GAN_DataGeneratorPaired_for_sketchy( args.photo_dir, args.sketch_dir, 'train') train_dataloader = DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=2) test_dataset = GAN_DataGeneratorPaired_for_sketchy(args.photo_dir, args.sketch_dir, 'test') test_trained, _ = random_split(train_dataset, [8, len(train_dataset) - 8]) test_untrained, _ = random_split(test_dataset, [8, len(test_dataset) - 8]) test_total = ConcatDataset([test_trained, test_untrained]) imglist = [] sketchlist = [] for i in range(len(test_total)): image, sketch = test_total[i] imglist.append(image) sketchlist.append(sketch) save_image(imglist, '../figure/' + 'image' + '.jpg', padding=5) save_image(sketchlist, '../figure/' + 'sketch' + '.png', padding=5) ## Init model model = SRNet_BG(4, 64) #model = Generator() loss = nn.L1Loss() optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) model.cuda() model.train() for i in range(args.epoch): epoch_index = i + 1 total_loss = 0.0 for images, sketches in tqdm( train_dataloader, desc='Train epoch: {}'.format(epoch_index)): images, sketches = images.cuda(), sketches.cuda() images, sketches = Variable(images), Variable(sketches) optimizer.zero_grad() outputs = model(images) batch_loss = loss(outputs, sketches) batch_loss.backward() optimizer.step() total_loss += batch_loss info = 'Epoch: {}, train loss: {:.3f}'.format( epoch_index, total_loss / len(train_dataloader)) output_test_list = [] testloader = DataLoader(dataset=test_total, batch_size=len(test_total), shuffle=False) for _, datas in enumerate(testloader): tesimage, xx = datas tesimage = Variable(tesimage.cuda()) output_test = model(tesimage) save_image(output_test, '../figure/' + 'sketch_epoch_' + str(epoch_index) + '.png', padding=5)