This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |