Upload caption_map.ipynb
Browse files- caption_map.ipynb +154 -0
caption_map.ipynb
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"data": {
|
10 |
+
"application/vnd.jupyter.widget-view+json": {
|
11 |
+
"model_id": "0dd1298de3c84ea3ab8ed31b2a0b2888",
|
12 |
+
"version_major": 2,
|
13 |
+
"version_minor": 0
|
14 |
+
},
|
15 |
+
"text/plain": [
|
16 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
"metadata": {},
|
20 |
+
"output_type": "display_data"
|
21 |
+
}
|
22 |
+
],
|
23 |
+
"source": [
|
24 |
+
"import torch\n",
|
25 |
+
"from multiprocessing import set_start_method\n",
|
26 |
+
"from transformers import Blip2Processor, Blip2ForConditionalGeneration\n",
|
27 |
+
"from datasets import load_dataset\n",
|
28 |
+
"\n",
|
29 |
+
"# Load BLIP-2 model and processor\n",
|
30 |
+
"processor = Blip2Processor.from_pretrained(\"Salesforce/blip2-opt-2.7b\")\n",
|
31 |
+
"model = Blip2ForConditionalGeneration.from_pretrained(\"Salesforce/blip2-opt-2.7b\", torch_dtype=torch.float16)"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"cell_type": "code",
|
36 |
+
"execution_count": 11,
|
37 |
+
"metadata": {},
|
38 |
+
"outputs": [],
|
39 |
+
"source": [
|
40 |
+
"def gpu_computation(batch, rank):\n",
|
41 |
+
" device = f\"cuda:{(rank or 0) % torch.cuda.device_count()}\"\n",
|
42 |
+
" model.to(device)\n",
|
43 |
+
" inputs = processor(images=batch[\"image\"], return_tensors=\"pt\").to(device, torch.float16)\n",
|
44 |
+
"\n",
|
45 |
+
" with torch.no_grad():\n",
|
46 |
+
" generated_ids = model.generate(**inputs, max_length=51)\n",
|
47 |
+
" \n",
|
48 |
+
" batch[\"caption\"] = processor.batch_decode(generated_ids, skip_special_tokens=True)\n",
|
49 |
+
" return batch"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": 12,
|
55 |
+
"metadata": {},
|
56 |
+
"outputs": [
|
57 |
+
{
|
58 |
+
"data": {
|
59 |
+
"application/vnd.jupyter.widget-view+json": {
|
60 |
+
"model_id": "61fe62d696904a7c894bd2c6f082b426",
|
61 |
+
"version_major": 2,
|
62 |
+
"version_minor": 0
|
63 |
+
},
|
64 |
+
"text/plain": [
|
65 |
+
"Map: 0%| | 0/10 [00:00<?, ? examples/s]"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
"metadata": {},
|
69 |
+
"output_type": "display_data"
|
70 |
+
}
|
71 |
+
],
|
72 |
+
"source": [
|
73 |
+
"import multiprocessing\n",
|
74 |
+
"\n",
|
75 |
+
"\n",
|
76 |
+
"if __name__ == \"__main__\":\n",
|
77 |
+
" # Check if start method is already set\n",
|
78 |
+
" try:\n",
|
79 |
+
" multiprocessing.get_start_method()\n",
|
80 |
+
" except RuntimeError:\n",
|
81 |
+
" multiprocessing.set_start_method(\"spawn\")\n",
|
82 |
+
"\n",
|
83 |
+
" # Load your dataset\n",
|
84 |
+
" dataset = load_dataset(\"visual-layer/oxford-iiit-pet-vl-enriched\", split=\"train\")\n",
|
85 |
+
" dataset = dataset.select(range(10))\n",
|
86 |
+
"\n",
|
87 |
+
" updated_dataset = dataset.map(\n",
|
88 |
+
" gpu_computation,\n",
|
89 |
+
" batched=True,\n",
|
90 |
+
" batch_size=4, # Adjust based on your GPU memory\n",
|
91 |
+
" with_rank=True,\n",
|
92 |
+
" num_proc=torch.cuda.device_count(), # one process per GPU\n",
|
93 |
+
" )"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"cell_type": "code",
|
98 |
+
"execution_count": 13,
|
99 |
+
"metadata": {},
|
100 |
+
"outputs": [
|
101 |
+
{
|
102 |
+
"data": {
|
103 |
+
"text/plain": [
|
104 |
+
"['a cat walking on grass\\n',\n",
|
105 |
+
" 'a white dog playing with a ball\\n',\n",
|
106 |
+
" 'a dog sitting in the grass\\n',\n",
|
107 |
+
" 'a dog laying in the grass\\n',\n",
|
108 |
+
" 'a dog standing in the snow\\n',\n",
|
109 |
+
" 'a dog laying in the grass\\n',\n",
|
110 |
+
" 'a dog laying on a brick sidewalk\\n',\n",
|
111 |
+
" 'a man holding a black dog\\n',\n",
|
112 |
+
" 'a large dog standing in the grass\\n',\n",
|
113 |
+
" 'a pug dog with its tongue out standing on a tiled floor\\n']"
|
114 |
+
]
|
115 |
+
},
|
116 |
+
"execution_count": 13,
|
117 |
+
"metadata": {},
|
118 |
+
"output_type": "execute_result"
|
119 |
+
}
|
120 |
+
],
|
121 |
+
"source": [
|
122 |
+
"updated_dataset['caption']"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"cell_type": "code",
|
127 |
+
"execution_count": null,
|
128 |
+
"metadata": {},
|
129 |
+
"outputs": [],
|
130 |
+
"source": []
|
131 |
+
}
|
132 |
+
],
|
133 |
+
"metadata": {
|
134 |
+
"kernelspec": {
|
135 |
+
"display_name": "Python 3",
|
136 |
+
"language": "python",
|
137 |
+
"name": "python3"
|
138 |
+
},
|
139 |
+
"language_info": {
|
140 |
+
"codemirror_mode": {
|
141 |
+
"name": "ipython",
|
142 |
+
"version": 3
|
143 |
+
},
|
144 |
+
"file_extension": ".py",
|
145 |
+
"mimetype": "text/x-python",
|
146 |
+
"name": "python",
|
147 |
+
"nbconvert_exporter": "python",
|
148 |
+
"pygments_lexer": "ipython3",
|
149 |
+
"version": "3.10.12"
|
150 |
+
}
|
151 |
+
},
|
152 |
+
"nbformat": 4,
|
153 |
+
"nbformat_minor": 2
|
154 |
+
}
|