Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new with python and I'm doing a project trying to classify movie posters by genre. Then, when I run main, I came up to this problem :
File "/home/.../Desktop/movies_genre_model.py", line 56, in build
num_classes = len(y_train(0))
TypeError: 'NoneType' object is not callable

What does this mean?

What I have tried:

Python
  1  import os
  2  import time
  3  
  4  import keras
  5  from keras.layers import Conv2D, MaxPooling2D
  6  from keras.layers import Dense, Dropout, Flatten
  7  from keras.models import Sequential
  8  
  9  import movies_dataset as movies
 10  
 11  
 12  def get_kernel_dimensions(version, shape, divisor):
 13      image_width = shape[0]
 14  
 15      # original
 16      if version == 1:
 17          return 3, 3
 18  
 19      # square 10% width
 20      if version == 2:
 21          return int(0.1 * image_width / divisor), int(0.1 * image_width / divisor)
 22  
 23      # square 20% width
 24      if version == 3:
 25          return int(0.2 * image_width / divisor), int(0.2 * image_width / divisor)
 26  
 27  
 28  def build(version, min_year, max_year, genres, ratio, epochs,
 29            x_train=None, y_train=None, x_validation=None, y_validation=None):
 30      # log
 31      print()
 32      print('version:', version)
 33      print('min_year:', min_year)
 34      print('max_year:', max_year)
 35      print('genres:', genres)
 36      print('ratio:', ratio)
 37      print()
 38  
 39      # load data if not provided
 40      if x_train is None or y_train is None or x_validation is None or y_validation is None:
 41          begin = time.time()
 42          x_train = movies.load_genre_data(min_year, max_year, genres, ratio, 'train')
 43          y_train = movies.load_genre_data(min_year, max_year, genres, ratio, 'train')
 44          x_validation = movies.load_genre_data(min_year, max_year, genres, ratio, 'validation')
 45          y_validation = movies.load_genre_data(min_year, max_year, genres, ratio, 'validation')
 46          print('loaded in', (time.time() - begin) / 60, 'min.')
 47      else:
 48          print('data provided in arguments')
 49  
 50      print()
 51      print('x_train shape:', x_train.shape)
 52      print(x_train.shape[0], 'train samples')
 53      print(x_validation.shape[0], 'validation samples')
 54  
 55      # build model
 56      num_classes = len(y_train(0))
 57      kernel_dimensions1 = get_kernel_dimensions(version, x_train.shape, 1)
 58      kernel_dimensions2 = get_kernel_dimensions(version, x_train.shape, 2)
 59      print('kernel_dimensions1:', kernel_dimensions1)
 60      print('kernel_dimensions2:', kernel_dimensions2)
 61  
 62      model = Sequential([
 63          Conv2D(32, kernel_dimensions1, padding='same', input_shape=x_train.shape[1:], activation='relu'),
 64          Conv2D(32, kernel_dimensions1, activation='relu'),
 65          MaxPooling2D(pool_size=(2, 2)),
 66          Dropout(0.25),
 67  
 68          Conv2D(64, kernel_dimensions2, padding='same', activation='relu'),
 69          Conv2D(64, kernel_dimensions2, activation='relu'),
 70          MaxPooling2D(pool_size=(2, 2)),
 71          Dropout(0.25),
 72  
 73          Flatten(),
 74          Dense(512, activation='relu'),
 75          Dropout(0.5),
 76          Dense(num_classes, activation='sigmoid')
 77      ])
 78  
 79      opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
 80      model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 81      print(model.summary())
 82  
 83      model.fit(x_train, y_train, batch_size=32, epochs=epochs, validation_data=(x_validation, y_validation))
 84  
 85      # create dir if none
 86      save_dir = os.path.join(os.getcwd(), 'saved_models')
 87      if not os.path.isdir(save_dir):
 88          os.makedirs(save_dir)
 89  
 90      # save model
 91      model_file_name = 'genres' \
 92                        + '_' + str(min_year) + '_' + str(max_year) \
 93                        + '_g' + str(len(genres)) \
 94                        + '_r' + str(ratio) \
 95                        + '_e' + str(epochs) \
 96                        + '_v' + str(version) + '.h5'
 97  
 98      model_path = os.path.join(save_dir, model_file_name)
 99      model.save(model_path)
100      print('Saved trained model at %s ' % model_path)
Posted
Updated 11-Mar-22 6:28am
v4
Comments
Στ Κ 2022 12-Mar-22 7:02am    
*Update with the main.py code

import movies_dataset as movies
import movies_genre_model

min_year = 1977
max_year = 2017
epochs = 50
genres = movies.list_genres(7)

# select a smaller ratio (e.g. 40) for quicker training
for ratio in [30]:
    # we load the data once for each ratio, so we can use it for multiple versions, epochs, etc.
    x_train = movies.load_genre_data(min_year, max_year, genres, ratio, 'train')
    y_train = movies.load_genre_data(min_year, max_year, genres, ratio, 'train')
    x_validation = movies.load_genre_data(min_year, max_year, genres, ratio, 'validation') 
    y_validation = movies.load_genre_data(min_year, max_year, genres, ratio, 'validation')
    for version in [1, 2, 3]:
        movies_genre_model.build(version, min_year, max_year, genres, ratio, epochs,
                                 x_train=x_train,
                                 y_train=y_train,
                                 x_validation=x_validation,
                                 y_validation=y_validation
                                 )


But now I have a new problem when I run main:

File "/home/.../Desktop/movies_genre_model.py", line 54, in build
num_classes = len(y_train[0])
IndexError: index 0 is out of bounds for axis 0 with size 0

As you can see in line 43, y_train creation is in a comment line.

[edit]
OK, you are trying to call a function named y_train, (which did not exist earlier). Line 56 should be
Python
num_classes = len(y_train[0]) # square brackets around the zero.

[/edit]
 
Share this answer
 
v2
Comments
Στ Κ 2022 11-Mar-22 10:31am    
Yeah, I changed that and I forgot to delete that, but still the same problem
Richard MacCutchan 11-Mar-22 10:35am    
Please show the exact code you are trying to build and the associated error message(s).
Richard MacCutchan 11-Mar-22 12:21pm    
See my update solution.
Στ Κ 2022 13-Mar-22 9:27am    
updated with main code
Richard MacCutchan 13-Mar-22 9:47am    
The new error message is telling you that y_train does not contain any items.
Wrong kind of brackets? As Richard says, without all your code, we can only guess. I'm guessing that you intend this to be y_train[0] - the first element of array/list y_train. If y_train is actually a function, then y_train(0) should exist . . . but without knowing we can't be sure.
 
Share this answer
 
Comments
Στ Κ 2022 13-Mar-22 9:28am    
updated with main code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900