Python 3.x ValueError:两个形状中的尺寸2必须相等,但分别为3和32
我目前正在学习Tensorflow。我通过Django应用程序使用预先训练的模型进行预测,但是在预测过程中,我得到了错误,请帮助我解决错误Python 3.x ValueError:两个形状中的尺寸2必须相等,但分别为3和32,python-3.x,tensorflow,keras,deep-learning,Python 3.x,Tensorflow,Keras,Deep Learning,我目前正在学习Tensorflow。我通过Django应用程序使用预先训练的模型进行预测,但是在预测过程中,我得到了错误,请帮助我解决错误 def alpha_to_color(image, color=(255, 255, 255)): x = np.array(image) r, g, b, a = np.rollaxis(x, axis=-1) r[a == 0] = color[0] g[a == 0] = color[1]
def alpha_to_color(image, color=(255, 255, 255)):
x = np.array(image)
r, g, b, a = np.rollaxis(x, axis=-1)
r[a == 0] = color[0]
g[a == 0] = color[1]
b[a == 0] = color[2]
x = np.dstack([r, g, b, a])
return Image.fromarray(x, 'RGBA')
def preprocess(data):
# dimensions of our images.
img_width, img_height = 250, 250
dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$')
imgb64 = dataUrlPattern.match(data).group(2)
if imgb64 is not None and len(imgb64) > 0:
data= base64.b64decode(imgb64)
im1 = Image.open(BytesIO(data))
im1 = alpha_to_color(im1)
im1=im1.convert('RGB')
im1= im1.resize((250,250))
print("[INFO] loading and preprocessing image...")
image = img_to_array(im1)
image = image.reshape((1,) + image.shape) # this is a Numpy array with shape (1, 3, 250,250)
test_ob = ImageDataGenerator(rescale=1./255)
X=[]
for batch in test_ob.flow(image, batch_size=1):
X= batch
break
return X
def build_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(250, 250, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
#model.add(Dropout(0.5))
model.add(Dense(250))
model.add(Activation('sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
module_dir = os.path.dirname(__file__) # get current directory
file_path = os.path.join(module_dir, 'bestWeight.hdf5')
model.load_weights(file_path)
return model
def load_labels():
module_dir = os.path.dirname(__file__) # get current directory
file_path = os.path.join(module_dir, 'labels.csv')
df = pd.read_csv(file_path,
header=0)
target_names = df['Category'].tolist()
return target_names
def predict_labels(data):
model = build_model()
image = preprocess(data)
target_names = load_labels()
encoder = LabelEncoder()
encoder.fit(target_names)
pL = model.predict(image)
prob = model.predict_proba(image)
p= np.argsort(pL, axis=1)
n1 = (p[:,-4:]) #gives top 5 labels
pL_names = (encoder.inverse_transform(n1))
pL_names = pL_names[0]
p= np.sort(prob, axis=1)
convertperc = [stats.percentileofscore(p[0], a, 'rank') for a in p[0]]
n = (convertperc[-4:]) #gives top 5 probabilities perc
prob_values = (p[:,-4:])
prob_single_values = prob_values[0]
return zip(pL_names,n,prob_single_values)
代码给出了这个错误
ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].
运行交叉熵行时发生此错误。我不明白为什么会这样,如果你需要更多的信息,我很乐意给你。
这是我的编译日志
Traceback (most recent call last):
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1576, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 148, in recognizeSketch
result = predict_labels(data)
File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 113, in predict_labels
model = build_model()
File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 99, in build_model
model.load_weights(file_path)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\engine\network.py", line 1161, in load_weights
f, self.layers, reshape=reshape)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\engine\saving.py", line 928, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2435, in batch_set_value
assign_op = x.assign(assign_placeholder)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 645, in assign
return state_ops.assign(self._variable, value, use_locking=use_locking)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\state_ops.py", line 216, in assign
validate_shape=validate_shape)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_state_ops.py", line 63, in assign
use_locking=use_locking, name=name)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
return func(*args, **kwargs)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
op_def=op_def)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1731, in __init__
control_input_ops)
File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1579, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].
你能发送一个最小的例子,你的代码与错误,可以在我们这边执行?这将非常有帮助。我想这个错误是以错误的通道顺序出现的。生成具有以下形状的批处理:
image = image.reshape((1,) + image.shape) # shape = (1, 3, 250,250)
在keras中,通道应该是最后一个维度:(1250、250、3)请发布完整的stacktrace,因为现在还不清楚是什么原因造成的erorr@MikhailStepanov我已经更新了stacktrace。