CNN + RNN错误" __ init __()至少需要4个参数(4个给定)"

时间:2017-05-16 13:45:53

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

我最近参加了一个关于深度学习的教程,我现在正试图自己创建一个。想法是拍摄视频,将其分成单帧并通过神经网络提供。因为它是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)

// EDIT @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)

1 个答案:

答案 0 :(得分:1)

以下是TimeDistributedConvolution2D的初始化函数定义中的方法标题:

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):

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

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

将有足够数量的args。

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

请注意,即使使用此修复程序,您也需要一个非常古老的版本(0.3)keras才能使用,所以如果您可以使用其他方法,也许应该尝试其他方法。