def load_yaml_model(model_path, state_path, make_conv_model, video_frame): assert_equal(os.path.splitext(model_path)[1], '.yaml') assert_equal(os.path.splitext(state_path)[1], '.h5') def get_datasets(yaml_dict): def get_composite_dataset(composite_dict): fg_path = os.path.join(data_path, composite_dict['fg_path']) bg_path = os.path.join(data_path, composite_dict['bg_path']) return CompositingDataset(MemmapDataset(fg_path), MemmapDataset(bg_path)) datasets_clause = yaml_dict['datasets'] return (get_composite_dataset(datasets_clause['training']), get_composite_dataset(datasets_clause['validation'])) with open(model_path, 'r') as yaml_file: yaml_dict = yaml.load(yaml_file) hyperparams_dict = yaml_dict['hyperparams'] training_set = get_datasets(yaml_dict)[0] iterator_rng = numpy.random.RandomState(2352) training_iter = training_set.iterator( 'random_vec3', batch_size=hyperparams_dict['training_batch_size'], rng=iterator_rng) input_image_node = training_iter.make_input_nodes()[0] # Doesn't matter; the random weights this generates will be # overwritten by the weights in the .h5 file numpy_rng = numpy.random.RandomState(2342) theano_rng = RandomStreams(3463) # for dropout masks model = IdAndCameraDirModel(input_image_node, yaml_dict, numpy_rng, theano_rng) if make_conv_model: assert_equal(input_image_node.output_format.axes, ('b', '0', '1', 'c')) input_image_node = InputNode( fmt=DenseFormat(axes=input_image_node.output_format.axes, shape=(-1, video_frame.shape[0], video_frame.shape[1], 3), dtype=input_image_node.output_format.dtype)) model = VideoModel(model, input_image_node, yaml_dict, numpy_rng, theano_rng) with h5py.File(state_path, mode='r') as h5_file: model.load_from_h5(h5_file['model'], disable_class_name_check=make_conv_model) return model
def main(): args = parse_args() def read_yaml(filepath): with open(filepath, 'r') as yaml_file: return yaml.load(yaml_file) yaml_dict = read_yaml(args.yaml) input_h5 = h5py.File(args.input_h5, mode='r') # output_yaml = read_yaml(args.output_yaml) output_h5 = h5py.File(args.output_h5, mode='w') numpy_seed = 1341 theano_seed = 2353 training_iter, validation_iter = get_iters(yaml_dict, numpy_seed) input_nodes = training_iter.make_input_nodes() image_node = input_nodes[0] # Build input model, load params from input_h5 input_model = IdAndCameraDirModel(image_node, yaml_dict, RandomState(numpy_seed), RandomStreams(theano_seed)) input_model.load_from_h5(input_h5['model']) # Build TrainingState around input_model, load its momentum etc from # input_h5 input_loss_node = IdAndCameraDirLoss(input_model.id_layers[-1], input_model.cam_dir_layers[-1], input_nodes, blank_id=6) input_state = IdAndCameraDirTrainingState(input_model, input_loss_node, yaml_dict, training_iter, validation_iter) # Need to disable class name check, since we've renamed the class from # TrainingState to IdAndCameraDirTrainingState at some point betw. sept 3 # and sept 12 input_state.load_from_h5(input_h5['training_state'], disable_class_name_check=True) # Build output (conv) model from the same yaml dict as input model, # copy params from input model. output_model = IdAndCameraDirModelConv(image_node, yaml_dict, RandomState(numpy_seed), RandomStreams(theano_seed)) copy_model_params(input_model, output_model) output_model.save_to_h5(output_h5.create_group('model')) # Build TrainingState around output model, check that model params have # been copied correctly by comparing against input TrainingState's params, # and also copy over the momenta etc from input TrainingState. output_loss_node = IdAndCameraDirConvLoss(output_model.id_layers[-1], output_model.cam_dir_layers[-1], input_nodes, blank_id=6) output_state = IdAndCameraDirTrainingState(output_model, output_loss_node, yaml_dict, training_iter, validation_iter) copy_state_except_params(input_state, output_state) output_state.save_to_h5(output_h5.create_group('training_state'))