我正在使用AWS EC2实例上的4个GPU训练Resnet模型。该模型是使用Keras构建的,看起来像这样:
from PIL import Image
import numpy as np
from keras.applications.resnet50 import ResNet50
from keras.layers import Flatten, Dense
from keras.utils import multi_gpu_model
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import Model
def construct_model():
base = ResNet50(weights="imagenet", include_top=False, input_shape=(448, 448, 3))
x = Flatten()(base.output)
x = Dense(3)(x)
model = Model(inputs=base.inputs, outputs=x)
return model
def try_multi_gpu(model):
try:
model = multi_gpu_model(model, gpus=4, cpu_relocation=True)
print ("Using multi gpu model")
except ValueError:
print ("Using single GPU/CPU model")
return model
def train():
X_train, Y_train = load_dataset()
model = construct_model()
model = try_multi_gpu(model)
# Set checkpoints
filepath="weights-improvement-{epoch:02d}-{val_loss:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# Create a TensorBoard instance with the path to the logs directory
tensorboard = TensorBoard(log_dir='logs/{}'.format(time()))
callbacks_list.append(tensorboard)
model.fit(X_train, Y_train, initial_epoch=epoch_start ,epochs = 50, batch_size = 10, validation_split=0.15, verbose=1, callbacks=callbacks_list) model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])
训练完成后,我想通过加载训练后的权重来在本地CPU上运行预测:
def test(imgPath, weights):
im = Image.open(imgPath)
w, h = im.size
im = im.crop((0, (h-w)/2 ,w , (h+w)/2))
im = im.resize((448, 448), Image.ANTIALIAS)
imArr = np.array([np.asarray(im)])
model = construct_model()
model = try_multi_gpu(model)
model.load_weights(weights, by_name=True)
preds = model.predict(imArr)
print ("Prediction: ", preds)
但是当我尝试运行它时,我遇到了ValueError:
File "train.py", line 133, in <module>
test("test/IMG_5767_dark.jpg", "weights-improvement-48-0.01-phi-thetha.hdf5")
File "train.py", line 115, in test
model.load_weights(weights, by_name=True)
File "/usr/local/lib/python3.7/site-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/keras/engine/network.py", line 1227, in load_weights
reshape=reshape)
File "/usr/local/lib/python3.7/site-packages/keras/engine/saving.py", line 1309, in load_weights_from_hdf5_group_by_name
' element(s).')
ValueError: Layer #6 (named "dense_1") expects 2 weight(s), but the saved weights have 0 element(s).
当我在AWS实例上运行相同的预测代码时,它可以工作。有什么方法可以在本地计算机的CPU上运行它吗?