【发布时间】:2021-07-09 14:44:04
【问题描述】:
尝试在 poly-u 数据集上实现 resnet 50。 我在这里想做的是我想制作一个模型,将个人的掌纹分类为不同的身份,我发现 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.
身份块
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
卷积块
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 架构
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)
【问题讨论】:
标签: python tensorflow machine-learning deep-learning resnet