Justobe opened a new issue #17662: [NaN Bug] MXNET backend outputs NAN when 
using the built-in DenseNet121 model of keras for image classification task.
URL: https://github.com/apache/incubator-mxnet/issues/17662
 
 
   ## Description
   I encountered a problem when I used keras and backend mxnet for image 
classification. I loaded the model of densenet121 provided by keras to predict 
a picture. But I got NaN results. When I use tensorflow as the backend, I can 
get normal numerical results. The code is shown below.
   
   ### Error Message
   Output of MXNET:
   
   
![image-20200223182806807](https://user-images.githubusercontent.com/15032308/75110721-9ab17800-566c-11ea-8a07-e7487a8d7b16.png)
   
   Output of  TensorFlow:
   
   
![image-20200223182921364](https://user-images.githubusercontent.com/15032308/75110722-9edd9580-566c-11ea-9320-e97a62a8790b.png)
   
   
   ## To Reproduce
   The code is really simple. 
   
   ```python
   import os
   import sys
   import pickle
   import numpy as np
   from PIL import Image
   bk = sys.argv[1]
   os.environ['KERAS_BACKEND'] = bk
   os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
   os.environ["CUDA_VISIBLE_DEVICES"] = "0"
   
   import keras
   from keras import backend as K
   print("Using backend :{}".format(K.backend()))
   
   def image_resize(x, shape):
       x_return = []
       for x_test in x:
           tmp = np.copy(x_test)
           img = Image.fromarray(tmp.astype('uint8')).convert('RGB')
           img = img.resize(shape, Image.ANTIALIAS)
           x_return.append(np.array(img))
       return np.array(x_return)
   
   base_model = keras.applications.densenet.DenseNet121(weights='imagenet', 
include_top=True, input_shape=(224,224,3))
   base_model.summary()
   print("Done")
   adam = keras.optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
   base_model.compile(loss='categorical_crossentropy', optimizer=adam, 
metrics=['accuracy'])
   
   imagename = '183_bucket.png'
   img = Image.open(imagename)
   img = img.resize((224,224), Image.ANTIALIAS)
   x_test = np.array(img)
   select_data = np.expand_dims(x_test, axis=0)
   
   prediction = base_model.predict(select_data)
   print(prediction)
   ```
   
   
[nan-densenet.zip](https://github.com/apache/incubator-mxnet/files/4240956/nan-densenet.zip)
   
   
   ### Steps to reproduce
   1. Download the script
   
   2. Run the script by following commands.
   
   > python get_prediction.py tensorflow
   
   or
   
   > python get_prediction.py mxnet
   
   ## What have you tried to solve it?
   
   I encountered this problem on mxnet-cu101(Version 1.5.1.post0). When I 
upgraded mxnet to 1.6, the bug still exists.
   
   ## Environment
   
   - MXNet: mxnet-cu101==1.5.1.post0
   - keras-mxnet: 2.2.4.2
   - CUDA: 10.1
   
   You can use the following command to configure the environment
   
   ```shell
   pip install keras-mxnet
   pip install mxnet-cu101
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to