Tensorflow density_image_warp无法将类型<type'list'=“”>的对象转换为Tensor

时间:2018-11-24 15:47:47

标签: tensorflow keras

在Ubuntu 16.04和14.04上使用最新的Keras和Tensorflow。对于以下代码:

img2D = Input(shape=(100, 100, 3))
refPosX = Input(shape=(100, 100, 1))
refPosY = Input(shape=(100, 100, 1))
# refImg = Input(shape=(100, 100, 3))

# Passing values to depth net
depth_map = depth_net(dFeatures)
curX = tf.multiply(depth_map, refPosX)
# curY = K.dot(depth_map, refPosY)
curY = tf.multiply(depth_map, refPosY)
# dMove = concatenate([curX, curY])
dMove = tf.concat([curX, curY], axis=3)

warped = tfc.image.dense_image_warp(img2D, dMove)

我收到错误输出:

Using TensorFlow backend.
Traceback (most recent call last):
File "/home/carson/ws/dla/test_network_5/depth_and_color_nets.py", line 89, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/image/python/ops/dense_image_warp.py", line 195, in dense_image_warp
    [batch_size, height * width, 2])
 File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 6482, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
raise err
TypeError: Failed to convert object of type <type 'list'> to Tensor. Contents: [None, 10000, 2]. Consider casting elements to a supported type.

我有点困惑,因为我不知道我要去哪里错了。为什么元素不是支持的类型?我在什么时候将列表对象传递给函数?

2 个答案:

答案 0 :(得分:1)

对于那些感兴趣的人,您必须将输入定义为

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {

        if (data.getClipData() != null) {
            totalItemsSelected = data.getClipData().getItemCount();

            for (int i = 0; i < totalItemsSelected; i++) {
                Uri fileUri = data.getClipData().getItemAt(i).getUri();
                String fileName = getFileName(fileUri);

                fileNameList.add(fileName);
                fileDoneList.add("uploading");
                uploadListAdapter.notifyDataSetChanged();

                StorageReference fileToUpload = mStorage.child("Images").child(fileName);

                final int finalI = i;
                fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        fileDoneList.remove(finalI);
                        fileDoneList.add(finalI, "done");
                        uploadListAdapter.notifyDataSetChanged();
                        Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
                        ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
                        imageURL = imageUploadInfo.getImageURL();
                        imagesList.add(imageURL);

                        Intent idata = new Intent();
                        idata.putExtra("imageURL", imageURL);
                        idata.putExtra("count", totalItemsSelected);
                        setResult(RESULT_OK, idata);
                        finish();
                    }
                });
            }
        } else {
            Uri fileUri = data.getData();
            String fileName = getFileName(fileUri);

            fileNameList.add(fileName);
            fileDoneList.add("uploading");
            uploadListAdapter.notifyDataSetChanged();

            StorageReference fileToUpload = mStorage.child("Images").child(fileName);

            fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    fileDoneList.remove(0);
                    fileDoneList.add(0, "done");
                    uploadListAdapter.notifyDataSetChanged();
                    Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
                    ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
                    imageURL = imageUploadInfo.getImageURL();
                    imagesList.add(imageURL);

                    Intent idata = new Intent();
                    idata.putExtra("imageURL", imageURL);
                    idata.putExtra("count", 1);
                    setResult(RESULT_OK, idata);
                    finish();
                }
            });
        }
    }
}

该函数需要batch_size。通常不用Keras就可以逃脱。

答案 1 :(得分:0)

错误来自输入的形状。函数tfc.image.dense_image_warp需要分别以指定的批处理大小输入形状[batch, height, width, channels][batch, height, width, 2]。 (cf https://www.tensorflow.org/api_docs/python/tf/contrib/image/dense_image_warp

具体地说,不支持None的批量大小值,这会导致您遇到错误。