【发布时间】:2020-11-19 04:21:27
【问题描述】:
我是 Keras 深度学习的新手,所以如果我需要在这篇文章中包含更多数据,请通知我!所以目前我已经对我拥有的 MNIST 数据集的训练集进行了一些图像增强。所以,我参考了这篇文章here,并尝试将我的增强图像模型保存到数组中。但是当我尝试重新加载图像并放入我的mode.fit() 时,它会抛出这个错误:
AttributeError: 'NoneType' object has no attribute 'shape'
我该如何解决?
这是我当前的代码:
# get the dataset from keras library in tensorflow 2.0
mnist = tf.keras.datasets.mnist
# unpack the dataset to the respective x_train, y_train, x_test and y_test
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# flatten 28*28 pixel images to 784 pixels for each image(from a 2D array to a 1D array)
num_pixels = x_train.shape[1] * x_train.shape[2]
X_train = x_train.reshape(x_train.shape[0], num_pixels).astype('float32')
X_test = x_test.reshape(x_test.shape[0], num_pixels).astype('float32')
# standardise X_train and X_test
X_train /= 255
X_test /= 255
# convert to categorical
Y_train = tf.keras.utils.to_categorical(y_train, num_classes)
Y_test = tf.keras.utils.to_categorical(y_test, num_classes)
# reshape x_train and x_test to (n_images, x_shape, y_shape, channels)
# we are going to make chanels be 1 as we are not dealing with rgb images.
X_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
X_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# my cnn model
# Instantiate a Sequential model
model = Sequential(name="cnn_model_hyperParamTuned_sequential_LeNet")
# Layer 1 Conv2D
model.add(Conv2D(filters=150, kernel_size=(5, 5), strides=(1, 1), activation='elu', input_shape=(28, 28, 1), padding='same'))
model.add(BatchNormalization())
# Layer 2 Pooling Layer
model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))
# Layer 3 Conv2D
model.add(Conv2D(filters=140, kernel_size=(5, 5), strides=(1, 1), activation='elu', padding='valid'))
model.add(BatchNormalization())
# Layer 4 Pooling Layer
model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))
# Layer 5 Flatten
model.add(Flatten())
# Layer 6 Fully Connected Layer (Hidden Layer)
model.add(Dense(units=120, activation='tanh', kernel_initializer='normal', kernel_regularizer=l2(0.0001)))
model.add(Dropout(0.2))
# # Layer 7 Fully Connected Layer (Hidden Layer)
# model.add(Dense(units=84, activation='softmax'))
# Output layer
model.add(Dense(units=num_classes, activation='softmax'))
model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
# declare decay_rate and learning_rate here
learning_rate = 0.1
decay_rate = 0.1
# define the learning rate change
def exp_decay(epoch):
lrate = learning_rate * np.exp(-decay_rate*epoch)
return lrate
# learning schedule callback
loss_history = History()
lr_rate = LearningRateScheduler(exp_decay)
callbacks_list = [lr_rate]
# we shall do some ImageDataGenerator here to augment the input data, which can help prevent over-fitting
train_gen = ImageDataGenerator(
rotation_range=8,
shear_range=0.01,
zoom_range=0.08,
width_shift_range=0.1,
height_shift_range=0.1,
zca_whitening=True
)
test_gen = ImageDataGenerator()
# we want to store the augmented data in a numpy array so the next time we run this we do not get a different set of images, which might result in a totally different loss score
augmented_data = []
num_augmented = 0
for X_batch, Y_batch in train_gen.flow(X_train, Y_train, batch_size=100):
augmented_data.append(X_batch)
num_augmented += 100
if num_augmented == X_train.shape[0]:
break
# concatenate the augmented data into a numpy array
augmented_data = np.concatenate(augmented_data)
# create a "flow" for the data to flow through
# train_generator = train_gen.flow(X_train, Y_train, batch_size=100)
test_generator = test_gen.flow(X_test, Y_test, batch_size=100)
# train the model(we use .fit since .fit_generator is depreciated already)
history = model.fit(augmented_data,
batch_size=640,
steps_per_epoch=60000//100,
epochs=60,
callbacks=[EarlyStopping(monitor='val_loss', patience=5), callbacks_list],
validation_data=test_generator,
validation_steps=10000//100,
verbose=1)
【问题讨论】:
-
你的代码没有提到
X_train,它既没有被导入也没有被定义。它只表明你正在使用它,但不能说它是如何起源的。请提供代码,以便有办法查看为什么是None。 -
@dmitryro 嗨,我已经把零件包括在里面了。
-
@proddoggy4life 所以在你的
(x_train, y_train), (x_test, y_test) = mnist.load_data()行中,你假设你从mnist.load_data()调用得到的tuple 有x_train元素not empty,结果是 not case 并且必须是在使用其他所有内容之前进行检查。您必须检查返回的内容并找出其为空的原因。
标签: python numpy tensorflow keras