代码笔记7 pydensecrf库使用中出现的ValueError: ndarray is not C-contiguous
作者:互联网
1 源码
源码来自于github[1],主要是用于对语义分割的结果进行DenseCRF处理,以便进一步增加精度。至于理论,等我弄懂了再来写哈哈哈,感觉写这个的文特别少,大家又都没讲清楚。待我有空去研究一下提出的原文。
"""
Function which returns the labelled image after applying CRF
"""
# Original_image = Image which has to labelled
# Annotated image = Which has been labelled by some technique( FCN in this case)
# Output_image = Name of the final output image after applying CRF
# Use_2d = boolean variable
# if use_2d = True specialised 2D fucntions will be applied
# else Generic functions will be applied
def crf(original_image, annotated_image, output_image, use_2d=True):
# Converting annotated image to RGB if it is Gray scale
if (len(annotated_image.shape) < 3):
annotated_image = gray2rgb(annotated_image).astype(np.uint8)
cv2.imwrite("testing2.png", annotated_image)
annotated_image = annotated_image.astype(np.uint32)
# Converting the annotations RGB color to single 32 bit integer
annotated_label = annotated_image[:, :, 0].astype(np.uint32) + (annotated_image[:, :, 1] << 8).astype(np.uint32) + (
annotated_image[:, :, 2] << 16).astype(np.uint32)
# Convert the 32bit integer color to 0,1, 2, ... labels.
colors, labels = np.unique(annotated_label, return_inverse=True)
# Creating a mapping back to 32 bit colors
colorize = np.empty((len(colors), 3), np.uint8)
colorize[:, 0] = (colors & 0x0000FF)
colorize[:, 1] = (colors & 0x00FF00) >> 8
colorize[:, 2] = (colors & 0xFF0000) >> 16
# Gives no of class labels in the annotated image
n_labels = len(set(labels.flat))
print("No of labels in the Image are ")
print(n_labels)
# Setting up the CRF model
if use_2d:
d = dcrf.DenseCRF2D(original_image.shape[1], original_image.shape[0], n_labels)
# get unary potentials (neg log probability)
U = unary_from_labels(labels, n_labels, gt_prob=0.90, zero_unsure=False)
d.setUnaryEnergy(U)
# This adds the color-independent term, features are the locations only.
d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
# This adds the color-dependent term, i.e. features are (x,y,r,g,b).
print(original_image.flags)
original_image = original_image.copy(order='C')
d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,
compat=10,
kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
# Run Inference for 5 steps
Q = d.inference(5)
# Find out the most probable class for each pixel.
MAP = np.argmax(Q, axis=0)
# Convert the MAP (labels) back to the corresponding colors and save the image.
# Note that there is no "unknown" here anymore, no matter what we had at first.
MAP = colorize[MAP, :]
cv2.imwrite(output_image, MAP.reshape(original_image.shape))
return MAP.reshape(original_image.shape)
关于代码的解读,一个知乎老哥写的不错[2],我就不多言了。
2 bug
不知道大家会不会出现这样的问题。我在使用时出现了bug
d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,
File "pydensecrf\densecrf.pyx", line 126, in pydensecrf.densecrf.DenseCRF2D.addPairwiseBilateral
File "stringsource", line 658, in View.MemoryView.memoryview_cwrapper
File "stringsource", line 349, in View.MemoryView.memoryview.__cinit__
ValueError: ndarray is not C-contiguous
在网上找了许多关于这个报错的问题,不过好像没有与这种情况相关的。
3 解决
不过我觉得这个是转载的,我也找不到原处了。[3]
大概原因是因为输入的nparray的C_CONTIGUOUS属性为False。这个属性可以通过array.flags
进行查看
C_CONTIGUOUS : False
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
所以我们只要修改nparray的属性就行
original_image = original_image.copy(order='C')
完美解决
Refrences
[1]https://github.com/Gurupradeep/FCN-for-Semantic-Segmentation
[2]https://zhuanlan.zhihu.com/p/64854535
[3]https://www.manongdao.com/article-2171216.html
标签:MAP,False,annotated,contiguous,labels,ValueError,image,pydensecrf,original 来源: https://www.cnblogs.com/HumbleHater/p/16247287.html