Как читать / импортировать / загружать много файлов .mat для обучения в python? - PullRequest
1 голос
/ 21 марта 2020

Я хочу загрузить много .mat файлов в python для обучения. scipy.io.loadmat() загружает один файл! flow_from_directory() для .jpg файлов. но для .mat файлов, что я могу сделать? помогите мне, пожалуйста.

1 Ответ

0 голосов
/ 21 марта 2020

Вы всегда можете написать генератор самостоятельно.

mat_locations = [...]  # list containing paths to all mat files

def mat_generator():
  indices = np.random.permutation(len(mat_locations))  # shuffle the data
  ind = 0
  while i < len(indices):
    yield scipy.io.loadmat(mat_locations[i])
    i+= 1

gen = mat_generator()

Затем вы можете перебирать gen и загружать файлы матов один за другим . В зависимости от ключей, которые имеют ваши файлы матов, и формы возвращаемых массивов, вы можете объединить несколько массивов вместе и вернуть их как пакет.

mat_locations = [...]

def load_mat_files(list_of_mat_files):
  '''
  Loads a series of mat files from a list and returns them as a batch
  '''
  out = []
  for file in list_of_mat_files:
    mat = scipy.io.loadmat(file)
    processed_mat = ...  # do your magic here
    out.append(processed_mat)
  return np.stack(out)

def mat_generator(batch_size=128):
  '''
  Generator that loads 
  '''
  indices = np.random.permutation(len(mat_locations))  # shuffle the data
  ind = 0
  while ind < len(indices):
    yield load_mat_files(mat_locations[ind:ind+batch_size])
    ind += batch_size

gen = mat_generator()

Теперь gen может быть перебирать и возвращать пакеты данных. Вы можете тренировать свою модель так:

for e in range(epochs):

  gen = mat_generator()  # initialize the generator at each epoch

  for x in gen:

    # train model

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...