Попытка построить классификатор изображений, используя керасы - PullRequest
0 голосов
/ 03 апреля 2019

Я пытаюсь построить классификатор изображений с помощью keras, и размер моего набора данных требует, чтобы я использовал класс ImageDataGenerator вместе с его методом flow_from_dataframe.Это код, который я использую.

training = pd.read_csv("dataset/train/training.csv")

training = (training[(training["view_type"].isin(["view_4","view_5"]))])

training = (training[["id", "class"]]).reset_index(drop = True)

datagen = ImageDataGenerator(rescale = 1. /255)

training.head()


id  class
0   image_view4_0.jpg   lace_up
1   image_view4_1.jpg   lace_up
2   image_view4_2.jpg   zipper
3   image_view4_3.jpg   lace_up
4   image_view4_4.jpg   hook&look

train_generaor = datagen.flow_from_dataframe(dataframe = training, 
                              directory = "dataset/train/images/", 
                                             xcol = "id", 
                                             ycol = "class", 
                                             has_ext = True, 
                                             class_mode = "categorical", 
                                             target_size = (224, 224))

Однако, когда я запускаю генератор поездов в colab, я получаю следующую ошибку


KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2524             try:
-> 2525                 return self._engine.get_loc(key)
   2526             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: 'filename'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-39-e1787553cadc> in <module>()
      7                                              target_size = (224, 224),
      8                                              color_mode  = "rgb",
----> 9                                              validate_filenames = True)

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py in flow_from_dataframe(self, dataframe, directory, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation, drop_duplicates, **kwargs)
    664             subset=subset,
    665             interpolation=interpolation,
--> 666             drop_duplicates=drop_duplicates
    667         )
    668 

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/dataframe_iterator.py in __init__(self, dataframe, directory, image_data_generator, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, subset, interpolation, dtype, drop_duplicates)
    118         self.dtype = dtype
    119         # check that inputs match the required class_mode
--> 120         self._check_params(df, x_col, y_col, classes)
    121         if drop_duplicates:
    122             df.drop_duplicates(x_col, inplace=True)

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/dataframe_iterator.py in _check_params(self, df, x_col, y_col, classes)
    162                              .format(self.class_mode, self.allowed_class_modes))
    163         # check that filenames/filepaths column values are all strings
--> 164         if not all(df[x_col].apply(lambda x: isinstance(x, str))):
    165             raise ValueError('All values in column x_col={} must be strings.'
    166                              .format(x_col))

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __getitem__(self, key)
   2137             return self._getitem_multilevel(key)
   2138         else:
-> 2139             return self._getitem_column(key)
   2140 
   2141     def _getitem_column(self, key):

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in _getitem_column(self, key)
   2144         # get column
   2145         if self.columns.is_unique:
-> 2146             return self._get_item_cache(key)
   2147 
   2148         # duplicate columns & possible reduce dimensionality

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1840         res = cache.get(item)
   1841         if res is None:
-> 1842             values = self._data.get(item)
   1843             res = self._box_item_values(item, values)
   1844             cache[item] = res

/usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in get(self, item, fastpath)
   3841 
   3842             if not isna(item):
-> 3843                 loc = self.items.get_loc(item)
   3844             else:
   3845                 indexer = np.arange(len(self.items))[isna(self.items)]

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2525                 return self._engine.get_loc(key)
   2526             except KeyError:
-> 2527                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2528 
   2529         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: 'filename'

Что происходит не так?Я пробовал несколько вещей, чтобы исправить это, но не могу понять, почему это происходит

1 Ответ

0 голосов
/ 03 апреля 2019

Просто взглянув на документацию keras, «filename» - это то, что он ожидает в аргументе xcol.xcol относится к столбцу в кадре данных, который должен содержать путь к изображению относительно того, что вы указали в аргументе directory.

Таким образом, вы просто предоставляете неправильные столбцы в качестве аргументов.Измените имена столбцов вашего 2-столбцового фрейма данных:

training.columns = ['path', 'class']

Затем измените аргументы на то, что требуется:

train_generaor = datagen.flow_from_dataframe(dataframe = training, 
                                             directory = "dataset/train/images/", 
                                             xcol = "path", 
                                             ycol = "class", 
                                             has_ext = True, 
                                             class_mode = "categorical", 
                                             target_size = (224, 224))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...