【发布时间】:2020-06-15 01:08:07
【问题描述】:
我想使用此代码训练 CNN + LSTM 模型以使用 CNN + LSTM 驾驶汽车
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, GlobalMaxPool2D, Flatten, Dropout, Dense, TimeDistributed, GRU, LSTM
from tensorflow.keras.applications.vgg16 import VGG16
def vgg(input_shape, num_classes):
# create a VGG16 "model", we will use
# image with shape (224, 224, 3)
vgg = VGG16(
include_top=False,
weights='imagenet',
input_shape=(200, 160, 3)
)
# do not train first layers, I want to only train
# the 4 last layers (my own choice, up to you)
for layer in vgg.layers[:-4]:
layer.trainable = False
# create a Sequential model
model = Sequential()
# add vgg model for 4 input images (keeping the right shape
model.add(
TimeDistributed(vgg, input_shape=(4, 200, 160, 3))
)
# now, flatten on each output to send 5
# outputs with one dimension to LSTM
model.add(
TimeDistributed(
Flatten()
)
)
model.add(LSTM(256, activation='relu', return_sequences=False))
# finalize with standard Dense, Dropout...
model.add(Dense(128, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
这是我的主要代码
# Splitting data into a training set and test set
train_data = np.load("/mydrive/Mister_car/training_data-1.npy", allow_pickle=True)
train = train_data[:]
test = train_data[:]
print(np.shape(train[5]))
X = np.array([i[0] for i in train]).reshape(-1, 4, WIDTH, HEIGHT, 3)
Y = np.array([i[1] for i in train])#.reshape(-1, 9)
x_test = np.array([i[0] for i in test]).reshape(-1, 4, WIDTH, HEIGHT, 3)
y_test = np.array([i[1] for i in test])#.reshape(-1, 9)
# start training
model.fit(x=X,
y=Y,
epochs=10,
validation_data=(x_test, y_test)
)
# save the whole model
model.save(MODEL_DIR)
我有一个数组,这个数组中的每个元素都有 2 个元素: 数组 [0] 是 4 个形状为 (200,160,3) 的图像的序列 array 1 是一个有 9 个元素的数组
但在模型保存时的最后一个纪元后出现以下错误
【问题讨论】:
标签: python tensorflow keras conv-neural-network