@@ -33,5 +33,122 @@ def walk_through_dir(dir_path):
33
33
walk_through_dir (data_path )
34
34
35
35
36
+
37
+
38
+
36
39
import random
37
40
from PIL import Image
41
+ image_path_list = list (data_path .glob ("*/*/*.jpg" ))
42
+ random_image_path = random .choice (image_path_list )
43
+ # str(random_image_path).split("/")[-2]
44
+ image_class = random_image_path .parent .stem
45
+ img = Image .open (random_image_path )
46
+ # 5. Print metadata
47
+ img
48
+
49
+
50
+ random_image_path ,image_class ,img .height ,img .width ,
51
+
52
+
53
+
54
+ import numpy as np
55
+ import matplotlib .pyplot as plt
56
+
57
+
58
+ plt .figure (figsize = (10 ,7 ))
59
+ plt .imshow (np .asarray (img ))
60
+ plt .axis (False );
61
+
62
+
63
+ import torch
64
+ from torch .utils .data import DataLoader
65
+ from torchvision import datasets ,transforms
66
+
67
+
68
+ data_transform = transforms .Compose ([
69
+ transforms .Resize (size = (128 ,128 )),
70
+ transforms .RandomHorizontalFlip (p = 0.5 ),
71
+ transforms .ToTensor ()
72
+ ])
73
+
74
+
75
+ plt .imshow (torch .permute (data_transform (img ),(1 ,2 ,0 )))
76
+
77
+
78
+ def plot_transformed_images (image_paths ,transform ,n = 3 ,seed = 42 ):
79
+ """
80
+ Selects random iamges from a pth of images and loads/transforms
81
+ them then plots the original vs the transformed version
82
+ """
83
+ random_image_paths = random .sample (image_paths ,k = n )
84
+ for image_path in random_image_paths :
85
+ with Image .open (image_path ) as f :
86
+ fig ,ax = plt .subplots (1 ,2 )
87
+ ax [0 ].imshow (f )
88
+ ax [0 ].axis (False )
89
+ ax [1 ].imshow (torch .permute (transform (f ),(1 ,2 ,0 )))
90
+ ax [1 ].axis (False )
91
+ fig .suptitle (f"Class : { image_path .parent .stem } " ,fontsize = 16 )
92
+
93
+
94
+ plot_transformed_images (image_path_list ,data_transform )
95
+
96
+
97
+ train_dir = "data/04/01/train/"
98
+ test_dir = "data/04/01/test/"
99
+
100
+
101
+ # Use ImageFolder to create dataset(s)
102
+ train_data = datasets .ImageFolder (root = train_dir ,transform = data_transform )
103
+ test_data = datasets .ImageFolder (root = test_dir ,transform = data_transform )
104
+
105
+
106
+ train_data ,test_data
107
+
108
+
109
+ # Get class names
110
+ class_names = train_data .classes
111
+ class_names
112
+
113
+
114
+ class_dict = train_data .class_to_idx
115
+ class_dict
116
+
117
+
118
+ img ,label = train_data [0 ][0 ],train_data [0 ][1 ]
119
+
120
+
121
+ img .shape ,img .dtype
122
+
123
+
124
+ label ,type (label )
125
+
126
+
127
+ img_permute = img .permute (1 ,2 ,0 )
128
+
129
+
130
+ plt .imshow (img_permute )
131
+
132
+
133
+ import os
134
+ os .cpu_count ()
135
+
136
+
137
+ train_dataloader = DataLoader (train_data ,batch_size = 32 ,shuffle = True ,num_workers = os .cpu_count ())
138
+ test_dataloader = DataLoader (test_data ,batch_size = 32 ,shuffle = True ,num_workers = os .cpu_count ())
139
+
140
+
141
+ train_dataloader ,test_dataloader
142
+
143
+
144
+
145
+ img ,label = next (iter (train_dataloader ))
146
+
147
+
148
+ img .shape
149
+
150
+
151
+ plt .imshow (img [0 ].permute (1 ,2 ,0 ))
152
+
153
+
154
+
0 commit comments