Я хочу обучить свою нейронную сеть с помощью набора данных cifar10, а позже я попробую некоторые новые вещи с набором данных cifar100. У меня есть оба набора данных на моем жестком диске, и я могу распаковать папку и импортировать файлы bash с кодом, который я нашел, я думаю о stackoverflow, но мне нужно решение для импорта этих данных, которое я могу понять и изучить! потому что я не хочу копировать и вставлять каждый раз код, который я действительно не понимаю.
есть ли у кого-нибудь простой, для новичка ie "обучаемый" фрагмент кода? Вы также можете построчно объяснить мне, что делает этот код, и, пожалуйста, как вы разговариваете с 6-летним ребенком: D
import pickle
import sys
from keras import backend as K
from keras.utils import to_categorical
def load_batch(fpath, label_key='labels'):
with open(fpath, 'rb') as f:
if sys.version_info < (3,):
d = pickle.load(f)
else:
d = pickle.load(f, encoding='bytes')
# decode utf8
d_decoded = {}
for k, v in d.items():
d_decoded[k.decode('utf8')] = v
d = d_decoded
data = d['data']
labels = d[label_key]
data = data.reshape(data.shape[0], 3, 32, 32)
return data, labels
def load_data():
path = 'cifar-10-batches-py'
num_train_samples = 50000
x_train_local = np.empty((num_train_samples, 3, 32, 32), dtype='uint8')
y_train_local = np.empty((num_train_samples,), dtype='uint8')
for i in range(1, 6):
fpath = os.path.join(path, 'data_batch_' + str(i))
(x_train_local[(i - 1) * 10000: i * 10000, :, :, :],
y_train_local[(i - 1) * 10000: i * 10000]) = load_batch(fpath)
fpath = os.path.join(path, 'test_batch')
x_test_local, y_test_local = load_batch(fpath)
y_train_local = np.reshape(y_train_local, (len(y_train_local), 1))
y_test_local = np.reshape(y_test_local, (len(y_test_local), 1))
if K.image_data_format() == 'channels_last':
x_train_local = x_train_local.transpose(0, 2, 3, 1)
x_test_local = x_test_local.transpose(0, 2, 3, 1)
return (x_train_local, y_train_local), (x_test_local, y_test_local)
(x_train, x_label), (y_train, y_label) = load_data()
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
x_label = to_categorical(x_label)
y_label = to_categorical(y_label)