python-3.x - ValueError:两个形状的维度 2 必须相等,但分别是 3 和 32
问题描述
我目前正在学习 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]
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].
运行 cross_entropy 的行时会发生此错误。我不明白为什么会这样,如果您需要更多信息,我很乐意提供给您。这是我的编译日志
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 中,通道应该是最后一个维度:(1, 250, 250, 3)
推荐阅读
- sql - 如何合并这些将列插入表的查询
- node.js - 如何使用节点将用户数据对象与令牌一起发送到角度前端?
- javascript - 如何编写按钮以指向另一个网页并选中复选框
- android - 为什么android画布中的drawVertices需要禁用硬件加速?
- javascript - var damage = Math.max(Math.floor(Math.random() * max)+1, min) 第一个 'Math.max' 和最后一个 ',min' 是什么意思
- php - 网页抓取不仅在本网站上有效
- xcode - 条件绑定的初始化程序必须具有 Optional 类型,而不是 '[String : Double]'
- intellij-idea - 在点 (.) 运算符之后启用建议?
- asp.net - AntiFogeryToken Cookie 始终存储并发送回服务器,即使我阻止了 IE 11 上的所有 cookie(其他浏览器按预期工作)
- javascript - 单节点脚本顺序运行多个节点脚本