How to effectively read ".yml" files as structures?

The ".yml" file is also a storage structure similar to the ".xml" file. How do I read the content inside and save it as a struct type structure or dictionary type(introduced in R2022b)? Similar to the new function readstruct function in 2020b.
Similarly, whether format files such as'.json' can be read in matlab with a unified function, such as the readstruct function, the specified'FileType' can be {'.xml','.json','.yml' ,'.xml'} One or more than one kind of'.xml', although there are functions such as jsondecode and xmlread.
My ".yml" file is similar to the following format(in attachments):
#Config File example
save_dir: workspace/nanodet_m
model:
arch:
name: GFL
backbone:
name: ShuffleNetV2
model_size: 1.0x
out_stages: [2,3,4]
activation: LeakyReLU
fpn:
name: PAN
in_channels: [116, 232, 464]
out_channels: 96
start_level: 0
num_outs: 3
head:
name: NanoDetHead
num_classes: 80
input_channel: 96
feat_channels: 96
stacked_convs: 2
share_cls_reg: True
octave_base_scale: 5
scales_per_octave: 1
strides: [8, 16, 32]
reg_max: 7
norm_cfg:
type: BN
loss:
loss_qfl:
name: QualityFocalLoss
use_sigmoid: True
beta: 2.0
loss_weight: 1.0
loss_dfl:
name: DistributionFocalLoss
loss_weight: 0.25
loss_bbox:
name: GIoULoss
loss_weight: 2.0
data:
train:
name: coco
img_path: coco/train2017
ann_path: coco/annotations/instances_train2017.json
input_size: [320,320] #[w,h]
keep_ratio: True

2 Comments

I found a third-party solution, but I need to compile opencv with mex, cv.FileStorage reads and writes various formats above, but I still feel that it is not what I really want. I very much hope that the official version will strengthen the readstruct function in the future!

Sign in to comment.

 Accepted Answer

Hi, I wrote a wrapper for SnakeYAML with which you can load and dump YAML:
Requires R2019b or newer.

12 Comments

@Martin Koch , I tried your library, but still can't import and export my yml format, like this one, see attachment.
The exported matrix has parentheses "[]" in each row, which of course is not what I expected.
@Walter Roberson I know, I meant that it is a large matrix and should not be made up of small matrices enclosed in one bracket per row, like the matrix in the first attachment I posted.
I have looked at the YAML specifications. In order to have more than one numeric entry per line you need a "flow sequence" which uses []
If you do not want [] to be present then you need to switch to one value per line, and use indentation to indicate when a row ends.
YAML does not have the concept of 2d arrays. The most you can do is to have a name which is a sequence of sequences. Which is what the [] syntax you are seeing does.
I believe that you are incorrect in expecting more than one numeric value to work on a line without using []
@Walter Roberson So the question is, no matter how yaml is defined in wikipedia, then how do I write the code to reach the formal type of the 2 attachments I gave? I tried to specify more parameters, but all failed.
As a user, the most important thing is how to make it easier to use the read/write function to meet your requirements, followed by how the original author wrote the library/refer to the Wikipedia definition, etc.
Did you experiment with passing "block" as the style option?
yaml.dumpFile('test.yml',data,'block')
This is part of the yml file exported with the "block" parameter, all variables appear to be formally "one column", where the rows are distinguished by "-", which of course does not formally match the first linked attachment I gave
But to be sure: my original original question from the year before was valid for import and export using the package yml format written by the current author@Martin Koch
I think then the answer is that the package is not suitable for your purposes.
@Walter Roberson,Yes, this package does not currently suitable for the yml import and export attached in my comment.
But on the other hand, the attached yml format is produced by the opencv library, not our common yml format, is unique to opencv, so to achieve the same function in matlab, can only package the opencv filestorage class interface for matlab to use, as you said in your comments the year before the third-party cvyamlparser library
Hi, here is a copy of my answer from the GitHub issue:
there are two problems:
  • The underlying SnakeYAML is written for YAML 1.1 for which the first line %YAML:1.0 is not valid (%YAML 1.0 without : would be acceptable). I will update the documentation to mention YAML 1.1.
  • The custom opencv types are not supported.
You can build a workaround like this:
  • Read file into string with fileread
  • Delete all text occurrences of %YAML:1.0 and !!opencv-matrix (e.g. with strrep)
  • Parse modified string with yaml.load
  • Manually convert the content of the data field to a 2D array (e.g. with reshape)
To write YAML files in the same format you have to reverse the above process. However, it will not be formatted such that every row is in a new line.

Sign in to comment.

More Answers (1)

2 Comments

Thank you, this may be a better solution before the official unified read interface function (readstruct).
Just for others to know. The function from https://github.com/llerussell/ReadYAML does not properly deals with multi level Yaml files.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!