-
Notifications
You must be signed in to change notification settings - Fork 965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cntk batch normalisation layers problem #396
Comments
Hi @jeandebleau , thanks! I tried this code snippet ,but I only got [nan nan nan ... nan nan nan] |
Hi, yes I tested the code and manage to make it work as intended. The problem is that with the current conversion, you will lose the batch normalisation layers. Let me give you more detail. I first load a model from keras and write its weights and architecture:
I convert it to an intermediate representation: and create the file kit_imagenet.py python -m mmdnn.conversion._script.IRToCode --dstFramework cntk --IRModelPath kit_imagenet.pb --dstModelPath kit_imagenet.py --IRWeightPath kit_imagenet.npy In the generated kit_imagenet.py file you will find this piece of code:
To get the cntk model, you finally call: If you want the BN layers to be trainable, you need to modify the code to insert a cntk batch normalisation layer. Otherwise the model will not have bn layers and retraining it could be slow or impossible. |
Hi @jeandebleau , yes! I know what is your meaning, after conversion I also can get xxx.dnn cntk model file. But I only got [nan nan nan ... nan nan nan] as output of the converted cntk model file when I test the accuracy of theis conversion and input a image. You can refer to this file to get the result and intermediate result to check the accuracy. It's my result: your code :
('n15075141 toilet tissue, toilet paper, bathroom tissue', 999, nan)
('n02319095 sea urchin', 328, nan)
('n02395406 hog, pig, grunter, squealer, Sus scrofa', 341, nan)
('n02391049 zebra', 340, nan)
('n02389026 sorrel', 339, nan)
MMdnn:
('n02051845 pelican', 144, 0.77398205)
('n01616318 vulture', 23, 0.10650859)
('n01608432 kite', 21, 0.08107732)
('n02058221 albatross, mollymawk', 146, 0.0092755165)
('n03388043 fountain', 562, 0.008964523) To search the reason, I read the code in CNTK layers. And I find: # TODO: map_rank is broken. We should specify the #slowest-changing axes. E.g. 1 would work for images and vectors. Requires C++ change.
def BatchNormalization(map_rank=default_override_or(None), # if given then normalize only over this many dimensions. E.g. pass 1 to tie all (h,w) in a (C, H, W)-shaped input
init_scale=1,
normalization_time_constant=default_override_or(5000), blend_time_constant=0,
epsilon=default_override_or(0.00001), use_cntk_engine=default_override_or(False),
disable_regularization=default_override_or(False),
name=''): MMdnn IR is in channel last (eg. [, 112, 112, 64]) and we also emit the CNTK model code file in channel last . So it cannot assign the mean and variance using your code, or you will assign an mean array with shape[64,] into a bn.aggregate_mean with shape[112,] If you want to finetune your CNTK model and also use the converted weight (.npy) , There are two ways:
Please ping us if you have any problem ! Thanks! |
thanks for the answer, indeed I also change the code to have a channel first representation ; which also involves modifying other part of the generated kit_imagenet.py. |
I converted successfully several keras pretrained models to cntk.
I used the kit_imagenet to do that.
My issue is that I would like to use these models for transfer learning. I noticed that the batch normalisation layers from keras are not converted to a real batch normalisation layer in cntk. This makes the model almost not usable for transfer learning, at least if one wants to retrain the BN layers.
this is the code from the kit_imagenet.py:
it should be replaced by something like that:
is it right ?
The text was updated successfully, but these errors were encountered: