File size: 5,780 Bytes
d39027f |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from pycocotools.coco import COCO\n",
"from pycocoevalcap.eval import COCOEvalCap\n",
"from pycocoevalcap.eval_spice import COCOEvalCapSpice\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import skimage.io as io\n",
"import pylab\n",
"pylab.rcParams['figure.figsize'] = (10.0, 8.0)\n",
"\n",
"import json\n",
"from json import encoder\n",
"encoder.FLOAT_REPR = lambda o: format(o, '.3f')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Found Stanford CoreNLP.\nDownloading...\nsed: illegal option -- r\nusage: sed script [-Ealn] [-i extension] [file ...]\n sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]\n--2020-02-23 15:20:46-- https://docs.google.com/uc?export=download&id=0B7XkCwpI5KDYNlNUTTlSS21pQmM\nResolving docs.google.com... 172.217.0.14\nConnecting to docs.google.com|172.217.0.14|:443...connected.\nHTTP request sent, awaiting response...200 OK\nLength: unspecified [text/html]\nSaving to: 'STDOUT'\n\n- [ <=> ] 0 --.-KB/s in 0s \n\n\nCannot write to '-' (Success).\nCode:\n--2020-02-23 15:20:48-- https://docs.google.com/uc?export=download&confirm=&id=0B7XkCwpI5KDYNlNUTTlSS21pQmM\nResolving docs.google.com... 172.217.0.14\nConnecting to docs.google.com|172.217.0.14|:443...connected.\nHTTP request sent, awaiting response...200 OK\nLength: unspecified [text/html]\nSaving to: 'pycocoevalcap/wmd/data/GoogleNews-vectors-negative300.bin.gz'\n\npycocoevalcap/wmd/d [ <=> ] 3.19K --.-KB/s in 0.003s \n\n2020-02-23 15:20:49 (1.18 MB/s) - 'pycocoevalcap/wmd/data/GoogleNews-vectors-negative300.bin.gz' saved [3268]\n\nUnzipping...\ngzip: pycocoevalcap/wmd/data/GoogleNews-vectors-negative300.bin.gz: not in gzip format\ngzip: pycocoevalcap/wmd/data/ is a directory\nDone.\n"
}
],
"source": [
"# set up file names and pathes\n",
"dataDir='.'\n",
"dataType='val2014'\n",
"algName = 'fakecap'\n",
"annFile='%s/annotations/captions_%s.json'%(dataDir,dataType)\n",
"subtypes=['results', 'evalImgs', 'eval']\n",
"[resFile, evalImgsFile, evalFile]= \\\n",
"['%s/results/captions_%s_%s_%s.json'%(dataDir,dataType,algName,subtype) for subtype in subtypes]\n",
"\n",
"# download Stanford models\n",
"! bash get_stanford_models.sh\n",
"\n",
"# download Google word2vec model\n",
"! bash get_google_word2vec_model.sh"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "[{'image_id': 404464,\n 'caption': 'black and white photo of a man standing in front of a building'},\n {'image_id': 404464,\n 'caption': 'group of people are on the side of a snowy field'},\n {'image_id': 565778, 'caption': 'train traveling down a train station'},\n {'image_id': 565778,\n 'caption': 'red fire hydrant sitting on a park bench in front of a road'},\n {'image_id': 322226,\n 'caption': 'black and white cat is sitting on top of a wooden bench'},\n {'image_id': 322226, 'caption': 'baseball player swinging a bat at a game'},\n {'image_id': 351053, 'caption': 'laptop computer sitting on top of a table'},\n {'image_id': 351053,\n 'caption': 'zebra standing on top of a lush green field'},\n {'image_id': 40102,\n 'caption': 'group of giraffes standing next to each other in a grassy field'},\n {'image_id': 40102,\n 'caption': 'close up of a pile of oranges sitting on a table'}]"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import tempfile\n",
"preds = json.load(open(resFile, 'r'))\n",
"# Create fake predictions\n",
"for i in range(1, len(preds), 2):\n",
" preds[i]['image_id'] = preds[i-1]['image_id']\n",
"tmp_resFile = tempfile.NamedTemporaryFile('w+')\n",
"tmp_resFile.write(json.dumps(preds))\n",
"preds[:10]\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "loading annotations into memory...\n0:00:00.366050\ncreating index...\nindex created!\nLoading and preparing results... \nDONE (t=0.01s)\ncreating index...\nindex created!\ntokenization...\nsetting up scorers...\ncomputing SPICE score...\nSPICE: 0.121\n"
}
],
"source": [
"# Eval AllSPICE\n",
"coco = COCO(annFile)\n",
"cocoRes_n = coco.loadRes(tmp_resFile.name)\n",
"cocoEvalAllSPICE = COCOEvalCapSpice(coco, cocoRes_n)\n",
"cocoEvalAllSPICE.params['image_id'] = cocoRes_n.getImgIds()\n",
"cocoEvalAllSPICE.evaluate()\n",
"tmp_resFile.close()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "AllSPICE: 0.121\n"
}
],
"source": [
"# print output evaluation scores\n",
"for metric, score in cocoEvalAllSPICE.eval.items():\n",
" print('%s: %.3f'%('All'+metric, score))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4-final"
}
},
"nbformat": 4,
"nbformat_minor": 1
} |