summaryrefslogtreecommitdiff
path: root/cv/holiday_similarity/pretrained-vec-nn-classifier.py
blob: e8fff09b914377d4b09dcc50d9951b20603ab14b (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from keras import backend as K
from keras import layers, models, callbacks, utils

from cv.holiday_similarity.data_utils import *
from cv.holiday_similarity.image_vectors_utils import *

DATA_DIR = "./data"
IMAGE_DIR = '/Users/chunhuizhang/workspaces/00_datasets/images/INRIA Holidays dataset /jpg'

BATCH_SIZE = 32
NUM_EPOCHS = 10


def e2e_model_concat(vec_size):
    input_1 = layers.Input(shape=(vec_size,))
    input_2 = layers.Input(shape=(vec_size,))

    merged = layers.merge.Concatenate(axis=-1)([input_1, input_2])

    fc1 = layers.Dense(512, kernel_initializer="glorot_uniform")(merged)
    fc1 = layers.Dropout(0.2)(fc1)
    fc1 = layers.Activation("relu")(fc1)

    fc2 = layers.Dense(128, kernel_initializer="glorot_uniform")(fc1)
    fc2 = layers.Dropout(0.2)(fc2)
    fc2 = layers.Activation("relu")(fc2)

    pred = layers.Dense(2, kernel_initializer="glorot_uniform")(fc2)
    pred = layers.Activation("softmax")(pred)
    model = models.Model(inputs=[input_1, input_2], outputs=pred)
    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
    utils.plot_model(model, to_file='./data/images/concat.png', show_shapes=True, show_layer_names=True)
    return model


def e2e_model_dot(vec_size):
    def cosine_distance(vecs, normalize=False):
        x, y = vecs
        if normalize:
            x = K.l2_normalize(x, axis=0)
            y = K.l2_normalize(x, axis=0)
        return K.prod(K.stack([x, y], axis=1), axis=1)

    def cosine_distance_output_shape(shapes):
        return shapes[0]

    input_1 = layers.Input(shape=(vec_size,))
    input_2 = layers.Input(shape=(vec_size,))

    merged = layers.Lambda(cosine_distance,
                           output_shape=cosine_distance_output_shape)([input_1, input_2])

    fc1 = layers.Dense(512, kernel_initializer="glorot_uniform")(merged)
    fc1 = layers.Dropout(0.2)(fc1)
    fc1 = layers.Activation("relu")(fc1)

    fc2 = layers.Dense(128, kernel_initializer="glorot_uniform")(fc1)
    fc2 = layers.Dropout(0.2)(fc2)
    fc2 = layers.Activation("relu")(fc2)

    pred = layers.Dense(2, kernel_initializer="glorot_uniform")(fc2)
    pred = layers.Activation("softmax")(pred)
    model = models.Model(inputs=[input_1, input_2], outputs=pred)
    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
    utils.plot_model(model, to_file='./data/images/dot.png', show_shapes=True, show_layer_names=True)
    return model


def e2e_model_l1(vec_size):
    def absdiff(vecs):
        x, y = vecs
        return K.abs(K.sum(K.stack([x, -y], axis=1), axis=1))

    def absdiff_output_shape(shapes):
        return shapes[0]

    input_1 = layers.Input(shape=(vec_size,))
    input_2 = layers.Input(shape=(vec_size,))
    merged = layers.Lambda(absdiff, output_shape=absdiff_output_shape)([input_1, input_2])

    fc1 = layers.Dense(512, kernel_initializer="glorot_uniform")(merged)
    fc1 = layers.Dropout(0.2)(fc1)
    fc1 = layers.Activation("relu")(fc1)

    fc2 = layers.Dense(128, kernel_initializer="glorot_uniform")(fc1)
    fc2 = layers.Dropout(0.2)(fc2)
    fc2 = layers.Activation("relu")(fc2)

    pred = layers.Dense(2, kernel_initializer="glorot_uniform")(fc2)
    pred = layers.Activation("softmax")(pred)
    model = models.Model(inputs=[input_1, input_2], outputs=pred)
    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
    utils.plot_model(model, to_file='./data/images/l1.png', show_shapes=True, show_layer_names=True)
    return model


def e2e_model_l2(vec_size):
    def euclidean_distance(vecs):
        x, y = vecs
        return K.sqrt(K.sum(K.stack([K.square(x), -K.square(y)], axis=1), axis=1))

    def euclidean_distance_output_shape(shapes):
        xshape, yshape = shapes
        return xshape

    input_1 = layers.Input(shape=(vec_size,))
    input_2 = layers.Input(shape=(vec_size,))
    merged = layers.Lambda(euclidean_distance,
                           output_shape=euclidean_distance_output_shape)([input_1, input_2])

    fc1 = layers.Dense(512, kernel_initializer="glorot_uniform")(merged)
    fc1 = layers.Dropout(0.2)(fc1)
    fc1 = layers.Activation("relu")(fc1)

    fc2 = layers.Dense(128, kernel_initializer="glorot_uniform")(fc1)
    fc2 = layers.Dropout(0.2)(fc2)
    fc2 = layers.Activation("relu")(fc2)

    pred = layers.Dense(2, kernel_initializer="glorot_uniform")(fc2)
    pred = layers.Activation("softmax")(pred)

    model = models.Model(inputs=[input_1, input_2], outputs=pred)
    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
    utils.plot_model(model, to_file='./data/images/l2.png', show_shapes=True, show_layer_names=True)
    return model


if __name__ == '__main__':
    image_triples = get_holiday_triples(IMAGE_DIR)
    train_triples, val_triples, test_triples = train_test_split(image_triples,
                                                                splits=[0.7, 0.1, 0.2])
    print(len(train_triples), len(val_triples), len(test_triples))

    VECTOR_SIZE = 2048
    VECTOR_FILE = os.path.join(DATA_DIR, "resnet-vectors.tsv")

    vec_dict = load_vectors(VECTOR_FILE)

    train_gen = data_generator(train_triples, VECTOR_SIZE, vec_dict, BATCH_SIZE)
    val_gen = data_generator(val_triples, VECTOR_SIZE, vec_dict, BATCH_SIZE)

    # model_name = get_model_file(DATA_DIR, 'resnet', 'dot', 'best')
    # model_name = get_model_file(DATA_DIR, 'resnet', 'concat', 'best')
    model_name = get_model_file(DATA_DIR, 'resnet', 'l2', 'best')

    checkpoint = callbacks.ModelCheckpoint(model_name, save_best_only=True)
    train_steps_per_epoch = len(train_triples) // BATCH_SIZE
    val_steps_per_epoch = len(val_triples) // BATCH_SIZE

    # model = e2e_model_concat(VECTOR_SIZE)
    # model = e2e_model_dot(VECTOR_SIZE)
    # model = e2e_model_l1(VECTOR_SIZE)
    e2e_model_l2(VECTOR_SIZE)

    # history = model.fit_generator(train_gen, steps_per_epoch=train_steps_per_epoch,
    #                               epochs=NUM_EPOCHS,
    #                               validation_data=val_gen, validation_steps=val_steps_per_epoch,
    #                               callbacks=[checkpoint])
    #
    # plot_training_curve(history)
    #
    # final_model_name = get_model_file(DATA_DIR, "resnet", "l2", 'final')
    # model.save(final_model_name)
    #
    # test_gen = data_generator(test_triples, VECTOR_SIZE, vec_dict, BATCH_SIZE)
    # final_accuracy = evaluate_model(models.load_model(final_model_name), test_gen, test_triples, BATCH_SIZE)
    #
    # test_gen = data_generator(test_triples, VECTOR_SIZE, vec_dict, BATCH_SIZE)
    # best_accuracy = evaluate_model(models.load_model(model_name), test_gen, test_triples, BATCH_SIZE)
    #
    # scores = best_accuracy if best_accuracy > final_accuracy else final_accuracy