Skip to content

Instantly share code, notes, and snippets.

View aiwithshekhar's full-sized avatar

aiwithshekhar

View GitHub Profile
class Convolution(Conv_Module):
def forward(self, img, ker):
self.out_h= int((self.img_h-self.ker_h)+(2*self.pad)/self.stride) +1 #output height with same padding(10).
self.out_w= int((self.img_w-self.ker_w)+(2*self.pad)/self.stride) +1 #output width with same padding(10).
pad_img=np.pad(img, ((0,0),(0,0),(1,1),(1,1)),mode='constant') #pad the input images with zeros around
i0=np.repeat(np.arange(self.ker_h), self.ker_h)
i1=np.repeat(np.arange(self.img_h), self.img_h)
j0=np.tile(np.arange(self.ker_w), self.ker_h)
class Conv_Module():
def __init__(self, img, ker, b, pad=1, stride=1): #initialization
self.img_b, self.img_d, self.img_h, self.img_w=img.shape
self.ker_b, self.ker_d, self.ker_h, self.ker_w=ker.shape
self.b, self.pad, self.stride, self.img, self.ker=b, pad, stride, img, ker
def __call__(self):
self.out=self.forward(self.img, self.ker) #forward method is called with images, kernel as input
return self.out
@aiwithshekhar
aiwithshekhar / inference.py
Created December 13, 2019 20:07
inference on validation dataset
test_dataloader=CarDataloader(df,img_fol,mask_fol,mean,std,'val',1,4)
ckpt_path='/media/shashank/CE7E082A7E080DC1/PycharmProjects/object_detection/model_newloss.pth'
device = torch.device("cuda")
model = smp.Unet("resnet18", encoder_weights=None, classes=1, activation=None)
model.to(device)
model.eval()
state = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
model.load_state_dict(state["state_dict"])
@aiwithshekhar
aiwithshekhar / trainer.py
Created December 13, 2019 19:50
training starts here
class Trainer(object):
def __init__(self,model):
self.num_workers=4
self.batch_size={'train':1, 'val':1}
self.accumulation_steps=4//self.batch_size['train']
self.lr=5e-4
self.num_epochs=10
self.phases=['train','val']
self.best_loss=float('inf')
self.device=torch.device("cuda:0")
@aiwithshekhar
aiwithshekhar / scores.py
Created December 13, 2019 19:35
calculate dice scores
'''calculates dice scores when Scores class for it'''
def dice_score(pred, targs):
pred = (pred>0).float()
return 2. * (pred*targs).sum() / (pred+targs).sum()
''' initialize a empty list when Scores is called, append the list with dice scores
for every batch, at the end of epoch calculates mean of the dice scores'''
class Scores:
def __init__(self, phase, epoch):
self.base_dice_scores = []
@aiwithshekhar
aiwithshekhar / return_dataloader.py
Created December 13, 2019 19:01
depending on the phase split the dataset & generate dataloaders
'''divide data into train and val and return the dataloader depending upon train or val phase.'''
def CarDataloader(df,img_fol,mask_fol,mean,std,phase,batch_size,num_workers):
df_train,df_valid=train_test_split(df, test_size=0.2, random_state=69)
df = df_train if phase=='train' else df_valid
for_loader=CarDataset(df, img_fol, mask_fol, mean, std, phase)
dataloader=DataLoader(for_loader, batch_size=batch_size, num_workers=num_workers, pin_memory=True)
return dataloader
@aiwithshekhar
aiwithshekhar / getting_datasets.py
Last active December 14, 2019 10:45
loading images & mask and use transformation
'''when dataloader request for samples using index it fetches input image and target mask,
apply transformation and returns it'''
class CarDataset(Dataset):
def __init__(self,df,img_fol,mask_fol,mean,std,phase):
self.fname=df['img'].values.tolist()
self.img_fol=img_fol
self.mask_fol=mask_fol
self.mean=mean
self.std=std
self.phase=phase
@aiwithshekhar
aiwithshekhar / transforms.py
Created December 13, 2019 18:57
create transforms for image and mask
# during traning/val phase make a list of transforms to be used.
# input-->"phase",mean,std
# output-->list
def get_transform(phase,mean,std):
list_trans=[]
if phase=='train':
list_trans.extend([HorizontalFlip(p=0.5)])
list_trans.extend([Normalize(mean=mean,std=std, p=1), ToTensor()]) #normalizing the data & then converting to tensors
list_trans=Compose(list_trans)
return list_trans
@aiwithshekhar
aiwithshekhar / data_conversion.py
Last active December 14, 2019 09:26
convert and resize the images
PATH = Path('/media/shashank/New Volume/carvana')
# using fastai below lines convert the gif image to pil image.
(PATH/'train_masks_png').mkdir(exist_ok=True)
def convert_img(fn):
fn = fn.name
PIL.Image.open(PATH/'train_masks'/fn).save(PATH/'train_masks_png'/f'{fn[:-4]}.png') #opening and saving image
files = list((PATH/'train_masks').iterdir())
with concurrent.futures.ThreadPoolExecutor(8) as e: e.map(convert_img, files) #uses multi thread for fast conversion
@aiwithshekhar
aiwithshekhar / imports.py
Last active December 14, 2019 09:24
starting with all the libraries to import
# visualization library
import cv2
from matplotlib import pyplot as plt
# data storing library
import numpy as np
import pandas as pd
# torch libraries
from torch.optim.lr_scheduler import ReduceLROnPlateau
import torch
import torch.nn as nn