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
models fail to decompress #25
Comments
You don't have to decompress it, you just load them with the pretrained argument in the main file ! |
Got it. Thanks very much. |
how and where to load the .pth.tar file? |
in the command line options, there is a
Other than that, if you want to use it in another projet, ie in a notebook, you have to call the constructor function for you network (say from models import flownets
weights = torch.load(path_to_.pth.tar)
my_model = flownets(data=weights)
dummy_img = torch.randn(1,6,100,100)
my_flow = my_model(dummy_img) You can see in the |
Thank you for the reply. will try it and get back to you soon .. |
this is the code that i have written import torch
from models import flownets
import cv2
import numpy
filename1 = 'img_1.jpg'
filename2 = 'img_2.jpg'
w =512.
h =384.
oriimg1 = cv2.imread(filename1,cv2.IMREAD_COLOR)
oriimg2 = cv2.imread(filename2,cv2.IMREAD_COLOR)
height, width, depth = oriimg1.shape
imgScale1 = w/width
imgScale0= h/height
newX,newY = oriimg1.shape[1]*imgScale1, oriimg1.shape[0]*imgScale0
newimg = cv2.resize(oriimg1,(int(newX),int(newY)))
cv2.imshow("Show by CV2",newimg)
#cv2.waitKey(0)
cv2.imwrite("resizeimg1.jpg",newimg)
height, width, depth = oriimg2.shape
imgScale1 = w/width
imgScale0= h/height
newX,newY = oriimg2.shape[1]*imgScale1, oriimg2.shape[0]*imgScale0
newimg = cv2.resize(oriimg2,(int(newX),int(newY)))
cv2.imshow("Show by CV2",newimg)
#cv2.waitKey(0)
cv2.imwrite("resizeimg2.jpg",newimg)
c1=cv2.imread('resizeimg1.jpg',cv2.IMREAD_COLOR)
c2=cv2.imread('resizeimg2.jpg',cv2.IMREAD_COLOR)
c1 = torch.from_numpy(numpy.array(c1)).double()
c2 = torch.from_numpy(numpy.array(c2)).double()
input_image=torch.cat((c1,c2),2)
input_image=numpy.expand_dims(input_image,axis=0)
input_image=torch.from_numpy(numpy.array(input_image))
print(input_image.shape)
input_image = input_image.transpose(2,3).transpose(1,2)
print(input_image.shape)
input_image = input_image.type('torch.FloatTensor')
weights = torch.load('/home/prateek/Prateek/FlowNetPytorch/flownets_EPE1.951.pth.tar')
my_model = flownets(data=weights)
my_flow = my_model(input_image)
print(my_flow)
my_flow = torch.Tensor.numpy(my_flow)
cv2.imshow('opticalflow.jpg',my_flow)
cv2.waitKey(0) i get the error what to do in order to get rid of this ? |
You need to make the model in eval mode. That way, it won't output multiple scale flow, but only the highest scale. also your code is very convoluted, here is a simplified version : import torch
from models import flownets
import cv2
import numpy
filename1 = 'img_1.jpg'
filename2 = 'img_2.jpg'
w =512.
h =384.
oriimg1 = cv2.imread(filename1,cv2.IMREAD_COLOR)
oriimg2 = cv2.imread(filename2,cv2.IMREAD_COLOR)
img1_scaled = cv2.resize(oriimg1,(h,w))
cv2.imshow("Show by CV2",newimg)
img2_scaled = cv2.resize(oriimg2,(h,w))
img1_tensor = torch.from_numpy(img1_scaled).float().permute(2,0,1)
img1_tensor = img1_tensor/255 - 0.5
img2_tensor = torch.from_numpy(img2_scaled).float().permute(2,0,1)
img2_tensor = img1_tensor/255 - 0.5
input_image=torch.cat((img1_tensor, img2_tensor)).unsqueeze(0)
weights = torch.load('/home/prateek/Prateek/FlowNetPytorch/flownets_EPE1.951.pth.tar')
my_model = flownets(data=weights).eval()
my_flow = my_model(input_image)
print(my_flow)
my_flow = my_flow[0].detach().numpy()
cv2.imshow('opticalflowu.jpg',my_flow[0])
cv2.imshow('opticalflowv.jpg',my_flow[1])
cv2.waitKey(0) You can note that no cuda is used for the moment, this will then be a bit slow, you can convert your input and the model to cuda with the Also don't forget that optical flow is 2D, so opencv won't be able to show it, since it can only show grayscale (1D data) or color (3D data), hence the two imshow at the end. If you want a unified flow map window, you can have a look at that function that I designed, it's essentially a YUV->RGB converter that maps flow to U and V and puts Y to 1. |
Sir, # compute output
output = model(input)
if args.sparse:
# Since Target pooling is not very precise when sparse,
# take the highest resolution prediction and upsample it instead of downsampling target
h, w = target.size()[-2:]
output = [F.interpolate(output[0], (h,w)), *output[1:]]** what does this code do? could you please elaborate it. one last doubt, could you please explain me why we are only considering my_flow[0] but not the whole tensor? sorry for the inconvenience caused. |
Some insight for you to read : #23 #27 As for my_flow[0], the main idea is that in pytorch everything is a batch. Even if you have only one element, you must construct batch of one element. As a consequence when taking the output of your network, if you want to work with the flow map of your only element, you must use the first element of your batch. |
Thank you, sir, for these insights. |
Respected Sir, import torch
from models import flownets
import cv2
import numpy
import torch.nn.functional as F
filename1 = 'img_1.jpg'
filename2 = 'img_2.jpg'
w =512.
h =384.
oriimg1 = cv2.imread(filename1,cv2.IMREAD_COLOR)
print(oriimg1.shape)
oriimg2 = cv2.imread(filename2,cv2.IMREAD_COLOR)
img1_scaled = cv2.resize(oriimg1,(int(w),int(h)))
#cv2.imshow("Show by CV2",newimg)
img2_scaled = cv2.resize(oriimg2,(int(w),int(h)))
print(img1_scaled.shape)
img1_tensor = torch.from_numpy(img1_scaled).float().permute(2,0,1)
img1_tensor = (img1_tensor/255) - 0.5
img2_tensor = torch.from_numpy(img2_scaled).float().permute(2,0,1)
img2_tensor = (img1_tensor/255) - 0.5
input_image=torch.cat((img1_tensor, img2_tensor)).unsqueeze(0)
weights = torch.load('/home/prateek/Prateek/FlowNetPytorch/flownets_EPE1.951.pth.tar')
my_model = flownets(data=weights).eval()
my_flow = my_model(input_image)
my_flow = F.interpolate(my_flow, size=(int(h), int(w)), mode='bilinear').permute(0,2,3,1)
img1_tensor=img1_tensor.unsqueeze(0)
verify_output= F.grid_sample(img1_tensor, my_flow, mode='bilinear')
verify_output= verify_output[0].detach().numpy().reshape(384,512,3)
cv2.imshow('aaa',verify_output)
cv2.waitKey(0) |
can you paste the stack trace ? What fails ? Can you visualize the flow ? Also remeber than grid_sample takes explicit coordinates, not relative (that means you have to add the identity matrix to flow, otherwise you would sample the point at (0,0) with a null flow instead of sampling it at (i,j). Besides, the grid_sample wants values between 1 and -1 relative to image boundaries. everything outside will give you gray colors (the color (0,0,0)) so before sampling you image from flow, you need to
I recommend you to read the documentation on grid sample here Finally, remeber that grid sample is an inverse warp. That means with an optical flow from Img1 to Img2, you can reconstruct Img1 from Img2, and not the other way around, you want to make a grid_sample from |
there is no problem with the syntax as such but i get this as the output in the terminal: i get the above image as the output |
Respected Sir, **import torch filename1 = 'img_1.jpg' w =512. oriimg1 = cv2.imread(filename1,cv2.IMREAD_COLOR) oriimg2 = cv2.imread(filename2,cv2.IMREAD_COLOR) img1_scaled = cv2.resize(oriimg1,(int(w),int(h))) img1_tensor = torch.from_numpy(img1_scaled).float().permute(2,0,1) img2_tensor = torch.from_numpy(img2_scaled).float().permute(2,0,1) input_image=torch.cat((img1_tensor, img2_tensor)).unsqueeze(0) weights = torch.load('/home/prateek/Prateek/FlowNetPytorch/flownets_EPE1.951.pth.tar') my_model = flownets(data=weights).eval() my_flow = F.interpolate(my_flow, size=(int(h), int(w)), mode='bilinear',align_corners=True).permute(0,2,3,1) mapx= numpy.zeros((384,512,1)) height=my_flow.shape[0] cv2.convertMaps(pixel_map[:,:,0],pixel_map[:,:,1], cv2.CV_32FC1 && cv2.CV_32FC1,mapx,mapy, nninterpolation=False) new_frame = cv2.remap(img1_scaled, pixel_map[:,:,0],pixel_map[:,:,1],interpolation=cv2.INTER_LINEAR)** BUT i get this error, Could you please help me resolve this error. |
I don't know about your problem which seems to be related with cv2 only. Sorry, there is no trivial way to make a direct warp, you will likely end up with ambiguity on occlusion zones: how to decide which point comes at front and overwrite the other one ? Best advice is probably to compute inverse flow (img2 to img1) if you want to warp Img1 into Img2 |
Respected Sir, which function is better to use for inverse warp? grid_sample or remap? looking forward to your reply Sir. |
The two function are the same. Grid_sample can be used with cuda though. |
Respected Sir, |
Sir could you please explain what is proj_c2p_rot & proj_c2p_tr in: and could you please elaborate on what is intrinsic_inv |
These are not something you should worry about, it's the parti of the inverse warp function that compute the pixel coordinates from depth + 6degrees of freedom pose. For your usecase, you already have the coordinates. All you need to do is explained in this comment (above) :
You already had it with opencv2 when you tried to use numpy.meshgrid and cv2.remap, the workflow is the exact same, just the functions are different, and the grid sample takes normalized coordinates instead of pixel coordinates. Good luck ! |
Sir, Sir, i have checked the flow values. why are the flow values really small? is this understanding right or wrong ? kindly correct me if i am wrong. |
The values are usually divided by 20, this is a legacy coming from original author's code. |
Hello, i download the flownets_bn_EPE2.459.pth.tar and flownets_EPE1.951.pth.tar
And when i decompress it, the file is broken. Can you share the two files again?
Thanks very much.
The text was updated successfully, but these errors were encountered: