# How to Decode InputIDs back to String in LayoutLMV3

**URL:** <https://discuss.huggingface.co/t/how-to-decode-inputids-back-to-string-in-layoutlmv3/24609>\
**Category:** 🤗Transformers\
**Created:** [October 18, 2022, 1:14pm UTC](https://discuss.huggingface.co/t/how-to-decode-inputids-back-to-string-in-layoutlmv3/24609 "2022-10-18T13:14:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![purnasai](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@purnasai](https://discuss.huggingface.co/u/purnasai)\
**Post date:** [October 18, 2022, 1:14pm UTC](https://discuss.huggingface.co/t/how-to-decode-inputids-back-to-string-in-layoutlmv3/24609/1 "2022-10-18T13:14:15Z")

</div>

Hi, I am using Layoutlmv3. I Have used LMV3 processor. At train time I have labels as well along with image,text,boxes and labels. But at Inference time, I do not have labels. So the code for inference goes like below.

```auto
processor = AutoProcessor.from_pretrained("microsfotlmv3_repo", apply_ocr=False)
encoding = processor(images = resize_image, 
                    text = tokens,
                    boxes= boxes,
                    return_offsets_mapping=True, 
                    return_tensors="pt",
                    padding = "max_length",
                    truncation = True,
                    max_length = 512
                    )
offset_mapping = encoding.pop('offset_mapping')
outputs = test_model1(**encoding)
predictions = outputs.logits.argmax(-1).squeeze().tolist()
is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
true_predictions = [id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]]

```

Currently, I am decoding text like:

```auto
cleaned_input_ids = encoding['input_ids'][encoding['attention_mask']>0]
text = processor.tokenizer.decode(cleaned_input_ids.squeeze().tolist())
text = text[4:-4]
tokens = text.split(" ")

```

But the count of tokens and count of true\_predictions are not matching.

I am expecting the result to be:  
“sun”:label,  
“rises”:label,  
“in”:label,  
“the”:label,  
“east”:label.

Currently i am not able to map them, as their counts/lengths are not matching. How to resolve this.

Tagging @nielsr, and Others

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [October 19, 2022, 7:20am UTC](https://discuss.huggingface.co/t/how-to-decode-inputids-back-to-string-in-layoutlmv3/24609/2 "2022-10-19T07:20:33Z")

</div>

Hi,

You can use `encoding.word_ids` to know to which word each token belongs.

---

<div class="post-metadata">

**Author:** ![theburntapp](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@theburntapp](https://discuss.huggingface.co/u/theburntapp)\
**Post date:** [March 8, 2024, 6:08am UTC](https://discuss.huggingface.co/t/how-to-decode-inputids-back-to-string-in-layoutlmv3/24609/4 "2024-03-08T06:08:34Z")

</div>

I am also having same problem. i have finetunes the layoutlmv3 model and saved it but while doing inference i cannot get the labeled word. In the training we usually give ner\_tags in encoding but in inference we do not have that ner\_tags for the image. How to do that ? please help me
