UnboundLocalError:在使用火炬和yolov3进行对象白色分配训练之前引用了局部变量'labels'

问题描述 投票:0回答:1

我正在使用yolo-v3和PyTorch训练自定义对象以进行对象检测。标签和所有其他任务已完成。在运行train命令时,出现以下错误

(base) C:\Users\murali\Desktop\yolov3>python train.py --data coco.data --cfg cfg/yolov3.cfg
Namespace(accumulate=2, adam=False, arc='defaultpw', batch_size=32, bucket='', cache_images=False, cfg='cfg/yolov3.cfg', data='coco.data', device='', epochs=273, evolve=False, img_size=416, img_weights=False, multi_scale=False, name='', nosave=False, notest=False, prebias=False, rect=False, resume=False, transfer=False, var=None, weights='')
Using CPU

WARNING:root:This caffe2 python run does not have GPU support. Will run in CPU only mode.
Reading labels (357 found, 0 missing, 4 empty for 361 images): 100%|███████████████| 361/361 [00:00<00:00, 6489.34it/s]
Model Summary: 222 layers, 6.19491e+07 parameters, 6.19491e+07 gradients
Starting training for 273 epochs...

 Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size

Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
0%| | 0/12 [00:00<?, ?it/s]Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
Traceback (most recent call last):
File "train.py", line 426, in
train() # train normally
File "train.py", line 235, in train
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\tqdm_tqdm.py", line 1005, in iter
for obj in iterable:
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 819, in next
return self._process_data(data)
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 846, in _process_data
data.reraise()
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch_utils.py", line 369, in reraise
raise self.exc_type(msg)
UnboundLocalError: Caught UnboundLocalError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data_utils\worker.py", line 178, in _worker_loop
data = fetcher.fetch(index)
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "C:\Users\murali\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py", line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "C:\Users\murali\Desktop\yolov3\utils\datasets.py", line 416, in getitem
img, labels = load_mosaic(self, index)
File "C:\Users\murali\Desktop\yolov3\utils\datasets.py", line 590, in load_mosaic
labels4.append(labels)
UnboundLocalError: local variable 'labels' referenced before assignment

提到的行,错误紧跟在dataset.py

416行在下面的代码中

    if mosaic:
        # Load mosaic
    img, labels = load_mosaic(self, index)
        h, w, _ = img.shape

以下代码中的590行(labels4.append(labels))

    # Load labels
    label_path = self.label_files[index]
    if os.path.isfile(label_path):
        x = self.labels[index]
        if x is None:  # labels not preloaded
            with open(label_path, 'r') as f:
                x = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32)

        if x.size > 0:
            # Normalized xywh to pixel xyxy format
            labels = x.copy()
            labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw
            labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh
            labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw
            labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh

        labels4.append(labels)
labels4 = np.concatenate(labels4, 0)

如何解决?

完整代码为here

`

python pytorch object-detection torch yolo
1个回答
0
投票

此错误是由于一个或多个标签文件将为空。因此,请检查您的训练或测试数据集是否包含空标签文件。如果包含,则将其删除,并为其创建带有注释值的新标签文件。您可以使用以下代码查找标签文件是否为空。

import os
label_paths =''
labels = os.listdir(label_paths)

for label_file in labels:
    label_file_with_path = label_paths + label_file
    size_of_label_file = os.path.getsize(label_file_with_path)
    if size_of_label_file == 0:
       print('Empty label file :', label_file)

参考this link

© www.soinside.com 2019 - 2024. All rights reserved.