File size: 6,165 Bytes
7800904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import tensorflow as tf
import numpy as np
import pickle
from preprocess import preprocess_image
from model import TransformerEncoder, TransformerDecoder, ImageCaptioningModel
from cnn import CNN_encoder

embed_dim=256
max_len=40
vocab_size=8002

cnn_model = CNN_encoder()
encoder = TransformerEncoder(embed_dim, 1)
decoder = TransformerDecoder(
    num_layers=4,          
    embed_dim=256,
    units=512,
    num_heads=4,
    vocab_size=vocab_size,
    max_len=max_len
)

caption_model = ImageCaptioningModel(
    cnn_model=cnn_model,
    encoder=encoder,
    decoder=decoder,
    image_aug=None
)

dummy_img = tf.zeros((1, 224, 224, 3))
dummy_txt = tf.zeros((1, 40))

_ = caption_model([dummy_img, dummy_txt], training=False)

caption_model.load_weights("my_caption_model.weights.h5")

tokenizer = pickle.load(open('tokenizer.pkl', 'rb'))

vocab = tokenizer.get_vocabulary()
clean_vocab = [token for token in vocab if token not in ['', '[UNK]']]

word2idx = tf.keras.layers.StringLookup(
    vocabulary = clean_vocab,
    invert=False,
    mask_token = '',           
    oov_token = '[UNK]' 
)

idx2word = tf.keras.layers.StringLookup(
    vocabulary = clean_vocab,
    invert = True,
    mask_token = '',            
    oov_token = '[UNK]' 
)

def get_image_features(img_path):
    img = preprocess_image(img_path)            
    img = tf.expand_dims(img, axis=0)                
    img_embed = caption_model.cnn_model(img)        
    return caption_model.encoder(img_embed, training=False)

def decode_ids_to_sentence(ids, idx2word):
   
    words = []
    for i in ids:
        if i == 0:               
            continue
        w = idx2word(tf.constant([i]))[0].numpy().decode("utf-8")
        if w in ("[start]", "[end]"):
            continue
        words.append(w)
    return " ".join(words)

def advanced_generate(encoder_output,

                      method="beam",     

                      beam_width=3,

                      top_k=40,

                      temperature=1.0,

                      repetition_penalty=1.2, 

                      alpha=0.6,             

                      max_len=None):

    if max_len is None:
        max_len = 40

    # --- Preprocessing & Encoding ---
    # img = preprocess_image(img_path)            
    # img = tf.expand_dims(img, axis=0)                
    # img_embed = caption_model.cnn_model(img)        
    # encoder_output = caption_model.encoder(img_embed, training=False)

    start_id = int(tokenizer(["[start]"])[0,0].numpy())  
    end_id   = int(tokenizer(["[end]"])[0,0].numpy())
    unk_id   = 1 

    def process_logits(logits, current_seq):
        logits[unk_id] = -1e10
        
        if temperature != 1.0:
            logits = logits / float(temperature)
            
        if repetition_penalty != 1.0:
            for tok in set(current_seq):
                if logits[tok] > 0:
                    logits[tok] /= repetition_penalty
                else:
                    logits[tok] *= repetition_penalty
        return logits

    if method == "greedy":
        y = [start_id]
        for _ in range(max_len-1):
            inp = tf.constant([y], dtype=tf.int32)
            mask = tf.cast(inp != 0, tf.int32)
            preds = caption_model.decoder(inp, encoder_output, training=False, mask=mask)
            
            logits = np.log(np.clip(preds[0, -1, :].numpy(), 1e-9, 1.0))
            logits = process_logits(logits, y)
            
            next_id = int(np.argmax(logits))
            if next_id == end_id:
                break
            y.append(next_id)
        return decode_ids_to_sentence(y, idx2word)

    # ---------- BEAM SEARCH ----------
    if method == "beam":
        sequences = [([start_id], 0.0)]
        
        for _step in range(max_len-1):
            all_candidates = []
            for seq, score in sequences:
                if seq[-1] == end_id:
                    all_candidates.append((seq, score))
                    continue

                inp = tf.constant([seq], dtype=tf.int32)
                mask = tf.cast(inp != 0, tf.int32)
                preds = caption_model.decoder(inp, encoder_output, training=False, mask=mask)
                
                logits = np.log(np.clip(preds[0, -1, :].numpy(), 1e-9, 1.0))
                logits = process_logits(logits, seq)

                top_ids = np.argsort(logits)[-beam_width:]
                for tok in top_ids:
                    candidate = seq + [int(tok)]
                    new_score = score + float(logits[tok])
                    all_candidates.append((candidate, new_score))

            def get_lp_score(c_seq, c_score):
                lp = ((5 + len(c_seq))**alpha) / ((5 + 1)**alpha)
                return c_score / lp

            sequences = sorted(all_candidates, key=lambda x: get_lp_score(x[0], x[1]), reverse=True)[:beam_width]

            if all(seq[-1] == end_id for seq, sc in sequences):
                break

        return decode_ids_to_sentence(sequences[0][0], idx2word)

    # ---------- SAMPLING ----------
    if method == "sample":
        y = [start_id]
        for _ in range(max_len-1):
            inp = tf.constant([y], dtype=tf.int32)
            mask = tf.cast(inp != 0, tf.int32)
            preds = caption_model.decoder(inp, encoder_output, training=False, mask=mask)
            
            logits = np.log(np.clip(preds[0, -1, :].numpy(), 1e-9, 1.0))
            logits = process_logits(logits, y)

            topk_idx = np.argsort(logits)[-top_k:]
            topk_logits = logits[topk_idx]
            
            exp_logits = np.exp(topk_logits - np.max(topk_logits))
            probs = exp_logits / np.sum(exp_logits)
            
            next_id = int(np.random.choice(topk_idx, p=probs))

            if next_id == end_id:
                break
            y.append(next_id)

        return decode_ids_to_sentence(y, idx2word)

    raise ValueError("Unknown method: choose 'greedy', 'beam', or 'sample'")