Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sacmehta committed Jun 7, 2019
1 parent 9f138f0 commit 5067df1
Show file tree
Hide file tree
Showing 151 changed files with 8,862 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
__pycache__
.DS_STORE
.idea
19 changes: 19 additions & 0 deletions Instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Install Conda
wget https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh
bash Anaconda3-5.3.1-Linux-x86_64.sh
rm -rf Anaconda3-5.3.1-Linux-x86_64.sh

# down grade to 3.6 because Tensorflow (as of 19 Dec, 2018) does not support v3.7
conda install python=3.6
conda install pip

#Install Pytorch
#conda install pytorch torchvision -c pytorch
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch


#install thre requirements
pip install -r requirements.txt

# Install opencv
conda install -c anaconda opencv
2 changes: 2 additions & 0 deletions Instructions_NMS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd build
python build.py build_ext develop
Empty file added commons/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions commons/general_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ============================================
__author__ = "Sachin Mehta"
__maintainer__ = "Sachin Mehta"
# ============================================

# classification related details
classification_datasets = ['imagenet', 'coco']
classification_schedulers = ['fixed', 'clr', 'hybrid', 'linear', 'poly']
classification_models = ['shuffle_dw', 'dicenet']
classification_exp_choices = ['main', 'ablation']

# segmentation related details
segmentation_schedulers = ['poly', 'fixed', 'clr', 'linear', 'hybrid']
segmentation_datasets = ['pascal', 'city']
segmentation_models = ['espnet', 'dicenet']
segmentation_loss_fns = ['ce', 'bce']


# detection related details

detection_datasets = ['coco', 'voc']
detection_models = ['espnet', 'dicenet']
detection_schedulers = ['poly', 'hybrid', 'clr', 'cosine']
5 changes: 5 additions & 0 deletions data_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#============================================
__author__ = "Sachin Mehta"
__license__ = "MIT"
__maintainer__ = "Sachin Mehta"
#============================================
Empty file.
91 changes: 91 additions & 0 deletions data_loader/classification/coco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# ============================================
__author__ = "Sachin Mehta"
__maintainer__ = "Sachin Mehta"
# ============================================

import os
import torch.utils.data
import numpy as np
from PIL import Image
from pycocotools.coco import COCO
from torchvision.transforms import Compose, RandomResizedCrop, RandomHorizontalFlip, ToTensor, Normalize, Resize
from transforms.classification.data_transforms import MEAN, STD
from torch.utils import data

COCO_CLASS_LIST = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire', 'hydrant',
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush'
]


class COCOClassification(data.Dataset):
def __init__(self, root, split='train', year='2017', inp_size=224, scale=(0.2, 1.0), is_training=True):
super(COCOClassification, self).__init__()
ann_file = os.path.join(root, 'annotations/instances_{}{}.json'.format(split, year))
self.root = root
self.img_dir = os.path.join(root, 'images/{}{}'.format(split, year))
self.coco = COCO(ann_file)
self.ids = list(self.coco.imgs.keys())
self.cat2cat = dict()
for cat in self.coco.cats.keys():
self.cat2cat[cat] = len(self.cat2cat)
self.transform = self.transforms(inp_size=inp_size, inp_scale=scale, is_training=is_training)


def transforms(self, inp_size, inp_scale, is_training):

if is_training:
return Compose(
[
RandomResizedCrop(inp_size, scale=inp_scale),
RandomHorizontalFlip(),
ToTensor(),
Normalize(mean=MEAN, std=STD)
]
)
else:
return Compose(
[
Resize(size=(inp_size, inp_size)),
ToTensor(),
Normalize(mean=MEAN, std=STD)
]
)

def __len__(self):
return len(self.ids)

def __getitem__(self, index):
coco = self.coco
img_id = self.ids[index]
ann_ids = coco.getAnnIds(imgIds=img_id)
target = coco.loadAnns(ann_ids)

output = torch.zeros((3, len(COCO_CLASS_LIST)), dtype=torch.long)
for obj in target:
if obj['area'] < 32 * 32:
output[0][self.cat2cat[obj['category_id']]] = 1
elif obj['area'] < 96 * 96:
output[1][self.cat2cat[obj['category_id']]] = 1
else:
output[2][self.cat2cat[obj['category_id']]] = 1
target = output

path = coco.loadImgs(img_id)[0]['file_name']
img = Image.open(os.path.join(self.img_dir, path)).convert('RGB')
if self.transform is not None:
img = self.transform(img)

return img, target
55 changes: 55 additions & 0 deletions data_loader/classification/imagenet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#============================================
__author__ = "Sachin Mehta"
__maintainer__ = "Sachin Mehta"
#============================================
import os
import torch
import torchvision.datasets as datasets
from torchvision import transforms
from transforms.classification.data_transforms import Lighting, normalize


def train_transforms(inp_size, scale):
return transforms.Compose([
transforms.RandomResizedCrop(inp_size, scale=scale),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])


def val_transforms(inp_size):
return transforms.Compose([
transforms.Resize(int(inp_size / 0.875)),
transforms.CenterCrop(inp_size),
transforms.ToTensor(),
normalize,
])

#helper function for the loading the training data
def train_loader(args):
traindir = os.path.join(args.data, 'train')
train_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(traindir, train_transforms(args.inpSize, scale=args.scale)),
batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True)

return train_loader


#helper function for the loading the validation data
def val_loader(args):
valdir = os.path.join(args.data, 'val')
val_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(valdir, val_transforms(args.inpSize)),
batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True)

return val_loader

# helped function for loading trianing and validation data
def data_loaders(args):
tr_loader = train_loader(args)
vl_loader = val_loader(args)
return tr_loader, vl_loader

Empty file.
71 changes: 71 additions & 0 deletions data_loader/detection/augmentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# from https://github.com/amdegroot/ssd.pytorch

from transforms.detection.data_transforms import ConvertFromInts, PhotometricDistort, Expand, RandomSampleCrop, \
RandomFlipping, ToPercentCoords, Resize, SubtractMeans, ToTensor, Compose


class TrainTransform:
'''
Transformation for training set
'''
def __init__(self, size):
"""
Args:
size: the size the of final image.
"""
self.size = size
self.augment = Compose([
ConvertFromInts(),
PhotometricDistort(),
Expand(),
RandomSampleCrop(),
RandomFlipping(),
ToPercentCoords(),
Resize(self.size),
SubtractMeans(),
ToTensor(),
])

def __call__(self, img, boxes, labels):
"""
Args:
img: the output of cv.imread in RGB layout.
boxes: boundding boxes in the form of (x1, y1, x2, y2).
labels: labels of boxes.
"""
return self.augment(img, boxes, labels)


class ValTransform:
'''
Transformation for validation set
'''

def __init__(self, size):
self.transform = Compose([
ToPercentCoords(),
Resize(size),
SubtractMeans(),
ToTensor(),
])

def __call__(self, image, boxes, labels):
return self.transform(image, boxes, labels)


class TestTransform:
'''
Transformation for test set
'''

def __init__(self, size):
self.transform = Compose([
Resize(size),
SubtractMeans(),
ToTensor()
])

def __call__(self, image):
image, _, _ = self.transform(image)
return image
103 changes: 103 additions & 0 deletions data_loader/detection/coco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# ============================================
__author__ = "Sachin Mehta"
__maintainer__ = "Sachin Mehta"
# ============================================

import os
import torch.utils.data
import numpy as np
from PIL import Image
from pycocotools.coco import COCO

COCO_CLASS_LIST = ['__background__',
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire', 'hydrant',
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush'
]

class COCOObjectDetection(torch.utils.data.Dataset):

def __init__(self, root_dir, transform=None, target_transform=None, is_training=True):
super(COCOObjectDetection, self).__init__()
if is_training:
split = 'train'
year = '2017'
else:
split = 'val'
year = '2017'
ann_file = os.path.join(root_dir, 'annotations/instances_{}{}.json'.format(split, year))
self.coco = COCO(ann_file)
self.root_dir = root_dir
self.img_dir = os.path.join(root_dir, 'images/{}{}'.format(split, year))
self.transform = transform
self.target_transform = target_transform
if is_training:
#Remove image IDS that don't have annotations from training set
self.ids = list(self.coco.imgToAnns.keys())
else:
# use all images from Validation Set
self.ids = list(self.coco.imgs.keys())
coco_categories = sorted(self.coco.getCatIds())
self.coco_id_to_contiguous_id = {coco_id: i + 1 for i, coco_id in enumerate(coco_categories)}
self.contiguous_id_to_coco_id = {v: k for k, v in self.coco_id_to_contiguous_id.items()}
self.CLASSES = COCO_CLASS_LIST

def __getitem__(self, index):
img_id = self.ids[index]
boxes, labels = self._get_annotation(img_id)
image = self._get_image(img_id)
if self.transform:
image, boxes, labels = self.transform(image, boxes, labels)
if self.target_transform:
boxes, labels = self.target_transform(boxes, labels)
return image, boxes, labels

def get_image(self, index):
image_id = self.ids[index]
image = self._get_image(image_id)
if self.transform:
image, _ = self.transform(image)
return image

def get_annotation(self, index):
image_id = self.ids[index]
return image_id, self._get_annotation(image_id)

def __len__(self):
return len(self.ids)

def _get_annotation(self, image_id):
ann_ids = self.coco.getAnnIds(imgIds=image_id)
ann = self.coco.loadAnns(ann_ids)
# filter crowd annotations
ann = [obj for obj in ann if obj["iscrowd"] == 0]
boxes = np.array([self._xywh2xyxy(obj["bbox"]) for obj in ann], np.float32).reshape((-1, 4))
labels = np.array([self.coco_id_to_contiguous_id[obj["category_id"]] for obj in ann], np.int64).reshape((-1,))
# remove invalid boxes
keep = (boxes[:, 3] > boxes[:, 1]) & (boxes[:, 2] > boxes[:, 0])
boxes = boxes[keep]
labels = labels[keep]
return boxes, labels

def _xywh2xyxy(self, box):
x1, y1, w, h = box
return [x1, y1, x1 + w, y1 + h]

def _get_image(self, image_id):
file_name = self.coco.loadImgs(image_id)[0]['file_name']
image_file = os.path.join(self.img_dir, file_name)
image = Image.open(image_file).convert("RGB")
image = np.array(image)
return image
Loading

0 comments on commit 5067df1

Please sign in to comment.