示例#1
0
def test_model_get_outputs_rnn(backend_default, data):

    dataset = PTB(50, path=data)
    dataiter = dataset.train_iter

    # weight initialization
    init = Constant(0.08)

    # model initialization
    layers = [
        Recurrent(150, init, activation=Logistic()),
        Affine(len(dataiter.vocab), init, bias=init, activation=Rectlin())
    ]

    model = Model(layers=layers)
    output = model.get_outputs(dataiter)

    assert output.shape == (dataiter.ndata, dataiter.seq_length, dataiter.nclass)

    # since the init are all constant and model is un-trained:
    # along the feature dim, the values should be all the same
    assert allclose_with_out(output[0, 0], output[0, 0, 0], rtol=0, atol=1e-4)
    assert allclose_with_out(output[0, 1], output[0, 1, 0], rtol=0, atol=1e-4)

    # along the time dim, the values should be increasing:
    assert np.alltrue(output[0, 2] > output[0, 1])
    assert np.alltrue(output[0, 1] > output[0, 0])
示例#2
0
def test_model_get_outputs_rnn(backend_default, data):

    data_path = load_ptb_test(path=data)
    data_set = Text(time_steps=50, path=data_path)

    # weight initialization
    init = Constant(0.08)

    # model initialization
    layers = [
        Recurrent(150, init, activation=Logistic()),
        Affine(len(data_set.vocab), init, bias=init, activation=Rectlin())
    ]

    model = Model(layers=layers)
    output = model.get_outputs(data_set)

    assert output.shape == (data_set.ndata, data_set.seq_length,
                            data_set.nclass)

    # since the init are all constant and model is un-trained:
    # along the feature dim, the values should be all the same
    assert np.allclose(output[0, 0], output[0, 0, 0], rtol=0, atol=1e-5)
    assert np.allclose(output[0, 1], output[0, 1, 0], rtol=0, atol=1e-5)

    # along the time dim, the values should be increasing:
    assert np.alltrue(output[0, 2] > output[0, 1])
    assert np.alltrue(output[0, 1] > output[0, 0])
示例#3
0
def train_eval(
        train_set,
        valid_set,
        args,
        hidden_size = 100,
        clip_gradients = True,
        gradient_limit = 5):

    # weight initialization
    init = Uniform(low=-0.08, high=0.08)

    # model initialization
    layers = [
        LSTM(hidden_size, init, Logistic(), Tanh()),
        LSTM(hidden_size, init, Logistic(), Tanh()),
        Affine(2, init, bias=init, activation=Softmax())
    ]

    cost = GeneralizedCost(costfunc=CrossEntropyMulti(usebits=True))
    model = Model(layers=layers)
    optimizer = RMSProp(clip_gradients=clip_gradients, gradient_limit=gradient_limit, stochastic_round=args.rounding)

    # configure callbacks
    callbacks = Callbacks(model, train_set, progress_bar=args.progress_bar)

    # train model
    model.fit(train_set,
              optimizer=optimizer,
              num_epochs=args.epochs,
              cost=cost,
              callbacks=callbacks)

    pred = model.get_outputs(valid_set)
    pred_neg_rate = model.eval(valid_set, metric=Misclassification())
    return (pred[:,1], pred_neg_rate)
示例#4
0
文件: test_model.py 项目: sunclx/neon
def test_model_get_outputs(backend):
    (X_train, y_train), (X_test, y_test), nclass = load_mnist()
    train_set = DataIterator(X_train[:backend.bsz * 3])

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
              Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]
    mlp = Model(layers=layers)
    out_list = []
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output)
示例#5
0
文件: test_model.py 项目: sunclx/neon
def test_model_get_outputs_rnn(backend):

    data_path = load_text('ptb-valid')

    data_set = Text(time_steps=50, path=data_path)

    # weight initialization
    init = Constant(0.08)

    # model initialization
    layers = [
        Recurrent(150, init, Logistic()),
        Affine(len(data_set.vocab), init, bias=init, activation=Rectlin())
    ]

    model = Model(layers=layers)
    output = model.get_outputs(data_set)

    assert output.shape == (data_set.ndata, data_set.seq_length, data_set.nclass)
示例#6
0
def test_model_get_outputs(backend_default):
    (X_train, y_train), (X_test, y_test), nclass = load_mnist()
    train_set = DataIterator(X_train[:backend_default.bsz * 3])

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
              Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output)
示例#7
0
def run_once(web_input):
    """
    Run forward pass for a single input. Receives input vector from the web form.
    """

    parser = NeonArgparser(__doc__)
    
    args = parser.parse_args()
    
    num_feat = 4
    
    npzfile = np.load('./model/homeapp_preproc.npz')
    mean = npzfile['mean']
    std = npzfile['std']
    mean = np.reshape(mean, (1,mean.shape[0]))
    std = np.reshape(std, (1,std.shape[0]))
    
    # Reloading saved model
    mlp=Model("./model/homeapp_model.prm")
    
    # Horrible terrible hack that should never be needed :-(
    NervanaObject.be.bsz = 1
    
    # Actual: 275,000 Predicted: 362,177 
    #web_input = np.array([51.2246169879,-1.48577399748,223.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0])
    # Actual 185,000 Predicted: 244,526
    #web_input = np.array([51.4395375168,-1.07174234072,5.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,1.0])
    # Actual 231,500 Predicted 281,053
    web_input = np.array([52.2010084131,-2.18181259148,218.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0])
    web_input = np.reshape(web_input, (1,web_input.shape[0]))
    
    web_input[:,:num_feat-1] -= mean[:,1:num_feat]
    web_input[:,:num_feat-1] /= std[:,1:num_feat]
    
    web_test_set = ArrayIterator(X=web_input, make_onehot=False)
    
    web_output = mlp.get_outputs(web_test_set)
    
    #Rescale the output
    web_output *= std[:,0]
    web_output += mean[:,0]
    
    return web_output[0]
示例#8
0
def test_model_get_outputs_rnn(backend_default, data):

    data_path = load_text('ptb-valid', path=data)

    data_set = Text(time_steps=50, path=data_path)

    # weight initialization
    init = Constant(0.08)

    # model initialization
    layers = [
        Recurrent(150, init, Logistic()),
        Affine(len(data_set.vocab), init, bias=init, activation=Rectlin())
    ]

    model = Model(layers=layers)
    output = model.get_outputs(data_set)

    assert output.shape == (
        data_set.ndata, data_set.seq_length, data_set.nclass)
示例#9
0
class MostCommonWordSense:

    def __init__(self, rounding, callback_args, epochs):
        # setup weight initialization function
        self.init = Gaussian(loc=0.0, scale=0.01)
        # setup optimizer
        self.optimizer = GradientDescentMomentum(learning_rate=0.1, momentum_coef=0.9,
                                                 stochastic_round=rounding)
        # setup cost function as CrossEntropy
        self.cost = GeneralizedCost(costfunc=SumSquared())
        self.epochs = epochs
        self.model = None
        self.callback_args = callback_args

    def build(self):
        # setup model layers
        layers = [Affine(nout=100, init=self.init, bias=self.init, activation=Rectlin()),
                  Affine(nout=2, init=self.init, bias=self.init, activation=Softmax())]

        # initialize model object
        self.model = Model(layers=layers)

    def fit(self, valid_set, train_set):
        # configure callbacks
        callbacks = Callbacks(self.model, eval_set=valid_set, **self.callback_args)
        self.model.fit(train_set, optimizer=self.optimizer, num_epochs=self.epochs,
                       cost=self.cost, callbacks=callbacks)

    def save(self, save_path):
        self.model.save_params(save_path)

    def load(self, model_path):
        self.model = Model(model_path)

    def eval(self, valid_set):
        eval_rate = self.model.eval(valid_set, metric=Misclassification())
        return eval_rate

    def get_outputs(self, valid_set):
        return self.model.get_outputs(valid_set)
示例#10
0
def test_model_get_outputs(backend_default, data):
    (X_train, y_train), (X_test, y_test), nclass = load_mnist(path=data)
    train_set = ArrayIterator(X_train[:backend_default.bsz * 3])

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
              Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output)

    # test model benchmark inference
    mlp.benchmark(train_set, inference=True, niterations=5)
示例#11
0
def test_model_get_outputs(backend_default, data):
    dataset = MNIST(path=data)
    train_set = dataset.train_iter

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
              Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert allclose_with_out(output, ref_output[:output.shape[0], :])

    # test model benchmark inference
    mlp.benchmark(train_set, inference=True, niterations=5)
示例#12
0
def test_model_get_outputs(backend_default, data):
    dataset = MNIST(path=data)
    train_set = dataset.train_iter

    init_norm = Gaussian(loc=0.0, scale=0.1)

    layers = [
        Affine(nout=20, init=init_norm, bias=init_norm, activation=Rectlin()),
        Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))
    ]
    mlp = Model(layers=layers)
    out_list = []
    mlp.initialize(train_set)
    for x, t in train_set:
        x = mlp.fprop(x)
        out_list.append(x.get().T.copy())
    ref_output = np.vstack(out_list)

    train_set.reset()
    output = mlp.get_outputs(train_set)
    assert np.allclose(output, ref_output[:output.shape[0], :])

    # test model benchmark inference
    mlp.benchmark(train_set, inference=True, niterations=5)
示例#13
0
    model = Model(layers=layers)
    cost = GeneralizedCost(MeanSquared())
    optimizer = RMSProp(stochastic_round=args.rounding)

    callbacks = Callbacks(model, eval_set=valid_set, **args.callback_args)

    # fit model
    model.fit(train_set,
              optimizer=optimizer,
              num_epochs=args.epochs,
              cost=cost,
              callbacks=callbacks)

    # =======visualize how the model does on validation set==============
    # run the trained model on train and valid dataset and see how the outputs match
    train_output = model.get_outputs(train_set).reshape(-1, train_set.nfeatures)
    valid_output = model.get_outputs(valid_set).reshape(-1, valid_set.nfeatures)
    train_target = train_set.y_series
    valid_target = valid_set.y_series

    # calculate accuracy
    terr = err(train_output, train_target)
    verr = err(valid_output, valid_target)

    print 'terr = %g, verr = %g' % (terr, verr)

    if do_plots:
        plt.figure()
        plt.plot(train_output[:, 0], train_output[:, 1], 'bo', label='prediction')
        plt.plot(train_target[:, 0], train_target[:, 1], 'r.', label='target')
        plt.legend()
示例#14
0
init = Uniform(low=-0.08, high=0.08)

# model initialization
if args.rlayer_type == 'lstm':
    rlayer = LSTM(hidden_size, init, activation=Logistic(), gate_activation=Tanh())
else:
    rlayer = GRU(hidden_size, init, activation=Tanh(), gate_activation=Logistic())

layers = [rlayer,
          Affine(len(train_set.vocab), init, bias=init, activation=Softmax())]

cost = GeneralizedCost(costfunc=CrossEntropyMulti(usebits=True))

model = Model(layers=layers)

optimizer = RMSProp(gradient_clip_value=gradient_clip_value, stochastic_round=args.rounding)

# configure callbacks
callbacks = Callbacks(model, train_set, eval_set=valid_set, **args.callback_args)

# train model
model.fit(train_set, optimizer=optimizer, num_epochs=args.epochs, cost=cost, callbacks=callbacks)

# get predictions
ypred = model.get_outputs(valid_set)
prediction = ypred.argmax(2).reshape((valid_set.nbatches,
                                      args.batch_size,
                                      time_steps)).transpose(1, 0, 2)
fraction_correct = (prediction == valid_set.y).mean()
print 'Misclassification error = %.1f%%' % ((1-fraction_correct)*100)
be = gen_backend(**extract_valid_args(args, gen_backend))

# We don't need to one hot encode this since it is only 2 classes 
# and the sklearn stuff is just looking at the probability of class 1
test_set = HDF5Iterator(testFileName)
#test_set = HDF5IteratorOneHot(testFileName)


model_filename= 'LUNA16_resnetHDF_subset{}.prm'.format(subset)
#model_filename= 'LUNA16_resnetHDF_subset0.prm'

print('Using model: {}'.format(model_filename))

lunaModel = Model(model_filename)

prob, target = lunaModel.get_outputs(test_set, return_targets=True) 


np.set_printoptions(precision=3, suppress=True)


from sklearn.metrics import classification_report, roc_auc_score, average_precision_score
from sklearn.metrics import precision_recall_curve, log_loss, confusion_matrix

# precision, recall, thresholds = precision_recall_curve(target, prob)

print('Average precision = {}'.format(average_precision_score(target, prob[:,1], average='weighted')))


print(classification_report(target, np.argmax(prob, axis=1), target_names=['Class 0', 'Class 1']))
示例#16
0
        else:
            # make dummy random data just for testing model inits
            test = RandomEMDataIterator(name='outputs')

        print(
            'Model output (forward prop) for %d testing batches, %d examples/batch'
            % (test.nmacrobatches, test.parser.num_cases_per_batch))
        feature_path = ''
        #out_thread = None
        for i in range(test.nmacrobatches):
            batchnum = test.batch_range[0] + i
            last_batch = i == test.nmacrobatches - 1
            sys.stdout.write('%d ... ' % (batchnum, ))
            t = time.time()

            outputs = model.get_outputs(test)

            # serial version
            test.parser.checkOutputCubes(feature_path,
                                         batchnum,
                                         last_batch,
                                         outputs=outputs)

            # xxx - could not get any improvment here, possibly overhead of the thread is longer than the
            #   computations for formatting the outputs (the h5 write itself is parallelized in the parser)
            ## parallel version
            #if out_thread: out_thread.join()
            #out_thread = FuncThread(test.parser.checkOutputCubes, feature_path, batchnum, last_batch, outputs)
            #out_thread.start()
            #if last_batch: out_thread.join()
class NpSemanticSegClassifier:
    """
    NP Semantic Segmentation classifier model (based on Neon framework).

    Args:
        num_epochs(int): number of epochs to train the model
        **callback_args (dict): callback args keyword arguments to init a Callback for the model
        cost: the model's cost function. Default is 'neon.transforms.CrossEntropyBinary' cost
        optimizer (:obj:`neon.optimizers`): the model's optimizer. Default is
        'neon.optimizers.GradientDescentMomentum(0.07, momentum_coef=0.9)'
    """

    def __init__(self, num_epochs, callback_args,
                 optimizer=GradientDescentMomentum(0.07, momentum_coef=0.9)):
        """

        Args:
            num_epochs(int): number of epochs to train the model
            **callback_args (dict): callback args keyword arguments to init Callback for the model
            cost: the model's cost function. Default is 'neon.transforms.CrossEntropyBinary' cost
            optimizer (:obj:`neon.optimizers`): the model's optimizer. Default is
            `neon.optimizers.GradientDescentMomentum(0.07, momentum_coef=0.9)`
        """
        self.model = None
        self.cost = GeneralizedCost(costfunc=CrossEntropyBinary())
        self.optimizer = optimizer
        self.epochs = num_epochs
        self.callback_args = callback_args

    def build(self):
        """
        Build the model's layers
        """
        first_layer_dens = 64
        second_layer_dens = 64
        output_layer_dens = 2
        # setup weight initialization function
        init_norm = Gaussian(scale=0.01)
        # setup model layers
        layers = [Affine(nout=first_layer_dens, init=init_norm,
                         activation=Rectlin()),
                  Affine(nout=second_layer_dens, init=init_norm,
                         activation=Rectlin()),
                  Affine(nout=output_layer_dens, init=init_norm,
                         activation=Logistic(shortcut=True))]

        # initialize model object
        self.model = Model(layers=layers)

    def fit(self, test_set, train_set):
        """
        Train and fit the model on the datasets

        Args:
            test_set (:obj:`neon.data.ArrayIterators`): The test set
            train_set (:obj:`neon.data.ArrayIterators`): The train set
            args: callback_args and epochs from ArgParser input
        """
        # configure callbacks
        callbacks = Callbacks(self.model, eval_set=test_set, **self.callback_args)
        self.model.fit(train_set, optimizer=self.optimizer, num_epochs=self.epochs, cost=self.cost,
                       callbacks=callbacks)

    def save(self, model_path):
        """
        Save the model's prm file in model_path location

        Args:
            model_path(str): local path for saving the model
        """
        self.model.save_params(model_path)

    def load(self, model_path):
        """
        Load pre-trained model's .prm file to NpSemanticSegClassifier object

        Args:
            model_path(str): local path for loading the model
        """
        self.model = Model(model_path)

    def eval(self, test_set):
        """
        Evaluate the model's test_set on error_rate, test_accuracy_rate and precision_recall_rate

        Args:
            test_set (ArrayIterator): The test set

        Returns:
            tuple(int): error_rate, test_accuracy_rate and precision_recall_rate
        """
        error_rate = self.model.eval(test_set, metric=Misclassification())
        test_accuracy_rate = self.model.eval(test_set, metric=Accuracy())
        precision_recall_rate = self.model.eval(test_set, metric=PrecisionRecall(2))
        return error_rate, test_accuracy_rate, precision_recall_rate

    def get_outputs(self, test_set):
        """
        Classify the dataset on the model

        Args:
            test_set (:obj:`neon.data.ArrayIterators`): The test set

        Returns:
            list(float): model's predictions
        """
        return self.model.get_outputs(test_set)
示例#18
0
from neon.util.argparser import NeonArgparser
from neon.layers import Pooling
from neon.models import Model
from neon.data import ImageLoader
from neon.util.persist import save_obj, load_obj

# parse the command line arguments (generates the backend)
parser = NeonArgparser(__doc__)
args = parser.parse_args()

scales = [112, 128, 160, 240]
for scale in scales:
    print scale
    test = ImageLoader(set_name='validation', shuffle=False, do_transforms=False, inner_size=scale,
                       scale_range=scale, repo_dir=args.data_dir)

    model_desc = load_obj(args.model_file)
    model_desc['model']['config']['layers'].insert(-1, Pooling('all', op='avg').get_description())
    model = Model(model_desc, test, inference=True)
    softmaxes = model.get_outputs(test)
    save_obj(softmaxes, "bigfeat_dropout_SM_{}.pkl".format(scale))
示例#19
0
def main():
    parser = NeonArgparser(__doc__)
    args = parser.parse_args(gen_be=False)

    #mat_data = sio.loadmat('../data/timeseries/02_timeseries.mat')

    #ts = V1TimeSeries(mat_data['timeseries'], mat_data['stim'], binning=10)

    seq_len = 30
    hidden = 20

    be = gen_backend(**extract_valid_args(args, gen_backend))

    kohn = KohnV1Dataset(path='../tmp/')
    kohn.gen_iterators(seq_len)
    import pdb; pdb.set_trace()
    train_spike_set = V1IteratorSequence(ts.train, seq_len, return_sequences=False)
    valid_spike_set = V1IteratorSequence(ts.test, seq_len, return_sequences=False)

    init = GlorotUniform()

    # dataset = MNIST(path=args.data_dir)
    # (X_train, y_train), (X_test, y_test), nclass = dataset.load_data()
    # train_set = ArrayIterator([X_train, X_train], y_train, nclass=nclass, lshape=(1, 28, 28))
    # valid_set = ArrayIterator([X_test, X_test], y_test, nclass=nclass, lshape=(1, 28, 28))

    # # weight initialization
    # init_norm = Gaussian(loc=0.0, scale=0.01)

    # # initialize model
    # path1 = Sequential(layers=[Affine(nout=100, init=init_norm, activation=Rectlin()),
    #                            Affine(nout=100, init=init_norm, activation=Rectlin())])

    # path2 = Sequential(layers=[Affine(nout=100, init=init_norm, activation=Rectlin()),
    #                            Affine(nout=100, init=init_norm, activation=Rectlin())])

    # layers = [MergeMultistream(layers=[path1, path2], merge="stack"),
    #           Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]

    spike_rnn_path = Sequential( layers = [

        LSTM(hidden, init, activation=Logistic(),
            gate_activation=Logistic(), reset_cells=False),

        Dropout(keep=0.5),

         LSTM(hidden, init, activation=Logistic(),
             gate_activation=Logistic(), reset_cells=False),

        #Dropout(keep=0.85),

        RecurrentLast(),

        Affine(train_set.nfeatures, init, bias=init, activation=Identity(), name='spike_in')])

    stim_rnn_path = Sequential( layers = [

        LSTM(hidden, init, activation=Logistic(),
            gate_activation=Logistic(), reset_cells=False),

        Dropout(keep=0.5),

        RecurrentLast(),
        Affine(1, init, bias=init, activation=Identity(), name='stim')])

    layers = [
            MergeMultiStream(
                layers = [
                    spike_rnn_path,
                    stim_rnn_path],
                merge="stack"),

            Affine(train_set.nfeatures, init, bias=init, activation=Identity(), name='spike_out'),

            Round()
            ]

    model = Model(layers=layers)

    sched = ExpSchedule(decay=0.7)

    # cost = GeneralizedCost(SumSquared())
    cost = GeneralizedCost(MeanSquared())

    optimizer_two = RMSProp(stochastic_round=args.rounding)
    optimizer_one = GradientDescentMomentum(learning_rate=0.1, momentum_coef=0.9, schedule=sched)

    opt = MultiOptimizer({'default': optimizer_one,
                          'Bias': optimizer_two,
                          'special_linear': optimizer_two})

    callbacks = Callbacks(model, eval_set=valid_set, **args.callback_args)
    callbacks.add_hist_callback(filter_key = ['W'])
    #callbacks.add_callback(MetricCallback(eval_set=valid_set, metric=FractionExplainedVariance(), epoch_freq=args.eval_freq))
    #callbacks.add_callback(MetricCallback(eval_set=valid_set,metric=Accuracy(),  epoch_freq=args.eval_freq))

    model.fit(train_set,
              optimizer=opt,
              num_epochs=args.epochs,
              cost=cost,
              callbacks=callbacks)

    train_output = model.get_outputs(
    train_set).reshape(-1, train_set.nfeatures)
    valid_output = model.get_outputs(
    valid_set).reshape(-1, valid_set.nfeatures)
    train_target = train_set.y_series
    valid_target = valid_set.y_series

    tfev = fev(train_output, train_target, train_set.mean)
    vfev = fev(valid_output, valid_target, valid_set.mean)

    neon_logger.display('Train FEV: %g, Valid FEV:  %g' % (tfev, vfev))
    # neon_logger.display('Train Mean: %g, Valid Mean:  %g' % (train_set.mean, valid_set.mean))

    plt.figure()
    plt.plot(train_output[:, 0], train_output[
        :, 1], 'bo', label='prediction')
    plt.plot(train_target[:, 0], train_target[:, 1], 'r.', label='target')
    plt.legend()
    plt.title('Neon on training set')
    plt.savefig('neon_series_training_output.png')

    plt.figure()
    plt.plot(valid_output[:, 0], valid_output[
        :, 1], 'bo', label='prediction')
    plt.plot(valid_target[:, 0], valid_target[:, 1], 'r.', label='target')
    plt.legend()
    plt.title('Neon on validation set')
    plt.savefig('neon_series_validation_output.png')
class NpSemanticSegClassifier:
    """
    NP Semantic Segmentation classifier model (based on Neon framework).

    Args:
        num_epochs(int): number of epochs to train the model
        **callback_args (dict): callback args keyword arguments to init a Callback for the model
        cost: the model's cost function. Default is 'neon.transforms.CrossEntropyBinary' cost
        optimizer (:obj:`neon.optimizers`): the model's optimizer. Default is
        'neon.optimizers.GradientDescentMomentum(0.07, momentum_coef=0.9)'
    """
    def __init__(self,
                 num_epochs,
                 callback_args,
                 optimizer=GradientDescentMomentum(0.07, momentum_coef=0.9)):
        """

        Args:
            num_epochs(int): number of epochs to train the model
            **callback_args (dict): callback args keyword arguments to init Callback for the model
            cost: the model's cost function. Default is 'neon.transforms.CrossEntropyBinary' cost
            optimizer (:obj:`neon.optimizers`): the model's optimizer. Default is
            `neon.optimizers.GradientDescentMomentum(0.07, momentum_coef=0.9)`
        """
        self.model = None
        self.cost = GeneralizedCost(costfunc=CrossEntropyBinary())
        self.optimizer = optimizer
        self.epochs = num_epochs
        self.callback_args = callback_args

    def build(self):
        """
        Build the model's layers
        """
        first_layer_dens = 64
        second_layer_dens = 64
        output_layer_dens = 2
        # setup weight initialization function
        init_norm = Gaussian(scale=0.01)
        # setup model layers
        layers = [
            Affine(nout=first_layer_dens, init=init_norm,
                   activation=Rectlin()),
            Affine(nout=second_layer_dens,
                   init=init_norm,
                   activation=Rectlin()),
            Affine(nout=output_layer_dens,
                   init=init_norm,
                   activation=Logistic(shortcut=True))
        ]

        # initialize model object
        self.model = Model(layers=layers)

    def fit(self, test_set, train_set):
        """
        Train and fit the model on the datasets

        Args:
            test_set (:obj:`neon.data.ArrayIterators`): The test set
            train_set (:obj:`neon.data.ArrayIterators`): The train set
            args: callback_args and epochs from ArgParser input
        """
        # configure callbacks
        callbacks = Callbacks(self.model,
                              eval_set=test_set,
                              **self.callback_args)
        self.model.fit(train_set,
                       optimizer=self.optimizer,
                       num_epochs=self.epochs,
                       cost=self.cost,
                       callbacks=callbacks)

    def save(self, model_path):
        """
        Save the model's prm file in model_path location

        Args:
            model_path(str): local path for saving the model
        """
        self.model.save_params(model_path)

    def load(self, model_path):
        """
        Load pre-trained model's .prm file to NpSemanticSegClassifier object

        Args:
            model_path(str): local path for loading the model
        """
        self.model = Model(model_path)

    def eval(self, test_set):
        """
        Evaluate the model's test_set on error_rate, test_accuracy_rate and precision_recall_rate

        Args:
            test_set (ArrayIterator): The test set

        Returns:
            tuple(int): error_rate, test_accuracy_rate and precision_recall_rate
        """
        error_rate = self.model.eval(test_set, metric=Misclassification())
        test_accuracy_rate = self.model.eval(test_set, metric=Accuracy())
        precision_recall_rate = self.model.eval(test_set,
                                                metric=PrecisionRecall(2))
        return error_rate, test_accuracy_rate, precision_recall_rate

    def get_outputs(self, test_set):
        """
        Classify the dataset on the model

        Args:
            test_set (:obj:`neon.data.ArrayIterators`): The test set

        Returns:
            list(float): model's predictions
        """
        return self.model.get_outputs(test_set)
示例#21
0
def main():
    # Get command-line parameters
    parser = get_p1b1_parser()
    args = parser.parse_args()
    #print('Args:', args)
    # Get parameters from configuration file
    fileParameters = p1b1.read_config_file(args.config_file)
    #print ('Params:', fileParameters)

    # Correct for arguments set by default by neon parser
    # (i.e. instead of taking the neon parser default value fall back to the config file,
    # if effectively the command-line was used, then use the command-line value)
    # This applies to conflictive parameters: batch_size, epochs and rng_seed
    if not any("--batch_size" in ag or "-z" in ag for ag in sys.argv):
        args.batch_size = fileParameters['batch_size']
    if not any("--epochs" in ag or "-e" in ag for ag in sys.argv):
        args.epochs = fileParameters['epochs']
    if not any("--rng_seed" in ag or "-r" in ag for ag in sys.argv):
        args.rng_seed = fileParameters['rng_seed']

    # Consolidate parameter set. Command-line parameters overwrite file configuration
    gParameters = p1_common.args_overwrite_config(args, fileParameters)
    print('Params:', gParameters)

    # Determine verbosity level
    loggingLevel = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=loggingLevel, format='')
    # Construct extension to save model
    ext = p1b1.extension_from_parameters(gParameters, '.neon')

    # Get default parameters for initialization and optimizer functions
    kerasDefaults = p1_common.keras_default_config()
    seed = gParameters['rng_seed']

    # Load dataset
    X_train, X_val, X_test = p1b1.load_data(gParameters, seed)

    print("Shape X_train: ", X_train.shape)
    print("Shape X_val: ", X_val.shape)
    print("Shape X_test: ", X_test.shape)

    print("Range X_train --> Min: ", np.min(X_train), ", max: ",
          np.max(X_train))
    print("Range X_val --> Min: ", np.min(X_val), ", max: ", np.max(X_val))
    print("Range X_test --> Min: ", np.min(X_test), ", max: ", np.max(X_test))

    input_dim = X_train.shape[1]
    output_dim = input_dim

    # Re-generate the backend after consolidating parsing and file config
    gen_backend(backend=args.backend,
                rng_seed=seed,
                device_id=args.device_id,
                batch_size=gParameters['batch_size'],
                datatype=gParameters['datatype'],
                max_devices=args.max_devices,
                compat_mode=args.compat_mode)

    # Set input and target to X_train
    train = ArrayIterator(X_train)
    val = ArrayIterator(X_val)
    test = ArrayIterator(X_test)

    # Initialize weights and learning rule
    initializer_weights = p1_common_neon.build_initializer(
        gParameters['initialization'], kerasDefaults)
    initializer_bias = p1_common_neon.build_initializer(
        'constant', kerasDefaults, 0.)

    activation = p1_common_neon.get_function(gParameters['activation'])()

    # Define Autoencoder architecture
    layers = []
    reshape = None

    # Autoencoder
    layers_params = gParameters['dense']

    if layers_params != None:
        if type(layers_params) != list:
            layers_params = list(layers_params)
        # Encoder Part
        for i, l in enumerate(layers_params):
            layers.append(
                Affine(nout=l,
                       init=initializer_weights,
                       bias=initializer_bias,
                       activation=activation))
        # Decoder Part
        for i, l in reversed(list(enumerate(layers_params))):
            if i < len(layers) - 1:
                layers.append(
                    Affine(nout=l,
                           init=initializer_weights,
                           bias=initializer_bias,
                           activation=activation))

    layers.append(
        Affine(nout=output_dim,
               init=initializer_weights,
               bias=initializer_bias,
               activation=activation))

    # Build Autoencoder model
    ae = Model(layers=layers)

    # Define cost and optimizer
    cost = GeneralizedCost(p1_common_neon.get_function(gParameters['loss'])())
    optimizer = p1_common_neon.build_optimizer(gParameters['optimizer'],
                                               gParameters['learning_rate'],
                                               kerasDefaults)

    callbacks = Callbacks(ae, eval_set=val, eval_freq=1)

    # Seed random generator for training
    np.random.seed(seed)

    ae.fit(train,
           optimizer=optimizer,
           num_epochs=gParameters['epochs'],
           cost=cost,
           callbacks=callbacks)

    # model save
    #save_fname = "model_ae_W" + ext
    #ae.save_params(save_fname)

    # Compute errors
    X_pred = ae.get_outputs(test)
    scores = p1b1.evaluate_autoencoder(X_pred, X_test)
    print('Evaluation on test data:', scores)

    diff = X_pred - X_test
    # Plot histogram of errors comparing input and output of autoencoder
    plt.hist(diff.ravel(), bins='auto')
    plt.title("Histogram of Errors with 'auto' bins")
    plt.savefig('histogram_neon.png')
示例#22
0
# To view images, install Imagemagic
def show_sample(x):
    image = x.reshape(3, 32, 32)
    image = np.transpose(image, (1, 2, 0))
    image = Image.fromarray(np.uint8(image * 255))
    image.show()

image = Image.open('frog.jpg')
image = image.crop((0,0,min(image.size),min(image.size)))
image.thumbnail((32, 32))
# image.show()
image = np.asarray(image, dtype=np.float32)     # (width, height, channel)
image = np.transpose(image, (2, 0, 1))  # ArrayIterator expects (channel, height, width)
x_new = image.reshape(1, 3072) / 255
show_sample(x_new)
inference_set = ArrayIterator(x_new, None, nclass=nclass, lshape=(3, 32, 32))
out = model.get_outputs(inference_set)
print classes[out.argmax()] + (", ground truth FROG")


# Sanity check 2
from neon.data import load_cifar10
(X_train, y_train), (X_test, y_test), nclass = load_cifar10()
i = 1
x_new = X_test[i]
x_new = x_new.reshape(1, 3072)
show_sample(x_new)
inference_set = ArrayIterator(x_new, None, nclass=nclass, lshape=(3, 32, 32))
out = model.get_outputs(inference_set)
print classes[out.argmax()] + ", ground truth " + classes[y_test[i][0]]
示例#23
0
def main():
    # Get command-line parameters
    parser = get_p1b2_parser()
    args = parser.parse_args()
    #print('Args:', args)
    # Get parameters from configuration file
    fileParameters = p1b2.read_config_file(args.config_file)
    #print ('Params:', fileParameters)

    # Correct for arguments set by default by neon parser
    # (i.e. instead of taking the neon parser default value fall back to the config file,
    # if effectively the command-line was used, then use the command-line value)
    # This applies to conflictive parameters: batch_size, epochs and rng_seed
    if not any("--batch_size" in ag or "-z" in ag for ag in sys.argv):
        args.batch_size = fileParameters['batch_size']
    if not any("--epochs" in ag or "-e" in ag for ag in sys.argv):
        args.epochs = fileParameters['epochs']
    if not any("--rng_seed" in ag or "-r" in ag for ag in sys.argv):
        args.rng_seed = fileParameters['rng_seed']

    # Consolidate parameter set. Command-line parameters overwrite file configuration
    gParameters = p1_common.args_overwrite_config(args, fileParameters)
    print('Params:', gParameters)

    # Determine verbosity level
    loggingLevel = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=loggingLevel, format='')
    # Construct extension to save model
    ext = p1b2.extension_from_parameters(gParameters, '.neon')

    # Get default parameters for initialization and optimizer functions
    kerasDefaults = p1_common.keras_default_config()
    seed = gParameters['rng_seed']

    # Load dataset
    #(X_train, y_train), (X_test, y_test) = p1b2.load_data(gParameters, seed)
    (X_train, y_train), (X_val,
                         y_val), (X_test,
                                  y_test) = p1b2.load_data(gParameters, seed)

    print("Shape X_train: ", X_train.shape)
    print("Shape X_val: ", X_val.shape)
    print("Shape X_test: ", X_test.shape)
    print("Shape y_train: ", y_train.shape)
    print("Shape y_val: ", y_val.shape)
    print("Shape y_test: ", y_test.shape)

    print("Range X_train --> Min: ", np.min(X_train), ", max: ",
          np.max(X_train))
    print("Range X_val --> Min: ", np.min(X_val), ", max: ", np.max(X_val))
    print("Range X_test --> Min: ", np.min(X_test), ", max: ", np.max(X_test))
    print("Range y_train --> Min: ", np.min(y_train), ", max: ",
          np.max(y_train))
    print("Range y_val --> Min: ", np.min(y_val), ", max: ", np.max(y_val))
    print("Range y_test --> Min: ", np.min(y_test), ", max: ", np.max(y_test))

    input_dim = X_train.shape[1]
    num_classes = int(np.max(y_train)) + 1
    output_dim = num_classes  # The backend will represent the classes using one-hot representation (but requires an integer class as input !)

    # Re-generate the backend after consolidating parsing and file config
    gen_backend(backend=args.backend,
                rng_seed=seed,
                device_id=args.device_id,
                batch_size=gParameters['batch_size'],
                datatype=gParameters['data_type'],
                max_devices=args.max_devices,
                compat_mode=args.compat_mode)

    train = ArrayIterator(X=X_train, y=y_train, nclass=num_classes)
    val = ArrayIterator(X=X_val, y=y_val, nclass=num_classes)
    test = ArrayIterator(X=X_test, y=y_test, nclass=num_classes)

    # Initialize weights and learning rule
    initializer_weights = p1_common_neon.build_initializer(
        gParameters['initialization'], kerasDefaults, seed)
    initializer_bias = p1_common_neon.build_initializer(
        'constant', kerasDefaults, 0.)

    activation = p1_common_neon.get_function(gParameters['activation'])()

    # Define MLP architecture
    layers = []
    reshape = None

    for layer in gParameters['dense']:
        if layer:
            layers.append(
                Affine(nout=layer,
                       init=initializer_weights,
                       bias=initializer_bias,
                       activation=activation))
        if gParameters['dropout']:
            layers.append(Dropout(keep=(1 - gParameters['dropout'])))

    layers.append(
        Affine(nout=output_dim,
               init=initializer_weights,
               bias=initializer_bias,
               activation=activation))

    # Build MLP model
    mlp = Model(layers=layers)

    # Define cost and optimizer
    cost = GeneralizedCost(p1_common_neon.get_function(gParameters['loss'])())
    optimizer = p1_common_neon.build_optimizer(gParameters['optimizer'],
                                               gParameters['learning_rate'],
                                               kerasDefaults)

    callbacks = Callbacks(mlp, eval_set=val, metric=Accuracy(), eval_freq=1)

    # Seed random generator for training
    np.random.seed(seed)

    mlp.fit(train,
            optimizer=optimizer,
            num_epochs=gParameters['epochs'],
            cost=cost,
            callbacks=callbacks)

    # model save
    #save_fname = "model_mlp_W_" + ext
    #mlp.save_params(save_fname)

    # Evalute model on test set
    print('Model evaluation by neon: ', mlp.eval(test, metric=Accuracy()))
    y_pred = mlp.get_outputs(test)
    #print ("Shape y_pred: ", y_pred.shape)
    scores = p1b2.evaluate_accuracy(p1_common.convert_to_class(y_pred), y_test)
    print('Evaluation on test data:', scores)
示例#24
0
class SequenceChunker(object):
    """
    Sequence chunker model (Neon based)

    Args:
        sentence_length (str): max sentence length
        token_vocab_size (int): word vocabulary size
        pos_vocab_size (int, optional): POS vocabulary size
        char_vocab_size (int, optional): characters vocabulary size
        max_char_word_length (int, optional): max word length in characters
        token_embedding_size (int, optional): word embedding dims
        pos_embedding_size (int, optional): POS embedding dims
        char_embedding_size (int, optional): character embedding dims
        num_labels (int, optional): number of output labels possible per token
        lstm_hidden_size (int, optional): LSTM hidden size
        num_lstm_layers (int, optional): number of LSTM layers
        use_external_embedding (bool, optional): input is provided as external word embedding
        dropout (float, optional): dropout rate
    """
    def __init__(self,
                 sentence_length,
                 token_vocab_size,
                 pos_vocab_size=None,
                 char_vocab_size=None,
                 max_char_word_length=20,
                 token_embedding_size=None,
                 pos_embedding_size=None,
                 char_embedding_size=None,
                 num_labels=None,
                 lstm_hidden_size=100,
                 num_lstm_layers=1,
                 use_external_embedding=None,
                 dropout=0.5):

        init = GlorotUniform()
        tokens = []
        if use_external_embedding is None:
            tokens.append(
                LookupTable(vocab_size=token_vocab_size,
                            embedding_dim=token_embedding_size,
                            init=init,
                            pad_idx=0))
        else:
            tokens.append(DataInput())
        tokens.append(Reshape((-1, sentence_length)))
        f_layers = [tokens]

        # add POS tag input
        if pos_vocab_size is not None and pos_embedding_size is not None:
            f_layers.append([
                LookupTable(vocab_size=pos_vocab_size,
                            embedding_dim=pos_embedding_size,
                            init=init,
                            pad_idx=0),
                Reshape((-1, sentence_length))
            ])

        # add Character RNN input
        if char_vocab_size is not None and char_embedding_size is not None:
            char_lut_layer = LookupTable(vocab_size=char_vocab_size,
                                         embedding_dim=char_embedding_size,
                                         init=init,
                                         pad_idx=0)
            char_nn = [
                char_lut_layer,
                TimeDistBiLSTM(char_embedding_size,
                               init,
                               activation=Logistic(),
                               gate_activation=Tanh(),
                               reset_cells=True,
                               reset_freq=max_char_word_length),
                TimeDistributedRecurrentLast(timesteps=max_char_word_length),
                Reshape((-1, sentence_length))
            ]

            f_layers.append(char_nn)

        layers = []
        if len(f_layers) == 1:
            layers.append(f_layers[0][0])
        else:
            layers.append(MergeMultistream(layers=f_layers, merge="stack"))
            layers.append(Reshape((-1, sentence_length)))
        layers += [
            DeepBiLSTM(lstm_hidden_size,
                       init,
                       activation=Logistic(),
                       gate_activation=Tanh(),
                       reset_cells=True,
                       depth=num_lstm_layers),
            Dropout(keep=dropout),
            Affine(num_labels, init, bias=init, activation=Softmax())
        ]
        self._model = Model(layers=layers)

    def fit(self, dataset, optimizer, cost, callbacks, epochs=10):
        """
        fit a model

        Args:
            dataset: train/test set of CONLL2000 dataset
            optimizer: optimizer (Neon based)
            cost: cost function (Neon based)
            callbacks: callbacks (Neon based)
            epochs (int, optional): number of epochs to train
        """
        self._model.fit(dataset,
                        optimizer=optimizer,
                        num_epochs=epochs,
                        cost=cost,
                        callbacks=callbacks)

    def predict(self, dataset):
        """
        predict output of given dataset

        Args:
            dataset: Neon based iterator

        Returns:
            prediction on given dataset
        """
        return self._model.get_outputs(dataset)

    def save(self, path):
        """
        Save model weights to path

        Args:
            path (str): path to weights file
        """
        self._model.save_params(path)

    def get_model(self):
        """
        Get model

        Returns:
            Neon model object
        """
        return self._model
示例#25
0
class SequenceChunker(object):
    """
    Sequence chunker model (Neon based)

    Args:
        sentence_length (str): max sentence length
        token_vocab_size (int): word vocabulary size
        pos_vocab_size (int, optional): POS vocabulary size
        char_vocab_size (int, optional): characters vocabulary size
        max_char_word_length (int, optional): max word length in characters
        token_embedding_size (int, optional): word embedding dims
        pos_embedding_size (int, optional): POS embedding dims
        char_embedding_size (int, optional): character embedding dims
        num_labels (int, optional): number of output labels possible per token
        lstm_hidden_size (int, optional): LSTM hidden size
        num_lstm_layers (int, optional): number of LSTM layers
        use_external_embedding (bool, optional): input is provided as external word embedding
        dropout (float, optional): dropout rate
    """

    def __init__(self, sentence_length,
                 token_vocab_size,
                 pos_vocab_size=None,
                 char_vocab_size=None,
                 max_char_word_length=20,
                 token_embedding_size=None,
                 pos_embedding_size=None,
                 char_embedding_size=None,
                 num_labels=None,
                 lstm_hidden_size=100,
                 num_lstm_layers=1,
                 use_external_embedding=None,
                 dropout=0.5
                 ):

        init = GlorotUniform()
        tokens = []
        if use_external_embedding is None:
            tokens.append(LookupTable(vocab_size=token_vocab_size,
                                      embedding_dim=token_embedding_size,
                                      init=init,
                                      pad_idx=0))
        else:
            tokens.append(DataInput())
        tokens.append(Reshape((-1, sentence_length)))
        f_layers = [tokens]

        # add POS tag input
        if pos_vocab_size is not None and pos_embedding_size is not None:
            f_layers.append([
                LookupTable(vocab_size=pos_vocab_size,
                            embedding_dim=pos_embedding_size,
                            init=init,
                            pad_idx=0),
                Reshape((-1, sentence_length))
            ])

        # add Character RNN input
        if char_vocab_size is not None and char_embedding_size is not None:
            char_lut_layer = LookupTable(vocab_size=char_vocab_size,
                                         embedding_dim=char_embedding_size,
                                         init=init,
                                         pad_idx=0)
            char_nn = [char_lut_layer,
                       TimeDistBiLSTM(char_embedding_size, init, activation=Logistic(),
                                      gate_activation=Tanh(),
                                      reset_cells=True, reset_freq=max_char_word_length),
                       TimeDistributedRecurrentLast(timesteps=max_char_word_length),
                       Reshape((-1, sentence_length))]

            f_layers.append(char_nn)

        layers = []
        if len(f_layers) == 1:
            layers.append(f_layers[0][0])
        else:
            layers.append(MergeMultistream(layers=f_layers, merge="stack"))
            layers.append(Reshape((-1, sentence_length)))
        layers += [DeepBiLSTM(lstm_hidden_size, init, activation=Logistic(),
                              gate_activation=Tanh(),
                              reset_cells=True,
                              depth=num_lstm_layers),
                   Dropout(keep=dropout),
                   Affine(num_labels, init, bias=init, activation=Softmax())]
        self._model = Model(layers=layers)

    def fit(self, dataset, optimizer, cost, callbacks, epochs=10):
        """
        fit a model

        Args:
            dataset: train/test set of CONLL2000 dataset
            optimizer: optimizer (Neon based)
            cost: cost function (Neon based)
            callbacks: callbacks (Neon based)
            epochs (int, optional): number of epochs to train
        """
        self._model.fit(dataset,
                        optimizer=optimizer,
                        num_epochs=epochs,
                        cost=cost,
                        callbacks=callbacks)

    def predict(self, dataset):
        """
        predict output of given dataset

        Args:
            dataset: Neon based iterator

        Returns:
            prediction on given dataset
        """
        return self._model.get_outputs(dataset)

    def save(self, path):
        """
        Save model weights to path

        Args:
            path (str): path to weights file
        """
        self._model.save_params(path)

    def get_model(self):
        """
        Get model

        Returns:
            Neon model object
        """
        return self._model
示例#26
0
scales = [112, 128, 160, 240]

for scale in scales:
    print scale

    layers = []
    layers += [Conv(**conv_params(7, 32, 2))]
    for nfm, stride in zip(nfms, strides):
        layers.append(module_factory(nfm, stride))
    layers.append(Pooling(7, op='avg'))

    layers.append(
        Conv(fshape=(1, 1, 100), init=Kaiming(local=True), batch_norm=True))
    layers.append(Pooling(fshape='all', op='avg'))
    layers.append(Activation(Softmax()))

    model = Model(layers=layers)
    test = ImageLoader(set_name='validation',
                       shuffle=False,
                       do_transforms=False,
                       inner_size=scale,
                       scale_range=scale,
                       repo_dir=args.data_dir)

    model.load_params("/home/users/hunter/bigfeat_dropout.pkl")

    softmaxes = model.get_outputs(test)
    from neon.util.persist import save_obj
    save_obj(softmaxes, "bigfeat_dropout_SM_{}.pkl".format(scale))
示例#27
0
class WordseqRegressor():
    def __init__(self, pickle_model="", datadir=None):
        self.maxlen = 100
        self.n_words = 100000
        parser = NeonArgparser(__doc__)
        self.args = parser.parse_args()
        self.args.batch_size = self.batch_size = 2048  #
        self.args.deterministic = None
        self.args.rng_seed = 0
        print extract_valid_args(self.args, gen_backend)
        self.be = gen_backend(**extract_valid_args(self.args, gen_backend))

        embedding_dim = 100
        init_emb = Uniform(-0.1 / embedding_dim, 0.1 / embedding_dim)
        init_glorot = GlorotUniform()
        self.layers = [
            LookupTable(vocab_size=self.n_words,
                        embedding_dim=embedding_dim,
                        init=init_emb,
                        pad_idx=0,
                        update=True,
                        name="LookupTable"),
            Dropout(keep=0.5),
            BiLSTM(100,
                   init=init_glorot,
                   activation=Tanh(),
                   gate_activation=Logistic(),
                   reset_cells=True,
                   split_inputs=False,
                   name="BiLSTM"),
            RecurrentMean(),
            Affine(1,
                   init_glorot,
                   bias=init_glorot,
                   activation=Identity(),
                   name="Affine")
        ]

        self.wordbatch = wordbatch.WordBatch(normalize_text,
                                             n_words=self.n_words,
                                             extractors=[(wordbatch.WordSeq, {
                                                 "seq_maxlen":
                                                 self.maxlen
                                             })])

        if datadir == None:
            self.model = Model(self.layers)
            self.model.load_params(pickle_model)
            self.wordbatch = pkl.load(gzip.open(pickle_model + ".wb", 'rb'))
        else:
            self.train(datadir, pickle_model)

    def remove_unks(self, x):
        return [[self.n_words if w >= self.n_words else w for w in sen]
                for sen in x]

    def format_texts(self, texts):
        return self.remove_unks(self.wordbatch.transform(texts))

    class ThreadWithReturnValue(Thread):
        def __init__(self,
                     group=None,
                     target=None,
                     name=None,
                     args=(),
                     kwargs={},
                     Verbose=None):
            Thread.__init__(self, group, target, name, args, kwargs, Verbose)
            self._return = None

        def run(self):
            if self._Thread__target is not None:
                self._return = self._Thread__target(*self._Thread__args,
                                                    **self._Thread__kwargs)

        def join(self):
            Thread.join(self)
            return self._return

    def train(self, datadir, pickle_model=""):
        texts = []
        labels = []
        training_data = os.listdir(datadir)
        rcount = 0
        texts2 = []
        batchsize = 100000

        t = None
        for jsonfile in training_data:
            with open(datadir + "/" + jsonfile, u'r') as inputfile:
                for line in inputfile:
                    #if rcount > 1000000: break
                    try:
                        line = json.loads(line.strip())
                    except:
                        continue
                    for review in line["Reviews"]:
                        rcount += 1
                        if rcount % 100000 == 0: print rcount
                        if rcount % 8 != 0: continue
                        if "Overall" not in review["Ratings"]: continue
                        texts.append(review["Content"])
                        labels.append(
                            (float(review["Ratings"]["Overall"]) - 3) * 0.5)
                        if len(texts) % batchsize == 0:
                            if t != None: texts2.append(t.join())
                            t = self.ThreadWithReturnValue(
                                target=self.wordbatch.transform,
                                args=(texts, ))
                            t.start()
                            texts = []
        texts2.append(t.join())
        texts2.append(self.wordbatch.transform(texts))
        del (texts)
        texts = sp.vstack(texts2)

        self.wordbatch.dictionary_freeze = True

        train = [
            np.asarray(texts, dtype='int32'),
            np.asanyarray(labels, dtype='float32')
        ]
        train[1].shape = (train[1].shape[0], 1)

        num_epochs = 10
        cost = GeneralizedCost(costfunc=SumSquared())
        self.model = Model(layers=self.layers)
        optimizer = Adam(learning_rate=0.01)

        index_shuf = list(range(len(train[0])))
        random.shuffle(index_shuf)
        train[0] = np.asarray([train[0][x] for x in index_shuf], dtype='int32')
        train[1] = np.asarray([train[1][x] for x in index_shuf],
                              dtype='float32')
        train_iter = ArrayIterator(train[0],
                                   train[1],
                                   nclass=1,
                                   make_onehot=False)
        self.model.fit(train_iter,
                       optimizer=optimizer,
                       num_epochs=num_epochs,
                       cost=cost,
                       callbacks=Callbacks(self.model,
                                           **self.args.callback_args))

        if pickle_model != "":
            self.model.save_params(pickle_model)
            with gzip.open(pickle_model + ".wb", 'wb') as model_file:
                pkl.dump(self.wordbatch, model_file, protocol=2)

    def predict_batch(self, texts):
        input = np.array(self.format_texts(texts))
        output = np.zeros((texts.shape[0], 1))
        test = ArrayIterator(input, output, nclass=1, make_onehot=False)
        results = [row[0] for row in self.model.get_outputs(test)]
        return results
示例#28
0
        nlp = spacy.load('en')
        pos_tokens = []
        for doc in [nlp(t) for t in input_texts]:
            vec = [pos_vocab[t.tag_.lower()] if t.tag_.lower(
            ) in pos_vocab else len(pos_vocab) + 1 for t in doc]
            pos_tokens.append(vec)
        pos_features, _ = get_paddedXY_sequence(
            pos_tokens, [], sentence_length=sentence_len, shuffle=False)
        input_features.append(TaggedTextSequence(
            sentence_len, pos_features, vec_input=False))

    if len(input_features) > 1:
        x = MultiSequenceDataIterator(input_features, ignore_y=True)
    else:
        x = input_features[0]

    model = Model(args.model)
    model.initialize(dataset=x)
    preds = model.get_outputs(x).argmax(2)

    # print inferred tags and original text
    print_nps = args.print_only_nps
    rev_y = {v + 1: k for k, v in mp['y_vocab'].items()}
    for sen, text in zip(preds, input_texts):
        text_tokens = text.lower().split()
        text_tags = [rev_y[i] for i in sen if i > 0]
        if print_nps is True:
            print(extract_nps(text_tokens, text_tags))
        else:
            print(list(zip(text_tokens, text_tags)))
示例#29
0
X_val = np.hstack([X_val_info, hit_flat])

mu = np.mean(X_train, axis=0).reshape(1, -1)
s = np.std(X_train, axis=0).reshape(1, -1)
s[s == 0] = 1

X_train = (X_train - mu) / s
X_val = (X_val - mu) / s

# X = np.random.rand(10000, 100)
# y = np.sum(X, axis=1).reshape(-1, 1)
# y = np.hstack([y, y])

train_set = ArrayIterator(X=X_train, y=y_train, make_onehot=False)
val_set = ArrayIterator(X=X_val, y=y_val, make_onehot=False)

print("!!! TEST STARTED !!!")
for bs in [8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]:
    be = gen_backend('gpu', batch_size=bs)
    bnn = Model("bin_model/final_model.prm")
    tries = 3
    t_start = time.time()
    for i in range(tries):
        out = bnn.get_outputs(train_set)
    t_end = time.time()

    tot_time = t_end - t_start
    n = X_train.shape[0]
    fps = tries * n / tot_time
    print("fps {}; batch size {}; time elapsed: {}".format(fps, bs, tot_time))
示例#30
0
optimizer = RMSProp(gradient_clip_value=gradient_clip_value, stochastic_round=args.rounding)

# configure callbacks
callbacks = Callbacks(model, eval_set=valid_set, **args.callback_args)

# train model
model.fit(train_set,
          optimizer=optimizer,
          num_epochs=args.epochs,
          cost=cost, callbacks=callbacks)

# obtain predictions
shape = (valid_set.nbatches, args.batch_size, time_steps)
if num_beams == 0:
    ypred = model.get_outputs(valid_set)
    # flip the reversed predictions back to normal sentence order
    prediction = ypred.argmax(2).reshape(shape).transpose(1, 0, 2)[:, :, ::-1]
else:
    ypred = model.get_outputs_beam(valid_set, num_beams=num_beams)
    prediction = ypred.reshape(shape).transpose(1, 0, 2)[:, :, ::-1]

# print some examples
src_dict, tgt_dict = valid_set.s_index_to_token, valid_set.t_index_to_token
for i in range(3):
    print_sample(ex_source=valid_set.X[i, 0, :],
                 ex_reference=valid_set.y[i, 0, :],
                 ex_prediction=prediction[i, 0, :],
                 src_dict=src_dict, tgt_dict=tgt_dict)

# compute BLEU scores
示例#31
0
文件: model.py 项目: unyqhz/sp-2016
        Dropout(0.4),
        Conv((3, 3, 256), init=gauss, strides=small, **common),
        Dropout(0.2),
        Conv((2, 2, 512), init=gauss, strides=tiny, **common),
        Conv((2, 2, 128), init=gauss, strides=tiny, **common),
        DeepBiRNN(64, init=glorot, reset_cells=True, depth=5, **common),
        RecurrentMean(),
        Affine(nout=2, init=gauss, activation=Softmax())
    ]
}[subj]

model = Model(layers=layers)
opt = Adagrad(learning_rate=rate)
callbacks = Callbacks(model, eval_set=test, **args.callback_args)
if args.validate_mode:
    evaluator = Evaluator(subj, data_dir, test)
    callbacks.add_callback(evaluator)
    preds_name = 'eval.'
else:
    preds_name = 'test.'
cost = GeneralizedCost(costfunc=CrossEntropyBinary())

model.fit(tain,
          optimizer=opt,
          num_epochs=nepochs,
          cost=cost,
          callbacks=callbacks)
preds = model.get_outputs(test)[:, 1]
preds_file = preds_name + str(subj) + '.npy'
np.save(os.path.join(out_dir, preds_file), preds)
示例#32
0
                pos_vocab[t.tag_.lower()]
                if t.tag_.lower() in pos_vocab else len(pos_vocab) + 1
                for t in doc
            ]
            pos_tokens.append(vec)
        pos_features, _ = get_paddedXY_sequence(pos_tokens, [],
                                                sentence_length=sentence_len,
                                                shuffle=False)
        input_features.append(
            TaggedTextSequence(sentence_len, pos_features, vec_input=False))

    if len(input_features) > 1:
        x = MultiSequenceDataIterator(input_features, ignore_y=True)
    else:
        x = input_features[0]

    model = Model(args.model)
    model.initialize(dataset=x)
    preds = model.get_outputs(x).argmax(2)

    # print inferred tags and original text
    print_nps = args.print_only_nps
    rev_y = {v + 1: k for k, v in mp['y_vocab'].items()}
    for sen, text in zip(preds, input_texts):
        text_tokens = text.lower().split()
        text_tags = [rev_y[i] for i in sen if i > 0]
        if print_nps is True:
            print(extract_nps(text_tokens, text_tags))
        else:
            print(list(zip(text_tokens, text_tags)))
示例#33
0
文件: run.py 项目: locussam/fmc-2017
              activation=Rectlin(),
              batch_norm=True)
nchan = 16
layers = [
    Conv((1, 2, nchan), **common),
    Conv((1, 2, nchan), **common),
    Conv((1, 2, nchan / 2), **common),
    Conv((1, 2, nchan / 4), **common),
    Conv((1, 2, nchan / 8), **common),
    Conv((1, 2, nchan / 16), **common),
    Dropout(0.8),
    DeepBiRNN(16, init=init, activation=Rectlin(), reset_cells=True, depth=3),
    RecurrentMean(),
    Affine(nout=1, init=init, activation=None)
]

cost = GeneralizedCost(costfunc=SumSquared())
net = Model(layers=layers)
callbacks = Callbacks(net, eval_set=valid, **args.callback_args)

net.fit(train,
        optimizer=opt,
        num_epochs=args.epochs,
        cost=cost,
        callbacks=callbacks)

train_preds = net.get_outputs(train)
print('  training R %.4f' % r_score(dataset.train_y, train_preds))
valid_preds = net.get_outputs(valid)
print('validation R %.4f' % r_score(dataset.valid_y, valid_preds))
示例#34
0
def main():
    # larger batch sizes may not fit on GPU
    parser = NeonArgparser(__doc__, default_overrides={'batch_size': 4})
    parser.add_argument("--bench", action="store_true", help="run benchmark instead of training")
    parser.add_argument("--num_classes", type=int, default=12, help="number of classes in the annotation")
    parser.add_argument("--height", type=int, default=256, help="image height")
    parser.add_argument("--width", type=int, default=512, help="image width")

    args = parser.parse_args(gen_be=False)

    # check that image dimensions are powers of 2
    if((args.height & (args.height - 1)) != 0):
        raise TypeError("Height must be a power of 2.")
    if((args.width & (args.width - 1)) != 0):
        raise TypeError("Width must be a power of 2.")

    (c, h, w) = (args.num_classes, args.height, args.width)

    # need to use the backend with the new upsampling layer implementation
    be = NervanaGPU_Upsample(rng_seed=args.rng_seed,
                             device_id=args.device_id)
    # set batch size
    be.bsz = args.batch_size

    # couple backend to global neon object
    NervanaObject.be = be

    shape = dict(channel_count=3, height=h, width=w, subtract_mean=False)
    train_params = ImageParams(center=True, flip=False,
                               scale_min=min(h, w), scale_max=min(h, w),
                               aspect_ratio=0, **shape)
    test_params = ImageParams(center=True, flip=False,
                              scale_min=min(h, w), scale_max=min(h, w),
                              aspect_ratio=0, **shape)
    common = dict(target_size=h*w, target_conversion='read_contents',
                  onehot=False, target_dtype=np.uint8, nclasses=args.num_classes)

    train_set = PixelWiseImageLoader(set_name='train', repo_dir=args.data_dir,
                                      media_params=train_params,
                                      shuffle=False, subset_percent=100,
                                      index_file=os.path.join(args.data_dir, 'train_images.csv'),
                                      **common)
    val_set = PixelWiseImageLoader(set_name='val', repo_dir=args.data_dir,media_params=test_params, 
                      index_file=os.path.join(args.data_dir, 'val_images.csv'), **common)

    # initialize model object
    layers = gen_model(c, h, w)
    segnet_model = Model(layers=layers)

    # configure callbacks
    callbacks = Callbacks(segnet_model, eval_set=val_set, **args.callback_args)

    opt_gdm = GradientDescentMomentum(1.0e-6, 0.9, wdecay=0.0005, schedule=Schedule())
    opt_biases = GradientDescentMomentum(2.0e-6, 0.9, schedule=Schedule())
    opt_bn = GradientDescentMomentum(1.0e-6, 0.9, schedule=Schedule())
    opt = MultiOptimizer({'default': opt_gdm, 'Bias': opt_biases, 'BatchNorm': opt_bn})

    cost = GeneralizedCost(costfunc=CrossEntropyMulti())

    if args.bench:
        segnet_model.initialize(train_set, cost=cost)
        segnet_model.benchmark(train_set, cost=cost, optimizer=opt)
        sys.exit(0)
    else:
        segnet_model.fit(train_set, optimizer=opt, num_epochs=args.epochs, cost=cost, callbacks=callbacks)

    # get the trained segnet model outputs for valisation set
    outs_val = segnet_model.get_outputs(val_set)

    with open('outputs.pkl', 'w') as fid:
        pickle.dump(outs_val, fid, -1)
示例#35
0
layers = []
layers.append(Affine(nout=100, init=init_norm,activation=Rectlin()))
layers.append(Affine(nout=nclass, init=init_norm,activation=Softmax()))


# we now construct the model
mlp = Model(layers=layers)

# We define our cost function - need to work out which is best
cost = GeneralizedCost(costfunc=CrossEntropyMulti())

# Learning rules (ie what kind of gradient descent, stochastic here)
# learning rate is 0.1 here, momentum coefficient (eta) is 0.9

optimizer = GradientDescentMomentum(0.1, momentum_coef=0.9)

# Progress bar for each epoch - what's an epoch again? by default 10 Crazy magic - don't even go here!

callbacks = Callbacks(mlp, eval_set=test_set, **args.callback_args)

# put everything together!

mlp.fit(train_set, optimizer=optimizer, num_epochs=args.epochs, cost=cost,
        callbacks=callbacks)

# Calculate test set results
results = mlp.get_outputs(test_set)

# work out the performance!
error = mlp.eval(test_set, metric=Misclassification())
print('Misclassification error rate is {}'.format(error))
print('Batch size = {}'.format(args.batch_size))

# setup backend
be = gen_backend(**extract_valid_args(args, gen_backend))

# Set up the testset to load via aeon
image_config = dict(height=64, width=64, channels=1)
label_config = dict(binary=False)
config = dict(type="image,label",
              image=image_config,
              label=label_config,
              manifest_filename=testFileName,
              minibatch_size=args.batch_size,
              subset_fraction=1)
test_set = DataLoader(config, be)
test_set = TypeCast(test_set, index=0, dtype=np.float32)  # cast image to float
test_set = OneHot(test_set, index=1, nclasses=2)

lunaModel = Model('LUNA16_VGG_model_no_batch.prm')

pred, target = lunaModel.get_outputs(test_set, return_targets=True) # Reshape to a single prediction vector
pred = pred.T
target = target.T

np.set_printoptions(precision=3, suppress=True)
print(' ')
print(pred)
print(target)

示例#37
0
cost = GeneralizedCost(costfunc=CrossEntropyMulti())
mlp = Model(layers=layers)
callbacks = Callbacks(mlp, train, **args.callback_args)
mlp.fit(train,
        optimizer=opt,
        num_epochs=args.epochs,
        cost=cost,
        callbacks=callbacks)
train.exit_batch_provider()

test = ClassifierLoader(repo_dir=args.test_data_dir,
                        inner_size=imwidth,
                        set_name='validation',
                        do_transforms=False)
test.init_batch_provider()
probs = mlp.get_outputs(test)
test.exit_batch_provider()

filcsv = np.loadtxt(os.path.join(args.test_data_dir, 'val_file.csv'),
                    delimiter=',',
                    skiprows=1,
                    dtype=str)
files = [os.path.basename(row[0]) for row in filcsv]
datadir = os.path.dirname(args.data_dir)

with open(os.path.join(datadir, 'sample_submission.csv'), 'r') as fd:
    header = fd.readline()

with gzip.open('subm.csv.gz', 'wb') as fd:
    fd.write(header)
    for i in range(probs.shape[0]):
示例#38
0
assert args.model_file is not None, "need a model file for testing"
model = Model(args.model_file)

assert 'test' in args.manifest, "Missing test manifest"
assert 'categories' in args.manifest, "Missing categories file"

category_map = {
    t[0].decode(): t[1]
    for t in np.genfromtxt(
        args.manifest['categories'], dtype=None, delimiter=',')
}

test = make_test_loader(args.manifest['test'], args.manifest_root, model.be)

clip_pred = model.get_outputs(test)

video_pred = accumulate_video_pred(args.manifest['test'], args.manifest_root,
                                   clip_pred)

correct = np.zeros((len(video_pred), 2))

TOP1, TOP5 = 0, 1  # indices in correct count array (for readability)

for idx, (video_name, (label,
                       prob_list)) in enumerate(list(video_pred.items())):
    # Average probabilities for each clip
    tot_prob = np.sum(prob_list, axis=0)
    label_idx = category_map[label]

    correct[idx, TOP1] = (label_idx == tot_prob.argmax())
示例#39
0
    layers.append(Pooling(2, strides=2))
    nchan *= 2
layers.append(DropoutBinary(keep=0.2))
layers.append(Affine(nout=447, init=init, activation=Softmax()))

cost = GeneralizedCost(costfunc=CrossEntropyMulti())
mlp = Model(layers=layers)
callbacks = Callbacks(mlp, train, **args.callback_args)
mlp.fit(train, optimizer=opt, num_epochs=args.epochs, cost=cost,
        callbacks=callbacks)
train.exit_batch_provider()

test = ClassifierLoader(repo_dir=args.test_data_dir, inner_size=imwidth,
                        set_name='validation', do_transforms=False)
test.init_batch_provider()
probs = mlp.get_outputs(test)
test.exit_batch_provider()

filcsv = np.loadtxt(os.path.join(args.test_data_dir, 'val_file.csv'),
                    delimiter=',', skiprows=1, dtype=str)
files = [os.path.basename(row[0]) for row in filcsv]
datadir = os.path.dirname(args.data_dir)

with open(os.path.join(datadir, 'sample_submission.csv'), 'r') as fd:
    header = fd.readline()

with gzip.open('subm.csv.gz', 'wb') as fd:
    fd.write(header)
    for i in range(probs.shape[0]):
        fd.write('{},'.format(files[i]))
        row = probs[i].tolist()
示例#40
0
                                         is_augment=False)

    files = []
    locations = []
    for filename in filenames:
        files.append(filename.split('/')[-1])
        locations.append(filename.split(files[-1])[0])

    log('Data loaded from ' + os.path.join(NACPolarDir, 'Unlabelled Regions'))

    classes = ["Crater", "Not Crater"]
    inference_set = ArrayIterator(polar_predict,
                                  None,
                                  nclass=2,
                                  lshape=(1, 32, 32))
    out = model.get_outputs(inference_set)

    labels = np.argmax(out, axis=1)

    count = []
    threshold = []
    threshold = np.subtract(
        1,
        np.divide(
            np.multiply(-400, np.power((np.arange(100).astype(float)), 2)),
            10 * np.min(
                np.multiply(-400, np.power(
                    (np.arange(100).astype(float)), 2)))))

    for i in range(100):
        count.append(
示例#41
0
from neon.optimizers import GradientDescentMomentum

optimizer = GradientDescentMomentum(0.1, momentum_coef=0.9)
#callbacks show the progress of calculations
from neon.callbacks.callbacks import Callbacks

callbacks = Callbacks(mlp, eval_set=test_set, **args.callback_args)

#train the model
mlp.fit(train_set,
        optimizer=optimizer,
        num_epochs=10,
        cost=cost,
        callbacks=callbacks)
#The variable results is a numpy array with
#shape (num_test_examples, num_outputs) = (10000,10) with the model probabilities for each label.
results = mlp.get_outputs(test_set)

print "labels: %s, %s, %s: " % (y_test[2], y_test[5], y_test[100])
#ind1, val1 = max(results[2].tolist())
#ind2, val2 = max(results[5].tolist())
#ind3, val3 = max(results[100].tolist())
print "results: %s, %s, %s:" % (results[2].tolist(), results[5].tolist(),
                                results[100].tolist())

from neon.transforms import Misclassification

# evaluate the model on test_set using the misclassification metric
error = mlp.eval(test_set, metric=Misclassification()) * 100
print('Misclassification error = %.1f%%' % error)
示例#42
0
文件: demo.py 项目: rlugojr/neon
config_files = [demo_config] if os.path.exists(demo_config) else []

parser = NeonArgparser(__doc__, default_config_files=config_files)
parser.add_argument('--input_video', help='video file')
parser.add_argument('--output_video', help='Video file with overlayed inference hypotheses')
args = parser.parse_args()

assert args.model_file is not None, "need a model file for testing"
model = Model(args.model_file)

assert 'categories' in args.manifest, "Missing categories file"
category_map = {t[0]: t[1] for t in np.genfromtxt(args.manifest['categories'],
                                                  dtype=None, delimiter=',')}

# Make a temporary directory and clean up afterwards
outdir = mkdtemp()
atexit.register(shutil.rmtree, outdir)
caption_file = os.path.join(outdir, 'caption.txt')

manifest = segment_video(args.input_video, outdir)

test = make_inference_loader(manifest, model.be)
clip_pred = model.get_outputs(test)
tot_prob = clip_pred[:test.ndata, :].mean(axis=0)
top_5 = np.argsort(tot_prob)[-5:]

hyps = ["{:0.5f} {}".format(tot_prob[i], category_map[i]) for i in reversed(top_5)]
np.savetxt(caption_file, hyps, fmt='%s')

caption_video(args.input_video, caption_file, args.output_video)
示例#43
0
    print("Loaded " + test_file_name)
    image.show()
    image = image.resize(size, Image.ANTIALIAS)
    r, g, b = image.split()
    image = Image.merge("RGB", (b, g, r))
    image = np.asarray(image, dtype=np.float32)
    image = np.transpose(image, (2, 0, 1))
    return image.reshape(1, L) - 127


x_new[0] = load_sample(iamge_dir + "forward.jpg")
x_new[1] = load_sample(image_dir + "right.jpg")
x_new[2] = load_sample(image_dir + "left.jpg")
x_new[3] = load_sample(image_dir + "backward.jpg")

# Run neural network
inference_set = ArrayIterator(x_new, None, nclass=nclasses, lshape=(3, H, W))
out = mlp.get_outputs(inference_set)
# print out

print(class_names[out[0].argmax()])
print(class_names[out[1].argmax()])
print(class_names[out[2].argmax()])
print(class_names[out[3].argmax()])

# Sanity check 2
out = mlp.get_outputs(test)
print "Validation set result:"
print(out.argmax(1))
print "Compare the above to ground truth in dora/train/neon/val_file.csv.gz"
示例#44
0
    model = Model(layers=layers)
    cost = GeneralizedCost(MeanSquared())
    optimizer = RMSProp(stochastic_round=args.rounding)

    callbacks = Callbacks(model, eval_set=valid_set, **args.callback_args)

    # fit model
    model.fit(train_set,
              optimizer=optimizer,
              num_epochs=args.epochs,
              cost=cost,
              callbacks=callbacks)

    # =======visualize how the model does on validation set==============
    # run the trained model on train and valid dataset and see how the outputs match
    train_output = model.get_outputs(train_set).reshape(
        -1, train_set.nfeatures)
    valid_output = model.get_outputs(valid_set).reshape(
        -1, valid_set.nfeatures)
    train_target = train_set.y_series
    valid_target = valid_set.y_series

    # calculate accuracy
    terr = err(train_output, train_target)
    verr = err(valid_output, valid_target)

    print 'terr = %g, verr = %g' % (terr, verr)

    if do_plots:
        plt.figure()
        plt.plot(train_output[:, 0],
                 train_output[:, 1],
示例#45
0
          Deconv(fshape=(2, 2, 1), init=init_uni, activation=Logistic(), strides=2, padding=1)]

# Define the cost
cost = GeneralizedCost(costfunc=CrossEntropyMulti())

model = Model(layers=layers)

# configure callbacks
callbacks = Callbacks(model, **args.callback_args)

# Fit the model
model.fit(train_set, optimizer=opt_gdm, num_epochs=args.epochs,
          cost=cost, callbacks=callbacks)

# Let's predict the test set with the current model
results = model.get_outputs(test_set)

# Plot the predicted images
try:
    import matplotlib.pyplot as plt
    fi = 0

    # Plot a 10x12 set of predictions and originals
    nrows = 10
    ncols = 12   

    preds = np.zeros((height * nrows, width * ncols))
    origs = np.zeros((height * nrows, width * ncols))

    idxs = [(row, col) for row in range(nrows) for col in range(ncols)]
示例#46
0
    Conv(fshape=(4, 4, 32), init=init_uni, activation=Rectlin()),
    Pooling(fshape=2, strides=2),
    Conv(fshape=(4, 4, 32), init=init_uni, activation=Rectlin()),
    Pooling(fshape=2, strides=2),
    Affine(nout=500, init=init_uni, activation=Rectlin()),
    Affine(nout=11, init=init_uni, activation=Softmax())
]

model = Model(layers)

model.load_params('model.pkl')
data = readfile('PreImage', 'label.csv')
X_test = data.test_data
test_set = ArrayIterator(X_test, None, nclass=11, lshape=(1, 200, 200))
true = data.test_label
out = model.get_outputs(test_set)
row = len(X_test)
pred = np.zeros((row, 1))
i = 0
while i < row:
    pred[i] = out[i].argmax()
    i = i + 1
pred = pred + 1
loss = abs(true - pred)
print(loss)
count = 0
for i in range(len(loss)):
    if loss[i] != 0:
        count = count + 1
print(count)
print(1 - float(count) / 110)
示例#47
0
# neon_logger.display('Misclassification error (test) = {:.2f}%'.format(lunaModel.eval(test_set, metric=Misclassification())[0] * 100))

# neon_logger.display('Precision/recall (test) = {}'.format(lunaModel.eval(test_set, metric=PrecisionRecall(num_classes=2))))
# neon_logger.display('Misclassification (test) = {}'.format(lunaModel.eval(test_set, metric=Misclassification())))
# neon_logger.display('Accuracy (test) = {}'.format(lunaModel.eval(test_set, metric=Accuracy())))
#dfOuts = pd.DataFrame(lunaModel.get_outputs(test_set))
#dfOuts.to_csv('outTest.csv')

#pr = lunaModel.eval(test_set, metric=PrecisionRecall(num_classes=2))
#print(pr.outputs)

dfTarget = pd.read_csv(testFileName, header=None, names=['file', 'label'])
ot = np.zeros(dfTarget.shape[0])
ot[np.where(dfTarget['label'] == 'label_1.txt')[0]] = 1
dfTarget['target'] = ot

pred = lunaModel.get_outputs(test_set)
#print(pred)
pred = np.argmax(pred, axis=1)
# print(pred),
# print('predictions')

target = np.array(dfTarget['target'].values, dtype=int)
# print(target),
# print('targets')

# print('All equal = {}'.format(np.array_equal(pred, target)))

from sklearn.metrics import classification_report

print(classification_report(target, pred, target_names=['Class 0', 'Class 1']))
示例#48
0
文件: trainbot.py 项目: oomwoo/ubuntu
L = W*H*3
size = H, W
x_new = np.zeros((128, L), dtype=np.float32)


def load_sample(test_file_name):
    image = Image.open(test_file_name)
    print("Loaded " + test_file_name)
    image.show()
    image = image.resize(size)  # , Image.ANTIALIAS)
    r, g, b = image.split()
    image = Image.merge("RGB", (b, g, r))
    image = np.asarray(image, dtype=np.float32)
    image = np.transpose(image, (2, 0, 1))
    return image.reshape(1, L) - 127

x_new[0] = load_sample(home_dir + "/ubuntu/test/forward.jpg")
x_new[1] = load_sample(home_dir + "/ubuntu/test/right.jpg")
x_new[2] = load_sample(home_dir + "/ubuntu/test/left.jpg")
x_new[3] = load_sample(home_dir + "/ubuntu/test/backward.jpg")

# Run neural network
inference_set = ArrayIterator(x_new, None, nclass=nclass, lshape=(3, H, W))
out = mlp.get_outputs(inference_set)
# print out

print(class_names[out[0].argmax()])
print(class_names[out[1].argmax()])
print(class_names[out[2].argmax()])
print(class_names[out[3].argmax()])
示例#49
0
文件: emneon.py 项目: elhuhdron/emdrp
            else:
                # added small hack in model/model.py in neon so that batch_meta gets serialized
                print('WARNING: No batch meta in saved model file!')
        else:
            # make dummy random data just for testing model inits
            test = RandomEMDataIterator(name='outputs')
            
        print('Model output (forward prop) for %d testing batches, %d examples/batch' % (test.nmacrobatches,
            test.parser.num_cases_per_batch)); 
        feature_path = ''; #out_thread = None
        for i in range(test.nmacrobatches):
            batchnum = test.batch_range[0]+i
            last_batch = i==test.nmacrobatches-1
            sys.stdout.write('%d ... ' % (batchnum,)); t = time.time()
    
            outputs = model.get_outputs(test)

            # serial version            
            test.parser.checkOutputCubes(feature_path, batchnum, last_batch, outputs=outputs)

            # xxx - could not get any improvment here, possibly overhead of the thread is longer than the
            #   computations for formatting the outputs (the h5 write itself is parallelized in the parser)            
            ## parallel version
            #if out_thread: out_thread.join()
            #out_thread = FuncThread(test.parser.checkOutputCubes, feature_path, batchnum, last_batch, outputs)
            #out_thread.start()
            #if last_batch: out_thread.join()

            sys.stdout.write('\tdone in %.2f s\n' % (time.time() - t,)); sys.stdout.flush()
            
        if args.data_config: