plaidml: could not broadcast input array from shape (3,2048) into shape (6144)

I just installed plaidml and i tried to run this example:

#!/usr/bin/env python

import plaidml.keras
plaidml.keras.install_backend() 

import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.datasets import mnist
from keras.utils import np_utils

# fix a random seed for reproducibility
np.random.seed(9)

# user inputs
nb_epoch = 25
num_classes = 10
batch_size = 128
train_size = 60000
test_size = 10000
v_length = 784

# split the mnist data into train and test
(trainData, trainLabels), (testData, testLabels) = mnist.load_data()


# reshape the dataset
trainData = trainData.reshape(train_size, v_length)
testData = testData.reshape(test_size, v_length)
trainData = trainData.astype("float32")
testData = testData.astype("float32")
trainData /= 255
testData /= 255


# convert class vectors to binary class matrices --> one-hot encoding
mTrainLabels = np_utils.to_categorical(trainLabels, num_classes)
mTestLabels = np_utils.to_categorical(testLabels, num_classes)

# create the model
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(256))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(num_classes))
model.add(Activation("softmax"))

# summarize the model
model.summary()

# compile the model
model.compile(loss="categorical_crossentropy",
			  optimizer="adam",
			  metrics=["accuracy"])

# fit the model
history = model.fit(trainData, 
				 	mTrainLabels,
					validation_data=(testData, mTestLabels),
					batch_size=batch_size,
					nb_epoch=nb_epoch,
					verbose=2)

# print the history keys


# evaluate the model
scores = model.evaluate(testData, mTestLabels, verbose=0)

# history plot for accuracy
plt.plot(history.history["acc"])
plt.plot(history.history["val_acc"])
plt.title("Model Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend(["train", "test"], loc="upper left")
plt.show()

# history plot for accuracy
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.title("Model Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend(["train", "test"], loc="upper left")
plt.show()

and I got this error

could not broadcast input array from shape (3,2048) into shape (6144)

Then I tried running Hello VGG example from plaidml github page and I got the same error.

I am using plaidml 0.3.4 on ubuntu in virtualenv and I am trying to run this code on rx 480.

Tnx for help.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 16 (2 by maintainers)

Most upvoted comments

Took a realy quick look and toyed with the code. Seems that the two methods “copy_from_ndarray” and “copy_to_ndarray” use the function np.ctypeslib.as_array(self, shape=src.shape). “self” is from type “class _view” and therefore i asume ctypeslib.as_array, according to the documentation, ignores the shape parameter. Intuitively i tried np.ctypeslib.as_array(self._base, shape=src.shape) in both methods and this seems to work. Im really new to this topic so maybe (propably!) this is complet nonsense but i haven’t had the time yet to take a deeper look.

The workaround proposed by @mzeug worked for me.

Trying to test my installation, it was giving me this error.

PS C:\WINDOWS\system32> plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1
INFO:plaidml:Opening device "opencl_amd_ellesmere.0"
could not broadcast input array from shape (3,2048) into shape (6144)

After mzeug’s suggested changes to the init.py file…

for me, the file was located at C:\Python37\Lib\site-packages\plaidml_init_.py

in both of these methods, “copy_from_ndarray” and “copy_to_ndarray” on lines 1193 and 1197

np.ctypeslib.as_array(self, shape=src.shape) changed to np.ctypeslib.as_array(self._base, shape=src.shape)

it seems like the operation was able to be completed.

PS C:\WINDOWS\system32> plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1
INFO:plaidml:Opening device "opencl_amd_ellesmere.0"
could not broadcast input array from shape (3,2048) into shape (6144)
Set --print-stacktraces to see the entire traceback
PS C:\WINDOWS\system32> plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1
INFO:plaidml:Opening device "opencl_amd_ellesmere.0"
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.6/mobilenet_1_0_224_tf.h5
16809984/17225924 [============================>.] - ETA: 0sModel loaded.
Compiling network...
Warming up ...
Main timing
Example finished, elapsed: 4.597696304321289 (compile), 6.579812526702881 (execution), 0.006425598170608282 (execution per example)
Correctness: PASS, max_error: 1.8053706298815086e-05, max_abs_error: 9.760260581970215e-07, fail_ratio: 0.0

Windows 10 Pro 64-bit | CPU: AMD Ryzen 1800X | GPU: AMD RX580 8GB | python 3.7.0

I have basically no idea what I’m doing, but I hope this helps.

Thanks @mzeug , that is a good solution for this bug! We’re putting together a release that will include this bugfix; in the interim, if you’re experiencing this bug you can use @mzeug 's fix (@dr-romster 's post above gives good details about where to make the changes).