Пример #1
0
    def upload_result_project(self, _):
        self.report_step_done(TaskStep.MAIN)
        self._read_verif_status()
        graph_pr_name = sly.get_res_project_name(self.info['graph'])

        no_image_files = not self.download_images

        if self.is_archive is False:
            pr_id = self.data_mgr.upload_project(self.dir_results,
                                                 graph_pr_name,
                                                 no_image_files=no_image_files)
            self.logger.info('PROJECT_CREATED',
                             extra={
                                 'event_type': EventType.PROJECT_CREATED,
                                 'project_id': pr_id
                             })
        else:
            # remove excess fields from json
            root_path, project_name = sly.ProjectFS.split_dir_project(
                self.dir_results)
            project_fs = sly.ProjectFS.from_disk(root_path,
                                                 project_name,
                                                 by_annotations=True)
            for item in project_fs:
                ann_path = item.ann_path
                ann = sly.json_load(ann_path)
                for exc_field in ann_special_fields():
                    ann.pop(exc_field, None)
                sly.json_dump(ann, ann_path)

            self.data_mgr.upload_archive(self.dir_results, graph_pr_name)

        self.report_step_done(TaskStep.UPLOAD)
        return {}
Пример #2
0
    def run(self):
        try:
            self.init_logger()
            self.init_api()
            self.future_log = self.executor_log.submit(
                self.submit_log)  # run log submitting
        except Exception as e:
            # unable to do something another if crashed
            print(e)
            sly.json_dump(
                e, osp.join(constants.AGENT_ROOT_DIR, 'logger_fail.json'))
            os._exit(1)  # ok, documented

        log_extra = {'event_type': EventType.TASK_FINISHED}
        logger_fn = self.logger.info
        try:
            self.report_start()
            self.init_additional()
            self.run_and_wait(self.task_main_func)
        except StopTaskException:
            log_extra = {
                'event_type': EventType.TASK_STOPPED,
                'stopped': 'by_user'
            }
        except Exception:
            log_extra = {
                'event_type': EventType.TASK_CRASHED,
                'error': traceback.format_exc().split('\n')
            }
            logger_fn = self.logger.critical
        logger_fn('TASK_END', extra=log_extra)

        self.logger.info("WAIT_FOR_TASK_LOG")
        self.stop_log_thread()
Пример #3
0
    def download_step(self):
        self.logger.info("DOWNLOAD_DATA")
        sly.json_dump(self.info['config'], self.config_path)

        pr_info = self.info['project']
        project = api_proto.Project(id=pr_info['id'], title=pr_info['title'])
        datasets = [
            api_proto.Dataset(id=ds['id'], title=ds['title'])
            for ds in pr_info['datasets']
        ]
        self.data_mgr.download_project(self.dir_data, project, datasets)

        if self.info.get('nn_model', None) is not None:
            nn_id = self.info['nn_model']['id']
            nn_hash = self.info['nn_model']['hash']
            self.logger.info('DOWNLOAD_NN',
                             extra={
                                 'nn_id': nn_id,
                                 'nn_hash': nn_hash
                             })
            self.data_mgr.download_nn(nn_id, nn_hash, self.dir_model)
        else:
            self.logger.info('Initializing task without source NN.')

        self.report_step_done(TaskStep.DOWNLOAD)
Пример #4
0
    def run_inference(self):
        out_project_fs = copy(self.in_project_fs)
        out_project_fs.root_path = self.helper.paths.results_dir
        out_project_fs.make_dirs()

        inf_feeder = sly.InferenceFeederFactory.create(self.config, self.helper.in_project_meta, self.train_classes)
        out_pr_meta = inf_feeder.out_meta
        out_pr_meta.to_dir(out_project_fs.project_path)

        ia_cnt = out_project_fs.pr_structure.image_cnt
        progress = sly.progress_counter_inference(cnt_imgs=ia_cnt)

        for sample in self.in_project_fs:
            logger.info('Will process image',
                        extra={'dataset_name': sample.ds_name, 'image_name': sample.image_name})
            ann_packed = sly.json_load(sample.ann_path)
            ann = sly.Annotation.from_packed(ann_packed, self.helper.in_project_meta)

            img = cv2.imread(sample.img_path)[:, :, ::-1]
            res_ann = inf_feeder.feed(img, ann, self._infer_on_img)

            out_ann_fpath = out_project_fs.ann_path(sample.ds_name, sample.image_name)
            res_ann_packed = res_ann.pack()
            sly.json_dump(res_ann_packed, out_ann_fpath)

            if self.debug_copy_images:
                out_img_fpath = out_project_fs.img_path(sample.ds_name, sample.image_name)
                sly.ensure_base_path(out_img_fpath)
                shutil.copy(sample.img_path, out_img_fpath)

            progress.iter_done_report()

        sly.report_inference_finished()
Пример #5
0
    def _convert_sample(self, sample_info):
        image_name = sample_info.image_name
        ext = sample_info.ia_data['image_ext']
        src_image_path = join(self.dataset_dir, 'images', image_name + ext)

        sly.copy_file(src_image_path, sample_info.img_path)
        ann = self._get_ann(self.dataset_dir, image_name + ext)
        packed_ann = ann.pack()
        sly.json_dump(packed_ann, sample_info.ann_path)
Пример #6
0
    def _convert_sample(self, sample_info, masks_map):
        image_name = sample_info.image_name
        src_image_path = sample_info.ia_data['image_orig_path']

        src_image = np.array(Image.open(src_image_path))

        image_size_wh = src_image.shape[:2][::-1]
        cv2.imwrite(sample_info.img_path, src_image[:, :, ::-1])
        ann = self._get_ann(image_name, masks_map, image_size_wh)
        packed_ann = ann.pack()
        sly.json_dump(packed_ann, sample_info.ann_path)
Пример #7
0
 def on_verify(self, jlog):
     download_images = jlog.get('output', {}).get('download_images', None)
     is_archive = jlog.get('output', {}).get('is_archive', None)
     if download_images is None or is_archive is None:
         raise ValueError('VERIFY_IS_NONE')
     sly.json_dump(
         {
             'download_images': download_images,
             'is_archive': is_archive
         }, self.verif_status_path)
     return {}
Пример #8
0
    def _convert_sample(self, sample_info):
        log_dct = sample_info._asdict()  # ok, it's documented
        # logger.trace('Will process sample.', extra=log_dct)

        sample_data = sample_info.ia_data
        sly.copy_file(sample_data['src_img_path'],
                      sample_info.img_path)  # img is ready

        ann = self._get_ann(sample_data['segm_path'],
                            sample_data.get('inst_path'), log_dct)
        packed_ann = ann.pack()
        sly.json_dump(packed_ann, sample_info.ann_path)  # ann is ready
Пример #9
0
    def convert(self):
        in_datasets = self._find_in_datasets()

        # map input structure to output
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for ds_name, ds_path in in_datasets:
            img_fnames = sly.ImportImgLister.list_images(ds_path)
            for name_with_ext in img_fnames:
                img_name, img_ext = osp.splitext(name_with_ext)
                src_img_path = osp.join(ds_path, name_with_ext)
                dt = {
                    'src_img_path': src_img_path,
                    'image_ext': img_ext,
                }
                out_pr.add_item(ds_name, img_name, dt)
            logger.info(
                'Found source dataset with raw images: "{}", {} sample(s).'.
                format(ds_name, len(img_fnames)))

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()  # empty
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            sample_data = sample_info.ia_data
            src_img_path = sample_data['src_img_path']
            sly.copy_file(src_img_path, sample_info.img_path)  # img is ready

            image = Image.open(sample_info.img_path)
            exif_data = pyexiv2.metadata.ImageMetadata(sample_info.img_path)
            exif_data.read()

            if exif_data.get_orientation() != 1:
                logger.debug('Image with flip/rot EXIF',
                             extra={
                                 'orientation': exif_data.get_orientation(),
                                 'image_path': sample_info.img_path
                             })
                image = sly.image_transpose_exif(image)
                image.save(sample_info.img_path)
                exif_data['Exif.Image.Orientation'] = pyexiv2.ExifTag(
                    'Exif.Image.Orientation', 1)
                exif_data.modified = True
                exif_data.write()

            imsize_wh = image.size
            ann = sly.Annotation.new_with_objects(imsize_wh, [])
            sly.json_dump(ann.pack(), sample_info.ann_path)  # ann is ready
            progress.iter_done_report()
Пример #10
0
    def _convert_sample(self, sample_info):
        sample_data = sample_info.ia_data

        try:
            ann = self._load_citysc_annotation(sample_data['orig_ann_path'])
            ann['tags'].append(sample_data['tag_name'])
            packed_ann = ann.pack()
        except Exception:
            raise AnnConvException()  # ok, may continue work with another sample

        self.tags.add(sample_data['tag_name'])
        sly.json_dump(packed_ann, sample_info.ann_path)  # ann is ready
        sly.copy_file(sample_data['orig_img_path'], sample_info.img_path)  # img is ready
Пример #11
0
    def dump_schemas(output_path):
        output_path = join(output_path, 'schemas')
        shutil.rmtree(output_path)
        layers_output_path = join(output_path, 'layers')
        sly.mkdir(layers_output_path)
        global_schema = {'definitions': deepcopy(Layer.base_params['definitions'])}
        global_schema['definitions']['layers'] = dict()
        global_schema['items'] = {'anyOf': []}
        #global_schema['items']['minItems'] = 1
        #global_schema['items']['maxItems'] = 1

        for action, cls in Layer.actions_mapping.items():
            layer_schema = deepcopy(cls.params)
            sly.json_dump(layer_schema, join(layers_output_path, '%s.json' % (action)), indent=4)
            del layer_schema['definitions']
            global_schema['definitions']['layers'][action] = layer_schema
            global_schema['items']['anyOf'].append({'$ref': '#/definitions/layers/%s' % action})

        sly.json_dump(global_schema, join(output_path, 'schema.json'), indent=4)
Пример #12
0
    def download_step(self):
        if self.info.get('nn_model', None) is None:
            self.logger.critical('TASK_NN_EMPTY')
            raise ValueError('TASK_NN_EMPTY')

        nn_id = self.info['nn_model']['id']
        nn_hash = self.info['nn_model']['hash']
        self.logger.info('DOWNLOAD_NN', extra={'nn_id': nn_id, 'nn_hash': nn_hash})
        self.data_mgr.download_nn(nn_id, nn_hash, self.dir_model)

        self.logger.info("DOWNLOAD_DATA")
        sly.json_dump(self.info['config'], self.config_path)

        pr_info = self.info['project']
        project = api_proto.Project(id=pr_info['id'], title=pr_info['title'])
        datasets = [api_proto.Dataset(id=ds['id'], title=ds['title']) for ds in pr_info['datasets']]
        self.data_mgr.download_project(self.dir_data, project, datasets)

        self.report_step_done(TaskStep.DOWNLOAD)
Пример #13
0
    def download_step(self):
        if self.info.get('nn_model', None) is None:
            self.logger.critical('TASK_NN_EMPTY')
            raise ValueError('TASK_NN_EMPTY')

        nn_id = self.info['nn_model']['id']
        nn_hash = self.info['nn_model']['hash']
        self.logger.info('DOWNLOAD_NN',
                         extra={
                             'nn_id': nn_id,
                             'nn_hash': nn_hash
                         })
        self.data_mgr.download_nn(nn_id, nn_hash, self.dir_model)

        out_cfg = {
            **self.info,
            'connection': {
                'server_address': constants.SERVER_ADDRESS,
                'token': constants.TOKEN,
                'task_id': str(self.info['task_id']),
            },
        }
        sly.json_dump(out_cfg, self.config_path)
        self.report_step_done(TaskStep.DOWNLOAD)
Пример #14
0
def main():
    # Please note that auxiliary methods from sly (supervisely_lib) use supervisely_lib.logger to format output.
    # So don't replace formatters or handlers of the logger.
    # One may use other loggers or simple prints for other output, but it's recommended to use supervisely_lib.logger.
    logger.info('Hello ML world')
    print('Glad to see u')

    # TaskHelperTrain contains almost all needed to run inference as Supervisely task,
    # including task settings and paths to data and model.
    task_helper = sly.TaskHelperInference()

    # All settings and parameters are passed to task in json file.
    # Content of the file is entirely dependent on model implementation.
    inference_settings = task_helper.task_settings
    logger.info('Task settings are read',
                extra={'task_settings': inference_settings})

    # Let's imitate loading training model weights. Task acquires directory with the weights.
    # And content of the directory is entirely dependent on model implementation.
    model_dir = task_helper.paths.model_dir
    model = load_fake_model(model_dir)
    logger.info('Model weights are loaded', extra={'model_dir': model_dir})

    # We are going to read input project with data for inference.
    project_meta = task_helper.in_project_meta  # Project meta contains list of project classes.
    project_dir = task_helper.paths.project_dir
    project_fs = sly.ProjectFS.from_disk_dir_project(project_dir)
    # ProjectFS enlists all samples (image/annotation pairs) in input project.

    # We are to write inference results as sly project with same structure into provided results dir.
    # There is no need to save images, only annotations and project meta are required.
    results_dir = task_helper.paths.results_dir
    out_project_fs = sly.ProjectFS(results_dir, project_fs.pr_structure)

    # It's necessary to write project meta (with list of classes) for output project.
    out_meta = sly.ProjectMeta([{
        'title': 'hat',
        'shape': 'point',
        'color': '#FF0000'
    }])  # create meta
    out_meta.to_dir(out_project_fs.project_path)  # and save

    # We are to report progress of task over sly.ProgressCounter if we want to observe the progress in web panel.
    # In fact one task may report progress for some sequential (not nested) subtasks,
    # but here we will report inference progress only.
    ia_cnt = out_project_fs.pr_structure.image_cnt
    progress = sly.progress_counter_inference(cnt_imgs=ia_cnt)

    # Iterating over samples (image/annotation pairs) in input project.
    for item_descr in project_fs:
        logger.info('Processing input sample',
                    extra={
                        'dataset': item_descr.ds_name,
                        'image_name': item_descr.image_name
                    })

        # Open some image...
        img = cv2.imread(item_descr.img_path)
        logger.info('Read image from input project',
                    extra={
                        'width': img.shape[1],
                        'height': img.shape[0]
                    })

        # And read corresponding annotation...
        ann_packed = sly.json_load(item_descr.ann_path)
        ann = sly.Annotation.from_packed(ann_packed, project_meta)
        logger.info('Read annotation from input project',
                    extra={'tags': ann['tags']})

        logger.info('Some forward pass...')
        time.sleep(1)

        # Let's imitate inference output.
        # We are to save results as sly Figures in annotation.
        ann['objects'] = [sly.FigurePoint.from_pt('hat', (800, 800))
                          ]  # imagine that our model found the point
        out_ann_path = out_project_fs.ann_path(item_descr.ds_name,
                                               item_descr.image_name)
        sly.ensure_base_path(out_ann_path)  # create intermediate directories
        sly.json_dump(ann.pack(), out_ann_path)  # and save annotation
        # Note that there is no need to save image.

        progress.iter_done_report(
        )  # call it after every iteration to report progress

    # It's necessary to report that the inference task is finished.
    sly.report_inference_finished()

    # Thank you for your patience.
    logger.info('Applying finished.')
Пример #15
0
 def init_additional(self):
     super().init_additional()
     sly.mkdir(self.dir_data)
     sly.mkdir(self.dir_results)
     sly.json_dump(self.info['graph'], self.graph_path)
Пример #16
0
 def download_step(self):
     self.logger.info("DOWNLOAD_DATA")
     sly.json_dump(self.human_config, self.config_path)
     self.data_mgr.download_import_files(self.info['task_id'], self.dir_data)
     self.report_step_done(TaskStep.DOWNLOAD)
Пример #17
0
 def save(self, config):
     sly.json_dump(config, self.train_config_fpath)
Пример #18
0
def main():
    logger.info('Hello world.')

    # It isn't necessary, but let's suppose that our data will be stored as for Supervisely task:
    # input in '/sly_task_data/data` and results in '/sly_task_data/results'.
    # So TaskPaths provides the paths.
    task_paths = sly.TaskPaths()

    in_pr_dir = task_paths.project_dir  # the paths includes project name

    in_pr_meta = sly.ProjectMeta.from_dir(in_pr_dir)
    # Now we've read meta of input project.
    logger.info('Input project meta: {} class(es).'.format(
        len(in_pr_meta.classes)))

    in_pr_fs = sly.ProjectFS.from_disk(
        *sly.ProjectFS.split_dir_project(in_pr_dir))
    # Now we've read project structure.
    logger.info(
        'Input project: "{}" contains {} dataset(s) and {} image(s).'.format(
            in_pr_fs.pr_structure.name, len(in_pr_fs.pr_structure.datasets),
            in_pr_fs.image_cnt))

    # It's convenient to create output project structure and store source file paths in ia_data.
    out_pr_structure = sly.ProjectStructure(
        'my_new_project')  # rename project... just for fun
    for item_descr in in_pr_fs:  # iterate over input project
        new_ia_data = {
            'src_ann_path': item_descr.ann_path,
            'src_img_path': item_descr.img_path,
            **item_descr.ia_data  # contains 'image_ext' which is required to write images
        }
        out_pr_structure.add_item(item_descr.ds_name, item_descr.image_name,
                                  new_ia_data)
    # ProjectFS will provide out file paths
    out_pr_fs = sly.ProjectFS(task_paths.results_dir, out_pr_structure)

    # We will add the rectangle to each annotation.
    new_class_title = 'new-region'
    rect_to_add = sly.Rect(left=20, top=20, right=50, bottom=100)

    # Ok, start processing.
    out_pr_fs.make_dirs()  # create all directories required for writing
    for item_descr in out_pr_fs:  # iterate over output project
        logger.info('Processing sample',
                    extra={
                        'dataset': item_descr.ds_name,
                        'image_name': item_descr.image_name
                    })

        # Copy image unchanged.
        sly.copy_file(item_descr.ia_data['src_img_path'], item_descr.img_path)

        # Read annotation.
        ann_packed = sly.json_load(item_descr.ia_data['src_ann_path'])
        ann = sly.Annotation.from_packed(ann_packed, in_pr_meta)

        # Add new figure to the annotation.
        # Method to construct figures returns iterable of new figures.
        # (e.g., line cropped with image bounds may produce some lines), but here we'll get not more than one figure
        # ...or no figures if image is less than 20x20.
        new_figures = sly.FigureRectangle.from_rect(new_class_title,
                                                    ann.image_size_wh,
                                                    rect_to_add)
        ann['objects'].extend(new_figures)

        # Save annotation.
        sly.json_dump(ann.pack(), item_descr.ann_path)

    # OK, and don't forget to create and save output project meta.
    # We'll save given data and add new class with shape "rectangle".
    out_pr_meta = deepcopy(in_pr_meta)
    out_pr_meta.classes.add({
        'title': new_class_title,
        'shape': 'rectangle',
        'color': '#FFFF00'
    })
    # Then store the meta.
    out_pr_meta.to_dir(out_pr_fs.project_path)

    logger.info('Done.')
Пример #19
0
 def download_step(self):
     self.logger.info("DOWNLOAD_DATA")
     sly.json_dump(self.human_config, self.config_path)
     copy_tree(constants.AGENT_IMPORT_DIR, self.dir_data)
     self.report_step_done(TaskStep.DOWNLOAD)