def train_classifier(genres=[ 'Chases', 'Dance', 'Eating', 'Fight', 'Heated_Discussions', 'Normal_Chatting', 'Romance', 'Running', 'Tragic' ], model_name=default_model_name): trainingData = [] trainingLabels = [] num_of_classes = len(genres) print("Number of classes:", num_of_classes) for genreIndex, genre in enumerate(genres): try: genreFeatures = load_pkl(genre + "_ultimate_OF_" + default_model_name) genreFeatures = np.array([np.array(f) for f in genreFeatures]) # numpy hack except Exception as e: print(e) return print("OK.") for videoFeatures in genreFeatures: randomIndices = range(len(videoFeatures)) selectedFeatures = np.array(videoFeatures[randomIndices]) for feature in selectedFeatures: trainingData.append(feature) trainingLabels.append([genreIndex]) trainingData = np.array(trainingData) trainingLabels = np.array(trainingLabels) print("Train data shape : ", trainingData.shape) print(trainingLabels.shape) model = spatial_model(trainingData) model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy']) batch_size = 32 nb_epoch = 100 history = model.fit(trainingData, trainingLabels, batch_size=batch_size, nb_epoch=nb_epoch) modelOutPath = 'data/models/temporal' + model_name + '_' + str( num_of_classes) + "g_bs" + str(batch_size) + "_ep" + str( nb_epoch) + ".h5" model.save(modelOutPath) print("Model saved at", modelOutPath) plt.plot(history.history["acc"]) plt.title("model accuracy") plt.ylabel("accuracy") plt.xlabel("epoch") plt.show() plt.plot(history.history["loss"]) plt.title("model loss") plt.ylabel("loss") plt.xlabel("epoch") plt.show()
def train_classifier(genres=['romance', 'horror', 'action'], num_of_videos=100): """Gather data for selected genres""" trainingData = [] trainingLabels = [] num_of_random_frames = 75 num_of_classes = len(genres) print "Number of classes:", num_of_classes for genreIndex, genre in enumerate(genres): print "Looking for pickle file: data/{0}{1}.p".format( genre, str(num_of_videos)), try: genreFeatures = load_pkl(genre + str(num_of_videos)) genreFeatures = np.array([np.array(f) for f in genreFeatures]) # numpy hack except Exception as e: print e return print "OK." for videoFeatures in genreFeatures: if len(videoFeatures) > num_of_random_frames: randomIndices = np.random.randint(0, len(videoFeatures), num_of_random_frames) selectedFeatures = np.array(videoFeatures[randomIndices]) for feature in selectedFeatures: trainingData.append(feature) trainingLabels.append([genreIndex]) trainingData = np.array(trainingData) trainingLabels = np.array(trainingLabels) print trainingData.shape print trainingLabels.shape # trainingLabels = to_categorical(trainingLabels, num_of_classes) print trainingLabels # trainingLabels = trainingLabels.reshape((-1,num_of_classes)) """Initialize the mode""" model = spatial_model(num_of_classes) model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy']) """Start training""" batch_size = 32 nb_epoch = 50 model.fit(trainingData, trainingLabels, batch_size=batch_size, nb_epoch=nb_epoch) #, callbacks=[remote]) modelOutPath = 'data/models/spatial_' + str(num_of_classes) + "g_bs" + str( batch_size) + "_ep" + str(nb_epoch) + "_nf_" + str( num_of_random_frames) + ".h5" model.save(modelOutPath) print "Model saved at", modelOutPath
def train_classifier(genres=['comedy', 'horror', 'action'], model_train='spatial', model_name=default_model_name, epoch=100): """Gather data for selected genres""" trainingData = [] trainingLabels = [] # num_of_random_frames = 75 num_of_classes = len(genres) print("Number of classes:", num_of_classes) for genreIndex, genre in enumerate(genres): # print "Looking for pickle file: data/{0}{1}.p".format(genre, str(num_of_videos)), try: print("train/" + genre + "_train_" + model_name) genreFeatures = load_pkl("train/" + genre + "_train_" + model_name) genreFeatures = np.array([np.array(f) for f in genreFeatures]) # numpy hack except Exception as e: print(e) return print("OK.") for videoFeatures in genreFeatures: """to get all frames from a video -- hacky""" randomIndices = range(len(videoFeatures)) selectedFeatures = np.array(videoFeatures[randomIndices]) for feature in selectedFeatures: trainingData.append(feature) trainingLabels.append([genreIndex]) trainingData = np.array(trainingData) trainingLabels = np.array(trainingLabels) print(trainingData.shape) print(trainingLabels.shape) # trainingLabels = to_categorical(trainingLabels, num_of_classes) print(trainingLabels) # trainingLabels = trainingLabels.reshape((-1,num_of_classes)) """Initialize the mode""" if (model_train == 'mlp'): model = mlp_model(num_of_classes, trainingData.shape[1]) model.compile(optimizer='adadelta', loss='sparse_categorical_crossentropy', metrics=['accuracy']) elif (model_train == 'lstm'): num_of_frames = 9 num_of_samples = len(trainingLabels) trainingDataTensor = np.zeros( (num_of_samples, num_of_frames, trainingData.shape[1])) print(len(trainingData)) for sampleIndex in range(num_of_samples): for vectorIndex in range(num_of_frames): try: trainingDataTensor[sampleIndex][ vectorIndex] = trainingData[sampleIndex][vectorIndex] except Exception as e: print(e) continue print(trainingDataTensor.shape) trainingData = trainingDataTensor model = lstm_model(num_of_classes, input_dim=trainingData.shape[2]) model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy']) elif (model_train == 'spatial'): model = spatial_model(num_of_classes, trainingData.shape[1]) model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy']) """Start training""" batch_size = 32 #b_epoch = 100 model.fit(trainingData, trainingLabels, batch_size=batch_size, epochs=int(epoch)) #, callbacks=[remote]) modelOutPath = 'data/models/' + model_train + '_' + model_name + '_' + str( num_of_classes) + "g_bs" + str(batch_size) + "_ep" + str(epoch) + ".h5" model.save(modelOutPath) print("Model saved at", modelOutPath)