Skip to content

Commit

Permalink
#23: moidifed dataloader.py and dependency code
Browse files Browse the repository at this point in the history
 1. move detection_collate function utils.py to dataloader.py
 2. class selective option in convert2Yolo/Format.py
  • Loading branch information
ssaru committed Sep 28, 2018
1 parent f026dc4 commit fd4b200
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 144 deletions.
19 changes: 4 additions & 15 deletions pytorch_version/convertYolo/Format.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def save(xml_list, path):
return False, msg

@staticmethod
def parse(path, cls_option=False, selective_cls=None):
def parse(path):
try:

(dir_path, dir_names, filenames) = next(os.walk(os.path.abspath(path)))
Expand Down Expand Up @@ -264,15 +264,9 @@ def parse(path, cls_option=False, selective_cls=None):
}
tmp["bndbox"] = bndbox

if (cls_option==True) and (selective_cls != None):
if _object.find("name").text != selective_cls:
continue
else:
obj[str(obj_index)] = tmp
obj_index += 1
else:
obj[str(obj_index)] = tmp
obj_index += 1

obj[str(obj_index)] = tmp
obj_index += 1

annotation = {
"size": size,
Expand All @@ -281,11 +275,6 @@ def parse(path, cls_option=False, selective_cls=None):

if obj_index != 0:

if cls_option==True:
annotation["objects"]["num_obj"] = obj_index
#else:
#annotation["objects"]["num_obj"] = obj_index

data[filename.split(".")[0]] = annotation

printProgressBar(progress_cnt + 1, progress_length, prefix='VOC Parsing:'.ljust(15), suffix='Complete', length=40)
Expand Down
145 changes: 63 additions & 82 deletions pytorch_version/utilities/dataloader.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
import sys, os

sys.path.insert(0, os.path.dirname(__file__))
import sys
import os

import torch
import torchvision
import torch.utils.data as data
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

import numpy as np
from PIL import Image

from convertYolo.Format import YOLO as cvtYOLO
from convertYolo.Format import VOC as cvtVOC
"""
try:
from convertYolo.Format import YOLO as cvtYOLO
from convertYolo.Format import VOC as cvtVOC
except Exception: # ImportError
import convertYolo.Format as Format
cvtYOLO = Format.YOLO
cvtVOC = Format.VOC
"""

sys.path.insert(0, os.path.dirname(__file__))

def detection_collate(batch):
""" `Puts each data field into a tensor with outer dimension batch size`
Args:
batch : batch data ``batch[0]`` : image, ``batch[1]`` : label, ``batch[3]`` : size
Return:
image tensor, label tensor, sizes
Future work:
return value(torch.stack) change to Torch.FloatTensor()
"""

targets = []
imgs = []
sizes = []

for sample in batch:
imgs.append(sample[0])

# for drawing box
# if using batch it should keep original image size.
sizes.append(sample[2])

np_label = np.zeros((7, 7, 6), dtype=np.float32)
for object in sample[1]:
objectness = 1
cls = object[0]
x_ratio = object[1]
y_ratio = object[2]
w_ratio = object[3]
h_ratio = object[4]

# can be acuqire grid (x,y) index when divide (1/S) of x_ratio
scale_factor = (1 / 7)
grid_x_index = int(x_ratio // scale_factor)
grid_y_index = int(y_ratio // scale_factor)
x_offset = (x_ratio / scale_factor) - grid_x_index
y_offset = (y_ratio / scale_factor) - grid_y_index

# insert object row in specific label tensor index as (x,y)
# object row follow as
# [objectness, class, x offset, y offset, width ratio, height ratio]
np_label[grid_x_index][grid_y_index] = np.array([objectness, cls, x_offset, y_offset, w_ratio, h_ratio])

label = torch.from_numpy(np_label)
targets.append(label)

return torch.stack(imgs, 0), torch.stack(targets, 0), sizes

class VOC(data.Dataset):
""" `VOC PASCAL Object Detection Challenge <http://host.robots.ox.ac.uk/pascal/VOC/voc2012/>_ Dataset `
Expand All @@ -39,23 +79,18 @@ class VOC(data.Dataset):
"""

#CLASSES = "./voc.names"
#CLASSES = "./person.names"
CLASSES = "./5class.names"
IMAGE_FOLDER = "JPEGImages"
LABEL_FOLDER = "Annotations"
IMG_EXTENSIONS = '.jpg'

def __init__(self, root, train=True, transform=None, target_transform=None, resize=448, cls_option=False, selective_cls=None):
def __init__(self, root, train=True, transform=None, target_transform=None, resize=448, class_path='./voc.names'):
self.root = root
self.transform = transform
self.target_transform = target_transform
self.train = train
self.resize_factor = resize
self.cls_option = cls_option
self.selective_cls = selective_cls

with open("./5class.names") as f:
with open(class_path) as f:
self.classes = f.read().splitlines()

if not self._check_exists():
Expand All @@ -75,10 +110,8 @@ def cvtData(self):

result = []
voc = cvtVOC()
yolo = cvtYOLO(os.path.abspath(self.CLASSES))
flag, self.dict_data =voc.parse(os.path.join(self.root, self.LABEL_FOLDER), cls_option=self.cls_option, selective_cls=self.selective_cls)
#print(flag, data)
#exit()
yolo = cvtYOLO(os.path.abspath(self.classes))
flag, self.dict_data =voc.parse(os.path.join(self.root, self.LABEL_FOLDER))

try:

Expand All @@ -105,9 +138,11 @@ def cvtData(self):
except Exception as e:
raise RuntimeError("Error : {}".format(e))


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


def __getitem__(self, index):
"""
Args:
Expand All @@ -125,70 +160,17 @@ def __getitem__(self, index):
]
"""


key = list(self.data[index].keys())[0]

img = Image.open(key).convert('RGB')

current_shape = img.size

img = img.resize((self.resize_factor, self.resize_factor))
img = np.array(img.getdata(), dtype=np.float).reshape(img.size[0], img.size[1], 3)

target = self.data[index][key]

# for debug & visualization
"""
_image = img.astype(dtype=np.uint8)
_image = Image.fromarray(_image, "RGB")
draw = ImageDraw.Draw(_image)
for obj in target:
cls = obj[0]
x_center = obj[1]
y_center = obj[2]
w_ratio = obj[3]
h_ratio = obj[4]
width = int(w_ratio * self.resize_factor)
height = int(h_ratio * self.resize_factor)
xmin = int(x_center * self.resize_factor - width/2)
ymin = int(y_center * self.resize_factor - height/2)
xmax = xmin + width
ymax = ymin + height
cls = self.classes[int(cls)]
draw.rectangle(((xmin, ymin), (xmax, ymax)), outline="blue")
draw.text((xmin, ymin), cls)
plt.imshow(_image)
plt.show()
"""


if self.transform is not None:
#img, target = self.transform([img, target])
#img = torchvision.transforms.ToTensor()(img)
img = img.astype(dtype=np.uint8)
print(img.shape)
print(img.dtype)
plt.imshow(img)
plt.show()
img = img.astype(dtype=np.float64)
img = self.transform(img)
print(img.shape)
image = img.type(torch.ByteTensor).view(448,448,3)
image = image.cpu().numpy()
plt.imshow(image)
plt.show()
print("It's not supported, transform parameter should be None")
exit()


else:
img = torch.FloatTensor(img)
img = torch.div(img, 255)
Expand All @@ -197,5 +179,4 @@ def __getitem__(self, index):
# Future works
pass


return img, target
return img, target, current_shape
47 changes: 0 additions & 47 deletions pytorch_version/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,6 @@ def one_hot(output , label):

return torch.from_numpy(dst)


def detection_collate_with_size(batch):
r"""Puts each data field into a tensor with outer dimension batch size"""
targets = []
imgs = []
sizes = []
#print(len(batch[0]))
for sample in batch:
imgs.append(sample[0])
#sizes.append(sample[2])

#Add Size
#shape_np = sample[2]
#shape = torch.Tensor(shape_np)
sizes.append(sample[2])

np_label = np.zeros((7,7,6), dtype=np.float32)
for i in range(7):
for j in range(7):
np_label[i][j][1] = (num_classes-1)

for object in sample[1]:
objectness=1
#print(len(object))
cls = object[0]
x_ratio = object[1]
y_ratio = object[2]
w_ratio = object[3]
h_ratio = object[4]

# can be acuqire grid (x,y) index when divide (1/S) of x_ratio
grid_x_index = int(x_ratio // (1/7))
grid_y_index = int(y_ratio // (1/7))
x_offset = x_ratio - ((grid_x_index) * (1/7))
y_offset = y_ratio - ((grid_y_index) * (1/7))

# insert object row in specific label tensor index as (x,y)
# object row follow as
# [objectness, class, x offset, y offset, width ratio, height ratio]
np_label[grid_x_index-1][grid_y_index-1] = np.array([objectness, cls, x_offset, y_offset, w_ratio, h_ratio])

label = torch.from_numpy(np_label)
targets.append(label)

return torch.stack(imgs,0), torch.stack(targets, 0), sizes


# visdom function

def create_vis_plot(viz, _xlabel, _ylabel, _title, _legend):
Expand Down

0 comments on commit fd4b200

Please sign in to comment.