Exemple #1
0
def fit_specialists(fname_pretrain=None):
    if fname_pretrain:
        with open(fname_pretrain, 'rb') as f:
            net_pretrain = pickle.load(f)
    else:
        net_pretrain = None

    specialists = OrderedDict()

    for setting in SPECIALIST_SETTINGS:
        cols = setting['columns']
        X, y = load2d(cols=cols)

        model = clone(net8)
        model.output_num_units = y.shape[1]
        model.batch_iterator_train.flip_indices = setting['flip_indices']
        # set number of epochs relative to number of training examples:
        model.max_epochs = int(1e7 / y.shape[0])

        if 'kwargs' in setting:
            # an option 'kwargs' in the settings list may be used to
            # set any other parameter of the net:
            vars(model).update(setting['kwargs'])

        if net_pretrain is not None:
            # if a pretrain model was given, use it to initialize the
            # weights of our new specialist model:
            model.load_params_from(net_pretrain)

        print("Training model for columns {} for {} epochs".format(
            cols, model.max_epochs))
        model.fit(X, y)
        specialists[cols] = model

    from pickle import dump
    with open("fit-specialists_{0}.pickle".format(sys.argv[1]), 'wb') as f:
        # this time we're persisting a dictionary with all models:
        dump(specialists, f, -1)
  		('hidden5', layers.DenseLayer),
  		('output', layers.DenseLayer)
  		],
  	input_shape=(None, 1, 96, 96),
  	conv1_num_filters=32, conv1_filter_size=(3, 3), pool1_pool_size=(2, 2),
  	conv2_num_filters=64, conv2_filter_size=(2, 2), pool2_pool_size=(2, 2),
  	conv3_num_filters=128, conv3_filter_size=(2, 2), pool3_pool_size=(2, 2),
  	hidden4_num_units=500,
  	hidden5_num_units=500,
  	output_num_units=30,
  	output_nonlinearity=None,

  	update_learning_rate=0.01,
  	update_momentum=0.9,

  	regression=True,
  	batch_iterator_train=FlipBatchIterator(batch_size=128),
  	max_epochs=3000,
  	verbose=1
  	)

X, y = load2d() # load 2D data
net3.fit(X, y)

# Training for 3000 epochs will take a while. We'll pickle the
# trained model so that we can load it back later:

from pickle import dump
with open("net3_{0}.pickle".format(sys.argv[1]), 'wb') as f:
	dump(net3, f, -1)
import matplotlib.pyplot as pyplot
from kfkd import load2d, plot_sample

X, y = load2d()
X_flipped = X[:, :, :, ::-1]  # simple slice to flip all images

# plot two images
fig = pyplot.figure(figsize=(6, 3))

ax = fig.add_subplot(1, 2, 1, xticks=[], yticks=[])
plot_sample(X[1], y[1], ax)

ax = fig.add_subplot(1, 2, 2, xticks=[], yticks=[])
plot_sample(X_flipped[1], y[1], ax)

pyplot.show()
Exemple #4
0
import matplotlib.pyplot as pyplot
from kfkd import load, load2d, plot_loss, plot_sample
from net1 import net1
from net2 import net2

# X1, y1 = load()
# net1.fit(X1, y1)

# X2, y2 = load2d() # load 2D data
# net2.fit(X2, y2)

sample1 = load(test=True)[0][6:7]
sample2 = load2d(test=True)[0][6:7]
y_pred1 = net1.predict(sample1)[0]
y_pred2 = net2.predict(sample2)[0]

fig = pyplot.figure(figsize=(6, 3))

ax = fig.add_subplot(1, 2, 1, xticks=[], yticks=[])
plot_sample(sample1[0], y_pred1, ax)

ax = fig.add_subplot(1, 2, 2, xticks=[], yticks=[])
plot_sample(sample1[0], y_pred2, ax)

pyplot.show()
    return opts

opts = parseOptions()

#
# logging
#
head = '%(asctime)-15s - %(message)s'
logging.basicConfig(level=logging.DEBUG, format=head)

#
# Load data
#
FTRAIN = os.path.join(opts.dataDir, 'training.csv')
FTEST  = os.path.join(opts.dataDir, 'test.csv')
X, y, = load2d(FTRAIN=FTRAIN, FTEST=FTEST)

if opts.augment:
    print "Augmenting data"
    X, y = augment(X, y) 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state = 42)
trainIter = mx.io.NDArrayIter(data = X_train, label = y_train, batch_size = 64)
valIter   = mx.io.NDArrayIter(data = X_test , label = y_test , batch_size = 64)

#
# Select model
#
if opts.model=='mlp':
    net = mlp()
elif opts.model=='lenet2':
    net = lenet2()