【发布时间】: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