{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Transformer-explainability.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyOZAljZCH9K62jPH5tqgQlf",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Zj6EnzRyAY5q"
},
"source": [
"# **Transformer Interpretability Beyond Attention Visualization**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ej3H_oMmAx3C",
"outputId": "859266b9-ae95-4dc1-dfd9-44853e43855a"
},
"source": [
"!git clone https://github.com/hila-chefer/Transformer-Explainability.git\n",
"\n",
"import os\n",
"os.chdir(f'./Transformer-Explainability')\n",
"\n",
"!pip install einops"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"fatal: destination path 'Transformer-Explainability' already exists and is not an empty directory.\n",
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: einops in /usr/local/lib/python3.7/dist-packages (0.6.0)\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "IdJ4YOiTBtAz"
},
"source": [
"from PIL import Image\n",
"import torchvision.transforms as transforms\n",
"import matplotlib.pyplot as plt\n",
"import torch\n",
"import numpy as np\n",
"import cv2"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"cellView": "form",
"id": "TtqMdXdTEKAP"
},
"source": [
"#@title Imagenet class indices to names\n",
"%%capture\n",
"CLS2IDX = {0: 'tench, Tinca tinca',\n",
" 1: 'goldfish, Carassius auratus',\n",
" 2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',\n",
" 3: 'tiger shark, Galeocerdo cuvieri',\n",
" 4: 'hammerhead, hammerhead shark',\n",
" 5: 'electric ray, crampfish, numbfish, torpedo',\n",
" 6: 'stingray',\n",
" 7: 'cock',\n",
" 8: 'hen',\n",
" 9: 'ostrich, Struthio camelus',\n",
" 10: 'brambling, Fringilla montifringilla',\n",
" 11: 'goldfinch, Carduelis carduelis',\n",
" 12: 'house finch, linnet, Carpodacus mexicanus',\n",
" 13: 'junco, snowbird',\n",
" 14: 'indigo bunting, indigo finch, indigo bird, Passerina cyanea',\n",
" 15: 'robin, American robin, Turdus migratorius',\n",
" 16: 'bulbul',\n",
" 17: 'jay',\n",
" 18: 'magpie',\n",
" 19: 'chickadee',\n",
" 20: 'water ouzel, dipper',\n",
" 21: 'kite',\n",
" 22: 'bald eagle, American eagle, Haliaeetus leucocephalus',\n",
" 23: 'vulture',\n",
" 24: 'great grey owl, great gray owl, Strix nebulosa',\n",
" 25: 'European fire salamander, Salamandra salamandra',\n",
" 26: 'common newt, Triturus vulgaris',\n",
" 27: 'eft',\n",
" 28: 'spotted salamander, Ambystoma maculatum',\n",
" 29: 'axolotl, mud puppy, Ambystoma mexicanum',\n",
" 30: 'bullfrog, Rana catesbeiana',\n",
" 31: 'tree frog, tree-frog',\n",
" 32: 'tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui',\n",
" 33: 'loggerhead, loggerhead turtle, Caretta caretta',\n",
" 34: 'leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea',\n",
" 35: 'mud turtle',\n",
" 36: 'terrapin',\n",
" 37: 'box turtle, box tortoise',\n",
" 38: 'banded gecko',\n",
" 39: 'common iguana, iguana, Iguana iguana',\n",
" 40: 'American chameleon, anole, Anolis carolinensis',\n",
" 41: 'whiptail, whiptail lizard',\n",
" 42: 'agama',\n",
" 43: 'frilled lizard, Chlamydosaurus kingi',\n",
" 44: 'alligator lizard',\n",
" 45: 'Gila monster, Heloderma suspectum',\n",
" 46: 'green lizard, Lacerta viridis',\n",
" 47: 'African chameleon, Chamaeleo chamaeleon',\n",
" 48: 'Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis',\n",
" 49: 'African crocodile, Nile crocodile, Crocodylus niloticus',\n",
" 50: 'American alligator, Alligator mississipiensis',\n",
" 51: 'triceratops',\n",
" 52: 'thunder snake, worm snake, Carphophis amoenus',\n",
" 53: 'ringneck snake, ring-necked snake, ring snake',\n",
" 54: 'hognose snake, puff adder, sand viper',\n",
" 55: 'green snake, grass snake',\n",
" 56: 'king snake, kingsnake',\n",
" 57: 'garter snake, grass snake',\n",
" 58: 'water snake',\n",
" 59: 'vine snake',\n",
" 60: 'night snake, Hypsiglena torquata',\n",
" 61: 'boa constrictor, Constrictor constrictor',\n",
" 62: 'rock python, rock snake, Python sebae',\n",
" 63: 'Indian cobra, Naja naja',\n",
" 64: 'green mamba',\n",
" 65: 'sea snake',\n",
" 66: 'horned viper, cerastes, sand viper, horned asp, Cerastes cornutus',\n",
" 67: 'diamondback, diamondback rattlesnake, Crotalus adamanteus',\n",
" 68: 'sidewinder, horned rattlesnake, Crotalus cerastes',\n",
" 69: 'trilobite',\n",
" 70: 'harvestman, daddy longlegs, Phalangium opilio',\n",
" 71: 'scorpion',\n",
" 72: 'black and gold garden spider, Argiope aurantia',\n",
" 73: 'barn spider, Araneus cavaticus',\n",
" 74: 'garden spider, Aranea diademata',\n",
" 75: 'black widow, Latrodectus mactans',\n",
" 76: 'tarantula',\n",
" 77: 'wolf spider, hunting spider',\n",
" 78: 'tick',\n",
" 79: 'centipede',\n",
" 80: 'black grouse',\n",
" 81: 'ptarmigan',\n",
" 82: 'ruffed grouse, partridge, Bonasa umbellus',\n",
" 83: 'prairie chicken, prairie grouse, prairie fowl',\n",
" 84: 'peacock',\n",
" 85: 'quail',\n",
" 86: 'partridge',\n",
" 87: 'African grey, African gray, Psittacus erithacus',\n",
" 88: 'macaw',\n",
" 89: 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',\n",
" 90: 'lorikeet',\n",
" 91: 'coucal',\n",
" 92: 'bee eater',\n",
" 93: 'hornbill',\n",
" 94: 'hummingbird',\n",
" 95: 'jacamar',\n",
" 96: 'toucan',\n",
" 97: 'drake',\n",
" 98: 'red-breasted merganser, Mergus serrator',\n",
" 99: 'goose',\n",
" 100: 'black swan, Cygnus atratus',\n",
" 101: 'tusker',\n",
" 102: 'echidna, spiny anteater, anteater',\n",
" 103: 'platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus',\n",
" 104: 'wallaby, brush kangaroo',\n",
" 105: 'koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus',\n",
" 106: 'wombat',\n",
" 107: 'jellyfish',\n",
" 108: 'sea anemone, anemone',\n",
" 109: 'brain coral',\n",
" 110: 'flatworm, platyhelminth',\n",
" 111: 'nematode, nematode worm, roundworm',\n",
" 112: 'conch',\n",
" 113: 'snail',\n",
" 114: 'slug',\n",
" 115: 'sea slug, nudibranch',\n",
" 116: 'chiton, coat-of-mail shell, sea cradle, polyplacophore',\n",
" 117: 'chambered nautilus, pearly nautilus, nautilus',\n",
" 118: 'Dungeness crab, Cancer magister',\n",
" 119: 'rock crab, Cancer irroratus',\n",
" 120: 'fiddler crab',\n",
" 121: 'king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica',\n",
" 122: 'American lobster, Northern lobster, Maine lobster, Homarus americanus',\n",
" 123: 'spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish',\n",
" 124: 'crayfish, crawfish, crawdad, crawdaddy',\n",
" 125: 'hermit crab',\n",
" 126: 'isopod',\n",
" 127: 'white stork, Ciconia ciconia',\n",
" 128: 'black stork, Ciconia nigra',\n",
" 129: 'spoonbill',\n",
" 130: 'flamingo',\n",
" 131: 'little blue heron, Egretta caerulea',\n",
" 132: 'American egret, great white heron, Egretta albus',\n",
" 133: 'bittern',\n",
" 134: 'crane',\n",
" 135: 'limpkin, Aramus pictus',\n",
" 136: 'European gallinule, Porphyrio porphyrio',\n",
" 137: 'American coot, marsh hen, mud hen, water hen, Fulica americana',\n",
" 138: 'bustard',\n",
" 139: 'ruddy turnstone, Arenaria interpres',\n",
" 140: 'red-backed sandpiper, dunlin, Erolia alpina',\n",
" 141: 'redshank, Tringa totanus',\n",
" 142: 'dowitcher',\n",
" 143: 'oystercatcher, oyster catcher',\n",
" 144: 'pelican',\n",
" 145: 'king penguin, Aptenodytes patagonica',\n",
" 146: 'albatross, mollymawk',\n",
" 147: 'grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus',\n",
" 148: 'killer whale, killer, orca, grampus, sea wolf, Orcinus orca',\n",
" 149: 'dugong, Dugong dugon',\n",
" 150: 'sea lion',\n",
" 151: 'Chihuahua',\n",
" 152: 'Japanese spaniel',\n",
" 153: 'Maltese dog, Maltese terrier, Maltese',\n",
" 154: 'Pekinese, Pekingese, Peke',\n",
" 155: 'Shih-Tzu',\n",
" 156: 'Blenheim spaniel',\n",
" 157: 'papillon',\n",
" 158: 'toy terrier',\n",
" 159: 'Rhodesian ridgeback',\n",
" 160: 'Afghan hound, Afghan',\n",
" 161: 'basset, basset hound',\n",
" 162: 'beagle',\n",
" 163: 'bloodhound, sleuthhound',\n",
" 164: 'bluetick',\n",
" 165: 'black-and-tan coonhound',\n",
" 166: 'Walker hound, Walker foxhound',\n",
" 167: 'English foxhound',\n",
" 168: 'redbone',\n",
" 169: 'borzoi, Russian wolfhound',\n",
" 170: 'Irish wolfhound',\n",
" 171: 'Italian greyhound',\n",
" 172: 'whippet',\n",
" 173: 'Ibizan hound, Ibizan Podenco',\n",
" 174: 'Norwegian elkhound, elkhound',\n",
" 175: 'otterhound, otter hound',\n",
" 176: 'Saluki, gazelle hound',\n",
" 177: 'Scottish deerhound, deerhound',\n",
" 178: 'Weimaraner',\n",
" 179: 'Staffordshire bullterrier, Staffordshire bull terrier',\n",
" 180: 'American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier',\n",
" 181: 'Bedlington terrier',\n",
" 182: 'Border terrier',\n",
" 183: 'Kerry blue terrier',\n",
" 184: 'Irish terrier',\n",
" 185: 'Norfolk terrier',\n",
" 186: 'Norwich terrier',\n",
" 187: 'Yorkshire terrier',\n",
" 188: 'wire-haired fox terrier',\n",
" 189: 'Lakeland terrier',\n",
" 190: 'Sealyham terrier, Sealyham',\n",
" 191: 'Airedale, Airedale terrier',\n",
" 192: 'cairn, cairn terrier',\n",
" 193: 'Australian terrier',\n",
" 194: 'Dandie Dinmont, Dandie Dinmont terrier',\n",
" 195: 'Boston bull, Boston terrier',\n",
" 196: 'miniature schnauzer',\n",
" 197: 'giant schnauzer',\n",
" 198: 'standard schnauzer',\n",
" 199: 'Scotch terrier, Scottish terrier, Scottie',\n",
" 200: 'Tibetan terrier, chrysanthemum dog',\n",
" 201: 'silky terrier, Sydney silky',\n",
" 202: 'soft-coated wheaten terrier',\n",
" 203: 'West Highland white terrier',\n",
" 204: 'Lhasa, Lhasa apso',\n",
" 205: 'flat-coated retriever',\n",
" 206: 'curly-coated retriever',\n",
" 207: 'golden retriever',\n",
" 208: 'Labrador retriever',\n",
" 209: 'Chesapeake Bay retriever',\n",
" 210: 'German short-haired pointer',\n",
" 211: 'vizsla, Hungarian pointer',\n",
" 212: 'English setter',\n",
" 213: 'Irish setter, red setter',\n",
" 214: 'Gordon setter',\n",
" 215: 'Brittany spaniel',\n",
" 216: 'clumber, clumber spaniel',\n",
" 217: 'English springer, English springer spaniel',\n",
" 218: 'Welsh springer spaniel',\n",
" 219: 'cocker spaniel, English cocker spaniel, cocker',\n",
" 220: 'Sussex spaniel',\n",
" 221: 'Irish water spaniel',\n",
" 222: 'kuvasz',\n",
" 223: 'schipperke',\n",
" 224: 'groenendael',\n",
" 225: 'malinois',\n",
" 226: 'briard',\n",
" 227: 'kelpie',\n",
" 228: 'komondor',\n",
" 229: 'Old English sheepdog, bobtail',\n",
" 230: 'Shetland sheepdog, Shetland sheep dog, Shetland',\n",
" 231: 'collie',\n",
" 232: 'Border collie',\n",
" 233: 'Bouvier des Flandres, Bouviers des Flandres',\n",
" 234: 'Rottweiler',\n",
" 235: 'German shepherd, German shepherd dog, German police dog, alsatian',\n",
" 236: 'Doberman, Doberman pinscher',\n",
" 237: 'miniature pinscher',\n",
" 238: 'Greater Swiss Mountain dog',\n",
" 239: 'Bernese mountain dog',\n",
" 240: 'Appenzeller',\n",
" 241: 'EntleBucher',\n",
" 242: 'boxer',\n",
" 243: 'bull mastiff',\n",
" 244: 'Tibetan mastiff',\n",
" 245: 'French bulldog',\n",
" 246: 'Great Dane',\n",
" 247: 'Saint Bernard, St Bernard',\n",
" 248: 'Eskimo dog, husky',\n",
" 249: 'malamute, malemute, Alaskan malamute',\n",
" 250: 'Siberian husky',\n",
" 251: 'dalmatian, coach dog, carriage dog',\n",
" 252: 'affenpinscher, monkey pinscher, monkey dog',\n",
" 253: 'basenji',\n",
" 254: 'pug, pug-dog',\n",
" 255: 'Leonberg',\n",
" 256: 'Newfoundland, Newfoundland dog',\n",
" 257: 'Great Pyrenees',\n",
" 258: 'Samoyed, Samoyede',\n",
" 259: 'Pomeranian',\n",
" 260: 'chow, chow chow',\n",
" 261: 'keeshond',\n",
" 262: 'Brabancon griffon',\n",
" 263: 'Pembroke, Pembroke Welsh corgi',\n",
" 264: 'Cardigan, Cardigan Welsh corgi',\n",
" 265: 'toy poodle',\n",
" 266: 'miniature poodle',\n",
" 267: 'standard poodle',\n",
" 268: 'Mexican hairless',\n",
" 269: 'timber wolf, grey wolf, gray wolf, Canis lupus',\n",
" 270: 'white wolf, Arctic wolf, Canis lupus tundrarum',\n",
" 271: 'red wolf, maned wolf, Canis rufus, Canis niger',\n",
" 272: 'coyote, prairie wolf, brush wolf, Canis latrans',\n",
" 273: 'dingo, warrigal, warragal, Canis dingo',\n",
" 274: 'dhole, Cuon alpinus',\n",
" 275: 'African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus',\n",
" 276: 'hyena, hyaena',\n",
" 277: 'red fox, Vulpes vulpes',\n",
" 278: 'kit fox, Vulpes macrotis',\n",
" 279: 'Arctic fox, white fox, Alopex lagopus',\n",
" 280: 'grey fox, gray fox, Urocyon cinereoargenteus',\n",
" 281: 'tabby, tabby cat',\n",
" 282: 'tiger cat',\n",
" 283: 'Persian cat',\n",
" 284: 'Siamese cat, Siamese',\n",
" 285: 'Egyptian cat',\n",
" 286: 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',\n",
" 287: 'lynx, catamount',\n",
" 288: 'leopard, Panthera pardus',\n",
" 289: 'snow leopard, ounce, Panthera uncia',\n",
" 290: 'jaguar, panther, Panthera onca, Felis onca',\n",
" 291: 'lion, king of beasts, Panthera leo',\n",
" 292: 'tiger, Panthera tigris',\n",
" 293: 'cheetah, chetah, Acinonyx jubatus',\n",
" 294: 'brown bear, bruin, Ursus arctos',\n",
" 295: 'American black bear, black bear, Ursus americanus, Euarctos americanus',\n",
" 296: 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus',\n",
" 297: 'sloth bear, Melursus ursinus, Ursus ursinus',\n",
" 298: 'mongoose',\n",
" 299: 'meerkat, mierkat',\n",
" 300: 'tiger beetle',\n",
" 301: 'ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle',\n",
" 302: 'ground beetle, carabid beetle',\n",
" 303: 'long-horned beetle, longicorn, longicorn beetle',\n",
" 304: 'leaf beetle, chrysomelid',\n",
" 305: 'dung beetle',\n",
" 306: 'rhinoceros beetle',\n",
" 307: 'weevil',\n",
" 308: 'fly',\n",
" 309: 'bee',\n",
" 310: 'ant, emmet, pismire',\n",
" 311: 'grasshopper, hopper',\n",
" 312: 'cricket',\n",
" 313: 'walking stick, walkingstick, stick insect',\n",
" 314: 'cockroach, roach',\n",
" 315: 'mantis, mantid',\n",
" 316: 'cicada, cicala',\n",
" 317: 'leafhopper',\n",
" 318: 'lacewing, lacewing fly',\n",
" 319: \"dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk\",\n",
" 320: 'damselfly',\n",
" 321: 'admiral',\n",
" 322: 'ringlet, ringlet butterfly',\n",
" 323: 'monarch, monarch butterfly, milkweed butterfly, Danaus plexippus',\n",
" 324: 'cabbage butterfly',\n",
" 325: 'sulphur butterfly, sulfur butterfly',\n",
" 326: 'lycaenid, lycaenid butterfly',\n",
" 327: 'starfish, sea star',\n",
" 328: 'sea urchin',\n",
" 329: 'sea cucumber, holothurian',\n",
" 330: 'wood rabbit, cottontail, cottontail rabbit',\n",
" 331: 'hare',\n",
" 332: 'Angora, Angora rabbit',\n",
" 333: 'hamster',\n",
" 334: 'porcupine, hedgehog',\n",
" 335: 'fox squirrel, eastern fox squirrel, Sciurus niger',\n",
" 336: 'marmot',\n",
" 337: 'beaver',\n",
" 338: 'guinea pig, Cavia cobaya',\n",
" 339: 'sorrel',\n",
" 340: 'zebra',\n",
" 341: 'hog, pig, grunter, squealer, Sus scrofa',\n",
" 342: 'wild boar, boar, Sus scrofa',\n",
" 343: 'warthog',\n",
" 344: 'hippopotamus, hippo, river horse, Hippopotamus amphibius',\n",
" 345: 'ox',\n",
" 346: 'water buffalo, water ox, Asiatic buffalo, Bubalus bubalis',\n",
" 347: 'bison',\n",
" 348: 'ram, tup',\n",
" 349: 'bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis',\n",
" 350: 'ibex, Capra ibex',\n",
" 351: 'hartebeest',\n",
" 352: 'impala, Aepyceros melampus',\n",
" 353: 'gazelle',\n",
" 354: 'Arabian camel, dromedary, Camelus dromedarius',\n",
" 355: 'llama',\n",
" 356: 'weasel',\n",
" 357: 'mink',\n",
" 358: 'polecat, fitch, foulmart, foumart, Mustela putorius',\n",
" 359: 'black-footed ferret, ferret, Mustela nigripes',\n",
" 360: 'otter',\n",
" 361: 'skunk, polecat, wood pussy',\n",
" 362: 'badger',\n",
" 363: 'armadillo',\n",
" 364: 'three-toed sloth, ai, Bradypus tridactylus',\n",
" 365: 'orangutan, orang, orangutang, Pongo pygmaeus',\n",
" 366: 'gorilla, Gorilla gorilla',\n",
" 367: 'chimpanzee, chimp, Pan troglodytes',\n",
" 368: 'gibbon, Hylobates lar',\n",
" 369: 'siamang, Hylobates syndactylus, Symphalangus syndactylus',\n",
" 370: 'guenon, guenon monkey',\n",
" 371: 'patas, hussar monkey, Erythrocebus patas',\n",
" 372: 'baboon',\n",
" 373: 'macaque',\n",
" 374: 'langur',\n",
" 375: 'colobus, colobus monkey',\n",
" 376: 'proboscis monkey, Nasalis larvatus',\n",
" 377: 'marmoset',\n",
" 378: 'capuchin, ringtail, Cebus capucinus',\n",
" 379: 'howler monkey, howler',\n",
" 380: 'titi, titi monkey',\n",
" 381: 'spider monkey, Ateles geoffroyi',\n",
" 382: 'squirrel monkey, Saimiri sciureus',\n",
" 383: 'Madagascar cat, ring-tailed lemur, Lemur catta',\n",
" 384: 'indri, indris, Indri indri, Indri brevicaudatus',\n",
" 385: 'Indian elephant, Elephas maximus',\n",
" 386: 'African elephant, Loxodonta africana',\n",
" 387: 'lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens',\n",
" 388: 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca',\n",
" 389: 'barracouta, snoek',\n",
" 390: 'eel',\n",
" 391: 'coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch',\n",
" 392: 'rock beauty, Holocanthus tricolor',\n",
" 393: 'anemone fish',\n",
" 394: 'sturgeon',\n",
" 395: 'gar, garfish, garpike, billfish, Lepisosteus osseus',\n",
" 396: 'lionfish',\n",
" 397: 'puffer, pufferfish, blowfish, globefish',\n",
" 398: 'abacus',\n",
" 399: 'abaya',\n",
" 400: \"academic gown, academic robe, judge's robe\",\n",
" 401: 'accordion, piano accordion, squeeze box',\n",
" 402: 'acoustic guitar',\n",
" 403: 'aircraft carrier, carrier, flattop, attack aircraft carrier',\n",
" 404: 'airliner',\n",
" 405: 'airship, dirigible',\n",
" 406: 'altar',\n",
" 407: 'ambulance',\n",
" 408: 'amphibian, amphibious vehicle',\n",
" 409: 'analog clock',\n",
" 410: 'apiary, bee house',\n",
" 411: 'apron',\n",
" 412: 'ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin',\n",
" 413: 'assault rifle, assault gun',\n",
" 414: 'backpack, back pack, knapsack, packsack, rucksack, haversack',\n",
" 415: 'bakery, bakeshop, bakehouse',\n",
" 416: 'balance beam, beam',\n",
" 417: 'balloon',\n",
" 418: 'ballpoint, ballpoint pen, ballpen, Biro',\n",
" 419: 'Band Aid',\n",
" 420: 'banjo',\n",
" 421: 'bannister, banister, balustrade, balusters, handrail',\n",
" 422: 'barbell',\n",
" 423: 'barber chair',\n",
" 424: 'barbershop',\n",
" 425: 'barn',\n",
" 426: 'barometer',\n",
" 427: 'barrel, cask',\n",
" 428: 'barrow, garden cart, lawn cart, wheelbarrow',\n",
" 429: 'baseball',\n",
" 430: 'basketball',\n",
" 431: 'bassinet',\n",
" 432: 'bassoon',\n",
" 433: 'bathing cap, swimming cap',\n",
" 434: 'bath towel',\n",
" 435: 'bathtub, bathing tub, bath, tub',\n",
" 436: 'beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon',\n",
" 437: 'beacon, lighthouse, beacon light, pharos',\n",
" 438: 'beaker',\n",
" 439: 'bearskin, busby, shako',\n",
" 440: 'beer bottle',\n",
" 441: 'beer glass',\n",
" 442: 'bell cote, bell cot',\n",
" 443: 'bib',\n",
" 444: 'bicycle-built-for-two, tandem bicycle, tandem',\n",
" 445: 'bikini, two-piece',\n",
" 446: 'binder, ring-binder',\n",
" 447: 'binoculars, field glasses, opera glasses',\n",
" 448: 'birdhouse',\n",
" 449: 'boathouse',\n",
" 450: 'bobsled, bobsleigh, bob',\n",
" 451: 'bolo tie, bolo, bola tie, bola',\n",
" 452: 'bonnet, poke bonnet',\n",
" 453: 'bookcase',\n",
" 454: 'bookshop, bookstore, bookstall',\n",
" 455: 'bottlecap',\n",
" 456: 'bow',\n",
" 457: 'bow tie, bow-tie, bowtie',\n",
" 458: 'brass, memorial tablet, plaque',\n",
" 459: 'brassiere, bra, bandeau',\n",
" 460: 'breakwater, groin, groyne, mole, bulwark, seawall, jetty',\n",
" 461: 'breastplate, aegis, egis',\n",
" 462: 'broom',\n",
" 463: 'bucket, pail',\n",
" 464: 'buckle',\n",
" 465: 'bulletproof vest',\n",
" 466: 'bullet train, bullet',\n",
" 467: 'butcher shop, meat market',\n",
" 468: 'cab, hack, taxi, taxicab',\n",
" 469: 'caldron, cauldron',\n",
" 470: 'candle, taper, wax light',\n",
" 471: 'cannon',\n",
" 472: 'canoe',\n",
" 473: 'can opener, tin opener',\n",
" 474: 'cardigan',\n",
" 475: 'car mirror',\n",
" 476: 'carousel, carrousel, merry-go-round, roundabout, whirligig',\n",
" 477: \"carpenter's kit, tool kit\",\n",
" 478: 'carton',\n",
" 479: 'car wheel',\n",
" 480: 'cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM',\n",
" 481: 'cassette',\n",
" 482: 'cassette player',\n",
" 483: 'castle',\n",
" 484: 'catamaran',\n",
" 485: 'CD player',\n",
" 486: 'cello, violoncello',\n",
" 487: 'cellular telephone, cellular phone, cellphone, cell, mobile phone',\n",
" 488: 'chain',\n",
" 489: 'chainlink fence',\n",
" 490: 'chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour',\n",
" 491: 'chain saw, chainsaw',\n",
" 492: 'chest',\n",
" 493: 'chiffonier, commode',\n",
" 494: 'chime, bell, gong',\n",
" 495: 'china cabinet, china closet',\n",
" 496: 'Christmas stocking',\n",
" 497: 'church, church building',\n",
" 498: 'cinema, movie theater, movie theatre, movie house, picture palace',\n",
" 499: 'cleaver, meat cleaver, chopper',\n",
" 500: 'cliff dwelling',\n",
" 501: 'cloak',\n",
" 502: 'clog, geta, patten, sabot',\n",
" 503: 'cocktail shaker',\n",
" 504: 'coffee mug',\n",
" 505: 'coffeepot',\n",
" 506: 'coil, spiral, volute, whorl, helix',\n",
" 507: 'combination lock',\n",
" 508: 'computer keyboard, keypad',\n",
" 509: 'confectionery, confectionary, candy store',\n",
" 510: 'container ship, containership, container vessel',\n",
" 511: 'convertible',\n",
" 512: 'corkscrew, bottle screw',\n",
" 513: 'cornet, horn, trumpet, trump',\n",
" 514: 'cowboy boot',\n",
" 515: 'cowboy hat, ten-gallon hat',\n",
" 516: 'cradle',\n",
" 517: 'crane',\n",
" 518: 'crash helmet',\n",
" 519: 'crate',\n",
" 520: 'crib, cot',\n",
" 521: 'Crock Pot',\n",
" 522: 'croquet ball',\n",
" 523: 'crutch',\n",
" 524: 'cuirass',\n",
" 525: 'dam, dike, dyke',\n",
" 526: 'desk',\n",
" 527: 'desktop computer',\n",
" 528: 'dial telephone, dial phone',\n",
" 529: 'diaper, nappy, napkin',\n",
" 530: 'digital clock',\n",
" 531: 'digital watch',\n",
" 532: 'dining table, board',\n",
" 533: 'dishrag, dishcloth',\n",
" 534: 'dishwasher, dish washer, dishwashing machine',\n",
" 535: 'disk brake, disc brake',\n",
" 536: 'dock, dockage, docking facility',\n",
" 537: 'dogsled, dog sled, dog sleigh',\n",
" 538: 'dome',\n",
" 539: 'doormat, welcome mat',\n",
" 540: 'drilling platform, offshore rig',\n",
" 541: 'drum, membranophone, tympan',\n",
" 542: 'drumstick',\n",
" 543: 'dumbbell',\n",
" 544: 'Dutch oven',\n",
" 545: 'electric fan, blower',\n",
" 546: 'electric guitar',\n",
" 547: 'electric locomotive',\n",
" 548: 'entertainment center',\n",
" 549: 'envelope',\n",
" 550: 'espresso maker',\n",
" 551: 'face powder',\n",
" 552: 'feather boa, boa',\n",
" 553: 'file, file cabinet, filing cabinet',\n",
" 554: 'fireboat',\n",
" 555: 'fire engine, fire truck',\n",
" 556: 'fire screen, fireguard',\n",
" 557: 'flagpole, flagstaff',\n",
" 558: 'flute, transverse flute',\n",
" 559: 'folding chair',\n",
" 560: 'football helmet',\n",
" 561: 'forklift',\n",
" 562: 'fountain',\n",
" 563: 'fountain pen',\n",
" 564: 'four-poster',\n",
" 565: 'freight car',\n",
" 566: 'French horn, horn',\n",
" 567: 'frying pan, frypan, skillet',\n",
" 568: 'fur coat',\n",
" 569: 'garbage truck, dustcart',\n",
" 570: 'gasmask, respirator, gas helmet',\n",
" 571: 'gas pump, gasoline pump, petrol pump, island dispenser',\n",
" 572: 'goblet',\n",
" 573: 'go-kart',\n",
" 574: 'golf ball',\n",
" 575: 'golfcart, golf cart',\n",
" 576: 'gondola',\n",
" 577: 'gong, tam-tam',\n",
" 578: 'gown',\n",
" 579: 'grand piano, grand',\n",
" 580: 'greenhouse, nursery, glasshouse',\n",
" 581: 'grille, radiator grille',\n",
" 582: 'grocery store, grocery, food market, market',\n",
" 583: 'guillotine',\n",
" 584: 'hair slide',\n",
" 585: 'hair spray',\n",
" 586: 'half track',\n",
" 587: 'hammer',\n",
" 588: 'hamper',\n",
" 589: 'hand blower, blow dryer, blow drier, hair dryer, hair drier',\n",
" 590: 'hand-held computer, hand-held microcomputer',\n",
" 591: 'handkerchief, hankie, hanky, hankey',\n",
" 592: 'hard disc, hard disk, fixed disk',\n",
" 593: 'harmonica, mouth organ, harp, mouth harp',\n",
" 594: 'harp',\n",
" 595: 'harvester, reaper',\n",
" 596: 'hatchet',\n",
" 597: 'holster',\n",
" 598: 'home theater, home theatre',\n",
" 599: 'honeycomb',\n",
" 600: 'hook, claw',\n",
" 601: 'hoopskirt, crinoline',\n",
" 602: 'horizontal bar, high bar',\n",
" 603: 'horse cart, horse-cart',\n",
" 604: 'hourglass',\n",
" 605: 'iPod',\n",
" 606: 'iron, smoothing iron',\n",
" 607: \"jack-o'-lantern\",\n",
" 608: 'jean, blue jean, denim',\n",
" 609: 'jeep, landrover',\n",
" 610: 'jersey, T-shirt, tee shirt',\n",
" 611: 'jigsaw puzzle',\n",
" 612: 'jinrikisha, ricksha, rickshaw',\n",
" 613: 'joystick',\n",
" 614: 'kimono',\n",
" 615: 'knee pad',\n",
" 616: 'knot',\n",
" 617: 'lab coat, laboratory coat',\n",
" 618: 'ladle',\n",
" 619: 'lampshade, lamp shade',\n",
" 620: 'laptop, laptop computer',\n",
" 621: 'lawn mower, mower',\n",
" 622: 'lens cap, lens cover',\n",
" 623: 'letter opener, paper knife, paperknife',\n",
" 624: 'library',\n",
" 625: 'lifeboat',\n",
" 626: 'lighter, light, igniter, ignitor',\n",
" 627: 'limousine, limo',\n",
" 628: 'liner, ocean liner',\n",
" 629: 'lipstick, lip rouge',\n",
" 630: 'Loafer',\n",
" 631: 'lotion',\n",
" 632: 'loudspeaker, speaker, speaker unit, loudspeaker system, speaker system',\n",
" 633: \"loupe, jeweler's loupe\",\n",
" 634: 'lumbermill, sawmill',\n",
" 635: 'magnetic compass',\n",
" 636: 'mailbag, postbag',\n",
" 637: 'mailbox, letter box',\n",
" 638: 'maillot',\n",
" 639: 'maillot, tank suit',\n",
" 640: 'manhole cover',\n",
" 641: 'maraca',\n",
" 642: 'marimba, xylophone',\n",
" 643: 'mask',\n",
" 644: 'matchstick',\n",
" 645: 'maypole',\n",
" 646: 'maze, labyrinth',\n",
" 647: 'measuring cup',\n",
" 648: 'medicine chest, medicine cabinet',\n",
" 649: 'megalith, megalithic structure',\n",
" 650: 'microphone, mike',\n",
" 651: 'microwave, microwave oven',\n",
" 652: 'military uniform',\n",
" 653: 'milk can',\n",
" 654: 'minibus',\n",
" 655: 'miniskirt, mini',\n",
" 656: 'minivan',\n",
" 657: 'missile',\n",
" 658: 'mitten',\n",
" 659: 'mixing bowl',\n",
" 660: 'mobile home, manufactured home',\n",
" 661: 'Model T',\n",
" 662: 'modem',\n",
" 663: 'monastery',\n",
" 664: 'monitor',\n",
" 665: 'moped',\n",
" 666: 'mortar',\n",
" 667: 'mortarboard',\n",
" 668: 'mosque',\n",
" 669: 'mosquito net',\n",
" 670: 'motor scooter, scooter',\n",
" 671: 'mountain bike, all-terrain bike, off-roader',\n",
" 672: 'mountain tent',\n",
" 673: 'mouse, computer mouse',\n",
" 674: 'mousetrap',\n",
" 675: 'moving van',\n",
" 676: 'muzzle',\n",
" 677: 'nail',\n",
" 678: 'neck brace',\n",
" 679: 'necklace',\n",
" 680: 'nipple',\n",
" 681: 'notebook, notebook computer',\n",
" 682: 'obelisk',\n",
" 683: 'oboe, hautboy, hautbois',\n",
" 684: 'ocarina, sweet potato',\n",
" 685: 'odometer, hodometer, mileometer, milometer',\n",
" 686: 'oil filter',\n",
" 687: 'organ, pipe organ',\n",
" 688: 'oscilloscope, scope, cathode-ray oscilloscope, CRO',\n",
" 689: 'overskirt',\n",
" 690: 'oxcart',\n",
" 691: 'oxygen mask',\n",
" 692: 'packet',\n",
" 693: 'paddle, boat paddle',\n",
" 694: 'paddlewheel, paddle wheel',\n",
" 695: 'padlock',\n",
" 696: 'paintbrush',\n",
" 697: \"pajama, pyjama, pj's, jammies\",\n",
" 698: 'palace',\n",
" 699: 'panpipe, pandean pipe, syrinx',\n",
" 700: 'paper towel',\n",
" 701: 'parachute, chute',\n",
" 702: 'parallel bars, bars',\n",
" 703: 'park bench',\n",
" 704: 'parking meter',\n",
" 705: 'passenger car, coach, carriage',\n",
" 706: 'patio, terrace',\n",
" 707: 'pay-phone, pay-station',\n",
" 708: 'pedestal, plinth, footstall',\n",
" 709: 'pencil box, pencil case',\n",
" 710: 'pencil sharpener',\n",
" 711: 'perfume, essence',\n",
" 712: 'Petri dish',\n",
" 713: 'photocopier',\n",
" 714: 'pick, plectrum, plectron',\n",
" 715: 'pickelhaube',\n",
" 716: 'picket fence, paling',\n",
" 717: 'pickup, pickup truck',\n",
" 718: 'pier',\n",
" 719: 'piggy bank, penny bank',\n",
" 720: 'pill bottle',\n",
" 721: 'pillow',\n",
" 722: 'ping-pong ball',\n",
" 723: 'pinwheel',\n",
" 724: 'pirate, pirate ship',\n",
" 725: 'pitcher, ewer',\n",
" 726: \"plane, carpenter's plane, woodworking plane\",\n",
" 727: 'planetarium',\n",
" 728: 'plastic bag',\n",
" 729: 'plate rack',\n",
" 730: 'plow, plough',\n",
" 731: \"plunger, plumber's helper\",\n",
" 732: 'Polaroid camera, Polaroid Land camera',\n",
" 733: 'pole',\n",
" 734: 'police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria',\n",
" 735: 'poncho',\n",
" 736: 'pool table, billiard table, snooker table',\n",
" 737: 'pop bottle, soda bottle',\n",
" 738: 'pot, flowerpot',\n",
" 739: \"potter's wheel\",\n",
" 740: 'power drill',\n",
" 741: 'prayer rug, prayer mat',\n",
" 742: 'printer',\n",
" 743: 'prison, prison house',\n",
" 744: 'projectile, missile',\n",
" 745: 'projector',\n",
" 746: 'puck, hockey puck',\n",
" 747: 'punching bag, punch bag, punching ball, punchball',\n",
" 748: 'purse',\n",
" 749: 'quill, quill pen',\n",
" 750: 'quilt, comforter, comfort, puff',\n",
" 751: 'racer, race car, racing car',\n",
" 752: 'racket, racquet',\n",
" 753: 'radiator',\n",
" 754: 'radio, wireless',\n",
" 755: 'radio telescope, radio reflector',\n",
" 756: 'rain barrel',\n",
" 757: 'recreational vehicle, RV, R.V.',\n",
" 758: 'reel',\n",
" 759: 'reflex camera',\n",
" 760: 'refrigerator, icebox',\n",
" 761: 'remote control, remote',\n",
" 762: 'restaurant, eating house, eating place, eatery',\n",
" 763: 'revolver, six-gun, six-shooter',\n",
" 764: 'rifle',\n",
" 765: 'rocking chair, rocker',\n",
" 766: 'rotisserie',\n",
" 767: 'rubber eraser, rubber, pencil eraser',\n",
" 768: 'rugby ball',\n",
" 769: 'rule, ruler',\n",
" 770: 'running shoe',\n",
" 771: 'safe',\n",
" 772: 'safety pin',\n",
" 773: 'saltshaker, salt shaker',\n",
" 774: 'sandal',\n",
" 775: 'sarong',\n",
" 776: 'sax, saxophone',\n",
" 777: 'scabbard',\n",
" 778: 'scale, weighing machine',\n",
" 779: 'school bus',\n",
" 780: 'schooner',\n",
" 781: 'scoreboard',\n",
" 782: 'screen, CRT screen',\n",
" 783: 'screw',\n",
" 784: 'screwdriver',\n",
" 785: 'seat belt, seatbelt',\n",
" 786: 'sewing machine',\n",
" 787: 'shield, buckler',\n",
" 788: 'shoe shop, shoe-shop, shoe store',\n",
" 789: 'shoji',\n",
" 790: 'shopping basket',\n",
" 791: 'shopping cart',\n",
" 792: 'shovel',\n",
" 793: 'shower cap',\n",
" 794: 'shower curtain',\n",
" 795: 'ski',\n",
" 796: 'ski mask',\n",
" 797: 'sleeping bag',\n",
" 798: 'slide rule, slipstick',\n",
" 799: 'sliding door',\n",
" 800: 'slot, one-armed bandit',\n",
" 801: 'snorkel',\n",
" 802: 'snowmobile',\n",
" 803: 'snowplow, snowplough',\n",
" 804: 'soap dispenser',\n",
" 805: 'soccer ball',\n",
" 806: 'sock',\n",
" 807: 'solar dish, solar collector, solar furnace',\n",
" 808: 'sombrero',\n",
" 809: 'soup bowl',\n",
" 810: 'space bar',\n",
" 811: 'space heater',\n",
" 812: 'space shuttle',\n",
" 813: 'spatula',\n",
" 814: 'speedboat',\n",
" 815: \"spider web, spider's web\",\n",
" 816: 'spindle',\n",
" 817: 'sports car, sport car',\n",
" 818: 'spotlight, spot',\n",
" 819: 'stage',\n",
" 820: 'steam locomotive',\n",
" 821: 'steel arch bridge',\n",
" 822: 'steel drum',\n",
" 823: 'stethoscope',\n",
" 824: 'stole',\n",
" 825: 'stone wall',\n",
" 826: 'stopwatch, stop watch',\n",
" 827: 'stove',\n",
" 828: 'strainer',\n",
" 829: 'streetcar, tram, tramcar, trolley, trolley car',\n",
" 830: 'stretcher',\n",
" 831: 'studio couch, day bed',\n",
" 832: 'stupa, tope',\n",
" 833: 'submarine, pigboat, sub, U-boat',\n",
" 834: 'suit, suit of clothes',\n",
" 835: 'sundial',\n",
" 836: 'sunglass',\n",
" 837: 'sunglasses, dark glasses, shades',\n",
" 838: 'sunscreen, sunblock, sun blocker',\n",
" 839: 'suspension bridge',\n",
" 840: 'swab, swob, mop',\n",
" 841: 'sweatshirt',\n",
" 842: 'swimming trunks, bathing trunks',\n",
" 843: 'swing',\n",
" 844: 'switch, electric switch, electrical switch',\n",
" 845: 'syringe',\n",
" 846: 'table lamp',\n",
" 847: 'tank, army tank, armored combat vehicle, armoured combat vehicle',\n",
" 848: 'tape player',\n",
" 849: 'teapot',\n",
" 850: 'teddy, teddy bear',\n",
" 851: 'television, television system',\n",
" 852: 'tennis ball',\n",
" 853: 'thatch, thatched roof',\n",
" 854: 'theater curtain, theatre curtain',\n",
" 855: 'thimble',\n",
" 856: 'thresher, thrasher, threshing machine',\n",
" 857: 'throne',\n",
" 858: 'tile roof',\n",
" 859: 'toaster',\n",
" 860: 'tobacco shop, tobacconist shop, tobacconist',\n",
" 861: 'toilet seat',\n",
" 862: 'torch',\n",
" 863: 'totem pole',\n",
" 864: 'tow truck, tow car, wrecker',\n",
" 865: 'toyshop',\n",
" 866: 'tractor',\n",
" 867: 'trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi',\n",
" 868: 'tray',\n",
" 869: 'trench coat',\n",
" 870: 'tricycle, trike, velocipede',\n",
" 871: 'trimaran',\n",
" 872: 'tripod',\n",
" 873: 'triumphal arch',\n",
" 874: 'trolleybus, trolley coach, trackless trolley',\n",
" 875: 'trombone',\n",
" 876: 'tub, vat',\n",
" 877: 'turnstile',\n",
" 878: 'typewriter keyboard',\n",
" 879: 'umbrella',\n",
" 880: 'unicycle, monocycle',\n",
" 881: 'upright, upright piano',\n",
" 882: 'vacuum, vacuum cleaner',\n",
" 883: 'vase',\n",
" 884: 'vault',\n",
" 885: 'velvet',\n",
" 886: 'vending machine',\n",
" 887: 'vestment',\n",
" 888: 'viaduct',\n",
" 889: 'violin, fiddle',\n",
" 890: 'volleyball',\n",
" 891: 'waffle iron',\n",
" 892: 'wall clock',\n",
" 893: 'wallet, billfold, notecase, pocketbook',\n",
" 894: 'wardrobe, closet, press',\n",
" 895: 'warplane, military plane',\n",
" 896: 'washbasin, handbasin, washbowl, lavabo, wash-hand basin',\n",
" 897: 'washer, automatic washer, washing machine',\n",
" 898: 'water bottle',\n",
" 899: 'water jug',\n",
" 900: 'water tower',\n",
" 901: 'whiskey jug',\n",
" 902: 'whistle',\n",
" 903: 'wig',\n",
" 904: 'window screen',\n",
" 905: 'window shade',\n",
" 906: 'Windsor tie',\n",
" 907: 'wine bottle',\n",
" 908: 'wing',\n",
" 909: 'wok',\n",
" 910: 'wooden spoon',\n",
" 911: 'wool, woolen, woollen',\n",
" 912: 'worm fence, snake fence, snake-rail fence, Virginia fence',\n",
" 913: 'wreck',\n",
" 914: 'yawl',\n",
" 915: 'yurt',\n",
" 916: 'web site, website, internet site, site',\n",
" 917: 'comic book',\n",
" 918: 'crossword puzzle, crossword',\n",
" 919: 'street sign',\n",
" 920: 'traffic light, traffic signal, stoplight',\n",
" 921: 'book jacket, dust cover, dust jacket, dust wrapper',\n",
" 922: 'menu',\n",
" 923: 'plate',\n",
" 924: 'guacamole',\n",
" 925: 'consomme',\n",
" 926: 'hot pot, hotpot',\n",
" 927: 'trifle',\n",
" 928: 'ice cream, icecream',\n",
" 929: 'ice lolly, lolly, lollipop, popsicle',\n",
" 930: 'French loaf',\n",
" 931: 'bagel, beigel',\n",
" 932: 'pretzel',\n",
" 933: 'cheeseburger',\n",
" 934: 'hotdog, hot dog, red hot',\n",
" 935: 'mashed potato',\n",
" 936: 'head cabbage',\n",
" 937: 'broccoli',\n",
" 938: 'cauliflower',\n",
" 939: 'zucchini, courgette',\n",
" 940: 'spaghetti squash',\n",
" 941: 'acorn squash',\n",
" 942: 'butternut squash',\n",
" 943: 'cucumber, cuke',\n",
" 944: 'artichoke, globe artichoke',\n",
" 945: 'bell pepper',\n",
" 946: 'cardoon',\n",
" 947: 'mushroom',\n",
" 948: 'Granny Smith',\n",
" 949: 'strawberry',\n",
" 950: 'orange',\n",
" 951: 'lemon',\n",
" 952: 'fig',\n",
" 953: 'pineapple, ananas',\n",
" 954: 'banana',\n",
" 955: 'jackfruit, jak, jack',\n",
" 956: 'custard apple',\n",
" 957: 'pomegranate',\n",
" 958: 'hay',\n",
" 959: 'carbonara',\n",
" 960: 'chocolate sauce, chocolate syrup',\n",
" 961: 'dough',\n",
" 962: 'meat loaf, meatloaf',\n",
" 963: 'pizza, pizza pie',\n",
" 964: 'potpie',\n",
" 965: 'burrito',\n",
" 966: 'red wine',\n",
" 967: 'espresso',\n",
" 968: 'cup',\n",
" 969: 'eggnog',\n",
" 970: 'alp',\n",
" 971: 'bubble',\n",
" 972: 'cliff, drop, drop-off',\n",
" 973: 'coral reef',\n",
" 974: 'geyser',\n",
" 975: 'lakeside, lakeshore',\n",
" 976: 'promontory, headland, head, foreland',\n",
" 977: 'sandbar, sand bar',\n",
" 978: 'seashore, coast, seacoast, sea-coast',\n",
" 979: 'valley, vale',\n",
" 980: 'volcano',\n",
" 981: 'ballplayer, baseball player',\n",
" 982: 'groom, bridegroom',\n",
" 983: 'scuba diver',\n",
" 984: 'rapeseed',\n",
" 985: 'daisy',\n",
" 986: \"yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum\",\n",
" 987: 'corn',\n",
" 988: 'acorn',\n",
" 989: 'hip, rose hip, rosehip',\n",
" 990: 'buckeye, horse chestnut, conker',\n",
" 991: 'coral fungus',\n",
" 992: 'agaric',\n",
" 993: 'gyromitra',\n",
" 994: 'stinkhorn, carrion fungus',\n",
" 995: 'earthstar',\n",
" 996: 'hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa',\n",
" 997: 'bolete',\n",
" 998: 'ear, spike, capitulum',\n",
" 999: 'toilet tissue, toilet paper, bathroom tissue'}"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title Perform thresholding on the relevance (using Otsu's method)\n",
"#@title Number of layers for image Transformer\n",
"use_thresholding = False#@param {type:\"boolean\"}"
],
"metadata": {
"cellView": "form",
"id": "IDKqZyCG3rmS"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "2jXDQpLLFJBP"
},
"source": [
"# **ViT examples**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "UtHosD9lCgAA"
},
"source": [
"from baselines.ViT.ViT_LRP import vit_base_patch16_224 as vit_LRP\n",
"from baselines.ViT.ViT_explanation_generator import LRP\n",
"\n",
"normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n",
"transform = transforms.Compose([\n",
" transforms.Resize(256),\n",
" transforms.CenterCrop(224),\n",
" transforms.ToTensor(),\n",
" normalize,\n",
"])\n",
"\n",
"# create heatmap from mask on image\n",
"def show_cam_on_image(img, mask):\n",
" heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)\n",
" heatmap = np.float32(heatmap) / 255\n",
" cam = heatmap + np.float32(img)\n",
" cam = cam / np.max(cam)\n",
" return cam\n",
"\n",
"# initialize ViT pretrained\n",
"model = vit_LRP(pretrained=True).cuda()\n",
"model.eval()\n",
"attribution_generator = LRP(model)\n",
"\n",
"def generate_visualization(original_image, class_index=None):\n",
" transformer_attribution = attribution_generator.generate_LRP(original_image.unsqueeze(0).cuda(), method=\"transformer_attribution\", index=class_index).detach()\n",
" transformer_attribution = transformer_attribution.reshape(1, 1, 14, 14)\n",
" transformer_attribution = torch.nn.functional.interpolate(transformer_attribution, scale_factor=16, mode='bilinear')\n",
" transformer_attribution = transformer_attribution.reshape(224, 224).data.cpu().numpy()\n",
" transformer_attribution = (transformer_attribution - transformer_attribution.min()) / (transformer_attribution.max() - transformer_attribution.min())\n",
"\n",
" if use_thresholding:\n",
" transformer_attribution = transformer_attribution * 255\n",
" transformer_attribution = transformer_attribution.astype(np.uint8)\n",
" ret, transformer_attribution = cv2.threshold(transformer_attribution, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n",
" transformer_attribution[transformer_attribution == 255] = 1\n",
"\n",
" image_transformer_attribution = original_image.permute(1, 2, 0).data.cpu().numpy()\n",
" image_transformer_attribution = (image_transformer_attribution - image_transformer_attribution.min()) / (image_transformer_attribution.max() - image_transformer_attribution.min())\n",
" vis = show_cam_on_image(image_transformer_attribution, transformer_attribution)\n",
" vis = np.uint8(255 * vis)\n",
" vis = cv2.cvtColor(np.array(vis), cv2.COLOR_RGB2BGR)\n",
" return vis\n",
"\n",
"def print_top_classes(predictions, **kwargs): \n",
" # Print Top-5 predictions\n",
" prob = torch.softmax(predictions, dim=1)\n",
" class_indices = predictions.data.topk(5, dim=1)[1][0].tolist()\n",
" max_str_len = 0\n",
" class_names = []\n",
" for cls_idx in class_indices:\n",
" class_names.append(CLS2IDX[cls_idx])\n",
" if len(CLS2IDX[cls_idx]) > max_str_len:\n",
" max_str_len = len(CLS2IDX[cls_idx])\n",
" \n",
" print('Top 5 classes:')\n",
" for cls_idx in class_indices:\n",
" output_string = '\\t{} : {}'.format(cls_idx, CLS2IDX[cls_idx])\n",
" output_string += ' ' * (max_str_len - len(CLS2IDX[cls_idx])) + '\\t\\t'\n",
" output_string += 'value = {:.3f}\\t prob = {:.1f}%'.format(predictions[0, cls_idx], 100 * prob[0, cls_idx])\n",
" print(output_string)"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 233
},
"id": "ZPbx6CIHEl08",
"outputId": "d42b5ff4-8206-4588-971f-ac7d0ed6fa89"
},
"source": [
"image = Image.open('samples/catdog.png')\n",
"dog_cat_image = transform(image)\n",
"\n",
"fig, axs = plt.subplots(1, 3)\n",
"axs[0].imshow(image);\n",
"axs[0].axis('off');\n",
"\n",
"output = model(dog_cat_image.unsqueeze(0).cuda())\n",
"print_top_classes(output)\n",
"\n",
"# cat - the predicted class\n",
"cat = generate_visualization(dog_cat_image)\n",
"\n",
"# dog \n",
"# generate visualization for class 243: 'bull mastiff'\n",
"dog = generate_visualization(dog_cat_image, class_index=243)\n",
"\n",
"\n",
"axs[1].imshow(cat);\n",
"axs[1].axis('off');\n",
"axs[2].imshow(dog);\n",
"axs[2].axis('off');"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Top 5 classes:\n",
"\t282 : tiger cat \t\tvalue = 10.559\t prob = 68.6%\n",
"\t281 : tabby, tabby cat\t\tvalue = 9.059\t prob = 15.3%\n",
"\t285 : Egyptian cat \t\tvalue = 8.414\t prob = 8.0%\n",
"\t243 : bull mastiff \t\tvalue = 7.425\t prob = 3.0%\n",
"\t811 : space heater \t\tvalue = 5.152\t prob = 0.3%\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"