问题
I get this Error:
InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(1, 2304), b.shape=(2304, 512), m=1, n=512, k=2304
[[Node: dense_1/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/gpu:0"](flatten_1/Reshape, dense_1/kernel/read)]]
This is my code. I try to load a keras model. and predict the class of an image. But it always crashes when I want to predict the class of the image.
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.models import load_model
import cv2
import numpy as np
from keras.preprocessing import image
import time
import os
num_classes = 10
save_dir = os.path.join(os.getcwd(), 'examples/saved_models')
model_name = 'keras_cifar10_trained_model.h5'
model = load_model(save_dir + '/' + model_name)
# The data, shuffled and split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
x_train_float = x_train.astype('float32')
x_test_float = x_test.astype('float32')
x_train_bin = x_train_float / 255
x_test_bin = x_test_float / 255
nr_pic = 0
# predicting images
img = image.load_img('apfel.jpg', target_size=(32, 32))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255
#while True:
image = x_test[nr_pic]
image = cv2.resize(image,(320, 320), interpolation = cv2.INTER_CUBIC)
image_bin = np.expand_dims(x_test_bin[nr_pic], axis=0)
classes = model.predict_classes(x, batch_size=10)
print(classes)
If I write the same code and delete this part from the code:
#while True:
image = x_test[nr_pic]
image = cv2.resize(image,(320, 320), interpolation = cv2.INTER_CUBIC)
image_bin = np.expand_dims(x_test_bin[nr_pic], axis=0)
then there is no Error. Here is the full code:
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.models import load_model
import cv2
import numpy as np
from keras.preprocessing import image
import time
import os
num_classes = 10
save_dir = os.path.join(os.getcwd(), 'examples/saved_models')
model_name = 'keras_cifar10_trained_model.h5'
model = load_model(save_dir + '/' + model_name)
# The data, shuffled and split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
x_train_float = x_train.astype('float32')
x_test_float = x_test.astype('float32')
x_train_bin = x_train_float / 255
x_test_bin = x_test_float / 255
nr_pic = 0
# predicting images
img = image.load_img('apfel.jpg', target_size=(32, 32))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255
classes = model.predict_classes(x, batch_size=10)
print(classes)
Why is that so? Is the cv2 part using to much GPU? What is the difference?
回答1:
From 32x32 (CIFAR10 images) to 320x320 images you grow your dataset size by 100. What's happening there I think is that in the first example the data passed in your model are too big (100x bigger than the 2nd example without interpolation).
来源:https://stackoverflow.com/questions/47047716/cv2-keras-internalerror-see-above-for-traceback-blas-gemm-launch-failed