TypeError:object() takes no parameters
Traceback (most recent call last):
File "vgg16_train.py", line 132, in <module>
finalAct="sigmoid")
File "/home/data/zpp/work2/pyimagessearch/pyimagesearch/vggnet16.py", line 33, in build
base_model = ResNet50(weights='imagenet',include_top=False,pooling='avg')
TypeError: object() takes no parameters
问题描述:我首先在 module vggnet16.py 中定义了一个类 class RseNet50, 然后我在主程序中引用这个类,代码如下:
from vggnet16 import ResNet50
...
model = Resnet50.build(
width=IMAGE_DIMS[1], height=IMAGE_DIMS[0],
depth=IMAGE_DIMS[2], classes=len(mlb.classes_),
finalAct="sigmoid")
发生错误:TypeError: object() takes no parameters
错误原因为:在自定义的模块 vggnet16.py 中, 类 ResNet50 与 导入的模块 ResNet50 重名。自定义的类应避免与导入的模块重名。
vggnet16.py
# import the necessary packages
from keras.models import Model
from keras.layers import Dense
from keras.applications.resnet50 import ResNet50
class ResNet50:
@staticmethod
def build(width,height,depth,classes,finalAct="softmax"):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
width = 224
width = 224
depth = 3
base_model = ResNet50(weights='imagenet',include_top=False,pooling='avg')
# first (and only) set of FC => RELU layers
fc1 = Dense(1024, activation='relu')(base_model.output)
# softmax classifier
predications = Dense(classes, activation=finalAct)(fc1)
model = Model(inputs=base_model.input, outputs=predications)
# return the constructed network architecture
return model