How to extract -Top(K-10 )- probabilities and labels from CTC layer , I’m trying to get the final top k-10 -words with the associated probabilities.
The code is based on https://github.com/meijieru/crnn.pytorch/blob/master/demo.py
any hint or help ?
I added this code to extract the top k but I’m getting an error
AssertionError: text with length: 260 does not match declared length: 26
my code
model .eval()
output = model(image)
_, predicted = output.topk(10)
cl = predicted.data[0][0]
print (cl)
predicted = predicted.transpose(1, 0).contiguous().view(-1)
predicted_size = Variable(torch.IntTensor([preds.size(0)])) sim_predicted = converter.decode(predicted.data, predicted_size.data, raw=True)
Thanks
royboy
(Roy Li)
March 28, 2018, 9:30pm
2
I think this is an issue with the demo code and not pytorch:
if raw:
return ''.join([self.alphabet[i - 1] for i in t])
else:
char_list = []
for i in range(length):
if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
char_list.append(self.alphabet[t[i] - 1])
return ''.join(char_list)
else:
# batch mode
assert t.numel() == length.sum(), "texts with length: {} does not match declared length: {}".format(t.numel(), length.sum())
texts = []
index = 0
for i in range(length.numel()):
l = length[i]
texts.append(
self.decode(
t[index:index + l], torch.IntTensor([l]), raw=raw))
index += l
return texts
I found this related issue, hopefully it helps. https://github.com/meijieru/crnn.pytorch/issues/62
Thank you for pointing that out, still I’m not able to get the top k even after changing the index. Any hint/help to point me into the right direction.
Thanks in advance.
royboy
(Roy Li)
March 30, 2018, 7:30pm
4
Can you post the full script that you’re running?
So Sorry for the late reply… Still, I’m not able to run that demo.py with extract multiple words, 2,3 words, and its associated probabilities.
import torch
from torch.autograd import Variable
import utils
import dataset
from PIL import Image
import models.crnn as crnn
model_path = './data/crnn.pth'
img_path = './data/demo.png'
alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'
model = crnn.CRNN(32, 1, 37, 256)
if torch.cuda.is_available():
model = model.cuda()
print('loading pretrained model from %s' % model_path)
model.load_state_dict(torch.load(model_path))
converter = utils.strLabelConverter(alphabet)
This file has been truncated. show original
Not sure where to start, from my understanding, CTC has softmax, and score. from where we could extract the sum of probabilities for each word1, word2
CTC code
#!/usr/bin/python
# encoding: utf-8
import torch
import torch.nn as nn
from torch.autograd import Variable
import collections
class strLabelConverter(object):
"""Convert between str and label.
NOTE:
Insert `blank` to the alphabet for CTC.
Args:
alphabet (str): set of the possible characters.
ignore_case (bool, default=True): whether or not to ignore all of the case.
"""
This file has been truncated. show original
Thanks for your help in advance!