parent
fa754db940
commit
6c376d8139
@ -0,0 +1,40 @@
|
|||||||
|
import glob
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
from torch.utils.data import Dataset, DataLoader
|
||||||
|
|
||||||
|
|
||||||
|
class CustomDataset(Dataset):
|
||||||
|
def __init__(self):
|
||||||
|
self.imgs_path = "Dog_Cat_Dataset/"
|
||||||
|
file_list = glob.glob(self.imgs_path + "*")
|
||||||
|
print(file_list)
|
||||||
|
self.data = []
|
||||||
|
for class_path in file_list:
|
||||||
|
class_name = class_path.split("/")[-1]
|
||||||
|
for img_path in glob.glob(class_path + "/*.jpeg"):
|
||||||
|
self.data.append([img_path, class_name])
|
||||||
|
print(self.data)
|
||||||
|
self.class_map = {"dogs" : 0, "cats": 1}
|
||||||
|
self.img_dim = (416, 416)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.data)
|
||||||
|
|
||||||
|
def __getitem__(self, idx):
|
||||||
|
img_path, class_name = self.data[idx]
|
||||||
|
img = cv2.imread(img_path)
|
||||||
|
img = cv2.resize(img, self.img_dim)
|
||||||
|
class_id = self.class_map[class_name]
|
||||||
|
img_tensor = torch.from_numpy(img)
|
||||||
|
img_tensor = img_tensor.permute(2, 0, 1)
|
||||||
|
class_id = torch.tensor([class_id])
|
||||||
|
return img_tensor, class_id
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
dataset = CustomDataset()
|
||||||
|
data_loader = DataLoader(dataset, batch_size=4, shuffle=True)
|
||||||
|
for imgs, labels in data_loader:
|
||||||
|
print("Batch of images has shape: ",imgs.shape)
|
||||||
|
print("Batch of labels has shape: ", labels.shape)
|
@ -0,0 +1,60 @@
|
|||||||
|
from torch.utils.data import Dataset, DataLoader
|
||||||
|
import os
|
||||||
|
import torch
|
||||||
|
import glob
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
class TextDataset(Dataset):
|
||||||
|
def __init__(self, dir_path, split):
|
||||||
|
self.path = dir_path
|
||||||
|
self.split = split
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
count = 0
|
||||||
|
for root_dir, cur_dir, files in os.walk(self.path):
|
||||||
|
count += len(files)
|
||||||
|
#print('file count:', count)
|
||||||
|
count = count*32
|
||||||
|
return count
|
||||||
|
|
||||||
|
def __getitem__(self, idx):
|
||||||
|
# index sequentially as per file list
|
||||||
|
|
||||||
|
# Go to file idx//32
|
||||||
|
# Get label(1x1) based on file name
|
||||||
|
# Get vector(1x4096) at idx%32 in the file
|
||||||
|
#return a tensor x*y (x*y = 4096) and target tensor (1,) //Use x,y =64
|
||||||
|
|
||||||
|
|
||||||
|
def listdir_nohidden(AllVideos_Path): # To ignore hidden files
|
||||||
|
file_dir_extension = os.path.join(AllVideos_Path, '*.txt')
|
||||||
|
for f in glob.glob(file_dir_extension):
|
||||||
|
if not f.startswith('.'):
|
||||||
|
yield os.path.basename(f)
|
||||||
|
|
||||||
|
All_Videos = sorted(listdir_nohidden(self.path))
|
||||||
|
#print(self.path)
|
||||||
|
#print(len(All_Videos))
|
||||||
|
All_Videos.sort()
|
||||||
|
#print(All_Videos)
|
||||||
|
VideoPath = os.path.join(self.path, All_Videos[idx//32])
|
||||||
|
f = open(VideoPath, "r")
|
||||||
|
feat = idx%32
|
||||||
|
words = f.read().split()
|
||||||
|
features = np.float32(words[feat * 4096:feat * 4096 + 4096])
|
||||||
|
features = torch.tensor(features)
|
||||||
|
features = torch.reshape(features, (16, 256))
|
||||||
|
print(VideoPath)
|
||||||
|
if VideoPath.find('Normal') == -1:
|
||||||
|
label = 0
|
||||||
|
else:
|
||||||
|
label = 1
|
||||||
|
|
||||||
|
label = torch.tensor(label)
|
||||||
|
#print(features.shape)
|
||||||
|
#print(features)
|
||||||
|
#print(label.shape)
|
||||||
|
print(label)
|
||||||
|
|
||||||
|
return features, label
|
Loading…
Reference in new issue