我正在嘗試使用我自己的資料集重新創建這項作業:https ://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays/notebook
我對代碼做了一些細微的調整以適應我的資料,但我認為這不是導致問題的原因;當然可以。
我的代碼:
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))
和錯誤:
---------------------------------------------------------------------------
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]
我可以從錯誤中收集到我在調整大小時不匹配,但我相信它指向了錯誤的位置,這與線有關resizedImage = tf.image.resize(img, IMAGE_SIZE)
我已經瀏覽了檔案和 SO,但我找不到任何可能對我有幫助的東西。我已經盡我所能通過在螢屏上列印出來進行除錯。我也嘗試過更改,IMAGE_SIZE
但這沒有任何區別。
在影像上,它們在磁盤上的大小不同,格式為 JPG。我希望這無關緊要,因為我們可以使用此步驟來調整它們的大小,以便稍后由模型處理。
其他有用的資訊是我正在開發 Google Collab 的專業版,并且檔案存盤在 Google 驅動器位置。同樣,我不認為這是任何問題或原因。我試圖先發制人地寫一些人們可能會問的東西。
最后,Amy 在 Kaggle 上的代碼中有一個prepare_for_training
函式,它在我在上面代碼中提供的最后一行之前被呼叫。我可以在不呼叫該函式的情況下觸發相同的錯誤,因此我故意省略了它以幫助保持示例代碼更清晰。如果您想查看它,這里有一個筆記本中的快速鏈接:https ://www.kaggle.com/code/amyjang/tensorflow-pneumonia-classification-on-x-rays?scriptVersionId=39162263&cellId=26
uj5u.com熱心網友回復:
可能是get_label(path)
方法的問題,由于您是根據 sep 拆分路徑,它會回傳一個包含許多元素的串列,請確保您只選擇一個元素進行測驗,試試這個:
tf.strings.split(path, os.path.sep)[-1] == "inactive"
我假設最后一個元素是標簽,您只需將此索引替換為此串列中標簽的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/508272.html