【问题标题】:AttributeError: 'KerasTensor' object has no attribute '_keras_shape'AttributeError:“KerasTensor”对象没有属性“_keras_shape”
【发布时间】:2021-07-30 00:24:09
【问题描述】:

嗨,我正在制作一个模型来训练数据集,但是在我的 resnet_model 中,我遇到了属性错误,所以请帮我解决这个错误。代码如下:

from Lib.data_loader import DataLoader
from Lib.resnet_model import Resnet3DBuilder
from Lib.HistoryGraph import HistoryGraph
import Lib.image as img
from Lib.utils import mkdirs
#import tensorflow as tf
import os

from math import ceil

from keras.optimizers import SGD

#from tensorflow.keras.optimizers import SGD

from keras.callbacks import ModelCheckpoint

#from tensorflow.keras.callbacks import ModelCheckpoint

target_size = (64,96)
nb_frames = 16  # here this will get number of pictres from datasets folder
skip = 1 # using resnet we skip different layers
nb_classes = 27
batch_size = 64 
input_shape = (nb_frames,) + target_size + (3,)

workers = 8 
use_multiprocessing = False 
max_queue_size = 20 



resnet_model = Resnet3DBuilder.build_resnet_101(input_shape, nb_classes, drop_rate = 0.5)

optimizer = SGD(lr=0.01, momentum=0.9, decay=0.0001, nesterov=False)

resnet_model.compile(optimizer = optimizer, loss= "categorical_crossentropy" , metrics=["accuracy"])

model_file = os.path.join(path_model, 'resnetmodel.hdf5')

model_checkpointer = ModelCheckpoint(model_file, monitor='val_acc',verbose=1, save_best_only=True, mode='max')

history_graph = HistoryGraph(model_path_name = os.path.join(path_model, "graphs"))

nb_sample_train = data.train_df["video_id"].size
nb_sample_val = data.val_df["video_id"].size

所有上面的代码都工作正常。 错误就在这里:

AttributeError                            Traceback (most recent call last)
<ipython-input-11-be30533fbfba> in <module>
----> 1 resnet_model = Resnet3DBuilder.build_resnet_101(input_shape, nb_classes, drop_rate = 0.5)
      2 
      3 optimizer = SGD(lr=0.01, momentum=0.9, decay=0.0001, nesterov=False)
      4 
      5 resnet_model.compile(optimizer = optimizer, loss= "categorical_crossentropy" , metrics=["accuracy"])

D:\HandGesturesProject\Lib\resnet_model.py in build_resnet_101(input_shape, num_outputs, reg_factor, drop_rate)
    258     def build_resnet_101(input_shape, num_outputs, reg_factor=1e-4, drop_rate=0):
    259         """Build resnet 101."""
--> 260         return Resnet3DBuilder.build(input_shape, num_outputs, bottleneck,
    261                                      [3, 4, 23, 3], reg_factor=reg_factor, drop_rate=drop_rate)

D:\HandGesturesProject\Lib\resnet_model.py in build(input_shape, num_outputs, block_fn, repetitions, reg_factor, drop_rate)
    223         filters = 64
    224         for i, r in enumerate(repetitions):
--> 225             block = _residual_block3d(block_fn, filters=filters,
    226                                       kernel_regularizer=l2(reg_factor),
    227                                       repetitions=r, is_first_layer=(i == 0)

D:\HandGesturesProject\Lib\resnet_model.py in f(input)
    105             if i == 0 and not is_first_layer:
    106                 strides = (2, 2, 2)
--> 107             input = block_function(filters=filters, strides=strides,
    108                                    kernel_regularizer=kernel_regularizer,
    109                                    is_first_block_of_first_layer=(

D:\HandGesturesProject\Lib\resnet_model.py in f(input)
    164                                    )(conv_3_3)
    165 
--> 166         return _shortcut3d(input, residual)
    167 
    168     return f

D:\HandGesturesProject\Lib\resnet_model.py in _shortcut3d(input, residual)
     76 
     77 def _shortcut3d(input, residual):
---> 78     stride_dim1 = input._keras_shape[DIM1_AXIS] \
     79         // residual._keras_shape[DIM1_AXIS]
     80     stride_dim2 = input._keras_shape[DIM2_AXIS] \

AttributeError: 'KerasTensor' 对象没有属性 '_keras_shape'

所以请帮我解决这个错误,即使我正在升级它的库,我也无法理解它。

【问题讨论】:

  • 我可以知道更多关于这个from Lib,这是你的本地包吗?因为我觉得你是在tensorflow > 2 并且lib是建立在之前的版本之上的。
  • 是的 lib 是我的本地包文件夹
  • 我正在使用虚拟环境
  • 谢谢所有帮助我的人!实际上我的错误是因为在我使用 Keras 2.4.x 和 tensorflow 2.4.2 之前的版本差异,然后我卸载这些版本并使用 Keras == 2.3.1 和 tensorflow==2.2.0 并且我的问题解决了。

标签: python tensorflow image-processing keras resnet


【解决方案1】:

属性_keras_shape 在张量流> 2.0 中不存在。要么升级你的 lib 使其与 > 2.0 兼容,要么设置一个虚拟环境来支持它。

用来测试的小程序

import tensorflow as tf
from keras.layers import Input

s = Input(shape=[1], dtype=tf.float32, name='1')
print("shape of s is: ",s._keras_shape) # (None, 1)

tf.__version__ '2.3.1' 这不起作用并抛出AttributeError: 'Tensor' object has no attribute '_keras_shape'

解决方法,如果您还没有为 2.0 做好准备

创建一个虚拟环境:tensor1

virtualenv -p 2.7 tensor1

在张量和 keras 上安装旧版本

pip install tensorflow==1.15
pip install keras==2.2.4 

然后希望这个特定错误会消失,但您可能会遇到新的错误。

【讨论】:

  • 当我安装 tensorflow 1.15 时出现此错误:错误:找不到满足要求 tensorflow==1.15 的版本(来自版本:2.2.0rc1、2.2.0rc2、2.2.0rc3、2.2 .0rc4、2.2.0、2.2.1、2.2.2、2.3.0rc0、2.3.0rc1、2.3.0rc2、2.3.0、2.3.1、2.3.2、2.4.0rc0、2.4.0rc1、2.4.0rc2 , 2.4.0rc3, 2.4.0rc4, 2.4.0, 2.4.1, 2.5.0rc0, 2.5.0rc1, 2.5.0rc2, 2.5.0rc3) 错误:没有找到 tensorflow==1.15 的匹配分布
  • 谢谢所有帮助我的人!实际上我的错误是因为在我使用 Keras 2.4.x 和 tensorflow 2.4.2 之前的版本差异,然后我卸载这些版本并使用 Keras == 2.3.1 和 tensorflow==2.2.0 并且我的问题解决了。
猜你喜欢
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多