Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow裁剪和调整大小:形状必须为等级4,但为等级3_Python_Tensorflow_Image Processing - Fatal编程技术网

Python Tensorflow裁剪和调整大小:形状必须为等级4,但为等级3

Python Tensorflow裁剪和调整大小:形状必须为等级4,但为等级3,python,tensorflow,image-processing,Python,Tensorflow,Image Processing,我不熟悉Python和Tensorflow,所以如果这个问题非常愚蠢和简单,请不要怪我^^ 我正在尝试从图像中心裁剪一部分,并将其调整为图像的原始大小 这是我的密码: def crop(img_tensor, size): if size[0] < img_tensor.shape[0] and size[1] < img_tensor.shape[1]: anchor = (img_tensor.shape[0] // 2 - size[0] // 2, img_tenso

我不熟悉Python和Tensorflow,所以如果这个问题非常愚蠢和简单,请不要怪我^^

我正在尝试从图像中心裁剪一部分,并将其调整为图像的原始大小

这是我的密码:

def crop(img_tensor, size):
if size[0] < img_tensor.shape[0] and size[1] < img_tensor.shape[1]:
    anchor = (img_tensor.shape[0] // 2 - size[0] // 2, img_tensor.shape[1] // 2 - size[1] // 2)
    return tf.image.crop_and_resize(image=img_tensor, boxes=[[anchor[0] // img.shape[0], anchor[1] // img.shape[1],
                                                              (anchor[0] + size[0]) // img.shape[0],
                                                              (anchor[1] + size[1]) // img.shape[1]]],
                                    box_indices=[0], crop_size=[img_tensor.shape[0], img_tensor.shape[1]])
else:
    return img
大小是一个正常的列表:(100100)

当我运行程序时,我得到以下错误代码:

ValueError: Shape must be rank 4 but is rank 3 for '{{node CropAndResize}} = CropAndResize[T=DT_UINT8, extrapolation_value=0, method="bilinear"](img_tensor, CropAndResize/boxes, CropAndResize/box_ind, CropAndResize/crop_size)' with input shapes: [539,500,4], [1,4], [1], [2].
现在我想知道,我的错误是什么。据我所知,函数需要四个输入参数,不是吗?它得到了四个参数


我在这里完全迷路了^ ^

尝试
tf.image.crop\u和\u resize(image=tf.expand\u dims(img\u tensor,axis=0),…
。您对resize函数的输入需要一个批处理维度不幸的是无法工作,但我得到了一个新的错误消息:D
ValueError:Shape对于“{{node encode jpeg}}=encode jpeg”,必须是秩3,但是秩4[chroma_downsampling=true,density_unit=“in”,format=“”,optimize_size=false,progressive=false,quality=95,x_density=300,xmp_metadata=“”,y_density=300](跨步_切片)和输入形状:[1539,3,4].
此错误发生在您发布的代码之外。您是否在完成大小调整后将JPEG保存在某个位置?哦,是的,我明白了…我将其保存为:
img\u tensor=tf.image.convert\u image\u dtype(img\u tensor,dtype=tf.uint8)img=tf.io.encode\u JPEG(img\u tensor[:,0:3])tf.io.write\u文件(输出文件名\u tensor,img)
。所以我必须找出,如何将第四维切掉:D
tf.squence(img\u张量)
ValueError: Shape must be rank 4 but is rank 3 for '{{node CropAndResize}} = CropAndResize[T=DT_UINT8, extrapolation_value=0, method="bilinear"](img_tensor, CropAndResize/boxes, CropAndResize/box_ind, CropAndResize/crop_size)' with input shapes: [539,500,4], [1,4], [1], [2].