typeerror:“非类型”对象在dataloader中不可下标

csga3l58  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(380)

我试图运行此存储库,但出现了一个错误
我的数据结构是\content\dataset,在数据集中,有14个不同的子类需要加载以进行训练

Abuse
Arrest
Arson
Assault
Burglary
Explosion
Fighting
Normal
RoadAccidents
Robbery
Shooting
Shoplifting
Stealing
Vandalism

train.ipynb中的错误

dict_keys(['train'])

--- Phase train ---
---------------------------------------------------------------------------
BrokenPipeError                           Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7520/2434693286.py in <module>
     14         print(f"--- Phase {phase} ---")
     15         epoch_metrics = {"loss": [], "acc": []}
---> 16         for batch_i, (X, y) in enumerate(dataloaders[phase]):
     17             print(batch_i,x,y)
     18             #iteration = iteration+1

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py in __iter__(self)
    357             return self._iterator
    358         else:
--> 359             return self._get_iterator()
    360 
    361     @property

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py in _get_iterator(self)
    303         else:
    304             self.check_worker_number_rationality()
--> 305             return _MultiProcessingDataLoaderIter(self)
    306 
    307     @property

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py in __init__(self, loader)
    916             #     before it starts, and __del__ tries to join but will get:
    917             #     AssertionError: can only join a started process.
--> 918             w.start()
    919             self._index_queues.append(index_queue)
    920             self._workers.append(w)

~\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py in start(self)
    110                'daemonic processes are not allowed to have children'
    111         _cleanup()
--> 112         self._popen = self._Popen(self)
    113         self._sentinel = self._popen.sentinel
    114         # Avoid a refcycle if the target function holds an indirect

~\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py in _Popen(process_obj)
    221     @staticmethod
    222     def _Popen(process_obj):
--> 223         return _default_context.get_context().Process._Popen(process_obj)
    224 
    225 class DefaultContext(BaseContext):

~\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py in _Popen(process_obj)
    320         def _Popen(process_obj):
    321             from .popen_spawn_win32 import Popen
--> 322             return Popen(process_obj)
    323 
    324     class SpawnContext(BaseContext):

~\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py in __init__(self, process_obj)
     63             try:
     64                 reduction.dump(prep_data, to_child)
---> 65                 reduction.dump(process_obj, to_child)
     66             finally:
     67                 set_spawning_popen(None)

~\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py in dump(obj, file, protocol)
     58 def dump(obj, file, protocol=None):
     59     '''Replacement for pickle.dump() using ForkingPickler.'''
---> 60     ForkingPickler(file, protocol).dump(obj)
     61 
     62 #

BrokenPipeError: [Errno 32] Broken pipe

然后发现错误与num_worker有关,它应该等于0,但在这之后,我出现了不同的错误

--- Epoch 0 ---
dict_keys(['train'])

--- Phase train ---
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7520/2434693286.py in <module>
     14         print(f"--- Phase {phase} ---")
     15         epoch_metrics = {"loss": [], "acc": []}
---> 16         for batch_i, (X, y) in enumerate(dataloaders[phase]):
     17             print(batch_i,x,y)
     18             #iteration = iteration+1

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    519             if self._sampler_iter is None:
    520                 self._reset()
--> 521             data = self._next_data()
    522             self._num_yielded += 1
    523             if self._dataset_kind == _DatasetKind.Iterable and \

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    559     def _next_data(self):
    560         index = self._next_index()  # may raise StopIteration
--> 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    562         if self._pin_memory:
    563             data = _utils.pin_memory.pin_memory(data)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

~\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

~\AppData\Local\Temp/ipykernel_7520/1226535014.py in __getitem__(self, idx)
     11         seq_img = list()
     12         for i in range(23):
---> 13           img1 = img[:,128*i:128*(i+1),:]
     14           if(self.transform):
     15             img1 = self.transform(img1)

TypeError: 'NoneType' object is not subscriptable

这是我认为它有问题的函数

class video_dataset(Dataset):
    def __init__(self,frame_list,sequence_length = 16,transform = None):
        self.frame_list = frame_list
        self.transform = transform
        self.sequence_length = sequence_length
    def __len__(self):
        return len(self.frame_list)
    def __getitem__(self,idx):
        label,path = self.frame_list[idx]
        img = cv2.imread(path)
        seq_img = list()
        for i in range(16):
          img1 = img[:,128*i:128*(i+1),:]
          if(self.transform):
            img1 = self.transform(img1)
          seq_img.append(img1)
        seq_image = torch.stack(seq_img)
        seq_image = seq_image.reshape(3,16,im_size,im_size)
        return seq_image,decoder[label]
pkwftd7m

pkwftd7m1#

回溯指向:

---> 13           img1 = img[:,128*i:128*(i+1),:]
``` `img` 是“非类型”对象。
检查此处返回的内容: `img = cv2.imread(path)` .
伊姆雷德:
函数imread从指定文件加载图像并返回。如果无法读取图像(由于缺少文件、权限不正确、格式不受支持或无效),函数将返回一个空矩阵(mat::data==null)。

相关问题