Skip to content

Instantly share code, notes, and snippets.

View nilesh0109's full-sized avatar
🎯
Focusing

Nilesh Vijayrania nilesh0109

🎯
Focusing
View GitHub Profile
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
## Fisheye Transformation
def get_of_fisheye(height, width, center, magnitude):
@nilesh0109
nilesh0109 / BYOL.py
Last active January 2, 2021 13:19
BYOL imeplementation
import torch
from torch import nn
import torch.nn.functional as F
import copy
from torchvision import models
class BYOL(nn.Module):
def __init__(self, backbone: nn.Module, target_momentum=0.996):
super().__init__()
self.online_network = backbone
@nilesh0109
nilesh0109 / distributedDataParallel.py
Last active December 22, 2020 12:45
DistributedDataParallel pytorch
import argparse
import torch
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data import DataLoader
#prase the local_rank argument from command line for the current process
parser = argparse.ArgumentParser()
parser.add_argument("--local_rank", default=0, type=int)
args = parser.parse_args()
@nilesh0109
nilesh0109 / accumulate_gradients.py
Last active December 21, 2020 12:34
Accumulating gradients for large batch training on single GPU.
TARGET_BATCH_SIZE, BATCH_FIT_IN_MEMORY = 256, 32
accumulation_steps = int(TARGET_BATCH_SIZE / BATCH_FIT_IN_MEMORY)
network.zero_grad() # Reset gradients tensors
for i, (imgs, labels) in enumerate(dataloader):
preds = network(imgs) # Forward pass
loss = loss_function(preds, labels) # Compute loss function
loss = loss / accumulation_steps # Normalize our loss (if averaged)
loss.backward() # Backward pass
if (i+1) % accumulation_steps == 0: # Wait for several backward steps
@nilesh0109
nilesh0109 / multi_gpu_model.py
Last active December 22, 2020 15:11
Pytorch model split across different gpus
from torch import nn
class Network(nn.Module):
def __init__(self, split_gpus=False):
super().__init__()
self.module1 = ...
self.module2 = ...
self.split_gpus = split_gpus
if split_gpus: #considering only two gpus
self.module1.cuda(0)
@nilesh0109
nilesh0109 / get_inverse_pespective
Last active December 13, 2022 11:54
Homography Matrix
import cv2
import numpy as np
def get_inverse_pespective(perspective_matrix: np.array)-> np.array:
"""
This method calculates the inverse of prespective matrix by homography.
- Takes 4 random points on the floor plane(destination_plane) and calculates the corresponding points
on the camera image plane(src_plane) using perspective matrix.
- Calculates the Homography matrix to map any point in image plane to floor plane.
Parameters