def load_model(model_config): """ Loads the model specified by the model config. """ model_name = model_config['architecture'].lower() if model_name == 'vrnn': from vrnn import VRNN return VRNN(model_config) elif model_name == 'srnn': from srnn import SRNN return SRNN(model_config) # elif model_name == 'dvbf': # from dvbf import DVBF # return DVBF(model_config) elif model_name == 'svg': from svg import SVG return SVG(model_config) elif model_name in ['conv', 'convolutional']: from convolutional import ConvolutionalDLVM return ConvolutionalDLVM(model_config) elif model_name in ['fc', 'fully_connected']: from fully_connected import FullyConnectedDLVM return FullyConnectedDLVM(model_config) else: raise Exception('Model architecture not found.')
def calculate_perplexity(config): """ Calculate perplexity for a given (processed) test data """ # load the config files and vocabulary files if not os.path.exists(config.model_path + '.meta'): raise Exception("Could not open and/or read model file {}".format( config.model_path + '.meta')) # process the test corpus and load it into batches # vocabulary is loaded from the model directory test_data = DataProcessor(config.test_file, config.batch_size, config.seq_length, is_training=False, unk='<unk>', history_size=config.history_size, vocab_dir_path=config.save_dir) # define/load the model with tf.variable_scope("Model", reuse=False): if config.model == 'lsrc': model_test = LSRC(config, False) elif config.model == 'wi-srnn' or config.model == 'wd-srnn' or config.model == 'ff-srnn': model_test = SRNN(config, False) elif config.model == 'lstm' or config.model == 'lstmp' or config.model == 'rnn' or config.model == 'gru': model_test = Basic_RNN(config, False) else: raise Exception("model type not supported: {}".format( config.model)) with tf.Session() as session: # restore the model saver = tf.train.Saver(tf.global_variables()) saver.restore(session, config.model_path) test_perplexity = model_test.run_model(session, test_data, eval_op=None, verbosity=10000, verbose=True) print("\n[SUMMARY] Perplexity: %.3f" % test_perplexity) print('========================\n')
type=str, default='relu', help='activation function') parser.add_argument('--shift_mean', default=True, help='subtract pixel mean from the input') #################################### ########loss parser.add_argument( '--content_loss_factor', type=float, default=1e-2, help='content loss factor when training generator oriented') parser.add_argument('--perceptual_loss_factor', type=float, default=1, help='perceptual loss factor when training ' 'generator oriented') arg = parser.parse_args() return arg if __name__ == '__main__': arg = parse_args() srnn = SRNN(arg, trainable=False) test_loader = load_test_dataset(arg) srnn.test(test_loader)
if args.trainstop: train_stop_index = args.trainstop if args.validstop: valid_stop_index = args.validstop random_state = np.random.RandomState(1999) np.random.seed(1337) if args.sample: print('will do sampling') pred_srnn = SRNN(batch_size=1, seq_len=SLOW_FS, slow_fs=SLOW_FS, slow_dim=SLOW_DIM, dim=DIM, mid_fs=2, q_levels=256, mlp_activation='relu') pred_srnn.load_weights(args.sample) print(pred_srnn.model().summary()) try: from keras.utils import plot_model plot_model(pred_srnn.top_tier_model_predictor, to_file='top_model.png') plot_model(pred_srnn.mid_tier_model_predictor, to_file='mid_model.png') plot_model(pred_srnn.slow_tier_model_predictor, to_file='slow_model.png') except: print('failed to plot models to png') pass
type=str, default='relu', help='activation function') #################################### ########loss parser.add_argument( '--content_loss_factor', type=float, default=1, #0.01 help='content loss factor when training generator oriented') parser.add_argument( '--perceptual_loss_factor', type=float, default=0, #1 help='perceptual loss factor when training ' 'generator oriented') arg = parser.parse_args() return arg if __name__ == '__main__': arg = parse_args() train_loader = load_train_dataset(arg, single=False) valid_loader = load_valid_dataset(arg, single=True) srnn = SRNN(arg, trainable=True) srnn.train(train_loader, valid_loader)