-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (76 loc) · 3.81 KB
/
main.py
File metadata and controls
105 lines (76 loc) · 3.81 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
import os
import numpy
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.linear_model.logistic import LogisticRegression
import pandas as pd
FILEPATH_TRAIN_IMAGES_NAME = "data/train/train_images_names.txt"
FILEPATH_TRAIN_IMAGES_VECTORS = "data/train/train_images_vectors.bin"
FILEPATH_TRAIN_CAPTION = "data/train/train_captions.txt"
FILEPATH_TEST_IMAGES_NAME = "data/test/test_A_images_names.txt"
FILEPATH_TEST_IMAGES_VECTORS = "data/test/test_A_images_vectors.bin"
FILEPATH_TEST_CAPTION = "data/test/test_A_captions.txt"
TRAIN_NUM_VECTORS = 20000
TRAIN_VECTOR_DIMENSIONS = 2048
TEST_NUM_VECTORS = 1000
TEST_VECTOR_DIMENSIONS = 2048
def load_file(file_names, file_vectors, num_vectors, vector_dimensions):
assert os.path.isfile(file_names), "file doesnt exists " + file_names
assert os.path.isfile(file_vectors), "file doesnt exists " + file_vectors
print("Reading file of names: " + file_names)
names = [line.strip() for line in open(file_names)]
assert num_vectors == len(names), "len no compatible " + len(names)
print("Reading file of vectors: " + file_vectors)
mat = numpy.fromfile(file_vectors, dtype=numpy.float32)
vectors = numpy.reshape(mat, (num_vectors, vector_dimensions))
print(str(num_vectors) + " vector of size " + str(vector_dimensions))
return names, vectors
def load_train_vectors():
print("Load train vectors")
return load_file(FILEPATH_TRAIN_IMAGES_NAME, FILEPATH_TRAIN_IMAGES_VECTORS, TRAIN_NUM_VECTORS,
TRAIN_VECTOR_DIMENSIONS)
def load_test_vectors():
print("Load test vectors")
return load_file(FILEPATH_TEST_IMAGES_NAME, FILEPATH_TEST_IMAGES_VECTORS, TEST_NUM_VECTORS, TEST_VECTOR_DIMENSIONS)
def load_captions(file_captions):
assert os.path.isfile(file_captions), "file doesnt exists " + file_captions
return [line.strip().split("\t") for line in open(file_captions, encoding='utf-8')]
def get_text_descriptor_by_tf_idf(texts):
tf_idf_vect = TfidfVectorizer(lowercase=False, ngram_range=(1, 3), max_df=0.9, min_df=0.002, norm=None)
tf_idf_vect.fit(texts)
X_train = tf_idf_vect.transform(texts)
print("Training: {}".format(X_train))
print(X_train.shape)
return X_train
def get_images_names_with_captions_grouped(dataframe):
df = pd.DataFrame(dataframe, columns=['image_name', 'caption'])
df_grouped_by_caption = df.groupby('image_name')['caption'].apply(
' '.join).reset_index()
df_grouped_by_caption_with_image_name_as_index = df_grouped_by_caption.set_index('image_name')
df_final = df_grouped_by_caption_with_image_name_as_index.reindex(train_names)
return df_final
def get_all_captions_of_all_images_names(train_captions):
captions_train = list()
for filename, caption in train_captions:
captions_train.append(caption)
return captions_train
def get_logistic_regression_classifier(X_train, y_train):
print("X train shape")
print(X_train.shape)
print("y_train")
print(y_train.shape)
print("Entrenar: {} -> {}".format(X_train.shape, len(y_train)))
# TODO REMOVE THIS: this is only a hack for working with the logisticRegression and binary classification.
import random
test_temp_binary_label = []
for i in range(20000):
test_temp_binary_label.append(random.randint(0, 1))
classifier = LogisticRegression()
classifier.fit(X_train, test_temp_binary_label)
return classifier
if __name__ == '__main__':
(train_names, train_vectors) = load_train_vectors()
(test_names, test_vectors) = load_test_vectors()
train_captions = load_captions(FILEPATH_TRAIN_CAPTION)
captions_train = get_images_names_with_captions_grouped(train_captions)
X_train = get_text_descriptor_by_tf_idf(captions_train['caption'])
classifier = get_logistic_regression_classifier(X_train, train_vectors)