报错:AttributeError: NoneType object has no attribute device

今天搞个测试,测试是在horovod下进行的。

问题就出在加载权重(参数)文件的地方,加载权重命令load_weights前要先build一下,结果就build出这么一个错误:

Exception ignored in: <bound method _RandomSeedGeneratorDeleter.__del__ of <tensorflow.python.data.ops.dataset_ops._RandomSeedGeneratorDeleter object at 0x7f363100d4e0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3462, in __del__
AttributeError: 'NoneType' object has no attribute 'device'

这个报错真的是谜之报错,我根本没搞懂错哪了。但反正是注释掉build这句就不会报错。

于是我脱离horovod,在单纯的tensorflow2.1.0中重写了一下,发现没有问题,可以正常运行。

但是我一开始运行了一下发现报错是这样的:

Traceback (most recent call last):
  File "error.py", line 30, in <module>
    mnist_model.build(input_shape = (None, 28 ,28))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/sequential.py", line 260, in build
    super(Sequential, self).build(input_shape)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py", line 682, in build
    self.call(x, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/sequential.py", line 281, in call
    outputs = layer(inputs, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer.py", line 737, in __call__
    self.name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/input_spec.py", line 177, in assert_input_compatibility
    str(x.shape.as_list()))
ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 28, 28]
Exception ignored in: <bound method _RandomSeedGeneratorDeleter.__del__ of <tensorflow.python.data.ops.dataset_ops._RandomSeedGeneratorDeleter object at 0x7f7c76f37320>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3462, in __del__
AttributeError: 'NoneType' object has no attribute 'device'

可以看到是model.build那句的问题,最后同样是AttributeError: 'NoneType' object has no attribute 'device'。

但是这次的错误提示比较多,里面有个有用信息就是倒数第5行的提示:

ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 784]

原来是build输入尺度错了,修改正确后发现脱离horovod运行是不会报错的。

 

最后我无意间发现了这样的Warning提示:

[1,0]<stderr>:2020-06-30 08:34:47.818137: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 376320000 exceeds 10% of system memory.
[1,0]<stderr>:2020-06-30 08:34:49.030225: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 188160000 exceeds 10% of system memory.

这个提示我就很熟悉了,这是提示memory占用率过高啊。但是我的程序是一个mnist测试程序而已,又是放在相当不错的服务器上运行的,而且服务器我是一个人独占并没有其他任务占用memory,怎么会有这个提示那?同时,https://github.com/tensorflow/tensorflow/issues/35326里面也有人遇见这个错误并提出了一样的怀疑。

为了验证是不是这个问题我观察mem使用率在程序运行的全过程都没看见有超过10%。但是我还是把程序中关于数据处理部分的程序注释掉,发现果然能正常运行没有报错了。所以这个报错究竟是不是memory问题还有待考证,初步判断是和memory有可能有一定关系的。

下面把程序放上来详细说明一下:

import tensorflow as tf
import horovod.tensorflow.keras as hvd
import os
import datetime
import package

time_start = datetime.datetime.now()

# 初始化
Log,arg = package.initial()

# 指定GPU信息和operator
gpus, opt = package.gpu_setting('keras+tensorflow2.0', Log)


(mnist_images, mnist_labels), _ = \
    tf.keras.datasets.mnist.load_data(path='mnist-%d.npz' % hvd.rank())

dataset = tf.data.Dataset.from_tensor_slices(
    (tf.cast(mnist_images[..., tf.newaxis] / 255.0, tf.float32),
             tf.cast(mnist_labels, tf.int64))
)
# dataset = dataset.repeat().shuffle(10000).batch(128)
dataset = dataset.repeat().batch(128)
mnist_model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, [3, 3], activation='relu'),
    tf.keras.layers.Conv2D(64, [3, 3], activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    tf.keras.layers.Dropout(0.25),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(10, activation='softmax')
])

mnist_model.compile(loss=tf.losses.SparseCategoricalCrossentropy(),
                    optimizer=opt,
                    metrics=['accuracy'],
                    experimental_run_tf_function=False)
# weight_file = os.path.join(arg.ckp_path,'checkpoint-break-step-64.h5')
if hvd.rank()==0:
    mnist_model.build(input_shape=(None, 28, 28, 1))
    mnist_model.load_weights('checkpoint-1.h5')

第23行被注释掉的“# dataset = dataset.repeat().shuffle(10000).batch(128)”是原来的程序。

下面第24行“dataset = dataset.repeat().batch(128)”是我修改后的程序。

实验发现,23行换成24行(删除shuffle)就不会报错了。

难道真是内存的事???

只能说有一定关联吧,但应该也不是memory不足引起的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常出现在使用对象的某个属性时,该对象是None类型,而None类型没有该属性。在你提供的引用中,有关于此错误的博文和解决方法。通过这些引用,可以了解到这个错误可能是由于加载权重文件之前没有进行build操作导致的,或者可能是由于输入的形状与期望的形状不匹配导致的。具体而言,对于“AttributeError: 'NoneType' object has no attribute 'images'”错误,它表示你正在尝试访问一个名为'images'的属性,但该属性所属的对象是None。要解决这个错误,你需要确保在访问该属性之前,对象不是None,并且具有'images'属性。你可以检查对象在加载权重文件之前是否成功构建,并确保输入的形状与期望的形状相匹配。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [[报错]深析AttributeError: ‘NoneTypeobject has no attribute ‘xxx‘(持更)](https://blog.csdn.net/panbaoran913/article/details/124650015)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [报错AttributeError: NoneType object has no attribute device](https://blog.csdn.net/o0haidee0o/article/details/106903488)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值