Example #1
0
def read_data_args(data_spec):
    elements = data_spec.split(",")
    pfile_path_list = glob.glob(elements[0])
    dataset_args = {}
    # default settings
    dataset_args['type'] = 'pickle'
    dataset_args['random'] = False
    dataset_args['stream'] = False
    dataset_args['utt'] = False # True: use utterance feature
    dataset_args['partition'] = 1024 * 1024 * 600  # by default the partition size is 600m if stream is True
    dataset_args['lcxt'] = 0
    dataset_args['rcxt'] = 0

    # the type of the data: pickle, pfile   TO-DO: HDF5
    if '.pickle' in data_spec or '.pkl' in data_spec:
        dataset_args['type'] = 'pickle'
    elif '.pfile' in data_spec:
        dataset_args['type'] = 'pfile'
    elif '.scp' in data_spec:
        dataset_args['type'] = 'kaldi'
    else:
        dataset_args['type'] = ''

    for i in range(1, len(elements)):
        element = elements[i]
        arg_value = element.split("=")
        value = arg_value[1]
        key = arg_value[0]
        if key == 'partition':
            dataset_args['partition'] = 1024 * 1024 * int(value.replace('m',''))
        elif key == 'stream':
            dataset_args['stream'] = string2bool(value) # not supported for now
        elif key == 'random':
            dataset_args['random'] = string2bool(value)
        elif key == 'label':
            dataset_args['label'] = value
        elif key == 'lcxt':
            dataset_args['lcxt'] = int(value)
        elif key == 'rcxt':
            dataset_args['rcxt'] = int(value)
        elif key == 'context':
            value = tuple(int(x) for x in value.split(':'))
            if len(value) == 1: value += value
            dataset_args['lcxt'], dataset_args['rcxt'] = value
        elif key == 'ignore-label':
            dataset_args['ignore-label'] = parse_ignore_label(value)
        elif key == 'map-label':
            dataset_args['map-label'] = parse_map_label(value)
        elif key == 'utt':
            dataset_args['utt'] = string2bool(value)
        else:
            dataset_args[key] = value
    
    if dataset_args['utt'] == False:
        print("Don't use utterance save!")
        #assert len(pfile_path_list)==1
        #print(pfile_path_list)

    return pfile_path_list, dataset_args
Example #2
0
def read_data_args(data_spec):
    elements = data_spec.split(",")
    log("data argument = "+str(elements))
    pfile_path_list = sorted(glob.glob(elements[0]))
    log("Found = "+str(pfile_path_list))
    dataset_args = {}
    # default settings
    dataset_args['type'] = 'pickle'
    dataset_args['random'] = False
    dataset_args['stream'] = False
    dataset_args['partition'] = 1024 * 1024 * 600  # by default the partition size is 600m if stream is True
    dataset_args['lcxt'] = 0
    dataset_args['rcxt'] = 0

    # the type of the data: pickle, pfile   TO-DO: HDF5
    if '.pickle' in data_spec or '.pkl' in data_spec:
        dataset_args['type'] = 'pickle'
    elif '.blp' in data_spec:
        dataset_args['type'] = 'blocspack'
    elif '.pfile' in data_spec:
        dataset_args['type'] = 'pfile'
    elif '.scp' in data_spec:
        dataset_args['type'] = 'kaldi'
    else:
        dataset_args['type'] = ''

    for i in range(1, len(elements)):
        element = elements[i]
        arg_value = element.split("=")
        value = arg_value[1]
        key = arg_value[0]
        if key == 'partition':
            dataset_args['partition'] = 1024 * 1024 * int(value.replace('m',''))
        elif key == 'stream':
            dataset_args['stream'] = string2bool(value) # not supported for now
        elif key == 'random':
            dataset_args['random'] = string2bool(value)
        elif key == 'label':
            dataset_args['label'] = value
        elif key == 'lcxt':
            dataset_args['lcxt'] = int(value)
        elif key == 'rcxt':
            dataset_args['rcxt'] = int(value)
        elif key == 'context':
            value = tuple(int(x) for x in value.split(':'))
            if len(value) == 1: value += value
            dataset_args['lcxt'], dataset_args['rcxt'] = value
        elif key == 'ignore-label':
            dataset_args['ignore-label'] = parse_ignore_label(value)
        elif key == 'map-label':
            dataset_args['map-label'] = parse_map_label(value)
        else:
            dataset_args[key] = value
    return pfile_path_list, dataset_args
Example #3
0
    in_scp_file = arguments['in_scp_file']
    out_ark_file = arguments['out_ark_file']
    cnn_param_file = arguments['cnn_param_file']
    cnn_cfg_file = arguments['cnn_cfg_file']
    # network structure
    cfg = cPickle.load(smart_open(cnn_cfg_file, 'r'))

    conv_configs = cfg.conv_layer_configs
    conv_layer_number = len(conv_configs)
    for i in xrange(conv_layer_number):
        conv_configs[i]['activation'] = cfg.conv_activation

    # whether to use the fast mode
    use_fast = cfg.use_fast
    if arguments.has_key('use_fast'):
        use_fast = string2bool(arguments['use_fast'])

    kaldiread = KaldiReadIn(in_scp_file)
    kaldiwrite = KaldiWriteOut(out_ark_file)

    log('> ... setting up the CNN convolution layers')
    input_shape_train = conv_configs[0]['input_shape']
    input_shape_1 = (input_shape_train[1], input_shape_train[2],
                     input_shape_train[3])

    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2**30))

    cnn = CNN_Forward(numpy_rng=rng,
                      theano_rng=theano_rng,
                      conv_layer_configs=conv_configs,
Example #4
0
    # check the arguments
    arg_elements = [sys.argv[i] for i in range(1, len(sys.argv))]
    arguments = parse_arguments(arg_elements)
    required_arguments = ['data', 'nnet_param', 'nnet_cfg', 'output_file', 'layer_index', 'batch_size']
    for arg in required_arguments:
        if (arg in arguments) == False:
            print("Error: the argument %s has to be specified" % (arg)); exit(1)

    # mandatory arguments
    data_spec = arguments['data']
    nnet_param = arguments['nnet_param']
    nnet_cfg = arguments['nnet_cfg']
    output_file = arguments['output_file']
    layer_index = int(arguments['layer_index'])
    batch_size = float(arguments['batch_size'])
    argmax = 'argmax' in arguments and string2bool(arguments['argmax'])
    log("Extracting in batches with size="+str(batch_size))
    if batch_size == -1:
      log("Extracting all features per partition at once")

    # load network configuration and set up the model
    log('> ... setting up the model and loading parameters')
    numpy_rng = numpy.random.RandomState(89677)
    theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
    cfg = pickle.load(smart_open(nnet_cfg,'rb'))
    cfg.init_activation()
    model = None
    if cfg.model_type == 'DNN':
        model = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg)
    elif cfg.model_type == 'CNN':
        model = CNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg, testing = True)
Example #5
0
        'data', 'nnet_param', 'nnet_cfg', 'output_file', 'layer_index',
        'batch_size'
    ]
    for arg in required_arguments:
        if arguments.has_key(arg) == False:
            print "Error: the argument %s has to be specified" % (arg)
            exit(1)

    # mandatory arguments
    data_spec = arguments['data']
    nnet_param = arguments['nnet_param']
    nnet_cfg = arguments['nnet_cfg']
    output_file = arguments['output_file']
    layer_index = int(arguments['layer_index'])
    batch_size = int(arguments['batch_size'])
    argmax = arguments.has_key('argmax') and string2bool(arguments['argmax'])

    # load network configuration and set up the model
    log('> ... setting up the model and loading parameters')
    numpy_rng = numpy.random.RandomState(89677)
    theano_rng = RandomStreams(numpy_rng.randint(2**30))
    cfg = cPickle.load(smart_open(nnet_cfg, 'r'))
    cfg.init_activation()
    model = None
    if cfg.model_type == 'DNN':
        model = DNN(numpy_rng=numpy_rng, theano_rng=theano_rng, cfg=cfg)
    elif cfg.model_type == 'CNN':
        model = CNN(numpy_rng=numpy_rng,
                    theano_rng=theano_rng,
                    cfg=cfg,
                    testing=True)
Example #6
0
    in_scp_file = arguments['in_scp_file']
    out_ark_file = arguments['out_ark_file']
    cnn_param_file = arguments['cnn_param_file']
    cnn_cfg_file = arguments['cnn_cfg_file']
    # network structure
    cfg = cPickle.load(smart_open(cnn_cfg_file,'r'))

    conv_configs = cfg.conv_layer_configs
    conv_layer_number = len(conv_configs)
    for i in xrange(conv_layer_number):
        conv_configs[i]['activation'] = cfg.conv_activation

    # whether to use the fast mode
    use_fast = cfg.use_fast
    if arguments.has_key('use_fast'):
        use_fast = string2bool(arguments['use_fast'])

    kaldiread = KaldiReadIn(in_scp_file)
    kaldiwrite = KaldiWriteOut(out_ark_file)


    log('> ... setting up the CNN convolution layers')
    input_shape_train = conv_configs[0]['input_shape']
    input_shape_1 = (input_shape_train[1], input_shape_train[2], input_shape_train[3])

    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))

    cnn = CNN_Forward(numpy_rng = rng, theano_rng=theano_rng, conv_layer_configs = conv_configs, use_fast = use_fast)
    _file2nnet(cnn.conv_layers, set_layer_num = len(conv_configs), filename=cnn_param_file)
    out_function = cnn.build_out_function()
Example #7
0
    # check the arguments
    arg_elements = [sys.argv[i] for i in range(1, len(sys.argv))]
    arguments = parse_arguments(arg_elements)
    required_arguments = ['data', 'nnet_param', 'nnet_cfg', 'output_file', 'layer_index', 'batch_size']
    for arg in required_arguments:
        if arguments.has_key(arg) == False:
            print "Error: the argument %s has to be specified" % (arg); exit(1)

    # mandatory arguments
    data_spec = arguments['data']
    nnet_param = arguments['nnet_param']
    nnet_cfg = arguments['nnet_cfg']
    output_file = arguments['output_file']
    layer_index = int(arguments['layer_index'])
    batch_size = float(arguments['batch_size'])
    argmax = arguments.has_key('argmax') and string2bool(arguments['argmax'])

    # load network configuration and set up the model
    log('> ... setting up the model and loading parameters')
    numpy_rng = numpy.random.RandomState(89677)
    theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
    cfg = cPickle.load(smart_open(nnet_cfg,'r'))
    cfg.init_activation()
    model = None
    if cfg.model_type == 'DNN':
        model = DNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg)
    elif cfg.model_type == 'CNN':
        model = CNN(numpy_rng=numpy_rng, theano_rng = theano_rng, cfg = cfg, testing = True)

    # load model parameters
    _file2nnet(model.layers, filename = nnet_param)
Example #8
0
def main(arg_elements):

    # check the arguments
    arguments = parse_arguments(arg_elements)
    required_arguments = [
        'data', 'nnet_param', 'nnet_cfg', 'output_file', 'layer_index',
        'batch_size'
    ]
    for arg in required_arguments:
        if arguments.has_key(arg) == False:
            print "Error: the argument %s has to be specified" % (arg)
            exit(1)

    # mandatory arguments
    data_spec = arguments['data']
    nnet_param = arguments['nnet_param']
    nnet_cfg = arguments['nnet_cfg']
    output_file = arguments['output_file']
    layer_index = int(arguments['layer_index'])
    batch_size = int(arguments['batch_size'])
    argmax = arguments.has_key('argmax') and string2bool(arguments['argmax'])

    # load network configuration and set up the model
    log('> ... setting up the model and loading parameters')
    numpy_rng = numpy.random.RandomState(89677)
    theano_rng = RandomStreams(numpy_rng.randint(2**30))
    cfg = cPickle.load(smart_open(nnet_cfg, 'r'))
    cfg.init_activation()
    model = None
    if cfg.model_type == 'DNN':
        model = DNN(numpy_rng=numpy_rng, theano_rng=theano_rng, cfg=cfg)
    elif cfg.model_type == 'CNN':
        model = CNN(numpy_rng=numpy_rng,
                    theano_rng=theano_rng,
                    cfg=cfg,
                    testing=True)

    # load model parameters
    _file2nnet(model.layers, filename=nnet_param)

    # initialize data reading
    cfg.init_data_reading_test(data_spec)

    # get the function for feature extraction
    log('> ... getting the feat-extraction function')
    extract_func = model.build_extract_feat_function(layer_index)

    output_mats = [
    ]  # store the features for all the data in memory. TODO: output the features in a streaming mode
    log('> ... generating features from the specified layer')
    while (not cfg.test_sets.is_finish()):  # loop over the data
        cfg.test_sets.load_next_partition(cfg.test_xy)
        batch_num = int(
            math.ceil(1.0 * cfg.test_sets.cur_frame_num / batch_size))

        for batch_index in xrange(batch_num):  # loop over mini-batches
            start_index = batch_index * batch_size
            end_index = min((batch_index + 1) * batch_size,
                            cfg.test_sets.cur_frame_num
                            )  # the residue may be smaller than a mini-batch
            output = extract_func(
                cfg.test_x.get_value()[start_index:end_index])
            output_mats.append(output)

    output_mat = numpy.concatenate(output_mats)
    if argmax:
        output_mat = output_mat.argmax(axis=1)

    # output the feature representations using pickle
    f = smart_open(output_file, 'wb')
    cPickle.dump(output_mat, f, cPickle.HIGHEST_PROTOCOL)

    log('> ... the features are stored in ' + output_file)