示例#1
0
def load_vgg_all_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D.p'
    size = 554227541

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i += 1
        if i == 43:
            break
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])

    # to load the fc6 and fc7 from caffe into neon fc layers after ROI pooling
    neon_fc_layers = model.layers.layers[2].layers[1].layers[0].layers[2:5] +\
        model.layers.layers[2].layers[1].layers[0].layers[6:9]
    vgg_fc_layers = param_dict_list[44:47] + param_dict_list[48:51]

    for layer, ps in zip(neon_fc_layers, vgg_fc_layers):
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])
示例#2
0
文件: util.py 项目: rlugojr/neon
def load_vgg_all_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D.p'
    size = 554227541

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i += 1
        if i == 43:
            break
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])

    # to load the fc6 and fc7 from caffe into neon fc layers after ROI pooling
    neon_fc_layers = model.layers.layers[2].layers[1].layers[0].layers[2:5] +\
        model.layers.layers[2].layers[1].layers[0].layers[6:9]
    vgg_fc_layers = param_dict_list[44:47] + param_dict_list[48:51]

    for layer, ps in zip(neon_fc_layers, vgg_fc_layers):
        layer.load_weights(ps, load_states=True)
        print(layer.name + " <-- " + ps['config']['name'])
示例#3
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained Alexnet using ImageNet I1K ...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers_to_optimize]
    param_dict_list = pdict['layer_params_states']
    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i = i+1
        print i, layer.name
        layer.set_params(ps)
        if 'states' in ps:
            layer.set_states(ps)
        if i == 10:
            print 'Only load the pre-trained weights up to conv5 layer of Alexnet'
            break
示例#4
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/old/pre_v1.4.0/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'Loading the Alexnet pre-trained with ImageNet I1K from: ' + filepath
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers]

    param_dict_list = pdict['model']['config']['layers']
    skip_loading = False
    for i, layer in enumerate(param_layers):
        if not load_pre_trained_weight(i, layer):
            skip_loading = True
        if not skip_loading:
            ps = param_dict_list[i]
            print "Loading weights for:{} [src: {}]".format(
                layer.name, ps['config']['name'])
            layer.load_weights(ps, load_states=True)
        else:
            config_name = param_dict_list[i]['config']['name'] if i < len(
                param_dict_list) else ""
            print "Skipped loading weights for: {} [src: {}]".format(
                layer.name, config_name)

    return
示例#5
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained Alexnet using ImageNet I1K ...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers_to_optimize]
    param_dict_list = pdict['layer_params_states']
    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i = i + 1
        print i, layer.name
        layer.set_params(ps)
        if 'states' in ps:
            layer.set_states(ps)
        if i == 10:
            print 'Only load the pre-trained weights up to conv5 layer of Alexnet'
            break
示例#6
0
def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/'
    filename = 'VGG_ILSVRC_16_layers_fc_reduced_fused_conv_bias.p'
    size = 86046032

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model with dilated convolutions...')
    pdict = load_obj(filepath)

    model_layers = [l for l in model.layers.layers[0].layers]
    # convert source model into dictionary with layer name as keys
    src_layers = {layer['config']['name']: layer for layer in pdict['model']['config']['layers']}

    i = 0
    for layer in model_layers:
        if layer.classnm == 'Convolution_bias' and i < 15:
            # no states in above parameter file
            layer.load_weights(src_layers['Convolution_bias_'+str(i)], load_states=False)
            print('{} loaded from source file'.format(layer.name))
            i += 1
        elif hasattr(layer, 'W'):
            print('Skipping {} layer'.format(layer.name))
示例#7
0
def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/'
    filename = 'VGG_ILSVRC_16_layers_fc_reduced.p'
    size = 244190212

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print(
        'De-serializing the pre-trained VGG16 model with dilated convolutions...'
    )
    pdict = load_obj(filepath)

    model_layers = [l for l in model.layers.layers[0].layers]

    # convert source model into dictionary with layer name as keys
    src_layers = {
        layer['config']['name']: layer
        for layer in pdict['model']['config']['layers']
    }

    for layer in model_layers:
        if layer.name in src_layers.keys():
            layer.load_weights(src_layers[layer.name], load_states=True)
            print('{} loaded from source file'.format(layer.name))
        elif hasattr(layer, 'W'):
            print('Skipping {} layer'.format(layer.name))
示例#8
0
def test_pascalvoc(backend_default, data):
    url = "https://s3-us-west-1.amazonaws.com/nervana-pascal-voc-data"
    filename = "pascal_data_ref.pkl"
    size = 423982870

    workdir, filepath = Dataset._valid_path_append(data, "", filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    with open(filepath, "rb") as handle:
        neon_data_ref = pickle.loads(handle.read())

    n_mb = neon_data_ref["n_mb"]
    img_per_batch = neon_data_ref["img_per_batch"]
    rois_per_img = neon_data_ref["rois_per_img"]
    dataset = neon_data_ref["dataset"]
    year = neon_data_ref["year"]
    output_type = neon_data_ref["output_type"]
    rois_random_sample = neon_data_ref["rois_random_sample"]
    shuffle = neon_data_ref["shuffle"]

    train_set = PASCALVOC(
        dataset,
        year,
        path=data,
        output_type=output_type,
        n_mb=n_mb,
        img_per_batch=img_per_batch,
        rois_per_img=rois_per_img,
        rois_random_sample=rois_random_sample,
        shuffle=shuffle,
    )

    # X_batch[0]: image - (3e6, 2)
    # X_batch[1]: ROIs - (128, 5)
    # Y_batch[0]: labels - (21, 128)
    # Y_batch[1][0]: bbtarget - (84, 128)
    # Y_batch[1][1]: bb mask - (84, 128)
    for mb_i, (X_batch, y_batch) in enumerate(train_set):

        image_neon = X_batch[0].get()
        image_ref = neon_data_ref["X_batch_img"][mb_i]

        rois_neon = X_batch[1].get()
        rois_ref = neon_data_ref["X_batch_rois"][mb_i]

        label_neon = y_batch[0].get()
        label_ref = neon_data_ref["y_batch_label"][mb_i]

        bbtarget_neon = y_batch[1][0].get()
        bbtarget_ref = neon_data_ref["y_batch_bbtarget"][mb_i]

        mask_neon = y_batch[1][1].get()
        mask_ref = neon_data_ref["y_batch_mask"][mb_i]

        assert np.allclose(image_neon, image_ref, atol=1e-5, rtol=0)
        assert np.allclose(rois_neon, rois_ref, atol=1e-5, rtol=0)
        assert np.allclose(label_neon, label_ref, atol=1e-5, rtol=0)
        assert np.allclose(bbtarget_neon, bbtarget_ref, atol=1e-5, rtol=0)
        assert np.allclose(mask_neon, mask_ref, atol=1e-5, rtol=0)
示例#9
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/old/pre_v1.4.0/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'Loading the Alexnet pre-trained with ImageNet I1K from: ' + filepath
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers]
        
    param_dict_list = pdict['model']['config']['layers']
    skip_loading = False
    for i, layer in enumerate(param_layers):
        if not load_pre_trained_weight(i, layer):
            skip_loading = True
        if not skip_loading:
            ps = param_dict_list[i]
            print "Loading weights for:{} [src: {}]".format(layer.name, ps['config']['name'])
            layer.load_weights(ps, load_states=True)
        else:
            config_name = param_dict_list[i]['config']['name'] if i < len(param_dict_list) else ""
            print "Skipped loading weights for: {} [src: {}]".format(layer.name, config_name)
        
    return
示例#10
0
def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/'
    filename = 'VGG_ILSVRC_16_layers_fc_reduced_fused_conv_bias.p'
    size = 86046032

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print(
        'De-serializing the pre-trained VGG16 model with dilated convolutions...'
    )
    pdict = load_obj(filepath)

    model_layers = [l for l in model.layers.layers[0].layers]
    # convert source model into dictionary with layer name as keys
    src_layers = {
        layer['config']['name']: layer
        for layer in pdict['model']['config']['layers']
    }

    i = 0
    for layer in model_layers:
        if layer.classnm == 'Convolution_bias' and i < 15:
            # no states in above parameter file
            layer.load_weights(src_layers['Convolution_bias_' + str(i)],
                               load_states=False)
            print('{} loaded from source file'.format(layer.name))
            i += 1
        elif hasattr(layer, 'W'):
            print('Skipping {} layer'.format(layer.name))
示例#11
0
def test_pascalvoc(backend_default, data):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-pascal-voc-data'
    filename = 'pascal_data_ref.pkl'
    size = 423982870

    workdir, filepath = Dataset._valid_path_append(data, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    with open(filepath, 'rb') as handle:
        neon_data_ref = pickle.loads(handle.read())

    n_mb = neon_data_ref['n_mb']
    img_per_batch = neon_data_ref['img_per_batch']
    rois_per_img = neon_data_ref['rois_per_img']
    dataset = neon_data_ref['dataset']
    year = neon_data_ref['year']
    output_type = neon_data_ref['output_type']
    rois_random_sample = neon_data_ref['rois_random_sample']
    shuffle = neon_data_ref['shuffle']

    train_set = PASCALVOC(dataset,
                          year,
                          path=data,
                          output_type=output_type,
                          n_mb=n_mb,
                          img_per_batch=img_per_batch,
                          rois_per_img=rois_per_img,
                          rois_random_sample=rois_random_sample,
                          shuffle=shuffle)

    # X_batch[0]: image - (3e6, 2)
    # X_batch[1]: ROIs - (128, 5)
    # Y_batch[0]: labels - (21, 128)
    # Y_batch[1][0]: bbtarget - (84, 128)
    # Y_batch[1][1]: bb mask - (84, 128)
    for mb_i, (X_batch, y_batch) in enumerate(train_set):

        image_neon = X_batch[0].get()
        image_ref = neon_data_ref['X_batch_img'][mb_i]

        rois_neon = X_batch[1].get()
        rois_ref = neon_data_ref['X_batch_rois'][mb_i]

        label_neon = y_batch[0].get()
        label_ref = neon_data_ref['y_batch_label'][mb_i]

        bbtarget_neon = y_batch[1][0].get()
        bbtarget_ref = neon_data_ref['y_batch_bbtarget'][mb_i]

        mask_neon = y_batch[1][1].get()
        mask_ref = neon_data_ref['y_batch_mask'][mb_i]

        assert np.allclose(image_neon, image_ref, atol=1e-5, rtol=0)
        assert np.allclose(rois_neon, rois_ref, atol=1e-5, rtol=0)
        assert np.allclose(label_neon, label_ref, atol=1e-5, rtol=0)
        assert np.allclose(bbtarget_neon, bbtarget_ref, atol=1e-5, rtol=0)
        assert np.allclose(mask_neon, mask_ref, atol=1e-5, rtol=0)
示例#12
0
def get_data():
    """
    Download bilingual text dataset for Machine translation example.
    """

    # vocab_size and time_steps are hard coded here
    vocab_size = 16384
    time_steps = 20

    # download dataset
    url = 'http://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/data/'
    filename = 'bitexts.tgz'
    size = 1313280000

    parser = NeonArgparser(__doc__)
    args = parser.parse_args(gen_be=False)
    data_dir = os.path.join(args.data_dir, 'nmt')

    _, filepath = Dataset._valid_path_append(data_dir, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    # extract selected datasets
    datafiles = dict()
    datafiles['un2000'] = ('un2000_pc34.en.gz', 'un2000_pc34.fr.gz')
    datafiles['europarl7'] = ('ep7_pc45.en.gz', 'ep7_pc45.fr.gz')

    extractpath = os.path.join(data_dir, 'bitexts.selected')
    with tarfile.open(filepath, 'r') as tar_ref:
        for dset, files in datafiles.items():
            datasetpath = os.path.join(data_dir, dset)
            # extract the files for dataset, if not already there
            for zipped in files:
                fname = '.'.join(zipped.split('.')[:-1])
                fpath = os.path.join(datasetpath, fname)
                if not os.path.exists(fpath):
                    gzpath = os.path.join(extractpath, zipped)
                    if not os.path.exists(gzpath):
                        select = [ti for ti in tar_ref if os.path.split(ti.name)[1] == zipped]
                        tar_ref.extractall(path=data_dir, members=select)
                    # get contents of gz files
                    if not os.path.exists(datasetpath):
                        os.makedirs(datasetpath)
                    with gzip.open(gzpath, 'r') as fin, open(fpath, 'w') as fout:
                        fout.write(fin.read())
                    os.remove(gzpath)

    if os.path.exists(extractpath):
        os.rmdir(extractpath)

    # process data and save to h5 file
    # loop through all datasets and get train and valid splits
    for dataset in datafiles.keys():

        s_vocab, t_vocab = create_h5py(data_dir, dataset, 'train',
                                       vocab_size=vocab_size, time_steps=time_steps)
        create_h5py(data_dir, dataset, 'valid', s_vocab=s_vocab, t_vocab=t_vocab,
                    time_steps=time_steps)
示例#13
0
    def load_imagenet_weights(self, model_path):
        # download trained Alexnet weights
        url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/'
        filename = 'alexnet_conv_ns_fused_conv_bias.p'
        size = 19763308

        _, filepath = Dataset._valid_path_append(model_path, '', filename)
        if not osp.exists(filepath):
            Dataset.fetch_dataset(url, filename, filepath, size)

        return filepath
示例#14
0
    def load_imagenet_weights(self, model_path):
        # download trained Alexnet weights
        url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/'
        filename = 'alexnet_conv_ns_fused_conv_bias.p'
        size = 19763308

        _, filepath = Dataset._valid_path_append(model_path, '', filename)
        if not osp.exists(filepath):
            Dataset.fetch_dataset(url, filename, filepath, size)

        return filepath
示例#15
0
    def load_imagenet_weights(self, model_path):
        # download trained Alexnet weights
        url = "https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/"
        filename = "alexnet_conv_ns.p"
        size = 20550623

        _, filepath = Dataset._valid_path_append(model_path, "", filename)
        if not osp.exists(filepath):
            Dataset.fetch_dataset(url, filename, filepath, size)

        return filepath
示例#16
0
文件: util.py 项目: JediKoder/neon
def load_vgg_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D_Conv.p'
    size = 169645138

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    neon_logger.display('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    for layer, ps in zip(param_layers, param_dict_list):
        neon_logger.display("{}".format(layer.name, ps['config']['name']))
        layer.load_weights(ps, load_states=True)
示例#17
0
文件: util.py 项目: zmoon111/neon
def load_vgg_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D_Conv.p'
    size = 169645138

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    neon_logger.display('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    for layer, ps in zip(param_layers, param_dict_list):
        neon_logger.display("{}".format(layer.name, ps['config']['name']))
        layer.load_weights(ps, load_states=True)
示例#18
0
    def bleu_score(self, sents, targets):
        """
        Compute the BLEU score from a list of predicted sentences and reference sentences

        Args:
            sents (list): list of predicted sentences
            targets (list): list of reference sentences where each element is a list of
                            multiple references.
        """

        num_ref = len(targets[0])
        output_file = self.path + '/output'
        reference_files = [
            self.path + '/reference%d' % i for i in range(num_ref)
        ]
        bleu_script_url = 'https://raw.githubusercontent.com/karpathy/neuraltalk/master/eval/'
        bleu_script = 'multi-bleu.perl'

        neon_logger.display("Writing output and reference sents to dir %s" %
                            self.path)

        output_f = open(output_file, 'w+')
        for sent in sents:
            sent = sent.strip(self.end_token).split()
            output_f.write(" ".join(sent) + '\n')

        reference_f = [open(f, 'w') for f in reference_files]
        for i in range(num_ref):
            for target_sents in targets:
                reference_f[i].write(target_sents[i] + '\n')

        output_f.close()
        [x.close() for x in reference_f]

        owd = os.getcwd()
        os.chdir(self.path)
        if not os.path.exists(bleu_script):
            Dataset.fetch_dataset(bleu_script_url, bleu_script, bleu_script,
                                  6e6)
        bleu_command = 'perl multi-bleu.perl reference < output'
        neon_logger.display(
            "Executing bleu eval script: {}".format(bleu_command))
        os.system(bleu_command)
        os.chdir(owd)
示例#19
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained Alexnet using ImageNet I1K ...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    for layer, ps in zip(param_layers, param_dict_list):
        print layer.name, ps['config']['name']
        layer.load_weights(ps, load_states=True)
        if ps['config']['name'] == 'Pooling_2':
            print 'Only load the pre-trained weights up to conv5 layer of Alexnet'
            break
示例#20
0
def load_imagenet_weights(model, path):
    # load a pre-trained Alexnet from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/alexnet/'
    filename = 'alexnet.p'
    size = 488808400

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained Alexnet using ImageNet I1K ...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    for layer, ps in zip(param_layers, param_dict_list):
        print layer.name, ps['config']['name']
        layer.load_weights(ps, load_states=True)
        if ps['config']['name'] == 'Pooling_2':
            print 'Only load the pre-trained weights up to conv5 layer of Alexnet'
            break
示例#21
0
    def bleu_score(self, sents, targets):
        """
        Compute the BLEU score from a list of predicted sentences and reference sentences

        Args:
            sents (list): list of predicted sentences
            targets (list): list of reference sentences where each element is a list of
                            multiple references.
        """

        num_ref = len(targets[0])
        output_file = self.path + '/output'
        reference_files = [self.path + '/reference%d' % i for i in range(num_ref)]
        bleu_script_url = 'https://raw.githubusercontent.com/karpathy/neuraltalk/master/eval/'
        bleu_script = 'multi-bleu.perl'

        print "Writing output and reference sents to dir %s" % self.path

        output_f = open(output_file, 'w+')
        for sent in sents:
            sent = sent.strip(self.end_token).split()
            output_f.write(" ".join(sent) + '\n')

        reference_f = [open(f, 'w') for f in reference_files]
        for i in range(num_ref):
            for target_sents in targets:
                reference_f[i].write(target_sents[i] + '\n')

        output_f.close()
        [x.close() for x in reference_f]

        owd = os.getcwd()
        os.chdir(self.path)
        if not os.path.exists(bleu_script):
            Dataset.fetch_dataset(bleu_script_url, bleu_script, bleu_script, 6e6)
        bleu_command = 'perl multi-bleu.perl reference < output'
        print "Executing bleu eval script: ", bleu_command
        os.system(bleu_command)
        os.chdir(owd)
示例#22
0
def load_vgg_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D.p'
    size = 554227541

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained VGG16 model...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i += 1
        print layer.name, ps['config']['name']
        layer.load_weights(ps, load_states=True)
        if i == 43:
            break
示例#23
0
def load_vgg_weights(model, path):
    # load a pre-trained VGG16 from Neon model zoo to the local
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D.p'
    size = 554227541

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print 'De-serializing the pre-trained VGG16 model...'
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']
    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        i += 1
        print layer.name, ps['config']['name']
        layer.load_weights(ps, load_states=True)
        if i == 43:
            break
示例#24
0
def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D_Conv_fused_conv_bias.p'
    size = 58867537

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        # finished loading param_dict_list[00 - 29] and param_layers[00-29]
        if i == 30:
            break
        layer.load_weights(ps, load_states=False)
        i += 1
        print(layer.name + " <-- " + ps['config']['name'])
示例#25
0
def load_vgg_weights(model, path):
    url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
    filename = 'VGG_D_Conv_fused_conv_bias.p'
    size = 58867537

    workdir, filepath = Dataset._valid_path_append(path, '', filename)
    if not os.path.exists(filepath):
        Dataset.fetch_dataset(url, filename, filepath, size)

    print('De-serializing the pre-trained VGG16 model...')
    pdict = load_obj(filepath)

    param_layers = [l for l in model.layers.layers[0].layers]
    param_dict_list = pdict['model']['config']['layers']

    i = 0
    for layer, ps in zip(param_layers, param_dict_list):
        # finished loading param_dict_list[00 - 29] and param_layers[00-29]
        if i == 30:
            break
        layer.load_weights(ps, load_states=False)
        i += 1
        print(layer.name + " <-- " + ps['config']['name'])
示例#26
0
 def load_data(self):
     self.file_path = Dataset.load_zip('i1kmeta', self.path)
     return self.file_path
示例#27
0
def fetch_dataset(url, sourcefile, destfile, totalsz):
    Dataset.fetch_dataset(url, sourcefile, destfile, totalsz)
示例#28
0
def _valid_path_append(path, *args):
    return Dataset._valid_path_append(path, *args)
示例#29
0
 def load_data(self):
     self.file_path = Dataset.load_zip('i1kmeta', self.path)
     return self.file_path
示例#30
0
def load_text(dataset, path="."):
    logger.error('load_text function is deprecated as is the data_meta dictionary, '
                 'please use the new Dataset classes or the specific load_ function '
                 'for the dataset being used.')
    # deprecated method for load_text uses dataset as key to index
    # dataset_meta dictionary, for temporary backward compat, placed
    # dataset_meta dictionary here
    dataset_meta = {
        'mnist': {
            'size': 15296311,
            'file': 'mnist.pkl.gz',
            'url': 'https://s3.amazonaws.com/img-datasets',
            'func': load_mnist
        },
        'cifar-10': {
            'size': 170498071,
            'file': 'cifar-10-python.tar.gz',
            'url': 'http://www.cs.toronto.edu/~kriz',
            'func': load_cifar10
        },
        'babi': {
            'size': 11745123,
            'file': 'tasks_1-20_v1-2.tar.gz',
            'url': 'http://www.thespermwhale.com/jaseweston/babi',
            'func': load_babi
        },
        'ptb-train': {
            'size': 5101618,
            'file': 'ptb.train.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_train
        },
        'ptb-valid': {
            'size': 399782,
            'file': 'ptb.valid.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_valid
        },
        'ptb-test': {
            'size': 449945,
            'file': 'ptb.test.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_test
        },
        'hutter-prize': {
            'size': 35012219,
            'file': 'enwik8.zip',
            'url': 'http://mattmahoney.net/dc',
            'func': load_hutter_prize
        },
        'shakespeare': {
            'size': 4573338,
            'file': 'shakespeare_input.txt',
            'url': 'http://cs.stanford.edu/people/karpathy/char-rnn',
            'func': load_shakespeare
        },
        'flickr8k': {
            'size': 49165563,
            'file': 'flickr8k.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_flickr8k
        },
        'flickr30k': {
            'size': 195267563,
            'file': 'flickr30k.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_flickr30k
        },
        'coco': {
            'size': 738051031,
            'file': 'coco.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_coco
        },
        'i1kmeta': {
            'size': 758648,
            'file': 'neon_ILSVRC2012_devmeta.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/imagenet',
            'func': load_i1kmeta
        },
        'imdb': {
            'size': 33213513,
            'file': 'imdb.pkl',
            'url': 'https://s3.amazonaws.com/text-datasets',
            'func': load_imdb,
        }
    }
    meta = dataset_meta[dataset]
    ds = Dataset(meta['file'], meta['url'], meta['size'], path=path)
    return ds.load_zip(ds.filename, ds.size)
示例#31
0
def fetch_dataset(url, sourcefile, destfile, totalsz):
    Dataset.fetch_dataset(url, sourcefile, destfile, totalsz)
示例#32
0
def _valid_path_append(path, *args):
    return Dataset._valid_path_append(path, *args)
示例#33
0
def load_text(dataset, path="."):
    logger.error('load_text function is deprecated as is the data_meta dictionary, '
                 'please use the new Dataset classes or the specific load_ function '
                 'for the dataset being used.')
    # deprecated method for load_text uses dataset as key to index
    # dataset_meta dictionary, for temporary backward compat, placed
    # dataset_meta dictionary here
    dataset_meta = {
        'mnist': {
            'size': 15296311,
            'file': 'mnist.pkl.gz',
            'url': 'https://s3.amazonaws.com/img-datasets',
            'func': load_mnist
        },
        'cifar-10': {
            'size': 170498071,
            'file': 'cifar-10-python.tar.gz',
            'url': 'http://www.cs.toronto.edu/~kriz',
            'func': load_cifar10
        },
        'babi': {
            'size': 11745123,
            'file': 'tasks_1-20_v1-2.tar.gz',
            'url': 'http://www.thespermwhale.com/jaseweston/babi',
            'func': load_babi
        },
        'ptb-train': {
            'size': 5101618,
            'file': 'ptb.train.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_train
        },
        'ptb-valid': {
            'size': 399782,
            'file': 'ptb.valid.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_valid
        },
        'ptb-test': {
            'size': 449945,
            'file': 'ptb.test.txt',
            'url': 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data',
            'func': load_ptb_test
        },
        'hutter-prize': {
            'size': 35012219,
            'file': 'enwik8.zip',
            'url': 'http://mattmahoney.net/dc',
            'func': load_hutter_prize
        },
        'shakespeare': {
            'size': 4573338,
            'file': 'shakespeare_input.txt',
            'url': 'http://cs.stanford.edu/people/karpathy/char-rnn',
            'func': load_shakespeare
        },
        'flickr8k': {
            'size': 49165563,
            'file': 'flickr8k.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_flickr8k
        },
        'flickr30k': {
            'size': 195267563,
            'file': 'flickr30k.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_flickr30k
        },
        'coco': {
            'size': 738051031,
            'file': 'coco.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/image-caption',
            'func': load_coco
        },
        'i1kmeta': {
            'size': 758648,
            'file': 'neon_ILSVRC2012_devmeta.zip',
            'url': 'https://s3-us-west-1.amazonaws.com/neon-stockdatasets/imagenet',
            'func': load_i1kmeta
        },
        'imdb': {
            'size': 33213513,
            'file': 'imdb.pkl',
            'url': 'https://s3.amazonaws.com/text-datasets',
            'func': load_imdb,
        }
    }
    meta = dataset_meta[dataset]
    ds = Dataset(meta['file'], meta['url'], meta['size'], path=path)
    return ds.load_zip(ds.filename, ds.size)
示例#34
0
    'class_layer': opt_class_layer,
    'class_layer_bias': opt_bias_class
})

# use cross-entropy cost to train the network
cost = GeneralizedCost(costfunc=CrossEntropyBinary())

lunaModel = Model(layers=vgg_layers)

# location and size of the VGG weights file
url = 'https://s3-us-west-1.amazonaws.com/nervana-modelzoo/VGG/'
filename = 'VGG_E.p'  # VGG_E.p is VGG19; VGG_D.p is VGG16
size = 554227541

# edit filepath below if you have the file elsewhere
_, filepath = Dataset._valid_path_append('data', '', filename)
if not os.path.exists(filepath):
    print('Need to fetch VGG pre-trained weights from cloud. Please wait...')
    Dataset.fetch_dataset(url, filename, filepath, size)

# load the weights param file
print("Loading VGG weights from {}...".format(filepath))
trained_vgg = load_obj(filepath)
print("Done!")

param_layers = [l for l in lunaModel.layers.layers]
param_dict_list = trained_vgg['model']['config']['layers']
for layer, params in zip(param_layers, param_dict_list):
    if (layer.name == 'class_layer'):
        break