简体   繁体   English

CNN + RNN错误“ __init __()至少接受4个参数(给定4个)”

[英]Error with CNN + RNN “__init__() takes at least 4 arguments (4 given)”

I recently took a tutorial about Deep learning and I am now trying to creat one by myself. 我最近学习了有关深度学习的教程,现在我正尝试自己创建一个。 The Idea is to take a video, split it in single frames and feed it through an Neural Network. 想法是拍摄视频,将其分割为单个帧,然后通过神经网络进行输入。 Because it is jpg i though of an CNN. 因为它是jpg,但我是CNN。 But I don't categorize this picture, furthermore i wanted to get float values. 但是我没有对这张图片进行分类,而且我想获得浮点数。 Thats why i though using an RNN. 那就是为什么我虽然使用RNN。 I found a libary for Keras which support this: But at this point I stuck. 我找到了支持Keras的库来支持这一点:但是在这一点上,我坚持了下来。 (Running on Python 2.7) (在Python 2.7上运行)

Error Message: 错误信息:

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)

I have 4 arguments ? 我有4个论点? Did i put something wrong in? 我放错了吗?

This is my code: Do you need the kera-extra.py aswell? 这是我的代码:您还需要kera-extra.py吗? This is the library I added 这是我添加的库

"""
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 I changed it as you suggested but it still have an error even it has all arguments it need. // EDIT @JohanL我按照您的建议进行了更改,但是即使它具有所需的所有参数,它仍然有一个错误。

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

Now it gives me: Doing it with standart CNN works so maybe something wrong in the keras.extra library? 现在,它给了我:使用标准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)

Here is the method header from the definition of the init function of TimeDistributedConvolution2D : 这是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):

As can be seen, there are four arguments (three plus self) that have no defaults. 可以看出,有四个没有默认值的参数(三个加自身)。 These four arguments must be given. 这四个参数必须给出。 I assume that you do not intend to give a tuple as argument, but rather: 我假设您不打算以元组为参数,而是:

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

which will have sufficient number of args. 其中将具有足够数量的args。

The error message is a little bit strange, though, when you are providing arguments that have default values. 但是,当您提供具有默认值的参数时,错误消息有点奇怪。 That is why you end up with this strange error message. 这就是为什么您会收到此奇怪的错误消息的原因。

Note, though, that even with this fix, you need a pretty old version (0.3) of keras for this to work, so maybe you should try another method if it is available to you. 但是请注意,即使使用此修复程序,您也需要一个相当旧的keras版本(0.3)才能运行此功能,因此,也许您应该尝试另一种方法(如果可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python错误:__ init __()至少需要3个参数(给定3个) - Python error: __init__() takes at least 3 arguments (3 given) TypeError:__ init __()至少需要2个参数(给定1个)错误 - TypeError: __init__() takes at least 2 arguments (1 given) error UpdateView导致错误“ __init __()至少接受2个参数(给定1个)” - UpdateView causing error “__init__() takes at least 2 arguments (1 given)” TypeError:__init __()至少接受3个参数(给定1个) - TypeError: __init__() takes at least 3 arguments (1 given) 类型错误:__init__() 需要至少 3 个参数(给出 2 个) - TypeError: __init__() takes at least 3 arguments (2 given) 错误 - __init __()需要2个参数(给定1个) - Error - __init__() takes exactly 2 arguments (1 given) Django-ModelChoiceField-TypeError-__init __()至少接受2个参数(给定1个) - Django - ModelChoiceField - TypeError - __init__() takes at least 2 arguments (1 given) TypeError:__init __()至少接受4个非关键字参数(给定3个) - TypeError: __init__() takes at least 4 non-keyword arguments (3 given) TypeError:__init __()至少接受3个参数(给定5个)Keras - TypeError: __init__() takes at least 3 arguments (5 given) Keras 我得到__init __()在IntegerField上至少需要2个参数(给定1个) - I'm getting __init__() takes at least 2 arguments (1 given) on IntegerField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM