--[[----------------------------------------------------------------------------Interface for accessing the Common Objects in COntext (COCO) dataset.For an overview of the API please see http://mscoco.org/dataset/#download.CocoApi.lua (this file) is modeled after the Matlab CocoApi.m:https://github.com/pdollar/coco/blob/master/MatlabAPI/CocoApi.mThe following API functions are defined in the Lua API:CocoApi - Load COCO annotation file and prepare data structures.getAnnIds - Get ann ids that satisfy given filter conditions.getCatIds - Get cat ids that satisfy given filter conditions.getImgIds - Get img ids that satisfy given filter conditions.loadAnns - Load anns with the specified ids.loadCats - Load cats with the specified ids.loadImgs - Load imgs with the specified ids.showAnns - Display the specified annotations.Throughout the API "ann"=annotation, "cat"=category, and "img"=image.For detailed usage information please see cocoDemo.lua.LIMITATIONS: the following API functions are NOT defined in the Lua API:loadRes - Load algorithm results and create API for accessing them.download - Download COCO images from mscoco.org server.In addition, currently the getCatIds() and getImgIds() do not accept filters.getAnnIds() can be called using getAnnIds({imgId=id}) and getAnnIds({catId=id}).Note: loading COCO JSON annotations to Lua tables is quite slow. Hence, a callto CocApi(annFile) converts the annotations to a custom 'flattened' format thatis more efficient. The first time a COCO JSON is loaded, the conversion isinvoked (this may take up to a minute). The converted data is then stored in at7 file (the code must have write permission to the dir of the JSON file).Future calls of cocoApi=CocApi(annFile) take a fraction of a second. To view thecreated data just inspect cocoApi.data of a created instance of the CocoApi.Common Objects in COntext (COCO) Toolbox. version 3.0Data, paper, and tutorials available at: http://mscoco.org/Code written by Pedro O. Pinheiro and Piotr Dollar, 2016.Licensed under the Simplified BSD License [see coco/license.txt]------------------------------------------------------------------------------]]local json = require 'cjson'local coco = require 'coco.env'local TensorTable = torch.class('TensorTable',coco)local CocoSeg = torch.class('CocoSeg',coco)local CocoApi = torch.class('CocoApi',coco)----------------------------------------------------------------------------------[[ TensorTable is a lightweight data structure for storing variable size 1Dtensors. Tables of tensors are slow to save/load to disk. Instead, TensorTablestores all the data in a single long tensor (along with indices into the tensor)making serialization fast. A TensorTable may only contain 1D same-type torchtensors or strings. It supports only creation from a table and indexing. ]]function TensorTable:__init( T )local n = #T; assert(n>0)local isStr = torch.type(T[1])=='string'assert(isStr or torch.isTensor(T[1]))local c=function(s) return torch.CharTensor(torch.CharStorage():string(s)) endif isStr then local S=T; T={}; for i=1,n do T[i]=c(S[i]) end endlocal ms, idx = torch.LongTensor(n), torch.LongTensor(n+1)for i=1,n do ms[i]=T[i]:numel() endidx[1]=1; idx:narrow(1,2,n):copy(ms); idx=idx:cumsum()local type = string.sub(torch.type(T[1]),7,-1)local data = torch[type](idx[n+1]-1)if isStr then type='string' endfor i=1,n do if ms[i]>0 then data:sub(idx[i],idx[i+1]-1):copy(T[i]) end endif ms:eq(ms[1]):all() and ms[1]>0 then data=data:view(n,ms[1]); idx=nil endself.data, self.idx, self.type = data, idx, typeendfunction TensorTable:__index__( i )if torch.type(i)~='number' then return false endlocal d, idx, type = self.data, self.idx, self.typeif idx and idx[i]==idx[i+1] thenif type=='string' then d='' else d=torch[type]() endelseif idx then d=d:sub(idx[i],idx[i+1]-1) else d=d[i] endif type=='string' then d=d:clone():storage():string() endendreturn d, trueend----------------------------------------------------------------------------------[[ CocoSeg is an efficient data structure for storing COCO segmentations. ]]function CocoSeg:__init( segs )local polys, pIdx, sizes, rles, p, isStr = {}, {}, {}, {}, 0, 0for i,seg in pairs(segs) do if seg.size then isStr=seg.counts break end endisStr = torch.type(isStr)=='string'for i,seg in pairs(segs) dopIdx[i], sizes[i] = {}, {}if seg.size thensizes[i],rles[i] = seg.size,seg.countselseif isStr then rles[i]='' else rles[i]={} endfor j=1,#seg do p=p+1; pIdx[i][j],polys[p] = p,seg[j] endendpIdx[i],sizes[i] = torch.LongTensor(pIdx[i]),torch.IntTensor(sizes[i])if not isStr then rles[i]=torch.IntTensor(rles[i]) endendfor i=1,p do polys[i]=torch.DoubleTensor(polys[i]) endself.polys, self.pIdx = coco.TensorTable(polys), coco.TensorTable(pIdx)self.sizes, self.rles = coco.TensorTable(sizes), coco.TensorTable(rles)endfunction CocoSeg:__index__( i )if torch.type(i)~='number' then return false endif self.sizes[i]:numel()>0 thenreturn {size=self.sizes[i],counts=self.rles[i]}, trueelselocal ids, polys = self.pIdx[i], {}for i=1,ids:numel() do polys[i]=self.polys[ids[i]] endreturn polys, trueendend----------------------------------------------------------------------------------[[ CocoApi is the API to the COCO dataset, see main comment for details. ]]function CocoApi:__init( annFile )assert( string.sub(annFile,-4,-1)=='json' and paths.filep(annFile) )local torchFile = string.sub(annFile,1,-6) .. '.t7'if not paths.filep(torchFile) then self:__convert(annFile,torchFile) endlocal data = torch.load(torchFile)self.data, self.inds = data, {}for k,v in pairs({images='img',categories='cat',annotations='ann'}) dolocal M = {}; self.inds[v..'IdsMap']=Mif data[k] then for i=1,data[k].id:size(1) do M[data[k].id[i]]=i end endendendfunction CocoApi:__convert( annFile, torchFile )print('convert: '..annFile..' --> .t7 [please be patient]')local tic = torch.tic()-- load data and decode jsonlocal data = torch.CharStorage(annFile):string()data = json.decode(data); collectgarbage()-- transpose and flatten each field in the coco data structlocal convert = {images=true, categories=true, annotations=true}for field, d in pairs(data) do if convert[field] thenprint('converting: '..field)local n, out = #d, {}if n==0 then d,n={d},1 endfor k,v in pairs(d[1]) dolocal t, isReg = torch.type(v), truefor i=1,n do isReg=isReg and torch.type(d[i][k])==t endif t=='number' and isReg thenout[k] = torch.DoubleTensor(n)for i=1,n do out[k][i]=d[i][k] endelseif t=='string' and isReg thenout[k]={}; for i=1,n do out[k][i]=d[i][k] endout[k] = coco.TensorTable(out[k])elseif t=='table' and isReg and torch.type(v[1])=='number' thenout[k]={}; for i=1,n do out[k][i]=torch.DoubleTensor(d[i][k]) endout[k] = coco.TensorTable(out[k])if not out[k].idx then out[k]=out[k].data endelseout[k]={}; for i=1,n do out[k][i]=d[i][k] endif k=='segmentation' then out[k] = coco.CocoSeg(out[k]) endendcollectgarbage()endif out.id then out.idx=torch.range(1,out.id:size(1)) enddata[field] = outcollectgarbage()end end-- create mapping from cat/img index to anns indices for that cat/imgprint('convert: building indices')local makeMap = function( type, type_id )if not data[type] or not data.annotations then return nil endlocal invmap, n = {}, data[type].id:size(1)for i=1,n do invmap[data[type].id[i]]=i endlocal map = {}; for i=1,n do map[i]={} enddata.annotations[type_id..'x'] = data.annotations[type_id]:clone()for i=1,data.annotations.id:size(1) dolocal id = invmap[data.annotations[type_id][i]]data.annotations[type_id..'x'][i] = idtable.insert(map[id],data.annotations.id[i])endfor i=1,n do map[i]=torch.LongTensor(map[i]) endreturn coco.TensorTable(map)enddata.annIdsPerImg = makeMap('images','image_id')data.annIdsPerCat = makeMap('categories','category_id')-- save to disktorch.save( torchFile, data )print(('convert: complete [%.2f s]'):format(torch.toc(tic)))endfunction CocoApi:getAnnIds( filters )if not filters then filters = {} endif filters.imgId thenreturn self.data.annIdsPerImg[self.inds.imgIdsMap[filters.imgId]] or {}elseif filters.catId thenreturn self.data.annIdsPerCat[self.inds.catIdsMap[filters.catId]] or {}elsereturn self.data.annotations.idendendfunction CocoApi:getCatIds()return self.data.categories.idendfunction CocoApi:getImgIds()return self.data.images.idendfunction CocoApi:loadAnns( ids )return self:__load(self.data.annotations,self.inds.annIdsMap,ids)endfunction CocoApi:loadCats( ids )return self:__load(self.data.categories,self.inds.catIdsMap,ids)endfunction CocoApi:loadImgs( ids )return self:__load(self.data.images,self.inds.imgIdsMap,ids)endfunction CocoApi:showAnns( img, anns )local n, h, w = #anns, img:size(2), img:size(3)local MaskApi, clrs = coco.MaskApi, torch.rand(n,3)*.6+.4local O = img:clone():contiguous():float()if n==0 then anns,n={anns},1 endif anns[1].keypoints then for i=1,n do if anns[i].iscrowd==0 thenlocal sk, kp, j, k = self:loadCats(anns[i].category_id)[1].skeletonkp=anns[i].keypoints; k=kp:size(1); j=torch.range(1,k,3):long(); k=k/3;local x,y,v = kp:index(1,j), kp:index(1,j+1), kp:index(1,j+2)for _,s in pairs(sk) do if v[s[1]]>0 and v[s[2]]>0 thenMaskApi.drawLine(O,x[s[1]],y[s[1]],x[s[2]],y[s[2]],.75,clrs[i])end endfor j=1,k do if v[j]==1 then MaskApi.drawCirc(O,x[j],y[j],4,{0,0,0}) end endfor j=1,k do if v[j]>0 then MaskApi.drawCirc(O,x[j],y[j],3,clrs[i]) end endend end endif anns[1].segmentation or anns[1].bbox thenlocal Rs, alpha = {}, anns[1].keypoints and .25 or .4for i=1,n doRs[i]=anns[i].segmentationif Rs[i] and #Rs[i]>0 then Rs[i]=MaskApi.frPoly(Rs[i],h,w) endif not Rs[i] then Rs[i]=MaskApi.frBbox(anns[i].bbox,h,w)[1] endendMaskApi.drawMasks(O,MaskApi.decode(Rs),nil,alpha,clrs)endreturn Oendfunction CocoApi:__load( data, map, ids )if not torch.isTensor(ids) then ids=torch.LongTensor({ids}) endlocal out, idx = {}, nilfor i=1,ids:numel() doout[i], idx = {}, map[ids[i]]for k,v in pairs(data) do out[i][k]=v[idx] endendreturn outend
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。