简体   繁体   English

Keras InceptionV3模型。预测

[英]Keras InceptionV3 model.predict

Code: 码:

  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)

Error: 错误:

  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'

I can use model.predict on actual images, but end up with this error on tf.placeholders or tf.variables Can anyone help me debug this error? 我可以在实际图像上使用model.predict,但最终在tf.placeholders或tf.variables上出现此错误,有人可以帮助我调试此错误吗?

Keras' Model.predict expects an numpy array for input data. Model.predict需要一个用于输入数据的numpy数组。 You may also want to include a batch_size value, unless your batch size is 32. From the documentation: 您可能还希望包括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,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM