-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDA.py
More file actions
79 lines (65 loc) · 2.89 KB
/
Copy pathEDA.py
File metadata and controls
79 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import matplotlib.pyplot as plt
from PIL import Image
from torchvision import transforms
from collections import Counter
# Define dataset paths (replace with your actual paths)
train_path = "Test_Alphabet"
test_path = "Train_Alphabet"
# Function to get class distribution
def get_class_distribution(data_path):
classes = []
for class_dir in os.listdir(data_path):
if os.path.isdir(os.path.join(data_path, class_dir)):
num_images = len(os.listdir(os.path.join(data_path, class_dir)))
classes.append((class_dir, num_images))
return dict(classes)
# Function to visualize random samples from each class
def visualize_samples(data_path, num_samples=3):
class_dirs = [d for d in os.listdir(data_path) if os.path.isdir(os.path.join(data_path, d))]
fig, axes = plt.subplots(len(class_dirs), num_samples, figsize=(num_samples * 3, len(class_dirs) * 3))
for i, class_dir in enumerate(sorted(class_dirs)):
class_path = os.path.join(data_path, class_dir)
sample_images = os.listdir(class_path)[:num_samples]
for j, img_name in enumerate(sample_images):
img_path = os.path.join(class_path, img_name)
img = Image.open(img_path)
axes[i, j].imshow(img)
axes[i, j].axis('off')
if j == 0:
axes[i, j].set_title(class_dir)
plt.tight_layout()
plt.show()
# Perform EDA
print("Train Class Distribution:", get_class_distribution(train_path))
print("Test Class Distribution:", get_class_distribution(test_path))
visualize_samples(train_path)
# Preprocessing
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Function to preprocess and save images
def preprocess_and_save(data_path, save_path):
os.makedirs(save_path, exist_ok=True)
for class_dir in os.listdir(data_path):
class_path = os.path.join(data_path, class_dir)
save_class_path = os.path.join(save_path, class_dir)
os.makedirs(save_class_path, exist_ok=True)
if os.path.isdir(class_path):
for img_name in os.listdir(class_path):
img_path = os.path.join(class_path, img_name)
save_img_path = os.path.join(save_class_path, img_name)
try:
img = Image.open(img_path)
img_transformed = transform(img)
img_transformed = transforms.ToPILImage()(img_transformed)
img_transformed.save(save_img_path)
except Exception as e:
print(f"Error processing {img_name}: {e}")
# Preprocess and save datasets
preprocessed_train_path = "preprocessed_train"
preprocessed_test_path = "preprocessed_test"
preprocess_and_save(train_path, preprocessed_train_path)
preprocess_and_save(test_path, preprocessed_test_path)