Как сделать так, чтобы тензор имел четыре измерения? - PullRequest
1 голос
/ 06 марта 2020

Следующий код:

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [200, 200])


def process_path(file_path):
  #label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img

model.predict(process_path('data/train/nonfood/0_808.jpg'))

Дайте следующую ошибку

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (200, 200, 3)

Я ожидаю, что мне нужно отформатировать изображение размером

(1,200,200,3)

Но что правильный синтаксис для форматирования?

1 Ответ

1 голос
/ 06 марта 2020

Вам необходимо смоделировать индекс batch_size, поскольку в Keras и TensorFlow вы можете делать прогнозы только для партий.

Вы можете использовать np.expand_dims(photo,axis=0) или tf.expand_dims(photo, axis=0)

Переведено для вашего случая это означает, что в вашем decode_img, return tf.expand_dims(img,0)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...