def create_model(input_var, input_shape, options): conv_num_filters1 = 100 conv_num_filters2 = 150 conv_num_filters3 = 200 filter_size1 = 5 filter_size2 = 5 filter_size3 = 3 pool_size = 2 encode_size = options['BOTTLENECK'] dense_mid_size = options['DENSE'] pad_in = 'valid' pad_out = 'full' scaled_tanh = create_scaled_tanh() input = InputLayer(shape=input_shape, input_var=input_var, name='input') conv2d1 = Conv2DLayer(input, num_filters=conv_num_filters1, filter_size=filter_size1, pad=pad_in, name='conv2d1', nonlinearity=scaled_tanh) maxpool2d2 = MaxPool2DLayer(conv2d1, pool_size=pool_size, name='maxpool2d2') conv2d3 = Conv2DLayer(maxpool2d2, num_filters=conv_num_filters2, filter_size=filter_size2, pad=pad_in, name='conv2d3', nonlinearity=scaled_tanh) maxpool2d4 = MaxPool2DLayer(conv2d3, pool_size=pool_size, name='maxpool2d4', pad=(1,0)) conv2d5 = Conv2DLayer(maxpool2d4, num_filters=conv_num_filters3, filter_size=filter_size3, pad=pad_in, name='conv2d5', nonlinearity=scaled_tanh) reshape6 = ReshapeLayer(conv2d5, shape=([0], -1), name='reshape6') # 3000 reshape6_output = reshape6.output_shape[1] dense7 = DenseLayer(reshape6, num_units=dense_mid_size, name='dense7', nonlinearity=scaled_tanh) bottleneck = DenseLayer(dense7, num_units=encode_size, name='bottleneck', nonlinearity=linear) # print_network(bottleneck) dense8 = DenseLayer(bottleneck, num_units=dense_mid_size, W=bottleneck.W.T, name='dense8', nonlinearity=linear) dense9 = DenseLayer(dense8, num_units=reshape6_output, W=dense7.W.T, nonlinearity=scaled_tanh, name='dense9') reshape10 = ReshapeLayer(dense9, shape=([0], conv_num_filters3, 3, 5), name='reshape10') # 32 x 4 x 7 deconv2d11 = Deconv2DLayer(reshape10, conv2d5.input_shape[1], conv2d5.filter_size, stride=conv2d5.stride, W=conv2d5.W, flip_filters=not conv2d5.flip_filters, name='deconv2d11', nonlinearity=scaled_tanh) upscale2d12 = Upscale2DLayer(deconv2d11, scale_factor=pool_size, name='upscale2d12') deconv2d13 = Deconv2DLayer(upscale2d12, conv2d3.input_shape[1], conv2d3.filter_size, stride=conv2d3.stride, W=conv2d3.W, flip_filters=not conv2d3.flip_filters, name='deconv2d13', nonlinearity=scaled_tanh) upscale2d14 = Upscale2DLayer(deconv2d13, scale_factor=pool_size, name='upscale2d14') deconv2d15 = Deconv2DLayer(upscale2d14, conv2d1.input_shape[1], conv2d1.filter_size, stride=conv2d1.stride, crop=(1, 0), W=conv2d1.W, flip_filters=not conv2d1.flip_filters, name='deconv2d14', nonlinearity=scaled_tanh) reshape16 = ReshapeLayer(deconv2d15, ([0], -1), name='reshape16') print_network(reshape16) return reshape16
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('stream1')) print(config.items('stream2')) print(config.items('lstm_classifier')) print(config.items('training')) print('preprocessing dataset...') # stream 1 s1_data = load_mat_file(config.get('stream1', 'data')) s1_imagesize = tuple([int(d) for d in config.get('stream1', 'imagesize').split(',')]) s1 = config.get('stream1', 'model') s1_inputdim = config.getint('stream1', 'input_dimensions') s1_shape = config.get('stream1', 'shape') s1_nonlinearities = config.get('stream1', 'nonlinearities') # stream 2 s2_data = load_mat_file(config.get('stream2', 'data')) s2_imagesize = tuple([int(d) for d in config.get('stream2', 'imagesize').split(',')]) s2 = config.get('stream2', 'model') s2_inputdim = config.getint('stream2', 'input_dimensions') s2_shape = config.get('stream2', 'shape') s2_nonlinearities = config.get('stream2', 'nonlinearities') # lstm classifier fusiontype = config.get('lstm_classifier', 'fusiontype') weight_init = options['weight_init'] if 'weight_init' in options else config.get('lstm_classifier', 'weight_init') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('lstm_classifier', 'use_peepholes') output_classes = config.getint('lstm_classifier', 'output_classes') output_classnames = config.get('lstm_classifier', 'output_classnames').split(',') lstm_size = config.getint('lstm_classifier', 'lstm_size') matlab_target_offset = config.getboolean('lstm_classifier', 'matlab_target_offset') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int(options['num_epoch']) if 'num_epoch' in options else config.getint('training', 'num_epoch') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file(config.get('training', 'train_subjects_file')) val_subject_ids = read_data_split_file(config.get('training', 'val_subjects_file')) test_subject_ids = read_data_split_file(config.get('training', 'test_subjects_file')) s1_data_matrix = s1_data['dataMatrix'].astype('float32') s2_data_matrix = s2_data['dataMatrix'].astype('float32') targets_vec = s1_data['targetsVec'].reshape((-1,)) subjects_vec = s1_data['subjectsVec'].reshape((-1,)) vidlen_vec = s1_data['videoLengthVec'].reshape((-1,)) if matlab_target_offset: targets_vec -= 1 s1_data_matrix = presplit_dataprocessing(s1_data_matrix, vidlen_vec, config, 'stream1', imagesize=s1_imagesize) s2_data_matrix = presplit_dataprocessing(s2_data_matrix, vidlen_vec, config, 'stream2', imagesize=s2_imagesize) s1_train_X, s1_train_y, s1_train_vidlens, s1_train_subjects, \ s1_val_X, s1_val_y, s1_val_vidlens, s1_val_subjects, \ s1_test_X, s1_test_y, s1_test_vidlens, s1_test_subjects = split_seq_data(s1_data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) s2_train_X, s2_train_y, s2_train_vidlens, s2_train_subjects, \ s2_val_X, s2_val_y, s2_val_vidlens, s2_val_subjects, \ s2_test_X, s2_test_y, s2_test_vidlens, s2_test_subjects = split_seq_data(s2_data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) s1_train_X, s1_val_X, s1_test_X = postsplit_datapreprocessing(s1_train_X, s1_val_X, s1_test_X, config, 'stream1') s2_train_X, s2_val_X, s2_test_X = postsplit_datapreprocessing(s2_train_X, s2_val_X, s2_test_X, config, 'stream2') ae1 = load_decoder(s1, s1_shape, s1_nonlinearities) ae2 = load_decoder(s2, s2_shape, s2_nonlinearities) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) inputs1 = T.tensor3('inputs1', dtype='float32') inputs2 = T.tensor3('inputs2', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network, l_fuse = adenet_v2_nodelta.create_model(ae1, ae2, (None, None, s1_inputdim), inputs1, (None, None), mask, (None, None, s2_inputdim), inputs2, lstm_size, output_classes, fusiontype, w_init_fn=weight_init_fn, use_peepholes=use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function( [inputs1, targets, mask, inputs2], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs1, targets, mask, inputs2], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs1, targets, mask, inputs2], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs1, mask, inputs2], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(s1_train_X, s1_train_y, s1_train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(s1_train_vidlens) val_datagen = gen_lstm_batch_random(s1_val_X, s1_val_y, s1_val_vidlens, batchsize=len(s1_val_vidlens)) test_datagen = gen_lstm_batch_random(s1_test_X, s1_test_y, s1_test_vidlens, batchsize=len(s1_test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(s1_val_vidlens) X_diff_val = gen_seq_batch_from_idx(s2_val_X, idxs_val, s1_val_vidlens, integral_lens_val, np.max(s1_val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(s1_test_vidlens) X_diff_test = gen_seq_batch_from_idx(s2_test_X, idxs_test, s1_test_vidlens, integral_lens_test, np.max(s1_test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(s2_train_X, batch_idxs, s1_train_vidlens, integral_lens, np.max(s1_train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) best_params = las.layers.get_all_param_values(network) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) # plot confusion matrix table_str = plot_confusion_matrix(test_conf, output_classnames, fmt='pipe') print('confusion matrix: ') print(table_str) if 'save_plot' in options: prefix = options['save_plot'] plot_validation_cost(cost_train, cost_val, savefilename='{}.validloss.png'.format(prefix)) with open('{}.confmat.txt'.format(prefix), mode='a') as f: f.write(table_str) f.write('\n\n') if 'write_results' in options: print('writing results to {}'.format(options['write_results'])) results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val)) if 'save_best' in options: print('saving best model...') las.layers.set_all_param_values(network, best_params) save_model_params(network, options['save_best']) print('best model saved to {}'.format(options['save_best']))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_pretrained = config.get('models', 'pretrained') ae_pretrained_diff = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') lstm_size = config.getint('models', 'lstm_size') output_classes = config.getint('models', 'output_classes') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'models', 'use_peepholes') use_blstm = config.getboolean('models', 'use_blstm') delta_window = config.getint('models', 'delta_window') input_dimensions = config.getint('models', 'input_dimensions') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int( options['num_epoch']) if 'num_epoch' in options else config.getint( 'training', 'num_epoch') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'training', 'weight_init') use_finetuning = config.getboolean('training', 'use_finetuning') learning_rate = config.getfloat('training', 'learning_rate') batchsize = config.getint('training', 'batchsize') epochsize = config.getint('training', 'epochsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y), )) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects), )) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens, ))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) X_diff = compute_diff_images(X, video_lens) # mean remove dct features dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) train_subject_ids = read_data_split_file('data/train.txt') val_subject_ids = read_data_split_file('data/val.txt') test_subject_ids = read_data_split_file('data/test.txt') print('Train: {}'.format(train_subject_ids)) print('Validation: {}'.format(val_subject_ids)) print('Test: {}'.format(test_subject_ids)) train_X, train_y, train_dct, train_X_diff, train_vidlens, train_subjects, \ val_X, val_y, val_dct, val_X_diff, val_vidlens, val_subjects, \ test_X, test_y, test_dct, test_X_diff, test_vidlens, test_subjects = \ split_data(X, y, dct_feats, X_diff, subjects, video_lens, train_subject_ids, val_subject_ids, test_subject_ids) assert train_X.shape[0] + val_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + val_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + val_vidlens.shape[0] + test_vidlens.shape[ 0] == len(video_lens) assert train_subjects.shape[0] + val_vidlens.shape[ 0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) val_X = normalize_input(val_X, centralize=True) test_X = normalize_input(test_X, centralize=True) print('loading pretrained encoder: {}...'.format(ae_pretrained)) ae = load_dbn(ae_pretrained) print('loading pretrained encoder: {}...'.format(ae_pretrained_diff)) ae_diff = load_dbn(ae_pretrained_diff) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') if use_blstm: network, l_fuse = adenet_v2_2.create_model( ae, ae_diff, (None, None, input_dimensions), inputs, (None, None), mask, (None, None, input_dimensions), inputs_diff, lstm_size, window, output_classes, fusiontype, weight_init_fn, use_peepholes) else: network, l_fuse = adenet_v2_4.create_model( ae, ae_diff, (None, None, input_dimensions), inputs, (None, None), mask, (None, None, input_dimensions), inputs_diff, lstm_size, window, output_classes, fusiontype, weight_init_fn, use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function([inputs, targets, mask, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [inputs, targets, mask, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs, targets, mask, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, delta_window) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, delta_window) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, delta_window) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, delta_window, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values( l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, delta_window, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break phrases = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, phrases, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{}\n'.format(validation_window, weight_init, use_peepholes, use_blstm, use_finetuning)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s)) f.write('{},{},{}\n'.format(fusiontype, best_cr, best_val))
def main(): configure_theano() options = parse_options() config_file = 'config/leave_one_out.ini' print('loading config file: {}'.format(config_file)) config = ConfigParser.ConfigParser() config.read(config_file) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_pretrained = config.get('models', 'pretrained') ae_finetuned = config.get('models', 'finetuned') ae_finetuned_diff = config.get('models', 'finetuned_diff') learning_rate = float(config.get('training', 'learning_rate')) decay_rate = float(config.get('training', 'decay_rate')) decay_start = int(config.get('training', 'decay_start')) do_finetune = config.getboolean('training', 'do_finetune') save_finetune = config.getboolean('training', 'save_finetune') load_finetune = config.getboolean('training', 'load_finetune') load_finetune_diff = config.getboolean('training', 'load_finetune_diff') # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y), )) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects), )) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens, ))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) X_diff = compute_diff_images(X, video_lens) # mean remove dct features dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) test_subject_ids = [options['test_subj']] train_subject_ids = range(1, 54) for subj in test_subject_ids: train_subject_ids.remove(subj) if 'results' in options: results_file = options['results'] f = open(results_file, mode='a') print(train_subject_ids) print(test_subject_ids) train_X, train_y, train_dct, train_X_diff, train_vidlens, train_subjects, \ test_X, test_y, test_dct, test_X_diff, test_vidlens, test_subjects = \ split_data(X, y, dct_feats, X_diff, subjects, video_lens, train_subject_ids, test_subject_ids) assert train_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + test_vidlens.shape[0] == len(video_lens) assert train_subjects.shape[0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) test_X = normalize_input(test_X, centralize=True) # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) test_dct = (test_dct - dct_mean) / dct_std if do_finetune: print('performing finetuning on pretrained encoder: {}'.format( ae_pretrained)) ae = load_dbn(ae_pretrained) ae.initialize() ae.fit(train_X, train_X) if save_finetune: print('saving finetuned encoder: {}...'.format(ae_finetuned)) pickle.dump(ae, open(ae_finetuned, 'wb')) if load_finetune: print('loading finetuned encoder: {}...'.format(ae_finetuned)) ae = pickle.load(open(ae_finetuned, 'rb')) ae.initialize() if load_finetune_diff: print('loading finetuned encoder: {}...'.format(ae_finetuned_diff)) ae_diff = pickle.load(open(ae_finetuned_diff, 'rb')) ae_diff.initialize() # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') dct = T.tensor3('dct', dtype='float32') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') lr = theano.shared(np.array(learning_rate, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(decay_rate, dtype=theano.config.floatX) print('constructing end to end model...') ''' network = create_end_to_end_model(dbn, (None, None, 1144), inputs, (None, None), mask, 250, window) ''' network = adenet_v5.create_model(ae, ae_diff, (None, None, 1144), inputs, (None, None), mask, (None, None, 90), dct, (None, None, 1144), inputs_diff, 250, window, 10) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(las.objectives.categorical_crossentropy( predictions, targets)) updates = adadelta(cost, all_params, learning_rate=lr) # updates = adagrad(cost, all_params, learning_rate=lr) use_max_constraint = False if use_max_constraint: MAX_NORM = 4 for param in las.layers.get_all_params(network, regularizable=True): if param.ndim > 1: # only apply to dimensions larger than 1, exclude biases updates[param] = norm_constraint( param, MAX_NORM * las.utils.compute_norms(param.get_value()).mean()) train = theano.function([inputs, targets, mask, dct, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean( las.objectives.categorical_crossentropy(test_predictions, targets)) compute_test_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, dct, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] NUM_EPOCHS = 10 EPOCH_SIZE = 120 BATCH_SIZE = 10 WINDOW_SIZE = 9 STRIP_SIZE = 3 MAX_LOSS = 0.2 VALIDATION_WINDOW = 4 val_window = circular_list(VALIDATION_WINDOW) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_conf = None best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) val_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(test_vidlens) dct_val = gen_seq_batch_from_idx(test_dct, idxs_val, test_vidlens, integral_lens_val, np.max(test_vidlens)) X_diff_val = gen_seq_batch_from_idx(test_X_diff, idxs_val, test_vidlens, integral_lens_val, np.max(test_vidlens)) def early_stop(cost_window): if len(cost_window) < 2: return False else: curr = cost_window[0] for idx, cost in enumerate(cost_window): if curr < cost or idx == 0: curr = cost else: return False return True for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), float(lr.get_value())) print(print_str, end='') sys.stdout.flush() train(X, y, m, d, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, d, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) print( "Epoch {} train cost = {}, validation cost = {}, " "generalization loss = {:.3f}, GQ = {:.3f}, classification rate = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if val_cost < best_val: best_val = val_cost best_conf = val_conf best_cr = cr if epoch >= VALIDATION_WINDOW and early_stop(val_window): break # learning rate decay if epoch >= decay_start - 1: lr.set_value(lr.get_value() * lr_decay) phrases = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'] print('Final Model') print('classification rate: {}, validation loss: {}'.format( best_cr, best_val)) print('confusion matrix: ') plot_confusion_matrix(best_conf, phrases, fmt='grid') plot_validation_cost(cost_train, cost_val, class_rate, savefilename='valid_cost') if 'results' in options: print('writing to results file: {}...'.format(options['results'])) f.write('{}, {}, {}\n'.format(test_subject_ids[0], best_cr, best_val)) f.close()
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_pretrained = config.get('models', 'pretrained') ae_finetuned = config.get('models', 'finetuned') ae_finetuned_diff = config.get('models', 'finetuned_diff') fusiontype = config.get('models', 'fusiontype') learning_rate = float(config.get('training', 'learning_rate')) decay_rate = float(config.get('training', 'decay_rate')) decay_start = int(config.get('training', 'decay_start')) do_finetune = config.getboolean('training', 'do_finetune') save_finetune = config.getboolean('training', 'save_finetune') load_finetune = config.getboolean('training', 'load_finetune') load_finetune_diff = config.getboolean('training', 'load_finetune_diff') savemodel = config.getboolean('training', 'savemodel') t = data['filenamesVec'] target_filenames = list() for r in t: for j in r: target_filenames.append(str(j[0])) # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y), )) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects), )) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens, ))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) X_diff = compute_diff_images(X, video_lens) # mean remove dct features dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) train_subject_ids = read_data_split_file('data/train.txt') val_subject_ids = read_data_split_file('data/val.txt') test_subject_ids = read_data_split_file('data/test.txt') print('Train: {}'.format(train_subject_ids)) print('Validation: {}'.format(val_subject_ids)) print('Test: {}'.format(test_subject_ids)) train_X, train_y, train_dct, train_X_diff, train_vidlens, train_subjects, train_filenames, \ val_X, val_y, val_dct, val_X_diff, val_vidlens, val_subjects, val_filenames, \ test_X, test_y, test_dct, test_X_diff, test_vidlens, test_subjects, test_filenames = \ split_data(X, y, dct_feats, X_diff, subjects, video_lens, train_subject_ids, val_subject_ids, test_subject_ids, target_filenames) assert train_X.shape[0] + val_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + val_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + val_vidlens.shape[0] + test_vidlens.shape[ 0] == len(video_lens) assert train_subjects.shape[0] + val_vidlens.shape[ 0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) val_X = normalize_input(val_X, centralize=True) test_X = normalize_input(test_X, centralize=True) # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) if load_finetune: print('loading finetuned encoder: {}...'.format(ae_finetuned)) ae = pickle.load(open(ae_finetuned, 'rb')) ae.initialize() if load_finetune_diff: print('loading finetuned encoder: {}...'.format(ae_finetuned_diff)) ae_diff = pickle.load(open(ae_finetuned_diff, 'rb')) ae_diff.initialize() window = T.iscalar('theta') dct = T.tensor3('dct', dtype='float32') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') print('loading end to end model...') network, l_fuse = adenet_v3.create_model(ae, ae_diff, (None, None, 1144), inputs, (None, None), mask, (None, None, 90), dct, (None, None, 1144), inputs_diff, 250, window, 10, fusiontype) all_param_values = load_model('models/3stream.dat') all_params = las.layers.get_all_params(network) for p, v in zip(all_params, all_param_values): p.set_value(v) print_network(network) print('compiling model...') ''' train = theano.function( [inputs, targets, mask, dct, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask, dct, inputs_diff, window], cost, allow_input_downcast=True) ''' test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean( las.objectives.categorical_crossentropy(test_predictions, targets)) val_fn = theano.function([inputs, mask, dct, inputs_diff, window], test_predictions, allow_input_downcast=True) datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=10) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) raw_input("Press Enter to start demo...") for idx in range(len(X_test)): videofile = '../examples/data/{}'.format( test_filenames[idxs_test[idx]]) print('video file: {}'.format(videofile)) cap = cv2.VideoCapture(videofile) while cap.isOpened(): ret, frame = cap.read() if ret: frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame', gray) else: break if cv2.waitKey(1) & 0xFF == ord('q'): break pred = val_fn(X_test[idx:idx + 1], mask_test[idx:idx + 1], dct_test[idx:idx + 1], X_diff_test[idx:idx + 1], 9) pred_idx = np.argmax(pred, axis=1) print('Prediction: {}, Target: {}'.format(get_phrase(pred_idx[0]), get_phrase(y_test[idx]))) raw_input("Press Enter to continue...") cap.release() cv2.destroyAllWindows()
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_finetuned = config.get('models', 'finetuned') ae_finetuned_diff = config.get('models', 'finetuned_diff') fusiontype = config.get('models', 'fusiontype') learning_rate = float(config.get('training', 'learning_rate')) decay_rate = float(config.get('training', 'decay_rate')) decay_start = int(config.get('training', 'decay_start')) load_finetune = config.getboolean('training', 'load_finetune') load_finetune_diff = config.getboolean('training', 'load_finetune_diff') train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1, )) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1, )) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1, )) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_dct = dct_data['trDctFeatures'].astype('float32') val_dct = dct_data['valDctFeatures'].astype('float32') test_dct = dct_data['testDctFeatures'].astype('float32') train_X_diff = compute_diff_images(train_X, train_vidlens) val_X_diff = compute_diff_images(val_X, val_vidlens) test_X_diff = compute_diff_images(test_X, test_vidlens) train_y = data['trTargetsVec'].astype('int').reshape( (-1, )) + 1 # +1 to handle the -1 introduced in lstm_gendata val_y = data['valTargetsVec'].astype('int').reshape((-1, )) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1, )) + 1 # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std if load_finetune: print('loading finetuned encoder: {}...'.format(ae_finetuned)) ae = pickle.load(open(ae_finetuned, 'rb')) ae.initialize() if load_finetune_diff: print('loading finetuned encoder: {}...'.format(ae_finetuned_diff)) ae_diff = pickle.load(open(ae_finetuned_diff, 'rb')) ae_diff.initialize() # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') dct = T.tensor3('dct', dtype='float32') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') lr = theano.shared(np.array(learning_rate, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(decay_rate, dtype=theano.config.floatX) print('constructing end to end model...') network, l_fuse = adenet_v3.create_model(ae, ae_diff, (None, None, 1500), inputs, (None, None), mask, (None, None, 90), dct, (None, None, 1500), inputs_diff, 250, window, 10, fusiontype) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(las.objectives.categorical_crossentropy( predictions, targets)) updates = adadelta(cost, all_params, learning_rate=lr) # updates = adagrad(cost, all_params, learning_rate=lr) train = theano.function([inputs, targets, mask, dct, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean( las.objectives.categorical_crossentropy(test_predictions, targets)) compute_test_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, dct, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] NUM_EPOCHS = 30 EPOCH_SIZE = 45 BATCH_SIZE = 20 WINDOW_SIZE = 9 STRIP_SIZE = 3 MAX_LOSS = 0.2 VALIDATION_WINDOW = 4 val_window = circular_list(VALIDATION_WINDOW) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_conf = None best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) def early_stop(cost_window): if len(cost_window) < 2: return False else: curr = cost_window[0] for idx, cost in enumerate(cost_window): if curr < cost or idx == 0: curr = cost else: return False return True for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), float(lr.get_value())) print(print_str, end='') sys.stdout.flush() train(X, y, m, d, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, d, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values( l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model(X_test, y_test, mask_test, dct_test, X_diff_test, WINDOW_SIZE, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= VALIDATION_WINDOW and early_stop(val_window): break # learning rate decay if epoch + 1 >= decay_start: lr.set_value(lr.get_value() * lr_decay) numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if options['write_results']: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(fusiontype, test_cr, best_val))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('stream1')) print(config.items('stream2')) print(config.items('stream3')) print(config.items('lstm_classifier')) print(config.items('training')) print('preprocessing dataset...') # stream 1 s1_data = load_mat_file(config.get('stream1', 'data')) s1_imagesize = tuple( [int(d) for d in config.get('stream1', 'imagesize').split(',')]) s1 = config.get('stream1', 'model') s1_inputdim = config.getint('stream1', 'input_dimensions') s1_shape = config.get('stream1', 'shape') s1_nonlinearities = config.get('stream1', 'nonlinearities') # stream 2 s2_data = load_mat_file(config.get('stream2', 'data')) s2_imagesize = tuple( [int(d) for d in config.get('stream2', 'imagesize').split(',')]) s2 = config.get('stream2', 'model') s2_inputdim = config.getint('stream2', 'input_dimensions') s2_shape = config.get('stream2', 'shape') s2_nonlinearities = config.get('stream2', 'nonlinearities') # stream 3 s3_data = load_mat_file(config.get('stream3', 'data')) s3_imagesize = tuple( [int(d) for d in config.get('stream3', 'imagesize').split(',')]) s3 = config.get('stream3', 'model') s3_inputdim = config.getint('stream3', 'input_dimensions') s3_shape = config.get('stream3', 'shape') s3_nonlinearities = config.get('stream3', 'nonlinearities') # lstm classifier fusiontype = config.get('lstm_classifier', 'fusiontype') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'lstm_classifier', 'weight_init') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'lstm_classifier', 'use_peepholes') windowsize = config.getint('lstm_classifier', 'windowsize') output_classes = config.getint('lstm_classifier', 'output_classes') output_classnames = config.get('lstm_classifier', 'output_classnames').split(',') lstm_size = config.getint('lstm_classifier', 'lstm_size') matlab_target_offset = config.getboolean('lstm_classifier', 'matlab_target_offset') use_dropout = config.getboolean('lstm_classifier', 'use_dropout') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int( options['num_epoch']) if 'num_epoch' in options else config.getint( 'training', 'num_epoch') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file( config.get('training', 'train_subjects_file')) val_subject_ids = read_data_split_file( config.get('training', 'val_subjects_file')) test_subject_ids = read_data_split_file( config.get('training', 'test_subjects_file')) s1_data_matrix = s1_data['dataMatrix'].astype('float32') s2_data_matrix = s2_data['dataMatrix'].astype('float32') s3_data_matrix = s3_data['dataMatrix'].astype('float32') targets_vec = s1_data['targetsVec'].reshape((-1, )) subjects_vec = s1_data['subjectsVec'].reshape((-1, )) vidlen_vec = s1_data['videoLengthVec'].reshape((-1, )) force_align_data = config.getboolean('stream1', 'force_align_data') if matlab_target_offset: targets_vec -= 1 s1_data_matrix = presplit_dataprocessing(s1_data_matrix, vidlen_vec, config, 'stream1', imagesize=s1_imagesize) s2_data_matrix = presplit_dataprocessing(s2_data_matrix, vidlen_vec, config, 'stream2', imagesize=s2_imagesize) s3_data_matrix = presplit_dataprocessing(s3_data_matrix, vidlen_vec, config, 'stream3', imagesize=s3_imagesize) if force_align_data: s2_targets_vec = s2_data['targetsVec'].reshape((-1, )) s2_vidlen_vec = s2_data['videoLengthVec'].reshape((-1, )) s3_targets_vec = s3_data['targetsVec'].reshape((-1, )) s3_vidlen_vec = s3_data['videoLengthVec'].reshape((-1, )) orig_streams = [ (s1_data_matrix, targets_vec, vidlen_vec), (s2_data_matrix, s2_targets_vec, s2_vidlen_vec), (s3_data_matrix, s3_targets_vec, s3_vidlen_vec), ] new_streams = multistream_force_align(orig_streams) s1_data_matrix, targets_vec, vidlen_vec = new_streams[0] s2_data_matrix, _, _ = new_streams[1] s3_data_matrix, _, _ = new_streams[2] s1_train_X, s1_train_y, s1_train_vidlens, s1_train_subjects, \ s1_val_X, s1_val_y, s1_val_vidlens, s1_val_subjects, \ s1_test_X, s1_test_y, s1_test_vidlens, s1_test_subjects = split_seq_data(s1_data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) s2_train_X, s2_train_y, s2_train_vidlens, s2_train_subjects, \ s2_val_X, s2_val_y, s2_val_vidlens, s2_val_subjects, \ s2_test_X, s2_test_y, s2_test_vidlens, s2_test_subjects = split_seq_data(s2_data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) s3_train_X, s3_train_y, s3_train_vidlens, s3_train_subjects, \ s3_val_X, s3_val_y, s3_val_vidlens, s3_val_subjects, \ s3_test_X, s3_test_y, s3_test_vidlens, s3_test_subjects = split_seq_data(s3_data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) s1_train_X, s1_val_X, s1_test_X = postsplit_datapreprocessing( s1_train_X, s1_val_X, s1_test_X, config, 'stream1') s2_train_X, s2_val_X, s2_test_X = postsplit_datapreprocessing( s2_train_X, s2_val_X, s2_test_X, config, 'stream2') s3_train_X, s3_val_X, s3_test_X = postsplit_datapreprocessing( s3_train_X, s3_val_X, s3_test_X, config, 'stream3') ae1 = load_decoder(s1, s1_shape, s1_nonlinearities) ae2 = load_decoder(s2, s2_shape, s2_nonlinearities) ae3 = load_decoder(s3, s3_shape, s3_nonlinearities) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs1 = T.tensor3('inputs1', dtype='float32') inputs2 = T.tensor3('inputs2', dtype='float32') inputs3 = T.tensor3('inputs3', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') if use_dropout: network, l_fuse = adenet_3stream_dropout.create_model( ae1, ae2, ae3, (None, None, s1_inputdim), inputs1, (None, None, s2_inputdim), inputs2, (None, None, s3_inputdim), inputs3, (None, None), mask, lstm_size, window, output_classes, fusiontype, w_init_fn=weight_init_fn, use_peepholes=use_peepholes) else: network, l_fuse = adenet_3stream.create_model( ae1, ae2, ae3, (None, None, s1_inputdim), inputs1, (None, None, s2_inputdim), inputs2, (None, None, s3_inputdim), inputs3, (None, None), mask, lstm_size, window, output_classes, fusiontype, w_init_fn=weight_init_fn, use_peepholes=use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function([inputs1, inputs2, inputs3, targets, mask, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [inputs1, inputs2, inputs3, targets, mask, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs1, inputs2, inputs3, targets, mask, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs1, inputs2, inputs3, mask, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(s1_train_X, s1_train_y, s1_train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(s1_train_vidlens) val_datagen = gen_lstm_batch_random(s1_val_X, s1_val_y, s1_val_vidlens, batchsize=len(s1_val_vidlens)) test_datagen = gen_lstm_batch_random(s1_test_X, s1_test_y, s1_test_vidlens, batchsize=len(s1_test_vidlens)) # We'll use this "validation set" to periodically check progress X_s1_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(s1_val_vidlens) X_s2_val = gen_seq_batch_from_idx(s2_val_X, idxs_val, s1_val_vidlens, integral_lens_val, np.max(s1_val_vidlens)) X_s3_val = gen_seq_batch_from_idx(s3_val_X, idxs_val, s1_val_vidlens, integral_lens_val, np.max(s1_val_vidlens)) # we use the test set to check final classification rate X_s1_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(s1_test_vidlens) X_s2_test = gen_seq_batch_from_idx(s2_test_X, idxs_test, s1_test_vidlens, integral_lens_test, np.max(s1_test_vidlens)) X_s3_test = gen_seq_batch_from_idx(s3_test_X, idxs_test, s1_test_vidlens, integral_lens_test, np.max(s1_test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X_s1, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_s2 = gen_seq_batch_from_idx(s2_train_X, batch_idxs, s1_train_vidlens, integral_lens, np.max(s1_train_vidlens)) X_s3 = gen_seq_batch_from_idx(s3_train_X, batch_idxs, s1_train_vidlens, integral_lens, np.max(s1_train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam with learning rate = {}'.format( epoch + 1, i + 1, epochsize, len(X_s1), learning_rate) print(print_str, end='') sys.stdout.flush() train(X_s1, X_s2, X_s3, y, m, windowsize) print('\r', end='') cost = compute_train_cost(X_s1, X_s2, X_s3, y, m, windowsize) val_cost = compute_test_cost(X_s1_val, X_s2_val, X_s3_val, y_val, mask_val, windowsize) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_s1_val, X_s2_val, X_s3_val, y_val_evaluate, mask_val, windowsize, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr test_cr, test_conf = evaluate_model2(X_s1_test, X_s2_test, X_s3_test, y_test, mask_test, windowsize, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) best_params = las.layers.get_all_param_values(network) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) # plot confusion matrix table_str = plot_confusion_matrix(test_conf, output_classnames, fmt='pipe') print('confusion matrix: ') print(table_str) if 'save_plot' in options: prefix = options['save_plot'] plot_validation_cost(cost_train, cost_val, savefilename='{}.validloss.png'.format(prefix)) with open('{}.confmat.txt'.format(prefix), mode='a') as f: f.write(table_str) f.write('\n\n') if 'write_results' in options: print('writing results to {}'.format(options['write_results'])) results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val)) if 'save_best' in options: print('saving best model...') las.layers.set_all_param_values(network, best_params) save_model_params(network, options['save_best']) print('best model saved to {}'.format(options['save_best']))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('Reading Config File: {}...'.format(config_file)) print(config.items('stream1')) print(config.items('lstm_classifier')) print(config.items('training')) print('CLI options: {}'.format(options.items())) print('preprocessing dataset...') data = load_mat_file(config.get('stream1', 'data')) stream1_dim = config.getint('stream1', 'input_dimensions') output_classes = config.getint('lstm_classifier', 'output_classes') output_classnames = config.get('lstm_classifier', 'output_classnames').split(',') matlab_target_offset = config.getboolean('lstm_classifier', 'matlab_target_offset') lstm_size = config.getint('lstm_classifier', 'lstm_size') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'lstm_classifier', 'weight_init') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'lstm_classifier', 'use_peepholes') windowsize = config.getint('lstm_classifier', 'windowsize') # data preprocessing options meanremove = config.getboolean('stream1', 'meanremove') samplewisenormalize = config.getboolean('stream1', 'samplewisenormalize') featurewisenormalize = config.getboolean('stream1', 'featurewisenormalize') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int( options['num_epoch']) if 'num_epoch' in options else config.getint( 'training', 'num_epoch') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file( config.get('training', 'train_subjects_file')) val_subject_ids = read_data_split_file( config.get('training', 'val_subjects_file')) test_subject_ids = read_data_split_file( config.get('training', 'test_subjects_file')) data_matrix = data['dataMatrix'].astype('float32') targets_vec = data['targetsVec'].reshape((-1, )) subjects_vec = data['subjectsVec'].reshape((-1, )) vidlen_vec = data['videoLengthVec'].reshape((-1, )) if samplewisenormalize: data_matrix = normalize_input(data_matrix) if meanremove: data_matrix = sequencewise_mean_image_subtraction( data_matrix, vidlen_vec) data_matrix = concat_first_second_deltas(data_matrix, vidlen_vec, windowsize) train_dct, train_y, train_vidlens, train_subjects, \ val_dct, val_y, val_vidlens, val_subjects, \ test_dct, test_y, test_vidlens, test_subjects = split_seq_data(data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) if matlab_target_offset: train_y -= 1 val_y -= 1 test_y -= 1 # featurewise normalize dct features if featurewisenormalize: train_dct, dct_mean, dct_std = featurewise_normalize_sequence( train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) inputs = T.tensor3('inputs', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network = lstm_classifier_majority_vote.create_model( (None, None, stream1_dim * 3), inputs, (None, None), mask, lstm_size, output_classes, weight_init_fn, use_peepholes) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function([inputs, targets, mask], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function([inputs, targets, mask], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_dct, train_y, train_vidlens, batchsize=batchsize) val_datagen = gen_lstm_batch_random(val_dct, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_dct, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): _, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam with learning rate = {}'.format( epoch + 1, i + 1, epochsize, len(y), learning_rate) print(print_str, end='') sys.stdout.flush() train(d, y, m) print('\r', end='') cost = compute_train_cost(d, y, m) val_cost = compute_test_cost(dct_val, y_val, mask_val) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(dct_val, y_val_evaluate, mask_val, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_conf = val_conf best_cr = cr test_cr, test_conf = evaluate_model2(dct_test, y_test, mask_test, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) best_params = las.layers.get_all_param_values(network) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) # plot confusion matrix table_str = plot_confusion_matrix(test_conf, output_classnames, fmt='pipe') print('confusion matrix: ') print(table_str) if 'save_plot' in options: prefix = options['save_plot'] plot_validation_cost(cost_train, cost_val, savefilename='{}.validloss.png'.format(prefix)) with open('{}.confmat.txt'.format(prefix), mode='a') as f: f.write(table_str) f.write('\n\n') if 'write_results' in options: print('writing results to {}'.format(options['write_results'])) results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val)) if 'save_best' in options: print('saving best model...') las.layers.set_all_param_values(network, best_params) save_model_params(network, options['save_best']) print('best model saved to {}'.format(options['save_best']))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_finetuned = config.get('models', 'finetuned') ae_finetuned_diff = config.get('models', 'finetuned_diff') fusiontype = config.get('models', 'fusiontype') learning_rate = float(config.get('training', 'learning_rate')) decay_rate = float(config.get('training', 'decay_rate')) decay_start = int(config.get('training', 'decay_start')) load_finetune = config.getboolean('training', 'load_finetune') load_finetune_diff = config.getboolean('training', 'load_finetune_diff') train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1,)) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1,)) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1,)) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_dct = dct_data['trDctFeatures'].astype('float32') val_dct = dct_data['valDctFeatures'].astype('float32') test_dct = dct_data['testDctFeatures'].astype('float32') train_X_diff = compute_diff_images(train_X, train_vidlens) val_X_diff = compute_diff_images(val_X, val_vidlens) test_X_diff = compute_diff_images(test_X, test_vidlens) train_y = data['trTargetsVec'].astype('int').reshape((-1,)) + 1 # +1 to handle the -1 introduced in lstm_gendata val_y = data['valTargetsVec'].astype('int').reshape((-1,)) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1,)) + 1 # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std if load_finetune: print('loading finetuned encoder: {}...'.format(ae_finetuned)) ae = pickle.load(open(ae_finetuned, 'rb')) ae.initialize() if load_finetune_diff: print('loading finetuned encoder: {}...'.format(ae_finetuned_diff)) ae_diff = pickle.load(open(ae_finetuned_diff, 'rb')) ae_diff.initialize() # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') dct = T.tensor3('dct', dtype='float32') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') lr = theano.shared(np.array(learning_rate, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(decay_rate, dtype=theano.config.floatX) print('constructing end to end model...') network, l_fuse = adenet_v3.create_model(ae, ae_diff, (None, None, 1500), inputs, (None, None), mask, (None, None, 90), dct, (None, None, 1500), inputs_diff, 250, window, 10, fusiontype) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(las.objectives.categorical_crossentropy(predictions, targets)) updates = adadelta(cost, all_params, learning_rate=lr) # updates = adagrad(cost, all_params, learning_rate=lr) train = theano.function( [inputs, targets, mask, dct, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask, dct, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean(las.objectives.categorical_crossentropy(test_predictions, targets)) compute_test_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, dct, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] NUM_EPOCHS = 30 EPOCH_SIZE = 45 BATCH_SIZE = 20 WINDOW_SIZE = 9 STRIP_SIZE = 3 MAX_LOSS = 0.2 VALIDATION_WINDOW = 4 val_window = circular_list(VALIDATION_WINDOW) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_conf = None best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) def early_stop(cost_window): if len(cost_window) < 2: return False else: curr = cost_window[0] for idx, cost in enumerate(cost_window): if curr < cost or idx == 0: curr = cost else: return False return True for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), float(lr.get_value())) print(print_str, end='') sys.stdout.flush() train(X, y, m, d, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, d, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values(l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model(X_test, y_test, mask_test, dct_test, X_diff_test, WINDOW_SIZE, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= VALIDATION_WINDOW and early_stop(val_window): break # learning rate decay if epoch + 1 >= decay_start: lr.set_value(lr.get_value() * lr_decay) numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if options['write_results']: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(fusiontype, test_cr, best_val))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(options['dct_data'] if 'dct_data' in options else config.get('data', 'dct')) no_coeff = options['no_coeff'] if 'no_coeff' in options else config.getint('models', 'no_coeff') no_epochs = options['no_epochs'] if 'no_epochs' in options else config.getint('training', 'no_epochs') validation_window = options['validation_window'] if 'validation_window' in options \ else config.getint('training', 'validation_window') epochsize = options['epochsize'] if 'epochsize' in options else config.getint('training', 'epochsize') batchsize = options['batchsize'] if 'batchsize' in options else config.getint('training', 'batchsize') # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y),)) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects),)) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens,))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) # mean remove dct features # dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) train_subject_ids = read_data_split_file('data/train.txt') val_subject_ids = read_data_split_file('data/val.txt') test_subject_ids = read_data_split_file('data/test.txt') print('Train: {}'.format(train_subject_ids)) print('Validation: {}'.format(val_subject_ids)) print('Test: {}'.format(test_subject_ids)) train_X, train_y, train_dct, train_vidlens, train_subjects, \ val_X, val_y, val_dct, val_vidlens, val_subjects, \ test_X, test_y, test_dct, test_vidlens, test_subjects = \ split_data(X, y, dct_feats, subjects, video_lens, train_subject_ids, val_subject_ids, test_subject_ids) assert train_X.shape[0] + val_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + val_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + val_vidlens.shape[0] + test_vidlens.shape[0] == len(video_lens) assert train_subjects.shape[0] + val_subjects.shape[0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) val_X = normalize_input(val_X, centralize=True) test_X = normalize_input(test_X, centralize=True) # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) inputs = T.tensor3('inputs', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') print('constructing end to end model...') network = lstm_classifier_baseline.create_model((None, None, no_coeff*3), inputs, (None, None), mask, 250, 10) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(las.objectives.categorical_crossentropy(predictions, targets)) updates = adam(cost, all_params) train = theano.function( [inputs, targets, mask], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean(las.objectives.categorical_crossentropy(test_predictions, targets)) compute_test_cost = theano.function( [inputs, targets, mask], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) for epoch in range(no_epochs): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(d, y, m) print('\r', end='') cost = compute_train_cost(d, y, m) val_cost = compute_test_cost(dct_val, y_val, mask_val) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model(dct_val, y_val, mask_val, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr test_cr, test_conf = evaluate_model(dct_test, y_test, mask_test, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break phrases = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) print('confusion matrix: ') plot_confusion_matrix(test_conf, phrases, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{}\n'.format(test_cr, best_val))
def main(): def signal_handler(signal, frame): global terminate terminate = True print('terminating...'.format(terminate)) signal.signal(signal.SIGINT, signal_handler) configure_theano() options = parse_options() X, X_val = generate_data() # X = np.reshape(X, (-1, 1, 30, 40))[:-5] print('X type and shape:', X.dtype, X.shape) print('X.min():', X.min()) print('X.max():', X.max()) # X_val = np.reshape(X_val, (-1, 1, 30, 40))[:-1] print('X_val type and shape:', X_val.dtype, X_val.shape) print('X_val.min():', X_val.min()) print('X_val.max():', X_val.max()) # we need our target to be 1 dimensional X_out = X.reshape((X.shape[0], -1)) X_val_out = X_val.reshape((X_val.shape[0], -1)) print('X_out:', X_out.dtype, X_out.shape) print('X_val_out', X_val_out.dtype, X_val_out.shape) # X_noisy = apply_gaussian_noise(X_out) # visualize_reconstruction(X_noisy[0:25], X_out[0:25], shape=(28, 28)) # X = np.reshape(X_noisy, (-1, 1, 28, 28)) print('constructing and compiling model...') # input_var = T.tensor4('input', dtype='float32') input_var = T.tensor3('input', dtype='float32') target_var = T.matrix('output', dtype='float32') lr = theano.shared(np.array(0.8, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(0.9, dtype=theano.config.floatX) # try building a reshaping layer # network = create_model(input_var, (None, 1, 30, 40), options) l_input = InputLayer((None, None, 1200), input_var, name='input') l_input = ReshapeLayer(l_input, (-1, 1, 30, 40), name='reshape_input') # l_input = InputLayer((None, 1, 30, 40), input_var, name='input') if options['MODEL'] == 'normal': network, encoder = avletters_convae.create_model(l_input, options) if options['MODEL'] == 'batchnorm': network, encoder = avletters_convae_bn.create_model(l_input, options) if options['MODEL'] == 'dropout': network, encoder = avletters_convae_drop.create_model(l_input, options) if options['MODEL'] == 'bn+dropout': network, encoder = avletters_convae_bndrop.create_model(l_input, options) print('AE Network architecture: {}'.format(options['MODEL'])) print_network(network) recon = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(squared_error(recon, target_var)) updates = adadelta(cost, all_params, lr) # updates = las.updates.apply_nesterov_momentum(updates, all_params, momentum=0.90) use_max_constraint = False print('apply max norm constraint: {}'.format(use_max_constraint)) if use_max_constraint: MAX_NORM = 4 for param in las.layers.get_all_params(network, regularizable=True): if param.ndim > 1: # only apply to dimensions larger than 1, exclude biases # updates[param] = norm_constraint(param, MAX_NORM * las.utils.compute_norms(param.get_value()).mean()) updates[param] = norm_constraint(param, MAX_NORM) train = theano.function([input_var, target_var], recon, updates=updates, allow_input_downcast=True) train_cost_fn = theano.function([input_var, target_var], cost, allow_input_downcast=True) eval_recon = las.layers.get_output(network, deterministic=True) eval_cost = T.mean(las.objectives.squared_error(eval_recon, target_var)) eval_cost_fn = theano.function([input_var, target_var], eval_cost, allow_input_downcast=True) recon_fn = theano.function([input_var], eval_recon, allow_input_downcast=True) if terminate: exit() NUM_EPOCHS = options['NUM_EPOCHS'] EPOCH_SIZE = options['EPOCH_SIZE'] NO_STRIDES = options['NO_STRIDES'] VAL_NO_STRIDES = options['VAL_NO_STRIDES'] print('begin training for {} epochs...'.format(NUM_EPOCHS)) datagen = batch_iterator(X, X_out, 128) costs = [] val_costs = [] for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): batch_X, batch_y = next(datagen) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(batch_X), lr.get_value()) print(print_str, end='') sys.stdout.flush() batch_X = batch_X.reshape((-1, 1, 1200)) train(batch_X, batch_y) print('\r', end='') if terminate: break if terminate: break cost = batch_compute_cost(X, X_out, NO_STRIDES, train_cost_fn) val_cost = batch_compute_cost(X_val, X_val_out, VAL_NO_STRIDES, eval_cost_fn) costs.append(cost) val_costs.append(val_cost) print("Epoch {} train cost = {}, validation cost = {} ({:.1f}sec) " .format(epoch + 1, cost, val_cost, time.time() - time_start)) if epoch > 10: lr.set_value(lr.get_value() * lr_decay) X_val_recon = recon_fn(X_val) visualize_reconstruction(X_val_out[450:550], X_val_recon[450:550], shape=(30, 40), savefilename='avletters') plot_validation_cost(costs, val_costs, None, savefilename='valid_cost') conv2d1 = las.layers.get_all_layers(network)[2] visualize.plot_conv_weights(conv2d1, (15, 14)).savefig('conv2d1.png') print('saving encoder...') save_model(encoder, 'models/conv_encoder.dat') save_model(network, 'models/conv_ae.dat')
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) data_audio = load_mat_file(config.get('data', 'audio')) ae_pretrained = config.get('models', 'pretrained') ae_diff_pretrained = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') lstm_size = config.getint('models', 'lstm_size') output_classes = config.getint('models', 'output_classes') nonlinearity = options[ 'nonlinearity'] if 'nonlinearity' in options else config.get( 'models', 'nonlinearity') if nonlinearity == 'sigmoid': nonlinearity = sigmoid if nonlinearity == 'rectify': nonlinearity = rectify # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int( options['num_epoch']) if 'num_epoch' in options else config.getint( 'training', 'num_epoch') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'training', 'use_peepholes') input_dimension = config.getint('models', 'input_dimension') input_dimension2 = config.getint('models', 'input_dimension2') use_blstm = config.getboolean('training', 'use_blstm') use_finetuning = config.getboolean('training', 'use_finetuning') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1, )) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1, )) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1, )) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_X_audio = data_audio['trData'].astype('float32') val_X_audio = data_audio['valData'].astype('float32') test_X_audio = data_audio['testData'].astype('float32') # +1 to handle the -1 introduced in lstm_gendata train_y = data['trTargetsVec'].astype('int').reshape((-1, )) + 1 val_y = data['valTargetsVec'].astype('int').reshape((-1, )) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1, )) + 1 train_X = reorder_data(train_X, (30, 50)) val_X = reorder_data(val_X, (30, 50)) test_X = reorder_data(test_X, (30, 50)) visual_weights, visual_biases = load_dbn(ae_pretrained) audio_weights, audio_biases = load_dbn(ae_diff_pretrained) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') visual_input = T.tensor3('visual_input', dtype='float32') audio_input = T.tensor3('audio_input', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') visual_net = avnet.create_pretrained_substream( visual_weights, visual_biases, (None, None, input_dimension), visual_input, (None, None), mask, 'visual', lstm_size, window, nonlinearity, weight_init_fn, use_peepholes) audio_net = avnet.create_pretrained_substream( audio_weights, audio_biases, (None, None, input_dimension2), audio_input, (None, None), mask, 'audio', lstm_size, window, nonlinearity, weight_init_fn, use_peepholes) network, l_fuse = avnet.create_model([visual_net, audio_net], (None, None), mask, lstm_size, output_classes, fusiontype, weight_init_fn, use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function([visual_input, targets, mask, audio_input, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [visual_input, targets, mask, audio_input, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [visual_input, targets, mask, audio_input, window], test_cost, allow_input_downcast=True) val_fn = theano.function([visual_input, mask, audio_input, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] EPOCH_SIZE = 90 BATCH_SIZE = 10 WINDOW_SIZE = 9 STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_audio, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_audio, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_audio, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam with learning rate {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), learning_rate) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_tr = cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values( l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, WINDOW_SIZE, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{},{},{},{},{},{},{},{}\n'.format( use_finetuning, 'yes', use_peepholes, 'adam', weight_init, 'RELU', use_blstm, learning_rate, best_tr, best_val, best_cr * 100, test_cr * 100)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('CLI options: {}'.format(options.items())) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) no_coeff = config.getint('models', 'no_coeff') output_classes = config.getint('models', 'output_classes') lstm_size = config.getint('models', 'lstm_size') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') no_epochs = int(options['no_epochs']) if 'no_epochs' in options else config.getint('training', 'no_epochs') weight_init = options['weight_init'] if 'weight_init' in options else config.get('training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = options['epochsize'] if 'epochsize' in options else config.getint('training', 'epochsize') batchsize = options['batchsize'] if 'batchsize' in options else config.getint('training', 'batchsize') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('training', 'use_peepholes') use_blstm = config.getboolean('training', 'use_blstm') use_finetuning = config.getboolean('training', 'use_finetuning') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1,)) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1,)) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1,)) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_dct = dct_data['trDctFeatures'].astype('float32') val_dct = dct_data['valDctFeatures'].astype('float32') test_dct = dct_data['testDctFeatures'].astype('float32') # +1 to handle the -1 introduced in lstm_gendata train_y = data['trTargetsVec'].astype('int').reshape((-1,)) + 1 val_y = data['valTargetsVec'].astype('int').reshape((-1,)) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1,)) + 1 # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) inputs = T.tensor3('inputs', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network = lstm_classifier_majority_vote.create_model((None, None, no_coeff*3), inputs, (None, None), mask, lstm_size, output_classes, w_init=weight_init_fn) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params) train = theano.function( [inputs, targets, mask], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs, targets, mask], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(no_epochs): time_start = time.time() for i in range(epochsize): _, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(y)) print(print_str, end='') sys.stdout.flush() train(d, y, m) print('\r', end='') cost = compute_train_cost(d, y, m) val_cost = compute_test_cost(dct_val, y_val, mask_val) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(dct_val, y_val_evaluate, mask_val, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_conf = val_conf best_cr = cr test_cr, test_conf = evaluate_model2(dct_test, y_test, mask_test, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(use_finetuning, 'yes', use_peepholes, 'adam', weight_init, 'N/A', use_blstm, learning_rate, best_tr, best_val, best_cr*100, test_cr*100)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) ae_pretrained = config.get('models', 'pretrained') ae_pretrained_diff = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int(options['num_epoch']) if 'num_epoch' in options else config.getint('training', 'num_epoch') weight_init = options['weight_init'] if 'weight_init' in options else config.get('training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('training', 'use_peepholes') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') windowsize = config.getint('training', 'windowsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file('data/train.txt') val_subject_ids = read_data_split_file('data/val.txt') test_subject_ids = read_data_split_file('data/test.txt') data_matrix = data['dataMatrix'] targets_vec = data['targetsVec'].reshape((-1,)) subjects_vec = data['subjectsVec'].reshape((-1,)) vidlen_vec = data['videoLengthVec'].reshape((-1,)) data_matrix = reorder_data(data_matrix, (30, 50)) train_X, train_y, train_vidlens, train_subjects, \ val_X, val_y, val_vidlens, val_subjects, \ test_X, test_y, test_vidlens, test_subjects = split_seq_data(data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) train_X_diff = compute_diff_images(train_X, train_vidlens) val_X_diff = compute_diff_images(val_X, val_vidlens) test_X_diff = compute_diff_images(test_X, test_vidlens) train_X = sequencewise_mean_image_subtraction(train_X, train_vidlens) val_X = sequencewise_mean_image_subtraction(val_X, val_vidlens) test_X = sequencewise_mean_image_subtraction(test_X, test_vidlens) ae = load_dbn(ae_pretrained) ae_diff = load_dbn(ae_pretrained_diff) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network, l_fuse = adenet_v2_2.create_model(ae, ae_diff, (None, None, 1500), inputs, (None, None), mask, (None, None, 1500), inputs_diff, 250, window, 10, fusiontype, w_init_fn=weight_init_fn, use_peepholes=use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function( [inputs, targets, mask, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs, targets, mask, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, windowsize) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, windowsize) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, windowsize) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, windowsize, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_tr = cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values(l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, windowsize, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val))
def main(): def signal_handler(signal, frame): global terminate terminate = True print('terminating...'.format(terminate)) signal.signal(signal.SIGINT, signal_handler) configure_theano() options = parse_options() X, X_val = generate_data() # X = np.reshape(X, (-1, 1, 30, 40))[:-5] print('X type and shape:', X.dtype, X.shape) print('X.min():', X.min()) print('X.max():', X.max()) # X_val = np.reshape(X_val, (-1, 1, 30, 40))[:-1] print('X_val type and shape:', X_val.dtype, X_val.shape) print('X_val.min():', X_val.min()) print('X_val.max():', X_val.max()) # we need our target to be 1 dimensional X_out = X.reshape((X.shape[0], -1)) X_val_out = X_val.reshape((X_val.shape[0], -1)) print('X_out:', X_out.dtype, X_out.shape) print('X_val_out', X_val_out.dtype, X_val_out.shape) # X_noisy = apply_gaussian_noise(X_out) # visualize_reconstruction(X_noisy[0:25], X_out[0:25], shape=(28, 28)) # X = np.reshape(X_noisy, (-1, 1, 28, 28)) print('constructing and compiling model...') # input_var = T.tensor4('input', dtype='float32') input_var = T.tensor3('input', dtype='float32') target_var = T.matrix('output', dtype='float32') lr = theano.shared(np.array(0.8, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(0.9, dtype=theano.config.floatX) # try building a reshaping layer # network = create_model(input_var, (None, 1, 30, 40), options) l_input = InputLayer((None, None, 1200), input_var, name='input') l_input = ReshapeLayer(l_input, (-1, 1, 30, 40), name='reshape_input') # l_input = InputLayer((None, 1, 30, 40), input_var, name='input') if options['MODEL'] == 'normal': network, encoder = avletters_convae.create_model(l_input, options) if options['MODEL'] == 'batchnorm': network, encoder = avletters_convae_bn.create_model(l_input, options) if options['MODEL'] == 'dropout': network, encoder = avletters_convae_drop.create_model(l_input, options) if options['MODEL'] == 'bn+dropout': network, encoder = avletters_convae_bndrop.create_model( l_input, options) print('AE Network architecture: {}'.format(options['MODEL'])) print_network(network) recon = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(squared_error(recon, target_var)) updates = adadelta(cost, all_params, lr) # updates = las.updates.apply_nesterov_momentum(updates, all_params, momentum=0.90) use_max_constraint = False print('apply max norm constraint: {}'.format(use_max_constraint)) if use_max_constraint: MAX_NORM = 4 for param in las.layers.get_all_params(network, regularizable=True): if param.ndim > 1: # only apply to dimensions larger than 1, exclude biases # updates[param] = norm_constraint(param, MAX_NORM * las.utils.compute_norms(param.get_value()).mean()) updates[param] = norm_constraint(param, MAX_NORM) train = theano.function([input_var, target_var], recon, updates=updates, allow_input_downcast=True) train_cost_fn = theano.function([input_var, target_var], cost, allow_input_downcast=True) eval_recon = las.layers.get_output(network, deterministic=True) eval_cost = T.mean(las.objectives.squared_error(eval_recon, target_var)) eval_cost_fn = theano.function([input_var, target_var], eval_cost, allow_input_downcast=True) recon_fn = theano.function([input_var], eval_recon, allow_input_downcast=True) if terminate: exit() NUM_EPOCHS = options['NUM_EPOCHS'] EPOCH_SIZE = options['EPOCH_SIZE'] NO_STRIDES = options['NO_STRIDES'] VAL_NO_STRIDES = options['VAL_NO_STRIDES'] print('begin training for {} epochs...'.format(NUM_EPOCHS)) datagen = batch_iterator(X, X_out, 128) costs = [] val_costs = [] for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): batch_X, batch_y = next(datagen) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(batch_X), lr.get_value()) print(print_str, end='') sys.stdout.flush() batch_X = batch_X.reshape((-1, 1, 1200)) train(batch_X, batch_y) print('\r', end='') if terminate: break if terminate: break cost = batch_compute_cost(X, X_out, NO_STRIDES, train_cost_fn) val_cost = batch_compute_cost(X_val, X_val_out, VAL_NO_STRIDES, eval_cost_fn) costs.append(cost) val_costs.append(val_cost) print("Epoch {} train cost = {}, validation cost = {} ({:.1f}sec) ". format(epoch + 1, cost, val_cost, time.time() - time_start)) if epoch > 10: lr.set_value(lr.get_value() * lr_decay) X_val_recon = recon_fn(X_val) visualize_reconstruction(X_val_out[450:550], X_val_recon[450:550], shape=(30, 40), savefilename='avletters') plot_validation_cost(costs, val_costs, None, savefilename='valid_cost') conv2d1 = las.layers.get_all_layers(network)[2] visualize.plot_conv_weights(conv2d1, (15, 14)).savefig('conv2d1.png') print('saving encoder...') save_model(encoder, 'models/conv_encoder.dat') save_model(network, 'models/conv_ae.dat')
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_pretrained = config.get('models', 'pretrained') ae_pretrained_diff = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') lstm_size = config.getint('models', 'lstm_size') output_classes = config.getint('models', 'output_classes') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('models', 'use_peepholes') use_blstm = config.getboolean('models', 'use_blstm') delta_window = config.getint('models', 'delta_window') input_dimensions = config.getint('models', 'input_dimensions') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int(options['num_epoch']) if 'num_epoch' in options else config.getint('training', 'num_epoch') weight_init = options['weight_init'] if 'weight_init' in options else config.get('training', 'weight_init') use_finetuning = config.getboolean('training', 'use_finetuning') learning_rate = config.getfloat('training', 'learning_rate') batchsize = config.getint('training', 'batchsize') epochsize = config.getint('training', 'epochsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y),)) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects),)) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens,))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) X_diff = compute_diff_images(X, video_lens) # mean remove dct features dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) train_subject_ids = read_data_split_file('data/train_30_10_12.txt') val_subject_ids = read_data_split_file('data/val_30_10_12.txt') test_subject_ids = read_data_split_file('data/test_30_10_12.txt') print('Train: {}'.format(train_subject_ids)) print('Validation: {}'.format(val_subject_ids)) print('Test: {}'.format(test_subject_ids)) train_X, train_y, train_dct, train_X_diff, train_vidlens, train_subjects, \ val_X, val_y, val_dct, val_X_diff, val_vidlens, val_subjects, \ test_X, test_y, test_dct, test_X_diff, test_vidlens, test_subjects = \ split_data(X, y, dct_feats, X_diff, subjects, video_lens, train_subject_ids, val_subject_ids, test_subject_ids) assert train_X.shape[0] + val_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + val_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + val_vidlens.shape[0] + test_vidlens.shape[0] == len(video_lens) assert train_subjects.shape[0] + val_vidlens.shape[0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) val_X = normalize_input(val_X, centralize=True) test_X = normalize_input(test_X, centralize=True) train_y -= 1 val_y -= 1 test_y -= 1 print('loading pretrained encoder: {}...'.format(ae_pretrained)) ae = load_dbn(ae_pretrained) print('loading pretrained encoder: {}...'.format(ae_pretrained_diff)) ae_diff = load_dbn(ae_pretrained_diff) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') if use_blstm: network, l_fuse = adenet_v2_2.create_model(ae, ae_diff, (None, None, input_dimensions), inputs, (None, None), mask, (None, None, input_dimensions), inputs_diff, lstm_size, window, output_classes, fusiontype, weight_init_fn, use_peepholes) else: network, l_fuse = adenet_v2_4.create_model(ae, ae_diff, (None, None, input_dimensions), inputs, (None, None), mask, (None, None, input_dimensions), inputs_diff, lstm_size, window, output_classes, fusiontype, weight_init_fn, use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function( [inputs, targets, mask, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs, targets, mask, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format(epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, delta_window) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, delta_window) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, delta_window) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, delta_window, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values(l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, delta_window, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break phrases = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, phrases, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{}\n'.format(validation_window, weight_init, use_peepholes, use_blstm, use_finetuning)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s)) f.write('{},{},{}\n'.format(fusiontype, best_cr, best_val))
def create_model(input_var, input_shape, options): conv_num_filters1 = 100 conv_num_filters2 = 150 conv_num_filters3 = 200 filter_size1 = 5 filter_size2 = 5 filter_size3 = 3 pool_size = 2 encode_size = options['BOTTLENECK'] dense_mid_size = options['DENSE'] pad_in = 'valid' pad_out = 'full' scaled_tanh = create_scaled_tanh() input = InputLayer(shape=input_shape, input_var=input_var, name='input') conv2d1 = Conv2DLayer(input, num_filters=conv_num_filters1, filter_size=filter_size1, pad=pad_in, name='conv2d1', nonlinearity=scaled_tanh) maxpool2d2 = MaxPool2DLayer(conv2d1, pool_size=pool_size, name='maxpool2d2') conv2d3 = Conv2DLayer(maxpool2d2, num_filters=conv_num_filters2, filter_size=filter_size2, pad=pad_in, name='conv2d3', nonlinearity=scaled_tanh) maxpool2d4 = MaxPool2DLayer(conv2d3, pool_size=pool_size, name='maxpool2d4', pad=(1, 0)) conv2d5 = Conv2DLayer(maxpool2d4, num_filters=conv_num_filters3, filter_size=filter_size3, pad=pad_in, name='conv2d5', nonlinearity=scaled_tanh) reshape6 = ReshapeLayer(conv2d5, shape=([0], -1), name='reshape6') # 3000 reshape6_output = reshape6.output_shape[1] dense7 = DenseLayer(reshape6, num_units=dense_mid_size, name='dense7', nonlinearity=scaled_tanh) bottleneck = DenseLayer(dense7, num_units=encode_size, name='bottleneck', nonlinearity=linear) # print_network(bottleneck) dense8 = DenseLayer(bottleneck, num_units=dense_mid_size, W=bottleneck.W.T, name='dense8', nonlinearity=linear) dense9 = DenseLayer(dense8, num_units=reshape6_output, W=dense7.W.T, nonlinearity=scaled_tanh, name='dense9') reshape10 = ReshapeLayer(dense9, shape=([0], conv_num_filters3, 3, 5), name='reshape10') # 32 x 4 x 7 deconv2d11 = Deconv2DLayer(reshape10, conv2d5.input_shape[1], conv2d5.filter_size, stride=conv2d5.stride, W=conv2d5.W, flip_filters=not conv2d5.flip_filters, name='deconv2d11', nonlinearity=scaled_tanh) upscale2d12 = Upscale2DLayer(deconv2d11, scale_factor=pool_size, name='upscale2d12') deconv2d13 = Deconv2DLayer(upscale2d12, conv2d3.input_shape[1], conv2d3.filter_size, stride=conv2d3.stride, W=conv2d3.W, flip_filters=not conv2d3.flip_filters, name='deconv2d13', nonlinearity=scaled_tanh) upscale2d14 = Upscale2DLayer(deconv2d13, scale_factor=pool_size, name='upscale2d14') deconv2d15 = Deconv2DLayer(upscale2d14, conv2d1.input_shape[1], conv2d1.filter_size, stride=conv2d1.stride, crop=(1, 0), W=conv2d1.W, flip_filters=not conv2d1.flip_filters, name='deconv2d14', nonlinearity=scaled_tanh) reshape16 = ReshapeLayer(deconv2d15, ([0], -1), name='reshape16') print_network(reshape16) return reshape16
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) data_audio = load_mat_file(config.get('data', 'audio')) ae_pretrained = config.get('models', 'pretrained') ae_diff_pretrained = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') lstm_size = config.getint('models', 'lstm_size') output_classes = config.getint('models', 'output_classes') nonlinearity = options['nonlinearity'] if 'nonlinearity' in options else config.get('models', 'nonlinearity') if nonlinearity == 'sigmoid': nonlinearity = sigmoid if nonlinearity == 'rectify': nonlinearity = rectify # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int(options['num_epoch']) if 'num_epoch' in options else config.getint('training', 'num_epoch') weight_init = options['weight_init'] if 'weight_init' in options else config.get('training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('training', 'use_peepholes') input_dimension = config.getint('models', 'input_dimension') input_dimension2 = config.getint('models', 'input_dimension2') use_blstm = config.getboolean('training', 'use_blstm') use_finetuning = config.getboolean('training', 'use_finetuning') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1,)) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1,)) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1,)) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_X_audio = data_audio['trData'].astype('float32') val_X_audio = data_audio['valData'].astype('float32') test_X_audio = data_audio['testData'].astype('float32') # +1 to handle the -1 introduced in lstm_gendata train_y = data['trTargetsVec'].astype('int').reshape((-1,)) + 1 val_y = data['valTargetsVec'].astype('int').reshape((-1,)) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1,)) + 1 train_X = reorder_data(train_X, (30, 50)) val_X = reorder_data(val_X, (30, 50)) test_X = reorder_data(test_X, (30, 50)) visual_weights, visual_biases = load_dbn(ae_pretrained) audio_weights, audio_biases = load_dbn(ae_diff_pretrained) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') visual_input = T.tensor3('visual_input', dtype='float32') audio_input = T.tensor3('audio_input', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') visual_net = avnet.create_pretrained_substream(visual_weights, visual_biases, (None, None, input_dimension), visual_input, (None, None), mask, 'visual', lstm_size, window, nonlinearity, weight_init_fn, use_peepholes) audio_net = avnet.create_pretrained_substream(audio_weights, audio_biases, (None, None, input_dimension2), audio_input, (None, None), mask, 'audio', lstm_size, window, nonlinearity, weight_init_fn, use_peepholes) network, l_fuse = avnet.create_model([visual_net, audio_net], (None, None), mask, lstm_size, output_classes, fusiontype, weight_init_fn, use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function( [visual_input, targets, mask, audio_input, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([visual_input, targets, mask, audio_input, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [visual_input, targets, mask, audio_input, window], test_cost, allow_input_downcast=True) val_fn = theano.function([visual_input, mask, audio_input, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] EPOCH_SIZE = 90 BATCH_SIZE = 10 WINDOW_SIZE = 9 STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_audio, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_audio, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_audio, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam with learning rate {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), learning_rate) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_tr = cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values(l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, WINDOW_SIZE, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(use_finetuning, 'yes', use_peepholes, 'adam', weight_init, 'RELU', use_blstm, learning_rate, best_tr, best_val, best_cr*100, test_cr*100)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s))
def main(): configure_theano() config_file = 'config/trimodal.ini' print('loading config file: {}'.format(config_file)) config = ConfigParser.ConfigParser() config.read(config_file) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) ae_pretrained = config.get('models', 'pretrained') ae_finetuned = config.get('models', 'finetuned') ae_finetuned_diff = config.get('models', 'finetuned_diff') use_adascale = config.getboolean('models', 'use_adascale') learning_rate = float(config.get('training', 'learning_rate')) decay_rate = float(config.get('training', 'decay_rate')) decay_start = int(config.get('training', 'decay_start')) do_finetune = config.getboolean('training', 'do_finetune') save_finetune = config.getboolean('training', 'save_finetune') load_finetune = config.getboolean('training', 'load_finetune') load_finetune_diff = config.getboolean('training', 'load_finetune_diff') # 53 subjects, 70 utterances, 5 view angles # s[x]_v[y]_u[z].mp4 # resized, height, width = (26, 44) # ['dataMatrix', 'targetH', 'targetsPerVideoVec', 'videoLengthVec', '__header__', 'targetsVec', # '__globals__', 'iterVec', 'filenamesVec', 'dataMatrixCells', 'subjectsVec', 'targetW', '__version__'] print(data.keys()) X = data['dataMatrix'].astype('float32') y = data['targetsVec'].astype('int32') y = y.reshape((len(y),)) dct_feats = dct_data['dctFeatures'].astype('float32') uniques = np.unique(y) print('number of classifications: {}'.format(len(uniques))) subjects = data['subjectsVec'].astype('int') subjects = subjects.reshape((len(subjects),)) video_lens = data['videoLengthVec'].astype('int') video_lens = video_lens.reshape((len(video_lens,))) # X = reorder_data(X, (26, 44), 'f', 'c') # print('performing sequencewise mean image removal...') # X = sequencewise_mean_image_subtraction(X, video_lens) # visualize_images(X[550:650], (26, 44)) X_diff = compute_diff_images(X, video_lens) # mean remove dct features dct_feats = sequencewise_mean_image_subtraction(dct_feats, video_lens) train_subject_ids = read_data_split_file('data/train_val.txt') test_subject_ids = read_data_split_file('data/test.txt') print(train_subject_ids) print(test_subject_ids) train_X, train_y, train_dct, train_X_diff, train_vidlens, train_subjects, \ test_X, test_y, test_dct, test_X_diff, test_vidlens, test_subjects = \ split_data(X, y, dct_feats, X_diff, subjects, video_lens, train_subject_ids, test_subject_ids) assert train_X.shape[0] + test_X.shape[0] == len(X) assert train_y.shape[0] + test_y.shape[0] == len(y) assert train_vidlens.shape[0] + test_vidlens.shape[0] == len(video_lens) assert train_subjects.shape[0] + test_subjects.shape[0] == len(subjects) train_X = normalize_input(train_X, centralize=True) test_X = normalize_input(test_X, centralize=True) # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) test_dct = (test_dct - dct_mean) / dct_std if do_finetune: print('performing finetuning on pretrained encoder: {}'.format(ae_pretrained)) ae = load_dbn(ae_pretrained) ae.initialize() ae.fit(train_X, train_X) if save_finetune: print('saving finetuned encoder: {}...'.format(ae_finetuned)) pickle.dump(ae, open(ae_finetuned, 'wb')) if load_finetune: print('loading finetuned encoder: {}...'.format(ae_finetuned)) ae = pickle.load(open(ae_finetuned, 'rb')) ae.initialize() if load_finetune_diff: print('loading finetuned encoder: {}...'.format(ae_finetuned_diff)) ae_diff = pickle.load(open(ae_finetuned_diff, 'rb')) ae_diff.initialize() # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') dct = T.tensor3('dct', dtype='float32') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.ivector('targets') lr = theano.shared(np.array(learning_rate, dtype=theano.config.floatX), name='learning_rate') lr_decay = np.array(decay_rate, dtype=theano.config.floatX) print('constructing end to end model...') ''' network = create_end_to_end_model(dbn, (None, None, 1144), inputs, (None, None), mask, 250, window) ''' network, adascale = adenet_v5.create_model(ae, ae_diff, (None, None, 1144), inputs, (None, None), mask, (None, None, 90), dct, (None, None, 1144), inputs_diff, 250, window, 10, use_adascale) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = T.mean(las.objectives.categorical_crossentropy(predictions, targets)) updates = adadelta(cost, all_params, learning_rate=lr) # updates = adagrad(cost, all_params, learning_rate=lr) use_max_constraint = False if use_max_constraint: MAX_NORM = 4 for param in las.layers.get_all_params(network, regularizable=True): if param.ndim > 1: # only apply to dimensions larger than 1, exclude biases updates[param] = norm_constraint(param, MAX_NORM * las.utils.compute_norms(param.get_value()).mean()) train = theano.function( [inputs, targets, mask, dct, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask, dct, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = T.mean(las.objectives.categorical_crossentropy(test_predictions, targets)) compute_test_cost = theano.function( [inputs, targets, mask, dct, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, dct, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] NUM_EPOCHS = 30 EPOCH_SIZE = 120 BATCH_SIZE = 10 WINDOW_SIZE = 9 STRIP_SIZE = 3 MAX_LOSS = 0.2 VALIDATION_WINDOW = 4 val_window = circular_list(VALIDATION_WINDOW) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_conf = None best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=BATCH_SIZE) val_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(test_vidlens) dct_val = gen_seq_batch_from_idx(test_dct, idxs_val, test_vidlens, integral_lens_val, np.max(test_vidlens)) X_diff_val = gen_seq_batch_from_idx(test_X_diff, idxs_val, test_vidlens, integral_lens_val, np.max(test_vidlens)) def early_stop(cost_window): if len(cost_window) < 2: return False else: curr = cost_window[0] for idx, cost in enumerate(cost_window): if curr < cost or idx == 0: curr = cost else: return False return True for epoch in range(NUM_EPOCHS): time_start = time.time() for i in range(EPOCH_SIZE): X, y, m, batch_idxs = next(datagen) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples at learning rate = {:.4f}'.format( epoch + 1, i + 1, EPOCH_SIZE, len(X), float(lr.get_value())) print(print_str, end='') sys.stdout.flush() train(X, y, m, d, X_diff, WINDOW_SIZE) print('\r', end='') cost = compute_train_cost(X, y, m, d, X_diff, WINDOW_SIZE) val_cost = compute_test_cost(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model(X_val, y_val, mask_val, dct_val, X_diff_val, WINDOW_SIZE, val_fn) class_rate.append(cr) print("Epoch {} train cost = {}, validation cost = {}, " "generalization loss = {:.3f}, GQ = {:.3f}, classification rate = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if val_cost < best_val: best_val = val_cost best_conf = val_conf best_cr = cr if use_adascale: adascale_param = las.layers.get_all_param_values(adascale, scaling_param=True) if epoch >= VALIDATION_WINDOW and early_stop(val_window): break # learning rate decay if epoch >= decay_start - 1: lr.set_value(lr.get_value() * lr_decay) phrases = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'] print('Final Model') print('classification rate: {}, validation loss: {}'.format(best_cr, best_val)) if use_adascale: print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(best_conf, phrases, fmt='grid') plot_validation_cost(cost_train, cost_val, class_rate, savefilename='valid_cost')
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) ae_pretrained = config.get('models', 'pretrained') ae_pretrained_diff = config.get('models', 'pretrained_diff') fusiontype = config.get('models', 'fusiontype') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int( options['num_epoch']) if 'num_epoch' in options else config.getint( 'training', 'num_epoch') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'training', 'use_peepholes') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') windowsize = config.getint('training', 'windowsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file('data/train.txt') val_subject_ids = read_data_split_file('data/val.txt') test_subject_ids = read_data_split_file('data/test.txt') data_matrix = data['dataMatrix'] targets_vec = data['targetsVec'].reshape((-1, )) subjects_vec = data['subjectsVec'].reshape((-1, )) vidlen_vec = data['videoLengthVec'].reshape((-1, )) data_matrix = reorder_data(data_matrix, (30, 50)) train_X, train_y, train_vidlens, train_subjects, \ val_X, val_y, val_vidlens, val_subjects, \ test_X, test_y, test_vidlens, test_subjects = split_seq_data(data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) train_X_diff = compute_diff_images(train_X, train_vidlens) val_X_diff = compute_diff_images(val_X, val_vidlens) test_X_diff = compute_diff_images(test_X, test_vidlens) train_X = sequencewise_mean_image_subtraction(train_X, train_vidlens) val_X = sequencewise_mean_image_subtraction(val_X, val_vidlens) test_X = sequencewise_mean_image_subtraction(test_X, test_vidlens) ae = load_dbn(ae_pretrained) ae_diff = load_dbn(ae_pretrained_diff) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs = T.tensor3('inputs', dtype='float32') inputs_diff = T.tensor3('inputs_diff', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network, l_fuse = adenet_v2_2.create_model(ae, ae_diff, (None, None, 1500), inputs, (None, None), mask, (None, None, 1500), inputs_diff, 250, window, 10, fusiontype, w_init_fn=weight_init_fn, use_peepholes=use_peepholes) print_network(network) # draw_to_file(las.layers.get_all_layers(network), 'network.png') print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params, learning_rate=learning_rate) train = theano.function([inputs, targets, mask, inputs_diff, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function( [inputs, targets, mask, inputs_diff, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs, targets, mask, inputs_diff, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask, inputs_diff, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) integral_lens = compute_integral_len(train_vidlens) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) X_diff_val = gen_seq_batch_from_idx(val_X_diff, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) X_diff_test = gen_seq_batch_from_idx(test_X_diff, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) X_diff = gen_seq_batch_from_idx(train_X_diff, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(X)) print(print_str, end='') sys.stdout.flush() train(X, y, m, X_diff, windowsize) print('\r', end='') cost = compute_train_cost(X, y, m, X_diff, windowsize) val_cost = compute_test_cost(X_val, y_val, mask_val, X_diff_val, windowsize) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, X_diff_val, windowsize, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_tr = cost best_cr = cr if fusiontype == 'adasum': adascale_param = las.layers.get_all_param_values( l_fuse, scaling_param=True) test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, X_diff_test, windowsize, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) if fusiontype == 'adasum': print("final scaling params: {}".format(adascale_param)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('Reading Config File: {}...'.format(config_file)) print(config.items('data')) print(config.items('models')) print(config.items('training')) print('CLI options: {}'.format(options.items())) print('preprocessing dataset...') data = load_mat_file(config.get('data', 'images')) dct_data = load_mat_file(config.get('data', 'dct')) no_coeff = config.getint('models', 'no_coeff') output_classes = config.getint('models', 'output_classes') lstm_size = config.getint('models', 'lstm_size') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') no_epochs = int( options['no_epochs']) if 'no_epochs' in options else config.getint( 'training', 'no_epochs') weight_init = options[ 'weight_init'] if 'weight_init' in options else config.get( 'training', 'weight_init') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = options[ 'epochsize'] if 'epochsize' in options else config.getint( 'training', 'epochsize') batchsize = options[ 'batchsize'] if 'batchsize' in options else config.getint( 'training', 'batchsize') use_peepholes = options[ 'use_peepholes'] if 'use_peepholes' in options else config.getboolean( 'training', 'use_peepholes') use_blstm = config.getboolean('training', 'use_blstm') use_finetuning = config.getboolean('training', 'use_finetuning') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_vidlens = data['trVideoLengthVec'].astype('int').reshape((-1, )) val_vidlens = data['valVideoLengthVec'].astype('int').reshape((-1, )) test_vidlens = data['testVideoLengthVec'].astype('int').reshape((-1, )) train_X = data['trData'].astype('float32') val_X = data['valData'].astype('float32') test_X = data['testData'].astype('float32') train_dct = dct_data['trDctFeatures'].astype('float32') val_dct = dct_data['valDctFeatures'].astype('float32') test_dct = dct_data['testDctFeatures'].astype('float32') # +1 to handle the -1 introduced in lstm_gendata train_y = data['trTargetsVec'].astype('int').reshape((-1, )) + 1 val_y = data['valTargetsVec'].astype('int').reshape((-1, )) + 1 test_y = data['testTargetsVec'].astype('int').reshape((-1, )) + 1 # featurewise normalize dct features train_dct, dct_mean, dct_std = featurewise_normalize_sequence(train_dct) val_dct = (val_dct - dct_mean) / dct_std test_dct = (test_dct - dct_mean) / dct_std # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) inputs = T.tensor3('inputs', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network = lstm_classifier_majority_vote.create_model( (None, None, no_coeff * 3), inputs, (None, None), mask, lstm_size, output_classes, w_init=weight_init_fn) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) updates = adam(cost, all_params) train = theano.function([inputs, targets, mask], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs, targets, mask], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function([inputs, targets, mask], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs, mask], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE, )) best_val = float('inf') best_tr = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) integral_lens = compute_integral_len(train_vidlens) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) integral_lens_val = compute_integral_len(val_vidlens) dct_val = gen_seq_batch_from_idx(val_dct, idxs_val, val_vidlens, integral_lens_val, np.max(val_vidlens)) X_test, y_test, mask_test, idxs_test = next(test_datagen) integral_lens_test = compute_integral_len(test_vidlens) dct_test = gen_seq_batch_from_idx(test_dct, idxs_test, test_vidlens, integral_lens_test, np.max(test_vidlens)) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(no_epochs): time_start = time.time() for i in range(epochsize): _, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) d = gen_seq_batch_from_idx(train_dct, batch_idxs, train_vidlens, integral_lens, np.max(train_vidlens)) print_str = 'Epoch {} batch {}/{}: {} examples using adam'.format( epoch + 1, i + 1, epochsize, len(y)) print(print_str, end='') sys.stdout.flush() train(d, y, m) print('\r', end='') cost = compute_train_cost(d, y, m) val_cost = compute_test_cost(dct_val, y_val, mask_val) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(dct_val, y_val_evaluate, mask_val, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_conf = val_conf best_cr = cr test_cr, test_conf = evaluate_model2(dct_test, y_test, mask_test, val_fn) print( "Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)". format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) print('confusion matrix: ') plot_confusion_matrix(test_conf, numbers, fmt='latex') plot_validation_cost(cost_train, cost_val, savefilename='valid_cost') if 'write_results' in options: results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{},{},{},{},{},{},{},{},{},{}\n'.format( use_finetuning, 'yes', use_peepholes, 'adam', weight_init, 'N/A', use_blstm, learning_rate, best_tr, best_val, best_cr * 100, test_cr * 100)) s = ','.join([str(v) for v in cost_train]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in cost_val]) f.write('{}\n'.format(s)) s = ','.join([str(v) for v in class_rate]) f.write('{}\n'.format(s))
def main(): configure_theano() options = parse_options() config_file = options['config'] config = ConfigParser.ConfigParser() config.read(config_file) print('CLI options: {}'.format(options.items())) print('Reading Config File: {}...'.format(config_file)) print(config.items('stream1')) print(config.items('lstm_classifier')) print(config.items('training')) print('preprocessing dataset...') data = load_mat_file(config.get('stream1', 'data')) stream1 = config.get('stream1', 'model') imagesize = tuple([int(d) for d in config.get('stream1', 'imagesize').split(',')]) stream1_dim = config.getint('stream1', 'input_dimensions') stream1_shape = config.get('stream1', 'shape') stream1_nonlinearities = config.get('stream1', 'nonlinearities') # lstm classifier output_classes = config.getint('lstm_classifier', 'output_classes') output_classnames = config.get('lstm_classifier', 'output_classnames').split(',') lstm_size = config.getint('lstm_classifier', 'lstm_size') matlab_target_offset = config.getboolean('lstm_classifier', 'matlab_target_offset') # data preprocessing options reorderdata = config.getboolean('stream1', 'reorderdata') diffimage = config.getboolean('stream1', 'diffimage') meanremove = config.getboolean('stream1', 'meanremove') samplewisenormalize = config.getboolean('stream1', 'samplewisenormalize') featurewisenormalize = config.getboolean('stream1', 'featurewisenormalize') # lstm classifier configurations weight_init = options['weight_init'] if 'weight_init' in options else config.get('lstm_classifier', 'weight_init') use_peepholes = options['use_peepholes'] if 'use_peepholes' in options else config.getboolean('lstm_classifier', 'use_peepholes') windowsize = config.getint('lstm_classifier', 'windowsize') # capture training parameters validation_window = int(options['validation_window']) \ if 'validation_window' in options else config.getint('training', 'validation_window') num_epoch = int(options['num_epoch']) if 'num_epoch' in options else config.getint('training', 'num_epoch') learning_rate = options['learning_rate'] if 'learning_rate' in options \ else config.getfloat('training', 'learning_rate') epochsize = config.getint('training', 'epochsize') batchsize = config.getint('training', 'batchsize') weight_init_fn = las.init.GlorotUniform() if weight_init == 'glorot': weight_init_fn = las.init.GlorotUniform() if weight_init == 'norm': weight_init_fn = las.init.Normal(0.1) if weight_init == 'uniform': weight_init_fn = las.init.Uniform() if weight_init == 'ortho': weight_init_fn = las.init.Orthogonal() train_subject_ids = read_data_split_file(config.get('training', 'train_subjects_file')) val_subject_ids = read_data_split_file(config.get('training', 'val_subjects_file')) test_subject_ids = read_data_split_file(config.get('training', 'test_subjects_file')) data_matrix = data['dataMatrix'].astype('float32') targets_vec = data['targetsVec'].reshape((-1,)) subjects_vec = data['subjectsVec'].reshape((-1,)) vidlen_vec = data['videoLengthVec'].reshape((-1,)) if reorderdata: data_matrix = reorder_data(data_matrix, (imagesize[0], imagesize[1])) train_X, train_y, train_vidlens, train_subjects, \ val_X, val_y, val_vidlens, val_subjects, \ test_X, test_y, test_vidlens, test_subjects = split_seq_data(data_matrix, targets_vec, subjects_vec, vidlen_vec, train_subject_ids, val_subject_ids, test_subject_ids) if matlab_target_offset: train_y -= 1 val_y -= 1 test_y -= 1 if meanremove: train_X = sequencewise_mean_image_subtraction(train_X, train_vidlens) val_X = sequencewise_mean_image_subtraction(val_X, val_vidlens) test_X = sequencewise_mean_image_subtraction(test_X, test_vidlens) if diffimage: train_X = compute_diff_images(train_X, train_vidlens) val_X = compute_diff_images(val_X, val_vidlens) test_X = compute_diff_images(test_X, test_vidlens) if samplewisenormalize: train_X = normalize_input(train_X) val_X = normalize_input(val_X) test_X = normalize_input(test_X) if featurewisenormalize: train_X, mean, std = featurewise_normalize_sequence(train_X) val_X = (val_X - mean) / std test_X = (test_X - mean) / std ae1 = load_decoder(stream1, stream1_shape, stream1_nonlinearities) # IMPT: the encoder was trained with fortan ordered images, so to visualize # convert all the images to C order using reshape_images_order() # output = dbn.predict(test_X) # test_X = reshape_images_order(test_X, (26, 44)) # output = reshape_images_order(output, (26, 44)) # visualize_reconstruction(test_X[:36, :], output[:36, :], shape=(26, 44)) window = T.iscalar('theta') inputs1 = T.tensor3('inputs1', dtype='float32') mask = T.matrix('mask', dtype='uint8') targets = T.imatrix('targets') print('constructing end to end model...') network = deltanet_majority_vote.create_model(ae1, (None, None, stream1_dim), inputs1, (None, None), mask, lstm_size, window, output_classes, weight_init_fn, use_peepholes) print_network(network) print('compiling model...') predictions = las.layers.get_output(network, deterministic=False) all_params = las.layers.get_all_params(network, trainable=True) cost = temporal_softmax_loss(predictions, targets, mask) default_learning_rate = theano.shared(las.utils.floatX(learning_rate), 'default_lr') lr_config = { 'fc1': theano.shared(las.utils.floatX(0.001)), 'fc2': theano.shared(las.utils.floatX(0.001)), 'fc3': theano.shared(las.utils.floatX(0.001)) } lr_map = custom.updates.generate_lr_map(all_params, lr_config, default_learning_rate) # updates = adam(cost, all_params, default_learning_rate) updates = custom.updates.adam_vlr(cost, all_params, lr_map) train = theano.function( [inputs1, targets, mask, window], cost, updates=updates, allow_input_downcast=True) compute_train_cost = theano.function([inputs1, targets, mask, window], cost, allow_input_downcast=True) test_predictions = las.layers.get_output(network, deterministic=True) test_cost = temporal_softmax_loss(test_predictions, targets, mask) compute_test_cost = theano.function( [inputs1, targets, mask, window], test_cost, allow_input_downcast=True) val_fn = theano.function([inputs1, mask, window], test_predictions, allow_input_downcast=True) # We'll train the network with 10 epochs of 30 minibatches each print('begin training...') cost_train = [] cost_val = [] class_rate = [] STRIP_SIZE = 3 val_window = circular_list(validation_window) train_strip = np.zeros((STRIP_SIZE,)) best_val = float('inf') best_cr = 0.0 datagen = gen_lstm_batch_random(train_X, train_y, train_vidlens, batchsize=batchsize) val_datagen = gen_lstm_batch_random(val_X, val_y, val_vidlens, batchsize=len(val_vidlens)) test_datagen = gen_lstm_batch_random(test_X, test_y, test_vidlens, batchsize=len(test_vidlens)) # We'll use this "validation set" to periodically check progress X_val, y_val, mask_val, idxs_val = next(val_datagen) # we use the test set to check final classification rate X_test, y_test, mask_test, idxs_test = next(test_datagen) # reshape the targets for validation y_val_evaluate = y_val y_val = y_val.reshape((-1, 1)).repeat(mask_val.shape[-1], axis=-1) for epoch in range(num_epoch): time_start = time.time() for i in range(epochsize): X, y, m, batch_idxs = next(datagen) # repeat targets based on max sequence len y = y.reshape((-1, 1)) y = y.repeat(m.shape[-1], axis=-1) print_str = 'Epoch {} batch {}/{}: {} examples using adam with learning rate = {}'.format( epoch + 1, i + 1, epochsize, len(X), learning_rate) print(print_str, end='') sys.stdout.flush() train(X, y, m, windowsize) print('\r', end='') cost = compute_train_cost(X, y, m, windowsize) val_cost = compute_test_cost(X_val, y_val, mask_val, windowsize) cost_train.append(cost) cost_val.append(val_cost) train_strip[epoch % STRIP_SIZE] = cost val_window.push(val_cost) gl = 100 * (cost_val[-1] / np.min(cost_val) - 1) pk = 1000 * (np.sum(train_strip) / (STRIP_SIZE * np.min(train_strip)) - 1) pq = gl / pk cr, val_conf = evaluate_model2(X_val, y_val_evaluate, mask_val, windowsize, val_fn) class_rate.append(cr) if val_cost < best_val: best_val = val_cost best_cr = cr test_cr, test_conf = evaluate_model2(X_test, y_test, mask_test, windowsize, val_fn) print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f}, Test CR= {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, test_cr, time.time() - time_start)) best_params = las.layers.get_all_param_values(network) else: print("Epoch {} train cost = {}, val cost = {}, " "GL loss = {:.3f}, GQ = {:.3f}, CR = {:.3f} ({:.1f}sec)" .format(epoch + 1, cost_train[-1], cost_val[-1], gl, pq, cr, time.time() - time_start)) if epoch >= validation_window and early_stop2(val_window, best_val, validation_window): break # Show that learning rates are changed by exploding learning rates for encoder layers # The training loss should increase dramatically and learning should diverge if epoch + 1 == 4: print('explode fc1,fc2,fc3 learning rates to 100.0') lr_config['fc1'].set_value(100.0) lr_config['fc2'].set_value(100.0) lr_config['fc3'].set_value(100.0) print('Final Model') print('CR: {}, val loss: {}, Test CR: {}'.format(best_cr, best_val, test_cr)) # plot confusion matrix table_str = plot_confusion_matrix(test_conf, output_classnames, fmt='pipe') print('confusion matrix: ') print(table_str) if 'save_plot' in options: prefix = options['save_plot'] plot_validation_cost(cost_train, cost_val, savefilename='{}.validloss.png'.format(prefix)) with open('{}.confmat.txt'.format(prefix), mode='a') as f: f.write(table_str) f.write('\n\n') if 'write_results' in options: print('writing results to {}'.format(options['write_results'])) results_file = options['write_results'] with open(results_file, mode='a') as f: f.write('{},{},{}\n'.format(test_cr, best_cr, best_val)) if 'save_best' in options: print('saving best model...') las.layers.set_all_param_values(network, best_params) save_model_params(network, options['save_best']) print('best model saved to {}'.format(options['save_best']))