Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feedback #1

Open
wants to merge 32 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c04f6b8
Setting up GitHub Classroom Feedback
github-classroom[bot] Feb 6, 2021
f374510
Update GitHub Classroom Feedback
github-classroom[bot] Feb 6, 2021
c8ebfe1
Setting up GitHub Classroom Feedback
github-classroom[bot] Feb 6, 2021
08b33cd
added proposal slides
ShivanshRakesh Feb 14, 2021
f14e66a
slight mod in proposal
ShivanshRakesh Feb 15, 2021
a3a36cb
added tmp code for finding edges
ShivanshRakesh Mar 27, 2021
ad9f588
added code for connected components
ShivanshRakesh Mar 27, 2021
814c1ec
Files Added
vutkarsh01 Mar 27, 2021
855fd4d
flood fill works
ShivanshRakesh Mar 27, 2021
6d6cf02
init project.ipynb
ShivanshRakesh Mar 27, 2021
4ab6f9c
Done
Mar 27, 2021
1d9d6a9
Done
Mar 27, 2021
6cd7b51
added model.pt
ShivanshRakesh Mar 27, 2021
8d115d1
added outputs
ShivanshRakesh Mar 27, 2021
84ca19c
added helper function for context-synthesis, reorganised
ShivanshRakesh Mar 27, 2021
13ba05c
Delete Hufflepuff.pdf
worldinmyfist Apr 3, 2021
4eb6723
Add files via upload
worldinmyfist Apr 3, 2021
7ad96ab
PDF updated!
vutkarsh01 Apr 3, 2021
7a4dd80
removed model files
ShivanshRakesh Apr 3, 2021
08192a3
bilateral filtering
vutkarsh01 Apr 7, 2021
9c27cc6
Yes
Apr 11, 2021
97e7a00
Midas Added!
vutkarsh01 Apr 15, 2021
5d21fb8
Mesh utilities.
subodhsondkar Apr 17, 2021
19ab93c
Utilities.
subodhsondkar Apr 19, 2021
178a3b7
make_video
ShivanshRakesh Apr 24, 2021
6f2e37d
added working notebook
ShivanshRakesh Apr 24, 2021
4f87775
Added outputs
Apr 24, 2021
5f9c1cc
added final ppt
ShivanshRakesh Apr 24, 2021
85cf0e3
Merge pull request #2 from Computer-Vision-IIITH-2021/dev
ShivanshRakesh Apr 24, 2021
7321064
updated repo link
ShivanshRakesh Apr 24, 2021
18015e5
cleaned the main branch
ShivanshRakesh Apr 24, 2021
9e30972
Update README.md
ShivanshRakesh Apr 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.ipynb_checkpoints
.DS_Store
tmp*
Binary file added Hufflepuff.pdf
Binary file not shown.
Binary file added Hufflepuff.pptx
Binary file not shown.
192 changes: 192 additions & 0 deletions MiDaS/MiDaS_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
"""Utils for monoDepth.
"""
import sys
import re
import numpy as np
import cv2
import torch
import imageio


def read_pfm(path):
"""Read pfm file.

Args:
path (str): path to file

Returns:
tuple: (data, scale)
"""
with open(path, "rb") as file:

color = None
width = None
height = None
scale = None
endian = None

header = file.readline().rstrip()
if header.decode("ascii") == "PF":
color = True
elif header.decode("ascii") == "Pf":
color = False
else:
raise Exception("Not a PFM file: " + path)

dim_match = re.match(r"^(\d+)\s(\d+)\s$", file.readline().decode("ascii"))
if dim_match:
width, height = list(map(int, dim_match.groups()))
else:
raise Exception("Malformed PFM header.")

scale = float(file.readline().decode("ascii").rstrip())
if scale < 0:
# little-endian
endian = "<"
scale = -scale
else:
# big-endian
endian = ">"

data = np.fromfile(file, endian + "f")
shape = (height, width, 3) if color else (height, width)

data = np.reshape(data, shape)
data = np.flipud(data)

return data, scale


def write_pfm(path, image, scale=1):
"""Write pfm file.

Args:
path (str): pathto file
image (array): data
scale (int, optional): Scale. Defaults to 1.
"""

with open(path, "wb") as file:
color = None

if image.dtype.name != "float32":
raise Exception("Image dtype must be float32.")

image = np.flipud(image)

if len(image.shape) == 3 and image.shape[2] == 3: # color image
color = True
elif (
len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1
): # greyscale
color = False
else:
raise Exception("Image must have H x W x 3, H x W x 1 or H x W dimensions.")

file.write("PF\n" if color else "Pf\n".encode())
file.write("%d %d\n".encode() % (image.shape[1], image.shape[0]))

endian = image.dtype.byteorder

if endian == "<" or endian == "=" and sys.byteorder == "little":
scale = -scale

file.write("%f\n".encode() % scale)

image.tofile(file)


def read_image(path):
"""Read image and output RGB image (0-1).

Args:
path (str): path to file

Returns:
array: RGB image (0-1)
"""
img = cv2.imread(path)

if img.ndim == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0

return img


def resize_image(img):
"""Resize image and make it fit for network.

Args:
img (array): image

Returns:
tensor: data ready for network
"""
height_orig = img.shape[0]
width_orig = img.shape[1]
unit_scale = 384.

if width_orig > height_orig:
scale = width_orig / unit_scale
else:
scale = height_orig / unit_scale

height = (np.ceil(height_orig / scale / 32) * 32).astype(int)
width = (np.ceil(width_orig / scale / 32) * 32).astype(int)

img_resized = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)

img_resized = (
torch.from_numpy(np.transpose(img_resized, (2, 0, 1))).contiguous().float()
)
img_resized = img_resized.unsqueeze(0)

return img_resized


def resize_depth(depth, width, height):
"""Resize depth map and bring to CPU (numpy).

Args:
depth (tensor): depth
width (int): image width
height (int): image height

Returns:
array: processed depth
"""
depth = torch.squeeze(depth[0, :, :, :]).to("cpu")
depth = cv2.blur(depth.numpy(), (3, 3))
depth_resized = cv2.resize(
depth, (width, height), interpolation=cv2.INTER_AREA
)

return depth_resized

def write_depth(path, depth, bits=1):
"""Write depth map to pfm and png file.

Args:
path (str): filepath without extension
depth (array): depth
"""
# write_pfm(path + ".pfm", depth.astype(np.float32))

depth_min = depth.min()
depth_max = depth.max()

max_val = (2**(8*bits))-1

if depth_max - depth_min > np.finfo("float").eps:
out = max_val * (depth - depth_min) / (depth_max - depth_min)
else:
out = 0

if bits == 1:
cv2.imwrite(path + ".png", out.astype("uint8"))
elif bits == 2:
cv2.imwrite(path + ".png", out.astype("uint16"))

return
186 changes: 186 additions & 0 deletions MiDaS/monodepth_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
"""MonoDepthNet: Network for monocular depth estimation trained by mixing several datasets.
This file contains code that is adapted from
https://github.com/thomasjpfan/pytorch_refinenet/blob/master/pytorch_refinenet/refinenet/refinenet_4cascade.py
"""
import torch
import torch.nn as nn
from torchvision import models


class MonoDepthNet(nn.Module):
"""Network for monocular depth estimation.
"""

def __init__(self, path=None, features=256):
"""Init.

Args:
path (str, optional): Path to saved model. Defaults to None.
features (int, optional): Number of features. Defaults to 256.
"""
super().__init__()

resnet = models.resnet50(pretrained=False)

self.pretrained = nn.Module()
self.scratch = nn.Module()
self.pretrained.layer1 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu,
resnet.maxpool, resnet.layer1)

self.pretrained.layer2 = resnet.layer2
self.pretrained.layer3 = resnet.layer3
self.pretrained.layer4 = resnet.layer4

# adjust channel number of feature maps
self.scratch.layer1_rn = nn.Conv2d(256, features, kernel_size=3, stride=1, padding=1, bias=False)
self.scratch.layer2_rn = nn.Conv2d(512, features, kernel_size=3, stride=1, padding=1, bias=False)
self.scratch.layer3_rn = nn.Conv2d(1024, features, kernel_size=3, stride=1, padding=1, bias=False)
self.scratch.layer4_rn = nn.Conv2d(2048, features, kernel_size=3, stride=1, padding=1, bias=False)

self.scratch.refinenet4 = FeatureFusionBlock(features)
self.scratch.refinenet3 = FeatureFusionBlock(features)
self.scratch.refinenet2 = FeatureFusionBlock(features)
self.scratch.refinenet1 = FeatureFusionBlock(features)

# adaptive output module: 2 convolutions and upsampling
self.scratch.output_conv = nn.Sequential(nn.Conv2d(features, 128, kernel_size=3, stride=1, padding=1),
nn.Conv2d(128, 1, kernel_size=3, stride=1, padding=1),
Interpolate(scale_factor=2, mode='bilinear'))

# load model
if path:
self.load(path)

def forward(self, x):
"""Forward pass.

Args:
x (tensor): input data (image)

Returns:
tensor: depth
"""
layer_1 = self.pretrained.layer1(x)
layer_2 = self.pretrained.layer2(layer_1)
layer_3 = self.pretrained.layer3(layer_2)
layer_4 = self.pretrained.layer4(layer_3)

layer_1_rn = self.scratch.layer1_rn(layer_1)
layer_2_rn = self.scratch.layer2_rn(layer_2)
layer_3_rn = self.scratch.layer3_rn(layer_3)
layer_4_rn = self.scratch.layer4_rn(layer_4)

path_4 = self.scratch.refinenet4(layer_4_rn)
path_3 = self.scratch.refinenet3(path_4, layer_3_rn)
path_2 = self.scratch.refinenet2(path_3, layer_2_rn)
path_1 = self.scratch.refinenet1(path_2, layer_1_rn)

out = self.scratch.output_conv(path_1)

return out

def load(self, path):
"""Load model from file.

Args:
path (str): file path
"""
parameters = torch.load(path)

self.load_state_dict(parameters)


class Interpolate(nn.Module):
"""Interpolation module.
"""

def __init__(self, scale_factor, mode):
"""Init.

Args:
scale_factor (float): scaling
mode (str): interpolation mode
"""
super(Interpolate, self).__init__()

self.interp = nn.functional.interpolate
self.scale_factor = scale_factor
self.mode = mode

def forward(self, x):
"""Forward pass.

Args:
x (tensor): input

Returns:
tensor: interpolated data
"""
x = self.interp(x, scale_factor=self.scale_factor, mode=self.mode, align_corners=False)

return x


class ResidualConvUnit(nn.Module):
"""Residual convolution module.
"""

def __init__(self, features):
"""Init.

Args:
features (int): number of features
"""
super().__init__()

self.conv1 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=True)
self.conv2 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=False)
self.relu = nn.ReLU(inplace=True)

def forward(self, x):
"""Forward pass.

Args:
x (tensor): input

Returns:
tensor: output
"""
out = self.relu(x)
out = self.conv1(out)
out = self.relu(out)
out = self.conv2(out)

return out + x


class FeatureFusionBlock(nn.Module):
"""Feature fusion block.
"""

def __init__(self, features):
"""Init.

Args:
features (int): number of features
"""
super().__init__()

self.resConfUnit = ResidualConvUnit(features)

def forward(self, *xs):
"""Forward pass.

Returns:
tensor: output
"""
output = xs[0]

if len(xs) == 2:
output += self.resConfUnit(xs[1])

output = self.resConfUnit(output)
output = nn.functional.interpolate(output, scale_factor=2,
mode='bilinear', align_corners=True)

return output
Loading