Skip to content

Commit 272f550

Browse files
committedMar 30, 2024
Gemaakt met Colaboratory
1 parent d6480b1 commit 272f550

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed
 

‎BEiT/Understanding_BeitForMaskedImageModeling.ipynb

+35-38
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"colab": {
66
"name": "Understanding BeitForMaskedImageModeling.ipynb",
77
"provenance": [],
8-
"collapsed_sections": [],
9-
"authorship_tag": "ABX9TyML+527/GMXCF12tSDUgQFX",
8+
"authorship_tag": "ABX9TyMNKksEST+khtV9qo1CbZT9",
109
"include_colab_link": true
1110
},
1211
"kernelspec": {
@@ -737,7 +736,7 @@
737736
"source": [
738737
"!pip install -q transformers"
739738
],
740-
"execution_count": 8,
739+
"execution_count": null,
741740
"outputs": [
742741
{
743742
"output_type": "stream",
@@ -765,7 +764,7 @@
765764
"source": [
766765
"!git clone https://github.com/microsoft/unilm.git"
767766
],
768-
"execution_count": 2,
767+
"execution_count": null,
769768
"outputs": [
770769
{
771770
"output_type": "stream",
@@ -790,7 +789,7 @@
790789
"source": [
791790
"!pip install -q einops"
792791
],
793-
"execution_count": 3,
792+
"execution_count": null,
794793
"outputs": []
795794
},
796795
{
@@ -805,7 +804,7 @@
805804
"source": [
806805
"!pip install -q DALL-E"
807806
],
808-
"execution_count": 4,
807+
"execution_count": null,
809808
"outputs": [
810809
{
811810
"output_type": "stream",
@@ -836,7 +835,7 @@
836835
"source": [
837836
"%cd unilm/beit"
838837
],
839-
"execution_count": 5,
838+
"execution_count": null,
840839
"outputs": [
841840
{
842841
"output_type": "stream",
@@ -877,7 +876,7 @@
877876
"\n",
878877
"image"
879878
],
880-
"execution_count": 6,
879+
"execution_count": null,
881880
"outputs": [
882881
{
883882
"output_type": "execute_result",
@@ -917,15 +916,15 @@
917916
"outputId": "02c13ee1-11ba-4157-f15d-2d23162b66e7"
918917
},
919918
"source": [
920-
"from transformers import BeitFeatureExtractor\n",
919+
"from transformers import BeitImageProcessor\n",
921920
"\n",
922-
"feature_extractor = BeitFeatureExtractor()\n",
921+
"image_processor = BeitImageProcessor()\n",
923922
"\n",
924923
"# create input 1 (pixel_values)\n",
925-
"pixel_values = feature_extractor(image, return_tensors=\"pt\").pixel_values\n",
924+
"pixel_values = image_processor(image, return_tensors=\"pt\").pixel_values\n",
926925
"pixel_values.shape"
927926
],
928-
"execution_count": 9,
927+
"execution_count": null,
929928
"outputs": [
930929
{
931930
"output_type": "execute_result",
@@ -961,7 +960,7 @@
961960
"pixel_values_dall_e = visual_token_transform(image).unsqueeze(0)\n",
962961
"pixel_values_dall_e.shape"
963962
],
964-
"execution_count": 10,
963+
"execution_count": null,
965964
"outputs": [
966965
{
967966
"output_type": "execute_result",
@@ -985,7 +984,7 @@
985984
"!mkdir -p dall_e_tokenizer\n",
986985
"!wget -o dall_e_tokenizer/encoder.pkl https://cdn.openai.com/dall-e/encoder.pkl"
987986
],
988-
"execution_count": 11,
987+
"execution_count": null,
989988
"outputs": []
990989
},
991990
{
@@ -1027,7 +1026,7 @@
10271026
"\n",
10281027
"model = BeitForMaskedImageModeling.from_pretrained(\"microsoft/beit-base-patch16-224-pt22k\")"
10291028
],
1030-
"execution_count": 12,
1029+
"execution_count": null,
10311030
"outputs": [
10321031
{
10331032
"output_type": "display_data",
@@ -1079,7 +1078,7 @@
10791078
" min_num_patches=min_mask_patches_per_block,\n",
10801079
" )"
10811080
],
1082-
"execution_count": 13,
1081+
"execution_count": null,
10831082
"outputs": []
10841083
},
10851084
{
@@ -1094,7 +1093,7 @@
10941093
"bool_masked_pos = mask_generator()\n",
10951094
"bool_masked_pos = torch.from_numpy(bool_masked_pos).unsqueeze(0)"
10961095
],
1097-
"execution_count": 34,
1096+
"execution_count": null,
10981097
"outputs": []
10991098
},
11001099
{
@@ -1109,7 +1108,7 @@
11091108
"source": [
11101109
"bool_masked_pos.shape"
11111110
],
1112-
"execution_count": 35,
1111+
"execution_count": null,
11131112
"outputs": [
11141113
{
11151114
"output_type": "execute_result",
@@ -1129,14 +1128,14 @@
11291128
"id": "fFad_m_s41Ru"
11301129
},
11311130
"source": [
1132-
"from dall_e import map_pixels, load_model \n",
1131+
"from dall_e import map_pixels, load_model\n",
11331132
"import torch\n",
11341133
"\n",
11351134
"# step 2: get input_ids from OpenAI's DALL-E\n",
11361135
"device = torch.device('cpu')\n",
11371136
"encoder = load_model(\"https://cdn.openai.com/dall-e/encoder.pkl\", device)"
11381137
],
1139-
"execution_count": 16,
1138+
"execution_count": null,
11401139
"outputs": []
11411140
},
11421141
{
@@ -1151,7 +1150,7 @@
11511150
"source": [
11521151
"pixel_values_dall_e.shape"
11531152
],
1154-
"execution_count": 17,
1153+
"execution_count": null,
11551154
"outputs": [
11561155
{
11571156
"output_type": "execute_result",
@@ -1177,7 +1176,7 @@
11771176
" bool_masked_pos = bool_masked_pos.flatten(1).to(torch.bool)\n",
11781177
" labels = input_ids[bool_masked_pos]"
11791178
],
1180-
"execution_count": 37,
1179+
"execution_count": null,
11811180
"outputs": []
11821181
},
11831182
{
@@ -1192,7 +1191,7 @@
11921191
"source": [
11931192
"input_ids.shape"
11941193
],
1195-
"execution_count": 38,
1194+
"execution_count": null,
11961195
"outputs": [
11971196
{
11981197
"output_type": "execute_result",
@@ -1218,7 +1217,7 @@
12181217
"source": [
12191218
"labels.shape"
12201219
],
1221-
"execution_count": 39,
1220+
"execution_count": null,
12221221
"outputs": [
12231222
{
12241223
"output_type": "execute_result",
@@ -1249,7 +1248,7 @@
12491248
"source": [
12501249
"outputs = model(pixel_values, bool_masked_pos)"
12511250
],
1252-
"execution_count": 40,
1251+
"execution_count": null,
12531252
"outputs": []
12541253
},
12551254
{
@@ -1264,7 +1263,7 @@
12641263
"source": [
12651264
"labels"
12661265
],
1267-
"execution_count": 41,
1266+
"execution_count": null,
12681267
"outputs": [
12691268
{
12701269
"output_type": "execute_result",
@@ -1292,7 +1291,7 @@
12921291
"source": [
12931292
"predictions = outputs.logits[bool_masked_pos].argmax(-1)"
12941293
],
1295-
"execution_count": 42,
1294+
"execution_count": null,
12961295
"outputs": []
12971296
},
12981297
{
@@ -1307,7 +1306,7 @@
13071306
"source": [
13081307
"predictions"
13091308
],
1310-
"execution_count": 43,
1309+
"execution_count": null,
13111310
"outputs": [
13121311
{
13131312
"output_type": "execute_result",
@@ -1356,13 +1355,13 @@
13561355
"# prepare for model (simply resize + normalize)\n",
13571356
"mean = (0.5, 0.5, 0.5)\n",
13581357
"std = (0.5, 0.5, 0.5)\n",
1359-
"transform = transforms.Compose([transforms.Resize((224, 224)), \n",
1360-
" transforms.ToTensor(), \n",
1358+
"transform = transforms.Compose([transforms.Resize((224, 224)),\n",
1359+
" transforms.ToTensor(),\n",
13611360
" transforms.Normalize(mean, std)])\n",
13621361
"pixel_values = transform(image).unsqueeze(0)\n",
13631362
"pixel_values.shape"
13641363
],
1365-
"execution_count": 25,
1364+
"execution_count": null,
13661365
"outputs": [
13671366
{
13681367
"output_type": "execute_result",
@@ -1388,7 +1387,7 @@
13881387
"source": [
13891388
"pixel_values[0,:3,:3,:3]"
13901389
],
1391-
"execution_count": 26,
1390+
"execution_count": null,
13921391
"outputs": [
13931392
{
13941393
"output_type": "execute_result",
@@ -1426,7 +1425,7 @@
14261425
"# forward pass\n",
14271426
"outputs = model(pixel_values, bool_masked_pos)"
14281427
],
1429-
"execution_count": 27,
1428+
"execution_count": null,
14301429
"outputs": []
14311430
},
14321431
{
@@ -1441,7 +1440,7 @@
14411440
"source": [
14421441
"outputs.logits.shape"
14431442
],
1444-
"execution_count": 28,
1443+
"execution_count": null,
14451444
"outputs": [
14461445
{
14471446
"output_type": "execute_result",
@@ -1467,7 +1466,7 @@
14671466
"source": [
14681467
"outputs.logits[bool_masked_pos][:3,:3]"
14691468
],
1470-
"execution_count": 29,
1469+
"execution_count": null,
14711470
"outputs": [
14721471
{
14731472
"output_type": "execute_result",
@@ -1488,9 +1487,7 @@
14881487
"metadata": {
14891488
"id": "D-8mymKCOQZK"
14901489
},
1491-
"source": [
1492-
""
1493-
],
1490+
"source": [],
14941491
"execution_count": null,
14951492
"outputs": []
14961493
}

0 commit comments

Comments
 (0)
Please sign in to comment.