简体   繁体   English

Tensorflow/Keras - 重塑问题(重塑的输入是一个有 10 个值的张量,但请求的形状有 1 个)

[英]Tensorflow/Keras - Reshaping problem (Input to reshape is a tensor with 10 values, but the requested shape has 1)

I'm trying to recreate this work using my own dataset: https://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays/notebook我正在尝试使用我自己的数据集重新创建这项工作: https://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays/notebook

I've made some slight tweaks to the code to accommodate my data but I don't think that is what is causing an issue here;我对代码做了一些细微的调整以适应我的数据,但我认为这不是导致问题的原因; it could be though of course.当然可以

My code:我的代码:

AUTOTUNE = tf.data.AUTOTUNE
BATCH_SIZE = 16
IMAGE_SIZE = [180, 180]
EPOCHS = 25
CLASS_NAMES = np.array(['active', 'inactive'])

train_list_ds = tf.data.Dataset.from_tensor_slices(glob.glob(f'{WORKING}/train/*/*'))
val_list_ds = tf.data.Dataset.from_tensor_slices(glob.glob(f'{WORKING}/val/*/*'))
test_list_ds = tf.data.Dataset.from_tensor_slices(glob.glob(f'{WORKING}/test/*/*'))

#print(next(train_list_ds.batch(60_000).as_numpy_iterator())[:5])

def get_label(path):
  # switcher that converts the keywords to 1 or zero
  if tf.strings.split(path, os.path.sep) == "inactive":
    return 0 # inactive
  else:
    return 1 # active

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  resizedImage = tf.image.resize(img, IMAGE_SIZE)
  return resizedImage

def process_path(file_path):
    label = get_label(file_path)
    # load the raw data from the file as a string
    img = tf.io.read_file(file_path)
    img = decode_img(img)
    return img, label

train_ds = train_list_ds.map(process_path, num_parallel_calls=AUTOTUNE)
val_ds = val_list_ds.map(process_path, num_parallel_calls=AUTOTUNE)
test_ds = test_list_ds.map(process_path, num_parallel_calls=AUTOTUNE)
test_ds = test_ds.batch(BATCH_SIZE)

image_batch, label_batch = next(iter(train_ds))

And the error:和错误:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-21-d0b00c7e96e2> in <module>
     68 test_ds = test_ds.batch(BATCH_SIZE)
     69 
---> 70 image_batch, label_batch = next(iter(train_ds))
     71 
     72 # Use buffered prefetching so we can yield data from disk without having I/O become blocking.

3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   7184 def raise_from_not_ok_status(e, name):
   7185   e.message += (" name: " + name if name is not None else "")
-> 7186   raise core._status_to_exception(e) from None  # pylint: disable=protected-access
   7187 
   7188 

InvalidArgumentError: Input to reshape is a tensor with 10 values, but the requested shape has 1
     [[{{node Reshape}}]] [Op:IteratorGetNext]

I can gather from the error that I have a mismatch in resizing, I believe it is pointing to the wrong place though and it is something to do with the line resizedImage = tf.image.resize(img, IMAGE_SIZE)我可以从错误中收集到我在调整大小时不匹配,但我相信它指向了错误的位置,这与resizedImage = tf.image.resize(img, IMAGE_SIZE)行有关

I've looked through the docs and on SO but I can't find anything that might help me.我已经浏览了文档和 SO,但我找不到任何可能对我有帮助的东西。 I've tried debugging as best I can by printing things out on screen.我已经尽我所能通过在屏幕上打印出来进行调试。 I also tried changing the IMAGE_SIZE but that made no difference.我也尝试更改IMAGE_SIZE但这没有任何区别。

On the images, they are different sizes on disk, in JPG form.在图像上,它们在磁盘上的大小不同,格式为 JPG。 I expected that not to matter as we can use this step tot resize them for processing by the model later.我希望这无关紧要,因为我们可以使用此步骤来调整它们的大小,以便稍后由 model 处理。

Other useful information is that I'm working on the pro version of Google Collab and the files are stored in a Google drive location.其他有用的信息是我正在开发 Google Collab 的专业版,并且文件存储在 Google 驱动器位置。 Again I don't think that's a problem or cause of anything;同样,我不认为这是任何问题或原因。 I'm trying to write things pre-emptively that people might ask.我试图先发制人地写一些人们可能会问的东西。

Finally there is a prepare_for_training function in Amy's code on Kaggle that is called before the last line I provided in my code above.最后,在 Kaggle 上的 Amy 代码中有一个prepare_for_training function,它在我在上面代码中提供的最后一行之前调用。 I can trigger the same error without calling that function so I omitted it intentionally to help keep the example code cleaner.我可以在不调用 function 的情况下触发相同的错误,所以我故意省略了它以帮助保持示例代码更清晰。 If you want to see it, here is a quick link to it in the notebook: https://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays?scriptVersionId=39162263&cellId=26如果你想看的话,这里有一个在笔记本中的快速链接: https://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays?scriptVersionId=39162263&cellId=26

Any advice appreciated.任何建议表示赞赏。 Thanks.谢谢。

May be the problem come from the get_label(path) method, since you are splitting path according the sep, it will return a list of many elements, make sure that you select only one element to perform test, try this:可能是get_label(path)方法的问题,因为你是根据 sep 分割路径,它会返回一个包含许多元素的列表,确保你 select 只有一个元素来执行测试,试试这个:

tf.strings.split(path, os.path.sep)[-1] == "inactive"

i assume that the last element is the label, you just replace this index by the position of the label in this list.我假设最后一个元素是 label,您只需将此索引替换为此列表中 label 的 position 即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python / TensorFlow / Keras-要整形的输入是具有300个值的张量,但请求的形状具有200 [[{{nodecoder_1 / reshape_1 / Reshape}}]]] - Python/TensorFlow/Keras - Input to reshape is a tensor with 300 values, but the requested shape has 200 [[{{node decoder_1/reshape_1/Reshape}}]] tensorflow/serving - reshape 的输入是一个有 100 个值的张量,但请求的形状有 10000 - tensorflow/serving - Input to reshape is a tensor with 100 values, but the requested shape has 10000 Python / Tensorflow - 重塑的输入是具有 7849215 值的张量,但请求的形状具有 7839216 - Python / Tensorflow - Input to reshape is a tensor with 7849215 values, but the requested shape has 7839216 张量流,图像分割convnet InvalidArgumentError:整形的输入是张量为28800000的张量,但请求的形状为57600 - tensorflow, image segmentation convnet InvalidArgumentError: Input to reshape is a tensor with 28800000 values, but the requested shape has 57600 reshape 的输入是一个有 37632 个值的张量,但请求的形状有 150528 - Input to reshape is a tensor with 37632 values, but the requested shape has 150528 重塑的输入是张量为40804的张量,但请求的形状为10201 - Input to reshape is a tensor with 40804 values, but the requested shape has 10201 InvalidArgumentError:重塑的输入是一个178802值的张量,但请求的形状有89401 - InvalidArgumentError: Input to reshape is a tensor with 178802 values, but the requested shape has 89401 无效参数:reshape 的输入是一个有 64 个值的张量,但请求的形状有 4 个 - Invalid argument: Input to reshape is a tensor with 64 values, but the requested shape has 4 InvalidArgumentError:reshape 的输入是一个值为 0 的张量,但请求的形状有 54912 - InvalidArgumentError: Input to reshape is a tensor with 0 values, but the requested shape has 54912 rank1 张量的 20 个元素的切片然后重塑抛出“重塑的输入是具有 10272 个值的张量,但请求的形状需要 20 的倍数” - Slice of 20 elements of rank1 tensor then reshaping throws "Input to reshape is tensor with 10272 values, but requested shape requires multiple of 20"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM