【问题标题】:Error with CNN + RNN "__init__() takes at least 4 arguments (4 given)"CNN + RNN 错误“__init__() 至少需要 4 个参数(给定 4 个)”
【发布时间】:2017-10-15 15:26:01
【问题描述】:

我最近学习了一篇关于深度学习的教程,现在我正在尝试自己创建一个。想法是拍摄一段视频,将其分割成单帧并通过神经网络提供给它。因为它是 jpg 我虽然是 CNN。但我没有对这张图片进行分类,而且我想获得浮点值。这就是为什么我虽然使用 RNN。我为 Keras 找到了一个支持这一点的库:但此时我卡住了。 (在 Python 2.7 上运行)

错误信息:

runfile('/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py', wdir='/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/spyder/utils/site')

runfile('/Users/tobias/Desktop/Projekt/Speed_ANN.py', wdir='/Users/tobias/Desktop/Projekt')
Traceback (most recent call last):

  File "<ipython-input-14-b3a54cae7fa1>", line 1, in <module>
    runfile('/Users/tobias/Desktop/Projekt/Speed_ANN.py', wdir='/Users/tobias/Desktop/Projekt')

  File "/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)

  File "/Users/tobias/Desktop/Projekt/Speed_ANN.py", line 38, in <module>
    classifier.add(TimeDistributedConvolution2D(32,(3,3),input_shape = (64, 64, 3),activation = 'relu'))

TypeError: __init__() takes at least 4 arguments (4 given)

我有 4 个参数?是不是我写错了?

这是我的代码:你还需要 kera-extra.py 吗?这是我添加的库

"""
Creator: Tobias
Date: 15.05.17
"""
#Initialising video preprocessing
import cv2
import numpy as np
import pandas as pd

#Initialising all Libarys for Deep Learning
from keras.models import Sequential
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers.extra import TimeDistributedConvolution2D
from keras.layers.extra import TimeDistributedFlatten
from keras.layers.extra import TimeDistributedMaxPooling2D
"""
#Loading .txt with speed values
speed_values = pd.read_csv('data/train.txt')

#Loading Video in Python
video = cv2.VideoCapture('data/train.mp4')
success,image = video.read()
count = 0
success = True
#Splitting video in single images in jpg
while success:
    success,image = video.read()
    #cv2.imwrite('data/video_jpg/',speed_values[success],'.jpg')
    cv2.imwrite("data/video_jpg/%f.jpg" %speed_values.iloc[count,:].values,image) 
    count += 1 
print('Video Succefully Converted to jpg')
"""


classifier = Sequential()
# Initialising the CNN and building CNN
classifier.add(TimeDistributedConvolution2D(32,(3,3),input_shape = (64, 64, 3),activation = 'relu'))
classifier.add(TimeDistributedConvolution2D(16, 3, 3, border_mode='valid',activation = 'relu'))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics = ['accuracy'])

#Preprocessing the video data for CNN part 2

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('data/training/train_data',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('data/training/test_data',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 8000,
                         epochs = 5,
                         validation_data = test_set,
                         validation_steps = 2000)

//编辑 @JohanL 我按照您的建议进行了更改,但即使它具有所需的所有参数,它仍然有错误。

classifier = Sequential()
# Initialising the CNN and building CNN
classifier.add(TimeDistributedConvolution2D(32,3,3,input_shape = (64, 64, 3),activation = 'relu'))

现在它给了我:用标准的 CNN 做这件事,所以也许 keras.extra 库有问题?

classifier = Sequential()
# Initialising the CNN and building CNN
classifier.add(TimeDistributedConvolution2D(32,3,3,input_shape = (64, 64, 3),activation = 'relu'))
Traceback (most recent call last):



  File "<ipython-input-20-085e686ea1fc>", line 3, in <module>
    classifier.add(TimeDistributedConvolution2D(32,3,3,input_shape = (64, 64, 3),activation = 'relu'))

  File "/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/keras/models.py", line 433, in add
    layer(x)

  File "/Users/tobias/anaconda3/envs/opencv/lib/python2.7/site-packages/keras/engine/topology.py", line 558, in __call__
    self.build(input_shapes[0])

TypeError: build() takes exactly 1 argument (2 given)

【问题讨论】:

  • CNN 还可以进行回归,而不仅仅是分类。
  • keras-extra 库本身可能没有任何问题,但它似乎与 keras 0.3 版兼容。从那时起,构建函数得到了一个新的签名,所以你的新问题是由于你的主 keras 包的版本太新了。
  • 是的,我明白了。但我该如何解决这个问题?我没有找到任何其他结合了 CNN 和 RNN 的库,而且我没有看到它已经包含在 Keras 中

标签: python python-2.7 deep-learning keras keras-layer


【解决方案1】:

这里是TimeDistributedConvolution2D的init函数定义中的方法头:

def __init__(self, nb_filter, nb_row, nb_col,
             init='glorot_uniform', activation='linear', weights=None,
             border_mode='valid', subsample=(1, 1), dim_ordering='th',
             W_regularizer=None, b_regularizer=None, activity_regularizer=None,
             W_constraint=None, b_constraint=None, **kwargs):

可以看出,有四个参数(三个加上 self)没有默认值。这四个参数必须给出。我假设您不打算将元组作为参数,而是:

classifier.add(TimeDistributedConvolution2D(32,3,3,input_shape = (64, 64, 3),activation = 'relu'))

这将有足够数量的参数。

但是,当您提供具有默认值的参数时,错误消息有点奇怪。这就是为什么您最终会收到这个奇怪的错误消息。

但请注意,即使使用此修复程序,您也需要相当旧的 keras 版本 (0.3) 才能使其正常工作,因此如果您可以使用其他方法,也许您应该尝试其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2013-08-03
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多