{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/MMdnn/issues/318","repository_url":"https://api.github.com/repos/Microsoft/MMdnn","labels_url":"https://api.github.com/repos/Microsoft/MMdnn/issues/318/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/MMdnn/issues/318/comments","events_url":"https://api.github.com/repos/Microsoft/MMdnn/issues/318/events","html_url":"https://github.com/Microsoft/MMdnn/issues/318","id":342013788,"node_id":"MDU6SXNzdWUzNDIwMTM3ODg=","number":318,"title":"Converted PyTorch to Keras without error, but inference results are different","user":{"login":"netw0rkf10w","id":8569515,"node_id":"MDQ6VXNlcjg1Njk1MTU=","avatar_url":"https://avatars1.githubusercontent.com/u/8569515?v=4","gravatar_id":"","url":"https://api.github.com/users/netw0rkf10w","html_url":"https://github.com/netw0rkf10w","followers_url":"https://api.github.com/users/netw0rkf10w/followers","following_url":"https://api.github.com/users/netw0rkf10w/following{/other_user}","gists_url":"https://api.github.com/users/netw0rkf10w/gists{/gist_id}","starred_url":"https://api.github.com/users/netw0rkf10w/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netw0rkf10w/subscriptions","organizations_url":"https://api.github.com/users/netw0rkf10w/orgs","repos_url":"https://api.github.com/users/netw0rkf10w/repos","events_url":"https://api.github.com/users/netw0rkf10w/events{/privacy}","received_events_url":"https://api.github.com/users/netw0rkf10w/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-07-17T17:28:27Z","updated_at":"2018-07-19T10:18:13Z","closed_at":null,"author_association":"NONE","body":"Hello,\r\n\r\nI have converted a ResNet-152 model that I trained on a custom dataset from PyTorch to Keras, but it seems the conversion was not successful because when testing on an image, the output of the two networks are different.\r\n\r\nHere's how I performed the conversion:\r\n\r\n1. Convert PyTorch to IR:\r\n`mmtoir -f pytorch -d out --inputShape 3 224 224 -n resnet152_pytorch.pth`\r\nThree files are produced: `out.json`, `out.pb` and `out.npy`.\r\n\r\n2. Convert IR to Keras code snippet:\r\n`mmtocode -f keras --IRModelPath out.pb --dstModelPath resnet152_keras.py`\r\n\r\n3. Save the weights to Keras .h5:\r\n`python -m mmdnn.conversion.examples.keras.imagenet_test -n resnet152_keras.py -w out.npy --dump resnet152_keras.h5`\r\n\r\nAnd here's how I tested them:\r\n\r\n1. First read an image, resize it and convert to float format (it's just for testing so we don't need proper pre-processing):\r\n    \r\n   ```\r\n    image = cv2.imread(image_path, 1)\r\n    image = cv2.resize(image, (224, 224), interpolation = cv2.INTER_CUBIC)\r\n    image = image.astype(float)\r\n    ```\r\n   \r\n2. Load the model and do inference for PyTorch:\r\n\r\n        device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\r\n        model = torch.load('resnet152_pytorch.pth')\r\n        image_cwh = np.rollaxis(image, 2, 0) # convert the image to CWH format\r\n        im_tensor = torch.from_numpy(image_cwh)\r\n        im_tensor.unsqueeze_(0) # add batch dimension\r\n\r\n        # send the input the and the model to GPU device\r\n        im_tensor = im_tensor.to(device)\r\n        model = model.to(device)\r\n\r\n        # prediction\r\n        output = model(im_tensor)\r\n        print(output)\r\n\r\n3. Load the model and do inference for Keras:\r\n      \r\n        model = keras.models.load_model('resnet152_keras.h5')\r\n        output = model.predict_on_batch(np.expand_dims(image, axis=0))\r\n        print(output)\r\n\r\nAnd this is what I obtained:    \r\n\r\n> Pytorch: tensor([[-0.7422, -0.9134, -0.5829,  ..., -0.0954, -1.2378,  0.0789]], device='cuda:0')\r\n> Keras: [[ -52.964676 -101.15237   -71.54291  ...   20.734167  -69.53998  -58.768787]]\r\n\r\nThank you very much in advance for your help!"},"comment":{"url":"https://api.github.com/repos/Microsoft/MMdnn/issues/comments/406228333","html_url":"https://github.com/Microsoft/MMdnn/issues/318#issuecomment-406228333","issue_url":"https://api.github.com/repos/Microsoft/MMdnn/issues/318","id":406228333,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNjIyODMzMw==","user":{"login":"netw0rkf10w","id":8569515,"node_id":"MDQ6VXNlcjg1Njk1MTU=","avatar_url":"https://avatars1.githubusercontent.com/u/8569515?v=4","gravatar_id":"","url":"https://api.github.com/users/netw0rkf10w","html_url":"https://github.com/netw0rkf10w","followers_url":"https://api.github.com/users/netw0rkf10w/followers","following_url":"https://api.github.com/users/netw0rkf10w/following{/other_user}","gists_url":"https://api.github.com/users/netw0rkf10w/gists{/gist_id}","starred_url":"https://api.github.com/users/netw0rkf10w/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netw0rkf10w/subscriptions","organizations_url":"https://api.github.com/users/netw0rkf10w/orgs","repos_url":"https://api.github.com/users/netw0rkf10w/repos","events_url":"https://api.github.com/users/netw0rkf10w/events{/privacy}","received_events_url":"https://api.github.com/users/netw0rkf10w/received_events","type":"User","site_admin":false},"created_at":"2018-07-19T10:18:13Z","updated_at":"2018-07-19T10:18:13Z","author_association":"NONE","body":"@kitstar Sure. Here it is: https://drive.google.com/file/d/1Z393bCEKN1kMKI_x_nuccurIWUp2_9rg/view?usp=sharing\r\n\r\n> offical pytorch resnet 152 -> keras is tested\r\n\r\nYou mean we can convert PyTorch ResNet152 to Keras using a one-line command? Like this: `mmconvert -sf pytorch -in resnet152_pytorch.pth -df keras -om resnet152_keras.h5`?"}}
{"actor":{"display_login":"netw0rkf10w"}}