TypeError: ожидаемый tenorflow.python.framework.tensor_spec.TensorSpec, найдено numpy.ndarray

Я получаю следующую ошибку, когда хочу перейти с TFF 0.12.0 на TFF 0.18.0, зная, что у меня есть набор данных изображения, вот мой sample_batch

images, labels = next(img_gen.flow_from_directory(path0,target_size=(224, 224), batch_size=2))
sample_batch = (images,labels)
...

def model_fn():

  keras_model = create_keras_model()
  return tff.learning.from_keras_model(
      keras_model,
      input_spec=sample_batch,
      loss=tf.keras.losses.SparseCategoricalCrossentropy(),
      metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

Итак, как я могу изменить свой sample_batch, чтобы он соответствовал этой версии? пожалуйста помоги !! Благодарность


person seni    schedule 15.02.2021    source источник


Ответы (1)


В версии 0.13.0 параметр sample_batch устарел. Параметр input_spec должен быть tff.Type или _ 5_ согласно документация.

Чтобы построить структуру tf.TensorSpec из numpy.ndarray:

def tensor_spec_from_ndarray(a):
  return tf.TensorSpec(dtype=tf.dtypes.as_dtype(a.dtype),
                       shape=a.shape)

sample_batch = (images,labels)  # assumes images and labels are np.ndarray
input_spec = tf.nest.map_structure(
  tensor_spec_from_ndarray, sample_batch)
person Zachary Garrett    schedule 15.02.2021
comment
Большое тебе спасибо - person seni; 19.02.2021