[英]ResNet50 : TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor
Trying to implement resnet 50 over poly-u dataset.尝试在 poly-u 数据集上实现 resnet 50。 what i am trying to do over here is that i want to make a model which classifies the palm print of individuals into seperate identities, i found that resnet is working best for this kind of problems cant seems to get over this error tried many solutions online but cant resolve for some reason:
我在这里要做的是我想制作一个 model 将个人的掌纹分类为不同的身份,我发现 resnet 最适合这类问题 似乎无法克服这个错误 在网上尝试了很多解决方案但由于某种原因无法解决:
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(None, 2), dtype=int64), values=Tensor("DeserializeSparse:1", shape=(None,), dtype=float32), dense_shape=Tensor("stack:0", shape=(2,), dtype=int64)). Consider casting elements to a supported type.
identity block身份块
import keras
def identity_block(X,f,filters):
# retrieve filters
F1,F2,F3 = filters
X_shortcut = X
# first layer
X = Conv2D(filters = F1, kernel_size = (1,1),strides= (1,1),padding ='valid') (X)
X = BatchNormalization(axis = 3 )(X)
X = Activation('relu')(X)
# second layer
X = Conv2D(filters = F2, kernel_size = (f,f),strides= (1,1),padding ='same') (X)
X = BatchNormalization(axis = 3 )(X)
X = Activation('relu')(X)
# third layer
X = Conv2D(filters = F3, kernel_size = (1,1),strides= (1,1),padding ='valid') (X)
X = BatchNormalization(axis = 3 )(X)
# final step > adding shortcut value through relu activation
# X = Add()([X,X_shortcut])
X = tf.keras.layers.Add()([X,X_shortcut])
# X = keras.layers.concatenate() ([X,X_shortcut])
X = Activation('relu')(X)
return X
convolution block卷积块
def convolutional_block(X,f,filters,s =2):
# retrieve filters
F1,F2,F3 = filters
# save the input values
X_shortcut = X
# first layer
X = Conv2D(F1,(1,1),strides=(s,s))(X)
X = BatchNormalization(axis =3)(X)
X = Activation('relu')(X)
# second layer
X = Conv2D(filters = F2,kernel_size =(f,f),strides = (1,1), padding ="same")(X)
X = BatchNormalization(axis =3)(X)
X = Activation('relu')(X)
# third layer
X = Conv2D(filters = F3,kernel_size =(1,1),strides =(1,1), padding ="valid")(X)
X = BatchNormalization(axis = 3 )(X)
# shortcut path
X_shortcut = Conv2D(filters = F3,kernel_size =(1,1),strides = (s,s),padding ='valid')(X_shortcut)
X_shortcut = BatchNormalization( axis =3)(X_shortcut)
# final step > adding shortcut value through relu activation
# X = ADD()([X,X_shortcut])
X = tf.keras.layers.Add()([X,X_shortcut])
# X = keras.layers.concatenate() ([X,X_shortcut])
X = Activation('relu')(X)
return X
resnet50 architecture resnet50 架构
def Resnet50(input_shape= (224,224,3),classes = 309 ):
# implementing the resnet 50 achitechture over here
# define the input with shape input_shape
X_input = Input(input_shape)
# zero padding
X = ZeroPadding2D((3,3))(X_input)
# stage 1
X = Conv2D(64,(7,7),strides = (2,2))(X)
X = BatchNormalization(axis =3 )(X)
X = Activation('relu')(X)
X = MaxPooling2D((3,3),strides =(2,2))(X)
# stage 2
X = convolutional_block(X,f=3 , filters = [64,64,256],s=1)
# these line of code are the conv laters from convolution block function
# X = Conv2d(F1,(1,1),strides=(s,s))(X)
# X = Conv2D(filters = F2,kernel_size =(f,f),strides = (1,1), padding ="same")(X)
# X = conv2D(F3 ,(1,1),strides = (s,s),name = conv_name_base +'2a')(X)
X = identity_block(X ,3,[ 64, 64, 256])
# same line from identity block
X = identity_block(X, 3, [ 64, 64,256])
# same line from identity block
# stage 3
X = convolutional_block(X,f =3 , filters = [128,128,512],s=2)
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])
X = identity_block(X,3,[128,128,512])
# stage 4
X = convolutional_block(X,f = 3 , filters = [ 256, 256, 1024], s = 2 )
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
X = identity_block(X,3,[256,256,1024])
# stage 5
X = convolutional_block(X, f = 3 ,filters = [512,512,2048], s = 2 )
# X = identity_block(X,3 , [512,512,1024])
# X = identity_block(X,3 , [512,512,1024])
# AVGPOOL
X = AveragePooling2D((2,2),name='avg_pool')(X)
# achitech complete
# output
X = Flatten()(X)
X = Dense (classes, activation = 'softmax',name = 'fc'+str(classes),kernel_initializer = glorot_uniform(seed = 0))(X)
# Create model
model = Model(inputs = X_input , outputs =X , name='Resnet50')
return model
model = Resnet50(input_shape = (224,224,3), classes=309)
model.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_x,train_y,epochs= 10 , batch_size =32)
Looking at your resnet50 it seems your missing the last two identity blocks of stage 5看看你的 resnet50,你似乎错过了第 5 阶段的最后两个身份块
X = convolutional_block(X, f = 3 ,filters = [512,512,2048], s = 2 )
X = identity_block(X,3,[512,512,2048]]) # this part seems missing
X = identity_block(X,3,[512,512,2048]])
Btw you can just use keras applications for a shortcut顺便说一句,您可以使用 keras 应用程序作为快捷方式
import tensorflow as tf
tf.keras.applications.ResNet50(include_top=True, classes=200, weights=None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.