0

I am trying to do multi-label classification using Keras. I got my dataset from kaggle.

Link to dataset: https://www.kaggle.com/dadajonjurakuziev/movieposter

I am getting an error when I am trying to fit the model.

THE CODE:

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=20, test_size=0.3)


model = Sequential()

model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu", input_shape=(SIZE,SIZE,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=(5, 5), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(25, activation='sigmoid'))

model.summary()

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
#print(X_train, y_train, X_test, y_test)
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test)) #This is the line where I am getting an error
​

THE ERROR:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

Here are some values of X_train and y_train:

X_train[0]:

array([[[0.15294118, 0.1764706 , 0.3019608 ],
        [0.15294118, 0.1764706 , 0.3019608 ],
        [0.15686275, 0.18039216, 0.30588236],
        ...,
        [0.69803923, 0.69411767, 0.75686276],
        [0.69803923, 0.69411767, 0.75686276],
        [0.69803923, 0.69411767, 0.75686276]],

       [[0.11764706, 0.14509805, 0.25882354],
        [0.11764706, 0.14509805, 0.25882354],
        [0.12156863, 0.14509805, 0.27058825],
        ...,
        [0.69411767, 0.6901961 , 0.7529412 ],
        [0.69411767, 0.6901961 , 0.7529412 ],
        [0.69411767, 0.6901961 , 0.7529412 ]],

       [[0.10588235, 0.13333334, 0.24313726],
        [0.10588235, 0.13333334, 0.24313726],
        [0.10980392, 0.13725491, 0.2509804 ],
        ...,
        [0.6901961 , 0.6862745 , 0.7490196 ],
        [0.6901961 , 0.6862745 , 0.7490196 ],
        [0.6901961 , 0.6862745 , 0.7490196 ]],

       ...,

       [[0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        ...,
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943]],

       [[0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        ...,
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943],
        [0.16862746, 0.30588236, 0.62352943]],

       [[0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        ...,
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943],
        [0.1764706 , 0.31764707, 0.62352943]]], dtype=float32)

y_train[0]:

array(['https://m.media-amazon.com/images/M/MV5BY2IyODNmMjctMjNlNi00NmU0LTkxYTEtMTBjNzJiY2U0ZTI2XkEyXkFqcGdeQXVyMjI3MTc2MzU@._V1_UX256,0,256,256_AL_.jpg',
       'The Lawyer', 2020, 7.1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
      dtype=object)

I am not sure what the object dtype is. I am pretty new to these topics. Could someone please help me? Thanks!

1
  • It sounds like your numpy arrays are of type int while keras expects floats. Try X = X.astype('float32') and Y = Y.astype('float32') before doing anything else with them. Commented Jul 10, 2021 at 10:41

1 Answer 1

0

The error is the same of the question Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray) on ImageDataGenerator in Keras.

The function model.fit expects X_train and y_train to be np.ndarrays of numeric types like np.float32 or np.int32. Here, instead the type of y_train is np.object because this array contains elements with mixed types (strings and integers).

Probably you should process the labels in y_train and remove the strings, because you cannot use strings to compute a loss.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.