Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'dict_keys' object does not support indexing #217

Open
HARIHARAN1103 opened this issue Feb 7, 2019 · 3 comments
Open

TypeError: 'dict_keys' object does not support indexing #217

HARIHARAN1103 opened this issue Feb 7, 2019 · 3 comments

Comments

@HARIHARAN1103
Copy link

I tired demo.ipynb at google colab. After installing the requirements I started to run the code. There arise an issue in the follwing code,

heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
paf_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 38))

first figure shows padded images

f, axarr = plt.subplots(1, len(multiplier))
f.set_size_inches((20, 5))

second figure shows heatmaps

f2, axarr2 = plt.subplots(1, len(multiplier))
f2.set_size_inches((20, 5))

third figure shows PAFs

f3, axarr3 = plt.subplots(2, len(multiplier))
f3.set_size_inches((20, 10))

for m in range(len(multiplier)):
scale = multiplier[m]
imageToTest = cv.resize(oriImg, (0,0), fx=scale, fy=scale, interpolation=cv.INTER_CUBIC)
imageToTest_padded, pad = util.padRightDownCorner(imageToTest, model['stride'], model['padValue'])
print(imageToTest_padded.shape)

axarr[m].imshow(imageToTest_padded[:,:,[2,1,0]])
axarr[m].set_title('Input image: scale %d' % m)

net.blobs['data'].reshape(*(1, 3, imageToTest_padded.shape[0], imageToTest_padded.shape[1]))
#net.forward() # dry run
net.blobs['data'].data[...] = np.transpose(np.float32(imageToTest_padded[:,:,:,np.newaxis]), (3,2,0,1))/256 - 0.5;
start_time = time.time()
output_blobs = net.forward()
print('At scale %d, The CNN took %.2f ms.' % (m, 1000 * (time.time() - start_time)))

# extract outputs, resize, and remove padding
heatmap = np.transpose(np.squeeze(net.blobs[output_blobs.keys()[1]].data), (1,2,0)) # output 1 is heatmaps
heatmap = cv.resize(heatmap, (0,0), fx=model['stride'], fy=model['stride'], interpolation=cv.INTER_CUBIC)
heatmap = heatmap[:imageToTest_padded.shape[0]-pad[2], :imageToTest_padded.shape[1]-pad[3], :]
heatmap = cv.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv.INTER_CUBIC)

paf = np.transpose(np.squeeze(net.blobs[output_blobs.keys()[0]].data), (1,2,0)) # output 0 is PAFs
paf = cv.resize(paf, (0,0), fx=model['stride'], fy=model['stride'], interpolation=cv.INTER_CUBIC)
paf = paf[:imageToTest_padded.shape[0]-pad[2], :imageToTest_padded.shape[1]-pad[3], :]
paf = cv.resize(paf, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv.INTER_CUBIC)

# visualization
axarr2[m].imshow(oriImg[:,:,[2,1,0]])
ax2 = axarr2[m].imshow(heatmap[:,:,3], alpha=.5) # right wrist
axarr2[m].set_title('Heatmaps (Rwri): scale %d' % m)

axarr3.flat[m].imshow(oriImg[:,:,[2,1,0]])
ax3x = axarr3.flat[m].imshow(paf[:,:,16], alpha=.5) # right elbow
axarr3.flat[m].set_title('PAFs (x comp. of Rwri to Relb): scale %d' % m)
axarr3.flat[len(multiplier) + m].imshow(oriImg[:,:,[2,1,0]])
ax3y = axarr3.flat[len(multiplier) + m].imshow(paf[:,:,17], alpha=.5) # right wrist
axarr3.flat[len(multiplier) + m].set_title('PAFs (y comp. of Relb to Rwri): scale %d' % m)

heatmap_avg = heatmap_avg + heatmap / len(multiplier)
paf_avg = paf_avg + paf / len(multiplier)

f2.subplots_adjust(right=0.93)
cbar_ax = f2.add_axes([0.95, 0.15, 0.01, 0.7])
_ = f2.colorbar(ax2, cax=cbar_ax)

f3.subplots_adjust(right=0.93)
cbar_axx = f3.add_axes([0.95, 0.57, 0.01, 0.3])
_ = f3.colorbar(ax3x, cax=cbar_axx)
cbar_axy = f3.add_axes([0.95, 0.15, 0.01, 0.3])
_ = f3.colorbar(ax3y, cax=cbar_axy)

After excuting the code,

(184, 200, 3)
At scale 0, The CNN took 105.94 ms.

TypeError Traceback (most recent call last)
in ()
28
29 # extract outputs, resize, and remove padding
---> 30 heatmap = np.transpose(np.squeeze(net.blobs[output_blobs.keys()[1]].data), (1,2,0)) # output 1 is heatmaps
31 heatmap = cv.resize(heatmap, (0,0), fx=model['stride'], fy=model['stride'], interpolation=cv.INTER_CUBIC)
32 heatmap = heatmap[:imageToTest_padded.shape[0]-pad[2], :imageToTest_padded.shape[1]-pad[3], :]

TypeError: 'dict_keys' object does not support indexing

@retro2old
Copy link

Hello,
I have the same problem.
I managed to get a bit further by doing
tmp = output_blobs.keys()
print(tmp)

and manually write the key

output_blobs is dictionary and cannot be indexed as written by the authors

heatmap = np.transpose(np.squeeze(net.blobs['Mconv7_stage_6_L1'].data), (1,2,0)) # output 1

But afterwards
heatmap and heatmap_avg
pft and pft_avg
Have dimension problems problems

@utisolichah
Copy link

how you solve the dimension problem after change ouput_blobs.keys()?

@ghost
Copy link

ghost commented Jul 21, 2019

Are you running Python3? Then use list(dict.keys()). I think Python2 returned dict.keys() as a list, not Python3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants