Skip to content

Commit cbce4da

Browse files
committed
Adding new py files and source data
1 parent 0246e41 commit cbce4da

File tree

106 files changed

+312
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+312
-0
lines changed

PyTorch_1_Datasets.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import torch
2+
from torch.utils.data import Dataset
3+
4+
#crate a subclass from Dataset
5+
class toy_set(Dataset):
6+
#contructor
7+
def __init__(self,length = 50,transform = None):
8+
self.len = length
9+
# pylint: disable=E1101
10+
self.x = torch.ones(length,2)
11+
self.y = torch.ones(length,1)
12+
# pylint: enable=E1101
13+
self.transform = transform
14+
15+
#return data at a given index
16+
def __getitem__(self,index):
17+
sample = self.x[index],self.y[index]
18+
if self.transform:
19+
sample = self.transform(sample)
20+
return sample
21+
22+
#return length
23+
def __len__(self):
24+
return self.len
25+
26+
our_dataset = toy_set()
27+
28+
for idx in range(3):
29+
x,y = our_dataset[idx]
30+
print("x = {} y = {}".format(x,y))
31+
32+
for x,y in our_dataset:
33+
print("x = {} y = {}".format(x,y))

PyTorch_2_Transforms.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import torch
2+
from torch.utils.data import Dataset
3+
4+
#crate a subclass from Dataset
5+
class toy_set(Dataset):
6+
#contructor
7+
def __init__(self,length = 50,transform = None):
8+
self.len = length
9+
# pylint: disable=E1101
10+
self.x = torch.ones(length,2)
11+
self.y = torch.ones(length,1)
12+
# pylint: enable=E1101
13+
self.transform = transform
14+
15+
#return data at a given index
16+
def __getitem__(self,index):
17+
sample = self.x[index],self.y[index]
18+
if self.transform:
19+
sample = self.transform(sample)
20+
return sample
21+
22+
#return length
23+
def __len__(self):
24+
return self.len
25+
26+
#crate a transforms class
27+
class add_mult(object):
28+
#contructor
29+
def __init__(self,addx = 1,muly = 2):
30+
self.addx = addx
31+
self.muly = muly
32+
#executor
33+
def __call__(self,sample):
34+
return sample[0] + self.addx , sample[1] * self.muly
35+
36+
#create add_mult transforms object and toy_set Dataset object
37+
a_m = add_mult()
38+
data_set = toy_set()
39+
40+
#use loop to print first 3 elements
41+
for idx in range(3):
42+
x,y = data_set[idx]
43+
print("i = {} Original x = {} Original y = {}".format(idx,x,y))
44+
x_a_m,y_a_m = a_m(data_set[idx])
45+
print("i = {} Transformed x = {} Transformed y = {}".format(idx,x_a_m,y_a_m))
46+
47+
#create a new data_set object with add_mult object as transform
48+
cust_data_set = toy_set(transform = a_m)
49+
50+
#use loop to print first 3 elements
51+
for idx in range(3):
52+
x,y = data_set[idx]
53+
print("i = {} Original x = {} Original y = {}".format(idx,x,y))
54+
x_a_m,y_a_m = cust_data_set[idx]
55+
print("i = {} Transformed x = {} Transformed y = {}".format(idx,x_a_m,y_a_m))

PyTorch_3_Compose_Transforms.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import torch
2+
from torch.utils.data import Dataset
3+
from torchvision import transforms
4+
5+
#crate a subclass from Dataset
6+
class toy_set(Dataset):
7+
#contructor
8+
def __init__(self,length = 50,transform = None):
9+
self.len = length
10+
# pylint: disable=E1101
11+
self.x = torch.ones(length,2)
12+
self.y = torch.ones(length,1)
13+
# pylint: enable=E1101
14+
self.transform = transform
15+
16+
#return data at a given index
17+
def __getitem__(self,index):
18+
sample = self.x[index],self.y[index]
19+
if self.transform:
20+
sample = self.transform(sample)
21+
return sample
22+
23+
#return length
24+
def __len__(self):
25+
return self.len
26+
27+
#crate a transforms class
28+
class add_mult(object):
29+
#contructor
30+
def __init__(self,addx = 1,muly = 2):
31+
self.addx = addx
32+
self.muly = muly
33+
#executor
34+
def __call__(self,sample):
35+
return sample[0] + self.addx , sample[1] * self.muly
36+
37+
class mult(object):
38+
def __init__(self,mul = 100):
39+
self.mult = mul
40+
def __call__(self,sample):
41+
return sample[0] * self.mult , sample[1] * self.mult
42+
43+
# Combine the mult() & add_mult()
44+
data_transform = transforms.Compose([mult(),add_mult()])
45+
46+
#print(data_transform)
47+
data_set = toy_set()
48+
composed_data_set = toy_set(transform = data_transform)
49+
50+
for idx in range(3):
51+
x,y = data_set[idx]
52+
print("i = {} Original x = {} Original y = {}".format(idx,x,y))
53+
x_c,y_c = composed_data_set[idx]
54+
print("i = {} Transformed x = {} Transformed y = {}".format(idx,x_c,y_c))

PyTorch_4_Image_Datasets.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import torch
2+
from torch.utils.data import Dataset
3+
import torchvision.transforms as transforms
4+
5+
import pandas as pd
6+
import os
7+
from PIL import Image
8+
import matplotlib.pyplot as plt
9+
10+
data_dir = ''
11+
csv_file = 'index.csv'
12+
"""
13+
csv_path = os.path.join(data_dir,csv_file)
14+
15+
data_name = pd.read_csv(csv_path)
16+
print(data_name.head(5))
17+
print('File name : {}'.format(data_name.iloc[0,1]))
18+
print('y : {}'.format(data_name.iloc[0,0]))
19+
print('Total rows : {}'.format(data_name.shape[0]))
20+
21+
image_name = data_name.iloc[0,1]
22+
image_path = os.path.join(data_dir,image_name)
23+
24+
image = Image.open(image_path)
25+
plt.imshow(image,cmap='gray', vmin=0, vmax=255)
26+
plt.title(data_name.iloc[0, 0])
27+
plt.show()
28+
"""
29+
class ImageDataset(Dataset):
30+
def __init__(self,data_dir,csv_file,transform = None):
31+
self.data_dir = data_dir
32+
csv_path = os.path.join(self.data_dir,csv_file)
33+
self.data_name = pd.read_csv(csv_path)
34+
self.len = self.data_name.shape[0]
35+
self.transform = transform
36+
37+
def __getitem__(self,idx):
38+
image_path = os.path.join(self.data_dir, self.data_name.iloc[idx,1])
39+
image = Image.open(image_path)
40+
y = self.data_name.iloc[idx,0]
41+
if self.transform:
42+
image = self.transform(image)
43+
return image,y
44+
45+
def __len__(self):
46+
return self.len
47+
48+
"""
49+
my_image_dataset = ImageDataset(data_dir = data_dir,csv_file = csv_file)
50+
51+
image,y = my_image_dataset[0]
52+
plt.imshow(image,cmap='gray', vmin=0, vmax=255)
53+
plt.title(y)
54+
plt.show()
55+
"""
56+
57+
data_transform = transforms.Compose([transforms.CenterCrop(10), transforms.ToTensor()])
58+
my_image_dataset = ImageDataset(data_dir = data_dir,csv_file = csv_file,transform=data_transform)
59+
60+
#my_image_dataset = ImageDataset(data_dir = data_dir,csv_file = csv_file,transform=None)
61+
62+
#print(my_image_dataset[0][0].shape)
63+
64+
image,y = my_image_dataset[1]
65+
print(y)
66+
plt.imshow(transforms.ToPILImage()(image),cmap='gray', vmin=0, vmax=255)
67+
plt.title(y)
68+
plt.show()

img/fashion0.png

574 Bytes

img/fashion1.png

560 Bytes

img/fashion10.png

572 Bytes

img/fashion100.png

607 Bytes

img/fashion11.png

607 Bytes

img/fashion12.png

465 Bytes

0 commit comments

Comments
 (0)