использование пользовательского набора данных для распознавания лиц вместо MNIST - PullRequest
0 голосов
/ 31 января 2019

Я хотел бы использовать пользовательский набор данных, который содержит изображение лиц для другого человека.Я планирую использовать CNN и Stacked-Autoencoder для классификации моего изображения.

если я изменю (x_train, _), (x_test, _) = mnist.load_data ()?

илиизмените input_img, я думаю, что проблема заключается во входных данных, но я понятия не имею, где они должны быть изменены.

Я потерян, мне нужна помощь.

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(28, 28, 1))  # adapt this if using`channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
 encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if 
using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if 
using `channels_first` image data format


from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,
               epochs=50,
               batch_size=128,
               shuffle=True,
               validation_data=(x_test, x_test),
               callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

decoded_imgs = autoencoder.predict(x_test)

n = 10
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 4))
for i in range(n):
# display original
ax = plt.subplot(2, n, i)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# display reconstruction
ax = plt.subplot(2, n, i + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()

Ответы [ 2 ]

0 голосов
/ 31 января 2019

Вам потребуется изменить (x_train, _), (x_test, _) = mnist.load_data () с помощью загрузчика данных.Вы можете использовать класс keras ImageDataGenerator для этого или создать свой собственный .Если размер вашего изображения намного больше, чем 28 x 28, возможно, вам также потребуется изменить архитектуру модели, поскольку изменение их формы непосредственно на 28 x 28 не даст хорошего результата.

0 голосов
/ 31 января 2019

Вам нужно загрузить свой набор данных и разбить его на два подмножества: x_train и x_test.

В каком формате хранятся ваши данные?

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