dwofk / fast-depth

ICRA 2019 "FastDepth: Fast Monocular Depth Estimation on Embedded Systems"
MIT License
926 stars 189 forks source link

Strange Depth Output Image #52

Closed LulaSan closed 3 years ago

LulaSan commented 3 years ago

Hi everyone, I am not currently using TVM for compatibility issues with colab, so I tried visualize the output without it. This is my code:

import dataloaders.transforms as transforms
from dataloaders.dataloader import MyDataloader
import torch 
from PIL import Image
from PIL import Image
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import cv2

img_path = '/content/drive/MyDrive/TESI/FastDepth/fast-depth/cucina.jpg'
imgOrig = np.array(Image.open(img_path))
img = np.asfarray(imgOrig, dtype=np.float32) / 255
img = np.resize(img, (3, 224, 224))
img = np.expand_dims(img, axis=0)
img = torch.from_numpy(img.copy())

print(img.shape) #torch.Size([1, 3, 224, 224])

with torch.no_grad():
  Prediction=model(img)
print(Prediction.shape) #torch.Size([1, 1, 224, 224])`

prediction = cv2.resize(Prediction[0, 0].numpy(),
                       imgOrig.shape[:2][::-1],
                       interpolation=cv2.INTER_CUBIC)
print(f"prediction size is {prediction.shape}")
MAX_DEPTH = prediction.max()
MIN_DEPTH = prediction.min()
prediction = np.abs(prediction)
plt.figure(figsize=(18, 12))
plt.subplot(152)
plt.imshow(prediction, cmap='plasma', vmin=MIN_DEPTH, vmax=MAX_DEPTH)
plt.title('pred depth')
plt.axis('off')
plt.subplot(151)
img_path = '/content/drive/MyDrive/TESI/FastDepth/fast-depth/cucina.jpg'
img = np.array(Image.open(img_path))
plt.imshow(img)
plt.title('orig img')
 `

image Is it normal?

P.S. I am using this as trained model

loading model trained

checkpointpath='mobilenet-nnconv5dw-skipadd-pruned.pth.tar'

LulaSan commented 3 years ago

It turned out that this

imgOrig = np.array(Image.open(img_path))
img = np.asfarray(imgOrig, dtype=np.float32) / 255
img = np.resize(img, (3, 224, 224))
img = np.expand_dims(img, axis=0)
img = torch.from_numpy(img.copy())

the resize function, was wrong. I instead did in that way : img= plt.imread(img_path) imgResh = cv2.resize(img, dsize=(224, 224)) img = np.transpose(imgResh, (2,0,1)) img = np.expand_dims(img, axis=0)

metobom commented 3 years ago

@LulaSan Can you share which model did you use?

LulaSan commented 3 years ago

@metobom I used mobilenet-nnconv5dw-skipadd-pruned.pth.tar', loading in this way

import models
import torch
#loading model trained
checkpointpath='mobilenet-nnconv5dw-skipadd-pruned.pth.tar'

if torch.cuda.is_available():
  map_location=lambda storage, loc: storage.cuda()
else:
  map_location='cpu'
checkpoint = torch.load(checkpointpath,map_location=map_location)
if type(checkpoint) is dict:
    start_epoch = checkpoint['epoch']
    best_result = checkpoint['best_result']
    model = checkpoint['model']
    print("=> loaded best model (epoch {})".format(checkpoint['epoch']))