使用Tensorflow在TPU上进行训练

问题描述

当我执行

data = tfds.load("cycle_gan/monet2photo",try_gcs=True)
for i in data['trainA']:
    print(type(i))

当我不使用tpu尝试此操作时,我得到的是jpg格式的图像,但是使用tpu时,我正在使用tfrecords,并最终出现以下错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/iterator_ops.py in _next_internal(self)
    761         # Fast path for the case `self._structure` is not a nested structure.
--> 762         return self._element_spec._from_compatible_tensor_list(ret)  # pylint: disable=protected-access
    763       except AttributeError:

AttributeError: 'dict' object has no attribute '_from_compatible_tensor_list'

During handling of the above exception,another exception occurred:

UnimplementedError                        Traceback (most recent call last)
14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/context.py in execution_mode(mode)
   2101       ctx.executor = executor_new
-> 2102       yield
   2103     finally:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/iterator_ops.py in _next_internal(self)
    763       except AttributeError:
--> 764         return structure.from_compatible_tensor_list(self._element_spec,ret)
    765 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/structure.py in from_compatible_tensor_list(element_spec,tensor_list)
    229       lambda spec,value: spec._from_compatible_tensor_list(value),--> 230       element_spec,tensor_list)
    231 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/structure.py in _from_tensor_list_helper(decode_fn,element_spec,tensor_list)
    204     value = tensor_list[i:i + num_flat_values]
--> 205     flat_ret.append(decode_fn(component_spec,value))
    206     i += num_flat_values

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/util/structure.py in <lambda>(spec,value)
    228   return _from_tensor_list_helper(
--> 229       lambda spec,230       element_spec,tensor_list)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_spec.py in _from_compatible_tensor_list(self,tensor_list)
    176     assert len(tensor_list) == 1
--> 177     tensor_list[0].set_shape(self._shape)
    178     return tensor_list[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in set_shape(self,shape)
   1205   def set_shape(self,shape):
-> 1206     if not self.shape.is_compatible_with(shape):
   1207       raise ValueError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in shape(self)
   1166         # `EagerTensor`,in C.
-> 1167         self._tensor_shape = tensor_shape.TensorShape(self._shape_tuple())
   1168       except core._NotOkStatusException as e:

UnimplementedError: File system scheme '[local]' not implemented (file: '/root/tensorflow_datasets/cycle_gan/monet2photo/2.0.0/cycle_gan-trainA.tfrecord-00000-of-00001')

During handling of the above exception,another exception occurred:

UnimplementedError                        Traceback (most recent call last)
<ipython-input-13-71aa8855ccd2> in <module>()
----> 1 for i in data['trainA']:
      2     example = tf.train.Example()
      3     example.ParseFromString(i)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/iterator_ops.py in __next__(self)
    734 
    735   def __next__(self):  # For Python 3 compatibility
--> 736     return self.next()
    737 
    738   def _next_internal(self):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/iterator_ops.py in next(self)
    770   def next(self):
    771     try:
--> 772       return self._next_internal()
    773     except errors.OutOfRangeError:
    774       raise stopiteration

/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/iterator_ops.py in _next_internal(self)
    762         return self._element_spec._from_compatible_tensor_list(ret)  # pylint: disable=protected-access
    763       except AttributeError:
--> 764         return structure.from_compatible_tensor_list(self._element_spec,ret)
    765 
    766   @property

/usr/lib/python3.6/contextlib.py in __exit__(self,type,value,traceback)
     97                 value = type()
     98             try:
---> 99                 self.gen.throw(type,traceback)
    100             except stopiteration as exc:
    101                 # Suppress stopiteration *unless* it's the same exception that

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/context.py in execution_mode(mode)
   2103     finally:
   2104       ctx.executor = executor_old
-> 2105       executor_new.wait()
   2106 
   2107 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/executor.py in wait(self)
     65   def wait(self):
     66     """Waits for ops dispatched in this executor to finish."""
---> 67     pywrap_tfe.TFE_ExecutorWaitForAllPendingNodes(self._handle)
     68 
     69   def clear_error(self):

UnimplementedError: File system scheme '[local]' not implemented (file: '/root/tensorflow_datasets/cycle_gan/monet2photo/2.0.0/cycle_gan-trainA.tfrecord-00000-of-00001')

数据是

{'testA': <DatasetV1Adapter shapes: {image: (None,None,3),label: ()},types: {image: tf.uint8,label: tf.int64}>,'testB': <DatasetV1Adapter shapes: {image: (None,'trainA': <DatasetV1Adapter shapes: {image: (None,'trainB': <DatasetV1Adapter shapes: {image: (None,label: tf.int64}>}

与下载相关的文件

os.listdir('/root/tensorflow_datasets/cycle_gan/monet2photo/2.0.0/')
['dataset_info.json','cycle_gan-testB.tfrecord-00000-of-00001','label.labels.txt','cycle_gan-trainB.tfrecord-00000-of-00002','image.image.json','cycle_gan-trainA.tfrecord-00000-of-00001','cycle_gan-trainB.tfrecord-00001-of-00002','cycle_gan-testA.tfrecord-00000-of-00001']

我想使用data在数据集上训练cyclegan,所以 请帮助我如何预处理此数据集以获取张量图像以继续?

tensorflow 2.3.0, python 3.6.9

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)