示例#1
0
    def __init__(self, hostname, interval=1.0, expiry_seconds=300,
                 formatter=None, recent_point_threshold=None,
                 histogram_aggregates=None, histogram_percentiles=None,
                 utf8_decoding=False):
        # TODO(jaime): add support for event, service_check sources
        self.events = []
        self.service_checks = []
        self.stats = Stats()

        # TODO(jaime): we can probably kill total counts
        self.packet_count = 0
        self.metric_count = 0
        self.event_count = 0
        self.service_check_count = 0

        self.hostname = hostname
        self.expiry_seconds = expiry_seconds
        self.formatter = formatter or api_formatter
        self.interval = float(interval)

        recent_point_threshold = recent_point_threshold or DEFAULT_RECENT_POINT_THRESHOLD
        self.recent_point_threshold = int(recent_point_threshold)
        self.num_discarded_old_points = 0

        # Additional config passed when instantiating metric configs
        self.metric_config = {
            Histogram: {
                'aggregates': histogram_aggregates,
                'percentiles': histogram_percentiles
            }
        }

        self.utf8_decoding = utf8_decoding
示例#2
0
 def __init__(self, api_key, domain, nb_worker=4, proxies={}):
     self.api_key = api_key
     self.domain = domain
     self.stats = Stats()
     self.input_queue = queue.Queue(self.QUEUES_SIZE)
     self.retry_queue = queue.Queue(self.QUEUES_SIZE)
     self.workers = []
     self.nb_worker = nb_worker
     self.retry_worker = None
     self.proxies = proxies
示例#3
0
def processFolder(datasetPath):
    stats = Stats(datasetPath)
    props = SaliencyProps()
    sal = sal_instance(SaliencyMethods.REGION_CONTRAST, props)
    for category in getDirectories(datasetPath):
        stats.addCategories(category)
        categoryPath = os.path.join(datasetPath, category)
        for image in getDirectories(categoryPath):
            stats.update(category, image,
                         compareWithGroundtruth(sal, categoryPath, image))
        stats.writeCategoryResult(category)
    stats.writeOverallResults()
def processFolder(datasetPath,sal,bg,smoothner,num_prev_frames,num_blocks):
	stats = Stats(datasetPath) 
	for category in getDirectories(datasetPath):
		stats.addCategories(category)
		categoryPath = os.path.join(datasetPath, category)
		for video in getDirectories(categoryPath):
			videoPath = os.path.join(categoryPath, video)
			confusionMatrix = compareWithGroundtruth(videoPath,sal,bg,smoothner,
					num_prev_frames,num_blocks)
			stats.update(category, video, confusionMatrix)
		stats.writeCategoryResult(category)
	stats.writeOverallResults()
    def __init__(self, config, aggregator=None):
        self._config = config
        self._loaders = []
        self._check_classes = {}
        self._check_classes_errors = defaultdict(dict)
        self._check_instance_errors = defaultdict(dict)
        self._check_instances = defaultdict(list)
        self._check_instance_signatures = {}
        self._hostname = get_hostname()
        self._aggregator = aggregator
        self._status = Stats()

        self.set_loaders()
示例#6
0
def processFolder(datasetPath):
	stats = Stats(datasetPath) 
	props = SaliencyProps()
	sal = sal_instance(SaliencyMethods.REGION_CONTRAST,props);
	for category in getDirectories(datasetPath):
		stats.addCategories(category)
		categoryPath = os.path.join(datasetPath,category)
		for image in getDirectories(categoryPath):
			stats.update(category,image,compareWithGroundtruth(sal,categoryPath,image));
		stats.writeCategoryResult(category)
	stats.writeOverallResults()
def test_worker_process_transactions(m):
    stats = Stats()
    input_queue = queue.Queue(2)
    retry_queue = queue.Queue(2)
    w = Worker(input_queue, retry_queue, stats)

    t_success = Transaction("data", "https://datadog.com", "/success", None)
    t_error = Transaction("data", "https://datadog.com", "/error", None)
    m.post("https://datadog.com/success", status_code=200)
    m.post("https://datadog.com/error", status_code=402)

    input_queue.put(t_success)
    input_queue.put(t_error)

    # process 2 transactions
    w._process_transactions()
    w._process_transactions()

    assert input_queue.empty()
    assert not retry_queue.empty()

    assert t_success.nb_try == 1
    assert t_error.nb_try == 1
    assert t_error.next_flush is not None

    retry_item = retry_queue.get()
    assert t_error == retry_item
def test_worker_stop():
    stats = Stats()
    input_queue = queue.Queue()
    retry_queue = queue.Queue()
    w = Worker(input_queue, retry_queue, stats)
    w.start()

    w.stop()
    w.join(2)
    assert not w.isAlive()
示例#9
0
文件: runner.py 项目: dougsc/gp
 def __init__(self, population, termination_error_threshold, max_generations, tournament_size=2):
   self.population = population
   self.termination_error_threshold = termination_error_threshold
   self.current_generation = 1
   self.current_best_error = sys.maxint
   self.max_generations = max_generations
   self.tournament_size = tournament_size
   self.stats = Stats(self.__class__.__name__)
   self.stats.init_series([self.SK_LOWEST_ERROR, self.SK_BEST_INDIVIDUAL, self.SK_TARGET_SAMPLES, self.SK_ACTUAL_SAMPLES,
                           self.SK_BEST_TREE])
示例#10
0
class RokAuto:
    """The almighty script"""
    def __init__(self, config):
        self.config = config
        self.stats = Stats(self.config)
        self.adb = Adb()

    def attack_barbarians(self):
        """Runs the barbarian module"""

        combat_module = BarbarianCombat(self.config, self.stats)
        combat_module.navigate_to_map()
        combat_module.set_search_level()

        while True:
            try:
                combat_module.search_for_barbarian()
                combat_module.attack_barbarian()
                action_point_empty = combat_module.detect_action_points_empty()
                if action_point_empty is not None:
                    Logger.log_message('warning',
                                       "You have run out of action points")
                    if 'enabled' in self.config.refill_action_points[
                            'RefillActionPoints']:
                        combat_module.refill_action_points()
                        Logger.log_message("success", "Action points refilled")
                    else:
                        Logger.log_message(
                            "warning", "Action point refill set to disabled")
                        break
                combat_module.confirm_victory()
            except KeyboardInterrupt:
                Logger.log_message("error", "Script cancelled by user")
                break

        self.stats.print_stats()
def test_init():
    stats = Stats()
    input_queue = queue.Queue(2)
    retry_queue = queue.Queue(2)
    w = Worker(input_queue, retry_queue, stats)

    assert w.input_queue == input_queue
    assert w.retry_queue == retry_queue

    rw = RetryWorker(input_queue, retry_queue, stats)

    assert rw.input_queue == input_queue
    assert rw.retry_queue == retry_queue
    assert rw.transactions == []
    assert rw.flush_interval == rw.DEFAULT_FLUSH_INTERVAL
    assert rw.retry_queue_max_size == 30  # default value from config
def test_retry_worker_process_transaction():
    stats = Stats()
    input_queue = queue.Queue(2)
    retry_queue = queue.Queue(2)

    w = RetryWorker(input_queue, retry_queue, stats, flush_interval=1)

    # test pulling 1 transaction without flushing
    t1 = Transaction("data", "https://datadog.com", "/success", None)
    t1.next_flush = time.time()
    retry_queue.put(t1)

    w.GET_TIMEOUT = 10
    base_flush_time = time.time() + 10
    flush_time = w._process_transactions(base_flush_time)
    end = time.time()

    assert flush_time == base_flush_time
    assert retry_queue.qsize() == 0
    assert len(w.transactions) == 1
    assert w.transactions[0] == t1

    # now test with flush
    w.GET_TIMEOUT = 0.1
    start = time.time()
    flush_time = w._process_transactions(0)
    end = time.time()

    assert len(w.transactions) == 0
    try:
        t = input_queue.get(True, 1)
    except queue.Empty:
        raise Exception("input_queue should not be empty")
    assert t == t1

    assert flush_time >= start + 1
    assert flush_time <= end + 1
def test_retry_worker_flush():
    stats = Stats()
    input_queue = queue.Queue(1)
    retry_queue = queue.Queue(1)
    w = RetryWorker(input_queue, retry_queue, stats)

    t_ready = Transaction("data", "https://datadog.com", "/success", None)
    t_ready.next_flush = time.time() - 10
    w.transactions.append(t_ready)

    t_not_ready = Transaction("data", "https://datadog.com", "/success", None)
    t_not_ready.next_flush = time.time() + 1000
    w.transactions.append(t_not_ready)

    w._flush_transactions()
    assert len(w.transactions) == 1
    assert w.transactions[0] == t_not_ready

    try:
        t = input_queue.get_nowait()
    except Exception:
        # we should not fail to get a transaction
        raise Exception("input_queue should not be empty")
    assert t == t_ready
示例#14
0
def main(cfg: DictConfig):
    np.random.seed(cfg.seed)
    torch.manual_seed(cfg.seed)

    obj_path = cfg.data.obj_path
    texture_path = cfg.data.texture_path
    views_folder = cfg.data.views_folder
    params_file = os.path.join(views_folder, "params.json")
    dataset = CowMultiViews(obj_path,
                            views_folder,
                            texture_path,
                            params_file=params_file)

    train_dataset, validation_dataset, test_dataset = CowMultiViews.random_split_dataset(
        dataset, train_fraction=0.7, validation_fraction=0.2)

    del dataset
    train_dataset.unit_normalize()
    validation_dataset.unit_normalize()
    test_dataset.unit_normalize()

    mesh_verts = test_dataset.get_verts()
    mesh_edges = test_dataset.get_edges()
    mesh_vert_normals = test_dataset.get_vert_normals()
    mesh_texture = test_dataset.get_texture()
    pytorch_mesh = test_dataset.pytorch_mesh.cuda()
    face_attrs = test_dataset.get_faces_as_vertex_matrices()

    feature_size = test_dataset.param_vectors.shape[1]

    torch_verts = torch.from_numpy(np.array(mesh_verts)).float().cuda()
    torch_edges = torch.from_numpy(np.array(mesh_edges)).long().cuda()
    torch_normals = torch.from_numpy(
        np.array(mesh_vert_normals)).float().cuda()
    torch_texture = torch.from_numpy(np.array(mesh_texture)).float().cuda()
    torch_texture = torch.unsqueeze(torch_texture.permute(2, 0, 1), 0)
    torch_face_attrs = torch.from_numpy(np.array(face_attrs)).float().cuda()

    subset_indices = [82]  #random.sample(list(range(len(test_dataset))),1)
    test_dataloader = Subset(test_dataset, subset_indices)
    print(subset_indices, len(test_dataloader))

    image_translator = ImageTranslator(input_dim=6,
                                       output_dim=3,
                                       image_size=tuple(
                                           cfg.data.image_size)).cuda()

    mse_loss = torch.nn.MSELoss()

    # Initialize the optimizer.
    optimizer = torch.optim.Adam(
        image_translator.parameters(),
        lr=cfg.optimizer.lr,
    )

    stats = None
    start_epoch = 0
    checkpoint_path = os.path.join(hydra.utils.get_original_cwd(),
                                   cfg.checkpoint_path)

    # Init the stats object.
    if stats is None:
        stats = Stats(["mse_loss", "sec/it"], )

    # Learning rate scheduler setup.

    # Following the original code, we use exponential decay of the
    # learning rate: current_lr = base_lr * gamma ** (epoch / step_size)
    def lr_lambda(epoch):
        return cfg.optimizer.lr_scheduler_gamma**(
            epoch / cfg.optimizer.lr_scheduler_step_size)

    # The learning rate scheduling is implemented with LambdaLR PyTorch scheduler.
    lr_scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,
                                                     lr_lambda,
                                                     last_epoch=start_epoch -
                                                     1,
                                                     verbose=False)

    # Initialize the cache for storing variables needed for visulization.
    visuals_cache = collections.deque(maxlen=cfg.visualization.history_size)

    # Init the visualization visdom env.
    if cfg.visualization.visdom:
        viz = Visdom(
            server=cfg.visualization.visdom_server,
            port=cfg.visualization.visdom_port,
            use_incoming_socket=False,
        )
    else:
        viz = None

    loaded_data = torch.load(checkpoint_path)

    image_translator.load_state_dict(loaded_data["model"], strict=False)
    image_translator.eval()
    stats.new_epoch()

    image_list = []
    for iteration, data in enumerate(test_dataloader):
        print(iteration)
        optimizer.zero_grad()

        views, param_vectors = data
        views = torch.unsqueeze(torch.from_numpy(views), 0)
        param_vectors = torch.unsqueeze(torch.from_numpy(param_vectors), 0)
        views = views.float().cuda()
        param_vectors = param_vectors.float().cuda()
        camera_instance = Camera()
        camera_instance.lookAt(param_vectors[0][0],
                               math.degrees(param_vectors[0][1]),
                               math.degrees(param_vectors[0][2]))

        rasterizer_instance = Rasterizer()
        rasterizer_instance.init_rasterizer(camera_instance.camera)
        fragments = rasterizer_instance.rasterizer(pytorch_mesh)
        pix_to_face = fragments.pix_to_face
        bary_coords = fragments.bary_coords

        pix_features = torch.squeeze(
            interpolate_face_attributes(pix_to_face, bary_coords,
                                        torch_face_attrs), 3)
        param_matrix = torch.zeros(pix_features.size()[0],
                                   pix_features.size()[1],
                                   pix_features.size()[2],
                                   param_vectors.size()[1]).float().cuda()
        param_matrix[:, :, :, :] = param_vectors
        image_features = pix_features  # torch.cat([pix_features,param_matrix],3)
        predicted_render = image_translator(image_features, torch_texture)

        image_list = [
            views[0].permute(2, 0, 1), predicted_render[0].permute(2, 0, 1)
        ]

    if viz is not None:
        visualize_image_outputs(validation_images=image_list,
                                viz=viz,
                                visdom_env=cfg.visualization.visdom_env)
def test_stats():
    # The min is not enabled by default
    stats = Stats()

    metric_stats = {
        'foo': 2,
        'bar': 5,
    }

    # setters
    stats.set_stat('metrics', 4)
    stats.set_stat('events', 2)
    stats.set_stat('service_checks', 1)
    # totals
    stats.inc_stat('metrics_total', 4)
    stats.inc_stat('events_total', 2)
    stats.inc_stat('service_checks_total', 1)
    # info
    stats.set_info('metric_stats', metric_stats)

    stats_snapshot, info_snapshot = stats.snapshot()
    assert info_snapshot['metric_stats'] == metric_stats
    assert stats_snapshot['metrics'] == 4
    assert stats_snapshot['events'] == 2
    assert stats_snapshot['service_checks'] == 1
    assert stats_snapshot['metrics_total'] == 4
    assert stats_snapshot['events_total'] == 2
    assert stats_snapshot['service_checks_total'] == 1

    # test we got a deepcopy for stats
    stats.set_stat('metrics', 10)
    stats.inc_stat('metrics_total', 10)
    assert stats_snapshot != metric_stats
    assert stats_snapshot['metrics'] != stats.get_stat('metrics')

    # test we got a deepcopy for info
    metric_stats['bar'] += 1
    stats.set_info('metric_stats', metric_stats)
    assert info_snapshot != metric_stats
    assert info_snapshot['metric_stats']['foo'] == metric_stats['foo']
    assert info_snapshot['metric_stats']['bar'] != metric_stats['bar']

    # test for updated snapshots
    stats_snapshot, info_snapshot = stats.snapshot()
    assert stats_snapshot['metrics'] == 10
    assert stats_snapshot['metrics_total'] == 14
    assert info_snapshot['metric_stats']['foo'] == metric_stats['foo']
    assert info_snapshot['metric_stats']['bar'] == metric_stats['bar']

    # test strict get
    with pytest.raises(KeyError):
        stats.get_stat('nonexistent', strict=True)
    with pytest.raises(KeyError):
        stats.get_info('nonexistent', strict=True)
示例#16
0
                        args.__dict__[key] = float(value)
                    elif type_of_value == str:
                        if value == 'None':
                            args.__dict__[key] = None
                        elif not value:
                            args.__dict__[key] = ''
                        else:
                            args.__dict__[key] = value

    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    bleu = BLEU()
    if args.fold_name:
        print('fold_name_tail is : ', args.fold_name)
        stats = Stats(args.num_of_steps, args.processed_steps, args.fold_name)
    else:
        stats = Stats(args.num_of_steps, args.processed_steps)

    device_idx = args.device

    if any(x < 0 for x in device_idx):
        raise ValueError('Not supported for cpu mode.')
    if any(x < 0 for x in device_idx) and not all(x < 0 for x in device_idx):
        raise ValueError('Not supported for cpu + gpu mode.')

    if len(device_idx) == 1:
        if device_idx[0] < 0:
            corpus_device = torch.device('cpu')
            model_device = torch.device('cpu')
        else:
示例#17
0
def main(cfg: DictConfig):

    # Set the relevant seeds for reproducibility.
    np.random.seed(cfg.seed)
    torch.manual_seed(cfg.seed)

    # Device on which to run.
    if torch.cuda.is_available():
        device = "cuda"
    else:
        warnings.warn(
            "Please note that although executing on CPU is supported," +
            "the training is unlikely to finish in resonable time.")
        device = "cpu"

    # Initialize the Radiance Field model.
    model = RadianceFieldRenderer(
        image_size=cfg.data.image_size,
        n_pts_per_ray=cfg.raysampler.n_pts_per_ray,
        n_pts_per_ray_fine=cfg.raysampler.n_pts_per_ray,
        n_rays_per_image=cfg.raysampler.n_rays_per_image,
        min_depth=cfg.raysampler.min_depth,
        max_depth=cfg.raysampler.max_depth,
        stratified=cfg.raysampler.stratified,
        stratified_test=cfg.raysampler.stratified_test,
        chunk_size_test=cfg.raysampler.chunk_size_test,
        n_harmonic_functions_xyz=cfg.implicit_function.
        n_harmonic_functions_xyz,
        n_harmonic_functions_dir=cfg.implicit_function.
        n_harmonic_functions_dir,
        n_hidden_neurons_xyz=cfg.implicit_function.n_hidden_neurons_xyz,
        n_hidden_neurons_dir=cfg.implicit_function.n_hidden_neurons_dir,
        n_layers_xyz=cfg.implicit_function.n_layers_xyz,
        density_noise_std=cfg.implicit_function.density_noise_std,
    )
    # Move the model to the relevant device.
    model.to(device)

    # Init stats to None before loading.
    stats = None
    optimizer_state_dict = None
    start_epoch = 0

    checkpoint_path = os.path.join(hydra.utils.get_original_cwd(),
                                   cfg.checkpoint_path)
    if len(cfg.checkpoint_path) > 0:
        # Make the root of the experiment directory.
        checkpoint_dir = os.path.split(checkpoint_path)[0]
        os.makedirs(checkpoint_dir, exist_ok=True)

        # Resume training if requested.
        if cfg.resume and os.path.isfile(checkpoint_path):
            print(f"Resuming from checkpoint {checkpoint_path}.")
            loaded_data = torch.load(checkpoint_path)
            model.load_state_dict(loaded_data["model"])
            stats = pickle.loads(loaded_data["stats"])
            print(f"   => resuming from epoch {stats.epoch}.")
            optimizer_state_dict = loaded_data["optimizer"]
            start_epoch = stats.epoch

    # Initialize the optimizer.
    optimizer = torch.optim.Adam(
        model.parameters(),
        lr=cfg.optimizer.lr,
    )

    # Load the optimizer state dict in case we are resuming.
    if optimizer_state_dict is not None:
        optimizer.load_state_dict(optimizer_state_dict)
        optimizer.last_epoch = start_epoch

    # Init the stats object.
    if stats is None:
        stats = Stats([
            "loss", "mse_coarse", "mse_fine", "psnr_coarse", "psnr_fine",
            "sec/it"
        ], )

    # Learning rate scheduler setup.

    # Following the original code, we use exponential decay of the
    # learning rate: current_lr = base_lr * gamma ** (epoch / step_size)
    def lr_lambda(epoch):
        return cfg.optimizer.lr_scheduler_gamma**(
            epoch / cfg.optimizer.lr_scheduler_step_size)

    # The learning rate scheduling is implemented with LambdaLR PyTorch scheduler.
    lr_scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,
                                                     lr_lambda,
                                                     last_epoch=start_epoch -
                                                     1,
                                                     verbose=False)

    # Initialize the cache for storing variables needed for visulization.
    visuals_cache = collections.deque(maxlen=cfg.visualization.history_size)

    # Init the visualization visdom env.
    if cfg.visualization.visdom:
        viz = Visdom(
            server=cfg.visualization.visdom_server,
            port=cfg.visualization.visdom_port,
            use_incoming_socket=False,
        )
    else:
        viz = None

    # Load the training/validation data.
    train_dataset, val_dataset, _ = get_nerf_datasets(
        dataset_name=cfg.data.dataset_name,
        image_size=cfg.data.image_size,
    )

    if cfg.data.precache_rays:
        # Precache the projection rays.
        model.eval()
        with torch.no_grad():
            for dataset in (train_dataset, val_dataset):
                cache_cameras = [e["camera"].to(device) for e in dataset]
                cache_camera_hashes = [e["camera_idx"] for e in dataset]
                model.precache_rays(cache_cameras, cache_camera_hashes)

    train_dataloader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=1,
        shuffle=True,
        num_workers=0,
        collate_fn=trivial_collate,
    )

    # The validation dataloader is just an endless stream of random samples.
    val_dataloader = torch.utils.data.DataLoader(
        val_dataset,
        batch_size=1,
        num_workers=0,
        collate_fn=trivial_collate,
        sampler=torch.utils.data.RandomSampler(
            val_dataset,
            replacement=True,
            num_samples=cfg.optimizer.max_epochs,
        ),
    )

    # Set the model to the training mode.
    model.train()

    # Run the main training loop.
    for epoch in range(start_epoch, cfg.optimizer.max_epochs):
        stats.new_epoch()  # Init a new epoch.
        for iteration, batch in enumerate(train_dataloader):
            image, camera, camera_idx = batch[0].values()
            image = image.to(device)
            camera = camera.to(device)

            optimizer.zero_grad()

            # Run the forward pass of the model.
            nerf_out, metrics = model(
                camera_idx if cfg.data.precache_rays else None,
                camera,
                image,
            )

            # The loss is a sum of coarse and fine MSEs
            loss = metrics["mse_coarse"] + metrics["mse_fine"]

            # Take the training step.
            loss.backward()
            optimizer.step()

            # Update stats with the current metrics.
            stats.update(
                {
                    "loss": float(loss),
                    **metrics
                },
                stat_set="train",
            )

            if iteration % cfg.stats_print_interval == 0:
                stats.print(stat_set="train")

            # Update the visualisatioon cache.
            visuals_cache.append({
                "camera":
                camera.cpu(),
                "camera_idx":
                camera_idx,
                "image":
                image.cpu().detach(),
                "rgb_fine":
                nerf_out["rgb_fine"].cpu().detach(),
                "rgb_coarse":
                nerf_out["rgb_coarse"].cpu().detach(),
                "rgb_gt":
                nerf_out["rgb_gt"].cpu().detach(),
                "coarse_ray_bundle":
                nerf_out["coarse_ray_bundle"],
            })

        # Adjust the learning rate.
        lr_scheduler.step()

        print(cfg.validation_epoch_interval)
        # Validation
        if epoch % cfg.validation_epoch_interval == 0 and epoch > 0:

            # Sample a validation camera/image.
            val_batch = next(val_dataloader.__iter__())
            val_image, val_camera, camera_idx = val_batch[0].values()
            val_image = val_image.to(device)
            val_camera = val_camera.to(device)

            # Activate eval mode of the model (allows to do a full rendering pass).
            model.eval()
            with torch.no_grad():
                val_nerf_out, val_metrics = model(
                    camera_idx if cfg.data.precache_rays else None,
                    val_camera,
                    val_image,
                )

            # Update stats with the validation metrics.
            stats.update(val_metrics, stat_set="val")
            stats.print(stat_set="val")

            if viz is not None:
                # Plot that loss curves into visdom.
                stats.plot_stats(
                    viz=viz,
                    visdom_env=cfg.visualization.visdom_env,
                    plot_file=None,
                )
                # Visualize the intermediate results.
                visualize_nerf_outputs(val_nerf_out, visuals_cache, viz,
                                       cfg.visualization.visdom_env)

            # Set the model back to train mode.
            model.train()

        # Checkpoint.
        if (epoch % cfg.checkpoint_epoch_interval == 0
                and len(cfg.checkpoint_path) > 0 and epoch > 0):
            print(f"Storing checkpoint {checkpoint_path}.")
            data_to_store = {
                "model": model.state_dict(),
                "optimizer": optimizer.state_dict(),
                "stats": pickle.dumps(stats),
            }
            torch.save(data_to_store, checkpoint_path)
示例#18
0
class Collector(object):
    CORE_CHECKS = ['cpu', 'load', 'iostat', 'memory', 'filesystem', 'uptime']

    def __init__(self, config, aggregator=None):
        self._config = config
        self._loaders = []
        self._check_classes = {}
        self._check_classes_errors = defaultdict(dict)
        self._check_instance_errors = defaultdict(dict)
        self._check_instances = defaultdict(list)
        self._check_instance_signatures = {}
        self._hostname = get_hostname()
        self._aggregator = aggregator
        self._status = Stats()

        self.set_loaders()

    def set_loaders(self):
        check_loader = CheckLoader()
        check_loader.add_place(self._config['additional_checksd'])
        self._loaders = [check_loader]
        self._loaders.append(WheelLoader(namespace=DD_WHEEL_NAMESPACE))

    def set_aggregator(self, aggregator):
        if not isinstance(aggregator, Aggregator):
            raise ValueError('argument should be of type Aggregator')

        self._aggregator = aggregator

    @property
    def status(self):
        return self._status

    def load_core_checks(self):
        from checks.corechecks.system import (Cpu, Load, Memory, IOStat,
                                              Filesystem, UptimeCheck)
        self._check_classes['cpu'] = Cpu
        self._check_classes['filesystem'] = Filesystem
        self._check_classes['iostat'] = IOStat
        self._check_classes['load'] = Load
        self._check_classes['memory'] = Memory
        self._check_classes['filesystem'] = Filesystem
        self._check_classes['uptime'] = UptimeCheck

    def load_check_classes(self):
        self.load_core_checks()

        for _, check_configs in self._config.get_check_configs().items():
            for check_name in check_configs:
                log.debug("Found config for check %s...", check_name)

                if check_name in self._check_classes:
                    continue

                for loader in self._loaders:
                    try:
                        check_class, errors = loader.load(check_name)
                        if check_class:
                            self._check_classes[check_name] = check_class
                        if errors:
                            self._check_classes_errors[check_name][type(
                                loader).__name__] = errors

                        if check_class:
                            log.debug("Class found for %s...", check_name)
                            break
                    except Exception:
                        log.exception("unexpected error loading check %s",
                                      check_name)

        self._status.set_info('check_classes', copy(
            self._check_classes))  # shallow copy suffices
        self._status.set_info('loader_errors',
                              deepcopy(self._check_classes_errors))

    def instantiate_checks(self):
        for source, check_configs in self._config.get_check_configs().items():
            for check_name, configs in check_configs.items():
                log.debug('Trying to instantiate: %s', check_name)
                check_class = self._check_classes.get(check_name)
                if check_class:
                    for config in configs:
                        init_config = config.get('init_config', {})
                        if init_config is None:
                            init_config = {}
                        instances = config.get(
                            'instances')  # should be single instance
                        for instance in instances:
                            signature = (check_name, init_config, instance)
                            signature_hash = AgentCheck.signature_hash(
                                *signature)
                            if signature_hash in self._check_instance_signatures:
                                log.info(
                                    'instance with identical signature already configured - skipping'
                                )
                                continue

                            try:
                                check_instance = check_class(
                                    check_name, init_config, instance,
                                    self._aggregator)
                                self._check_instances[check_name].append(
                                    check_instance)
                                self._check_instance_signatures[
                                    signature_hash] = signature
                            except Exception as e:
                                log.error(
                                    "unable to instantiate instance %s for %s: %s",
                                    instance, check_name, e)

        for check_name in self.CORE_CHECKS:
            if check_name in self._check_instances:
                # already instantiated - skip
                continue

            check_class = self._check_classes[check_name]
            signature = (check_name, {}, {})
            signature_hash = AgentCheck.signature_hash(*signature)
            try:
                check_instance = check_class(*signature)
                check_instance.set_aggregator(self._aggregator)
                self._check_instances[check_name] = [check_instance]
                self._check_instance_signatures[signature_hash] = signature
            except Exception:
                log.error("unable to instantiate core check %s", check_name)

    def run_checks(self):
        for name, checks in self._check_instances.items():
            log.info('running check %s...', name)
            for check in checks:
                try:
                    result = check.run()
                except Exception:
                    log.exception("error for instance: %s",
                                  str(check.instance))

                if result:
                    self._check_instance_errors[name][check.signature] = result
                    log.error('There was an error running your %s: %s', name,
                              result.get('message'))
                    log.error('Traceback %s: %s', name,
                              result.get('traceback'))

        self._status.set_info('runtime_errors',
                              deepcopy(self._check_instance_errors))
示例#19
0
 def __init__(self, config):
     self.config = config
     self.stats = Stats(self.config)
     self.adb = Adb()
示例#20
0
def main(cfg: DictConfig):
    np.random.seed(cfg.seed)
    torch.manual_seed(cfg.seed)

    obj_path = cfg.data.obj_path
    texture_path = cfg.data.texture_path
    views_folder = cfg.data.views_folder
    params_file = os.path.join(views_folder,"params.json")
    dataset = CowMultiViews(obj_path,views_folder,texture_path,params_file=params_file)

    train_dataset, validation_dataset, test_dataset = CowMultiViews.random_split_dataset(dataset,
                                                                                         train_fraction=0.7,
                                                                                         validation_fraction=0.2)

    del dataset
    train_dataset.unit_normalize()
    validation_dataset.unit_normalize()

    mesh_verts = train_dataset.get_verts()
    mesh_edges = train_dataset.get_edges()
    mesh_vert_normals = train_dataset.get_vert_normals()
    mesh_texture = train_dataset.get_texture()
    pytorch_mesh = train_dataset.pytorch_mesh.cuda()

    random_face_attrs = train_dataset.get_faces_as_vertex_matrices(features_list=['random'],num_random_dims=cfg.training.feature_dim)
    coord_face_attrs = train_dataset.get_faces_as_vertex_matrices(features_list=['coord'],num_random_dims=cfg.training.feature_dim)
    normal_face_attrs = train_dataset.get_faces_as_vertex_matrices(features_list=['normal'],num_random_dims=cfg.training.feature_dim)

    torch_verts = torch.from_numpy(np.array(mesh_verts)).float().cuda()
    torch_edges = torch.from_numpy(np.array(mesh_edges)).long().cuda()
    torch_normals = torch.from_numpy(np.array(mesh_vert_normals)).float().cuda()
    torch_texture = torch.from_numpy(np.array(mesh_texture)).float().cuda()
    torch_texture = torch.unsqueeze(torch_texture,0)
    torch_random_face_attrs = torch.tensor(np.array(random_face_attrs),requires_grad=True).float().cuda()
    torch_random_face_attrs = torch.nn.Parameter(torch_random_face_attrs)
    torch_coord_face_attrs = torch.tensor(np.array(coord_face_attrs)).float().cuda()
    torch_normal_face_attrs = torch.tensor(np.array(normal_face_attrs)).float().cuda()

    train_dataloader = DataLoader(train_dataset,batch_size=cfg.training.batch_size,shuffle=True,num_workers=4)
    validation_dataloader = DataLoader(validation_dataset,batch_size=cfg.training.batch_size,shuffle=True,num_workers=4)

    image_translator = ImageTranslator(input_dim=cfg.training.feature_dim+9,output_dim=3,
                                   image_size=tuple(cfg.data.image_size)).cuda()

    mse_loss = torch.nn.MSELoss()

    # Initialize the optimizer.
    optimizer = torch.optim.Adam(
        list(image_translator.parameters())+[torch_random_face_attrs],
        lr=cfg.optimizer.lr,
    )

    stats = None
    start_epoch = 0
    checkpoint_path = os.path.join(hydra.utils.get_original_cwd(), cfg.checkpoint_path)

    # Init the stats object.
    if stats is None:
        stats = Stats(
            ["mse_loss", "sec/it"],
        )

    # Learning rate scheduler setup.

    # Following the original code, we use exponential decay of the
    # learning rate: current_lr = base_lr * gamma ** (epoch / step_size)
    def lr_lambda(epoch):
        return cfg.optimizer.lr_scheduler_gamma ** (
                epoch / cfg.optimizer.lr_scheduler_step_size
        )

    # The learning rate scheduling is implemented with LambdaLR PyTorch scheduler.
    lr_scheduler = torch.optim.lr_scheduler.LambdaLR(
        optimizer, lr_lambda, last_epoch=start_epoch - 1, verbose=False
    )

    # Initialize the cache for storing variables needed for visulization.
    visuals_cache = collections.deque(maxlen=cfg.visualization.history_size)

    # Init the visualization visdom env.
    if cfg.visualization.visdom:
        viz = Visdom(
            server=cfg.visualization.visdom_server,
            port=cfg.visualization.visdom_port,
            use_incoming_socket=False,
        )
    else:
        viz = None

    for epoch in range(cfg.optimizer.max_epochs):
        image_translator.train()
        stats.new_epoch()
        for iteration,data in enumerate(train_dataloader):
            optimizer.zero_grad()

            views,param_vectors = data
            views = views.float().cuda()
            param_vectors = param_vectors.float().cuda()
            camera_instance = Camera()
            camera_instance.lookAt(param_vectors[0][0],math.degrees(param_vectors[0][1]),math.degrees(param_vectors[0][2]))
            camera_location = param_vectors[0,3:6]
            light_location = param_vectors[0,6:9]
            torch_camera_face_attrs = torch_coord_face_attrs - camera_location
            torch_light_face_attrs = torch_coord_face_attrs - light_location
            torch_face_attrs = torch.cat([torch_camera_face_attrs,torch_normal_face_attrs,torch_light_face_attrs,torch_random_face_attrs],2)

            rasterizer_instance = Rasterizer()
            rasterizer_instance.init_rasterizer(camera_instance.camera)
            fragments = rasterizer_instance.rasterizer(pytorch_mesh)
            pix_to_face = fragments.pix_to_face
            bary_coords = fragments.bary_coords

            pix_features = torch.squeeze(interpolate_face_attributes(pix_to_face,bary_coords,torch_face_attrs),3)
            predicted_render = image_translator(pix_features,torch_texture)

            loss = 1000*mse_loss(predicted_render,views)
            loss.backward()
            optimizer.step()

            # Update stats with the current metrics.
            stats.update(
                {"mse_loss": float(loss)},
                stat_set="train",
            )

            if iteration % cfg.stats_print_interval == 0:
                stats.print(stat_set="train")

        # Adjust the learning rate.
        #lr_scheduler.step()

        # Validation
        if epoch % cfg.validation_epoch_interval == 0: # and epoch > 0:

            # Sample a validation camera/image.
            val_batch = next(validation_dataloader.__iter__())
            views, param_vectors= val_batch
            views = views.float().cuda()
            param_vectors = param_vectors.float().cuda()

            # Activate eval mode of the model (allows to do a full rendering pass).
            image_translator.eval()
            with torch.no_grad():
                camera_instance = Camera()
                camera_instance.lookAt(param_vectors[0][0], math.degrees(param_vectors[0][1]), math.degrees(param_vectors[0][2]))
                camera_location = param_vectors[0,3:6]
                light_location = param_vectors[0,6:9]
                torch_camera_face_attrs = torch_coord_face_attrs - camera_location
                torch_light_face_attrs = torch_coord_face_attrs - light_location
                torch_face_attrs = torch.cat([torch_camera_face_attrs,torch_normal_face_attrs,torch_light_face_attrs,torch_random_face_attrs],2)

                rasterizer_instance = Rasterizer()
                rasterizer_instance.init_rasterizer(camera_instance.camera)
                fragments = rasterizer_instance.rasterizer(pytorch_mesh)
                pix_to_face = fragments.pix_to_face
                bary_coords = fragments.bary_coords

                pix_features = torch.squeeze(interpolate_face_attributes(pix_to_face, bary_coords, torch_face_attrs), 3)
                #pix_features = pix_features.permute(0, 3, 1, 2)
                predicted_render = image_translator(pix_features,torch_texture)
                loss = 1000*mse_loss(predicted_render,views)


            # Update stats with the validation metrics.
            stats.update({"mse_loss":loss}, stat_set="val")
            stats.print(stat_set="val")

            if viz is not None:
                # Plot that loss curves into visdom.
                stats.plot_stats(
                    viz=viz,
                    visdom_env=cfg.visualization.visdom_env,
                    plot_file=None,
                )
                # Visualize the intermediate results.
                render_max = torch.max(predicted_render)
                visualize_image_outputs(
                    validation_images = [views[0].permute(2,0,1),predicted_render[0].permute(2,0,1)],viz=viz,visdom_env=cfg.visualization.visdom_env
                )

            # Set the model back to train mode.
            image_translator.train()

        # Checkpoint.
        if (
                epoch % cfg.checkpoint_epoch_interval == 0
                and len(cfg.checkpoint_path) > 0
                and epoch > 0
        ):
            print(f"Storing checkpoint {checkpoint_path}.")
            data_to_store = {
                "model": image_translator.state_dict(),
                "features" : torch_face_attrs,
                "optimizer": optimizer.state_dict(),
                "stats": pickle.dumps(stats),
            }
            torch.save(data_to_store, checkpoint_path)
示例#21
0
def stats(stats_type, **args):
    """Output general info about project"""
    stats = Stats()
    if stats_type == 'overview':
        stats.overview()
    elif stats_type == 'all':
        stats.all()
    elif stats_type == 'sample':
        stats.sample()
    elif stats_type == 'annotation':
        stats.annotation(**args)
    elif stats_type == 'annotator_outliers':
        stats.annotator_outliers(**args)
    elif stats_type == 'annotation_cleaned':
        stats.annotation_cleaned(**args)
    else:
        raise ValueError('Command {} is not available.'.format(stats_type))
    print(stats.text)
示例#22
0
class Aggregator(object):
    """
    Abstract metric aggregator class.
    """
    # Types of metrics that allow strings
    ALLOW_STRINGS = ['s', ]
    # Types that are not implemented and ignored
    IGNORE_TYPES = ['d', ]
    # prefixes
    SC_PREFIX = '_sc'
    EVENT_PREFIX = '_e'

    def __init__(self, hostname, interval=1.0, expiry_seconds=300,
                 formatter=None, recent_point_threshold=None,
                 histogram_aggregates=None, histogram_percentiles=None,
                 utf8_decoding=False):
        # TODO(jaime): add support for event, service_check sources
        self.events = []
        self.service_checks = []
        self.stats = Stats()

        # TODO(jaime): we can probably kill total counts
        self.packet_count = 0
        self.metric_count = 0
        self.event_count = 0
        self.service_check_count = 0

        self.hostname = hostname
        self.expiry_seconds = expiry_seconds
        self.formatter = formatter or api_formatter
        self.interval = float(interval)

        recent_point_threshold = recent_point_threshold or DEFAULT_RECENT_POINT_THRESHOLD
        self.recent_point_threshold = int(recent_point_threshold)
        self.num_discarded_old_points = 0

        # Additional config passed when instantiating metric configs
        self.metric_config = {
            Histogram: {
                'aggregates': histogram_aggregates,
                'percentiles': histogram_percentiles
            }
        }

        self.utf8_decoding = utf8_decoding

    def deduplicate_tags(self, tags):
        return sorted(set(tags))

    def packets_per_second(self, interval):
        if interval == 0:
            return 0
        return round(float(self.packet_count)/interval, 2)

    def parse_metric_packet(self, packet):
        """
        Schema of a dogstatsd packet:
        <name>:<value>|<metric_type>|@<sample_rate>|#<tag1_name>:<tag1_value>,<tag2_name>:<tag2_value>:<value>|<metric_type>...
        """
        parsed_packets = []
        name_and_metadata = packet.split(':', 1)

        if len(name_and_metadata) != 2:
            raise Exception('Unparseable metric packet: {}'.format(packet))

        name = name_and_metadata[0]
        broken_split = name_and_metadata[1].split(':')
        data = []
        partial_datum = None
        for token in broken_split:
            # We need to fix the tag groups that got broken by the : split
            if partial_datum is None:
                partial_datum = token
            elif "|" not in token:
                partial_datum += ":" + token
            else:
                data.append(partial_datum)
                partial_datum = token
        data.append(partial_datum)

        for datum in data:
            value_and_metadata = datum.split('|')

            if len(value_and_metadata) < 2:
                raise Exception('Unparseable metric packet: {}'.format(packet))

            # Submit the metric
            raw_value = value_and_metadata[0]
            metric_type = value_and_metadata[1]

            if metric_type in self.ALLOW_STRINGS:
                value = raw_value
            elif len(metric_type) > 0 and metric_type[0] in self.IGNORE_TYPES:
                continue
            else:
                # Try to cast as an int first to avoid precision issues, then as a
                # float.
                try:
                    value = int(raw_value)
                except ValueError:
                    try:
                        value = float(raw_value)
                    except ValueError:
                        # Otherwise, raise an error saying it must be a number
                        raise Exception('Metric value must be a number: {}, {}'.format(name, raw_value))

            # Parse the optional values - sample rate & tags.
            sample_rate = 1
            tags = None
            try:
                for m in value_and_metadata[2:]:
                    # Parse the sample rate
                    if m[0] == '@':
                        sample_rate = float(m[1:])
                        # in case it's in a bad state
                        sample_rate = 1 if sample_rate < 0 or sample_rate > 1 else sample_rate
                    elif m[0] == '#':
                        tags = tuple(sorted(m[1:].split(',')))
            except IndexError:
                log.warning('Incorrect metric metadata: metric_name:%s, metadata:%s',
                            name, ' '.join(value_and_metadata[2:]))

            parsed_packets.append((name, value, metric_type, tags, sample_rate))

        return parsed_packets

    def _unescape_sc_content(self, string):
        return string.replace('\\n', '\n').replace(r'm\:', 'm:')

    def _unescape_event_text(self, string):
        return string.replace('\\n', '\n')

    def parse_event_packet(self, packet):
        try:
            name_and_metadata = packet.split(':', 1)

            if len(name_and_metadata) != 2:
                raise Exception('Unparseable event packet: {}'.format(packet))
            # Event syntax:
            # _e{5,4}:title|body|meta
            name = name_and_metadata[0]
            metadata = name_and_metadata[1]
            title_length, text_length = name.split(',')

            title_length = int(title_length[3:])
            text_length = int(text_length[:-1])

            event = {
                'title': metadata[:title_length],
                'text': self._unescape_event_text(metadata[title_length+1:title_length+text_length+1])
            }
            meta = metadata[title_length+text_length+1:]
            for m in meta.split('|')[1:]:
                if m[0] == 't':
                    event['alert_type'] = m[2:]
                elif m[0] == 'k':
                    event['aggregation_key'] = m[2:]
                elif m[0] == 's':
                    event['source_type_name'] = m[2:]
                elif m[0] == 'd':
                    event['date_happened'] = int(m[2:])
                elif m[0] == 'p':
                    event['priority'] = m[2:]
                elif m[0] == 'h':
                    event['hostname'] = m[2:]
                elif m[0] == '#':
                    event['tags'] = self.deduplicate_tags(m[1:].split(','))
            return event
        except (IndexError, ValueError):
            raise Exception('Unparseable event packet: {}'.format(packet))

    def parse_sc_packet(self, packet):
        try:
            _, data_and_metadata = packet.split('|', 1)
            # Service check syntax:
            # _sc|check_name|status|meta
            if data_and_metadata.count('|') == 1:
                # Case with no metadata
                check_name, status = data_and_metadata.split('|')
                metadata = ''
            else:
                check_name, status, metadata = data_and_metadata.split('|', 2)

            service_check = {
                'check_name': check_name,
                'status': int(status)
            }

            message_delimiter = 'm:' if metadata.startswith('m:') else '|m:'
            if message_delimiter in metadata:
                meta, message = metadata.rsplit(message_delimiter, 1)
                service_check['message'] = self._unescape_sc_content(message)
            else:
                meta = metadata

            if not meta:
                return service_check

            meta = str(meta)
            for m in meta.split('|'):
                if m[0] == 'd':
                    service_check['timestamp'] = float(m[2:])
                elif m[0] == 'h':
                    service_check['hostname'] = m[2:]
                elif m[0] == '#':
                    service_check['tags'] = self.deduplicate_tags(m[1:].split(','))

            return service_check

        except (IndexError, ValueError):
            raise Exception('Unparseable service check packet: {}'.format(packet))

    def submit_packets(self, packets):
        # We should probably consider that packets are always encoded
        # in utf8, but decoding all packets has an perf overhead of 7%
        # So we let the user decide if we wants utf8 by default
        # Keep a very conservative approach anyhow
        # Clients MUST always send UTF-8 encoded content
        if self.utf8_decoding:
            try:
                packets = packets.decode('utf-8')
            except AttributeError:
                pass

        for packet in packets.splitlines():
            if not packet.strip():
                continue

            self.packet_count += 1

            if packet.startswith(self.EVENT_PREFIX):
                event = self.parse_event_packet(packet)
                self.event(**event)
                self.event_count += 1
            elif packet.startswith(self.SC_PREFIX):
                service_check = self.parse_sc_packet(packet)
                self.service_check(**service_check)
                self.service_check_count += 1
            else:
                parsed_packets = self.parse_metric_packet(packet)
                for name, value, mtype, tags, sample_rate in parsed_packets:
                    hostname, tags = self._extract_magic_tags(tags)
                    self.submit_metric(name, value, mtype, tags=tags,
                                       hostname=hostname, sample_rate=sample_rate)

    def _extract_magic_tags(self, tags):
        """Magic tags (host) override metric hostname attributes"""
        hostname = None
        # This implementation avoid list operations for the common case
        if tags:
            tags_to_remove = []
            for tag in tags:
                if tag.startswith('host:'):
                    hostname = tag[5:]
                    tags_to_remove.append(tag)
            if tags_to_remove:
                # tags is a tuple already sorted, we convert it into a list to pop elements
                tags = list(tags)
                for tag in tags_to_remove:
                    tags.remove(tag)
                tags = tuple(tags) or None
        return hostname, tags

    def submit_metric(self, name, value, mtype, tags=None, hostname=None,
                      timestamp=None, sample_rate=1):
        """ Add a metric to be aggregated """
        raise NotImplementedError()

    def event(self, title, text, date_happened=None, alert_type=None, aggregation_key=None,
              source_type_name=None, priority=None, tags=None, hostname=None):
        event = {
            'msg_title': title,
            'msg_text': text,
        }
        if date_happened is not None:
            event['timestamp'] = date_happened
        else:
            event['timestamp'] = int(time())
        if alert_type is not None:
            event['alert_type'] = alert_type
        if aggregation_key is not None:
            event['aggregation_key'] = aggregation_key
        if source_type_name is not None:
            event['source_type_name'] = source_type_name
        if priority is not None:
            event['priority'] = priority
        if tags is not None:
            event['tags'] = self.deduplicate_tags(tags)
        if hostname is not None:
            event['host'] = hostname
        else:
            event['host'] = self.hostname

        self.events.append(event)

    def service_check(self, check_name, status, tags=None, timestamp=None,
                      hostname=None, message=None):
        service_check = {
            'check': check_name,
            'status': status,
            'timestamp': timestamp or int(time())
        }
        if tags is not None:
            service_check['tags'] = self.deduplicate_tags(tags)

        if hostname is not None:
            service_check['host_name'] = hostname
        else:
            service_check['host_name'] = self.hostname
        if message is not None:
            service_check['message'] = message

        self.service_checks.append(service_check)

    def flush(self):
        """ Flush aggregated metrics """
        raise NotImplementedError()

    def flush_events(self):
        events = self.events
        self.events = []

        self.stats.set_stat('events', self.event_count)
        self.stats.inc_stat('events_total', self.event_count)
        self.event_count = 0

        log.info("Received %d events since last flush", len(events))

        return events

    def flush_service_checks(self):
        service_checks = self.service_checks
        self.service_checks = []

        self.stats.set_stat('service_checks', self.service_check_count)
        self.stats.inc_stat('service_checks_total', self.service_check_count)
        self.service_check_count = 0

        log.info("Received %d service check runs since last flush", len(service_checks))

        return service_checks

    def send_packet_count(self, metric_name):
        self.submit_metric(metric_name, self.packet_count, 'g')
示例#23
0
文件: runner.py 项目: dougsc/gp
class Runner:
  NEW_GEN_DIST = {'mutate': 0.05, 'reproduce': 0.5}
  # Stats Keys:
  SK_LOWEST_ERROR = 'lowest_error'
  SK_BEST_INDIVIDUAL = 'best_individual'
  SK_TARGET_SAMPLES = 'target_samples'
  SK_ACTUAL_SAMPLES = 'actual_samples'  
  SK_BEST_TREE = 'best_tree'

  @classmethod
  def log(cls):
    return GP_Logger.logger(cls.__name__)

  def __init__(self, population, termination_error_threshold, max_generations, tournament_size=2):
    self.population = population
    self.termination_error_threshold = termination_error_threshold
    self.current_generation = 1
    self.current_best_error = sys.maxint
    self.max_generations = max_generations
    self.tournament_size = tournament_size
    self.stats = Stats(self.__class__.__name__)
    self.stats.init_series([self.SK_LOWEST_ERROR, self.SK_BEST_INDIVIDUAL, self.SK_TARGET_SAMPLES, self.SK_ACTUAL_SAMPLES,
                            self.SK_BEST_TREE])

  def store_target_samples(self):
    experiment = self.population[0].exp_class(*self.population[0].exp_args)
    map(lambda x:self.stats.add_to_series(self.SK_TARGET_SAMPLES, x), experiment.target_data())

  def store_actual_samples(self, individual):
    self.stats.init_series(self.SK_ACTUAL_SAMPLES)
    map(lambda x:self.stats.add_to_series(self.SK_ACTUAL_SAMPLES, x), individual.evaluate_data())

  def findIndexOfBest(self):
    return numpy.argmin(map(lambda x:x.error, self.population))

  def evaluate(self):
    self.log().debug('evaluating generation %d' % (self.current_generation))
    for individual in self.population:
      individual.evaluate()

    best = self.findIndexOfBest()
    self.log().debug('population member %d was best with %d error (target: %d)' % (best, self.population[best].error, self.termination_error_threshold))
    self.stats.add_to_series(self.SK_LOWEST_ERROR, {'error': self.population[best].error, 'index': self.current_generation}, timestamp=True)
    self.stats.add_to_series(self.SK_BEST_INDIVIDUAL, {'best_ix': best, 'index': self.current_generation}, timestamp=True)
    return self.population[best]

  def update_standings(self):
    for i in xrange(0, len(self.population), self.tournament_size):
      lowest_error = min(map(lambda x:x.error, self.population[i:i+self.tournament_size]))
      winners = filter(lambda x:x.error == lowest_error, self.population[i:i+self.tournament_size])
      losers = filter(lambda x:x.error > lowest_error, self.population[i:i+self.tournament_size])
      assert len(winners) + len(losers) == self.tournament_size, 'Expected winners (%d) + losers (%d) = tournament size (%d)' % (len(winners), len(losers), self.tournament_size)
      if len(losers) == 0:
        continue

      self.log().debug('best in tournament [%d:%d](error: %d): %d winners' % (i, i+self.tournament_size, lowest_error, len(winners)))
      map(lambda x:x.increment_standing(), winners)
      map(lambda x:x.decrement_standing(), losers)

  def random_select_n_unique(self, number, weight_list):
    selection = []
    assert number < len(weight_list), 'attemt to get %d unique values from a list of %d elements' % (number, len(weight_list))
    weight_max = weight_list[-1]

    while len(selection) < number:
      ix = bisect.bisect_right(weight_list, random.uniform(0, weight_max))
      if not ix in selection:
        selection.append(ix)

    return selection

  def generate_new_population(self):
    new_population = []
    self.update_standings()
    weight_list = list(numpy.cumsum(map(lambda x:x.standing, self.population)))
    pop_size = len(self.population)

    chosen_number = int(pop_size * self.NEW_GEN_DIST['reproduce'])
    chosen_number = chosen_number - (chosen_number % 2)
    individuals_chosen = self.random_select_n_unique(chosen_number, weight_list)
    self.log().debug('%d indiviuals chosen to reproduce - %s' % (len(individuals_chosen), sorted(individuals_chosen)))
    for ix in xrange(0, len(individuals_chosen), 2):
      new_population.append(self.population[individuals_chosen[ix]].reproduce(self.population[individuals_chosen[ix+1]]))

    chosen_number = int(pop_size * self.NEW_GEN_DIST['mutate'])
    individuals_chosen = self.random_select_n_unique(chosen_number, weight_list)
    self.log().debug('%d indiviuals chosen to mutate - %s' % (len(individuals_chosen), sorted(individuals_chosen)))
    for ix in xrange(0, len(individuals_chosen)):
      new_population.append(self.population[individuals_chosen[ix]].mutate())

    chosen_number = len(self.population) - len(new_population)
    individuals_chosen = self.random_select_n_unique(chosen_number, weight_list)
    self.log().debug('%d indiviuals chosen to clone - %s' % (len(individuals_chosen), sorted(individuals_chosen)))
    for ix in xrange(0, len(individuals_chosen)):
      new_population.append(self.population[individuals_chosen[ix]].clone())
    
    assert len(self.population) == len(new_population), 'new population size does not match original'
    self.population = new_population
    self.current_generation += 1

  def check_evaluation(self, best):
    if best.error <= self.current_best_error:
      self.current_best_error = best.error
      self.stats.add_to_series(self.SK_BEST_TREE, {'tree': best.tree.dump_structure()})
      self.store_actual_samples(best)
    return (best.error <= self.termination_error_threshold)

  def run(self):
    self.store_target_samples()
    success = self.check_evaluation(self.evaluate())
    while self.current_generation <= self.max_generations and success == False:
      self.generate_new_population()
      self.log().debug('average standing for generation %d: %f' % (self.current_generation, 
                                       numpy.average(map(lambda x:x.standing, self.population))))
      success = self.check_evaluation(self.evaluate())
    print 'success: %s' % (success)
示例#24
0
class Forwarder(object):

    V1_ENDPOINT = "/intake/"
    V1_SERIES_ENDPOINT = "/api/v1/series"
    V1_SERVICE_CHECKS_ENDPOINT = "/api/v1/check_run"

    DD_API_HEADER = "DD-Api-Key"

    QUEUES_SIZE = 100
    WORKER_JOIN_TIME = 2

    def __init__(self, api_key, domain, nb_worker=4, proxies={}):
        self.api_key = api_key
        self.domain = domain
        self.stats = Stats()
        self.input_queue = queue.Queue(self.QUEUES_SIZE)
        self.retry_queue = queue.Queue(self.QUEUES_SIZE)
        self.workers = []
        self.nb_worker = nb_worker
        self.retry_worker = None
        self.proxies = proxies

    def start(self):
        self.retry_worker = RetryWorker(self.input_queue, self.retry_queue, self.stats)
        self.retry_worker.start()

        for i in range(self.nb_worker):
            w = Worker(self.input_queue, self.retry_queue, self.stats)
            w.start()
            self.workers.append(w)

    def stop(self):
        self.retry_worker.stop()

        for w in self.workers:
            w.stop()

        self.retry_worker.join(self.WORKER_JOIN_TIME)
        if self.retry_worker.is_alive():
            log.error("Could not stop thread '%s'", self.retry_worker.name)
        self.retry_worker = None

        for w in self.workers:
            # wait 2 seconds for the worker to stop
            w.join(self.WORKER_JOIN_TIME)
            if w.is_alive():
                log.error("Could not stop thread '%s'", w.name)
        self.workers = []

    def _submit_payload(self, endpoint, payload, extra_header=None):
        endpoint += "?api_key=" + self.api_key

        if extra_header:
            extra_header[self.DD_API_HEADER] = self.api_key
        else:
            extra_header = {self.DD_API_HEADER: self.api_key}

        t = Transaction(payload, self.domain, endpoint, extra_header, proxies=self.proxies)
        try:
            self.input_queue.put_nowait(t)
        except queue.Full as e:
            log.error("Could not submit transaction to '%s', queue is full (dropping it): %s", endpoint, e)

    def submit_v1_series(self, payload, extra_header):
        self.stats.inc_stat('series_payloads', 1)
        self._submit_payload(self.V1_SERIES_ENDPOINT, payload, extra_header)

    def submit_v1_intake(self, payload, extra_header):
        self.stats.inc_stat('intake_payloads', 1)
        self._submit_payload(self.V1_ENDPOINT, payload, extra_header)

    def submit_v1_service_checks(self, payload, extra_header):
        self.stats.inc_stat('service_check_payloads', 1)
        self._submit_payload(self.V1_SERVICE_CHECKS_ENDPOINT, payload, extra_header)
示例#25
0
def main(cfg: DictConfig):

    # Device on which to run.
    if torch.cuda.is_available():
        device = "cuda"
    else:
        warnings.warn(
            "Please note that although executing on CPU is supported," +
            "the testing is unlikely to finish in reasonable time.")
        device = "cpu"

    # Initialize the Radiance Field model.
    model = RadianceFieldRenderer(
        image_size=cfg.data.image_size,
        n_pts_per_ray=cfg.raysampler.n_pts_per_ray,
        n_pts_per_ray_fine=cfg.raysampler.n_pts_per_ray,
        n_rays_per_image=cfg.raysampler.n_rays_per_image,
        min_depth=cfg.raysampler.min_depth,
        max_depth=cfg.raysampler.max_depth,
        stratified=cfg.raysampler.stratified,
        stratified_test=cfg.raysampler.stratified_test,
        chunk_size_test=cfg.raysampler.chunk_size_test,
        n_harmonic_functions_xyz=cfg.implicit_function.
        n_harmonic_functions_xyz,
        n_harmonic_functions_dir=cfg.implicit_function.
        n_harmonic_functions_dir,
        n_hidden_neurons_xyz=cfg.implicit_function.n_hidden_neurons_xyz,
        n_hidden_neurons_dir=cfg.implicit_function.n_hidden_neurons_dir,
        n_layers_xyz=cfg.implicit_function.n_layers_xyz,
        density_noise_std=cfg.implicit_function.density_noise_std,
    )

    # Move the model to the relevant device.
    model.to(device)

    # Resume from the checkpoint.
    checkpoint_path = os.path.join(hydra.utils.get_original_cwd(),
                                   cfg.checkpoint_path)
    if not os.path.isfile(checkpoint_path):
        raise ValueError(f"Model checkpoint {checkpoint_path} does not exist!")

    print(f"Loading checkpoint {checkpoint_path}.")
    loaded_data = torch.load(checkpoint_path)
    # Do not load the cached xy grid.
    # - this allows to set an arbitrary evaluation image size.
    state_dict = {
        k: v
        for k, v in loaded_data["model"].items()
        if "_grid_raysampler._xy_grid" not in k
    }
    model.load_state_dict(state_dict, strict=False)

    # Load the test data.
    if cfg.test.mode == "evaluation":
        _, _, test_dataset = get_nerf_datasets(
            dataset_name=cfg.data.dataset_name,
            image_size=cfg.data.image_size,
        )
    elif cfg.test.mode == "export_video":
        train_dataset, _, _ = get_nerf_datasets(
            dataset_name=cfg.data.dataset_name,
            image_size=cfg.data.image_size,
        )
        test_dataset = generate_eval_video_cameras(
            train_dataset,
            trajectory_type=cfg.test.trajectory_type,
            up=cfg.test.up,
            scene_center=cfg.test.scene_center,
            n_eval_cams=cfg.test.n_frames,
            trajectory_scale=cfg.test.trajectory_scale,
        )
        # store the video in directory (checkpoint_file - extension + '_video')
        export_dir = os.path.splitext(checkpoint_path)[0] + "_video"
        os.makedirs(export_dir, exist_ok=True)
    else:
        raise ValueError(f"Unknown test mode {cfg.test_mode}.")

    # Init the test dataloader.
    test_dataloader = torch.utils.data.DataLoader(
        test_dataset,
        batch_size=1,
        shuffle=False,
        num_workers=0,
        collate_fn=trivial_collate,
    )

    if cfg.test.mode == "evaluation":
        # Init the test stats object.
        eval_stats = [
            "mse_coarse", "mse_fine", "psnr_coarse", "psnr_fine", "sec/it"
        ]
        stats = Stats(eval_stats)
        stats.new_epoch()
    elif cfg.test.mode == "export_video":
        # Init the frame buffer.
        frame_paths = []

    # Set the model to the eval mode.
    model.eval()

    # Run the main testing loop.
    for batch_idx, test_batch in enumerate(test_dataloader):
        print(batch_idx, len(test_dataloader))
        test_image, test_camera, camera_idx = test_batch[0].values()
        if test_image is not None:
            test_image = test_image.to(device)
        test_camera = test_camera.to(device)

        # Activate eval mode of the model (allows to do a full rendering pass).
        model.eval()
        with torch.no_grad():
            test_nerf_out, test_metrics = model(
                None,  # we do not use pre-cached cameras
                test_camera,
                test_image,
            )

        if cfg.test.mode == "evaluation":
            # Update stats with the validation metrics.
            stats.update(test_metrics, stat_set="test")
            stats.print(stat_set="test")

        elif cfg.test.mode == "export_video":
            # Store the video frame.
            frame = test_nerf_out["rgb_fine"][0].detach().cpu()
            frame_path = os.path.join(export_dir, f"frame_{batch_idx:05d}.png")
            print(f"Writing {frame_path}.")
            Image.fromarray(
                (frame.numpy() * 255.0).astype(np.uint8)).save(frame_path)
            frame_paths.append(frame_path)

    if cfg.test.mode == "evaluation":
        print(f"Final evaluation metrics on '{cfg.data.dataset_name}':")
        for stat in eval_stats:
            stat_value = stats.stats["test"][stat].get_epoch_averages()[0]
            print(f"{stat:15s}: {stat_value:1.4f}")

    elif cfg.test.mode == "export_video":
        # Convert the exported frames to a video.
        video_path = os.path.join(export_dir, "video.mp4")
        ffmpeg_bin = "ffmpeg"
        frame_regexp = os.path.join(export_dir, "frame_%05d.png")
        ffmcmd = (
            "%s -r %d -i %s -vcodec h264 -f mp4 -y -b 2000k -pix_fmt yuv420p %s"
            % (ffmpeg_bin, cfg.test.fps, frame_regexp, video_path))
        ret = os.system(ffmcmd)
        if ret != 0:
            raise RuntimeError("ffmpeg failed!")