import pickle

from tools import build_transfer_net
from tools.datasets.urban_tribes import load_data


def transfer_learn(layer_name, nb_sample, nb_epoch, output_file):
    """Transfer learning for image classification.

    Args:
        layer_name: Transfer layer name.
        nb_sample: Number of samples per categories.
        nb_epoch: Number of epochs to train.
        output_file: Name of the output file to pick history to.
    """
    (x_train, y_train), (x_val, y_val), (x_test, y_test) = \
        load_data(images_per_category=nb_sample)
    model = build_transfer_net(output_dim=11,
                               transfer_layer_name=layer_name)
    model.compile(optimizer='adadelta', loss='categorical_crossentropy',
                  metrics=['accuracy'])
    history = model.fit(x_train, y_train, batch_size=nb_sample,
                        nb_epoch=nb_epoch, validation_data=(x_val, y_val))
    with open(output_file, 'wb') as f:
        pickle.dump(history.history, f)

@@ -8,16 +8,29 @@
 pickle%0A%0A
+import h5py%0A%0A
 from too
@@ -110,16 +110,1522 @@
 _data%0A%0A%0A
+class Session(object):%0A    %22%22%22Training session.%0A%0A    This is a helper class to bundle model and history together and make it%0A    easy to resume training and recording history.%0A    %22%22%22%0A    def __init__(self, model, history=None):%0A        self.model = model%0A        self.history = history%0A%0A    def dump(self, path):%0A        W, b = self.model.layers%5B-1%5D.get_weights()%0A        with h5py.File(path, 'w') as f:%0A            f.create_dataset('W', data=W)%0A            f.create_dataset('b', data=b)%0A            group = f.create_group('history')%0A            for k, v in self.history.items():%0A                group.create_dataset(k, data=v)%0A%0A    @classmethod%0A    def load(cls, model, path):%0A        with h5py.File(path, 'r') as f:%0A            W = f%5B'W'%5D%5B:%5D%0A            b = f%5B'b'%5D%5B:%5D%0A            history = %7B%7D%0A            for k, v in f%5B'history'%5D.items():%0A                history%5Bk%5D = list(v)%0A        model.layers%5B-1%5D.set_weights(%5BW, b%5D)%0A        return cls(model, history=history)%0A%0A    def train(self, *args, **kwargs):%0A        new_history = self.model.fit(*args, **kwargs)%0A        self._record(new_history.history)%0A%0A    def _record(self, new_history):%0A        if self.history is None:%0A            self.history = new_history%0A        else:%0A            for key in self.history.keys():%0A                self.history%5Bkey%5D.extend(new_history%5Bkey%5D)%0A%0A    @staticmethod%0A    def _model_fpath(prefix):%0A        return prefix + '.model.h5'%0A%0A    @staticmethod%0A    def _history_fpath(prefix):%0A        return prefix + '.history.json'%0A%0A%0A
 def tran

Introduce session