如果我理解正確,而不是像這樣將完整的資料集加載到記憶體中:
images = []
file_list = glob.glob('path/to/images/*.jpg')
for file in file_list:
images.append(img_to_array(load_img(file, target_size=input_shape)))
images = np.stack(images, axis=0)
images = preprocess(images)
# classify the image
print("[INFO] classifying image with '{}'...".format(used_model))
predictions = model.predict(images)
decoded_predictions = imagenet_utils.decode_predictions(predictions)
應該使用 tensorflows 資料實用程式來獲得更好的記憶體管理和性能:
images = tf.keras.utils.image_dataset_from_directory(file_path, image_size=input_shape, labels=None)
AUTOTUNE = tf.data.AUTOTUNE
images = images.prefetch(buffer_size=AUTOTUNE)
# this line will now crash
images = preprocess(images)
# classify the image
print("[INFO] classifying image with '{}'...".format(used_model))
predictions = model.predict(images)
decoded_predictions = imagenet_utils.decode_predictions(predictions)
正如上面代碼中所寫的,我知道有不同的資料結構,它們不適用于相同的代碼。我的問題是:如何對我的資料進行預處理?所有相應的教程似乎都在處理訓練,而我想做簡單的推理。
附加問題:如果資料來自 S3 存盤桶(腳本在 Airflow-DAG 中運行),這將如何完成?
uj5u.com熱心網友回復:
您可以使用tf.data.Dataset.map對影像或影像批次應用預處理。這是一個例子:
import tensorflow as tf
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
seed=123,
image_size=(180, 180),
batch_size=batch_size)
scale_layer = tf.keras.layers.Rescaling(1./255)
def preprocess(images, labels):
images = tf.image.resize(scale_layer(images),[120, 120], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return images, labels
train_ds = train_ds.map(preprocess)
在您的情況下,您只有影像,因此您可以忽略此處的標簽。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432291.html
上一篇:Keras有狀態LSTM錯誤:從形狀為[32,1]的張量中指定了一個形狀為[4,1]的串列
下一篇:TensorFlow詞嵌入模型 LDA傳遞給LatentDirichletAllocation.fit的資料中的負值
