问题
Here is part of my code TFF
for item in data_root.glob("*"):
print(item.name)
CLASS_NAMES = np.array([item.name for item in data_root.glob('*')])
def get_label(file_path):
parts = tf.strings.split(file_path, os.path.sep)
return parts[-2] == CLASS_NAMES
IMG_WIDTH = 200
IMG_HEIGHT = 200
def decode_img(img):
img = tf.io.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
return tf.image.resize(img, [IMG_HEIGHT, IMG_WIDTH])
def process_path(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = decode_img(img)
return img
...
def create_compiled_keras_model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32,(3,3),input_shape=(200,200,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
model.add(tf.keras.layers.Conv2D(32,(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units = 128,activation='relu'))
model.add(tf.keras.layers.Dense(units = 2,activation='sigmoid'))
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),loss= tf.keras.losses.CategoricalCrossentropy(),metrics=[tf.keras.metrics.Accuracy()])
return model
def model_fn():
keras_model = create_compiled_keras_model()
return tff.learning.from_compiled_keras_model(keras_model, sample_batch)
....
state, metrics = iterative_process.next(state, federated_train_data)
print('round 1, metrics={}'.format(metrics))
When I execute the last part
state, metrics = iterative_process.next(state, federated_train_data)
print('round 1, metrics={}'.format(metrics))
I find this error
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1364 try:
-> 1365 return fn(*args)
1366 except errors.OpError as e:
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1349 return self._call_tf_sessionrun(options, feed_dict, fetch_list,
-> 1350 target_list, run_metadata)
1351
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1442 fetch_list, target_list,
-> 1443 run_metadata)
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
[[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset/_44]]
(1) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
[[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-62-fe6aa5cb4588> in <module>
----> 1 state, metrics = iterative_process.next(state, federated_train_data)
2 print('round 1, metrics={}'.format(metrics))
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/utils/function_utils.py in __call__(self, *args, **kwargs)
678 context = self._context_stack.current
679 arg = pack_args(self._type_signature.parameter, args, kwargs, context)
--> 680 return context.invoke(self, arg)
681
682
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in invoke(self, fn, arg)
665 else:
666 computed_arg = None
--> 667 result = computed_comp.value(computed_arg)
668 py_typecheck.check_type(result, ComputedValue)
669 type_utils.check_assignable_from(comp.type_signature.result,
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in <lambda>(x)
810 return ComputationContext(context, {comp.parameter_name: arg})
811
--> 812 return ComputedValue(lambda x: self._compute(comp.result, _wrap(x)),
813 comp.type_signature)
814
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in _compute(self, comp, context)
723 return self._compute_lambda(comp, context)
724 elif isinstance(comp, building_blocks.Block):
--> 725 return self._compute_block(comp, context)
726 elif isinstance(comp, building_blocks.Intrinsic):
727 return self._compute_intrinsic(comp, context)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in _compute_block(self, comp, context)
822 py_typecheck.check_type(context, ComputationContext)
823 for local_name, local_comp in comp.locals:
--> 824 local_val = self._compute(local_comp, context)
825 context = ComputationContext(context, {local_name: local_val})
826 return self._compute(comp.result, context)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in _compute(self, comp, context)
713 return self._compute_compiled(comp, context)
714 elif isinstance(comp, building_blocks.Call):
--> 715 return self._compute_call(comp, context)
716 elif isinstance(comp, building_blocks.Tuple):
717 return self._compute_tuple(comp, context)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in _compute_call(self, comp, context)
759 else:
760 computed_arg = None
--> 761 result = computed_fn.value(computed_arg)
762 py_typecheck.check_type(result, ComputedValue)
763 type_utils.check_assignable_from(computed_fn.type_signature.result,
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in <lambda>(x)
838 arg_type = comp.type_signature.parameter
839 return ComputedValue(
--> 840 lambda x: my_method(fit_argument(x, arg_type, context)),
841 comp.type_signature)
842 else:
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in _federated_map(self, arg)
882 fn = arg.value[0]
883 result_val = [
--> 884 fn(ComputedValue(x, mapping_type.parameter)).value for x in arg.value[1]
885 ]
886 result_type = computation_types.FederatedType(mapping_type.result,
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in <listcomp>(.0)
882 fn = arg.value[0]
883 result_val = [
--> 884 fn(ComputedValue(x, mapping_type.parameter)).value for x in arg.value[1]
885 ]
886 result_type = computation_types.FederatedType(mapping_type.result,
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in <lambda>(x)
743 'but found \'{}\' instead.'.format(computation_oneof))
744 else:
--> 745 return ComputedValue(lambda x: run_tensorflow(comp, x),
746 comp.type_signature)
747
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/reference_executor.py in run_tensorflow(comp, arg)
341 if init_op:
342 sess.run(init_op)
--> 343 result_val = tensorflow_utils.fetch_value_in_session(sess, result)
344 return capture_computed_value_from_graph(result_val,
345 comp.type_signature.result)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_federated/python/core/impl/utils/tensorflow_utils.py in fetch_value_in_session(sess, value)
1069 else:
1070 raise ValueError('Unsupported value type {}.'.format(v))
-> 1071 flat_computed_tensors = sess.run(flat_tensors)
1072 flattened_results = _interleave_dataset_results_and_tensors(
1073 dataset_results, flat_computed_tensors)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
954 try:
955 result = self._run(None, fetches, feed_dict, options_ptr,
--> 956 run_metadata_ptr)
957 if run_metadata:
958 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1178 if final_fetches or final_targets or (handle and feed_dict_tensor):
1179 results = self._do_run(handle, final_targets, final_fetches,
-> 1180 feed_dict_tensor, options, run_metadata)
1181 else:
1182 results = []
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1357 if handle is None:
1358 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1359 run_metadata)
1360 else:
1361 return self._do_call(_prun_fn, handle, feeds, fetches)
~/anaconda3/envs/env1/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1382 '\nsession_config.graph_options.rewrite_options.'
1383 'disable_meta_optimizer = True')
-> 1384 raise type(e)(node_def, op, message)
1385
1386 def _extend_graph(self):
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
[[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset/_44]]
(1) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
[[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]]
[[subcomputation/StatefulPartitionedCall_1/ReduceDataset]]
0 successful operations.
0 derived errors ignored.
Please can anyone have idea why this error appear?
InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU [[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]] [[subcomputation/StatefulPartitionedCall_1/ReduceDataset]] [[subcomputation/StatefulPartitionedCall_1/ReduceDataset/_44]] (1) Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU [[{{node StatefulPartitionedCall/StatefulPartitionedCall/sequential/max_pooling2d/MaxPool}}]] [[subcomputation/StatefulPartitionedCall_1/ReduceDataset]] 0 successful operations. 0 derived errors ignored.
来源:https://stackoverflow.com/questions/60060493/tff-invalid-argument-default-maxpoolingop-only-supports-nhwc-on-device-type-cp