data augmentation in FasterRCNN
IamExperimenting opened this issue · 1 comments
IamExperimenting commented
Hi @sizhky ,
Firstly, thanks for this wonderful books. Currently, I'm into chapter 8 Object detection. I would like to know how can I implement data augmentation in the below code. Could you please provide some example of implementing data augmentation in the FasterRCNN model.
from torch_snippets import *
from PIL import Image
import glob, numpy as np, cv2, warnings,random
warnings.filterwarnings('ignore')
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(42)
IMAGE_ROOT = 'images'
DF_RAW = pd.read_csv('train_labels.csv')
DF_RAW['image_id'] = DF_RAW['filename'].apply(lambda x: x.split('.')[0])
DF_RAW['labels'] = DF_RAW['class'].apply(lambda x: 1 if x=='car' else 0)
label2target = {l:t+1 for t,l in enumerate(DF_RAW['class'].unique())}
label2target['background'] = 0
target2label = {t:l for l,t in label2target.items()}
background_class = label2target['background']
num_classes = len(label2target)
def preprocess_image(img):
img = torch.tensor(img).permute(2,0,1)
return img.to(device).float()
class OpenDataset(torch.utils.data.Dataset):
def __init__(self, df, image_folder=IMAGE_ROOT):
self.root = image_folder
self.df = df
self.unique_images = df['image_id'].unique()
def __len__(self): return len(self.unique_images)
def __getitem__(self, ix):
image_id = self.unique_images[ix]
image_path = f'{self.root}/{image_id}.jpg'
img = Image.open(image_path).convert("RGB")
img = np.array(img)/255
df = self.df.copy()
df = df[df['image_id'] == image_id]
boxes = df[['xmin','ymin','xmax','ymax']].values
classes = df['class'].values
target = {}
target["boxes"] = torch.Tensor(boxes).float()
target["labels"] = torch.Tensor([label2target[i] for i in classes]).long()
img = preprocess_image(img)
return img, target
def collate_fn(self, batch):
return tuple(zip(*batch))
from sklearn.model_selection import train_test_split
trn_ids, val_ids = train_test_split(DF_RAW['image_id'].unique(), test_size=0.1, random_state=99)
trn_df, val_df = DF_RAW[DF_RAW['image_id'].isin(trn_ids)], DF_RAW[DF_RAW['image_id'].isin(val_ids)]
print(len(trn_df), len(val_df))
train_ds = OpenDataset(trn_df)
test_ds = OpenDataset(val_df)
train_loader = DataLoader(train_ds, batch_size=2, collate_fn=train_ds.collate_fn, drop_last=True,shuffle=True)
test_loader = DataLoader(test_ds, batch_size=2, collate_fn=test_ds.collate_fn, drop_last=True,shuffle=False)
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def get_model():
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
return model
# Defining training and validation functions for a single batch
def train_batch(inputs, model, optimizer):
model.train()
input, targets = inputs
input = list(image.to(device) for image in input)
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
optimizer.zero_grad()
losses = model(input, targets)
loss = sum(loss for loss in losses.values())
loss.backward()
optimizer.step()
return loss, losses
@torch.no_grad() # this will disable gradient computation in the function below
def validate_batch(inputs, model):
model.train() # to obtain the losses, model needs to be in train mode only. # #Note that here we are not defining the model's forward method
#and hence need to work per the way the model class is defined
input, targets = inputs
input = list(image.to(device) for image in input)
targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
optimizer.zero_grad()
losses = model(input, targets)
loss = sum(loss for loss in losses.values())
return loss, losses
model = get_model().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.005,
momentum=0.9, weight_decay=0.0005)
n_epochs = 5
log = Report(n_epochs)
for epoch in range(n_epochs):
_n = len(train_loader)
for ix, inputs in enumerate(train_loader):
loss, losses = train_batch(inputs, model, optimizer)
loc_loss, regr_loss, loss_objectness, loss_rpn_box_reg = \
[losses[k] for k in ['loss_classifier','loss_box_reg','loss_objectness','loss_rpn_box_reg']]
pos = (epoch + (ix+1)/_n)
log.record(pos, trn_loss=loss.item(), trn_loc_loss=loc_loss.item(),
trn_regr_loss=regr_loss.item(), trn_objectness_loss=loss_objectness.item(),
trn_rpn_box_reg_loss=loss_rpn_box_reg.item(), end='\r')
_n = len(test_loader)
for ix,inputs in enumerate(test_loader):
loss, losses = validate_batch(inputs, model)
loc_loss, regr_loss, loss_objectness, loss_rpn_box_reg = \
[losses[k] for k in ['loss_classifier','loss_box_reg','loss_objectness','loss_rpn_box_reg']]
pos = (epoch + (ix+1)/_n)
log.record(pos, val_loss=loss.item(), val_loc_loss=loc_loss.item(),
val_regr_loss=regr_loss.item(), val_objectness_loss=loss_objectness.item(),
val_rpn_box_reg_loss=loss_rpn_box_reg.item(), end='\r')
if (epoch+1)%(n_epochs//5)==0: log.report_avgs(epoch+1)
log.plot_epochs(['trn_loss','val_loss'])
from torchvision.ops import nms
def decode_output(output):
'convert tensors to numpy arrays'
bbs = output['boxes'].cpu().detach().numpy().astype(np.uint16)
labels = np.array([target2label[i] for i in output['labels'].cpu().detach().numpy()])
confs = output['scores'].cpu().detach().numpy()
ixs = nms(torch.tensor(bbs.astype(np.float32)), torch.tensor(confs), 0.05)
bbs, confs, labels = [tensor[ixs] for tensor in [bbs, confs, labels]]
if len(ixs) == 1:
bbs, confs, labels = [np.array([tensor]) for tensor in [bbs, confs, labels]]
return bbs.tolist(), confs.tolist(), labels.tolist()
model.eval()
model.eval()
for ix, (images, targets) in enumerate(test_loader):
if ix==6: break
images = [im for im in images]
outputs = model(images)
for ix, output in enumerate(outputs):
bbs, confs, labels = decode_output(output)
info = [f'{l}@{c:.2f}' for l,c in zip(labels, confs)]
print(info)
show(images[ix].cpu().permute(1,2,0), bbs=bbs, texts=labels, sz=5)
IamExperimenting commented
I figured it out. Thanks