-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatisticsDataset.py
144 lines (120 loc) · 5.55 KB
/
statisticsDataset.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from tqdm import tqdm
import numpy as np
import pandas as pd
from src.data_utils.dataset import get_loader_stats as get_loader_stats
config = {"train": {'batch_size': 1, 'shuffle': True, 'num_workers': 4},
"valid": {'batch_size': 1, 'shuffle': False, 'num_workers': 4},
"test": {'batch_size': 1, 'shuffle': False, 'num_workers': 4}}
base_path = '/fhome/gia07/Image_captioning/src/Dataset/'
img_path = f'{base_path}Images/'
cap_path = f'{base_path}captions.txt'
path_partitions = f'{base_path}flickr8k_partitions.npy'
data = pd.read_csv(cap_path)
# Open partitions file
NUM_CAPTIONS = 5
partitions = np.load(path_partitions, allow_pickle=True).item()
s = []
for idx in partitions['train']:
s.extend([(idx * NUM_CAPTIONS) + i for i in range(5)])
idx = np.array(s)
result = data.iloc[idx]
captions = result.caption.apply(lambda x: x.lower()).values
unique_words = set()
list_words = []
for i in range(len(captions)):
caption = captions[i]
caption = caption.split()
list_words.append(caption)
unique_words.update(caption)
# Count the number of times that each word appears in the dataset
word_count = {}
from collections import Counter
word_count = Counter([word for caption in list_words for word in caption])
# total/(num_clases * count_word)
total = sum(word_count.values())
num_classes = len(unique_words)
NUM_WORDS = len(unique_words)
unique_words = ['<SOS>', '<EOS>', '<PAD>'] + sorted(list(unique_words))
idx2word = {k: v for k, v in enumerate(unique_words)}
word2idx = {v: k for k, v in enumerate(unique_words)}
word_weights = {word2idx[word]: total/(num_classes * count_word) for word, count_word in word_count.items()}
weights = torch.tensor([weight for idx, weight in sorted(word_weights.items(), key=lambda x: x[0])])
print(weights)
train_loader = get_loader_stats('train', config)
valid_loader = get_loader_stats('valid', config)
test_loader = get_loader_stats('test', config)
length = []
imgs = []
num_words = []
for img, caption, caption_words in tqdm(train_loader):
imgs.append(img.size())
num_words.append(len(caption_words))
length.append((len(caption)))
print("Train Data")
print("\nCaptions per character")
print(f'Average length of captions: {sum(length)/len(length)}')
print(f'Maximum length of captions: {max(length)}')
print(f'Minimum length of captions: {min(length)}')
print(f'Median length of captions: {np.median(length)} +- {np.std(length)}')
print("\nCaptions per word")
print(f'Average length of captions: {sum(num_words)/len(num_words)}')
print(f'Maximum length of captions: {max(num_words)}')
print(f'Minimum length of captions: {min(num_words)}')
print(f'Median length of captions: {np.median(num_words)} +- {np.std(num_words)}')
print("\nImages")
print(f'Mean image width: {np.mean([img[2] for img in imgs])} +- {np.std([img[2] for img in imgs])}')
print(f'Mean image height: {np.mean([img[3] for img in imgs])} +- {np.std([img[3] for img in imgs])}')
print(f"Max image width: {max([img[2] for img in imgs])}")
print(f"Max image height: {max([img[3] for img in imgs])}")
print(f"Min image width: {min([img[2] for img in imgs])}")
print(f"Min image height: {min([img[3] for img in imgs])}")
length = []
imgs = []
num_words = []
for img, caption, caption_words in tqdm(valid_loader):
imgs.append(img.size())
num_words.append(len(caption_words))
length.append((len(caption)))
print("Validation")
print("\nCaptions per character")
print(f'Average length of captions: {sum(length)/len(length)}')
print(f'Maximum length of captions: {max(length)}')
print(f'Minimum length of captions: {min(length)}')
print(f'Median length of captions: {np.median(length)} +- {np.std(length)}')
print("\nCaptions per word")
print(f'Average length of captions: {sum(num_words)/len(num_words)}')
print(f'Maximum length of captions: {max(num_words)}')
print(f'Minimum length of captions: {min(num_words)}')
print(f'Median length of captions: {np.median(num_words)} +- {np.std(num_words)}')
print("\nImages")
print(f'Mean image width: {np.mean([img[2] for img in imgs])} +- {np.std([img[2] for img in imgs])}')
print(f'Mean image height: {np.mean([img[3] for img in imgs])} +- {np.std([img[3] for img in imgs])}')
print(f"Max image width: {max([img[2] for img in imgs])}")
print(f"Max image height: {max([img[3] for img in imgs])}")
print(f"Min image width: {min([img[2] for img in imgs])}")
print(f"Min image height: {min([img[3] for img in imgs])}")
length = []
imgs = []
num_words = []
for img, caption, caption_words in tqdm(test_loader):
imgs.append(img.size())
num_words.append(len(caption_words))
length.append((len(caption)))
print("Test")
print("\nCaptions per character")
print(f'Average length of captions: {sum(length)/len(length)}')
print(f'Maximum length of captions: {max(length)}')
print(f'Minimum length of captions: {min(length)}')
print(f'Median length of captions: {np.median(length)} +- {np.std(length)}')
print("\nCaptions per word")
print(f'Average length of captions: {sum(num_words)/len(num_words)}')
print(f'Maximum length of captions: {max(num_words)}')
print(f'Minimum length of captions: {min(num_words)}')
print(f'Median length of captions: {np.median(num_words)} +- {np.std(num_words)}')
print("\nImages")
print(f'Mean image width: {np.mean([img[2] for img in imgs])} +- {np.std([img[2] for img in imgs])}')
print(f'Mean image height: {np.mean([img[3] for img in imgs])} +- {np.std([img[3] for img in imgs])}')
print(f"Max image width: {max([img[2] for img in imgs])}")
print(f"Max image height: {max([img[3] for img in imgs])}")
print(f"Min image width: {min([img[2] for img in imgs])}")
print(f"Min image height: {min([img[3] for img in imgs])}")