【问题标题】:Keras InceptionV3 model.predictKeras InceptionV3 model.predict
【发布时间】:2018-02-05 18:57:30
【问题描述】:

代码:

  from keras.applications import InceptionV3
  model = InceptionV3(weights="imagenet")
  shape = (None,image_size,image_size,num_channels)
  x = tf.placeholder(tf.float32, shape=shape)      
  adv_x,grad_x = fgm(x, model, model.predict(x), y=y, targeted=True, eps=0, clip_min=-0.5, clip_max=0.5)
  adv_,grad_ = batch_eval(sess, [x,y], [adv_x,grad_x], [inputs,targets], args={'batch_size': args['batch_size']})

  model.predict(x)

错误:

  File "/u/.../env/lib/python3.5/site-packages/keras/engine/training.py", line 1594, in predict
    batch_size=batch_size, verbose=verbose)
  File "/u/.../env/lib/python3.5/site-packages/keras/engine/training.py", line 1208, in _predict_loop
    batches = _make_batches(samples, batch_size)
  File "/u/.../env/lib/python3.5/site-packages/keras/engine/training.py", line 364, in _make_batches
    num_batches = int(np.ceil(size / float(batch_size)))
TypeError: unsupported operand type(s) for /: 'Dimension' and 'float'

我可以在实际图像上使用 model.predict,但最终在 tf.placeholders 或 tf.variables 上出现此错误 谁能帮我调试一下这个错误?

【问题讨论】:

    标签: python python-3.x tensorflow keras pre-trained-model


    【解决方案1】:

    Keras 的 Model.predict 需要一个 numpy 数组作为输入数据。您可能还需要包含 batch_size 值,除非您的批量大小为 32。从文档中:

    predict(self, x, batch_size=None, verbose=0, steps=None) method of keras.engine.training.Model instance
        Generates output predictions for the input samples.
    
        Computation is done in batches.
    
        # Arguments
            x: The input data, as a Numpy array
                (or list of Numpy arrays if the model has multiple outputs).
            batch_size: Integer. If unspecified, it will default to 32.
            verbose: Verbosity mode, 0 or 1.
            steps: Total number of steps (batches of samples)
                before declaring the prediction round finished.
                Ignored with the default value of `None`.
    
        # Returns
            Numpy array(s) of predictions.
    
        # Raises
            ValueError: In case of mismatch between the provided
                input data and the model's expectations,
    

    【讨论】:

    • 我正在使用 Cleverhans 库,其中符号攻击是在 tensorflow 中构建的。编辑了上面的问题。我应该能够做到这一点,因为我构建了自己的 MNIST 和 CIFAR 顺序模型,并且该代码能够执行。为什么预训练模型会有所不同?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多