解决MindSpore ResNet 模型推理时 ValueError: x shape size must be equal to 4, but got 2 错误
这个报错是因为在使用模型进行推理时,输入的数据维度不符合模型要求。具体来说,这个模型中的第一个卷积层需要输入四维数据,即(batch_size, channels, height, width),而你输入的数据可能只有两维。因此,需要将输入数据转换成四维数据再进行推理。可以在推理代码中添加如下代码:
face = cv2.resize(face, (100, 100))
face = face.transpose().astype(np.float32) / 255.
face = np.expand_dims(face, axis=0) # 扩展维度,变成(batch_size, channels, height, width)
face = Tensor(face)
output = network(face)
predicted_class = np.argmax(output.asnumpy(), axis=1)
label = subjects[predicted_class[0]]
这样就可以将输入数据转换成四维数据,然后进行推理。
原文地址: https://www.cveoy.top/t/topic/jqx7 著作权归作者所有。请勿转载和采集!