Keras 预测不适用于多个 GPU

Keras predict not working for multiple GPU's

我最近实现了此 make_parallel 代码 (https://github.com/kuza55/keras-extras/blob/master/utils/multi_gpu.py) 以在多个 GPU 上进行测试。在实现 predict_classes() 函数后,新模型结构无法正常工作,经过一些阅读后,我转而使用预测函数。此函数仅适用于特定批次大小,例如批次大小为 750 时有效,而 500、100 和 350 会失败并出现以下错误:

ValueError: could not broadcast input array from shape (348,15) into shape 
(350,15)

培训以 batch_size 75 分完成。知道为什么会发生这种情况或我该如何解决吗?

pointFeatures = np.zeros((batchSize,featureSize))
libfeatures.getBatchOfFeatures(i,batchSize,pointFeatures)

pointFeatures = pointFeatures.reshape(batchSize, FeatureShape.img_rows, 
                FeatureShape.img_cols, FeatureShape.img_width, 
                FeatureShape.img_channels)

pointFeatures = pointFeatures.astype('float32')

results = model.predict(pointFeatures, verbose=True, 
          batch_size=FeatureShape.mini_batch_size)

如果您使用 make_parallel 函数,您需要确保样本数可以被 batch_size*N 整除,其中 N 是您使用的 GPU 数量。例如:

nb_samples = X.shape[0] - X.shape[0]%(batch_size*N)
X = X[:nb_samples]

您可以使用不同的 batch_size 进行训练和测试,只要样本数能被 batch_size*N 整除即可。