def from_json(cls, data): ''' The function from_json convert ProjectMeta from json format to ProjectMeta class object. Generate exception error if all project tags not in a single collection :param data: input ProjectMeta in json format :return: ProjectMeta class object ''' tag_metas_json = data.get(ProjectMetaJsonFields.TAGS, []) img_tag_metas_json = data.get(ProjectMetaJsonFields.IMG_TAGS, []) obj_tag_metas_json = data.get(ProjectMetaJsonFields.OBJ_TAGS, []) project_type = data.get(ProjectMetaJsonFields.PROJECT_TYPE, None) if len(tag_metas_json) > 0: # New format - all project tags in a single collection. if any(len(x) > 0 for x in [img_tag_metas_json, obj_tag_metas_json]): raise ValueError( 'Project meta JSON contains both the {!r} section (current format merged tags for all of ' 'the project) and {!r} or {!r} sections (legacy format with separate collections for images ' 'and labeled objects). Either new format only or legacy format only are supported, but not a ' 'mix.'.format( ProjectMetaJsonFields.TAGS, ProjectMetaJsonFields.IMG_TAGS, ProjectMetaJsonFields.OBJ_TAGS)) tag_metas = TagMetaCollection.from_json(tag_metas_json) else: img_tag_metas = TagMetaCollection.from_json(img_tag_metas_json) obj_tag_metas = TagMetaCollection.from_json(obj_tag_metas_json) tag_metas = _merge_img_obj_tag_metas(img_tag_metas, obj_tag_metas) return cls(obj_classes=ObjClassCollection.from_json(data[ProjectMetaJsonFields.OBJ_CLASSES]), tag_metas=tag_metas, project_type=project_type)
def _load_train_config(self): self._load_raw_model_config_json() self.class_title_to_idx = self.train_config[self.class_title_to_idx_key] logger.info('Read model internal class mapping', extra={'class_mapping': self.class_title_to_idx}) train_classes = ObjClassCollection.from_json(self.train_config[self.train_classes_key]) logger.info('Read model out classes', extra={'classes': train_classes.to_json()}) # TODO: Factor out meta constructing from _load_train_config method. self._model_out_meta = ProjectMeta(obj_classes=train_classes, tag_metas=self._model_out_tags()) # Make a separate [index] --> [class] map that excludes the 'special' classes that should not be in the` # final output. self.out_class_mapping = {idx: train_classes.get(title) for title, idx in self.class_title_to_idx.items() if train_classes.has_key(title)}
def from_json(cls, data, project_meta): ''' The function from_json convert annotation from json format to Annotation class object. If one of the labels of annotation in json format cannot be convert to Label class object it generate exception error. :param data: input annotation in json format :param project_meta: ProjectMeta class object :return: Annotation class object ''' img_size_dict = data[AnnotationJsonFields.IMG_SIZE] img_height = img_size_dict[AnnotationJsonFields.IMG_SIZE_HEIGHT] img_width = img_size_dict[AnnotationJsonFields.IMG_SIZE_WIDTH] img_size = (img_height, img_width) try: labels = [ Label.from_json(label_json, project_meta) for label_json in data[AnnotationJsonFields.LABELS] ] except Exception: logger.fatal( 'Failed to deserialize annotation from JSON format. One of the Label objects could not be ' 'deserialized') raise custom_data = data.get(AnnotationJsonFields.CUSTOM_DATA, {}) prob_labels = None if AnnotationJsonFields.PROBABILITY_LABELS in custom_data and \ AnnotationJsonFields.PROBABILITY_CLASSES in custom_data: prob_classes = ObjClassCollection.from_json( custom_data[AnnotationJsonFields.PROBABILITY_CLASSES]) # @TODO: tony, maybe link with project meta (add probability classes???) prob_project_meta = ProjectMeta(obj_classes=prob_classes) prob_labels = [ Label.from_json(label_json, prob_project_meta) for label_json in custom_data[AnnotationJsonFields.PROBABILITY_LABELS] ] custom_data.pop(AnnotationJsonFields.PROBABILITY_CLASSES) custom_data.pop(AnnotationJsonFields.PROBABILITY_LABELS) return cls(img_size=img_size, labels=labels, img_tags=TagCollection.from_json( data[AnnotationJsonFields.IMG_TAGS], project_meta.tag_metas), img_description=data.get( AnnotationJsonFields.IMG_DESCRIPTION, ""), pixelwise_scores_labels=prob_labels, custom_data=custom_data)
def from_json(cls, data): return cls( ObjClassCollection.from_json( data[ProjectMetaJsonFields.OBJ_CLASSES]), TagMetaCollection.from_json(data[ProjectMetaJsonFields.IMG_TAGS]), TagMetaCollection.from_json(data[ProjectMetaJsonFields.OBJ_TAGS]))