File size: 4,871 Bytes
7f2160d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56d044b
7f2160d
56d044b
 
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
187
188
189
190
191
192
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session


get_ipython().getoutput("pip install nltk")


import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from tqdm import tqdm


device = "cuda" if torch.cuda.is_available() else "cpu"

model_name = "utkarshpise/blip-rsicd-captioning"

processor = BlipProcessor.from_pretrained(model_name)
model = BlipForConditionalGeneration.from_pretrained(model_name)

model.to(device)
model.eval()

print(" Model loaded")


smooth = SmoothingFunction().method1

def evaluate_model(model, loader, processor, device):
    model.eval()

    total_loss = 0
    preds = []
    refs = []

    with torch.no_grad():
        for batch in tqdm(loader, desc="Evaluating"):
            batch = {k: v.to(device) for k, v in batch.items()}

            # πŸ”₯ LM LOSS
            outputs = model(**batch)
            loss = outputs.loss
            total_loss += loss.item()

            # πŸ”₯ Generate captions
            generated_ids = model.generate(
                pixel_values=batch["pixel_values"],
                max_length=50,
                num_beams=5
            )

            pred = processor.batch_decode(generated_ids, skip_special_tokens=True)
            ref = processor.batch_decode(batch["labels"], skip_special_tokens=True)

            preds.extend(pred)
            refs.extend(refs if False else ref)

    avg_loss = total_loss / len(loader)

    bleu_scores = []
    for p, r in zip(preds, refs):
        score = sentence_bleu([r.split()], p.split(), smoothing_function=smooth)
        bleu_scores.append(score)

    bleu = sum(bleu_scores) / len(bleu_scores)

    return avg_loss, bleu



import kagglehub
import os
import pandas as pd

path = kagglehub.dataset_download("thedevastator/rsicd-image-caption-dataset")

print("Dataset path:", path)
print("Files:", os.listdir(path))


TEST_CSV = os.path.join(path, "test.csv")

df = pd.read_csv(TEST_CSV)

print("Total samples:", len(df))


import random



class RSICDDataset(Dataset):
    def __init__(self, df, processor):
        self.df = df
        self.processor = processor

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        row = self.df.iloc[idx]

        # πŸ”₯ IMAGE
        import ast
        from io import BytesIO
        from PIL import Image

        img_data = row["image"]

        if isinstance(img_data, str):
            img_dict = ast.literal_eval(img_data)
            image_bytes = img_dict["bytes"]
        else:
            image_bytes = img_data["bytes"]

        image = Image.open(BytesIO(image_bytes)).convert("RGB")

        # CORRECT CAPTION FIELD
        captions = row["captions"]

        if isinstance(captions, str):
            captions = ast.literal_eval(captions)

        caption = random.choice(captions)

        encoding = self.processor(
            images=image,
            text=caption,
            padding="max_length",
            truncation=True,
            return_tensors="pt"
        )

        encoding = {k: v.squeeze(0) for k, v in encoding.items()}
        encoding["labels"] = encoding["input_ids"]

        return encoding


from torch.utils.data import DataLoader

dataset = RSICDDataset(df, processor)

test_loader = DataLoader(dataset, batch_size=8, num_workers=2)


loss, bleu = evaluate_model(model, test_loader, processor, device)

print(f"\nLM Loss: {loss}")
print(f" BLEU Score: {bleu}")


from huggingface_hub import login

login()



from huggingface_hub import upload_file

repo_id = "utkarshpise/blip-rsicd-captioning"

upload_file(
    path_or_fileobj="/kaggle/working/.virtual_documents/__notebook_source__.ipynb", 
    path_in_repo="inference.py",        
    repo_id=repo_id,
    repo_type="model"
)

print(" Code uploaded!")


Evaluating: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 137/137 [05:44<00:00,  2.51s/it]

LM Loss: 16.873739333048356
 BLEU Score: 0.10002967072689554