List index out of range Error for Append method in YoloV5

hi i wrote codes for object detection using YoloV5 in Google Colab. i have some google earth images that i saved in my Google Drive Storage. i am using “Append” method in a list, but my list is empty after run codes in Google Colab. below image is Code snippets:

i am confused because Append does not work in colab and “imgs” list is empty.
thanks a lot

Please read the pinned thread and show code as properly formatted, copied and pasted text. Also, we can only possibly talk about the part of the code that is actually shown to us: in order to understand what went wrong with using the append method, we need to see the code that has append in it.

We actually need you to copy and paste and format the code so we can copy it and run it on our own machines to help you.

On the machine you are running this code, you are logged into Google Drive, right? Your login didn’t time out? That is required when using Colab and Google Drive access.

Again, this is a picture of the code, not its text. Please copy/paste the text from your programme. That way we can copy/paste it ourselves.

There’s a button on the copy bar like this: </>. The sequence is:

  • copy the text of your programme
  • click the </> button
  • paste (often control-v)
import os
import shutil
from random import choice
import pandas as pd

imgs = []
xmls = []

train_path = '/content/yolov5/data/images/train'
val_path = '/content/yolov5/data/images/val'
source_path = '/content/drive/MyDrive/DOTADataset'

if not os.path.exists(train_path):
if not os.path.exists(val_path):

train_ratio = 0.8
val_ratio = 0.2

totalImgCount = len(os.listdir(source_path))/2

for (dirname, dirs, files) in os.walk(source_path):
    for filename in files:
        if filename.endswith('.txt'):

  # Total number of image files

countfortrain = int(len(imgs) * train_ratio)
countforval = int(len(imgs) * val_ratio)

print("Training images are:", countfortrain)
print("Validation images are:", countforval)

trainimagepath = '/content/yolov5/data/images/train'
trainlabelpath = '/content/yolov5/data/labels/train'
valimagepath = '/content/yolov5/data/images/val'
vallabelpath = '/content/yolov5/data/labels/val'

if not os.path.exists(trainimagepath):
if not os.path.exists(trainlabelpath):
if not os.path.exists(valimagepath):
if not os.path.exists(vallabelpath):
imgs = os.listdir(trainimagepath)

for x in range(countfortrain):
  fileJpg = choice(imgs)
  fileXml = fileJpg[:-4] +'.txt'

shutil.copy(os.path.join(source_path, fileJpg), os.path.join(trainimagepath,fileJpg))
shutil.copy(os.path.join(source_path, fileXml), os.path.join(trainlabelpath,fileXml))


for x in range(countforval):
    fileJpg = choice(imgs)
    fileXml = fileJpg[:-4] +'.txt'

shutil.copy(os.path.join(source_path, fileJpg), os.path.join(valimagepath,fileJpg))
shutil.copy(os.path.join(source_path, fileXml), os.path.join(vallabelpath,fileXml))

1 Like

Thank you. Can you do the same to show the output slso?

[‘P0005.png’, ‘P0002.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’, ‘P0032.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’, ‘P0032.png’, ‘P0042.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’, ‘P0032.png’, ‘P0042.png’, ‘P0041.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’, ‘P0032.png’, ‘P0042.png’, ‘P0041.png’, ‘P0044.png’]
[‘P0005.png’, ‘P0002.png’, ‘P0008.png’, ‘P0010.png’, ‘P0013.png’, ‘P0018.png’, ‘P0032.png’, ‘P0042.png’, ‘P0041.png’, ‘P0044.png’, ‘dataset.yaml’]
Training images are: 8
Validation images are: 2

IndexError Traceback (most recent call last)
in <cell line: 54>()
54 for x in range(countfortrain):
—> 55 fileJpg = choice(imgs)
56 fileXml = fileJpg[:-4] +‘.txt’

/usr/lib/python3.10/ in choice(self, seq)
376 “”“Choose a random element from a non-empty sequence.”“”
377 # raises IndexError if seq is empty
→ 378 return seq[self._randbelow(len(seq))]
380 def shuffle(self, x, random=None):

IndexError: list index out of range

Thank you.

It isn’t clear to me what’s wrong with your code, and I haven’t got your
training data here.

I would put a print(imgs) at strategic points, for example before and
after the for-loops, before and after the imgs.remove() calls, and
before line 55 where you call choice().

The only way to get an index error from choice() is for the list you
give it to be empty, example:

 >>> choice([1])
 >>> choice([])
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/Users/cameron/var/pyenv/versions/3.10.6/lib/python3.10/", line 378, in choice
     return seq[self._randbelow(len(seq))]
 IndexError: list index out of range

Hopefully the print() calls can show you where the imgs list is
becoming not what you intended. You should put a littl context in the
prints, eg:

 print('before the first for-loop, imgs =', imgs)

and so forth.

From this documentation it says

"random.choice(seq )

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError."

And you have an IndexError.

Step through your program line by line and check the value of imgs at various points. See if it gets initialized to blank at some point. Does Colab has a debugger for python?

hi i put print(imgs) at some points that you said, unfortunately “imgs” list is empty at every where. please look at below image:

hi maybe login to Google Drive has been timed out. how to understand this problem?

hi this is Main Question: why Google Colab unloads (make empty) “imgs” list?

hi i put print(imgs) at some points that you said, unfortunately “imgs”
list is empty at every where. please look at below image:

We much prefer the output as text instead of a screenshot.

Anyway, it looks like it becomes empty right after your “Validation”
message. I suspect this line:

 imgs = os.listdir(trainimagepath)

which seems to be returning an empty list. And from then on the list is
empty, thus your error.

That says that the trainimagepath directory has no image files in it.

i put pdb.set_trace() some points in code for example after below codes:



and imgs list returned one image

imgs list has an image : P0005.png

thanks a lot. i commented imgs = os.listdir(trainimagepath) and solved my error. below image is output:

but i have a new error for below codes:
!python --img 416 --batch 16 --epochs 10 --data dataset.yaml --weights
my new error is :
python3: can’t open file ‘/content/’: [Errno 2] No such file or directory

The command python ..... tells Python to open the programme in the file in the current working directory. So Python is being run in the directory /content.

Where is your file actually located?

this file is in content/yolov5 and i downloaded this file then i uploaded into content directory and solved error.
Now colab shows new error:
Traceback (most recent call last):
File “/content/”, line 47, in
import val as validate # for end-of-epoch mAP
ModuleNotFoundError: No module named ‘val’