def __init__(self, total_num_styles, network_parameter_path, device): print("Setting up interpolator") print(" Using network parameters from", network_parameter_path) self.transfer_network = TransferNetwork(total_num_styles) self.transfer_network.load_state_dict( torch.load(network_parameter_path)) self.transfer_network.to(device).eval() print(" Ready")
def __init__(self, model_path, save_dir, num_styles): self.device = "cuda:0" if torch.cuda.is_available() else "cpu" self.save_dir = save_dir self.num_styles = num_styles self.style_manager = StyleManager('../data/images/style/', self.device) self.transfer_network = TransferNetwork(num_styles).to(self.device) if self.device == "cpu": self.transfer_network.load_state_dict( torch.load(model_path, map_location='cpu')) else: self.transfer_network.load_state_dict(torch.load(model_path)) self.transfer_network.eval()
def main(): key = Cache.generate_key(str(Config())) if Cache.check(key): data = Cache.get(key) points = data['points'] network = data['network'] else: pass # get points from trajectories preprocessor = Preprocessor(Config.DATASET_ROOT_DIR, Config.DATASET_SCALE) points = preprocessor.get_points() # use coherence expanded algorithm to form clusters clusters = Cluster(points).coherence_expanding() network = TransferNetwork(points, clusters) # derive transfer probability tp = TransferProbability(network) tp.derive() # save points and network to cache Cache.save(key, {"points": points, "network": network}) # show the distribution of transfer probability figure = Figure(width=8) figure.transfer_probability(network, 8).show() # search the most popular route mpr = MostPopularRoute(network) route = mpr.search(0, 4) print(route) figure = Figure() figure.most_popular_route(points, network, route).show()
def process(): # get points from trajectories preprocessor = Preprocessor( Config.DATASET_ROOT_DIR, Config.DATASET_SCALE) points = preprocessor.get_points() # use coherence expanded algorithm to form clusters cluster = Cluster(points) clusters = cluster.coherence_expanding() network = TransferNetwork(points, clusters) coherences = cluster.coherences coherences.sort() cluster_points = [point for cluster in clusters for point in cluster] return points, clusters, network, coherences, cluster_points
class Interpolator(ABC): def __init__(self, total_num_styles, network_parameter_path, device): print("Setting up interpolator") print(" Using network parameters from", network_parameter_path) self.transfer_network = TransferNetwork(total_num_styles) self.transfer_network.load_state_dict( torch.load(network_parameter_path)) self.transfer_network.to(device).eval() print(" Ready") def render_interpolated_image(self, interpolated_style_parameters, test_image_tensor, style_idx=0): self.transfer_network.set_style_parameters( interpolated_style_parameters, style_idx) return self.transfer_network(test_image_tensor, style_idx)
class Renderer: def __init__(self, model_path, save_dir, num_styles): self.device = "cuda:0" if torch.cuda.is_available() else "cpu" self.save_dir = save_dir self.num_styles = num_styles self.style_manager = StyleManager('../data/images/style/', self.device) self.transfer_network = TransferNetwork(num_styles).to(self.device) if self.device == "cpu": self.transfer_network.load_state_dict( torch.load(model_path, map_location='cpu')) else: self.transfer_network.load_state_dict(torch.load(model_path)) self.transfer_network.eval() def render(self, content_image, style_idx): content_image = content_image.to(self.device) output = self.transfer_network(content_image, style_idx=style_idx) output = output.detach().cpu() return output[0] def render_grid(self, content_dir, style_idx, style_id): rendered_images = [self.style_manager[style_id]] save_path = os.path.join(self.save_dir, 'style_grid_%d.png' % style_idx) for content_path in os.listdir(content_dir): content_image = load_image_as_tensor( os.path.join(content_dir, content_path)).unsqueeze(0) rendered_images.append(self.render(content_image, style_idx)) save_tensors_as_grid(rendered_images, save_path, nrow=4) def render_all(self, content_image): for style_idx in range(self.num_styles): self.render_single(content_image, style_idx) def render_single(self, content_image, style_idx): save_path = os.path.join(self.save_dir, 'style_%d.png' % style_idx) output = self.render(content_image, style_idx) save_tensor_as_image(output, save_path)
sys.path.append(src_path) from config import Config from preprocessor import Preprocessor from points import Points from cluster import Cluster from transfer_network import TransferNetwork from transfer_probability import TransferProbability from most_popular_route import MostPopularRoute from figure import Figure # get points from trajectories preprocessor = Preprocessor( Config.DATASET_ROOT_DIR, Config.DATASET_SCALE) points = preprocessor.get_points() # use coherence expanded algorithm to form clusters clusters = Cluster(points).coherence_expanding() network = TransferNetwork(points, clusters) # derive transfer probability tp = TransferProbability(network) tp.derive() # search the most popular route mpr = MostPopularRoute(network) route = mpr.search(0, 6) print(route) figure = Figure() figure.most_popular_route(points, network, route).show()