def __init__(self, filepath, batch_size=32, class_num=2, dim=(32, 32), n_channels=1, n_sequence=4, preprocess_input=None, with_aug=True, shuffle=True, path_dataset=None, type_gen='train', option=None): 'Initialization' data_dict = readfile_to_dict(filepath) data_keys = list(data_dict.keys()) self.dim = dim self.batch_size = batch_size self.class_num = class_num self.labels = data_dict self.list_IDs = data_keys self.n_channels = n_channels self.n_sequence = n_sequence # get n_sequence diff image self.shuffle = shuffle self.path_dataset = path_dataset self.type_gen = type_gen self.option = option self.aug_gen = ImageDataGenerator() self.steps_per_epoch = len(self.list_IDs) // self.batch_size print("all:", len(self.list_IDs), " batch per epoch", self.steps_per_epoch) self.on_epoch_end()
path_dataset = base_path + 'Dataset\\UCF-101\\' detail_weight = 'UCF-mobilenet2' # Parameters params = {'dim': dim, 'batch_size': 2, # 'n_classes': 6, 'n_sequence': n_sequence, 'n_channels': n_channels, 'path_dataset': path_dataset, 'shuffle': True} train_txt = "dataset_list/UCF-101/trainlistUCF-101.txt" test_txt = "dataset_list/UCF-101/testlistUCF-101.txt" train_d = readfile_to_dict(train_txt) test_d = readfile_to_dict(test_txt) # Prepare key train_keys = list(train_d.keys()) * 3 # duplicate 100 time test_keys = list(test_d.keys()) * 1 # Label labels = train_d.copy() labels.update(test_d) # Labels # # Generators training_generator = DataGeneratorBKB(train_keys, labels, **params, type_gen='train') validation_generator = DataGeneratorBKB(test_keys, labels, **params, type_gen='test') # # Design model
from model_ML import create_model_pretrain from data_helper import readfile_to_dict dim = (224, 224) n_sequence = 8 n_channels = 3 n_output = 5 # base_path = 'D:\\Peach\\' base_path = 'F:\\Master Project\\' # path_dataset = base_path+'Dataset\\sit_stand\\' path_dataset = base_path + 'Dataset\\BUPT-dataset\\RGBdataset\\' ## dataset test_txt = "dataset_list/testlistBUPT.txt" test_d = readfile_to_dict(test_txt) labels = test_d.copy() num_mul = 1 print(len(test_d.keys())) key_list = list(test_d.keys()) * num_mul # model weights_path = 'BUPT-augmentKeras-RGB-72-0.88-0.85.hdf5' model = create_model_pretrain(dim, n_sequence, n_channels, n_output, 'MobileNetV2') model.load_weights(weights_path) y_pred_all = [] def get_sampling_frame(len_frames):
# Keyword argument params = { 'dim': dim, 'batch_size': batch_size, # you can increase for faster training 'n_sequence': n_sequence, 'n_channels': n_channels, 'path_dataset': path_dataset, 'option': args['mode'], 'shuffle': True } train_txt = "dataset_list/train_64frames.txt" val_txt = "dataset_list/val_64frames.txt" # test_txt = "dataset_list/testlist.txt" train_d = readfile_to_dict(train_txt) val_d = readfile_to_dict(val_txt) # test_d = readfile_to_dict(test_txt) # def select_fights(path): # # elems = path.split('\\') # if elems[1] == 'fighting': # return True # else: # return False # print(len(train_d.keys())) # train_d = dict(filter(lambda elem: select_fights(elem[0]), train_d.items())) print(len(train_d.keys())) print(Counter(list(train_d.values())))
def evaluate(config, weights_path): ###### Parameters setting dim = (config['model']['input_width'], config['model']['input_height'] ) # for MobileNetV2 n_sequence = config['model']['sequence'] # for LSTM n_channels = config['model']['channels'] # color channel(RGB) n_output = config['model']['class_num'] # number of output class batch_size = config['train']['batch_size'] n_mul_train = 1 # To increase sample of train set n_mul_test = 4 # To increase sample of test set path_dataset = config['train']['data_dir'] ###### params = { 'dim': dim, 'batch_size': batch_size, 'n_sequence': n_sequence, 'n_channels': n_channels, 'path_dataset': path_dataset, 'option': 'RGBdiff', 'shuffle': False } test_txt = config['valid']['file_list'] test_d = readfile_to_dict(test_txt) key_list = list(test_d.keys()) * n_mul_test # IDs # validation_generator = DataGeneratorBKB(partition['validation'] , labels, **params, type_gen='test') # for evalutate_generator predict_generator = DataGenerator(key_list, test_d, **params, type_gen='predict') # evaluate eval_graph = tf.Graph() eval_sess = tf.Session(graph=eval_graph, config=tf_config) keras.backend.set_session(eval_sess) with eval_graph.as_default(): keras.backend.set_learning_phase(0) model = create_model(n_output) model.load_weights(weights_path) # Example for evaluate generator # If you want to use, just uncomment it # loss, acc = model.evaluate_generator(validation_generator, verbose=0) # print(loss,acc) # #### Confusion Matrix y_pred_prob = model.predict_generator(predict_generator, workers=0) test_y = np.array(list(test_d.values()) * n_mul_test) print("-----------") print(y_pred_prob.shape) print(len(test_y)) y_pred = np.argmax(y_pred_prob, axis=1) normalize = True all_y = len(test_y) sum = all_y for i in range(len(y_pred)): if test_y[i] != y_pred[i]: sum -= 1 print(key_list[i], ' actual:', test_y[i], 'predict:', y_pred[i]) cm = confusion_matrix(test_y, y_pred) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') accuracy = sum / all_y print("accuracy:", accuracy) classes = [*range(1, n_output + 1)] # [1,2,3,...,18] df_cm = pd.DataFrame(cm, columns=classes, index=classes) df_cm.index.name = 'Actual' df_cm.columns.name = 'Predicted' fig, ax = plt.subplots(figsize=(5, 5)) sn.set(font_scale=0.6) #for label size sn.heatmap(df_cm, cmap="Blues", annot=True, fmt=".2f", annot_kws={"size": 8}) # font size # ax.set_ylim(5, 0) # plt.show() plt.savefig('eval_model.png')
def train(config): ###### Parameters setting dim = (config['model']['input_width'], config['model']['input_height'] ) # for MobileNetV2 n_sequence = config['model']['sequence'] # for LSTM n_channels = config['model']['channels'] # color channel(RGB) n_output = config['model']['class_num'] # number of output class batch_size = config['train']['batch_size'] n_mul_train = 1 # To increase sample of train set n_mul_test = 4 # To increase sample of test set path_dataset = config['train']['data_dir'] ###### # Keyword argument params = { 'dim': dim, 'batch_size': batch_size, # you can increase for faster training 'n_sequence': n_sequence, 'n_channels': n_channels, 'path_dataset': path_dataset, 'option': 'RGBdiff', 'shuffle': True } train_txt = config['train']['file_list'] test_txt = config['valid']['file_list'] # Read file # train_d and test_d is dictionary that contain name of video as key and label as value # For example, {'a01\a01_s08_e01': 0, 'a01\a01_s08_e02': 0, .... } # It's used for getting label(Y) train_d = readfile_to_dict(train_txt) test_d = readfile_to_dict(test_txt) # Prepare key, name of video(X) train_keys = list(train_d.keys()) * n_mul_train test_keys = list(test_d.keys()) * n_mul_test # Generators training_generator = DataGenerator(train_keys, train_d, **params, type_gen='train') validation_generator = DataGenerator(test_keys, test_d, **params, type_gen='test') # define logs for tensorboard tensorboard = TensorBoard(log_dir='logs', histogram_freq=0) # train train_graph = tf.Graph() train_sess = tf.Session(graph=train_graph, config=tf_config) keras.backend.set_session(train_sess) with train_graph.as_default(): # Design model model = create_model(n_output) # model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['acc']) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc']) model.summary() start_epoch = 0 # Load weight of unfinish training model(optional) pretrained_weights = config['train'][ 'pretrained_weights'] # name of model if pretrained_weights != '': start_epoch = config['train']['start_epoch'] model.load_weights(pretrained_weights) # Set callback wgtdir = 'save_weight' if not os.path.exists(wgtdir): os.makedirs(wgtdir) validate_freq = 5 filepath = os.path.join( wgtdir, "weight-{epoch:02d}-{acc:.2f}-{val_acc:.2f}.hdf5") checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=False, period=validate_freq) callbacks_list = [checkpoint, tensorboard] # Train model on dataset model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=config['train']['nb_epochs'], callbacks=callbacks_list, initial_epoch=start_epoch, validation_freq=validate_freq)
n_output = 4 path_dataset = 'F:\\Master Project\\Dataset\\UCF-101-Temp-frames\\' # path_dataset = 'F:\\Master Project\\Dataset\\KARD-split-frames\\' params = { 'dim': dim, 'batch_size': 1, # 'n_classes': 6, 'n_sequence': n_sequence, 'n_channels': n_channels, 'path_dataset': path_dataset, 'shuffle': False } ## dataset test_d = readfile_to_dict("dataset_list/trainlistUCF.txt") labels = test_d.copy() partition = {'validation': list(test_d.keys())} # IDs validation_generator = DataGenerator(partition['validation'], labels, **params, type_gen='test') predict_generator = DataGenerator(partition['validation'], labels, **params, type_gen='predict') weights_path = 'pretrain/mobileNet-47-0.97.hdf5' # 15 frame model = create_model_pretrain(dim, n_sequence, n_channels, n_output, 'MobileNet') model.load_weights(weights_path)