【发布时间】:2021-03-20 18:26:34
【问题描述】:
我用Keras 和Tensorflow 训练了一个模型,并生成了一个保存模型的.h5 文件。这是我的代码(我只包含了模型而不是数据处理 sn-p 以便它更具可读性):
ts = 0.3 # Percentage of images that we want to use for testing. The rest is used for training.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=ts, random_state=42)
# Import of keras model and hidden layers for our convolutional network
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Dense, Flatten
from tensorflow.python.client import device_lib
from keras import backend as K
# Construction of model
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(120, 320, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
# Configures the model for training
model.compile(optimizer='adam', # Optimization routine, which tells the computer how to adjust the parameter values to minimize the loss function.
loss='sparse_categorical_crossentropy', # Loss function, which tells us how bad our predictions are.
metrics=['accuracy']) # List of metrics to be evaluated by the model during training and testing.
# Trains the model for a given number of epochs (iterations on a dataset) and validates it.
model.fit(X_train, y_train, epochs=5, batch_size=64, verbose=2, validation_data=(X_test, y_test))
# Save entire model to a HDF5 file
model.save('handrecognition_model.h5')
我已经在包含手势照片的 this 数据集上训练了我的模型。现在我有一张我用笔记本电脑相机拍摄的照片,假设它被称为"thumbs_up.jpg",它包含一张我竖起大拇指的照片。我想把这张照片放在我训练的模型上,看看它是否能正确预测。我知道我可能在这里遗漏了一些非常基本的东西,但是我该怎么做呢?我应该以某种方式使用.h5 文件吗?抱歉,如果我的问题是超级基本/显而易见的,我只是很困惑,不知道该怎么做。提前致谢
【问题讨论】:
标签: python tensorflow keras hdf5