【问题标题】:Error when using tf.keras.applications.resnet50.preprocess_input inside tf.data.Dataset.map在 tf.data.Dataset.map 中使用 tf.keras.applications.resnet50.preprocess_input 时出错
【发布时间】:2020-04-08 09:34:48
【问题描述】:

我在使用 tensorflow.compat.v1.keras.applications.resnet50

的函数 resnet50.preprocess_input() 时遇到问题

特别是,经过多次试验和错误,我可以说问题出在数据集生成器函数内部,有一个调用:

dataset.map(pre_processing_image)

在哪里

def pre_processing_image(image):
    image = resnet50.preprocess_input(image)
    return image

并且数据集被分批拆分。当我到达最后一批时,无论它是完整的还是更小的,我都会收到类似于

的错误

Tensor("Const:0", shape=(3,), dtype=float32) 必须与 Tensor("BatchDatasetV2:0", shape=(), dtype=variant) 来自同一图表

我真的不明白发生了什么,因为

  • 如果我使用另一个 preprocess_input,例如 mobilenet 的那个,而不更改任何其他内容,则没有问题。通过挖掘代码,我发现这些函数都在调用this one,但是mobilenet使用“mode='tf'”,而resnet应该使用'caffe'
  • 该错误与最后一批比其他批次更小这一事实无关,我试图使它们全部相等,但错误在第一个训练时期的最后一步不断发生
  • 如果我不使用 ma​​p 而是直接在 tf.data.Dataset.from_generator 内部调用 pre_processing_image 没有问题。 . 只有代码变慢了很多

给你完整的代码:

def image_gen(ds_path, ds_scores=None):
    for i, path in enumerate(ds_path):
        img = im.load_img(path,
                          color_mode='rgb',
                          target_size=(NETWORK_INFO.value[1],NETWORK_INFO.value[1]),
                          interpolation='bilinear')

        img_to_numpy = np.array(img)

        if (ds_scores is not None):
            yield img_to_numpy, ds_scores[i]
        else:
            yield img_to_numpy

def pre_processing_image(image, score=None):
    image = resnet50.preprocess_input(image)

    if score is None:
        return image
    else:
        return image, score

def generator(batchsize, train=False, val=False, test=False, shuffle=False):
    with tf.Session() as sess:    
        if (train):
            dataset = tf.data.Dataset.from_generator(lambda: image_gen(train_paths, train_scores),
                                                      output_types=(tf.float32, tf.float32))
        elif(val):
            dataset = tf.data.Dataset.from_generator(lambda: image_gen(val_paths, val_scores),
                                                      output_types=(tf.float32, tf.float32))
            else:
                dataset = tf.data.Dataset.from_generator(lambda: image_gen(test_paths),
                                                          output_types=(tf.float32))          

        if (shuffle):
            dataset = dataset.shuffle(buffer_size=10*batchsize)            

        dataset = dataset.batch(batchsize)        

        dataset = dataset.map(pre_processing_image,
                                  num_parallel_calls=tf.data.experimental.AUTOTUNE)

        dataset = dataset.prefetch(buffer_size=2)

        dataset = dataset.repeat(count = -1)        

        iterable = tf.data.make_initializable_iterator(dataset)
        batch = iterable.get_next()
        sess.run(iterable.initializer)

        # yield all the time it is required
        while True:
            try:
                yield sess.run(batch)
            except tf.errors.OutOfRangeError:
                pass

我试图弄乱 map 函数的位置和 shuffle/prefatch 参数,但没有解决问题。最后,如您所见,我对训练和验证生成器使用了相同的函数,我只是将输入参数更改为选择函数应该使用的数据集

【问题讨论】:

    标签: tensorflow keras tf.keras


    【解决方案1】:

    解决了这个问题。

    我尝试搜索类似但关于共享相同图像预处理的其他网络(例如 VGG16)的内容,结果发现这些相关问题是 keras 错误

    我将模块 keras-applications 更新到最后一次提交(提交,而不是发布!),代码现在可以正常工作

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 2021-06-02
      • 2020-12-28
      • 2019-08-24
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      相关资源
      最近更新 更多