AttributeError: 'KerasTensor' object has no attribute '_keras_shape'
AttributeError: 'KerasTensor' object has no attribute '_keras_shape'
你好,我正在制作一个模型来训练数据集,但在我的 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'
所以请帮我解决这个错误,即使我正在升级它的库,我也无法理解它。
张量流 > 2.0 中不存在属性 _keras_shape
。要么升级你的库使其与 > 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)
in tf.__version__
'2.3.1' 这不起作用并抛出 AttributeError: 'Tensor' object has no attribute '_keras_shape'
解决方法,如果您还没有为 2.0 做好准备
创建虚拟环境:tensor1
virtualenv -p 2.7 tensor1
在 tensor 和 keras 上安装旧版本
pip install tensorflow==1.15
pip install keras==2.2.4
然后希望这个特定错误会消失,但您可能会遇到新的错误。
你好,我正在制作一个模型来训练数据集,但在我的 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'
所以请帮我解决这个错误,即使我正在升级它的库,我也无法理解它。
张量流 > 2.0 中不存在属性 _keras_shape
。要么升级你的库使其与 > 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)
in tf.__version__
'2.3.1' 这不起作用并抛出 AttributeError: 'Tensor' object has no attribute '_keras_shape'
解决方法,如果您还没有为 2.0 做好准备
创建虚拟环境:tensor1
virtualenv -p 2.7 tensor1
在 tensor 和 keras 上安装旧版本
pip install tensorflow==1.15
pip install keras==2.2.4
然后希望这个特定错误会消失,但您可能会遇到新的错误。