gzuidhof / nn-transfer

Convert trained PyTorch models to Keras, and the other way around
MIT License
226 stars 39 forks source link

image_data_format('channels_last') #11

Open birdyalex opened 6 years ago

birdyalex commented 6 years ago

Hi Guido,

it looks like all Keras->Pytorch examples provided with the package are for the case of "channels_first" image data format. I have been struggling to make it work for the case of "channels_last" format (default tensorflow behaviour). But I can't figure out how to handle one specific case.

I modified a little bit the example notebook and put it here for clarity (https://github.com/birdyalex/nn-transfer/blob/master/example_channels_last.ipynb).

The Keras->Pytorch conversion works fine if the number of out-channels before flattening equals 1 (the latest block of conv/pool just before FC-layers). But when one increases the number of channels to 2+ something starts to go wrong. I included a variable NUM_OUT_CHANNEL_BEFORE_FLATTENING in the begging of the notebook which controls how many channels should the network have.

It looks like the concatenation-by-channel pattern (Keras' Flatten() vs. Pytorch .view()) differs between these two frameworks. Do you have any idea what can cause such a behaviour and maybe a way how to handle it?

KLH1472 commented 5 years ago

Also want to know how to deal with the "channels_last" format~