예제 #1
0
def load_network(loadfile, multiview=None, sort_layers=False):
    from convdata import DataProvider
    from python_util.gpumodel import IGPUModel

    load_dic = IGPUModel.load_checkpoint(loadfile)
    layers = load_dic['model_state']['layers']
    op = load_dic['op']

    if sort_layers:
        depths = get_depths(layers)
        layers = collections.OrderedDict(
            sorted(layers.items(), key=lambda item: depths[item[0]]))

    options = {}
    for o in load_dic['op'].get_options_list():
        options[o.name] = o.value

    dp_params = {}
    for v in ('color_noise', 'multiview_test', 'inner_size', 'scalar_mean',
              'minibatch_size'):
        dp_params[v] = options[v]

    lib_name = "cudaconvnet._ConvNet"
    print("Importing %s C++ module" % lib_name)
    libmodel = __import__(lib_name, fromlist=['_ConvNet'])
    dp_params['libmodel'] = libmodel

    if multiview is not None:
        dp_params['multiview_test'] = multiview

    dp = DataProvider.get_instance(options['data_path'],
                                   batch_range=options['test_batch_range'],
                                   type=options['dp_type'],
                                   dp_params=dp_params,
                                   test=True)

    epoch, batchnum, data = dp.get_next_batch()
    images, labels = data[:2]
    images = images.T
    images.shape = (images.shape[0], dp.num_colors, dp.inner_size,
                    dp.inner_size)
    labels.shape = (-1, )
    labels = labels.astype('int')
    assert images.shape[0] == labels.shape[0]

    if 1:
        rng = np.random.RandomState(8)
        i = rng.permutation(images.shape[0])
        images = images[i]
        labels = labels[i]

    data = [images, labels] + list(data[2:])
    # data['data_mean'] = dp.data_mean
    # data['label_names'] = dp.batch_meta['label_names']

    return layers, data, dp
예제 #2
0
def plain_pickle(loadfile, savefile):
    load_dic = IGPUModel.load_checkpoint(loadfile)

    options = {}
    for o in load_dic['op'].get_options_list():
        options[o.name] = o.value
    load_dic['op'] = options

    with open(savefile, 'wb') as f:
        pickle.dump(load_dic, f, protocol=-1)
    print("Wrote %r" % savefile)
예제 #3
0
def loadbcp(name, shape, params=None):
    load_dic = IGPUModel.load_checkpoint(params[0])
    rows, cols = shape
    return load_dic['model_state']['layers'][params[1]]['biases'].reshape(rows,cols)
예제 #4
0
def loadwcp(name, idx, shape, params=None):
    load_dic = IGPUModel.load_checkpoint(params[0])
    rows, cols = shape
    return load_dic['model_state']['layers'][params[1]]['weights'][idx].reshape(rows,cols)
예제 #5
0
def loadbcp(name, shape, params=None):
    load_dic = IGPUModel.load_checkpoint(params[0])
    rows, cols = shape
    return load_dic['model_state']['layers'][params[1]]['biases'].reshape(
        rows, cols)
예제 #6
0
def loadwcp(name, idx, shape, params=None):
    load_dic = IGPUModel.load_checkpoint(params[0])
    rows, cols = shape
    return load_dic['model_state']['layers'][
        params[1]]['weights'][idx].reshape(rows, cols)
예제 #7
0
    if not donames:
        load_convnets = sys.argv[1:]  # just the paths
    else:
        load_convnets = sys.argv[1::2]
        legend_names = sys.argv[2::2]  # the paths and a shorter legend name

    print 'Loading convnets ' + ', '.join(load_convnets)

    for cost_idx in range(ncosts):
        pl.figure(cost_idx + basefig)
        if navg_groups > 1:
            allte = None
            alltr = None
        for cv, i in zip(load_convnets, range(len(load_convnets))):
            cp = IGPUModel.load_checkpoint(cv)
            numbatches, train_errors, test_errors = get_errors(
                cp,
                show_cost,
                cost_idx,
                norm=cost_idx == 0,
                smooth=smooth,
                interp_test=dointerp)
            #plot_errors(numbatches, train_errors, test_errors)
            tr = np.array(train_errors, dtype=np.double)
            te = np.array(test_errors, dtype=np.double)
            if navg_groups > 1:
                if allte is None:
                    allte = np.zeros(list(te.shape) + [len(load_convnets)],
                                     dtype=np.double)
                    alltr = np.zeros(list(tr.shape) + [len(load_convnets)],
예제 #8
0
    avg_groups = ['warp','nowarp']
    navg_groups = len(avg_groups)
    
    if not donames:
        load_convnets = sys.argv[1:]    # just the paths
    else:
        load_convnets = sys.argv[1::2]; legend_names = sys.argv[2::2]    # the paths and a shorter legend name
    
    print 'Loading convnets ' + ', '.join(load_convnets)
    
    for cost_idx in range(ncosts):
        pl.figure(cost_idx + basefig)
        if navg_groups > 1: allte = None; alltr = None
        for cv,i in zip(load_convnets, range(len(load_convnets))):
            cp = IGPUModel.load_checkpoint(cv)
            numbatches, train_errors, test_errors = get_errors(cp, show_cost,cost_idx, norm=cost_idx == 0,
                smooth=smooth, interp_test=dointerp)
            #plot_errors(numbatches, train_errors, test_errors)
            tr = np.array(train_errors, dtype=np.double); te = np.array(test_errors, dtype=np.double)
            if navg_groups > 1:
                if allte is None:
                    allte = np.zeros(list(te.shape)+[len(load_convnets)], dtype=np.double)
                    alltr = np.zeros(list(tr.shape)+[len(load_convnets)], dtype=np.double)
                allte[:,i] = te; alltr[:,i] = tr
            else:
                if dolog: tr = np.log10(tr); te = np.log10(te)
                pl.plot(tr, te, label=os.path.basename(legend_names[i] if donames else os.path.normpath(cv)))
        if navg_groups > 1:
            groups = np.arange(len(load_convnets),dtype=np.int64)
            groups = groups.reshape([navg_groups,len(load_convnets)//navg_groups])