深度学习模型中预处理中的关键错误

时间:2020-07-12 17:22:23

标签: python pandas deep-learning

我正在从kaggle网站学习深度学习。我正在尝试创建神经深层网络,但是在第一步中,我需要进行预处理,并且由于图像ID和类ID是两个单独的实体,因此将它们分成两列比较容易,但是却遇到了一些问题接着就,随即。我的代码:

make_submission = False 
load_pretrained_model = True 
save_model = True 
train_dir = './Desktop/severstal-steel-defect-detection/' 
pretrained_model_path = './Desktop/severstal-pretrained-model/ResUNetSteel_z.h5' 
model_save_path = './ResUNetSteel_w800e50_z.h5' 
train_image_dir = os.path.join(train_dir, 'train_images') 
train_df = pd.read_csv(os.path.join(train_dir, 'train.csv')).fillna(-1)


train_df['ImageId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
train_df['ClassId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[1])

train_df['ClassId_EncodedPixels'] = train_df.apply(lambda row: (row['ClassId'], row['EncodedPixels']), axis = 1)
grouped_EncodedPixels = train_df.groupby('ImageId')['ClassId_EncodedPixels'].apply(list)

应用该代码后,我收到了此类关键错误:

KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'ImageId_ClassId'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-28-e1402e10fe66> in <module>
      2 train_df = pd.read_csv(os.path.join(train_dir, 'train.csv')).fillna(-1)
      3 # image id and class id are two seperate entities and it makes it easier to split them up in two columns
----> 4 train_df['ImageId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
      5 train_df['ClassId'] = train_df['ImageId_ClassId'].apply(lambda x: x.split('_')[1])
      6 # lets create a dict with class id and encoded pixels and group all the defaults per image

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

* KeyError:“ ImageId_ClassId”

有人知道如何解决这种错误并继续前进吗?

train_df.head()给了我这种数据: enter image description here

0 个答案:

没有答案