添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 :
Traceback (most recent call last): File " __main__.py" , line 17 , in <module> movies_genre_model.build(version, min_year, max_year, genres, ratio, epochs, File " /home/.../Desktop/movies_genre_model.py" , line 66 , in build y_binary = to_categorical(y_train) File " /home/.../.local/lib/python3.8/site-packages/keras/utils/np_utils.py" , line 68 , in to_categorical num_classes = np.max(y) + 1 File " <__array_function__ internals>" , line 180 , in amax File " /home/.../.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py" , line 2791 , in amax return _wrapreduction(a, np.maximum, ' max' , axis, None, out , File " /home/.../.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py" , line 86 , in _wrapreduction return ufunc.reduce(obj, axis, dtype, out , **passkwargs) ValueError: zero-size array to reduction operation maximum which has no identity
The first code is for movies_genre_model.py, and the other one is for __main__.py
What does this mean?
What I have tried:
import os import time import keras from keras.layers import Conv2D, MaxPooling2D from keras.layers import Dense, Dropout, Flatten from keras.models import Sequential from tensorflow import keras from keras import optimizers optimizers=keras.optimizers.RMSprop(learning_rate= 0 . 01 ) import tensorflow as tf tf.config.run_functions_eagerly(True) tf.compat.v1.disable_eager_execution() tf.config.run_functions_eagerly(False) from tensorflow.keras.utils import to_categorical import movies_dataset as movies def get_kernel_dimensions(version, shape, divisor): image_width = shape[0] # original if version == 1 : return 3 , 3 # square 10% width if version == 2 : return int ( 0 . 1 * image_width / divisor), int ( 0 . 1 * image_width / divisor) # square 20% width if version == 3 : return int ( 0 . 2 * image_width / divisor), int ( 0 . 2 * image_width / divisor) def build(version, min_year, max_year, genres, ratio, epochs, x_train=None, y_train=None, x_validation=None, y_validation=None): # log print() print( ' version:' , version) print( ' min_year:' , min_year) print( ' max_year:' , max_year) print( ' genres:' , genres) print( ' ratio:' , ratio) print() # load data if not provided if x_train is None or y_train is None or x_validation is None or y_validation is None: begin = time.time() x_train, y_train = movies.load_genre_data(min_year, max_year, genres, ratio, ' train' ) x_validation, y_validation = movies.load_genre_data(min_year, max_year, genres, ratio, ' validation' ) print( ' loaded in' , (time.time() - begin) / 60 , ' min.' ) else : print( ' data provided in arguments' ) print() print( ' x_train shape:' , x_train.shape) print(x_train.shape[0], ' train samples' ) print(x_validation.shape[0], ' validation samples' ) y_binary = to_categorical(y_train) # build model if y_train.size != 0 : num_classes = len(y_train[0]) num_classes = 0 kernel_dimensions1 = get_kernel_dimensions(version, x_train.shape, 1 ) kernel_dimensions2 = get_kernel_dimensions(version, x_train.shape, 2 ) print( ' kernel_dimensions1:' , kernel_dimensions1) print( ' kernel_dimensions2:' , kernel_dimensions2) x_train = x_train.reshape(x_train.shape[0], 106 , 161 , 1 ) input_shape = ( 14265 , 106 , 161 , 1 ) model = Sequential([ Conv2D( 32 , kernel_dimensions1, padding= ' same' , input_shape =input_shape[1:] , activation= ' relu' ), #problem with input size original was x_train.shape[1:] Conv2D( 32 , kernel_dimensions1, activation= ' relu' ), MaxPooling2D(pool_size=( 2 , 2 )), Dropout( 0 . 25 ), Conv2D( 64 , kernel_dimensions2, padding= ' same' , activation= ' relu' ), Conv2D( 64 , kernel_dimensions2, activation= ' relu' ), MaxPooling2D(pool_size=( 2 , 2 )), Dropout( 0 . 25 ), Flatten(), Dense( 512 , activation= ' relu' ), Dropout( 0 . 5 ), Dense(num_classes, activation= ' sigmoid' ) opt = keras.optimizers.RMSprop(lr= 0 . 0001 , decay=1e-6) model.compile(loss= ' categorical_crossentropy' , optimizer=opt, metrics=[ ' accuracy' ]) print(model.summary()) model.fit(x_train, y_train, batch_size= 32 , epochs=epochs, validation_data=(x_validation, y_validation)) # create dir if none save_dir = os.path. join (os.getcwd(), ' saved_models' ) if not os.path.isdir(save_dir): os.makedirs(save_dir) # save model model_file_name = ' genres' \ + ' _' + str(min_year) + ' _' + str(max_year) \ + ' _g' + str(len(genres)) \ + ' _r' + str(ratio) \ + ' _e' + str(epochs) \ + ' _v' + str(version) + ' .h5' model_path = os.path. join (save_dir, model_file_name) model.save(model_path) print( ' Saved trained model at %s ' % model_path)
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
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •