# Index out of range in self while using the LILT Model

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Findex-out-of-range-in-self-while-using-the-lilt-model%2F74705%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 25, 2024, 3:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Findex-out-of-range-in-self-while-using-the-lilt-model%2F74705 "2024-02-25T15:39:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Besrour](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@Besrour](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBesrour)%5C
**Post date:** [February 25, 2024, 3:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Findex-out-of-range-in-self-while-using-the-lilt-model%2F74705%2F1 "2024-02-25T15:39:00Z")

</div>

Hello,  
I am trying to fine-tune the LILT model to extract information from invoices. I annotated my documents using UBIAI, and then I ran this code to adjust the format to be the input of my model. This is the code:

import pandas as pd  
import numpy as np  
import os  
import argparse  
from datasets.features import ClassLabel  
from transformers import AutoProcessor  
from sklearn.model\_selection import train\_test\_split  
from datasets import Features, Sequence, ClassLabel, Value, Array2D, Array3D, Dataset  
from datasets import Image as Img  
from PIL import Image

import warnings  
warnings.filterwarnings(‘ignore’)

def read\_text\_file(file\_path):  
with open(file\_path, ‘r’) as f:  
return (f.readlines())

def prepare\_examples(examples):  
images = examples[image\_column\_name]  
words = examples[text\_column\_name]  
boxes = examples[boxes\_column\_name]  
word\_labels = examples[label\_column\_name]

encoding = processor(images, words, boxes=boxes, word\_labels=word\_labels,  
truncation=True, padding=“max\_length”, max\_length=512)

return encoding

def get\_zip\_dir\_name():  
try:  
os.chdir(‘/content/data’)  
dir\_list = os.listdir()  
any\_file\_name = dir\_list[0]  
zip\_dir\_name = any\_file\_name[:any\_file\_name.find(‘/’)]  
if all(list(map(lambda x: x.startswith(zip\_dir\_name), dir\_list))):  
return zip\_dir\_name  
return False  
finally:  
os.chdir(‘./../’)

def filter\_out\_unannotated(example):  
tags = example[‘ner\_tags’]  
return not all([tag == label2id[‘O’] for tag in tags])

if **name** == ‘ **main** ’:

```
parser = argparse.ArgumentParser()
parser.add_argument('--valid_size')
parser.add_argument('--output_path')
args = parser.parse_args()
TEST_SIZE = float(args.valid_size)
OUTPUT_PATH = args.output_path

os.makedirs(args.output_path, exist_ok=True)
files = {}
zip_dir_name = "/content/data"
zip_id = "34c8d6d4-5a0f-48be-bc8d-5085ae70ff89"
if zip_dir_name and zip_id:
    files['train_box'] = read_text_file(os.path.join(
        os.curdir, 'data', f'{zip_dir_name}/{zip_id}_box.txt'))
    files['train_image'] = read_text_file(os.path.join(
        os.curdir, 'data', f'{zip_dir_name}/{zip_id}_image.txt'))
    files['train'] = read_text_file(os.path.join(
        os.curdir, 'data', f'{zip_dir_name}/{zip_id}.txt'))
else:
    for f in os.listdir():
        if f.endswith('.txt') and f.find('box') != -1:
            files['train_box'] = read_text_file(os.path.join(os.curdir, f))
        elif f.endswith('.txt') and f.find('image') != -1:
            files['train_image'] = read_text_file(
                os.path.join(os.curdir, f))
        elif f.endswith('.txt') and f.find('labels') == -1:
            files['train'] = read_text_file(os.path.join(os.curdir, f))

assert(len(files['train']) == len(files['train_box']))
assert(len(files['train_box']) == len(files['train_image']))
assert(len(files['train_image']) == len(files['train']))

images = {}
for i, row in enumerate(files['train_image']):
    if row != '\n':
        image_name = row.split('\t')[-1]
        images.setdefault(image_name.replace('\n', ''), []).append(i)

words, bboxes, ner_tags, image_path = [], [], [], []
for image, rows in images.items():
    words.append([row.split('\t')[0].replace('\n', '')
                 for row in files['train'][rows[0]:rows[-1]+1] if len(row) > 1])
    ner_tags.append([row.split('\t')[1].replace('\n', '')
                    for row in files['train'][rows[0]:rows[-1]+1] if len(row) > 1])
    bboxes.append([box.split('\t')[1].replace('\n', '')
                  for box in files['train_box'][rows[0]:rows[-1]+1] if len(box) > 1])

    im = Image.open(str('data/'+image))
    width, height = im.size

    for i in range(len(bboxes)):

        list_box=bboxes[0][i].split()

        list_box[0]=str(round(1000 * (float(list_box[0]) / width)))
        list_box[1]=str(round(1000 * (float(list_box[1]) / height)))
        list_box[2]=str(round(1000 * (float(list_box[2]) / width)))
        list_box[3]=str(round(1000 * (float(list_box[3]) / height)))
        bboxes[0][i] = ' '.join(list_box)

    if zip_dir_name:
        image_path.append(f"/content/data/{image}")
    else:
        image_path.append(f"/content/data/{image}")

labels = list(set([tag for doc_tag in ner_tags for tag in doc_tag]))
id2label = {v: k for v, k in enumerate(labels)}
label2id = {k: v for v, k in enumerate(labels)}

dataset_dict = {
    'id': range(len(words)),
    'tokens': words,
    'bboxes': [[list(map(int, bbox.split())) for bbox in doc] for doc in bboxes],
    'ner_tags': [[label2id[tag] for tag in ner_tag] for ner_tag in ner_tags],
    'image': [Image.open(path).convert("RGB") for path in image_path]
}

#raw features
features = Features({
    'id': Value(dtype='string', id=None),
    'tokens': Sequence(feature=Value(dtype='string', id=None), length=-1, id=None),
    'bboxes': Sequence(feature=Sequence(feature=Value(dtype='int64', id=None), length=-1, id=None), length=-1, id=None),
    'ner_tags': Sequence(feature=ClassLabel(num_classes=len(labels), names=labels, names_file=None, id=None), length=-1, id=None),
    'image': Img(decode=True, id=None)
})

full_data_set = Dataset.from_dict(dataset_dict, features=features)
dataset = full_data_set.train_test_split(test_size=TEST_SIZE)
dataset["train"] = dataset["train"].filter(filter_out_unannotated)
processor = AutoProcessor.from_pretrained(
    "microsoft/layoutlmv3-base", apply_ocr=False)

features = dataset["train"].features
column_names = dataset["train"].column_names
image_column_name = "image"
text_column_name = "tokens"
boxes_column_name = "bboxes"
label_column_name = "ner_tags"

features = Features({
    'pixel_values': Array3D(dtype="float32", shape=(3, 224, 224)),
    'input_ids': Sequence(feature=Value(dtype='int64')),
    'attention_mask': Sequence(Value(dtype='int64')),
    'bbox': Array2D(dtype="int64", shape=(512, 4)),
    'labels': Sequence(ClassLabel(names=labels)),
})

train_dataset = dataset["train"].map(
    prepare_examples,
    batched=True,
    remove_columns=column_names,
    features=features,
)
eval_dataset = dataset["test"].map(
    prepare_examples,
    batched=True,
    remove_columns=column_names,
    features=features,
)
train_dataset.set_format("torch")
print(bboxes)
if not OUTPUT_PATH.endswith('/'):
    OUTPUT_PATH += '/'
train_dataset.save_to_disk(f'{OUTPUT_PATH}train_split')
eval_dataset.save_to_disk(f'{OUTPUT_PATH}eval_split')
dataset.save_to_disk(f'{OUTPUT_PATH}raw_data')

```

Then I used Trainer.train() to train the model:

from transformers import Trainer, TrainingArguments  
NUM\_TRAIN\_EPOCHS = 50  
PER\_DEVICE\_TRAIN\_BATCH\_SIZE = 8  
PER\_DEVICE\_EVAL\_BATCH\_SIZE = 8  
LEARNING\_RATE = 4e-5  
training\_args = TrainingArguments(output\_dir=“LiLT\_INVOICE”,  
# max\_steps=1500,  
num\_train\_epochs=NUM\_TRAIN\_EPOCHS,  
logging\_strategy=“epoch”,  
save\_total\_limit=1,  
per\_device\_train\_batch\_size=PER\_DEVICE\_TRAIN\_BATCH\_SIZE,  
per\_device\_eval\_batch\_size=PER\_DEVICE\_EVAL\_BATCH\_SIZE,  
learning\_rate=LEARNING\_RATE,  
evaluation\_strategy=“no”,  
save\_strategy=“no”,  
# eval\_steps=100,  
load\_best\_model\_at\_end=True,  
metric\_for\_best\_model=“f1”)

# Initialize our Trainer

trainer = Trainer(  
model=model,  
args=training\_args,  
train\_dataset=train\_dataset,  
eval\_dataset=eval\_dataset,  
data\_collator=default\_data\_collator,  
compute\_metrics=compute\_metrics,  
)  
trainer.train()

but I got this error:

IndexError Traceback (most recent call last) Cell In[12], line 1 ----\> 1 trainer.train() File [c:\Users\heha\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\trainer.py:1624](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1624), in Trainer.train(self, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval, \*\*kwargs) [1622](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1622) hf\_hub\_utils.enable\_progress\_bars() [1623](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1623) else: → [1624](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1624) return inner\_training\_loop( [1625](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1625) args=args, [1626](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1626) resume\_from\_checkpoint=resume\_from\_checkpoint, [1627](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1627) trial=trial, [1628](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1628) ignore\_keys\_for\_eval=ignore\_keys\_for\_eval, [1629](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1629) ) File [c:\Users\heha\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\trainer.py:1961](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1961), in Trainer.\_inner\_training\_loop(self, batch\_size, args, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval) [1958](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1958) self.control = self.callback\_handler.on\_step\_begin(args, self.state, self.control) [1960](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1960) with self.accelerator.accumulate(model): → [1961](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1961) tr\_loss\_step = self.training\_step(model, inputs) [1963](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1963) if ( [1964](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1964) args.logging\_nan\_inf\_filter [1965](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1965) and not is\_torch\_tpu\_available() [1966](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1966) and (torch.isnan(tr\_loss\_step) or torch.isinf(tr\_loss\_step)) [1967](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1967) ): [1968](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/transformers/trainer.py:1968) # if loss is nan or inf simply add the average of previous logged losses

…

[2235](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/torch/nn/functional.py:2235) # remove once script supports set\_grad\_enabled [2236](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/torch/nn/functional.py:2236) _no\_grad\_embedding\_renorm_(weight, input, max\_norm, norm\_type) → [2237](file:///C:/Users/heha/AppData/Local/Programs/Python/Python310/lib/site-packages/torch/nn/functional.py:2237) return torch.embedding(weight, input, padding\_idx, scale\_grad\_by\_freq, sparse)

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [February 25, 2024, 8:25pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Findex-out-of-range-in-self-while-using-the-lilt-model%2F74705%2F2 "2024-02-25T20:25:39Z")

</div>

Answered here: [Error while using LILT model "index out of range in self" - #3 by nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ferror-while-using-lilt-model-index-out-of-range-in-self%2F41242%2F3)
