allanzelener / YAD2K

YAD2K: Yet Another Darknet 2 Keras
Other
2.71k stars 877 forks source link

yolov2-tiny weights not loading properly. #131

Open jamesonthecrow opened 6 years ago

jamesonthecrow commented 6 years ago

Using the yad2k.py script as is with the yolov2-tiny.cfg and yolov2-tiny.weights results in all nan predictions in Keras. I tracked the problem down to the fact that the batch norm layers end up with negative values for the rolling_variance parameters in the first filter position. This should never happen (variance should be >0).

I traced things through the original Darknet repo and the issue appears to be an extra read when loading the weights header. In the original Darknet code, there is an if statement that either reads the last value of the header as sizeof(int) or sizeof(int) bytes depending on the header. This works out to 8 or 4 bytes, respectively. The python code in YAD2k, however, reads 4 bytes no matter what. The most recent version of the yolov2-tiny.weights should read 8 bytes in the last spot causing an alignment issue when you try to convert things with the yad2k.py script. You can remedy the issue by changing this line to read:

weights_header = np.ndarray(shape=(4, ), dtype='int32', buffer=weights_file.read(20))