python 将Tensorflows预处理函数用于InceptionV3的问题

camsedfj  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(126)
#Here my args, they are pretty much the same for all three functions:
training_preprocessing_args = dict(
    labels='inferred',
    label_mode='int',
    class_names=classes,
    color_mode='rgb',
    image_size=hyper_parameter["image_size"],
    shuffle=True,
    seed=seed,
    validation_split=None,
    subset=None,
    interpolation='bilinear',
    follow_links=False,
    crop_to_aspect_ratio=False
)


    logging.info("Training Data:")
    train_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_TRAINING, **training_preprocessing_args)

    logging.info("Testing Data:")
    test_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_TESTING, **testing_preprocessing_args)

    logging.info("Validation Data:")
    validation_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_VALIDATION, **validation_preprocessing_args)

    logging.info("Preprocessing:")
    train_dataset = tf.keras.applications.inception_v3.preprocess_input(tf.cast(train_dataset, tf.float32))
    validation_dataset = tf.keras.applications.inception_v3.preprocess_input(tf.cast(validation_dataset, tf.float32))
    test_dataset = tf.keras.applications.inception_v3.preprocess_input(tf.cast(test_dataset, tf.float32))

所以这就是我的设置。一开始我没有演员阵容,然后我会得到一个不同的错误-既然文档提到了演员阵容,我就这样讨论它。
文档按如下方式执行(https://www.tensorflow.org/api_docs/python/tf/keras/applications/inception_v3/preprocess_input):

i = tf.keras.layers.Input([None, None, 3], dtype = tf.uint8)
x = tf.cast(i, tf.float32)
x = tf.keras.applications.mobilenet.preprocess_input(x)
core = tf.keras.applications.MobileNet()
x = core(x)
model = tf.keras.Model(inputs=[i], outputs=[x])

image = tf.image.decode_png(tf.io.read_file('file.png'))
result = model(image)

这很难转化成我真实的的应用程序。
我收到以下错误:

15-12-2022 23:21:15 INFO     Training Data:
Found 6988 files belonging to 10 classes.
2022-12-15 23:21:16.075523: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
INFO:tensorflow:Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761FC0>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

15-12-2022 23:21:16 INFO     Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761FC0>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

INFO:tensorflow:Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761FC0>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761FC0>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Testing Data:
Found 1699 files belonging to 10 classes.
INFO:tensorflow:Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D763490>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

15-12-2022 23:21:16 INFO     Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D763490>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

INFO:tensorflow:Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D763490>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D763490>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Validation Data:
Found 1700 files belonging to 10 classes.
INFO:tensorflow:Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761BD0>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

15-12-2022 23:21:16 INFO     Converted call: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761BD0>
    args: (<tf.Tensor 'args_0:0' shape=() dtype=string>,)
    kwargs: {}

INFO:tensorflow:Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761BD0>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Allowlisted: <function paths_and_labels_to_dataset.<locals>.<lambda> at 0x000002063D761BD0>: DoNotConvert rule for keras
15-12-2022 23:21:16 INFO     Preprocessing:
Traceback (most recent call last):
  File "_CORE\main.py", line 27, in <module>
    main()
  File "_CORE\main.py", line 17, in main
    data:tuple = run_preprocessing()
  File "_CORE\preprocessing\run.py", line 10, in run_preprocessing
    data = create_datasets()
  File "_CORE\preprocessing\CreateDataset.py", line 23, in create_datasets
    train_dataset = tf.keras.applications.inception_v3.preprocess_input(train_dataset)#tf.cast(train_dataset, tf.float32))
  File "_ENV\_ENV_1\lib\site-packages\keras\applications\inception_v3.py", line 448, in preprocess_input
    return imagenet_utils.preprocess_input(
  File "_ENV\_ENV_1\lib\site-packages\keras\applications\imagenet_utils.py", line 123, in preprocess_input
    return _preprocess_symbolic_input(x, data_format=data_format, mode=mode)
  File "_ENV\_ENV_1\lib\site-packages\keras\applications\imagenet_utils.py", line 271, in _preprocess_symbolic_input
    x /= 127.5
TypeError: unsupported operand type(s) for /=: 'BatchDataset' and 'float'

现在,除了最后一行之外,这或多或少是无用的。有人知道我做错了什么吗?我需要调整任何东西或类似的东西吗?据我所知,预处理函数会为我做这些。

h7appiyu

h7appiyu1#

看起来文档对x到底是什么有点不清楚。
x必须是一个图像,所以为了在tf.data.dataset对象上应用该函数,我需要使用map函数,而不是仅仅抛出数据集,解决方案如下:

def preprocess(image, label):
    image = tf.keras.applications.inception_v3.preprocess_input(image)
    return image, label

logging.info("Training Data:")
train_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_TRAINING, **training_preprocessing_args)

logging.info("Testing Data:")
test_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_TESTING, **testing_preprocessing_args)

logging.info("Validation Data:")
validation_dataset:tf.data.Dataset =  tf.keras.utils.image_dataset_from_directory(directory=PATH_DATA_VALIDATION, **validation_preprocessing_args)

logging.info("Preprocessing:")        
train_dataset = train_dataset.map(preprocess)
validation_dataset = validation_dataset.map(preprocess)
test_dataset = test_dataset.map(preprocess)

这样我就不会出错了。当然这也可以作为lambda函数来完成,但那可能会更笨拙。
祝你好运!

相关问题