What does major, minor, revison, transpose means in loading weights in tensorflow?

Viewed 242

Loading weights of YOLOv3 Object-detection-with-yolov3-in-keras

Can somebody please tell me what is happening here in loading weights and the meaning of transpose statement

with open(weight_file, 'rb') as w_f:
        major,  = struct.unpack('i', w_f.read(4))
        minor,  = struct.unpack('i', w_f.read(4))
        revision, = struct.unpack('i', w_f.read(4))
        if (major*10 + minor) >= 2 and major < 1000 and minor < 1000:
            w_f.read(8)
        else:
            w_f.read(4)
        transpose = (major > 1000) or (minor > 1000)
        binary = w_f.read() 
1 Answers

It is parsing the weights file, which is saved in the format used by the Darknet framework (source code). This format is not formally specified anywhere outside the actual framework code, as far as I can tell. The relevant parts are in the file src/parser.c, functions save_weights_upto and load_weights_upto. As you can probably tell, that piece of Python code seems to be a direct translation of the corresponding C code.

It would seem that the file format begins with three 32-bit integers (although the C code uses sizeof(int), which is not necessarily 32 bits, but whatever) corresponding to the major, minor and revision values of the file format version. Then comes a seen attribute for the network that, depending on the file version, can be int or size_t, usually meaning 32 or 64 bits. Then, transpose is a boolean depending on whether the major and minor versions are greater than 1000. The usage of the major, minor and revision fields seems to be, to say the least, unorthodox.

In the Python code, neither seen or transpose are used. transpose is read into a variable, but then that variable is not used. In theory it should, though. I imagine the code works for the file linked in the post, but if transpose happened to be true, the loading of the weights should be different, as you can see in load_connected_weights.

In any case, all of that are very specific concerns related to the file format of Darknet used in that example. Unless you want to explicitly be compatible with several different weight files from Darknet models, you probably don't need to worry too much about that code, as long as it is working for that case.

Related