示例#1
0
    def predict(self, image_uri, label_uri=None, config_uri=None):
        """Generate predictions for the given image.

        Args:
            image_uri: URI of the image to make predictions against.
                This can be any type of URI readable by Raster Vision
                FileSystems.
            label_uri: Optional URI to save labels off into.
            config_uri: Optional URI in which to save the bundle_config,
                which can be useful to client applications for understanding
                how to interpret the labels.

            Returns:
                rastervision.data.labels.Labels containing the predicted labels.
        """
        if not self.model_loaded:
            self.load_model()
        scene_config = self.scene_config.for_prediction(image_uri, label_uri) \
                                        .create_local(self.tmp_dir)

        try:
            scene = scene_config.create_scene(self.task_config, self.tmp_dir)
            # If we are analyzing per scene, run analyzers
            # Analyzers should overwrite files in the tmp_dir
            if self.update_stats:
                for analyzer in self.analyzers:
                    analyzer.process([scene], self.tmp_dir)

                # Reload scene to refresh any new analyzer config
                scene = scene_config.create_scene(self.task_config,
                                                  self.tmp_dir)
        except ChannelOrderError:
            raise ValueError(
                'The predict package is using a channel_order '
                'with channels unavailable in the imagery.\nTo set a new '
                'channel_order that only uses channels available in the '
                'imagery, use the --channel-order option.')

        with scene.activate():
            labels = self.task.predict_scene(scene, self.tmp_dir)
            if label_uri:
                scene.prediction_label_store.save(labels)

        if config_uri:
            msg = self.bundle_config.to_builder() \
                                    .with_scene(scene_config) \
                                    .build() \
                                    .to_proto()
            save_json_config(msg, config_uri)

        return labels
示例#2
0
    def _run_experiment(self, command_dag):
        tmp_dir = self.tmp_dir or RVConfig.get_tmp_dir().name
        make_dir(tmp_dir)
        makefile_name = os.path.join(tmp_dir, 'Makefile')
        with open(makefile_name, 'w') as makefile:
            command_ids = command_dag.get_sorted_command_ids()

            # .PHONY: 0 1 2 3 4 5
            makefile.write('.PHONY:')
            for command_id in command_ids:
                makefile.write(' {}'.format(command_id))
            makefile.write('\n\n')

            # all: 0 1 2 3 4 5
            makefile.write('all:')
            for command_id in command_ids:
                makefile.write(' {}'.format(command_id))
            makefile.write('\n\n')

            for command_id in command_ids:
                # 0: 1 2
                makefile.write('{}:'.format(command_id))
                for upstream_id in command_dag.get_upstream_command_ids(
                        command_id):
                    makefile.write(' {}'.format(upstream_id))
                makefile.write('\n')

                # \t rastervision ...
                command_def = command_dag.get_command_definition(command_id)
                command_config = command_def.command_config
                command_root_uri = command_config.root_uri
                command_basename = 'command-config-{}.json'.format(
                    command_config.split_id)
                command_uri = os.path.join(command_root_uri, command_basename)
                print('Saving command configuration to {}...'.format(
                    command_uri))
                save_json_config(command_config.to_proto(), command_uri)
                run_command = make_command(command_uri, self.tmp_dir)
                makefile.write('\t{}\n\n'.format(run_command))

        process = Popen(['make', '-j', '-f', makefile_name])
        terminate_at_exit(process)
        exitcode = process.wait()
        if exitcode != 0:
            sys.exit(exitcode)
        else:
            return 0
        def run_commands(tmp_dir):
            for command_config in command_dag.get_sorted_commands():
                msg = command_config.to_proto()
                builder = rv._registry.get_command_config_builder(
                    msg.command_type)()
                command_config = builder.from_proto(msg).build()

                command_root_uri = command_config.root_uri
                command_basename = 'command-config-{}.json'.format(
                    command_config.split_id)
                command_uri = os.path.join(command_root_uri, command_basename)
                log.info('Saving command configuration to {}...'.format(
                    command_uri))
                save_json_config(command_config.to_proto(), command_uri)

                command = command_config.create_command()
                command.run(tmp_dir)
    def _run_experiment(self, command_dag):
        """Runs all commands."""

        ids_to_job = {}
        for command_id in command_dag.get_sorted_command_ids():
            command_def = command_dag.get_command_definition(command_id)
            command_config = command_def.command_config
            command_root_uri = command_config.root_uri
            command_basename = 'command-config-{}.json'.format(
                command_config.split_id)
            command_uri = os.path.join(command_root_uri, command_basename)
            print('Saving command configuration to {}...'.format(command_uri))
            save_json_config(command_config.to_proto(), command_uri)

            parent_job_ids = []
            for upstream_id in command_dag.get_upstream_command_ids(
                    command_id):
                if upstream_id not in ids_to_job:
                    cur_command = (command_config.command_type, command_id)
                    u = command_dag.get_command(upstream_id)
                    upstream_command = (u.command_type, upstream_id)
                    raise Exception(
                        '{} command has parent command of {}, '
                        'but does not exist in previous submissions - '
                        'topological sort on command_dag error.'.format(
                            cur_command, upstream_command))
                parent_job_ids.append(ids_to_job[upstream_id])

            run_command = make_command(command_uri, self.tmp_dir)
            job_id = self.submit(command_config.command_type,
                                 command_config.split_id,
                                 command_def.experiment_id,
                                 run_command,
                                 parent_job_ids,
                                 utilizes_gpu=command_config.utilizes_gpu())

            ids_to_job[command_id] = job_id
示例#5
0
 def save_config(self):
     msg = self.to_proto()
     uri = os.path.join(self.root_uri, 'experiments',
                        '{}.json'.format(self.id))
     save_json_config(msg, uri)