Tobias
Tobias

Reputation: 7

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. But I don't categorize this picture, furthermore i wanted to get float values. Thats why i though using an RNN. I found a libary for Keras which support this: But at this point I stuck. (Running on 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 ? Did i put something wrong in?

This is my code: Do you need the kera-extra.py aswell? 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.

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?

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)

Upvotes: 0

Views: 1408

Answers (1)

JohanL
JohanL

Reputation: 6891

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

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.

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.

Upvotes: 1

Related Questions