From 334fb51f3c1bc079372ffc203666cd1cdd59bba3 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Thu, 29 Sep 2022 12:35:50 +0530 Subject: [PATCH 01/16] Update load_weight_from_keras.py --- PyTorch/load_weight_from_keras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyTorch/load_weight_from_keras.py b/PyTorch/load_weight_from_keras.py index 76a9ced..76a3977 100644 --- a/PyTorch/load_weight_from_keras.py +++ b/PyTorch/load_weight_from_keras.py @@ -19,7 +19,7 @@ from torchvision import models import torch.nn.functional as F -from pytorch_model import PTModel +from model import PTModel # Argument Parser parser = argparse.ArgumentParser(description='High Quality Monocular Depth Estimation via Transfer Learning') From 9cb85ded141cba466475d70248462e6c85641478 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:01:19 +0530 Subject: [PATCH 02/16] Update load_weight_from_keras.py --- PyTorch/load_weight_from_keras.py | 127 ++++++++++++++++-------------- 1 file changed, 69 insertions(+), 58 deletions(-) diff --git a/PyTorch/load_weight_from_keras.py b/PyTorch/load_weight_from_keras.py index 76a3977..8062586 100644 --- a/PyTorch/load_weight_from_keras.py +++ b/PyTorch/load_weight_from_keras.py @@ -19,72 +19,78 @@ from torchvision import models import torch.nn.functional as F -from model import PTModel +from model_pt import PTModel -# Argument Parser +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') +#device = 'cpu' +#Argument Parser parser = argparse.ArgumentParser(description='High Quality Monocular Depth Estimation via Transfer Learning') parser.add_argument('--model', default='../nyu.h5', type=str, help='Trained Keras model file.') parser.add_argument('--input', default='../examples/*.png', type=str, help='Input filename or folder.') args = parser.parse_args() -# Custom object needed for inference and training -custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None} +# # Custom object needed for inference and training +# custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None} -print('Loading model...') +# print('Loading model...') -# Load model into GPU / CPU -model = load_model(args.model, custom_objects=custom_objects, compile=False) -names = [weight.name for layer in model.layers for weight in layer.weights] -weights = model.get_weights() +# # Load model into GPU / CPU +# model = load_model(args.model, custom_objects=custom_objects, compile=False) +# names = [weight.name for layer in model.layers for weight in layer.weights] +# weights = model.get_weights() -keras_name = [] -for name, weight in zip(names, weights): - keras_name.append(name) +# keras_name = [] +# for name, weight in zip(names, weights): +# keras_name.append(name) -pytorch_model = PTModel().float() +# pytorch_model = PTModel().float() -# load parameter from keras -keras_state_dict = {} -j = 0 -for name, param in pytorch_model.named_parameters(): +# # load parameter from keras +# keras_state_dict = {} +# j = 0 +# for name, param in pytorch_model.named_parameters(): - if 'classifier' in name: - keras_state_dict[name]=param - continue - - if 'conv' in name and 'weight' in name: - keras_state_dict[name]=torch.from_numpy(np.transpose(weights[j],(3, 2, 0, 1))) - # print(name,keras_name[j]) - j = j+1 - continue +# if 'classifier' in name: +# keras_state_dict[name]=param +# continue + +# if 'conv' in name and 'weight' in name: +# keras_state_dict[name]=torch.from_numpy(np.transpose(weights[j],(3, 2, 0, 1))) +# # print(name,keras_name[j]) +# j = j+1 +# continue - if 'conv' in name and 'bias' in name: - keras_state_dict[name]=torch.from_numpy(weights[j]) - # print(param.shape,weights[j].size) - j = j+1 - continue - - if 'norm' in name and 'weight' in name: - keras_state_dict[name]=torch.from_numpy(weights[j]) - # print(param.shape,weights[j].shape) - j = j+1 - continue - - if 'norm' in name and 'bias' in name: - keras_state_dict[name]=torch.from_numpy(weights[j]) - # print(param.shape,weights[j].size) - j = j+1 - keras_state_dict[name.replace("bias", "running_mean")]=torch.from_numpy(weights[j]) - # print(param.shape,weights[j].size) - j = j+1 - keras_state_dict[name.replace("bias", "running_var")]=torch.from_numpy(weights[j]) - # print(param.shape,weights[j].size) - j = j+1 - continue - - -pytorch_model.load_state_dict(keras_state_dict) +# if 'conv' in name and 'bias' in name: +# keras_state_dict[name]=torch.from_numpy(weights[j]) +# # print(param.shape,weights[j].size) +# j = j+1 +# continue + +# if 'norm' in name and 'weight' in name: +# keras_state_dict[name]=torch.from_numpy(weights[j]) +# # print(param.shape,weights[j].shape) +# j = j+1 +# continue + +# if 'norm' in name and 'bias' in name: +# keras_state_dict[name]=torch.from_numpy(weights[j]) +# # print(param.shape,weights[j].size) +# j = j+1 +# keras_state_dict[name.replace("bias", "running_mean")]=torch.from_numpy(weights[j]) +# # print(param.shape,weights[j].size) +# j = j+1 +# keras_state_dict[name.replace("bias", "running_var")]=torch.from_numpy(weights[j]) +# # print(param.shape,weights[j].size) +# j = j+1 +# continue + + +# pytorch_model.load_state_dict(keras_state_dict) +pytorch_model = torch.load('depth_3.pth') pytorch_model.eval() +#torch.save(pytorch_model,"depth_3.pth") +pytorch_model.to(device) +print("model_to_gpu") def my_DepthNorm(x, maxDepth): @@ -94,19 +100,24 @@ def my_predict(model, images, minDepth=10, maxDepth=1000): with torch.no_grad(): # Compute predictions - predictions = model(images) + predictions = model(images.to(device)) # Put in expected range - return np.clip(my_DepthNorm(predictions.numpy(), maxDepth=maxDepth), minDepth, maxDepth) / maxDepth - + return np.clip(my_DepthNorm(predictions.cpu().numpy(), maxDepth=maxDepth), minDepth, maxDepth) / maxDepth +import time # # Input images inputs = load_images( glob.glob(args.input) ).astype('float32') + pytorch_input = torch.from_numpy(inputs[0,:,:,:]).permute(2,0,1).unsqueeze(0) + print(pytorch_input.shape) # print('\nLoaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:])) - -# # Compute results -output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) +for i in range(100): + tic = time.time() + # # Compute results + output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) + toc = time.time() + print(toc-tic) print(output.shape) plt.imshow(output[0,0,:,:]) plt.savefig('test.png') From 9b6645caaf64ae68ecee5da2b2841ea2ebdc6994 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:02:52 +0530 Subject: [PATCH 03/16] Rename model.py to model_pt.py --- PyTorch/{model.py => model_pt.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename PyTorch/{model.py => model_pt.py} (100%) diff --git a/PyTorch/model.py b/PyTorch/model_pt.py similarity index 100% rename from PyTorch/model.py rename to PyTorch/model_pt.py From 6648785437470d58434f6e71d5107d7f8e32d238 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:09:45 +0530 Subject: [PATCH 04/16] Update DenseDepth.ipynb --- DenseDepth.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 793d418..2fa2df5 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -38,7 +38,7 @@ "outputId": "491e7243-6dee-4e32-95c9-a817004775cd" }, "source": [ - "!git clone https://github.com/ialhashim/DenseDepth.git" + "!git clone https://github.com/Avi241/DenseDepth.git" ], "execution_count": 1, "outputs": [ @@ -207,4 +207,4 @@ "outputs": [] } ] -} \ No newline at end of file +} From d7cf7b2765f97f767295c9cc851a1d714f0aaaf6 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:10:36 +0530 Subject: [PATCH 05/16] Update and rename load_weight_from_keras.py to test_pytorch.py --- .../{load_weight_from_keras.py => test_pytorch.py} | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) rename PyTorch/{load_weight_from_keras.py => test_pytorch.py} (95%) diff --git a/PyTorch/load_weight_from_keras.py b/PyTorch/test_pytorch.py similarity index 95% rename from PyTorch/load_weight_from_keras.py rename to PyTorch/test_pytorch.py index 8062586..e3fa823 100644 --- a/PyTorch/load_weight_from_keras.py +++ b/PyTorch/test_pytorch.py @@ -112,12 +112,11 @@ def my_predict(model, images, minDepth=10, maxDepth=1000): print(pytorch_input.shape) # print('\nLoaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:])) -for i in range(100): - tic = time.time() - # # Compute results - output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) - toc = time.time() - print(toc-tic) +tic = time.time() +# # Compute results +output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) +toc = time.time() +print(toc-tic) print(output.shape) plt.imshow(output[0,0,:,:]) plt.savefig('test.png') From ba5f25b7dbe1f2ec99ff9ba68c6f456156d37c9f Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:11:10 +0530 Subject: [PATCH 06/16] Update test_pytorch.py --- PyTorch/test_pytorch.py | 102 ++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/PyTorch/test_pytorch.py b/PyTorch/test_pytorch.py index e3fa823..8049f88 100644 --- a/PyTorch/test_pytorch.py +++ b/PyTorch/test_pytorch.py @@ -29,64 +29,64 @@ parser.add_argument('--input', default='../examples/*.png', type=str, help='Input filename or folder.') args = parser.parse_args() -# # Custom object needed for inference and training -# custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None} +# Custom object needed for inference and training +custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None} -# print('Loading model...') +print('Loading model...') -# # Load model into GPU / CPU -# model = load_model(args.model, custom_objects=custom_objects, compile=False) -# names = [weight.name for layer in model.layers for weight in layer.weights] -# weights = model.get_weights() +# Load model into GPU / CPU +model = load_model(args.model, custom_objects=custom_objects, compile=False) +names = [weight.name for layer in model.layers for weight in layer.weights] +weights = model.get_weights() -# keras_name = [] -# for name, weight in zip(names, weights): -# keras_name.append(name) +keras_name = [] +for name, weight in zip(names, weights): + keras_name.append(name) -# pytorch_model = PTModel().float() +pytorch_model = PTModel().float() -# # load parameter from keras -# keras_state_dict = {} -# j = 0 -# for name, param in pytorch_model.named_parameters(): +# load parameter from keras +keras_state_dict = {} +j = 0 +for name, param in pytorch_model.named_parameters(): -# if 'classifier' in name: -# keras_state_dict[name]=param -# continue - -# if 'conv' in name and 'weight' in name: -# keras_state_dict[name]=torch.from_numpy(np.transpose(weights[j],(3, 2, 0, 1))) -# # print(name,keras_name[j]) -# j = j+1 -# continue + if 'classifier' in name: + keras_state_dict[name]=param + continue + + if 'conv' in name and 'weight' in name: + keras_state_dict[name]=torch.from_numpy(np.transpose(weights[j],(3, 2, 0, 1))) + # print(name,keras_name[j]) + j = j+1 + continue -# if 'conv' in name and 'bias' in name: -# keras_state_dict[name]=torch.from_numpy(weights[j]) -# # print(param.shape,weights[j].size) -# j = j+1 -# continue - -# if 'norm' in name and 'weight' in name: -# keras_state_dict[name]=torch.from_numpy(weights[j]) -# # print(param.shape,weights[j].shape) -# j = j+1 -# continue - -# if 'norm' in name and 'bias' in name: -# keras_state_dict[name]=torch.from_numpy(weights[j]) -# # print(param.shape,weights[j].size) -# j = j+1 -# keras_state_dict[name.replace("bias", "running_mean")]=torch.from_numpy(weights[j]) -# # print(param.shape,weights[j].size) -# j = j+1 -# keras_state_dict[name.replace("bias", "running_var")]=torch.from_numpy(weights[j]) -# # print(param.shape,weights[j].size) -# j = j+1 -# continue - - -# pytorch_model.load_state_dict(keras_state_dict) -pytorch_model = torch.load('depth_3.pth') + if 'conv' in name and 'bias' in name: + keras_state_dict[name]=torch.from_numpy(weights[j]) + # print(param.shape,weights[j].size) + j = j+1 + continue + + if 'norm' in name and 'weight' in name: + keras_state_dict[name]=torch.from_numpy(weights[j]) + # print(param.shape,weights[j].shape) + j = j+1 + continue + + if 'norm' in name and 'bias' in name: + keras_state_dict[name]=torch.from_numpy(weights[j]) + # print(param.shape,weights[j].size) + j = j+1 + keras_state_dict[name.replace("bias", "running_mean")]=torch.from_numpy(weights[j]) + # print(param.shape,weights[j].size) + j = j+1 + keras_state_dict[name.replace("bias", "running_var")]=torch.from_numpy(weights[j]) + # print(param.shape,weights[j].size) + j = j+1 + continue + + +pytorch_model.load_state_dict(keras_state_dict) +# pytorch_model = torch.load('depth_3.pth') pytorch_model.eval() #torch.save(pytorch_model,"depth_3.pth") pytorch_model.to(device) From 832f71a099fd3af4c0e09973bf7f82c06dc73b11 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:13:01 +0530 Subject: [PATCH 07/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a9d77c..e79db88 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Offical Keras (TensorFlow) implementaiton. If you have any questions or need more help with the code, contact the **first author**. -**[Update]** Added a [Colab notebook](https://github.com/ialhashim/DenseDepth/blob/master/DenseDepth.ipynb) to try the method on the fly. +**[Update]** Added a [Colab notebook](https://github.com/Avi241/DenseDepth/blob/master/DenseDepth.ipynb) to try the method on the fly. **[Update]** Experimental TensorFlow 2.0 implementation added. From 8f2f868b7b947e0a64a29bdfd780fbce241bdb51 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:16:17 +0530 Subject: [PATCH 08/16] Update DenseDepth.ipynb --- DenseDepth.ipynb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 2fa2df5..4ae0426 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -197,11 +197,6 @@ }, "source": [ "!cd DenseDepth/PyTorch; python test_pytorch.py\n", - "from matplotlib import pyplot as plt\n", - "from skimage import io\n", - "\n", - "plt.figure(figsize=(20,20))\n", - "plt.imshow( io.imread('./DenseDepth/test.png') )" ], "execution_count": 0, "outputs": [] From 228b5f63752049bce7f832092a6735d791016bdb Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:16:44 +0530 Subject: [PATCH 09/16] Update DenseDepth.ipynb --- DenseDepth.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 4ae0426..61332a0 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -196,7 +196,7 @@ "colab": {} }, "source": [ - "!cd DenseDepth/PyTorch; python test_pytorch.py\n", + "!cd DenseDepth/PyTorch; python test_pytorch.py\n" ], "execution_count": 0, "outputs": [] From ab4dc9c972e6443bab35a8bca5670753971ff5c7 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:22:22 +0530 Subject: [PATCH 10/16] Created using Colaboratory --- DenseDepth.ipynb | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 61332a0..531a658 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -4,7 +4,6 @@ "metadata": { "colab": { "name": "DenseDepth", - "version": "0.3.2", "provenance": [], "collapsed_sections": [], "include_colab_link": true @@ -23,14 +22,13 @@ "colab_type": "text" }, "source": [ - "\"Open" + "\"Open" ] }, { "cell_type": "code", "metadata": { "id": "ahkR4C5dEnR0", - "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 139 @@ -38,9 +36,9 @@ "outputId": "491e7243-6dee-4e32-95c9-a817004775cd" }, "source": [ - "!git clone https://github.com/Avi241/DenseDepth.git" + "!git clone https://github.com/ialhashim/DenseDepth.git" ], - "execution_count": 1, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -61,7 +59,6 @@ "cell_type": "code", "metadata": { "id": "fFQgwMlNExak", - "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 208 @@ -71,7 +68,7 @@ "source": [ "!wget https://s3-eu-west-1.amazonaws.com/densedepth/nyu.h5 -O ./DenseDepth/nyu.h5" ], - "execution_count": 2, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -96,7 +93,6 @@ "cell_type": "code", "metadata": { "id": "AiJKd6uLE9Gr", - "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 468 @@ -106,7 +102,7 @@ "source": [ "!cd DenseDepth; python test.py" ], - "execution_count": 3, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -146,7 +142,6 @@ "cell_type": "code", "metadata": { "id": "HjzqM74-FfyL", - "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 627 @@ -160,7 +155,7 @@ "plt.figure(figsize=(20,20))\n", "plt.imshow( io.imread('./DenseDepth/test.png') )" ], - "execution_count": 7, + "execution_count": null, "outputs": [ { "output_type": "execute_result", @@ -191,15 +186,18 @@ { "cell_type": "code", "metadata": { - "id": "Ra7GEtQBHUMS", - "colab_type": "code", - "colab": {} + "id": "Ra7GEtQBHUMS" }, "source": [ - "!cd DenseDepth/PyTorch; python test_pytorch.py\n" + "!cd DenseDepth/PyTorch; python test_pytorch.py\n", + "from matplotlib import pyplot as plt\n", + "from skimage import io\n", + "\n", + "plt.figure(figsize=(20,20))\n", + "plt.imshow( io.imread('./DenseDepth/test.png') )" ], - "execution_count": 0, + "execution_count": null, "outputs": [] } ] -} +} \ No newline at end of file From 7618fc65c33056967f2389db2a1ebd1b339ba6e7 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:27:19 +0530 Subject: [PATCH 11/16] Created using Colaboratory --- DenseDepth.ipynb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 531a658..0f6cdb3 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -3,7 +3,6 @@ "nbformat_minor": 0, "metadata": { "colab": { - "name": "DenseDepth", "provenance": [], "collapsed_sections": [], "include_colab_link": true @@ -36,7 +35,7 @@ "outputId": "491e7243-6dee-4e32-95c9-a817004775cd" }, "source": [ - "!git clone https://github.com/ialhashim/DenseDepth.git" + "!git clone https://github.com/Avi241/DenseDepth.git" ], "execution_count": null, "outputs": [ @@ -189,12 +188,7 @@ "id": "Ra7GEtQBHUMS" }, "source": [ - "!cd DenseDepth/PyTorch; python test_pytorch.py\n", - "from matplotlib import pyplot as plt\n", - "from skimage import io\n", - "\n", - "plt.figure(figsize=(20,20))\n", - "plt.imshow( io.imread('./DenseDepth/test.png') )" + "!cd DenseDepth/PyTorch; python test_pytorch.py" ], "execution_count": null, "outputs": [] From 9eefd9295120be14c8790f6e0c25dd931d69a632 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:34:21 +0530 Subject: [PATCH 12/16] Update test_pytorch.py --- PyTorch/test_pytorch.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/PyTorch/test_pytorch.py b/PyTorch/test_pytorch.py index 8049f88..3576c67 100644 --- a/PyTorch/test_pytorch.py +++ b/PyTorch/test_pytorch.py @@ -112,11 +112,12 @@ def my_predict(model, images, minDepth=10, maxDepth=1000): print(pytorch_input.shape) # print('\nLoaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:])) -tic = time.time() -# # Compute results -output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) -toc = time.time() -print(toc-tic) +# Compute results (When it prdeicts on first it takes some time after that it runs fast you can check with using for loop) +for i in range(100): + tic = time.time() + output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) + toc = time.time() + print(toc-tic) print(output.shape) plt.imshow(output[0,0,:,:]) plt.savefig('test.png') From 25acdcdf282c2b1df85d8acc75621d22fc3f3fd6 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 12:44:39 +0530 Subject: [PATCH 13/16] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e79db88..fc4a4cb 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ Offical Keras (TensorFlow) implementaiton. If you have any questions or need mor **[Update]** Experimental PyTorch code added. +**[Update]** Implemented cuda version of Pytorch. + +**[Update]** Added Pytorch model for both Python3 and Python2. Please Find it here. + ## Results * KITTI From a4175d9118c9e29daef49787966c87f318b32912 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 13:10:08 +0530 Subject: [PATCH 14/16] Update test_pytorch.py --- PyTorch/test_pytorch.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/PyTorch/test_pytorch.py b/PyTorch/test_pytorch.py index 3576c67..197bb7b 100644 --- a/PyTorch/test_pytorch.py +++ b/PyTorch/test_pytorch.py @@ -27,7 +27,10 @@ parser = argparse.ArgumentParser(description='High Quality Monocular Depth Estimation via Transfer Learning') parser.add_argument('--model', default='../nyu.h5', type=str, help='Trained Keras model file.') parser.add_argument('--input', default='../examples/*.png', type=str, help='Input filename or folder.') +parser.add_argument('--cuda', default=1, type=int, help='Enable of Disbale Cuda') args = parser.parse_args() +if args.cuda==0: + device = 'cpu' # Custom object needed for inference and training custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None} @@ -90,7 +93,10 @@ pytorch_model.eval() #torch.save(pytorch_model,"depth_3.pth") pytorch_model.to(device) -print("model_to_gpu") +if device.__eq__('cuda'): + print("Loaded model to GPU") +else: + print("Loaded model to CPU") def my_DepthNorm(x, maxDepth): @@ -110,15 +116,17 @@ def my_predict(model, images, minDepth=10, maxDepth=1000): pytorch_input = torch.from_numpy(inputs[0,:,:,:]).permute(2,0,1).unsqueeze(0) -print(pytorch_input.shape) +print("Input Shape = " + str(pytorch_input.shape)) # print('\nLoaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:])) + # Compute results (When it prdeicts on first it takes some time after that it runs fast you can check with using for loop) -for i in range(100): +for i in range(10): tic = time.time() output = my_predict(pytorch_model,pytorch_input[0,:,:,:].unsqueeze(0)) toc = time.time() - print(toc-tic) -print(output.shape) + print("Time for test "+str(i)+" "+str(1000*(toc-tic))+" ms") + +print("Output Shape = " + str(output.shape)) plt.imshow(output[0,0,:,:]) plt.savefig('test.png') plt.show() From c5f26637d723f2976554edb15363c190fdce72b1 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Sat, 1 Oct 2022 13:11:44 +0530 Subject: [PATCH 15/16] Update DenseDepth.ipynb --- DenseDepth.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DenseDepth.ipynb b/DenseDepth.ipynb index 0f6cdb3..9b6923f 100644 --- a/DenseDepth.ipynb +++ b/DenseDepth.ipynb @@ -188,10 +188,10 @@ "id": "Ra7GEtQBHUMS" }, "source": [ - "!cd DenseDepth/PyTorch; python test_pytorch.py" + "!cd DenseDepth/PyTorch; python test_pytorch.py --cuda 1" ], "execution_count": null, "outputs": [] } ] -} \ No newline at end of file +} From ae95b05a9fcc13068ada1379b4ef4d255bddaf28 Mon Sep 17 00:00:00 2001 From: Arvind Pandit Date: Wed, 7 Dec 2022 10:45:05 +0530 Subject: [PATCH 16/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc4a4cb..601b040 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Offical Keras (TensorFlow) implementaiton. If you have any questions or need mor **[Update]** Implemented cuda version of Pytorch. -**[Update]** Added Pytorch model for both Python3 and Python2. Please Find it here. +**[Update]** Added Pytorch model for both Python3 and Python2. Please Find it [here.](https://drive.google.com/file/d/1Wi-w9uyErZwCn_x3DQeILntQgLdml72p/view?usp=share_link) ## Results