Skip to content

Commit

Permalink
shekhatr
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragGUPTA2235 committed Jun 16, 2024
1 parent c7edccb commit d61285e
Show file tree
Hide file tree
Showing 81 changed files with 4,514 additions and 111 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.18
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.10"
Binary file removed __pycache__/constructer.cpython-310.pyc
Binary file not shown.
Binary file removed __pycache__/constructer.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/constructer.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/flops_returner.cpython-310.pyc
Binary file not shown.
Binary file removed __pycache__/genome_to_surrinput.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/ret_encoder.cpython-310.pyc
Binary file not shown.
Binary file removed __pycache__/ret_encoder.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/ret_encoder.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/returngenes.cpython-310.pyc
Binary file not shown.
Binary file removed __pycache__/surrogates_fit.cpython-38.pyc
Binary file not shown.
Binary file added assets/annnot_img_0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/annnot_img_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkpoints.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code/1.pdf
Binary file not shown.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
Binary file not shown.
File renamed without changes.
Binary file not shown.
File renamed without changes.
30 changes: 30 additions & 0 deletions code/colour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import matplotlib.colors as mcolors

# Given color list
label_clrs = [
"#ff0000", "#2e8b57", "#808000", "#800000", "#000080", "#2f4f4f", "#ffa500",
"#00ff00", "#ba55d3", "#00fa9a", "#00ffff", "#0000ff", "#f08080", "#ff00ff",
"#1e90ff", "#ffff54", "#dda0dd", "#ff1493", "#87cefa", "#ffe4c4"
]

# Number of colors needed
num_colors_needed = 91

# Calculate the number of times to repeat the original list
repeat_times = num_colors_needed // len(label_clrs) + 1

# Repeat the original list
extended_clrs = label_clrs * repeat_times

# Get evenly spaced colors from the extended list
extended_clrs = extended_clrs[:num_colors_needed]

# Check if there are any duplicate colors
if len(set(extended_clrs)) != len(extended_clrs):
# If duplicates exist, generate additional colors
additional_colors_needed = num_colors_needed - len(extended_clrs)
additional_colors = list(mcolors.CSS4_COLORS.keys())[:additional_colors_needed]
extended_clrs.extend(additional_colors)

# Display the extended list of colors
print(extended_clrs)
79 changes: 79 additions & 0 deletions code/da.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import torch as th
from torch.utils.data import Dataset
import os
import PIL.Image as Image
import csv
from typing import Callable, Optional, Tuple, Union, List


class VOC_Detection(Dataset):
"""
A custom Dataset for the VOC Detection data. An index number (starting from 0) and a color is assigned to each of
the labels of the dataset.
"""
C = 20

index2label = ["person",
"bird", "cat", "cow", "dog", "horse", "sheep",
"aeroplane", "bicycle", "boat", "bus", "car", "motorbike", "train",
"bottle", "chair", "diningtable", "pottedplant", "sofa", "tvmonitor"]

label2index = {label: index for index, label in enumerate(index2label)}

label_clrs = ["#ff0000",
"#2e8b57", "#808000", "#800000", "#000080", "#2f4f4f", "#ffa500",
"#00ff00", "#ba55d3", "#00fa9a", "#00ffff", "#0000ff", "#f08080", "#ff00ff",
"#1e90ff", "#ffff54", "#dda0dd", "#ff1493", "#87cefa", "#ffe4c4"]

def __init__(self, root_dir: str, split: str = 'train',
transforms: Optional[Callable] = None) -> None:
""" Initialize the VOC_Detection Dataset object.
:param root_dir: The root directory of the dataset (this directory contains two directories 'train/' and
'test/'.
:param split: The split of the dataset ('train' or 'test')
:param transforms: The transforms that are applied to the images (x) and their corresponding targets (y).
"""

assert split == 'train' or split == 'test'
split_dir = os.path.join(root_dir, split)

self.img_dir = os.path.join(split_dir, "images")
self.annot_dir = os.path.join(split_dir, "targets")
self.pseudonyms = [filename[:-4] for filename in os.listdir(self.annot_dir)]

self.transforms = transforms

def __len__(self) -> int:
"""
Return the total number of instances of the dataset.
:return: total instances of the dataset
"""
return len(self.pseudonyms)

def __getitem__(self, idx: int) -> Tuple[Union[th.Tensor, Image.Image], th.Tensor]:
"""
Given an index number in range [0, dataset's length) , return the corresponding image and target of the dataset.
If transforms is defined, the images and their targets are first transformed and then return by the function.
:param idx: The given index number
:return: The (x,y)-pair of the image and the target
"""
pid = self.pseudonyms[idx]
img_path = os.path.join(self.img_dir, f'{pid}.jpg')
annot_path = os.path.join(self.annot_dir, f'{pid}.csv')

img = Image.open(img_path)
target = []
with open(annot_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader) # Remove the header
for row in csv_reader:
target.append([self.label2index[row[0]]] + [int(row[i]) for i in range(1, 5)])
target = th.Tensor(target)

if self.transforms is not None:
img, target = self.transforms((img, target))

return img, target
73 changes: 73 additions & 0 deletions code/dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import torch as th
from torch.utils.data import Dataset
import os
import PIL.Image as Image
import csv
from typing import Callable, Optional, Tuple, Union, List


class VOC_Detection(Dataset):
"""
A custom Dataset for the VOC Detection data. An index number (starting from 0) and a color is assigned to each of
the labels of the dataset.
"""
C = 91

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

label2index = {label: index for index, label in enumerate(index2label)}

label_clrs = ['#ff0000', '#2e8b57', '#808000', '#800000', '#000080', '#2f4f4f', '#ffa500', '#00ff00', '#ba55d3', '#00fa9a', '#00ffff', '#0000ff', '#f08080', '#ff00ff', '#1e90ff', '#ffff54', '#dda0dd', '#ff1493', '#87cefa', '#ffe4c4', '#ff0000', '#2e8b57', '#808000', '#800000', '#000080', '#2f4f4f', '#ffa500', '#00ff00', '#ba55d3', '#00fa9a', '#00ffff', '#0000ff', '#f08080', '#ff00ff', '#1e90ff', '#ffff54', '#dda0dd', '#ff1493', '#87cefa', '#ffe4c4', '#ff0000', '#2e8b57', '#808000', '#800000', '#000080', '#2f4f4f', '#ffa500', '#00ff00', '#ba55d3', '#00fa9a', '#00ffff', '#0000ff', '#f08080', '#ff00ff', '#1e90ff', '#ffff54', '#dda0dd', '#ff1493', '#87cefa', '#ffe4c4', '#ff0000', '#2e8b57', '#808000', '#800000', '#000080', '#2f4f4f', '#ffa500', '#00ff00', '#ba55d3', '#00fa9a', '#00ffff', '#0000ff', '#f08080', '#ff00ff', '#1e90ff', '#ffff54', '#dda0dd', '#ff1493', '#87cefa', '#ffe4c4', '#ff0000', '#2e8b57', '#808000', '#800000', '#000080', '#2f4f4f', '#ffa500', '#00ff00', '#ba55d3', '#00fa9a', '#00ffff']

def __init__(self, root_dir: str, split: str = 'train',
transforms: Optional[Callable] = None) -> None:
""" Initialize the VOC_Detection Dataset object.
:param root_dir: The root directory of the dataset (this directory contains two directories 'train/' and
'test/'.
:param split: The split of the dataset ('train' or 'test')
:param transforms: The transforms that are applied to the images (x) and their corresponding targets (y).
"""

assert split == 'train' or split == 'test'
split_dir = os.path.join(root_dir, split)

self.img_dir = os.path.join(split_dir, "images")
self.annot_dir = os.path.join(split_dir, "targets")
self.pseudonyms = [filename[:-4] for filename in os.listdir(self.annot_dir)]

self.transforms = transforms

def __len__(self) -> int:
"""
Return the total number of instances of the dataset.
:return: total instances of the dataset
"""
return len(self.pseudonyms)

def __getitem__(self, idx: int) -> Tuple[Union[th.Tensor, Image.Image], th.Tensor]:
"""
Given an index number in range [0, dataset's length) , return the corresponding image and target of the dataset.
If transforms is defined, the images and their targets are first transformed and then return by the function.
:param idx: The given index number
:return: The (x,y)-pair of the image and the target
"""
pid = self.pseudonyms[idx]
img_path = os.path.join(self.img_dir, f'{pid}.jpg')
annot_path = os.path.join(self.annot_dir, f'{pid}.csv')

img = Image.open(img_path)
target = []
with open(annot_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader) # Remove the header
for row in csv_reader:
target.append([self.label2index[row[0]]] + [int(row[i]) for i in range(1, 5)])
target = th.Tensor(target)

if self.transforms is not None:
img, target = self.transforms((img, target))

return img, target
104 changes: 104 additions & 0 deletions code/ddp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import os
from datetime import datetime
import argparse
import torch.multiprocessing as mp
import torchvision
import torchvision.transforms as transforms
import torch
import torch.nn as nn
import torch.distributed as dist
from apex.parallel import DistributedDataParallel as DDP
from apex import amp


def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--nodes', default=1, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('-g', '--gpus', default=1, type=int,
help='number of gpus per node')
parser.add_argument('-nr', '--nr', default=0, type=int,
help='ranking within the nodes')
parser.add_argument('--epochs', default=2, type=int, metavar='N',
help='number of total epochs to run')
args = parser.parse_args()
args.world_size = args.gpus * args.nodes
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '8888'
mp.spawn(train, nprocs=args.gpus, args=(args,))


class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc = nn.Linear(7*7*32, num_classes)

def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out


def train(gpu, args):
rank = args.nr * args.gpus + gpu
dist.init_process_group(backend='nccl', init_method='env://', world_size=args.world_size, rank=rank)
torch.manual_seed(0)
model = ConvNet()
torch.cuda.set_device(gpu)
model.cuda(gpu)
batch_size = 100
# define loss function (criterion) and optimizer
criterion = nn.CrossEntropyLoss().cuda(gpu)
optimizer = torch.optim.SGD(model.parameters(), 1e-4)
# Wrap the model
model = nn.parallel.DistributedDataParallel(model, device_ids=[gpu])
# Data loading code
train_dataset = torchvision.datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset,
num_replicas=args.world_size,
rank=rank)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=True,
sampler=train_sampler)

start = datetime.now()
total_step = len(train_loader)
for epoch in range(args.epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.cuda(non_blocking=True)
labels = labels.cuda(non_blocking=True)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0 and gpu == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, args.epochs, i + 1, total_step,
loss.item()))
if gpu == 0:
print("Training complete in: " + str(datetime.now() - start))


if __name__ == '__main__':
main()
Loading

0 comments on commit d61285e

Please sign in to comment.