简体   繁体   中英

AttributeError: 'KerasTensor' object has no attribute '_keras_shape'

hi i am making a model for training a datasets but at my resnet_model I am stuck at Attribute error so please help me to solving this error. do the code are here below:

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

All upper code is working fine. and the error is here:

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' object has no attribute '_keras_shape'

so please help me to solving this errors i am unable to understand it even i am upgrade its libries.

attribute _keras_shape is not present in tensor flow > 2.0. either upgrade your lib so it's compatible with > 2.0 or set up a virtual env to support it.

small program to test with

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' this does not work and throws AttributeError: 'Tensor' object has no attribute '_keras_shape'

the workaround, if you still not ready for 2.0

create a virtual env: tensor1

virtualenv -p 2.7 tensor1

install older version on tensor and keras

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

and then hopefully this particular error will go away, but you might land up with new errros.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM