-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Description.py
More file actions
111 lines (89 loc) · 3.67 KB
/
Copy pathData_Description.py
File metadata and controls
111 lines (89 loc) · 3.67 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from prettytable import PrettyTable
def label_count(dataset: str,
index_to_label: dict,
is_batched: bool = True
):
""" Counting the samples of each labels. """
labels_list = []
if is_batched:
for _, labels in dataset:
labels_list.extend(labels.numpy())
else:
for _, labels in dataset:
labels_list.append(labels.numpy())
labels_tensor = tf.constant(labels_list)
unique_labels, idx= tf.unique(labels_tensor)
label_counts = tf.math.bincount(idx)
table = PrettyTable(['Index', 'Label', 'Count'])
for label, count in zip(unique_labels.numpy(), label_counts.numpy()):
table.add_row([int(label), index_to_label[label], count])
print(table)
# ----------------------------------------------------------------------------
# Visual Train Data
def filter_by_label(label: int, target_label: int = 0):
""" It's a label filter that used for Tensorflow Dataset to filter only appropriate label. """
return tf.equal(label, target_label)
def size_dataset(Dataset: list):
""" This function used for size determination of Tensorflow Datasets. """
length = 0
for _ in Dataset:
length += 1
return length
def plot_dataset_sample(dataset: list,
index_to_label: dict,
name: str,
n_classes: int = 10,
is_batched: bool = True,
n_samples_each_label: int = 10261,
n_plot: int = 5,
img_size: int = (28, 28)
):
""" This function is used for plotting image samples from Dataset. """
pic = []
labl = []
if is_batched:
sample_dataset = dataset.unbatch()
else:
sample_dataset = dataset
for Class in range(n_classes):
LAMBDA = lambda image, label: filter_by_label(label, target_label = Class)
filtered = sample_dataset.filter(LAMBDA)
shuflled = filtered.shuffle(buffer_size= n_samples_each_label, seed= 42)
selected = shuflled.take(n_plot)
for img, label in selected:
pic.append(img.numpy())
labl.append(label.numpy())
fig, ax = plt.subplots(n_classes, n_plot, figsize= img_size)
column = 0
for i in range(len(pic)):
if i%5 == 0:
column = 0
label = labl[i]
label_name = index_to_label[label]
idx = int(label)
single_image = pic[i]
single_image = single_image.squeeze()
ax[idx, column].imshow(single_image, cmap='gray')
ax[idx, column].set_title(label_name)
ax[idx, column].axis('off')
column += 1
plt.tight_layout()
plt.savefig(f'CNN_Model_HandWriting_Digits/Plots/{name}_figure.png')
plt.close()
# plt.show() # Becuase Terminal environment doesn't support figure plot
def describe_dataset_sample(dataset: list):
""" This function is used for taking sample from Tensorflow Dataset at the end of Aumentation. """
for image, label in dataset.take(1):
print(f'\tsize of images: {image.shape}')
print(f'\tsize of labels: {label.shape}\n')
def describe_final_Dataset(Dataset: list):
""" This function is used for taking sample from Tensorflow Dataset
at the end of the Converting to categorical values. """
for image, label in Dataset.take(1):
print(f'label is:\n\t{label[0]}\n')
print(f'shape of label is:\n\t{label[0].shape}\n')
print(f'value of label is:\n\t{np.argmax(label[0])}\n')
print(f'shape of image is:\n\t{image[0].shape}\n')