def train(self, use_gpu, gpu_id):
        if use_gpu:
            cuda.check_cuda_available()
        xp = cuda.cupy if gpu_id >= 0 and use_gpu == True else np
        batch_count = 0
        for k, v in self.read_data.total_words_ids.items():
            if k in self.read_data.images_ids:
                image = np.asarray(Image.open(APP_ROOT + "/" + self.read_data.images_ids[k])).transpose(2, 0, 1)[::-1]
                image = image[:, self.start:self.stop, self.start:self.stop].astype(np.float32)
                image -= self.mean_image

                self.x_batch[batch_count] = image
                self.y_batch[batch_count] = self.trg_vocab.stoi(self.read_data.total_words_ids[k].split()[0])

                if batch_count < self.parameter_dict["minibatch"]:
                    x_data = xp.asarray(self.x_batch)
                    y_data = xp.asarray(self.y_batch)

                    x = chainer.Variable(x_data, volatile=True)
                    t = chainer.Variable(y_data, volatile=True)
                    self.parameter_dict["x"] = x
                    self.parameter_dict["first_word"] = t
                    encoderDecoderModel = EncoderDecoderModelAttention(self.parameter_dict)
                    encoderDecoderModel.train()
                    batch_count = 0
                batch_count = batch_count + 1
Example #2
0
    def __init__(self, gpu, model, model_type, out_dim):
        self.gpu = gpu
        self.model = 'bvlc_alexnet.caffemodel'
        self.model_type = 'alexnet'
        self.batchsize = 1
        self.out_dim = out_dim

        if self.gpu >= 0:
            cuda.check_cuda_available()

        print('Loading Caffe model file %s...' % self.model, file = sys.stderr)
        self.func = caffe.CaffeFunction(self.model)
        print('Loaded', file=sys.stderr)
        if self.gpu >= 0:
            cuda.get_device(self.gpu).use()
            self.func.to_gpu()

        if self.model_type == 'alexnet':
            self.in_size = 227
            mean_image = np.load('ilsvrc_2012_mean.npy')
            del self.func.layers[15:23]
            self.outname = 'pool5'
            #del self.func.layers[13:23]
            #self.outname = 'conv5'

            
        cropwidth = 256 - self.in_size
        start = cropwidth // 2
        stop = start + self.in_size
        self.mean_image = mean_image[:, start:stop, start:stop].copy()
Example #3
0
def inspect(image_path, mean, model_path, label, network, gpu=-1):
    network = network.split(os.sep)[-1]
    model_name = re.sub(r"\.py$", "", network)
    model_module = load_module(os.path.dirname(model_path), model_name)
    mean_image = pickle.load(open(mean, 'rb'))
    model = model_module.Network()
    serializers.load_hdf5(model_path, model)
    if gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(gpu).use()
        model.to_gpu()
    cropwidth = 256 - model.insize
    img = read_image(image_path, model, mean_image, cropwidth)
    x = np.ndarray((1, 3, model.insize, model.insize), dtype=np.float32)
    x[0] = img
    if gpu >= 0:
        x = cuda.to_gpu(x)
    score = model.predict(x)
    score = cuda.to_cpu(score.data)
    categories = np.loadtxt(label, str, delimiter="\t")
    top_k = 20
    prediction = zip(score[0].tolist(), categories)
    prediction.sort(cmp=lambda x, y:cmp(x[0], y[0]), reverse=True)
    ret = []
    for rank, (score, name) in enumerate(prediction[:top_k], start=1):
        ret.append({"rank": rank, "name": name, "score": "{0:4.1f}%".format(score*100)})
    return ret
Example #4
0
def train(args):
    time_start = timer()
    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()


    if args.path_vocab == '':
        vocab = create_from_dir(args.path_corpus)
    else:
        vocab = Vocabulary()
        vocab.load(args.path_vocab)
        logger.info("loaded vocabulary")

    if args.context_representation != 'word': # for deps or ner context representation, we need a new context vocab for NS or HSM loss function.
        vocab_context = create_from_annotated_dir(args.path_corpus, representation=args.context_representation)
    else :
        vocab_context = vocab

    vocab_ngram_tokens = None
    if args.subword != 'none':
        vocab_ngram_tokens = Vocabulary()
        vocab_ngram_tokens.load(args.path_vocab_ngram_tokens)


    loss_func = get_loss_func(args, vocab_context)
    model = get_model(args, loss_func, vocab, vocab_ngram_tokens)

    if args.gpu >= 0:
        model.to_gpu()
        logger.debug("model sent to gpu")

    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)

    if os.path.isfile(args.path_corpus):
        train, val = get_data(args.path_corpus, vocab)
        if args.test:
            train = train[:100]
            val = val[:100]
        train_iter = WindowIterator(train, args.window, args.batchsize)
        val_iter = WindowIterator(val, args.window, args.batchsize, repeat=False)
    else:
        train_iter = DirWindowIterator(path=args.path_corpus, vocab=vocab, window_size=args.window, batch_size=args.batchsize)
    updater = training.StandardUpdater(train_iter, optimizer, converter=convert, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.path_out)

    if os.path.isfile(args.path_corpus):
        trainer.extend(extensions.Evaluator(val_iter, model, converter=convert, device=args.gpu))
    trainer.extend(extensions.LogReport())
    if os.path.isfile(args.path_corpus):
        trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']))
    else:
        trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'elapsed_time']))
    # trainer.extend(extensions.ProgressBar())
    trainer.run()
    model = create_model(args, model, vocab)
    time_end = timer()
    model.metadata["execution_time"] = time_end - time_start
    return model
Example #5
0
 def test_xp(self):
     try:
         cuda.check_cuda_available()
         module = "cupy"
     except:
         module = "numpy"
     self.assertEqual(xp.__name__, module)
Example #6
0
    def to_gpu(self, device=None):
        """Copies parameter variables and persistent values to GPU.

        This method does not handle non-registered attributes. If some of such
        attributes must be copoied to GPU, the link implementation must
        override this method to do so.

        Args:
            device: Target device specifier. If omitted, the current device is
                used.

        Returns: self

        """
        cuda.check_cuda_available()
        if not self._cpu:
            return self
        d = self.__dict__
        with cuda.get_device(device):
            for name in self._params:
                d[name].to_gpu()
            for name in self._persistent:
                value = d[name]
                if isinstance(value, numpy.ndarray):
                    d[name] = cuda.to_gpu(value)
        self._cpu = False
        return self
Example #7
0
    def train(self, epoch=10, batch_size=32, gpu=False):
        if gpu:
            cuda.check_cuda_available()
        xp = cuda.cupy if gpu else np

        self.batch_size = batch_size

        label_types = ['none', 'tap', 'up', 'down', 'right', 'left']

        self.model = Alex(len(label_types))
        optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
        optimizer.setup(self.model)

        if gpu:
            self.model.to_gpu()

        training_data = TrainingData(IMAGE_ROOT, NOTE_ROOT, VIDEO_ROOT, SONG_LIST_PATH)
        self.x_train, self.x_test, self.y_train, self.y_test = training_data.get_train_data(label_types)
        data_size = self.x_train.shape[0]

        for ep in range(epoch):
            print('epoch {0}/{1}: (learning rate={2})'.format(ep + 1, epoch, optimizer.lr))
            indexes = np.random.permutation(data_size)
            for i in range(0, data_size, self.batch_size):
                x_batch = self.x_train[indexes[i:i + self.batch_size]]
                y_batch = self.y_train[indexes[i:i + self.batch_size]]
                x = chainer.Variable(x_batch)
                t = chainer.Variable(y_batch)
                optimizer.update(self.model, x, t)
                print("loss: {0}".format(self.model.loss.data))

            serializers.save_npz(MODEL_PATH, self.model)
            optimizer.lr *= 0.97
def get_args():
    parser = argparse.ArgumentParser(
        description='Evaluate a Caffe reference model on ILSVRC2012 dataset')
    parser.add_argument('model_type', choices=('alexnet', 'caffenet', 'googlenet', 'nin', 'vgg'),
                        help='Model type (alexnet, caffenet, googlenet, nin, vgg)')
    parser.add_argument('model', help='Path to the pretrained Caffe model')
    parser.add_argument('--src', '-s', default='./src',
                        help='source path for images')
    parser.add_argument('--dst', '-d', default='./dst',
                        help='distination path for features')
    parser.add_argument('--mean', '-m', default='~/caffemodels/ilsvrc_2012_mean.npy',
                        help='Path to the mean file')
    parser.add_argument('--batchsize', '-B', type=int, default=1024,
                        help='Minibatch size')
    parser.add_argument('--gpu', '-g', type=int, default=-1,
                        help='Zero-origin GPU ID (nevative value indicates CPU)')    

    args = parser.parse_args()
    assert(args.batchsize > 0)        

    xp = numpy
    if args.gpu >= 0:
        cuda.check_cuda_available()
        xp = cuda.cupy

    def slash(d):
        return d if d[-1] is '/' else d + '/'

    args.src = slash(args.src)
    args.dst = slash(args.dst)
    return args, xp
Example #9
0
def main(description, gpu, output):
	logging.basicConfig(level=logging.INFO)

	logging.info('fetch MNIST dataset')
	mnist = fetch_mldata(description)
	mnist.data = mnist.data.astype(numpy.float32)
	mnist.data /= 255
	mnist.target = mnist.target.astype(numpy.int32)

	data_train, data_test, target_train, target_test = train_test_split(mnist.data, mnist.target)

	data = data_train, data_test
	target = target_train, target_test

	start_time = time.time()

	if gpu >= 0:
		cuda.check_cuda_available()
		cuda.get_device(gpu).use()
		logging.info("Using gpu device {}".format(gpu))
	else:
		logging.info("Not using gpu device")

	mlp = MLP(data=data, target=target, gpu=gpu)
	mlp.train_and_test(n_epoch=1)

	end_time = time.time()

	logging.info("time = {} min".format((end_time - start_time) / 60.0))
	logging.info('saving trained mlp into {}'.format(output))
	with open(output, 'wb') as fp:
		pickle.dump(mlp, fp)
Example #10
0
def using_gpu():
    try:
        cuda.check_cuda_available()
        # xp = cuda.cupy
        # cuda.get_device(0).use()
        return True
    except:
        return False
 def setup_gpu(self):
     if self.gpu >= 0:
         cuda.check_cuda_available()
         cuda.get_device(self.gpu).use()
         self.model.to_gpu()
         self.mod = cuda.cupy
     else:
         self.mod = np
Example #12
0
 def __init__(self, meanpath, model, gpu=-1):
    self.mean = loader.load_mean(meanpath)
    self.model = model
    self.gpu = gpu
    self.insize = model.insize
    if gpu >= 0:
       cuda.check_cuda_available()
       cuda.get_device(gpu).use()
       self.model.to_gpu()
 def __init__(self):
     cuda.check_cuda_available()
     if not memory_hook_available:
         msg = 'CuPy >= 2.0 is required. %s' % str(_resolution_error)
         raise RuntimeError(msg)
     self.call_history = []
     self._memory_hook = CupyMemoryCumulativeHook()
     self._running_stack = []
     self._total_used_bytes = 0
     self._total_acquired_bytes = 0
Example #14
0
def train_nnet(model, optimizer, train_data_resource, opts):
    if opts.gpu >= 0:
        cuda.check_cuda_available()
        model.to_gpu(opts.gpu)
    accum_loss = 0

    i = 0
    train_loss = 0
    prev_dev_loss = 100000
    prev_percentage = 0
    dump_graph = True
    for train_idx, x_batch, y_batch, epoch, percentage, eos in train_data_resource:
        if train_idx is None:  # Done one epoch
            if opts.fname_dev:
                dev_loss, _, _ = evaluation(model, opts.fname_dev)
                if xp == cuda.cupy:
                    model.to_gpu()
                print(' dev loss: %.3f' % dev_loss, end='')
                if optimizer.lr < opts.lr_stop:
                    break
                if prev_dev_loss - dev_loss < opts.start_decay:
                    optimizer.lr *= opts.lr_decay
                    print('\n...reducing lr to %.6f' % optimizer.lr)
                prev_dev_loss = dev_loss
            print('')
            continue
        x = Variable(xp.asarray(x_batch))
        t = Variable(xp.asarray(y_batch))
        loss_i = model(x, t)
        accum_loss += loss_i
        if dump_graph:
            print('Dump graph')
            with open('graph.dot', 'w') as o:
                o.write(c.build_computational_graph((loss_i, )).dump())
            dump_graph = False
        if train_idx >= 1:
            train_loss = (train_loss * (train_idx - 1) + loss_i.data) / train_idx

        if eos and opts.forget_on_new_utt:
            model.predictor.forget_history()

        if eos or (i + 1) % opts.bprop_len == 0:
            model.zerograds()
            accum_loss.backward()
            accum_loss.unchain_backward()
            accum_loss = 0
            optimizer.update()
            i = 0

        if percentage != prev_percentage:
            prev_percentage = percentage
            print_stats(percentage, epoch, optimizer.lr, train_loss)
            sys.stdout.flush()
        i += 1
Example #15
0
def main():
    args = parse_args()

    global xp
    xp = cuda.cupy if args.gpu >= 0 else numpy

    if args.gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(args.gpu).use()

    train_model(args)
def test(args):
    """ 予測を行うメソッド
    """

    batchsize   = args.batchsize  # バッチサイズ

    # 語彙辞書の読込
    src_vocab2id, src_id2vocab, vocab_size = util.load_vocab(args.model + ".srcvocab")

    # モデルの読込
    model = NLM.load_spec(args.model + ".spec")

    # GPUを使うかどうか
    if args.use_gpu:
        cuda.check_cuda_available()
        cuda.get_device(1).use()
        model.to_gpu()

    xp = cuda.cupy if args.use_gpu else np # args.gpu <= 0: use cpu, otherwise: use gpu
    serializers.load_hdf5(args.model + ".weights", model)

    # Source sequence for test
    print 'loading source data for test...'
    # データセット読み込み
    test_src_dataset = util.load_test_src_data(args.src, src_vocab2id)

    generated = 0
    N = len(test_src_dataset) # テストの事例数

    word_list = src_vocab2id.keys()

    # 単語wordのembeddingを取得
    word_id_list = Variable(xp.asarray([src_vocab2id[word] for word in word_list ], dtype=xp.int32))
    embedding_list = model.get_embedding(word_id_list)
    src_embed = embedding_list.data[word_list.index(args.src_word)]
    #print model.embed.W.data.shape

    print "src word:", args.src_word
    print src_embed
    #src_embed = model.embed.W.data[src_vocab2id[args.src_word]]

    trg_embed_list = {}
    for _word, _id in src_vocab2id.items():
        trg_embed = embedding_list.data[word_list.index(_word)]
        #trg_embed = model.embed.W.data[src_vocab2id[_word]]
        trg_embed_list[_word] = 1 - scipy.spatial.distance.cosine(src_embed, trg_embed)

    # 上位10件を表示
    for i, (word, sim) in enumerate(sorted(trg_embed_list.items(), key=lambda x:x[1], reverse=True)):
        print word, sim

        if i == 10:
            break
Example #17
0
    def fit(self, x_train, y_train, x_test=[], y_test=[], is_classification=False):
        xp = np
        if self.gpu_flag >= 0:
            cuda.check_cuda_available()
            cuda.get_device(self.gpu_flag).use()
            xp = cuda.cupy
            self.model.to_gpu()

        if self.log_path != "" and not os.path.isfile(self.log_path):
            print "log_path: "+self.log_path
            utility.writeText(self.log_path, "a",
                "datetime,epoch,train_loss,test_loss,test_accuracy\n")

        for epoch in xrange(self.epoch):
            train_size = len(x_train)
            train_loss = 0.
            test_loss = 0.
            test_accuracy = 0.
            indexes = np.random.permutation(train_size)
            for i in xrange(0, train_size, self.batch_size):
                x = Variable(xp.asarray(x_train[indexes[i:i+self.batch_size]]))
                t = Variable(xp.asarray(y_train[indexes[i:i+self.batch_size]]))

                self.model.zerograds()
                loss = self.loss_function(self.forward(x), t)
                loss.backward()
                self.optimizer.update()
                train_loss += loss.data * len(t.data)
            train_loss /= train_size

            test_size = len(x_test)
            for i in xrange(0, test_size, self.batch_size):
                x = Variable(xp.asarray(x_test[i:i+self.batch_size]))
                t = Variable(xp.asarray(y_test[i:i+self.batch_size]))
                test_loss += float(self.loss_function(self.forward(x), t).data) * len(t.data)
                if is_classification:
                    test_accuracy += float(F.accuracy(self.forward(x), t).data) * len(t.data)
            if test_size != 0:
                test_loss /= test_size
                test_accuracy /= test_size

            if epoch > 1 and self.log_path != "":
                utility.writeText(self.log_path,"a",
                    "%s,%d,%f,%f,%f\n"% (utility.now(), epoch,
                        train_loss, test_loss, test_accuracy))

        if self.gpu_flag >= 0:
            self.model.to_cpu()
        if self.export_path != "":
            pickle.dump(self.model, open(self.export_path, 'wb'), -1)
        return self
Example #18
0
def do_train(db_path, train, test, mean, root_output_dir, model_dir, model_id, batchsize=32, val_batchsize=250, epoch=10, gpu=-1, loaderjob=20):
    conn = sqlite3.connect(db_path)
    db = conn.cursor()
    cursor = db.execute('select name from Model where id = ?', (model_id,))
    row = cursor.fetchone();
    model_name = row[0]
    # start initialization
    if gpu >= 0:
        cuda.check_cuda_available()
    xp = cuda.cupy if gpu >= 0 else np
    
    train_list = load_image_list(train)
    val_list = load_image_list(test)
    mean_image = pickle.load(open(mean, 'rb'))
    
    # @see http://qiita.com/progrommer/items/abd2276f314792c359da
    model_name = re.sub(r"\.py$", "", model_name)
    model_module = load_module(model_dir, model_name)
    model = model_module.Network()
    
    if gpu >= 0:
        cuda.get_device(gpu).use()
        model.to_gpu()
        
    optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
    optimizer.setup(model)
    
    data_q = queue.Queue(maxsize=1)
    res_q = queue.Queue()

    # create directory for saving trained models
    output_dir = root_output_dir + os.sep + model_name
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
        
    db.execute('update Model set epoch = ?, trained_model_path = ?, graph_data_path = ?, is_trained = 1, line_graph_data_path = ? where id = ?', (epoch, output_dir, output_dir + os.sep + 'graph.dot', output_dir + os.sep + 'line_graph.tsv', model_id))
    conn.commit()
    
    # Invoke threads
    feeder = threading.Thread(target=feed_data, args=(train_list, val_list, mean_image, batchsize, val_batchsize, model, loaderjob, epoch, optimizer, data_q))
    feeder.daemon = True
    feeder.start()
    logger = threading.Thread(target=log_result, args=(batchsize, val_batchsize, output_dir + os.sep + 'line_graph.tsv', res_q))
    logger.daemon = True
    logger.start()
    train_loop(model, output_dir, xp, optimizer, res_q, data_q)
    feeder.join()
    logger.join()
    db.execute('update Model set is_trained = 2 where id = ?', (model_id,))
    conn.commit()
    db.close()
Example #19
0
def getComputeMode():
    """
    GPUが使用可能か判定する
    :return: True:GPU使用可能, False:使用不可能(CPU処理)
    """
    using_gpu = False
    xp = np
    try:
       cuda.check_cuda_available()
       xp = cuda.cupy
       cuda.get_device(0).use()
       using_gpu = True
    except:
       print  "I'm sorry. Using CPU."
    return (using_gpu, xp)
Example #20
0
def setup_model(ninput, noutput, nlayer=1, layer_size=128, use_gpu=None,
        dropout=None, w2vec_model=None, seed=None):
    if seed is not None:
        np.random.seed(seed)

    model = chainer.FunctionSet()
    if w2vec_model:
        embed = w2vec_model
        if use_gpu is not None and (opts.fname_test is None):
            embed.use_gpu = use_gpu
        setattr(model, 'embed', embed)
        if layer_size != embed.embed_size:
            print('...forced hidden layer size to word2vec vector size', embed.embed_size)
            layer_size = embed.embed_size
    else:
        setattr(model, 'embed',  F.EmbedID(ninput, layer_size))
    

    init_W = np.random.normal(
                0, 1 * math.sqrt(1. / layer_size),
                (4 * layer_size, layer_size)).astype(np.float32)
    init_B = np.repeat(np.float32(0), layer_size * 4)

    for lid in xrange(1, nlayer + 1):
        x = F.Linear(layer_size, 4 * layer_size, initialW=init_W, initial_bias=init_B)
        h = F.Linear(layer_size, 4 * layer_size, initialW=init_W, initial_bias=init_B)
        setattr(model, 'l' + str(lid) + '_x',  x)
        setattr(model, 'l' + str(lid) + '_h',  h)

    init_W = np.random.normal(
                0, 1 * math.sqrt(1. / layer_size),
                (noutput , layer_size)).astype(np.float32)

    setattr(model, 'l' + str(lid + 1), F.Linear(layer_size, noutput))
    setattr(model, 'ninput', ninput)
    setattr(model, 'noutput', noutput)
    setattr(model, 'layer', nlayer)
    setattr(model, 'layer_size', layer_size)
    setattr(model, 'dropout', dropout)
    
    for param in model.parameters:
        param[:] = np.random.uniform(-0.1, 0.1, param.shape)

    if use_gpu is not None and (opts.fname_test is None):
        cuda.check_cuda_available()
        cuda.get_device(opts.gpu).use()
        model.to_gpu()
    return model
 def __call_miniatch_train(self, encoderDecoderModel, epoch):
     """
     Call minibatch train
     :param encoderDecoderModel:
     :param epoch:
     """
     if self.parameter_dict["use_gpu"]:
         cuda.check_cuda_available()
     xp = cuda.cupy if self.parameter_dict["gpu_id"] >= 0 and self.parameter_dict["use_gpu"] == True else np
     x_data = xp.asarray(self.x_batch)
     y_data = xp.asarray(self.y_batch)
     x = chainer.Variable(x_data, volatile=True)
     t = chainer.Variable(y_data, volatile=True)
     encoderDecoderModel.id2image = x
     encoderDecoderModel.first_word = t
     encoderDecoderModel.train(epoch)
Example #22
0
def inspect(image_path, mean, model_path, label, network_path, resize_mode,channels, gpu=0):
    network = network_path.split(os.sep)[-1]
    model_name = re.sub(r"\.py$", "", network)
    model_module = load_module(os.path.dirname(network_path), model_name)
    mean_image = pickle.load(open(mean, 'rb'))
    model = model_module.Network()
    serializers.load_hdf5(model_path, model)
    if gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(gpu).use()
        model.to_gpu()
    
    # for backward compatibility
    if mean_image.shape[1] != model.insize:
        output_side_length = mean_image.shape[1]
    else:
        output_side_length = model.insize
        
    img = read_image(image_path, 256, 256, resize_mode,channels)
    
    cropwidth = 256 - output_side_length
    top = left = cropwidth / 2
    bottom = output_side_length + top
    right = output_side_length + left
    img = img[:, top:bottom, left:right]
    
    if img.ndim == 3:
        img = img.transpose(2, 0, 1)
    img = img.astype(np.float32)
    img -= mean_image
    img /= 255
    
    x = np.ndarray((1, 3,  output_side_length, output_side_length), dtype=np.float32)
    x[0] = img
    
    if gpu >= 0:
        x = cuda.to_gpu(x)
    score = model.predict(x)
    score = cuda.to_cpu(score.data)
    categories = np.loadtxt(label, str, delimiter="\t")
    top_k = 20
    prediction = zip(score[0].tolist(), categories)
    prediction.sort(cmp=lambda x, y:cmp(x[0], y[0]), reverse=True)
    ret = []
    for rank, (score, name) in enumerate(prediction[:top_k], start=1):
        ret.append({"rank": rank, "name": name, "score": "{0:4.1f}%".format(score*100)})
    return ret
def inspect_by_chainer(image_path, mean, model_path, label,
                       network_path, resize_mode, channels, gpu=-1):
    model_module = load_module(network_path)
    mean_image = pickle.load(open(mean, 'rb'))
    model = model_module.Network()
    serializers.load_hdf5(model_path, model)
    if gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(gpu).use()
        model.to_gpu()

    img = read_image(image_path, 256, 256, resize_mode, channels)
    cropwidth = 256 - model.insize
    top = left = cropwidth / 2
    bottom = model.insize + top
    right = model.insize + left

    if img.ndim == 3:
        img = img.transpose(2, 0, 1)
        img = img[:, top:bottom, left:right].astype(np.float32)
    else:
        img = img[top:bottom, left:right].astype(np.float32)
        zeros = np.zeros((model.insize, model.insize))
        img = np.array([img, zeros, zeros])
    img -= mean_image[:, top:bottom, left:right]
    img /= 255

    x = np.ndarray((1, 3,  model.insize, model.insize), dtype=np.float32)
    x[0] = img

    if gpu >= 0:
        x = cuda.to_gpu(x)
    score = model.predict(x)
    score = cuda.to_cpu(score.data)
    categories = np.loadtxt(label, str, delimiter="\t")
    top_k = 20
    prediction = zip(score[0].tolist(), categories)
    prediction.sort(cmp=lambda x, y: cmp(x[0], y[0]), reverse=True)
    ret = []
    for rank, (score, name) in enumerate(prediction[:top_k], start=1):
        ret.append({"rank": rank, "name": name, "score": "{0:4.1f}%".format(score*100)})
    return ret
Example #24
0
def main(description, gpu, output):
	logging.basicConfig(level=logging.INFO)

	if gpu >= 0:
		cuda.check_cuda_available()
		cuda.get_device(gpu).use()


	logging.info('fetch MNIST dataset')
	mnist = fetch_mldata(description)
	mnist.data = mnist.data.astype(numpy.float32)
	mnist.data /= 255
	mnist.data = mnist.data.reshape(70000, 1, 28, 28)
	mnist.target = mnist.target.astype(numpy.int32)

	data_train, data_test, target_train, target_test = train_test_split(mnist.data, mnist.target)

	data = data_train, data_test
	target = target_train, target_test

	n_outputs = 10
	in_channels = 1

	start_time = time.time()

	cnn = CNN(
		data=data,
		target=target,
		gpu=gpu,
		in_channels=in_channels,
		n_outputs=n_outputs,
		n_hidden=100
	)

	cnn.train_and_test(n_epoch=10)

	end_time = time.time()

	logging.info("time = {} min".format((end_time - start_time) / 60.0))
	logging.info('saving trained cnn into {}'.format(output))
	with open(output, 'wb') as fp:
		pickle.dump(cnn, fp)
Example #25
0
    def __init__(self, gpu=-1):
        self.n_layer = 4

        # units
        number_of_units = [1080, 360, 120, 40, 12]

        # layers
        self.enc_layer = [
            F.Linear(number_of_units[0], number_of_units[1]),
            F.Linear(number_of_units[1], number_of_units[2]),
            F.Linear(number_of_units[2], number_of_units[3]),
            F.Linear(number_of_units[3], number_of_units[4])
        ]

        self.dec_layer = [
            F.Linear(number_of_units[4], number_of_units[3]),
            F.Linear(number_of_units[3], number_of_units[2]),
            F.Linear(number_of_units[2], number_of_units[1]),
            F.Linear(number_of_units[1], number_of_units[0])
        ]

        self.model = chainer.FunctionSet(
            enc1=self.enc_layer[0],
            enc2=self.enc_layer[1],
            enc3=self.enc_layer[2],
            enc4=self.enc_layer[3],
            dec1=self.dec_layer[0],
            dec2=self.dec_layer[1],
            dec3=self.dec_layer[2],
            dec4=self.dec_layer[3],
        )

        try:
            param = np.load('dae.param.npy.1')
            self.model.copy_parameters_from(param)
        except:
            pass

        if gpu >= 0:
            cuda.check_cuda_available()
            cuda.get_device(gpu).use()
            self.model.to_gpu()
Example #26
0
def set_mode(gpu):
    """ Set the global xp variable based on the GPU settings and the CUDA availability.

    """
    global xp
    gpu_mode = False
    print('aaaa')
    if gpu >= 0:
        # Set to GPU, CuPy
        cuda.check_cuda_available()
        cuda.get_device(gpu).use()
        xp = cuda.cupy
        gpu_mode = True
    else:
        # Set to CPU, numpy
        xp = np
        gpu_mode = False

    print('xp: {}'.format(xp))
    return gpu_mode
Example #27
0
def train(epoch=10, batch_size=32, gpu=False):
    if gpu:
        cuda.check_cuda_available()
    xp = cuda.cupy if gpu else np

    td = TrainingData(LABEL_FILE, img_root=IMAGES_ROOT, image_property=IMAGE_PROP)

    # make mean image
    if not os.path.isfile(MEAN_IMAGE_FILE):
        print("make mean image...")
        td.make_mean_image(MEAN_IMAGE_FILE)
    else:
        td.mean_image_file = MEAN_IMAGE_FILE

    # train model
    label_def = LabelingMachine.read_label_def(LABEL_DEF_FILE)
    model = alex.Alex(len(label_def))
    optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
    optimizer.setup(model)
    epoch = epoch
    batch_size = batch_size

    print("Now our model is {0} classification task.".format(len(label_def)))
    print("begin training the model. epoch:{0} batch size:{1}.".format(epoch, batch_size))

    if gpu:
        model.to_gpu()

    for i in range(epoch):
        print("epoch {0}/{1}: (learning rate={2})".format(i + 1, epoch, optimizer.lr))
        td.shuffle(overwrite=True)

        for x_batch, y_batch in td.generate_batches(batch_size):
            x = chainer.Variable(xp.asarray(x_batch))
            t = chainer.Variable(xp.asarray(y_batch))

            optimizer.update(model, x, t)
            print("loss: {0}, accuracy: {1}".format(float(model.loss.data), float(model.accuracy.data)))

        serializers.save_npz(MODEL_FILE, model)
        optimizer.lr *= 0.97
Example #28
0
    def __init__(self):

        parser = argparse.ArgumentParser(description='PredNet')

        parser.add_argument('train', help='Input movie filename')
        parser.add_argument('--gpu', '-g', default=0, type=int,
                            help='GPU ID (negative value indicates CPU)')
        parser.add_argument('--root', '-r', default='.',
                            help='Root directory path of image files')
        parser.add_argument('--filename', '-f', default='.',
                            help='Path of input movie file')
        parser.add_argument('--initmodel', default='',
                            help='Initialize the model from given file')
        parser.add_argument('--resume', default='',
                            help='Resume the optimization from snapshot')
        parser.add_argument('--test', dest='test', action='store_true')
        parser.set_defaults(test=False)
        
        self.args = parser.parse_args()

        if self.args.gpu >= 0:
            cuda.check_cuda_available()
        self.xp = cuda.cupy if self.args.gpu >= 0 else np

        # Create Model
        # self.in_width = 64
        # self.in_height = 64
        # self.in_channel = 3

        if self.args.gpu >= 0:
            cuda.get_device(self.args.gpu).use()
            self.model.to_gpu()

        # Init/Resume
        if self.args.initmodel:
            print('Load model from', self.args.initmodel)
            serializers.load_npz(self.args.initmodel, self.model)
        if self.args.resume:
            print('Load self.optimizer state from', self.args.resume)
            serializers.load_npz(self.args.resume, self.optimizer)
Example #29
0
def main(description, gpu, output):
	logging.basicConfig(level=logging.INFO)

	logging.info('fetch MNIST dataset')
	mnist = fetch_mldata(description)
	mnist.data   = mnist.data.astype(numpy.float32)
	mnist.data  /= 255
	mnist.target = mnist.target.astype(numpy.int32)

	data_train, data_test, target_train, target_test = train_test_split(mnist.data, mnist.target)

	data = data_train, data_test
	target = target_train, target_test

	rng = numpy.random.RandomState(1)

	if gpu >= 0:
		cuda.check_cuda_available()
		cuda.get_device(gpu).use()

	start_time = time.time()

	sda = SDA(
		rng=rng,
		data=data,
		target=target,
		gpu=gpu
	)
	# sda.pre_train(n_epoch=15)
	sda.fine_tune(n_epoch=5)

	end_time = time.time()

	logging.info("time = {} min".format((end_time - start_time) / 60.0))
	logging.info('saving trained sda into {}'.format(output))
	with open(output, 'wb') as fp:
		pickle.dump(sda, fp)
Example #30
0
def main(description, gpu, output):
	logging.basicConfig(level=logging.INFO)

	logging.info('fetch MNIST dataset')
	mnist = fetch_mldata(description)
	mnist.data = mnist.data.astype(numpy.float32)
	mnist.data /= 255
	mnist.target = mnist.target.astype(numpy.int32)

	data_train, data_test, target_train, target_test = train_test_split(mnist.data, mnist.target)

	data = data_train, data_test
	target = target_train, target_test

	if gpu >= 0:
		cuda.check_cuda_available()
		cuda.get_device(gpu).use()

	# draw_digits(mnist.data[0:9])
	rng = numpy.random.RandomState(1)
	start_time = time.time()

	da = DA(rng=rng, data=data, gpu=gpu)

	perm = numpy.random.permutation(len(data[0]))
	data = mnist.data[perm[0:9]]

	da.train_and_test(n_epoch=5)

	end_time = time.time()

	logging.info("time = {} min".format((end_time - start_time) / 60.0))

	logging.info("saving trained da into {}".format(output))
	with open(output, 'wb') as fp:
		pickle.dump(da, fp)
Example #31
0
def main():
    args = get_args()

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()

    print('GPU: {}'.format(args.gpu))
    print('# unit: {}'.format(args.unit))
    print('Window: {}'.format(args.window))
    print('Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('Training model: {}'.format(args.model))
    print('Output type: {}'.format(args.out_type))
    print('')

    train, val, _ = chainer.datasets.get_ptb_words()
    train: np.ndarray = train
    val: np.ndarray = val
    counts = collections.Counter(train)
    counts.update(collections.Counter(val))
    n_vocab: int = max(train) + 1

    assert len(train.shape) == 1
    assert len(val.shape) == 1

    if args.test:
        train: np.ndarray = train[:100]
        val: np.ndarray = val[:100]

    vocab: Dict[str, int] = chainer.datasets.get_ptb_words_vocabulary()
    index2word: Dict[int, str] = {wid: word for word, wid in vocab.items()}

    print('n_vocab: %d' % n_vocab)
    print('data length: %d' % len(train))

    if args.out_type == 'hsm':
        HSM = L.BinaryHierarchicalSoftmax
        tree = HSM.create_huffman_tree(counts)
        loss_func = HSM(args.unit, tree)
        loss_func.W.data[...] = 0
    elif args.out_type == 'ns':
        cs = [counts[w] for w in range(len(counts))]
        loss_func = L.NegativeSampling(args.unit, cs, args.negative_size)
        loss_func.W.data[...] = 0
    elif args.out_type == 'original':
        loss_func = SoftmaxCrossEntropyLoss(args.unit, n_vocab)
    else:
        raise Exception('Unknown output type: {}'.format(args.out_type))

    if args.model == 'skipgram':
        model = SkipGram(n_vocab, args.unit, loss_func)
    elif args.model == 'cbow':
        model = ContinuousBoW(n_vocab, args.unit, loss_func)
    else:
        raise Exception('Unknown model type: {}'.format(args.model))

    if args.gpu >= 0:
        model.to_gpu()

    optimizer = O.Adam()
    optimizer.setup(model)

    train_iter = WindowIterator(train, args.window, args.batchsize)
    val_iter = WindowIterator(val, args.window, args.batchsize, repeat=False)
    updater = training.StandardUpdater(train_iter,
                                       optimizer,
                                       converter=convert,
                                       device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    trainer.extend(
        extensions.Evaluator(val_iter,
                             model,
                             converter=convert,
                             device=args.gpu))
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss']))
    trainer.extend(extensions.ProgressBar())
    trainer.run()

    with open('word2vec.model', 'w') as f:
        f.write('%d %d\n' % (len(index2word), args.unit))
        w = cuda.to_cpu(model.embed.W.data)
        for i, wi in enumerate(w):
            v = ' '.join(map(str, wi))
            f.write('%s %s\n' % (index2word[i], v))
Example #32
0
def main():
    # parse command line args
    parser = argparse.ArgumentParser()
    parser.add_argument('--configfile', '-c', default="", type=str, help='')
    args = parser.parse_args()

    with open(args.configfile, "r+") as f:
        config = yaml.load(f)

    # GPU settings
    if config["GPU"] >= 0:
        cuda.check_cuda_available()
        cuda.get_device(config["GPU"]).use()
    xp = cuda.cupy if config["GPU"] >= 0 else np

    initial_embedding = ""
    if "init_emb" in config and config["init_emb"] != "None":
        with open(config["init_emb"], "rb") as f:
            initial_embedding = pickle.load(f)
    else:
        initial_embedding = None
    ######################
    #### create model ####
    ######################
    model, corpus = load_model(config, initial_embedding)

    wordvector_model = load_wordvector(config)

    if config["GPU"] >= 0:
        model.to_gpu()
    optimizer = optimizers.Adam(alpha=config["training_rate"])
    optimizer.setup(model)

    if "fix_embedding" in config and config["fix_embedding"]:
        model.enc.word_embed.disable_update()
    optimizer.add_hook(chainer.optimizer.GradientClipping(5))

    if config["NN_model"] in ["RNN", "GRU"]:
        corpus.train_data[0] = [
            xp.array(x, dtype=xp.int32) for x in corpus.train_data[0]
        ]
        corpus.train_data = list(
            zip(corpus.train_data[0], corpus.train_data[1],
                corpus.train_data[2], corpus.train_data[3]))
        if hasattr(corpus, "dev_data"):
            corpus.dev_data[0] = [
                xp.array(x, dtype=xp.int32) for x in corpus.dev_data[0]
            ]
            corpus.dev_data = list(
                zip(corpus.dev_data[0], corpus.dev_data[1], corpus.dev_data[2],
                    corpus.dev_data[3]))
    elif config["NN_model"] in ["CNN", "SUM", "SUMFF"]:
        corpus.train_data[0] = [
            xp.array([
                x[i] if i < len(x) else -1 for i in range(corpus.max_input_len)
            ],
                     dtype=xp.int32) for x in corpus.train_data[0]
        ]
        corpus.train_data = list(
            zip(corpus.train_data[0], corpus.train_data[1],
                corpus.train_data[2], corpus.train_data[3]))
        if hasattr(corpus, "dev_data"):
            corpus.dev_data[0] = [
                xp.array([
                    x[i] if i < len(x) else -1
                    for i in range(corpus.max_input_len)
                ],
                         dtype=xp.int32) for x in corpus.dev_data[0]
            ]
            corpus.dev_data = list(
                zip(corpus.dev_data[0], corpus.dev_data[1], corpus.dev_data[2],
                    corpus.dev_data[3]))
    else:
        print("model is not defined")
        exit()

    #############################
    #### train mimic model ####
    #############################

    if "overfit" in config and config["overfit"]:
        train_loss_data = []
        minimum_train_loss = 9999999
        minimum_epoch = 0
        minimum_train_loss_flag = 0
        for num, epoch in enumerate(range(999999)):
            total_loss = 0
            batch_num = 0
            random.shuffle(corpus.train_data)

            # for training
            for i in range(0, len(corpus.train_data), config["batchsize"]):

                # select batch data
                batch = corpus.train_data[i:i + config["batchsize"]]
                batch = list(zip(*batch))
                loss = calc_batch_loss(batch, config, model, wordvector_model)

                # learn model
                model.cleargrads()  # initialize all grad to zero
                loss.backward()  # back propagation
                optimizer.update()
                total_loss += float(loss.data)
                batch_num += 1
                # print('Epoch: ', num, 'Batch_num', batch_num, 'batch loss: {:.2f}'.format(float(loss.data)))

            # save model and optimizer
            if total_loss / batch_num < minimum_train_loss:
                print('-----', epoch + 1, ' times -----')
                print('save the model and optimizer for train loss')
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best_train_loss.model',
                    model)
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best_train_loss.state',
                    optimizer)
                minimum_train_loss = total_loss / batch_num
                minimum_epoch = epoch
                minimum_train_loss_flag = 0
            else:
                minimum_train_loss_flag += 1
                if minimum_train_loss_flag > 4:
                    break
            if epoch == 39:
                print('save the model and optimizer')
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best.model', model)
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best.state', optimizer)

            # display the on-going status
            print('Epoch: ', num, 'Train sim loss: {:.2f}'.format(total_loss))
            train_loss_data.append(float(total_loss / batch_num))

        # save loss data
        with open('./data/train_loss_' + config["modelname"] + '.pkl',
                  'wb') as f:
            pickle.dump(train_loss_data, f)
        print(minimum_epoch)

    else:
        train_loss_data = []
        dev_loss_data = []
        minimum_loss = 9999999
        minimum_train_loss = 9999999
        for num, epoch in enumerate(range(config["epoch"])):
            total_loss = dev_loss = 0
            batch_num = 0
            random.shuffle(corpus.train_data)

            # for training
            for i in range(0, len(corpus.train_data), config["batchsize"]):

                # select batch data
                batch = corpus.train_data[i:i + config["batchsize"]]
                batch = list(zip(*batch))
                loss = calc_batch_loss(batch, config, model, wordvector_model)

                # learn model
                model.cleargrads()  # initialize all grad to zero
                loss.backward()  # back propagation
                optimizer.update()
                total_loss += float(loss.data)
                batch_num += 1
                print('Epoch: ', num, 'Batch_num', batch_num,
                      'batch loss: {:.2f}'.format(float(loss.data)))

            # for developing
            for i in range(0, config["devsize"], config["batchsize"]):

                # select dev batch data
                batch = corpus.dev_data[i:i + config["batchsize"]]
                batch = list(zip(*batch))
                loss = calc_batch_loss(batch, config, model, wordvector_model)

                dev_loss += loss
            # save model and optimizer
            if dev_loss.data < minimum_loss:
                print('-----', epoch + 1, ' times -----')
                print('save the model and optimizer')
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best.model', model)
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best.state', optimizer)
                minimum_loss = dev_loss.data

            # save model and optimizer
            if total_loss / batch_num < minimum_train_loss:
                print('-----', epoch + 1, ' times -----')
                print('save the model and optimizer for train loss')
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best_train_loss.model',
                    model)
                serializers.save_hdf5(
                    'data/' + config["modelname"] + '_best_train_loss.state',
                    optimizer)
                minimum_train_loss = total_loss / batch_num

            # display the on-going status
            print('Epoch: ', num, 'Train sim loss: {:.2f}'.format(total_loss),
                  'dev sim loss: {:.2f}'.format(float(dev_loss.data)))
            train_loss_data.append(float(total_loss / batch_num))
            dev_loss_data.append(float(dev_loss.data))

        # save loss data
        with open('./data/train_loss_' + config["modelname"] + '.pkl',
                  'wb') as f:
            pickle.dump(train_loss_data, f)
        with open('./data/dev_loss_' + config["modelname"] + '.pkl',
                  'wb') as f:
            pickle.dump(dev_loss_data, f)

        # evaluate with origin vector
        from model import Interpreter
        interpreter = Interpreter(config)

        mse_total = 0
        cos_sim_total = 0
        total = 0
        for word in corpus.test_data:
            v_o = wordvector_model[word]
            v_m = interpreter(word)
            mse_total += mse(v_o, v_m)
            cos_sim_total += cos_sim(v_o, v_m)
            total += 1

        print(mse_total / total / config["feature_num"])
        print(cos_sim_total / total)
Example #33
0
 def __init__(self):
     cuda.check_cuda_available()
     if not cuda.cupy.cuda.nvtx_enabled:
         raise RuntimeError('nvtx is required for CUDAProfileHook')
Example #34
0
def resume_train_by_chainer(
        db_model,
        root_output_dir,
        val_batchsize=250,
        loaderjob=20,
        interruptable=None,
):
    logger.info('resume last imagenet train. model_id: {0} gpu: {1}'
                .format(db_model.id, db_model.gpu))
    # start initialization
    if db_model.gpu >= 0:
        cuda.check_cuda_available()
    xp = cuda.cupy if db_model.gpu >= 0 else np

    train_list = load_image_list(os.path.join(db_model.prepared_file_path, 'train.txt'))
    val_list = load_image_list(os.path.join(db_model.prepared_file_path, 'test.txt'))
    mean_image = pickle.load(open(os.path.join(db_model.prepared_file_path, 'mean.npy'), 'rb'))

    # @see http://qiita.com/progrommer/items/abd2276f314792c359da
    (model_dir, model_name) = os.path.split(db_model.network_path)
    model_name = re.sub(r"\.py$", "", model_name)
    model_module = load_module(model_dir, model_name)
    model = model_module.Network()

    if db_model.gpu >= 0:
        cuda.get_device(db_model.gpu).use()
        model.to_gpu()

    optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
    optimizer.setup(model)

    resume_path = os.path.join(db_model.trained_model_path, 'resume')
    resume_json = os.path.join(resume_path, 'resume.json')

    resume_epoch = TrainingEpoch.deserialize(resume_json)

    # load resume data.
    resume_state = os.path.join(resume_path, 'resume.state')
    resume_model = os.path.join(resume_path, 'resume.model')
    logger.info("Load optimizer state from : {}"
                .format(resume_state))
    serializers.load_npz(resume_model, model)
    serializers.load_npz(resume_state, optimizer)
    remove_resume_file(db_model.trained_model_path)

    data_q = queue.Queue(maxsize=1)
    res_q = queue.Queue()

    db_model.is_trained = 1
    db_model.update_and_commit()

    # Invoke threads
    feeder = threading.Thread(
        target=feed_data,
        args=(
            train_list,
            val_list,
            mean_image,
            db_model.batchsize,
            val_batchsize,
            model,
            loaderjob,
            db_model.epoch,
            optimizer,
            data_q,
            resume_epoch.avoid_flipping,
            resume_epoch.permutation,
            resume_epoch.epoch
        )
    )
    feeder.daemon = True
    feeder.start()
    train_logger = threading.Thread(
        target=log_result,
        args=(
            db_model.batchsize,
            val_batchsize,
            os.path.join(db_model.trained_model_path, 'line_graph.tsv'),
            os.path.join(db_model.trained_model_path, 'train.log'),
            res_q,
            True
        )
    )
    train_logger.daemon = True
    train_logger.start()
    train_loop(
        model,
        db_model.trained_model_path,
        xp,
        optimizer,
        res_q,
        data_q,
        resume_epoch.pretrained_model,
        interruptable
    )
    feeder.join()
    train_logger.join()

    # post-processing
    _post_process(db_model, resume_epoch.pretrained_model)
    interruptable.clear_interrupt()
    interruptable.terminate()
    logger.info('Finish imagenet train. model_id: {0}'.format(db_model.id))
Example #35
0
def main(args):
    global verbose, encoding
    verbose = args.verbose
    encoding = args.encoding
    assert args.poly_degree >= 1, '--degree must be positive integer'
    poly_degree = args.poly_degree

    gpu = args.gpu
    if gpu >= 0:
        cuda.check_cuda_available()
        if verbose:
            logger.info('Use GPU {}'.format(gpu))
        cuda.get_device_from_id(gpu).use()

    df = read_dataset(args.path_input, args.flag_has_header, args.threshold)

    agg = df.groupby('fact_en')['twa'].mean()
    invalid_facts = set(agg[(agg == 1.0)|(agg == 0.0)].index)
    if verbose:
        logger.info('Invalid facts: {}'.format(len(invalid_facts)))
    df = df[~df['fact_en'].isin(invalid_facts)]
    if verbose:
        logger.info('Remained {} lines'.format(len(df)))

    # Load vocabulary
    if verbose:
        logger.info('Load vocabulary')
    rel2id = Vocabulary()
    rel2id.read_from_file(args.path_rels)
    fact2id = Vocabulary()
    fact2id.read_from_list(np.unique(get_values(df, 'fact')))
    ja2id = Vocabulary()
    ja2id.read_from_list(np.unique(get_values(df, 'fact_ja')))
    en2id = Vocabulary()
    en2id.read_from_list(np.unique(get_values(df, 'fact_en')))

    df.loc[:, 'fact'] = replace_by_dic(df['fact'], fact2id).astype(np.int32)
    df.loc[:, 'fact_ja'] = replace_by_dic(df['fact_ja'], ja2id).astype(np.int32)
    df.loc[:, 'fact_en'] = replace_by_dic(df['fact_en'], en2id).astype(np.int32)
    df.loc[:, 'rel'] = replace_by_dic(df['rel'], rel2id).astype(np.int32)

    label2fact = {i: set(df[df['twa'] == i]['fact'].unique())
                  for i in [0, 1]}
    en2ja = {en: set(df[df['fact_en'] == en]['fact'].unique())
             for en in sorted(df['fact_en'].unique())}
    idx2vec = get_idx2vec(df, poly_degree=poly_degree)
    if gpu >= 0:
        idx2vec = cuda.to_gpu(idx2vec)

    ss = df[df['twa'] == 1].drop_duplicates('fact_en')
    itr = FactIterator(ss, len(ss), ja2id, en2id, train=False, repeat=False,
                       poly_degree=poly_degree)

    # Define a model
    model_type = args.model.lower()
    dim_in = len(COL_BASIC_FEATURES)
    rel_size = len(rel2id)
    if model_type.startswith('linear'):
        ensembler = LinearEnsembler(dim_in, rel_size, use_gpu=(gpu >= 0),
                                    poly_degree=poly_degree,
                                    flag_unifw=args.flag_unifw,
                                    verbose=verbose)
    elif model_type.startswith('mlp'):
        options = args.model.split(':')
        params = {}
        if len(options) > 1:
            params['dim_hid'] = int(options[1])
        if len(options) > 2:
            params['activation'] = options[2]
        ensembler = MLPEnsembler(
            dim_in, rel_size, use_gpu=(gpu >= 0),
            poly_degree=poly_degree, flag_unifw=args.flag_unifw,
            verbose=verbose, **params)
    else:
        raise ValueError('Invalid --model: {}'.format(model_type))

    ensembler.add_persistent('_mu', None)
    ensembler.add_persistent('_sigma', None)
    # load a trained model
    chainer.serializers.load_npz(args.path_model, ensembler)
    if ensembler._mu is not None:
        logger.info('standardize vectors: True')
        itr.standardize_vectors(mu=ensembler._mu, sigma=ensembler._sigma)
        idx2vec = standardize_vectors(idx2vec, ensembler._mu, ensembler._sigma)
    else:
        logger.info('standardize vectors: False')

    model = Classifier(ensembler, label2fact, en2ja, idx2vec)

    # calculate probabilities for testing set
    buff = []
    for i, (rels, _, en_indices) in enumerate(itr, start=1):
        if i % 500 == 0:
            logger.info('Evaluating: {}'.format(i))
        buff.append((model(rels, en_indices), en_indices))
    ranks = list(chain.from_iterable(t[0] for t in buff))
    facts_en = [en2id.id2word[idx]
                for idx in chain.from_iterable(t[1] for t in buff)]

    if verbose:
        logger.info('Output results to ' + args.path_output)
    with open(args.path_output, 'w') as f:
        for fact, rank in sorted(zip(facts_en, ranks), key=lambda t: t[0]):
            f.write('{}\t{}\n'.format(fact, rank))
Example #36
0
    def fit(self,
            x_train,
            y_train,
            x_test=[],
            y_test=[],
            is_classification=False):
        xp = np
        if self.gpu_flag >= 0:
            cuda.check_cuda_available()
            cuda.get_device(self.gpu_flag).use()
            xp = cuda.cupy
            self.model.to_gpu()

        if self.log_path != "" and not os.path.isfile(self.log_path):
            print "log_path: " + self.log_path
            utility.writeText(
                self.log_path, "a",
                "datetime,epoch,train_loss,test_loss,test_accuracy\n")

        for epoch in xrange(self.epoch):
            train_size = len(x_train)
            train_loss = 0.
            test_loss = 0.
            test_accuracy = 0.
            indexes = np.random.permutation(train_size)
            for i in xrange(0, train_size, self.batch_size):
                x = Variable(
                    xp.asarray(x_train[indexes[i:i + self.batch_size]]))
                t = Variable(
                    xp.asarray(y_train[indexes[i:i + self.batch_size]]))

                self.model.zerograds()
                loss = self.loss_function(self.forward(x), t)
                loss.backward()
                self.optimizer.update()
                train_loss += loss.data * len(t.data)
            train_loss /= train_size

            test_size = len(x_test)
            for i in xrange(0, test_size, self.batch_size):
                x = Variable(xp.asarray(x_test[i:i + self.batch_size]))
                t = Variable(xp.asarray(y_test[i:i + self.batch_size]))
                test_loss += float(
                    self.loss_function(self.forward(x), t).data) * len(t.data)
                if is_classification:
                    test_accuracy += float(
                        F.accuracy(self.forward(x), t).data) * len(t.data)
            if test_size != 0:
                test_loss /= test_size
                test_accuracy /= test_size

            if epoch > 1 and self.log_path != "":
                utility.writeText(
                    self.log_path, "a",
                    "%s,%d,%f,%f,%f\n" % (utility.now(), epoch, train_loss,
                                          test_loss, test_accuracy))

        if self.gpu_flag >= 0:
            self.model.to_cpu()
        if self.export_path != "":
            pickle.dump(self.model, open(self.export_path, 'wb'), -1)
        return self
Example #37
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('train', help='Path to training triplet list file')
    parser.add_argument('val', help='Path to validation triplet list file')
    parser.add_argument('ent_vocab', help='Path to entity vocab')
    parser.add_argument('rel_vocab', help='Path to relation vocab')
    parser.add_argument('--model', default='conve',
                    choices=['complex', 'conve', 'distmult'])
    parser.add_argument('--gpu', '-g', default=-1, type=int,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--batchsize', '-b', type=int, default=1000,
                        help='learning minibatch size')
    parser.add_argument('--epoch', '-e', default=20, type=int,
                        help='number of epochs to learn')
    parser.add_argument('--negative-size', default=10, type=int,
                        help='number of negative samples')
    parser.add_argument('--out', default='result',
                        help='Directory to output the result')
    parser.add_argument('--val-iter', type=int, default=1000,
                        help='validation iteration')
    parser.add_argument('--init-model', default=None,
                        help='initialize model with saved one')
    parser.add_argument('--expand-graph', action='store_true')
    parser.add_argument('--fast-eval', action='store_true')
    args = parser.parse_args()

    if not os.path.exists(args.out):
        os.mkdir(args.out)
    log_path = os.path.join(args.out, 'loginfo')
    file_handler = logging.FileHandler(log_path)
    fmt = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    file_handler.setFormatter(fmt)
    logger.addHandler(file_handler)

    logger.info('train: {}'.format(args.train))
    logger.info('val: {}'.format(args.val))
    logger.info('gpu: {}'.format(args.gpu))
    logger.info('model: {}'.format(args.model))
    logger.info('batchsize: {}'.format(args.batchsize))
    logger.info('epoch: {}'.format(args.epoch))
    logger.info('negative-size: {}'.format(args.negative_size))
    logger.info('out: {}'.format(args.out))

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()

    ent_vocab = Vocab.load(args.ent_vocab)
    rel_vocab = Vocab.load(args.rel_vocab)

    if args.fast_eval:
        train = FastEvalTripletDataset(ent_vocab, rel_vocab, args.train, args.expand_graph)
    else:
        train = TripletDataset(ent_vocab, rel_vocab, args.train, args.negative_size)
    val = TripletDataset(ent_vocab, rel_vocab, args.val, 0, train.graph)

    if args.model == 'conve':
        model = ConvE(train.num_entities, train.num_relations, args.fast_eval)
    elif args.model == 'complex':
        model = ComplEx(train.num_entities, train.num_relations, args.fast_eval)
    elif args.model == 'distmult':
        model = DistMult(train.num_entities, train.num_relations, args.fast_eval)
    else:
        raise "no such model available: {}".format(args.model)

    if args.init_model:
        logger.info("initialize model with: {}".format(args.init_model))
        serializers.load_npz(args.init_model, model)
    if args.gpu >= 0:
        model.to_gpu()

    optimizer = O.Adam() # (alpha=0.003)
    optimizer.setup(model)

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    val_iter = chainer.iterators.SerialIterator(val, args.batchsize, repeat=False)

    updater = training.StandardUpdater(
        train_iter, optimizer, converter=convert, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    val_interval = args.val_iter, 'iteration'
    log_interval = 100, 'iteration'

    trainer.extend(
        CalculateMetrics(model, val_iter, 'validation metrics', device=args.gpu),
        trigger=val_interval)
    trainer.extend(
        CalculateMetrics(model, train_iter, 'train metrics', device=args.gpu),
        trigger=val_interval)
    trainer.extend(extensions.snapshot_object(
        model, 'model_iter_{.updater.iteration}'), trigger=val_interval)
    trainer.extend(extensions.LogReport(trigger=log_interval))
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss']), trigger=log_interval)
    trainer.extend(extensions.ProgressBar())
    trainer.run()
def main(inputFile, outputFile, gpu=0, color='rgb', arch='seranet_v1'):

    filepath = os.path.dirname(os.path.realpath(__file__))

    #DEBUG
    #inputFile = os.path.join(filepath, '../assets/compare/4/photo4_xinput.jpg')
    #outputFile = os.path.join(filepath, '../assets/compare/4/seranet_v1.jpg')

    input_file_path = inputFile
    if not os.path.exists(input_file_path):
        raise ValueError('input file ', os.path.dirname(input_file_path),
                         ' not exist')

    if outputFile == None:
        file_name_with_ext = os.path.basename(
            inputFile)  # returns filename from path
        filename_wo_ext, ext = os.path.splitext(file_name_with_ext)
        output_file_path = os.path.join(os.path.dirname(inputFile),
                                        filename_wo_ext + '-seranet.jpg')
        conventional_file_path = os.path.join(
            os.path.dirname(inputFile), filename_wo_ext + '-conventional.jpg')
    else:
        file_name_with_ext = os.path.basename(
            outputFile)  # returns filename from path
        filename_wo_ext, ext = os.path.splitext(file_name_with_ext)
        output_file_path = outputFile
        conventional_file_path = os.path.join(
            os.path.dirname(outputFile), filename_wo_ext + '-conventional.jpg')
        output_file_dir = os.path.dirname(output_file_path)
        if not os.path.exists(output_file_dir):
            os.mkdir(output_file_dir)
            print('output file directory ', output_file_dir,
                  ' not exist, created automatically')

    if gpu >= 0:
        cuda.check_cuda_available()
    xp = cuda.cupy if gpu >= 0 else np

    if color == 'yonly':
        inout_ch = 1
    elif color == 'rgb':
        inout_ch = 3
    else:
        raise ValueError('Invalid color training scheme')

    # Prepare model
    print('prepare model')
    if arch == 'basic_cnn_tail':
        import arch.basic_cnn_tail as model_arch
        model = model_arch.basic_cnn_tail(inout_ch=inout_ch)

    elif arch == 'basic_cnn_middle':
        import arch.basic_cnn_middle as model_arch
        model = model_arch.basic_cnn_middle(inout_ch=inout_ch)
    elif arch == 'basic_cnn_head':
        import arch.basic_cnn_head as model_arch
        model = model_arch.basic_cnn_head(inout_ch=inout_ch)
    elif arch == 'basic_cnn_small':
        import arch.basic_cnn_small as model_arch
        model = model_arch.basic_cnn_small(inout_ch=inout_ch)
    elif arch == 'seranet':
        import arch.seranet_split as model_arch
        model = model_arch.seranet_split(inout_ch=inout_ch)
    elif arch == 'seranet_v1':
        import arch.seranet_v1 as model_arch
        model = model_arch.seranet_v1(inout_ch=inout_ch)

    else:
        raise ValueError('Invalid architecture name')
    arch_folder = model_arch.arch_folder
    # Directory/File setting for output
    output_folder = os.path.join(arch_folder, color, 'output')
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    #os.chdir(output_folder)
    inference_log_file_name = 'inference.log'
    inference_log_file = open(
        os.path.join(output_folder, inference_log_file_name), 'w')
    """ Model setup """
    print('setup model')
    model_load_path = os.path.join(arch_folder, color, 'training_process',
                                   'my.model')
    serializers.load_npz(model_load_path, model)
    if gpu >= 0:
        cuda.get_device(gpu).use()
        model.to_gpu()
    model.train = False
    """ Load data """
    print('loading data')
    input_img = cv2.imread(input_file_path, cv2.IMREAD_COLOR)

    print('upscaling to ', output_file_path)
    if color == 'rgb':
        input_img = np.transpose(input_img[:, :, :], (2, 0, 1))
        input_img = input_img / 255.0  # Must be handled as float
        input_img = input_img.reshape(
            (1, input_img.shape[0], input_img.shape[1], input_img.shape[2]))
        x_data = model.preprocess_x(input_img)
        x = Variable(xp.asarray(x_data), volatile='on')
        output_img = model(x)
        if (gpu >= 0):
            output_data = cuda.cupy.asnumpy(output_img.data)
        else:
            output_data = output_img.data
        output_img = output_data[0].transpose(1, 2, 0) * 255.

    elif color == 'yonly':
        ycc_input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2YCR_CB)
        y_input_img = np.transpose(ycc_input_img[:, :, 0:1], (2, 0, 1))
        y_input_img = y_input_img / 255.0  # Must be handled as float
        y_input_img = y_input_img.reshape(
            (1, y_input_img.shape[0], y_input_img.shape[1],
             y_input_img.shape[2]))

        x_data = model.preprocess_x(y_input_img)
        x = Variable(xp.asarray(x_data), volatile='on')
        output_y_img = model(x)
        if (gpu >= 0):
            output_y_data = cuda.cupy.asnumpy(output_y_img.data)
        else:
            output_y_data = output_y_img.data

        input_image_height = input_img.shape[0]
        input_image_width = input_img.shape[1]
        output_image_height = 2 * input_image_height
        output_image_width = 2 * input_image_width
        scaled_input_img = cv2.resize(
            input_img, (output_image_width, output_image_height),
            interpolation=cv2.INTER_LANCZOS4)
        ycc_scaled_input_img = cv2.cvtColor(scaled_input_img,
                                            cv2.COLOR_BGR2YCR_CB)
        ycc_scaled_input_img[:, :, 0:1] = output_y_data[0].transpose(
            1, 2, 0) * 255.  # (width, height, ch)
        output_img = cv2.cvtColor(ycc_scaled_input_img, cv2.COLOR_YCR_CB2BGR)

    print('saved to ', output_file_path)
    cv2.imwrite(output_file_path, output_img)
Example #39
0
def main():
    CHUNK = 1024
    length = 160
    expected_fs = 16000

    model = MUSIC_NET()

    cuda.get_device(0).use()  # Make a specified GPU current
    cuda.check_cuda_available()
    model.to_gpu()  # Copy the model to the GPU
    xp = cupy

    optimizer = optimizers.Adam(alpha=0.0001)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001))

    music_data = dict()
    music_data['LiSA1'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/LiSA/l-01.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['LiSA2'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/LiSA/l-02.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['LiSA3'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/LiSA/l-03.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['LiSA4'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/LiSA/l-04.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)

    music_data['BUMP1'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/BUMP/bump-01.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['BUMP2'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/BUMP/bump-02.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['BUMP3'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/BUMP/bump-03.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['BUMP4'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/BUMP/bump-04.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)

    music_data['Prono1'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/prono/p-01.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Prono2'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/prono/p-02.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Prono3'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/prono/p-03.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Prono4'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/prono/p-04.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)

    music_data['Bz1'] = READ_DATASET(wavfile='music/8bit-16000Hz/Bz/bz-01.wav',
                                     chunk=CHUNK,
                                     length=length,
                                     expected_fs=expected_fs)
    music_data['Bz2'] = READ_DATASET(wavfile='music/8bit-16000Hz/Bz/bz-02.wav',
                                     chunk=CHUNK,
                                     length=length,
                                     expected_fs=expected_fs)
    music_data['Bz3'] = READ_DATASET(wavfile='music/8bit-16000Hz/Bz/bz-03.wav',
                                     chunk=CHUNK,
                                     length=length,
                                     expected_fs=expected_fs)
    music_data['Bz4'] = READ_DATASET(wavfile='music/8bit-16000Hz/Bz/bz-04.wav',
                                     chunk=CHUNK,
                                     length=length,
                                     expected_fs=expected_fs)

    music_data['MrChildren1'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/MrChildren/mch-01.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['MrChildren2'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/MrChildren/mch-02.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['MrChildren3'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/MrChildren/mch-03.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['MrChildren4'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/MrChildren/mch-04.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)

    music_data['Utada1'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/utada/u-01.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Utada2'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/utada/u-02.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Utada3'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/utada/u-03.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)
    music_data['Utada4'] = READ_DATASET(
        wavfile='music/8bit-16000Hz/utada/u-04.wav',
        chunk=CHUNK,
        length=length,
        expected_fs=expected_fs)

    music_data['Other'] = READ_DATASET2(wavdir='wav_16000/',
                                        chunk=CHUNK,
                                        length=length,
                                        expected_fs=expected_fs)

    artist2label = {
        'LiSA': 1,
        'BUMP': 2,
        'Prono': 3,
        'Bz': 4,
        'MrChildren': 5,
        'Utada': 6
    }

    n_epoch = 1
    n_batch_per_oneclass = 10
    for epoch in np.arange(n_epoch):
        for key in music_data.keys():
            if key == 'Other':
                continue
            print key, artist2label[key[:-1]], music_data[key].n_blocks
            music_data[key].shuffle_indexes()

        n_blocks = music_data['LiSA1'].n_blocks
        for key in music_data.keys():
            if key == 'Other':
                continue
            n_blocks = min(n_blocks, music_data[key].n_blocks)

        end_flag = False
        for i in np.arange(0, n_blocks, n_batch_per_oneclass):
            xs, ts = None, None
            for key in music_data.keys():
                if key == 'Other':
                    continue
                if i + n_batch_per_oneclass < music_data[key].n_blocks:
                    indexes = np.arange(i, i + n_batch_per_oneclass)
                    features = music_data[key].get_batch_melspec(indexes)
                    label_num = artist2label[key[:-1]]
                    answers = np.ones(n_batch_per_oneclass,
                                      dtype=np.int32) * label_num
                else:
                    end_flag = True
                    break

                if xs is None:
                    xs = features.copy()
                    ts = answers.copy()
                else:
                    xs = np.concatenate((xs, features.copy()), axis=0)
                    ts = np.r_[ts, answers.copy()]

            if end_flag:
                break

            Other_feats = music_data['Other'].get_batch_melspec()
            Other_ts = np.zeros(music_data['Other'].n_data, dtype=np.int32)

            # 連結
            xs = np.concatenate((xs, Other_feats.copy()),
                                axis=0).astype('float32')
            # xs = np.log(xs + 1e-04)
            xs = np.log(xs + 1.0)

            # 教師信号
            ts = np.r_[ts, Other_ts.copy()]

            optimizer.zero_grads()
            loss, accuracy = model(cuda.to_gpu(xs),
                                   cuda.to_gpu(ts))  # 入力はログスケール

            loss.backward()
            optimizer.update()

            if i % (n_batch_per_oneclass * 10) == 0:
                print 'epoch:', epoch + 1, ', index:', i, '(/ %d)' % n_blocks
                print 'value:', cuda.to_cpu(loss.data), cuda.to_cpu(
                    accuracy.data)
                print '--------------------'

                if i % (n_batch_per_oneclass * 100) == 0 and i != 0:
                    # モデルパラメータの保存
                    serializers.save_hdf5(
                        "trainedmodel/model3-%02d-%05d.model" % (epoch + 1, i),
                        model)
                    print("model saved")
def main():
    parser = argparse.ArgumentParser()
    # logging
    parser.add_argument('--logfile',
                        '-l',
                        default='',
                        type=str,
                        help='write log data into a file')
    parser.add_argument('--debug',
                        '-d',
                        action='store_true',
                        help='run in debug mode')
    parser.add_argument('--silent',
                        '-s',
                        action='store_true',
                        help='run in silent mode')
    parser.add_argument('--no-progress-bar',
                        action='store_true',
                        help='hide progress bar')
    # train and validate data
    parser.add_argument('--train',
                        default='train.txt',
                        type=str,
                        help='set filename of training data')
    parser.add_argument('--validate',
                        default='dev.txt',
                        type=str,
                        help='set filename of validation data')
    parser.add_argument('--vocab-size',
                        '-V',
                        default=0,
                        type=int,
                        help='set vocabulary size (0 means no limitation)')
    parser.add_argument(
        '--target-speaker',
        '-T',
        default='S',
        help='set target speaker name to be learned for system output')
    # file settings
    parser.add_argument('--initial-model',
                        '-i',
                        help='start training from an initial model')
    parser.add_argument('--model',
                        '-m',
                        required=True,
                        help='set prefix of output model files')
    parser.add_argument(
        '--resume',
        action='store_true',
        help='resume training from a previously saved snapshot')
    parser.add_argument('--snapshot',
                        type=str,
                        help='dump a snapshot to a file after each epoch')
    # Model structure
    parser.add_argument('--enc-layer',
                        default=2,
                        type=int,
                        help='number of encoder layers')
    parser.add_argument('--enc-esize',
                        default=100,
                        type=int,
                        help='number of encoder input-embedding units')
    parser.add_argument('--enc-hsize',
                        default=512,
                        type=int,
                        help='number of encoder hidden units')

    parser.add_argument('--dec-layer',
                        default=2,
                        type=int,
                        help='number of decoder layers')
    parser.add_argument('--dec-esize',
                        default=100,
                        type=int,
                        help='number of decoder input-embedding units')
    parser.add_argument('--dec-hsize',
                        default=512,
                        type=int,
                        help='number of decoder hidden units')
    parser.add_argument('--dec-psize',
                        default=100,
                        type=int,
                        help='number of decoder pre-output projection units')
    # training conditions
    parser.add_argument(
        '--optimizer',
        default='Adam',
        type=str,
        help="set optimizer (SGD, Adam, AdaDelta, RMSprop, ...)")
    parser.add_argument('--L2-weight',
                        default=0.0,
                        type=float,
                        help="set weight for L2-regularization term")
    parser.add_argument('--clip-grads',
                        default=5.,
                        type=float,
                        help="set gradient clipping threshold")
    parser.add_argument('--dropout-rate',
                        default=0.5,
                        type=float,
                        help="set dropout rate in training")
    parser.add_argument('--num-epochs',
                        '-e',
                        default=20,
                        type=int,
                        help='number of epochs to be trained')
    parser.add_argument('--learn-rate',
                        '-R',
                        default=1.0,
                        type=float,
                        help='set initial learning rate for SGD')
    parser.add_argument('--learn-decay',
                        default=1.0,
                        type=float,
                        help='set decaying ratio of learning rate or epsilon')
    parser.add_argument(
        '--lower-bound',
        default=1e-16,
        type=float,
        help='set threshold of learning rate or epsilon for early stopping')
    parser.add_argument('--batch-size',
                        '-b',
                        default=50,
                        type=int,
                        help='set batch size for training and validation')
    parser.add_argument(
        '--max-batch-length',
        default=20,
        type=int,
        help='set maximum sequence length to control batch size')
    parser.add_argument('--seed',
                        default=99,
                        type=int,
                        help='set a seed for random numbers')
    # select a GPU device
    parser.add_argument('--gpu',
                        '-g',
                        default=0,
                        type=int,
                        help='GPU ID (negative value indicates CPU)')

    args = parser.parse_args()

    # flush stdout
    if six.PY2:
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
    # set up the logger
    tqdm_logging.config(logger,
                        args.logfile,
                        mode=('a' if args.resume else 'w'),
                        silent=args.silent,
                        debug=args.debug)
    # gpu setup
    if args.gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(args.gpu).use()
        xp = cuda.cupy
        xp.random.seed(args.seed)
    else:
        xp = np

    # randomize
    np.random.seed(args.seed)
    random.seed(args.seed)

    logger.info('----------------------------------')
    logger.info('Train a neural conversation model')
    logger.info('----------------------------------')
    if args.resume:
        if not args.snapshot:
            logger.error('snapshot file is not spacified.')
            sys.exit()

        with open(args.snapshot, 'rb') as f:
            vocab, optimizer, status, args = pickle.load(f)
        logger.info('Resume training from epoch %d' % status.epoch)
        logger.info('Args ' + str(args))
        model = optimizer.target
    else:
        logger.info('Args ' + str(args))
        # Prepare RNN model and load data
        if args.initial_model:
            logger.info('Loading a model from ' + args.initial_model)
            with open(args.initial_model, 'rb') as f:
                vocab, model, tmp_args = pickle.load(f)
            status.cur_at = time.time()
        else:
            logger.info('Making vocabulary from ' + args.train)
            vocab = dialog_corpus.get_vocabulary(args.train,
                                                 vocabsize=args.vocab_size)
            model = Sequence2SequenceModel(
                LSTMEncoder(args.enc_layer,
                            len(vocab),
                            args.enc_hsize,
                            args.enc_esize,
                            dropout=args.dropout_rate),
                LSTMDecoder(args.dec_layer,
                            len(vocab),
                            len(vocab),
                            args.dec_esize,
                            args.dec_hsize,
                            args.dec_psize,
                            dropout=args.dropout_rate))
        # Setup optimizer
        optimizer = vars(optimizers)[args.optimizer]()
        if args.optimizer == 'SGD':
            optimizer.lr = args.learn_rate
        optimizer.use_cleargrads()
        optimizer.setup(model)
        optimizer.add_hook(chainer.optimizer.GradientClipping(args.clip_grads))
        if args.L2_weight > 0.:
            optimizer.add_hook(chainer.optimizer.WeightDecay(args.L2_weight))
        status = None

    logger.info('Loading text data from ' + args.train)
    train_set = dialog_corpus.load(args.train, vocab, args.target_speaker)
    logger.info('Loading validation data from ' + args.validate)
    validate_set = dialog_corpus.load(args.validate, vocab,
                                      args.target_speaker)
    logger.info('Making mini batches')
    train_batchset = dialog_corpus.make_minibatches(
        train_set, batchsize=args.batch_size, max_length=args.max_batch_length)
    validate_batchset = dialog_corpus.make_minibatches(
        validate_set,
        batchsize=args.batch_size,
        max_length=args.max_batch_length)
    # report data summary
    logger.info('vocabulary size = %d' % len(vocab))
    logger.info('#train sample = %d  #mini-batch = %d' %
                (len(train_set), len(train_batchset)))
    logger.info('#validate sample = %d  #mini-batch = %d' %
                (len(validate_set), len(validate_batchset)))
    random.shuffle(train_batchset, random.random)

    # initialize status parameters
    if status is None:
        status = Status(max(round(len(train_batchset), -3) / 50, 500),
                        progress_bar=not args.no_progress_bar)
    else:
        status.progress_bar = not args.no_progress_bar

    # move model to gpu
    if args.gpu >= 0:
        model.to_gpu()

    while status.epoch <= args.num_epochs:
        logger.info('---------------------training--------------------------')
        if args.optimizer == 'SGD':
            logger.info('Epoch %d/%d : SGD learning rate = %g' %
                        (status.epoch, args.num_epochs, optimizer.lr))
        else:
            logger.info(
                'Epoch %d/%d : %s eps = %g' %
                (status.epoch, args.num_epochs, args.optimizer, optimizer.eps))
        train_ppl = train_step(model, optimizer, train_set, train_batchset,
                               status, xp)
        logger.info("epoch %d training perplexity: %f" %
                    (status.epoch, train_ppl))
        # write the model params
        modelfile = args.model + '.' + str(status.epoch)
        logger.info('writing model params to ' + modelfile)
        model.to_cpu()
        with open(modelfile, 'wb') as f:
            pickle.dump((vocab, model, args), f, -1)
        if args.gpu >= 0:
            model.to_gpu()

        # start validation step
        logger.info('---------------------validation------------------------')
        start_at = time.time()
        validate_ppl = validate_step(model, validate_set, validate_batchset,
                                     status, xp)
        logger.info('epoch %d validation perplexity: %.4f' %
                    (status.epoch, validate_ppl))
        # update best model with the minimum perplexity
        if status.min_validate_ppl >= validate_ppl:
            status.bestmodel_num = status.epoch
            logger.info('validation perplexity reduced: %.4f -> %.4f' %
                        (status.min_validate_ppl, validate_ppl))
            status.min_validate_ppl = validate_ppl

        elif args.optimizer == 'SGD':
            modelfile = args.model + '.' + str(status.bestmodel_num)
            logger.info('reloading model params from ' + modelfile)
            with open(modelfile, 'rb') as f:
                vocab, model, tmp_args = pickle.load(f)
            if args.gpu >= 0:
                model.to_gpu()
            optimizer.lr *= args.learn_decay
            if optimizer.lr < args.lower_bound:
                break
            optimizer.setup(model)
        else:
            optimizer.eps *= args.learn_decay
            if optimizer.eps < args.lower_bound:
                break

        status.new_epoch(validate_time=time.time() - start_at)
        # dump snapshot
        if args.snapshot:
            logger.info('writing snapshot to ' + args.snapshot)
            model.to_cpu()
            with open(args.snapshot, 'wb') as f:
                pickle.dump((vocab, optimizer, status, args), f, -1)
            if args.gpu >= 0:
                model.to_gpu()

    logger.info('----------------')
    # make a symbolic link to the best model
    logger.info('the best model is %s.%d.' %
                (args.model, status.bestmodel_num))
    logger.info('a symbolic link is made as ' + args.model + '.best')
    if os.path.exists(args.model + '.best'):
        os.remove(args.model + '.best')
    os.symlink(os.path.basename(args.model + '.' + str(status.bestmodel_num)),
               args.model + '.best')
    logger.info('done')
Example #41
0
def main():
    global xp

    import argparse
    parser = argparse.ArgumentParser(description='SER example: NStep LSTM')
    parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer', type=int, default=1,    help='Number of layes for turn')
    parser.add_argument('--unit',  type=int, default=256,  help='Number of units for turn')
    parser.add_argument('--dim',   type=int, default=384,  help='Number of dimensions')
    parser.add_argument('--batchsize', '-b', type=int, default=3,  help='Number of images in each mini-batch')
    parser.add_argument('--epoch',     '-e', type=int, default=20, help='Number of sweeps over the dataset to train')
    parser.add_argument('--train', default='train.tsv', type=str, help='training file (.txt)')
    parser.add_argument('--test',  default='test.tsv',  type=str, help='evaluating file (.txt)')
    parser.add_argument('--out', '-o', default='result', help='Directory to output the result')
    parser.add_argument('--noplot', dest='plot', action='store_true', help='Disable PlotReport extension')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    xp = cuda.cupy if args.gpu >= 0 else np

    model_dir = args.out
    if not os.path.exists(model_dir):
        os.mkdir(model_dir)

    # データの読み込み
    X_train, y_train, z_train, labels = load_data(args.train)
    X_test,  y_test,  z_test,  labels = load_data(args.test, labels=labels)

    n_class = len(labels)

    print('# train X: {}, y: {}, class: {}'.format(len(X_train), len(y_train), len(labels)))
    print('# eval  X: {}, y: {}, class: {}'.format(len(X_test), len(y_test), len(labels)))
    print('# class: {}'.format(n_class))
    sys.stdout.flush()

    with open(os.path.join(args.out, 'labels.pkl'), 'wb') as f:
        pickle.dump(labels, f)

    model = SER(args.dim, args.layer, args.unit, n_class)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 重み減衰
    decay = 0.0001

    # 勾配上限
    grad_clip = 3

    # 学習率の減衰
    lr_decay = 0.995

    # Setup optimizer (Optimizer の設定)
    optimizer = chainer.optimizers.Adam(alpha=0.001)
    optimizer.setup(model)
    # optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
    # optimizer.add_hook(chainer.optimizer.WeightDecay(decay))

    # プロット用に実行結果を保存する
    train_loss = []
    train_accuracy1 = []
    train_accuracy2 = []
    test_loss = []
    test_accuracy1 = []
    test_accuracy2 = []

    min_loss = float('inf')
    min_epoch = 0

    start_at = time.time()
    cur_at = start_at

    # Learning loop
    for epoch in range(1, args.epoch + 1):

        # training
        train_iter = batch_tuple([(s, t1, t2) for s, t1, t2 in zip(X_train, y_train, z_train)], args.batchsize)
        sum_train_loss = 0.
        sum_train_accuracy1 = 0.
        sum_train_accuracy2 = 0.
        K = 0

        for X, y, z in train_iter:

            # 勾配を初期化
            model.cleargrads()

            # 順伝播させて誤差と精度を算出
            loss, accuracy = model(X, y)
            sum_train_loss += float(loss.data)
            sum_train_accuracy1 += float(accuracy.data)
            sum_train_accuracy2 += .0
            K += len(y)

            # 誤差逆伝播で勾配を計算
            loss.backward()
            optimizer.update()

        # 訓練データの誤差と,正解精度を表示
        mean_train_loss = sum_train_loss / K
        mean_train_accuracy1 = sum_train_accuracy1 / K
        mean_train_accuracy2 = sum_train_accuracy2 / K
        train_loss.append(mean_train_loss)
        train_accuracy1.append(mean_train_accuracy1)
        train_accuracy2.append(mean_train_accuracy2)
        now = time.time()
        train_throughput = now - cur_at
        cur_at = now

        # evaluation
        test_iter = batch_tuple([(s, t1, t2) for s, t1, t2 in zip(X_test, y_test, z_test)], 1)
        sum_test_loss = 0.
        sum_test_accuracy1 = 0.
        sum_test_accuracy2 = 0.
        K = 0

        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            for X, y, z in test_iter:

                # 順伝播させて誤差と精度を算出
                loss, accuracy = model(X, y)
                sum_test_loss += float(loss.data)
                sum_test_accuracy1 += float(accuracy.data)
                sum_test_accuracy2 += .0
                K += len(y)

        # テストデータでの誤差と正解精度を表示
        mean_test_loss = sum_test_loss / K
        mean_test_accuracy1 = sum_test_accuracy1 / K
        mean_test_accuracy2 = sum_test_accuracy2 / K
        test_loss.append(mean_test_loss)
        test_accuracy1.append(mean_test_accuracy1)
        test_accuracy2.append(mean_test_accuracy2)
        now = time.time()
        test_throughput = now - cur_at
        cur_at = now

        logger.info(''
                    '[{:>3d}] '
                    'T/loss={:.6f} '
                    'T/acc1={:.6f} '
                    'T/acc2={:.6f} '
                    'T/sec= {:.6f} '
                    'D/loss={:.6f} '
                    'D/acc1={:.6f} '
                    'D/acc2={:.6f} '
                    'D/sec= {:.6f} '
                    'lr={:.6f}'
                    ''.format(
            epoch,
            mean_train_loss,
            mean_train_accuracy1,
            mean_train_accuracy2,
            train_throughput,
            mean_test_loss,
            mean_test_accuracy1,
            mean_test_accuracy2,
            test_throughput,
            optimizer.alpha
        )
        )
        sys.stdout.flush()

        # model と optimizer を保存する
        if mean_test_loss < min_loss:
            min_loss = mean_test_loss
            min_epoch = epoch
            if args.gpu >= 0: model.to_cpu()
            chainer.serializers.save_npz(os.path.join(model_dir, 'early_stopped.model'), model)
            chainer.serializers.save_npz(os.path.join(model_dir, 'early_stopped.state'), optimizer)
            if args.gpu >= 0: model.to_gpu()

        # optimizer.alpha *= lr_decay

        # 精度と誤差をグラフ描画
        if not args.plot:
            ylim1 = [min(train_loss + test_loss), max(train_loss + test_loss)]
            ylim2 = [min(train_accuracy1 + test_accuracy2), max(train_accuracy1 + test_accuracy2)]

            # グラフ左
            plt.figure(figsize=(10, 10))

            plt.subplot(1, 2, 1)
            plt.ylim(ylim1)
            plt.plot(range(1, len(train_loss) + 1), train_loss, color='C1', marker='x')
            # plt.grid()
            plt.ylabel('loss')
            plt.legend(['train loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1, len(train_accuracy1) + 1), train_accuracy1, color='C0', marker='x')
            # plt.plot(range(1, len(train_accuracy2) + 1), train_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            # plt.ylabel('accuracy')
            plt.legend(['train turn', 'train call'], loc="upper right")
            plt.title('Loss and accuracy of train.')

            # グラフ右
            plt.subplot(1, 2, 2)
            plt.ylim(ylim1)
            plt.plot(range(1, len(test_loss) + 1), test_loss, color='C1', marker='x')
            # plt.grid()
            # plt.ylabel('loss')
            plt.legend(['dev loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1, len(test_accuracy1) + 1), test_accuracy1, color='C0', marker='x')
            # plt.plot(range(1, len(test_accuracy2) + 1), test_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            plt.ylabel('accuracy')
            plt.legend(['dev turn', 'dev call'], loc="upper right")
            plt.title('Loss and accuracy of dev.')

            plt.savefig('{}.png'.format(args.out))
            # plt.savefig('{}.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()

        cur_at = now

    # model と optimizer を保存する
    if args.gpu >= 0: model.to_cpu()
    chainer.serializers.save_npz(os.path.join(model_dir, 'final.model'), model)
    chainer.serializers.save_npz(os.path.join(model_dir, 'final.state'), optimizer)
    if args.gpu >= 0: model.to_gpu()

    logger.info('time spent: {:.6f} sec\n'.format(time.time() - start_time))
Example #42
0
def main(args):
    global verbose
    verbose = args.verbose

    assert args.poly_degree >= 1, '--degree must be positive integer'
    poly_degree = args.poly_degree

    if verbose:
        report_params(args)

    gpu = args.gpu
    if gpu >= 0:
        cuda.check_cuda_available()
        if verbose:
            logger.info('Use GPU {}'.format(gpu))
        cuda.get_device_from_id(gpu).use()

    set_random_seed(0, use_gpu=(gpu >= 0))

    n_epochs = args.n_epochs
    batch_size = args.batch_size

    # Dataset
    dfs = {}
    dfs['train'] = read_dataset(path.join(args.dir_in, args.filename_train))
    dfs['devel'] = read_dataset(path.join(args.dir_in, args.filename_devel))

    # Load relation vocabulary
    rel2id = Vocabulary()
    rel2id.read_from_file(args.path_rels)

    # Load concept vocabulary
    if verbose:
        logger.info('Load vocabulary')
    fact2id = Vocabulary()
    fact2id.read_from_list(np.unique(get_values(list(dfs.values()), 'fact')))
    ja2id = Vocabulary()
    ja2id.read_from_list(np.unique(get_values(list(dfs.values()), 'fact_ja')))
    en2id = Vocabulary()
    en2id.read_from_list(np.unique(get_values(list(dfs.values()), 'fact_en')))

    if verbose:
        logger.info('Replace facts with indices')
    for col in dfs.keys():
        dfs[col].loc[:, 'fact'] = replace_by_dic(dfs[col]['fact'],
                                                 fact2id).astype(np.int32)
        dfs[col].loc[:, 'fact_ja'] = replace_by_dic(dfs[col]['fact_ja'],
                                                    ja2id).astype(np.int32)
        dfs[col].loc[:, 'fact_en'] = replace_by_dic(dfs[col]['fact_en'],
                                                    en2id).astype(np.int32)
        dfs[col].loc[:, 'rel'] = replace_by_dic(dfs[col]['rel'],
                                                rel2id).astype(np.int32)
    label2fact = {
        i: set(
            np.concatenate(
                [df[df['twa'] == i]['fact'].unique() for df in dfs.values()]))
        for i in [0, 1]
    }
    en2ja = {
        en: set(df[df['fact_en'] == en]['fact'].unique())
        for df in dfs.values() for en in sorted(df['fact_en'].unique())
    }
    idx2vec = get_idx2vec(list(dfs.values()), poly_degree=poly_degree)

    n_facts = len(fact2id)
    n_en = len(en2id)
    n_ja = len(ja2id)
    assert n_facts + 1 == len(
        idx2vec), '{}[n_facts] != {}[len(idx2vec)]'.format(
            n_facts + 1, len(idx2vec))

    if verbose:
        logger.info('Alignment: {}'.format(n_facts))
        logger.info('En: {}'.format(n_en))
        logger.info('Ja: {}'.format(n_ja))
        logger.info('Train: {}'.format(len(dfs['train'])))
        logger.info('Devel: {}'.format(len(dfs['devel'])))

    model_type = args.model.lower()
    dim_in = len(COL_BASIC_FEATURES)
    rel_size = len(rel2id)
    if model_type.startswith('linear'):
        ensembler = LinearEnsembler(dim_in,
                                    rel_size,
                                    use_gpu=(gpu >= 0),
                                    poly_degree=poly_degree,
                                    flag_unifw=args.flag_unifw,
                                    verbose=verbose)
    elif model_type.startswith('mlp'):
        options = args.model.split(':')
        params = {}
        if len(options) > 1:
            params['dim_hid'] = int(options[1])
        if len(options) > 2:
            params['activation'] = options[2]
        ensembler = MLPEnsembler(dim_in,
                                 rel_size,
                                 use_gpu=(gpu >= 0),
                                 poly_degree=poly_degree,
                                 flag_unifw=args.flag_unifw,
                                 verbose=verbose,
                                 **params)
    else:
        raise ValueError('Invalid --model: {}'.format(model_type))

    # Set up a dataset iterator
    train_iter = FactIterator(dfs['train'],
                              args.batch_size,
                              ja2id,
                              en2id,
                              train=True,
                              repeat=True,
                              poly_degree=poly_degree)
    # Only keep positive examples in development set
    df = dfs['devel'][dfs['devel']['twa'] == 1].drop_duplicates('fact_en')

    # Set batch size
    batch_size = find_greatest_divisor(len(df))
    if batch_size == 1 and len(df) <= 10**4:
        batch_size = len(df)
    if verbose:
        logger.info('Devel batch size = {}'.format(batch_size))
    devel_iter = FactIterator(df,
                              batch_size,
                              ja2id,
                              en2id,
                              train=False,
                              repeat=False,
                              poly_degree=poly_degree)

    # Standardize vectors
    if args.flag_standardize:
        mu, sigma = train_iter.standardize_vectors()
        devel_iter.standardize_vectors(mu=mu, sigma=sigma)
        idx2vec = standardize_vectors(idx2vec, mu, sigma)
    else:
        mu, sigma = None, None

    if gpu >= 0:
        idx2vec = cuda.to_gpu(idx2vec)

    # Set up a model
    model = Classifier(ensembler,
                       label2fact,
                       en2ja,
                       idx2vec,
                       margin=args.margin,
                       lam=args.lam)

    if gpu >= 0:
        model.to_gpu(device=gpu)

    # Set up an optimizer
    optimizer = optimizers.AdaGrad(lr=args.lr)
    optimizer.setup(model)

    # Set up a trainer
    updater = Updater(train_iter, optimizer, device=gpu)
    trainer = training.Trainer(updater, (n_epochs, 'epoch'), out=args.dir_out)

    # evaluate development set
    evaluator = Evaluator(devel_iter, model, device=gpu)
    trainer.extend(evaluator)

    # Write out a log
    trainer.extend(extensions.LogReport())
    # Display training status
    trainer.extend(
        extensions.PrintReport([
            'epoch', 'main/loss', 'validation/main/loss',
            'validation/main/meanrank', 'validation/main/mrr', 'elapsed_time'
        ]))

    if args.save:
        trainer.extend(extensions.snapshot(), trigger=(args.n_epochs, 'epoch'))
        trainer.extend(extensions.snapshot_object(
            ensembler, 'model_iter_{.updater.iteration}'),
                       trigger=(1, 'epoch'))

    # Launch training process
    trainer.run()

    # Report the best score
    (epoch, score) = evaluator.get_best_score()
    if verbose:
        logger.info('Best score: {} (epoch={})'.format(score, epoch))

    # Clean the output directory
    if args.save:
        save_best_model(args.dir_out, ensembler, mu=mu, sigma=sigma)

    del dfs
    del fact2id
    del ja2id
    del en2id

    return score
Example #43
0
    def TrainSetup(self,
                   inputFileName,
                   Name,
                   model,
                   thin=50,
                   Norm=[1, 1, 1],
                   delta=60,
                   target_epoch=500,
                   Interrupt=100,
                   TestInputFile=""):
        self._Name = Name
        self._Norm = Norm
        self._delta = delta
        self._target_epoch = target_epoch
        self._Interrupt = Interrupt
        self._gpu_flag = 0
        self._xp = np
        self._thin = thin
        if len(TestInputFile) == 0:
            self._TestInputFile = inputFileName
        else:
            self._TestInputFile = TestInputFile

        if self._gpu_flag >= 0:
            cuda.check_cuda_available()
            self._xp = cuda.cupy

        self._model = model

        if self._gpu_flag >= 0:
            cuda.get_device(self._gpu_flag).use()
            self._model.to_gpu()

        self._optimizer = optimizers.Adam()
        self._optimizer.setup(self._model)

        #CSV read
        data = np.loadtxt(inputFileName, delimiter=',', skiprows=1)

        fileSystem.MakeFolder(os.path.join("./", Name))

        self._Input = self._model.InputNum()

        #Data set
        SpeedData = np.array(data).T[0, :]
        if self._Input >= 2:
            BrakeData = np.array(data).T[1, :]
        if self._Input == 3:
            AccelatorData = np.array(data).T[2, :]
        '''
        if self._thin == 0:
            self._SpeedData = np.array(SpeedData)/self._Norm[0]
            self._BrakeData = np.array(BrakeData)/self._Norm[1]
            self._AccelatorData = np.array(AccelatorData)/self._Norm[2]
        else:
        '''
        self._SpeedData = np.array(
            [SpeedData[v] for v in xrange(0, SpeedData.size, self._thin)],
            dtype=np.float32) / self._Norm[0]
        if self._Input >= 2:
            self._BrakeData = np.array(
                [BrakeData[v] for v in xrange(0, BrakeData.size, self._thin)],
                dtype=np.float32) / self._Norm[1]
        if self._Input == 3:
            self._AccelatorData = np.array([
                AccelatorData[v]
                for v in xrange(0, AccelatorData.size, self._thin)
            ],
                                           dtype=np.float32) / self._Norm[2]

        self.TestSetup(self._TestInputFile,
                       self._model,
                       thin=self._thin,
                       Norm=self._Norm)

        self._fig = TrainFigure.TrainFigure(True, [
            np.arange(self._TestSpeedData.size - 1),
            self._TestSpeedData[1:] * self._Norm[0]
        ])
Example #44
0
def do_train_by_chainer(
        db_model,
        root_output_dir,
        val_batchsize=250,
        loaderjob=20,
        pretrained_model="",
        avoid_flipping=False,
        interruptable=None,
):
    logger.info('Start imagenet train. model_id: {0} gpu: {1}, pretrained_model: {2}'
                .format(db_model.id, db_model.gpu, pretrained_model))
    # start initialization
    if db_model.gpu >= 0:
        cuda.check_cuda_available()
    xp = cuda.cupy if db_model.gpu >= 0 else np

    train_list = load_image_list(os.path.join(db_model.prepared_file_path, 'train.txt'))
    val_list = load_image_list(os.path.join(db_model.prepared_file_path, 'test.txt'))
    mean_image = pickle.load(open(os.path.join(db_model.prepared_file_path, 'mean.npy'), 'rb'))

    # @see http://qiita.com/progrommer/items/abd2276f314792c359da
    (model_dir, model_name) = os.path.split(db_model.network_path)
    model_name = re.sub(r"\.py$", "", model_name)
    model_module = load_module(model_dir, model_name)
    model = model_module.Network()

    # Load pretrained model
    if pretrained_model is not None and pretrained_model.find("model") > -1:
        logger.info("load pretrained model : "
                    + os.path.join(db_model.trained_model_path, pretrained_model))
        serializers.load_hdf5(os.path.join(db_model.trained_model_path, pretrained_model), model)
        _backup_pretrained_model(db_model, pretrained_model)
        _delete_old_models(db_model, pretrained_model)

    # delete layer visualization cache
    for f in os.listdir(db_model.trained_model_path):
        if os.path.isdir(os.path.join(db_model.trained_model_path, f)):
            try:
                shutil.rmtree(os.path.join(db_model.trained_model_path, f))
            except Exception as e:
                logger.exception('Could not remove visualization cache. {0}'.format(e))
                raise e

    if db_model.gpu >= 0:
        cuda.get_device(db_model.gpu).use()
        model.to_gpu()

    optimizer = optimizers.MomentumSGD(lr=0.01, momentum=0.9)
    optimizer.setup(model)

    data_q = queue.Queue(maxsize=1)
    res_q = queue.Queue()

    db_model.is_trained = 1
    db_model.update_and_commit()

    remove_resume_file(db_model.trained_model_path)

    # Invoke threads
    feeder = threading.Thread(
        target=feed_data,
        args=(
            train_list,
            val_list,
            mean_image,
            db_model.batchsize,
            val_batchsize,
            model,
            loaderjob,
            db_model.epoch,
            optimizer,
            data_q,
            avoid_flipping,
        )
    )
    feeder.daemon = True
    feeder.start()
    train_logger = threading.Thread(
        target=log_result,
        args=(
            db_model.batchsize,
            val_batchsize,
            os.path.join(db_model.trained_model_path, 'line_graph.tsv'),
            os.path.join(db_model.trained_model_path, 'train.log'),
            res_q
        )
    )
    train_logger.daemon = True
    train_logger.start()
    train_loop(
        model,
        db_model.trained_model_path,
        xp,
        optimizer,
        res_q,
        data_q,
        pretrained_model,
        interruptable
    )
    feeder.join()
    train_logger.join()

    # post-processing
    _post_process(db_model, pretrained_model)
    interruptable.clear_interrupt()
    interruptable.terminate()
    logger.info('Finish imagenet train. model_id: {0}'.format(db_model.id))
Example #45
0
 def init(self):
     if self.use_gpu:
         cuda.check_cuda_available()
         cuda.get_device(self.gpu_id).use()
Example #46
0
def setup_cuda(gpu):
    if gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(gpu).use()
    global xp
    xp = cuda.cupy if gpu >= 0 else np
Example #47
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='SER example: MLP')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer',
                        type=int,
                        default=1,
                        help='number of layes for turn')
    parser.add_argument('--unit',
                        type=int,
                        default=500,
                        help='number of units for turn')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=100,
                        help='number of images in each mini-batch')
    parser.add_argument('--dropout',
                        '-d',
                        type=float,
                        default=0.4,
                        help='value of dropout rate')
    parser.add_argument('--weightdecay',
                        default=0.001,
                        type=float,
                        help='value of weight decay rate')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=100,
                        help='number of sweeps over the dataset to train')
    parser.add_argument('--train',
                        default='datasets/signate/smile/train.txt',
                        type=str,
                        help='training file (.txt)')
    parser.add_argument('--valid',
                        default='datasets/signate/smile/valid.txt',
                        type=str,
                        help='validation file (.txt)')
    parser.add_argument('--out',
                        '-o',
                        default='result-mlp_cw',
                        help='directory to output the result')
    parser.add_argument('--noplot',
                        action='store_true',
                        help='disable PlotReport extension')
    parser.add_argument('--optim',
                        default='adam',
                        choices=['adam', 'adadelta'],
                        help='type of optimizer')
    parser.add_argument('--cw',
                        default='none',
                        choices=['none', 'sum', 'norm'],
                        help='type of class weight')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    model_dir = args.out
    if not os.path.exists(model_dir):
        os.mkdir(model_dir)

    # データの読み込み
    labels = {'neu': 0, 'ang': 1, 'sad': 2, 'hap': 3}
    # labels = {'MA_CH': 0, 'FE_AD': 1, 'MA_AD': 2, 'FE_EL': 3, 'FE_CH': 4, 'MA_EL': 5}

    X_train, y_train, labels = load_data(args.train, labels=labels)
    X_eval, y_eval, labels = load_data(args.valid, labels=labels)

    n_class = len(labels)

    print('# train X: {}, y: {}, dim: {}, counts: {}'.format(
        len(X_train), len(y_train), len(X_train[0]),
        [y_train.count(x) for x in sorted(labels.values())]))
    print('# eval  X: {}, y: {}, dim: {}, counts: {}'.format(
        len(X_eval), len(y_eval), 1,
        [y_eval.count(x) for x in sorted(labels.values())]))
    print('# class: {}, labels: {}'.format(n_class, labels))

    class_weight = None
    class_count = np.array([y_train.count(x) for x in sorted(labels.values())],
                           'f')
    if args.cw == 'sum':
        class_weight = np.sum(class_count) / class_count
    elif args.cw == 'norm':
        class_weight = np.sum(class_count) / class_count
        class_weight = class_weight / np.max(class_weight)
    print('# class_weight: {}'.format(class_weight))
    sys.stdout.flush()

    if args.gpu >= 0:
        class_weight = cuda.to_gpu(class_weight)

    with open(os.path.join(args.out, 'labels.pkl'), 'wb') as f:
        pickle.dump(labels, f)

    model = SER(n_layers=args.layer,
                n_outputs=n_class,
                n_units=args.unit,
                dropout_rate=args.dropout,
                class_weight=class_weight)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 学習率
    lr = 0.001

    # 学習率の減衰
    lr_decay = 0.99

    # 勾配上限
    grad_clip = 3

    # Setup optimizer (Optimizer の設定)
    if args.optim == 'adam':
        optimizer = chainer.optimizers.Adam(alpha=lr,
                                            beta1=0.9,
                                            beta2=0.999,
                                            weight_decay_rate=args.weightdecay,
                                            eps=1e-8)
    elif args.optim == 'adadelta':
        optimizer = chainer.optimizers.AdaDelta()
    else:
        raise ValueError("Only support adam or adadelta.")

    # optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
    optimizer.setup(model)

    # プロット用に実行結果を保存する
    train_loss = []
    train_accuracy1 = []
    train_accuracy2 = []
    test_loss = []
    test_accuracy1 = []
    test_accuracy2 = []

    min_loss = float('inf')
    best_accuracy = .0

    start_at = time.time()
    cur_at = start_at

    # Learning loop
    for epoch in range(1, args.epoch + 1):

        # training
        train_iter = batch_iter([(x, t) for x, t in zip(X_train, y_train)],
                                args.batchsize,
                                shuffle=True)
        sum_train_loss = 0.
        sum_train_accuracy1 = 0.
        sum_train_accuracy2 = 0.
        K = 0

        for X, t in train_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            t = to_device(args.gpu, np.asarray(t, 'i'))

            # 勾配を初期化
            model.cleargrads()

            # 順伝播させて誤差と精度を算出
            loss, accuracy = model(x, t)
            sum_train_loss += float(loss.data) * len(t)
            sum_train_accuracy1 += float(accuracy.data) * len(t)
            sum_train_accuracy2 += .0
            K += len(t)

            # 誤差逆伝播で勾配を計算
            loss.backward()
            optimizer.update()

        # 訓練データの誤差と,正解精度を表示
        mean_train_loss = sum_train_loss / K
        mean_train_accuracy1 = sum_train_accuracy1 / K
        mean_train_accuracy2 = sum_train_accuracy2 / K
        train_loss.append(mean_train_loss)
        train_accuracy1.append(mean_train_accuracy1)
        train_accuracy2.append(mean_train_accuracy2)
        now = time.time()
        train_throughput = now - cur_at
        cur_at = now

        # evaluation
        test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)],
                               args.batchsize,
                               shuffle=False)
        sum_test_loss = 0.
        sum_test_accuracy1 = 0.
        sum_test_accuracy2 = 0.
        K = 0

        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            y_true = []
            y_pred = []
            for X, t in test_iter:
                x = to_device(args.gpu, np.asarray(X, 'f'))
                t = to_device(args.gpu, np.asarray(t, 'i'))

                # 順伝播させて誤差と精度を算出
                loss, accuracy = model(x, t)
                sum_test_loss += float(loss.data) * len(t)
                sum_test_accuracy1 += float(accuracy.data) * len(t)
                sum_test_accuracy2 += .0
                K += len(t)

                y = model.predict(x)
                y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
                y_true += t.tolist()

        cm = confusion_matrix(y_true, y_pred)
        cm2 = cm / np.sum(cm, axis=1)
        uar = np.mean(np.diag(cm2))
        sum_test_accuracy2 += uar * K

        # テストデータでの誤差と正解精度を表示
        mean_test_loss = sum_test_loss / K
        mean_test_accuracy1 = sum_test_accuracy1 / K
        mean_test_accuracy2 = sum_test_accuracy2 / K
        test_loss.append(mean_test_loss)
        test_accuracy1.append(mean_test_accuracy1)
        test_accuracy2.append(mean_test_accuracy2)
        now = time.time()
        test_throughput = now - cur_at
        cur_at = now

        logger.info(
            ''
            '[{:>3d}] '
            'T/loss={:.6f} '
            'T/acc1={:.6f} '
            'T/acc2={:.6f} '
            'T/sec= {:.6f} '
            'D/loss={:.6f} '
            'D/acc1={:.6f} '
            'D/acc2={:.6f} '
            'D/sec= {:.6f} '
            'rate={:.6f}'
            ''.format(
                epoch, mean_train_loss, mean_train_accuracy1,
                mean_train_accuracy2, train_throughput, mean_test_loss,
                mean_test_accuracy1, mean_test_accuracy2, test_throughput,
                optimizer.rho if args.optim == 'adadelta' else optimizer.lr))
        sys.stdout.flush()

        # model と optimizer を保存する
        if args.gpu >= 0: model.to_cpu()
        if mean_test_loss < min_loss:
            min_loss = mean_test_loss
            print(
                'saving early-stopped model (loss) at epoch {}'.format(epoch))
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped-loss.model'), model)
        if mean_test_accuracy2 > best_accuracy:
            best_accuracy = mean_test_accuracy2
            print('saving early-stopped model (uar) at epoch {}'.format(epoch))
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped-uar.model'), model)
        # print('saving final model at epoch {}'.format(epoch))
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.model'),
                                     model)
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.state'),
                                     optimizer)
        if args.gpu >= 0: model.to_gpu()
        sys.stdout.flush()

        # if args.optim == 'adam':
        #     optimizer.alpha *= lr_decay

        # 精度と誤差をグラフ描画
        if not args.noplot:
            ylim1 = [min(train_loss + test_loss), max(train_loss + test_loss)]
            # ylim2 = [min(train_accuracy1 + test_accuracy1 + train_accuracy2 + test_accuracy2), max(train_accuracy1 + test_accuracy1 + train_accuracy2 + test_accuracy2)]
            ylim2 = [0., 1.]

            # グラフ左
            plt.figure(figsize=(10, 10))

            plt.subplot(1, 2, 1)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(train_loss) + 1),
                     train_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            plt.ylabel('loss')
            plt.legend(['train loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(train_accuracy1) + 1),
                     train_accuracy1,
                     color='C0',
                     marker='x')
            # plt.plot(range(1, len(train_accuracy2) + 1), train_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            # plt.ylabel('accuracy')
            plt.legend(['train acc'], loc="upper right")
            plt.title('Loss and accuracy of train.')

            # グラフ右
            plt.subplot(1, 2, 2)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(test_loss) + 1),
                     test_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            # plt.ylabel('loss')
            plt.legend(['test loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(test_accuracy1) + 1),
                     test_accuracy1,
                     color='C0',
                     marker='x')
            plt.plot(range(1,
                           len(test_accuracy2) + 1),
                     test_accuracy2,
                     color='C2',
                     marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            plt.ylabel('accuracy')
            plt.legend(['test acc', 'test uar'], loc="upper right")
            plt.title('Loss and accuracy of test.')

            plt.savefig('{}.png'.format(args.out))
            # plt.savefig('{}-train.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        cur_at = now

    index2label = {v: k for k, v in labels.items()}
    sorted_labels = [
        k for k, _ in sorted(labels.items(), key=lambda x: x[1], reverse=False)
    ]

    # test (early_stopped model by loss)
    chainer.serializers.load_npz(
        os.path.join(model_dir, 'early_stopped-loss.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)],
                           args.batchsize,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (early_stopped-loss) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (early_stopped-loss) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label,
                                  "\t".join(map(lambda x: "%.2f" % x,
                                                counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j,
                             i,
                             "{:.2f}".format(cm2[i, j]),
                             horizontalalignment="center",
                             color="white"
                             if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-early_stopped-loss.png'.format(args.out))
            # plt.savefig('{}-train_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (early_stopped-loss) ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()

    # test (early_stopped model by uar)
    chainer.serializers.load_npz(
        os.path.join(model_dir, 'early_stopped-uar.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)],
                           args.batchsize,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (early_stopped-uar) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (early_stopped-uar) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label,
                                  "\t".join(map(lambda x: "%.2f" % x,
                                                counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j,
                             i,
                             "{:.2f}".format(cm2[i, j]),
                             horizontalalignment="center",
                             color="white"
                             if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-early_stopped-uar.png'.format(args.out))
            # plt.savefig('{}-train_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (early_stopped-uar) ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()

    # test (final model)
    chainer.serializers.load_npz(os.path.join(model_dir, 'final.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)],
                           args.batchsize,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (final model) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (final model) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label,
                                  "\t".join(map(lambda x: "%.2f" % x,
                                                counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j,
                             i,
                             "{:.2f}".format(cm2[i, j]),
                             horizontalalignment="center",
                             color="white"
                             if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-final.png'.format(args.out))
            # plt.savefig('{}-cm-final.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (final model) ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()
Example #48
0
def main():
    global xp

    import argparse
    parser = argparse.ArgumentParser(description='Chainer example: seq2seq')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer',
                        type=int,
                        default=1,
                        help='Number of layes for turn')
    parser.add_argument('--unit',
                        type=int,
                        default=256,
                        help='Number of units for turn')
    parser.add_argument('--dim',
                        type=int,
                        default=384,
                        help='Number of dimensions')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=10,
                        help='Number of images in each mini-batch')
    parser.add_argument('--test',
                        default='test.tsv',
                        type=str,
                        help='evaluating file (.txt)')
    parser.add_argument('--model',
                        default='final.model',
                        type=str,
                        help='model file (.model)')
    parser.add_argument('--label',
                        default='label.pkl',
                        type=str,
                        help='label file (.pkl)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--noplot',
                        dest='plot',
                        action='store_true',
                        help='Disable PlotReport extension')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    xp = cuda.cupy if args.gpu >= 0 else np

    # データの読み込み
    labels = pickle.load(open(args.label, 'rb'))
    X_test, y_test, z_test, labels = load_data(args.test, labels=labels)

    n_class = len(labels)
    index2label = {v: k for k, v in labels.items()}

    print('# test X: {}, y: {}, class: {}'.format(len(X_test), len(y_test),
                                                  n_class))
    sys.stdout.flush()

    model = SER(args.dim, args.layer, args.unit, n_class)
    chainer.serializers.load_npz(args.model, model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_tuple([(s, t1, t2)
                             for s, t1, t2 in zip(X_test, y_test, z_test)],
                            args.batchsize)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):

        trues = []
        preds = []
        probs = []

        for X, y, z in test_iter:
            y_pred = model.predict(X)

            for pred in y_pred:
                pred = cuda.to_cpu(pred.data)
                idx = np.argmax(pred, axis=1)

                preds.append([index2label[x] for x in idx])
                probs.append([float(p[i]) for i, p in zip(idx, pred)])

                for i, j in zip(preds[-1], probs[-1]):
                    print("{}:{:.4f}".format(i, j), end='\t')
                print()

            for i in range(len(y)):
                trues.append([index2label[x] for x in cuda.to_cpu(y[i].data)])

        sorted_labels = [
            k for k, _ in sorted(
                labels.items(), key=lambda x: x[1], reverse=False)
        ]

        print("\n==== Confusion matrix ====\n")
        cm = confusion_matrix([inner for outer in trues for inner in outer],
                              [inner for outer in preds for inner in outer],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        # グラフ描画
        if not args.plot:
            # plt.figure(figsize=(10, 10))
            plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm.shape[0]):
                for j in range(cm.shape[1]):
                    plt.text(
                        j,
                        i,
                        "{}".format(cm[i, j]),
                        horizontalalignment="center",
                        color="white" if cm[i, j] > cm.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-test_cm.png'.format(args.out))
            # plt.savefig('{}-test_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()

        print("\n==== Classification report ====\n")
        print(
            classification_report(
                [inner for outer in trues for inner in outer],
                [inner for outer in preds for inner in outer],
                labels=sorted_labels))
Example #49
0
def main(args):
    if args.gpu >= 0:
        cuda.check_cuda_available()
    xp = cuda.cupy if args.gpu >= 0 else np

    model_id = build_model_id(args)
    model_path = build_model_path(args, model_id)
    setup_model_dir(args, model_path)
    sys.stdout, sys.stderr = setup_logging(args)

    x_train, y_train = load_model_data(args.train_file,
            args.data_name, args.target_name,
            n=args.n_train)
    x_validation, y_validation = load_model_data(
            args.validation_file,
            args.data_name, args.target_name,
            n=args.n_validation)

    rng = np.random.RandomState(args.seed)

    N = len(x_train)
    N_validation = len(x_validation)

    n_classes = max(np.unique(y_train)) + 1
    json_cfg = load_model_json(args, x_train, n_classes)

    print('args.model_dir', args.model_dir)
    sys.path.append(args.model_dir)
    from model import Model
    model_cfg = ModelConfig(**json_cfg)
    model = Model(model_cfg)
    setattr(model, 'stop_training', False)
    
    if args.gpu >= 0:
        cuda.get_device(args.gpu).use()
        model.to_gpu()
    
    best_accuracy = 0.
    best_epoch = 0
    
    def keep_training(epoch, best_epoch):
        if model_cfg.n_epochs is not None and epoch > model_cfg.n_epochs:
                return False
        if epoch > 1 and epoch - best_epoch > model_cfg.patience:
            return False
        return True
    
    epoch = 1
    
    while True:
        if not keep_training(epoch, best_epoch):
            break
    
        if args.shuffle:
            perm = np.random.permutation(N)
        else:
            perm = np.arange(N)
    
        sum_accuracy = 0
        sum_loss = 0

        pbar = progressbar.ProgressBar(term_width=40,
            widgets=[' ', progressbar.Percentage(),
            ' ', progressbar.ETA()],
            maxval=N).start()

        for j, i in enumerate(six.moves.range(0, N, model_cfg.batch_size)):
            pbar.update(j+1)
            x_batch = xp.asarray(x_train[perm[i:i + model_cfg.batch_size]].flatten())
            y_batch = xp.asarray(y_train[perm[i:i + model_cfg.batch_size]])
            pred, loss, acc = model.fit(x_batch, y_batch)
            sum_loss += float(loss.data) * len(y_batch)
            sum_accuracy += float(acc.data) * len(y_batch)

        pbar.finish()
        print('train epoch={}, mean loss={}, accuracy={}'.format(
            epoch, sum_loss / N, sum_accuracy / N))
    
        # Validation set evaluation
        sum_accuracy = 0
        sum_loss = 0

        pbar = progressbar.ProgressBar(term_width=40,
            widgets=[' ', progressbar.Percentage(),
            ' ', progressbar.ETA()],
            maxval=N_validation).start()

        for i in six.moves.range(0, N_validation, model_cfg.batch_size):
            pbar.update(i+1)
            x_batch = xp.asarray(x_validation[i:i + model_cfg.batch_size].flatten())
            y_batch = xp.asarray(y_validation[i:i + model_cfg.batch_size])
            pred, loss, acc = model.predict(x_batch, target=y_batch)
            sum_loss += float(loss.data) * len(y_batch)
            sum_accuracy += float(acc.data) * len(y_batch)

        pbar.finish()
        validation_accuracy = sum_accuracy / N_validation
        validation_loss = sum_loss / N_validation
    
        if validation_accuracy > best_accuracy:
            best_accuracy = validation_accuracy
            best_epoch = epoch
            if model_path is not None:
                if args.gpu >= 0:
                    model.to_cpu()
                store = {
                        'args': args,
                        'model': model,
                    }
                cPickle.dump(store, open(model_path + '.store', 'w'))
                if args.gpu >= 0:
                    model.to_gpu()
    
        print('validation epoch={}, mean loss={}, accuracy={} best=[accuracy={} epoch={}]'.format(
            epoch, validation_loss, validation_accuracy, 
            best_accuracy,
            best_epoch))
    
        epoch += 1
Example #50
0
def train():
    if args.color == 'y':
        ch = 1
        weight = (1.0,)
    elif args.color == 'rgb':
        ch = 3
        weight = (0.29891 * 3, 0.58661 * 3, 0.11448 * 3)
    weight = np.array(weight, dtype=np.float32)
    weight = weight[:, np.newaxis, np.newaxis]

    print('* loading datalist...', end=' ')
    datalist = utils.load_datalist(args.dataset_dir, shuffle=True)
    valid_num = int(np.ceil(args.validation_rate * len(datalist)))
    valid_list, train_list = datalist[:valid_num], datalist[valid_num:]
    print('done')

    print('* loading model...', end=' ')
    if args.model_name is None:
        if args.method == 'noise':
            model_name = ('anime_style_noise%d_%s'
                          % (args.noise_level, args.color))
        elif args.method == 'scale':
            model_name = 'anime_style_scale_%s' % args.color
        elif args.method == 'noise_scale':
            model_name = ('anime_style_noise%d_scale_%s'
                          % (args.noise_level, args.color))
    else:
        model_name = args.model_name.rstrip('.npz')
    model_path = model_name + '.npz'
    if not os.path.exists('epoch'):
        os.makedirs('epoch')

    model = srcnn.archs[args.arch](ch)
    if args.finetune is not None:
        chainer.serializers.load_npz(args.finetune, model)

    if args.gpu >= 0:
        cuda.check_cuda_available()
        cuda.get_device(args.gpu).use()
        weight = cuda.cupy.array(weight)
        model.to_gpu()

    optimizer = optimizers.Adam(alpha=args.learning_rate)
    optimizer.setup(model)
    print('done')

    valid_config = get_config(args, model, train=False)
    train_config = get_config(args, model, train=True)

    print('* starting processes of dataset sampler...', end=' ')
    valid_queue = DatasetSampler(valid_list, valid_config)
    train_queue = DatasetSampler(train_list, train_config)
    print('done')

    best_count = 0
    best_score = 0
    best_loss = np.inf
    for epoch in range(0, args.epoch):
        print('### epoch: %d ###' % epoch)
        train_queue.reload_switch(init=(epoch < args.epoch - 1))
        for inner_epoch in range(0, args.inner_epoch):
            best_count += 1
            print('  # inner epoch: %d' % inner_epoch)
            start = time.time()
            train_loss = train_inner_epoch(
                model, weight, optimizer, train_queue, args.batch_size)
            train_queue.wait()
            if train_loss < best_loss:
                best_loss = train_loss
                print('    * best loss on train dataset: %f' % train_loss)
            valid_score = valid_inner_epoch(
                model, valid_queue, args.batch_size)
            if valid_score > best_score:
                best_count = 0
                best_score = valid_score
                print('    * best score on validation dataset: PSNR %f dB'
                      % valid_score)
                best_model = model.copy().to_cpu()
                epoch_path = 'epoch/%s_epoch%d.npz' % (model_name, epoch)
                chainer.serializers.save_npz(model_path, best_model)
                shutil.copy(model_path, epoch_path)
            if best_count >= args.lr_decay_interval:
                best_count = 0
                optimizer.alpha *= args.lr_decay
                if optimizer.alpha < args.lr_min:
                    optimizer.alpha = args.lr_min
                else:
                    print('    * learning rate decay: %f' % optimizer.alpha)
            print('    * elapsed time: %f sec' % (time.time() - start))
Example #51
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu',
                        '-g',
                        default=-1,
                        type=int,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--unit',
                        '-u',
                        default=100,
                        type=int,
                        help='number of units')
    parser.add_argument('--window',
                        '-w',
                        default=5,
                        type=int,
                        help='window size')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=1000,
                        help='learning minibatch size')
    parser.add_argument('--epoch',
                        '-e',
                        default=20,
                        type=int,
                        help='number of epochs to learn')
    parser.add_argument('--model',
                        '-m',
                        choices=['skipgram', 'cbow'],
                        default='skipgram',
                        help='model type ("skipgram", "cbow")')
    parser.add_argument('--negative-size',
                        default=5,
                        type=int,
                        help='number of negative samples')
    parser.add_argument('--out-type',
                        '-o',
                        choices=['hsm', 'ns', 'original'],
                        default='hsm',
                        help='output model type ("hsm": hierarchical softmax, '
                        '"ns": negative sampling, "original": '
                        'no approximation)')
    parser.add_argument('--out',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--test', dest='test', action='store_true')
    parser.set_defaults(test=False)
    args = parser.parse_args()

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()

    print('GPU: {}'.format(args.gpu))
    print('# unit: {}'.format(args.unit))
    print('Window: {}'.format(args.window))
    print('Minibatch-size: {}'.format(args.batchsize))
    print('# epoch: {}'.format(args.epoch))
    print('Training model: {}'.format(args.model))
    print('Output type: {}'.format(args.out_type))
    print('')

    if args.gpu >= 0:
        cuda.get_device_from_id(args.gpu).use()

    # Load the dataset
    train, val, _ = chainer.datasets.get_ptb_words()
    counts = collections.Counter(train)
    counts.update(collections.Counter(val))
    n_vocab = max(train) + 1

    if args.test:
        train = train[:100]
        val = val[:100]

    vocab = chainer.datasets.get_ptb_words_vocabulary()
    index2word = {wid: word for word, wid in six.iteritems(vocab)}

    print('n_vocab: %d' % n_vocab)
    print('data length: %d' % len(train))

    if args.out_type == 'hsm':
        HSM = L.BinaryHierarchicalSoftmax
        tree = HSM.create_huffman_tree(counts)
        loss_func = HSM(args.unit, tree)
        loss_func.W.data[...] = 0
    elif args.out_type == 'ns':
        cs = [counts[w] for w in range(len(counts))]
        loss_func = L.NegativeSampling(args.unit, cs, args.negative_size)
        loss_func.W.data[...] = 0
    elif args.out_type == 'original':
        loss_func = SoftmaxCrossEntropyLoss(args.unit, n_vocab)
    else:
        raise Exception('Unknown output type: {}'.format(args.out_type))

    # Choose the model
    if args.model == 'skipgram':
        model = SkipGram(n_vocab, args.unit, loss_func)
    elif args.model == 'cbow':
        model = ContinuousBoW(n_vocab, args.unit, loss_func)
    else:
        raise Exception('Unknown model type: {}'.format(args.model))

    if args.gpu >= 0:
        model.to_gpu()

    # Set up an optimizer
    optimizer = O.Adam()
    optimizer.setup(model)

    # Set up an iterator
    train_iter = WindowIterator(train, args.window, args.batchsize)
    val_iter = WindowIterator(val, args.window, args.batchsize, repeat=False)

    # Set up an updater
    updater = training.updaters.StandardUpdater(train_iter,
                                                optimizer,
                                                converter=convert,
                                                device=args.gpu)

    # Set up a trainer
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

    trainer.extend(
        extensions.Evaluator(val_iter,
                             model,
                             converter=convert,
                             device=args.gpu))
    trainer.extend(extensions.LogReport())
    trainer.extend(
        extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss']))
    trainer.extend(extensions.ProgressBar())
    trainer.run()

    # Save the word2vec model
    with open('word2vec.model', 'w') as f:
        f.write('%d %d\n' % (len(index2word), args.unit))
        w = cuda.to_cpu(model.embed.W.data)
        for i, wi in enumerate(w):
            v = ' '.join(map(str, wi))
            f.write('%s %s\n' % (index2word[i], v))
Example #52
0
                    '-p',
                    choices=('on', 'off'),
                    default='off',
                    help='Accuracy plot flag')
parser.add_argument('--resume',
                    '-r',
                    default='',
                    help='Resume the optimization from snapshot')
parser.add_argument('--saveflag',
                    '-s',
                    choices=('on', 'off'),
                    default='off',
                    help='Save model and optimizer flag')
args = parser.parse_args()

if args.gpu >= 0: cuda.check_cuda_available()

# Prepare dataset
print('load cifar-10 dataset')
if args.data == 'on': cifar = dh.process_data(augmentation=args.augmentation)
else: cifar = dh.load_data()

N = len(cifar['train']['x'])
N_test = len(cifar['test']['x'])
print(N, N_test)
batchsize = args.batchsize
n_epoch = args.epoch
assert N % batchsize == 0
assert N_test % batchsize == 0

# Prepare Convolution NN model
Example #53
0
import math
from random import random, randint, uniform

import pickle

parser = argparse.ArgumentParser(description='deep_actor_critic_for_swingup')
parser.add_argument('--gpu',
                    '-g',
                    default=-1,
                    type=int,
                    help='GPU ID (negative value indicates CPU)')
args = parser.parse_args()

if args.gpu >= 0:
    cuda.check_cuda_available()
xp = cuda.cupy if args.gpu >= 0 else np


class agent:
    pi = 3.141592

    ALPHA = 1e-6
    #  ALPHA = 0.001
    GAMMA = 0.9

    wait_flag = True

    filename_critic_model = "/home/amsl/ros_catkin_ws/src/deep_actor_critic/actor_critic_for_swingup/test_results/models/myrrbot5_critic_model.dat"
    filename_actor_model = "/home/amsl/ros_catkin_ws/src/deep_actor_critic/actor_critic_for_swingup/test_results/models/myrrbot5_actor_model.dat"
Example #54
0
log_interval = int(args['--log-interval'])
print "Recording training and testing ELBO every %d batches" % log_interval

# Setup training parameters
batchsize = int(args['--batchsize'])
print "Using a batchsize of %d instances" % batchsize

vae = model.VAE(d, nhidden, nlatent, zcount, nmap)
opt = optimizers.Adam()
opt.setup(vae)
opt.add_hook(chainer.optimizer.GradientClipping(4.0))

# Move to GPU
gpu_id = int(args['--device'])
if gpu_id >= 0:
    cuda.check_cuda_available(
    )  # comment out to surpress an unncessarry warning
if gpu_id >= 0:
    xp = cuda.cupy
    vae.to_gpu(gpu_id)
else:
    xp = np

start_at = time.time()
period_start_at = start_at
period_bi = 0
runtime = int(args['--runtime'])

print_every_s = float(args['--time-print'])
print_at = start_at + print_every_s

sample_every_s = float(args['--time-sample'])
Example #55
0
def main():
    global root_dir

    import argparse
    parser = argparse.ArgumentParser(description='SER example: 3-D ACRNN')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer',
                        type=int,
                        default=1,
                        help='number of layes for turn')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=40,
                        help='number of images in each mini-batch')
    parser.add_argument('--dropout',
                        '-d',
                        type=float,
                        default=0.1,
                        help='value of dropout rate')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=300,
                        help='number of sweeps over the dataset to train')
    parser.add_argument('--train',
                        default='datasets/01-train.txt',
                        type=str,
                        help='training file (.txt)')
    parser.add_argument('--valid',
                        default='datasets/01-valid.txt',
                        type=str,
                        help='validating file (.txt)')
    parser.add_argument('--test',
                        default='datasets/01-test.txt',
                        type=str,
                        help='evaluating file (.txt)')
    parser.add_argument('--out',
                        '-o',
                        default='iemocap-fbank-21-300_b040_e300_d010_adam',
                        help='directory to output the result')
    parser.add_argument('--noplot',
                        dest='noplot',
                        action='store_true',
                        help='disable PlotReport extension')
    parser.add_argument('--optim',
                        default='adam',
                        choices=['adam', 'adadelta'],
                        help='type of optimizer')
    parser.add_argument('--type',
                        default='fbank',
                        choices=['fbank', 'mfcc'],
                        help='type of feature')
    parser.add_argument('--resume',
                        default='',
                        type=str,
                        help='path to resume models')
    parser.add_argument('--start_epoch',
                        default=1,
                        type=int,
                        help='epoch number at start')
    parser.add_argument('--datasets_rootdir',
                        default='',
                        type=str,
                        help='path to datasets')
    parser.add_argument('--datasets_only',
                        action='store_true',
                        help='make and save datasets only')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    model_dir = args.out
    if not os.path.exists(model_dir):
        os.mkdir(model_dir)

    root_dir = args.datasets_rootdir

    # データの読み込み
    labels = {'neu': 0, 'ang': 1, 'sad': 2, 'hap': 3}

    if args.type == 'fbank':
        X_train, y_train, labels = load_and_fbank(args.train,
                                                  labels=labels,
                                                  train=True)
        X_valid, y_valid, labels = load_and_fbank(args.valid, labels=labels)
        X_test, y_test, labels = load_and_fbank(args.test, labels=labels)
    elif args.type == 'mfcc':
        X_train, y_train, labels = load_and_mfcc(args.train,
                                                 labels=labels,
                                                 train=True)
        X_valid, y_valid, labels = load_and_mfcc(args.valid, labels=labels)
        X_test, y_test, labels = load_and_mfcc(args.test, labels=labels)
    else:
        raise ValueError("Only support fbank or mfcc.")

    X_train_section = np.cumsum([len(x) for x in X_train][:-1])
    X_train = np.concatenate(X_train, axis=0)

    X_valid_section = np.cumsum([len(x) for x in X_valid][:-1])
    X_valid = np.concatenate(X_valid, axis=0)

    eps = 1e-5
    tensor = np.concatenate((X_train, X_valid), axis=0)
    mean = np.mean(tensor, axis=0)
    std = np.std(tensor, axis=0)
    with open(os.path.join(args.out, 'mean-std.pkl'), 'wb') as f:
        pickle.dump((mean, std), f)

    X_train = (X_train - mean) / (std + eps)
    X_train = np.split(X_train, X_train_section, 0)

    X_valid = (X_valid - mean) / (std + eps)
    X_valid = np.split(X_valid, X_valid_section, 0)

    with open(os.path.join(args.out, "labels.pkl"), 'wb') as f:
        pickle.dump(labels, f)
    with bz2.BZ2File(os.path.join(args.out, "data.pkl.bz2"), 'wb') as f:
        pickle.dump((X_train, y_train, X_valid, y_valid, X_test, y_test), f)

    with bz2.BZ2File(os.path.join(args.out, "data.pkl.bz2"), 'rb') as f:
        (X_train, y_train, X_valid, y_valid, X_test, y_test) = pickle.load(f)
    with open(os.path.join(args.out, "labels.pkl"), 'rb') as f:
        labels = pickle.load(f)

    n_class = len(labels)

    print('# train X: {}, y: {}'.format(len(X_train), len(y_train)))
    print('# valid X: {}, y: {}'.format(len(X_valid), len(y_valid)))
    print('# test  X: {}, y: {}'.format(len(X_test), len(y_test)))
    print('# class: {}'.format(n_class))
    print(labels)
    sys.stdout.flush()

    if args.datasets_only:
        return

    model = SER(n_layers=args.layer,
                n_inputs=3,
                n_outputs=n_class,
                dropout_rate=args.dropout)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 学習率
    lr = 0.001

    # 学習率の減衰
    lr_decay = 0.99

    # 重み減衰
    decay = 0.0001

    # 勾配上限
    grad_clip = 3

    # Setup optimizer (Optimizer の設定)
    if args.optim == 'adam':
        optimizer = chainer.optimizers.Adam(alpha=lr,
                                            beta1=0.9,
                                            beta2=0.999,
                                            weight_decay_rate=decay,
                                            eps=1e-8)
    elif args.optim == 'adadelta':
        optimizer = chainer.optimizers.AdaDelta()
    else:
        raise ValueError("Only support adam or adadelta.")

    # optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
    optimizer.setup(model)

    if args.resume:
        print('resuming model and state at: {}'.format(args.start_epoch))
        if args.gpu >= 0: model.to_cpu()
        chainer.serializers.load_npz(os.path.join(args.resume, 'final.model'),
                                     model)
        chainer.serializers.load_npz(os.path.join(args.resume, 'final.state'),
                                     optimizer)
        if args.gpu >= 0: model.to_gpu()
        sys.stdout.flush()

    # プロット用に実行結果を保存する
    train_loss = []
    train_accuracy1 = []
    train_accuracy2 = []
    test_loss = []
    test_accuracy1 = []
    test_accuracy2 = []

    min_loss = float('inf')
    min_epoch = 0

    start_epoch = args.start_epoch

    start_at = time.time()
    cur_at = start_at

    # Learning loop
    for epoch in range(start_epoch, args.epoch + 1):

        # training
        train_iter = batch_iter([(x, t) for x, t in zip(X_train, y_train)],
                                args.batchsize,
                                shuffle=True,
                                random_state=seed)
        sum_train_loss = 0.
        sum_train_accuracy1 = 0.
        sum_train_accuracy2 = 0.
        K = 0

        for X, t in train_iter:
            x = to_device(args.gpu, X)
            t = to_device(args.gpu, t)

            # 勾配を初期化
            model.cleargrads()

            # 順伝播させて誤差と精度を算出
            loss, accuracy = model(x, t)
            sum_train_loss += float(loss.data) * len(t)
            sum_train_accuracy1 += float(accuracy.data) * len(t)
            sum_train_accuracy2 += .0
            K += len(t)

            # 誤差逆伝播で勾配を計算
            loss.backward()
            optimizer.update()

        # 訓練データの誤差と,正解精度を表示
        mean_train_loss = sum_train_loss / K
        mean_train_accuracy1 = sum_train_accuracy1 / K
        mean_train_accuracy2 = sum_train_accuracy2 / K
        train_loss.append(mean_train_loss)
        train_accuracy1.append(mean_train_accuracy1)
        train_accuracy2.append(mean_train_accuracy2)
        now = time.time()
        train_throughput = now - cur_at
        cur_at = now

        # evaluation
        valid_iter = batch_iter([(x, t) for x, t in zip(X_valid, y_valid)],
                                1,
                                shuffle=False)
        sum_test_loss = 0.
        sum_test_accuracy1 = 0.
        sum_test_accuracy2 = 0.
        K = 0

        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            for X, t in valid_iter:
                x = to_device(args.gpu, X)
                t = to_device(args.gpu, t)

                # 順伝播させて誤差と精度を算出
                loss, accuracy = model(x, t)
                sum_test_loss += float(loss.data) * len(t)
                sum_test_accuracy1 += float(accuracy.data) * len(t)
                sum_test_accuracy2 += .0
                K += len(t)

        # テストデータでの誤差と正解精度を表示
        mean_test_loss = sum_test_loss / K
        mean_test_accuracy1 = sum_test_accuracy1 / K
        mean_test_accuracy2 = sum_test_accuracy2 / K
        test_loss.append(mean_test_loss)
        test_accuracy1.append(mean_test_accuracy1)
        test_accuracy2.append(mean_test_accuracy2)
        now = time.time()
        test_throughput = now - cur_at
        cur_at = now

        logger.info(
            ''
            '[{:>3d}] '
            'T/loss={:.6f} '
            'T/acc1={:.6f} '
            'T/acc2={:.6f} '
            'T/sec= {:.6f} '
            'V/loss={:.6f} '
            'V/acc1={:.6f} '
            'V/acc2={:.6f} '
            'V/sec= {:.6f} '
            'rate={:.6f}'
            ''.format(
                epoch, mean_train_loss, mean_train_accuracy1,
                mean_train_accuracy2, train_throughput, mean_test_loss,
                mean_test_accuracy1, mean_test_accuracy2, test_throughput,
                optimizer.rho if args.optim == 'adadelta' else optimizer.lr))
        sys.stdout.flush()

        # model と optimizer を保存する
        if args.gpu >= 0: model.to_cpu()
        if mean_test_loss < min_loss:
            min_loss = mean_test_loss
            min_epoch = epoch
            print('saving early-stopped model at epoch {}'.format(min_epoch))
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped.model'), model)
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped.state'), optimizer)
        # print('saving final model at epoch {}'.format(epoch))
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.model'),
                                     model)
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.state'),
                                     optimizer)
        if args.gpu >= 0: model.to_gpu()
        sys.stdout.flush()

        # if args.optim == 'adam':
        #     optimizer.alpha *= lr_decay

        # 精度と誤差をグラフ描画
        if not args.noplot:
            ylim1 = [min(train_loss + test_loss), max(train_loss + test_loss)]
            # ylim2 = [min(train_accuracy1 + test_accuracy2), max(train_accuracy1 + test_accuracy2)]
            ylim2 = [0., 1.]

            # グラフ左
            plt.figure(figsize=(10, 10))

            plt.subplot(1, 2, 1)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(train_loss) + 1),
                     train_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            plt.ylabel('loss')
            plt.legend(['train loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(train_accuracy1) + 1),
                     train_accuracy1,
                     color='C0',
                     marker='x')
            # plt.plot(range(1, len(train_accuracy2) + 1), train_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            # plt.ylabel('accuracy')
            plt.legend(['train acc1', 'train acc2'], loc="upper right")
            plt.title('Loss and accuracy of train.')

            # グラフ右
            plt.subplot(1, 2, 2)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(test_loss) + 1),
                     test_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            # plt.ylabel('loss')
            plt.legend(['valid loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(test_accuracy1) + 1),
                     test_accuracy1,
                     color='C0',
                     marker='x')
            # plt.plot(range(1, len(test_accuracy2) + 1), test_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            plt.ylabel('accuracy')
            plt.legend(['valid acc1', 'valid acc2'], loc="upper right")
            plt.title('Loss and accuracy of valid.')

            plt.savefig('{}.png'.format(args.out))
            # plt.savefig('{}-train.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        cur_at = now

    # test (early_stopped.model)
    chainer.serializers.load_npz(
        os.path.join(model_dir, 'early_stopped.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    from sklearn.metrics import confusion_matrix, classification_report
    test_iter = batch_iter([(x, t) for x, t in zip(X_test, y_test)],
                           1,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += [int(_) for _ in t]

        print("\n==== Confusion matrix (early-stopped model) ====\n")
        index2label = {v: k for k, v in labels.items()}
        sorted_labels = [
            k for k, _ in sorted(
                labels.items(), key=lambda x: x[1], reverse=False)
        ]
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm.shape[0]):
                for j in range(cm.shape[1]):
                    plt.text(
                        j,
                        i,
                        "{}".format(cm[i, j]),
                        horizontalalignment="center",
                        color="white" if cm[i, j] > cm.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-early_stopped.png'.format(args.out))
            # plt.savefig('{}-cm-early_stopped.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (early-stopped model) ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()

    # test (final.model)
    chainer.serializers.load_npz(os.path.join(model_dir, 'final.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    from sklearn.metrics import confusion_matrix, classification_report
    test_iter = batch_iter([(x, t) for x, t in zip(X_test, y_test)],
                           1,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += [int(_) for _ in t]

        print("\n==== Confusion matrix (final model) ====\n")
        index2label = {v: k for k, v in labels.items()}
        sorted_labels = [
            k for k, _ in sorted(
                labels.items(), key=lambda x: x[1], reverse=False)
        ]
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm.shape[0]):
                for j in range(cm.shape[1]):
                    plt.text(
                        j,
                        i,
                        "{}".format(cm[i, j]),
                        horizontalalignment="center",
                        color="white" if cm[i, j] > cm.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-final.png'.format(args.out))
            # plt.savefig('{}-cm-final.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (final model) ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()
Example #56
0
def train(args):
    if args.subword == 'none':
        current_utils = utils.word
    else:
        current_utils = utils.subword
    current_utils.args = args

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()

    if args.path_vocab == '':
        vocab = create_from_path(args.path_corpus, language=args.language)
    else:
        vocab = Vocabulary()
        vocab.load(args.path_vocab)
        logger.info("loaded vocabulary")

    if args.context_representation != 'word':  # for deps or ner context representation, we need a new context vocab for NS or HSM loss function.
        vocab_context = create_from_annotated_dir(args.path_corpus, representation=args.context_representation)
    else:
        vocab_context = vocab

    vocab_ngram_tokens = None
    if args.subword != 'none':
        if args.path_vocab_ngram_tokens == '':
            vocab_ngram_tokens = create_ngram_tokens_from_dir(args.path_corpus, args.min_gram, args.max_gram)
        else:
            vocab_ngram_tokens = Vocabulary()
            vocab_ngram_tokens.load(args.path_vocab_ngram_tokens)

        if args.path_word2chars == '':
            word2chars = None
        else:
            word2chars = get_word2chars(args.path_word2chars)

    loss_func = get_loss_func(args, vocab_context)
    model = get_model(args, loss_func, vocab, vocab_ngram_tokens, current_utils)

    if args.gpu >= 0:
        model.to_gpu()
        logger.debug("model sent to gpu")

    optimizer = chainer.optimizers.Adam()
    optimizer.setup(model)
    save_embeddings(args.path_out, 0, model, vocab, vars(args), 0)

    if os.path.isfile(args.path_corpus):
        # todo for file corpus
        pass
    else:
        if args.subword == 'none':
            train_iter = current_utils.DirWindowIterator(path=args.path_corpus, vocab=vocab, window_size=args.window,
                                                         batch_size=args.batchsize, language=args.language)
        else:
            train_iter = current_utils.DirWindowIterator(path=args.path_corpus, vocab=vocab,
                                                         vocab_ngram_tokens=vocab_ngram_tokens, word2chars=word2chars,
                                                         window_size=args.window, batch_size=args.batchsize,
                                                         language=args.language)
    updater = training.StandardUpdater(train_iter, optimizer, converter=current_utils.convert, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.path_out)

    if os.path.isfile(args.path_corpus):
        # todo for file corpus
        # trainer.extend(extensions.Evaluator(val_iter, model, converter=convert, device=args.gpu))
        # trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']))
        pass
    else:
        trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'elapsed_time']))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(extensions.LogReport())
    trainer.extend(EmbedDumper(vars(args), vocab), trigger=(1, 'epoch'))
    trainer.run()
Example #57
0
def get_activates(args):
    if args.gpu >= 0:
        cuda.check_cuda_available()
        print('use GPU')
    else:
        print('use CPU only')

    xp = cuda.cupy if args.gpu >= 0 else np
    # cuda.cudnn_enabled = False
    # Prepare dataset
    val_list = dataio.load_image_list(args.val, args.root)
    val_size = len(val_list)
    assert val_size % args.val_batchsize == 0

    categories = pd.read_csv(args.categories, header=None)

    assert args.layeractivates is not None
    layeractivates = np.load(args.layeractivates)
    assert layeractivates.shape[0] == val_size
    map_size = layeractivates.shape[1]
    #indexes = np.argsort(a)
    df = pd.DataFrame(layeractivates)
    val_path_list = [v[0] for v in val_list]
    val_label_list = [categories.ix[i[1], 0] for i in val_list]
    df['path'] = val_path_list
    df['label'] = val_label_list

    outdir = './' + args.arch + '/' + args.layer
    os.makedirs(outdir)

    # Prepare model
    model = models.getModel(args.arch)
    if model is None:
        raise ValueError('Invalid architecture name')
    #if args.finetune and hasattr(model, 'load_param'):
    #    print ('finetune')
    #    model.load_param()
    '''mean_image = None
    if hasattr(model, 'getMean'):
        mean_image = model.getMean()
    else:
        #mean_image = pickle.load(open(args.mean, 'rb'))
        mean_image = np.load(args.mean)

    assert mean_image is not None'''

    print(model.__class__.__name__)
    print('batchsize (validation) : ' + str(args.val_batchsize))
    val_image_pool = [None] * args.top
    impool = multiprocessing.Pool(args.loaderjob)

    for c in six.moves.range(map_size):
        #topacts = df.sort(i, ascending=False)[['path', 'label', i]].iloc[:args.top]
        topacts = df.sort_values(by=[c], ascending=False)[['path', 'label',
                                                           c]].iloc[:args.top]
        topacts.to_csv(outdir + '/' + "{0:0>4}".format(i) + '.csv',
                       header=None,
                       index=None)
        images = np.zeros((3, model.insize, model.insize * args.top))
        #for n, path in enumerate(topacts['path']):
        #    images[:,:,n*model.insize:(n+1)*model.insize] = \
        #        dataio.read_image(path, model.insize, None, True, False)

        for n, path in enumerate(topacts['path']):
            val_image_pool[n] = impool.apply_async(
                dataio.read_image, (path, model.insize, None, True, False))
        for n, im in enumerate(val_image_pool):
            images[:, :, n * model.insize:(n + 1) * model.insize] = im.get()

        pilImg = Image.fromarray(np.uint8(images[::-1].transpose(1, 2, 0)))
        pilImg.save(outdir + '/' + "{0:0>4}".format(c) + '.jpg',
                    'JPEG',
                    quality=100,
                    optimize=True)
    impool.close()
    impool.join()
    return
    nowt = datetime.datetime.today()
    #outdir = './results/' + args.arch + '_bs' + str(args.batchsize) + '_' + nowt.strftime("%Y%m%d-%H%M")
    #os.makedirs(outdir)
    #args.out = outdir + '/' + args.out + '_' + args.arch
    #args.outstate = outdir + '/' + args.outstate

    if args.gpu >= 0:
        cuda.get_device(args.gpu).use()
        model.to_gpu()

    # Init/Resume
    if args.initmodel:
        print('Load model from', args.initmodel)
        serializers.load_hdf5(args.initmodel, model)
    if args.resume:
        print('Load optimizer state from', args.resume)
        serializers.load_hdf5(args.resume, optimizer)

    # ------------------------------------------------------------------------------
    # This example consists of three threads: data feeder, logger and trainer.
    # These communicate with each other via Queue.
    data_q = queue.Queue(maxsize=1)
    res_q = queue.Queue()
    ret = TrainResult()

    def feed_data():
        # Data feeder
        i = 0
        count = 0

        val_x_batch = np.ndarray(
            (args.val_batchsize, 3, model.insize, model.insize),
            dtype=np.float32)
        val_y_batch = np.ndarray((args.val_batchsize, ), dtype=np.int32)

        val_batch_pool = [None] * args.val_batchsize
        pool = multiprocessing.Pool(args.loaderjob)
        data_q.put('val')
        j = 0
        for path, label in val_list:
            val_batch_pool[j] = pool.apply_async(
                dataio.read_image,
                (path, model.insize, mean_image, True, False))
            val_y_batch[j] = label
            j += 1

            if j == args.val_batchsize:
                for k, x in enumerate(val_batch_pool):
                    val_x_batch[k] = x.get()
                data_q.put((val_x_batch.copy(), val_y_batch.copy()))
                j = 0
        pool.close()
        pool.join()
        data_q.put('end')

    def log_result():
        # Logger
        testlogfilename = args.out + '/val.log'
        activatescsvfilename = args.out + '/acts_' + args.layer + '.csv'
        activatesnpyfilename = args.out + '/acts_' + args.layer + '.npy'
        train_count = 0
        train_cur_loss = 0
        train_cur_accuracy = 0
        begin_at = time.time()
        val_begin_at = None
        result = None
        Ret = [ret]
        activates = None

        while True:
            result = res_q.get()
            if result == 'end':
                print(file=sys.stderr)
                break
            elif result == 'val':
                print(file=sys.stderr)
                train = False
                val_count = val_loss = val_accuracy = 0
                val_begin_at = time.time()
                continue

            loss, accuracy, max_activates = result
            if activates is None:
                activates = cuda.to_cpu(max_activates)
            else:
                activates = np.r_[activates, cuda.to_cpu(max_activates)]

            val_count += args.val_batchsize
            duration = time.time() - val_begin_at
            throughput = val_count / duration
            sys.stderr.write(
                '\rval   {} batches ({} samples) time: {} ({} images/sec)'.
                format(val_count / args.val_batchsize, val_count,
                       datetime.timedelta(seconds=duration), throughput))

            val_loss += loss
            val_accuracy += accuracy
            #print('accuacy', accuracy)
            if val_count == val_size:
                mean_loss = val_loss * args.val_batchsize / val_size
                mean_error = 1 - val_accuracy * args.val_batchsize / val_size
                print(file=sys.stderr)
                print(
                    json.dumps({
                        'type': 'val',
                        'iteration': train_count,
                        'error': mean_error,
                        'loss': mean_loss
                    }))
                with open(testlogfilename, 'a') as f:
                    f.write(
                        json.dumps({
                            'type': 'val',
                            'iteration': train_count,
                            'error': mean_error,
                            'loss': mean_loss
                        }) + '\n')
                Ret[0].val_loss = mean_loss
                Ret[0].val_error = mean_error
                sys.stdout.flush()
        print(activates.shape)
        np.savetxt(activatescsvfilename, activates, delimiter=",")
        np.save(activatesnpyfilename, activates)
        Ret[0].activates = activates

    def train_loop():
        # Trainer
        while True:
            while data_q.empty():
                time.sleep(0.1)
            inp = data_q.get()
            if inp == 'end':  # quit
                res_q.put('end')
                break
            elif inp == 'val':  # start validation
                res_q.put('val')
                model.train = False
                continue

            model.train = False
            volatile = 'off'  #if model.train else 'on'
            x = chainer.Variable(xp.asarray(inp[0]), volatile=volatile)
            t = chainer.Variable(xp.asarray(inp[1]), volatile=volatile)

            model(x, t)

            #fc8, = model(inputs={'data': x}, outputs=['fc8'], train=False)
            #model.loss = F.softmax_cross_entropy(fc8, t)
            #model.accuracy = F.accuracy(fc8, t)

            #y, = model(inputs={'data': x}, outputs=['loss3/classifier'], disable=['loss1/ave_pool', 'loss2/ave_pool'], train=False)
            #model.loss = F.softmax_cross_entropy(y, t)
            #model.accuracy = F.accuracy(y, t)

            variable = model.getLayerVariableFromLoss(args.layer)
            #print(model.layer2rank(args.layer))
            #print(variable)
            ax = (2, 3) if len(variable.data.shape) == 4 else 1
            max_activates = variable.data.max(axis=ax)
            #max_activates = np.arange(args.val_batchsize * 10).reshape((args.val_batchsize, 10))
            #data = cuda.to_cpu(variable.data)
            #argmax = data.argmax(axis=(1))
            #print(data.shape)
            #print(argmax)

            res_q.put((float(model.loss.data), float(model.accuracy.data),
                       max_activates))
            del x, t,

    # Invoke threads
    feeder = threading.Thread(target=feed_data)
    feeder.daemon = True
    feeder.start()
    logger = threading.Thread(target=log_result)
    logger.daemon = True
    logger.start()

    train_loop()
    feeder.join()
    logger.join()

    # Save final model
    ret.outdir = args.out
    ret.valid = True
    return ret
def main():
    global xp

    import argparse
    parser = argparse.ArgumentParser(description='SER example: NStep BiLSTM')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer',
                        type=int,
                        default=1,
                        help='Number of layes for turn')
    parser.add_argument('--unit',
                        type=int,
                        default=256,
                        help='Number of units for turn')
    parser.add_argument('--dim',
                        type=int,
                        default=384,
                        help='Number of dimensions')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=3,
                        help='Number of images in each mini-batch')
    parser.add_argument('--epoch',
                        '-e',
                        type=int,
                        default=20,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--train',
                        default='train.tsv',
                        type=str,
                        help='training file (.txt)')
    parser.add_argument('--test',
                        default='test.tsv',
                        type=str,
                        help='evaluating file (.txt)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--noplot',
                        dest='plot',
                        action='store_true',
                        help='Disable PlotReport extension')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    xp = cuda.cupy if args.gpu >= 0 else np

    model_dir = args.out
    if not os.path.exists(model_dir):
        os.mkdir(model_dir)

    # データの読み込み
    X_train, y_train, z_train, labels = load_data(args.train)
    X_test, y_test, z_test, labels = load_data(args.test, labels=labels)

    n_class = len(labels)

    print('# train X: {}, y: {}, class: {}'.format(len(X_train), len(y_train),
                                                   len(labels)))
    print('# eval  X: {}, y: {}, class: {}'.format(len(X_test), len(y_test),
                                                   len(labels)))
    print('# class: {}'.format(n_class))
    sys.stdout.flush()

    with open(os.path.join(args.out, 'labels.pkl'), 'wb') as f:
        pickle.dump(labels, f)

    model = SER(args.dim, args.layer, args.unit, n_class)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 重み減衰
    decay = 0.0001

    # 勾配上限
    grad_clip = 3

    # 学習率の減衰
    lr_decay = 0.995

    # Setup optimizer (Optimizer の設定)
    optimizer = chainer.optimizers.Adam(alpha=0.001)
    optimizer.setup(model)
    # optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
    # optimizer.add_hook(chainer.optimizer.WeightDecay(decay))

    # プロット用に実行結果を保存する
    train_loss = []
    train_accuracy1 = []
    train_accuracy2 = []
    test_loss = []
    test_accuracy1 = []
    test_accuracy2 = []

    min_loss = float('inf')
    min_epoch = 0

    start_at = time.time()
    cur_at = start_at

    # Learning loop
    for epoch in range(1, args.epoch + 1):

        # training
        train_iter = batch_tuple(
            [(s, t1, t2) for s, t1, t2 in zip(X_train, y_train, z_train)],
            args.batchsize)
        sum_train_loss = 0.
        sum_train_accuracy1 = 0.
        sum_train_accuracy2 = 0.
        K = 0

        for X, y, z in train_iter:

            # 勾配を初期化
            model.cleargrads()

            # 順伝播させて誤差と精度を算出
            loss, accuracy = model(X, y)
            sum_train_loss += float(loss.data)
            sum_train_accuracy1 += float(accuracy.data)
            sum_train_accuracy2 += .0
            K += len(y)

            # 誤差逆伝播で勾配を計算
            loss.backward()
            optimizer.update()

        # 訓練データの誤差と,正解精度を表示
        mean_train_loss = sum_train_loss / K
        mean_train_accuracy1 = sum_train_accuracy1 / K
        mean_train_accuracy2 = sum_train_accuracy2 / K
        train_loss.append(mean_train_loss)
        train_accuracy1.append(mean_train_accuracy1)
        train_accuracy2.append(mean_train_accuracy2)
        now = time.time()
        train_throughput = now - cur_at
        cur_at = now

        # evaluation
        test_iter = batch_tuple([(s, t1, t2)
                                 for s, t1, t2 in zip(X_test, y_test, z_test)],
                                1)
        sum_test_loss = 0.
        sum_test_accuracy1 = 0.
        sum_test_accuracy2 = 0.
        K = 0

        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            for X, y, z in test_iter:

                # 順伝播させて誤差と精度を算出
                loss, accuracy = model(X, y)
                sum_test_loss += float(loss.data)
                sum_test_accuracy1 += float(accuracy.data)
                sum_test_accuracy2 += .0
                K += len(y)

        # テストデータでの誤差と正解精度を表示
        mean_test_loss = sum_test_loss / K
        mean_test_accuracy1 = sum_test_accuracy1 / K
        mean_test_accuracy2 = sum_test_accuracy2 / K
        test_loss.append(mean_test_loss)
        test_accuracy1.append(mean_test_accuracy1)
        test_accuracy2.append(mean_test_accuracy2)
        now = time.time()
        test_throughput = now - cur_at
        cur_at = now

        logger.info(''
                    '[{:>3d}] '
                    'T/loss={:.6f} '
                    'T/acc1={:.6f} '
                    'T/acc2={:.6f} '
                    'T/sec= {:.6f} '
                    'D/loss={:.6f} '
                    'D/acc1={:.6f} '
                    'D/acc2={:.6f} '
                    'D/sec= {:.6f} '
                    'lr={:.6f}'
                    ''.format(epoch, mean_train_loss, mean_train_accuracy1,
                              mean_train_accuracy2, train_throughput,
                              mean_test_loss, mean_test_accuracy1,
                              mean_test_accuracy2, test_throughput,
                              optimizer.alpha))
        sys.stdout.flush()

        # model と optimizer を保存する
        if mean_test_loss < min_loss:
            min_loss = mean_test_loss
            min_epoch = epoch
            if args.gpu >= 0: model.to_cpu()
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped.model'), model)
            chainer.serializers.save_npz(
                os.path.join(model_dir, 'early_stopped.state'), optimizer)
            if args.gpu >= 0: model.to_gpu()

        # optimizer.alpha *= lr_decay

        # 精度と誤差をグラフ描画
        if not args.plot:
            ylim1 = [min(train_loss + test_loss), max(train_loss + test_loss)]
            ylim2 = [
                min(train_accuracy1 + test_accuracy2),
                max(train_accuracy1 + test_accuracy2)
            ]

            # グラフ左
            plt.figure(figsize=(10, 10))

            plt.subplot(1, 2, 1)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(train_loss) + 1),
                     train_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            plt.ylabel('loss')
            plt.legend(['train loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(train_accuracy1) + 1),
                     train_accuracy1,
                     color='C0',
                     marker='x')
            # plt.plot(range(1, len(train_accuracy2) + 1), train_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            # plt.ylabel('accuracy')
            plt.legend(['train turn', 'train call'], loc="upper right")
            plt.title('Loss and accuracy of train.')

            # グラフ右
            plt.subplot(1, 2, 2)
            plt.ylim(ylim1)
            plt.plot(range(1,
                           len(test_loss) + 1),
                     test_loss,
                     color='C1',
                     marker='x')
            # plt.grid()
            # plt.ylabel('loss')
            plt.legend(['dev loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1,
                           len(test_accuracy1) + 1),
                     test_accuracy1,
                     color='C0',
                     marker='x')
            # plt.plot(range(1, len(test_accuracy2) + 1), test_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            plt.ylabel('accuracy')
            plt.legend(['dev turn', 'dev call'], loc="upper right")
            plt.title('Loss and accuracy of dev.')

            plt.savefig('{}-train.png'.format(args.out))
            # plt.savefig('{}-train.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        cur_at = now

    # model と optimizer を保存する
    if args.gpu >= 0: model.to_cpu()
    chainer.serializers.save_npz(os.path.join(model_dir, 'final.model'), model)
    chainer.serializers.save_npz(os.path.join(model_dir, 'final.state'),
                                 optimizer)
    if args.gpu >= 0: model.to_gpu()

    # test
    from sklearn.metrics import confusion_matrix, classification_report

    test_iter = batch_tuple([(s, t1, t2)
                             for s, t1, t2 in zip(X_test, y_test, z_test)],
                            args.batchsize)
    index2label = {v: k for k, v in labels.items()}

    with chainer.no_backprop_mode(), chainer.using_config('train', False):

        trues = []
        preds = []
        probs = []

        for X, y, z in test_iter:
            y_pred = model.predict(X)

            for pred in y_pred:
                pred = cuda.to_cpu(pred.data)
                idx = np.argmax(pred, axis=1)
                preds.append([index2label[x] for x in idx])
                probs.append([float(p[i]) for i, p in zip(idx, pred)])

            for i in range(len(y)):
                trues.append([index2label[x] for x in cuda.to_cpu(y[i].data)])

        sorted_labels = [
            k for k, _ in sorted(
                labels.items(), key=lambda x: x[1], reverse=False)
        ]

        print("\n==== Confusion matrix ====\n")
        cm = confusion_matrix([inner for outer in trues for inner in outer],
                              [inner for outer in preds for inner in outer],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        # グラフ描画
        if not args.plot:
            plt.figure()
            plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm.shape[0]):
                for j in range(cm.shape[1]):
                    plt.text(
                        j,
                        i,
                        "{}".format(cm[i, j]),
                        horizontalalignment="center",
                        color="white" if cm[i, j] > cm.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-train_cm.png'.format(args.out))
            # plt.savefig('{}-train_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report ====\n")
        print(
            classification_report(
                [inner for outer in trues for inner in outer],
                [inner for outer in preds for inner in outer],
                labels=sorted_labels))
Example #59
0
def main():
    global root_dir

    import argparse
    parser = argparse.ArgumentParser(description='SER example: 3-D ACRNN')
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer',
                        type=int,
                        default=1,
                        help='number of layes for turn')
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=1,
                        help='number of images in each mini-batch')
    parser.add_argument('--model',
                        default='models/final.model',
                        type=str,
                        help='trained model file (.model)')
    parser.add_argument('--label',
                        default='models/labels.pkl',
                        type=str,
                        help='saved label file (.pkl)')
    parser.add_argument('--mean',
                        default='models/mean-std.pkl',
                        type=str,
                        help='saved label file (.pkl)')
    parser.add_argument('--test',
                        default='datasets/test.txt',
                        type=str,
                        help='testing file (.txt)')
    parser.add_argument('--out',
                        '-o',
                        default='result-17',
                        help='directory to output the result')
    parser.add_argument('--noplot',
                        dest='noplot',
                        action='store_true',
                        help='disable PlotReport extension')
    parser.add_argument('--type',
                        default='mfcc',
                        choices=['fbank', 'mfcc'],
                        help='type of feature')
    parser.add_argument('--datasets_rootdir',
                        default='',
                        type=str,
                        help='path to datasets')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    root_dir = args.datasets_rootdir

    # test
    with open(args.label, 'rb') as f:
        labels = pickle.load(f)
    n_class = len(labels)

    # データの読み込み
    if args.type == 'fbank':
        X_test, y_test, _ = load_and_fbank(args.test, labels=labels)
    elif args.type == 'mfcc':
        X_test, y_test, _ = load_and_mfcc(args.test, labels=labels)
    else:
        raise ValueError("Only support fbank or mfcc.")

    print('# test X: {}'.format(len(X_test)))
    sys.stdout.flush()

    eps = 1e-5
    with open(args.mean, 'rb') as f:
        (mean, std) = pickle.load(f)
    X_test = [(x - mean) / (std + eps) for x in X_test]

    model = SER(n_layers=args.layer, n_inputs=3, n_outputs=n_class)

    chainer.serializers.load_npz(args.model, model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    from sklearn.metrics import confusion_matrix, classification_report
    test_iter = batch_iter([(x, t) for x, t in zip(X_test, y_test)],
                           1,
                           shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += [int(_) for _ in t]

        print("\n==== Confusion matrix ====\n")
        index2label = {v: k for k, v in labels.items()}
        sorted_labels = [
            k for k, _ in sorted(
                labels.items(), key=lambda x: x[1], reverse=False)
        ]
        cm = confusion_matrix([index2label[x] for x in y_true],
                              [index2label[x] for x in y_pred],
                              labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm.shape[0]):
                for j in range(cm.shape[1]):
                    plt.text(
                        j,
                        i,
                        "{}".format(cm[i, j]),
                        horizontalalignment="center",
                        color="white" if cm[i, j] > cm.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm.png'.format(args.out))
            # plt.savefig('{}-cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report ====\n")
        print(
            classification_report([sorted_labels[x] for x in y_true],
                                  [sorted_labels[x] for x in y_pred]))
        sys.stdout.flush()
Example #60
0
def train(trainL, trainA, testL, testA):
    trainLoss = []
    trainAccuracy = []
    testLoss = []
    testAccuracy = []
    l1w = []
    l2w = []
    l3w = []
    print('load MNIST dataset')
    mnist = data.load_mnist_data()
    mnist['data'] = mnist['data'].astype(np.float32)
    mnist['data'] /= 255
    mnist['target'] = mnist['target'].astype(np.int32)

    N = 1000
    lsizes = [784, 50, 50, 10]
    x_train, x_test = np.split(mnist['data'], [N])
    y_train, y_test = np.split(mnist['target'], [N])
    N_test = y_test.size

    # Prepare multi-layer perceptron model, defined in net.py
    if args.net == 'simple':
        #model = net.MnistMLP(lsizes)
        model = net.MnistMLP(layer_sizes=lsizes)
        if args.gpu >= 0:
            cuda.get_device(args.gpu).use()
            model.to_gpu()
        xp = np if args.gpu < 0 else cuda.cupy
    elif args.net == 'parallel':
        cuda.check_cuda_available()
        model = L.Classifier(net.MnistMLPParallel(784, n_units, 10))
        xp = cuda.cupy
    # Setup optimizer
    optimizer = optimizers.Adam()
    optimizer.setup(model)
    # Init/Resume
    if args.initmodel:
        print('Load model from', args.initmodel)
        serializers.load_npz(args.initmodel, model)
    if args.resume:
        print('Load optimizer state from', args.resume)
        serializers.load_npz(args.resume, optimizer)
    # Pretrain loop
    print("start pretrain")
    epo = p_epoch
    for j in six.moves.range(1, len(lsizes)):
        if j == len(lsizes) - 1:
            model.setfinetuning()
            print("start finetuning")
            epo = n_epoch
        for epoch in six.moves.range(1, epo + 1):
            print('layer ', j, 'p_epoch ', epoch)
            perm = np.random.permutation(N)
            sum_accuracy = 0
            sum_loss = 0
            for i in six.moves.range(0, N, batchsize):
                x = chainer.Variable(xp.asarray(x_train[perm[i:i +
                                                             batchsize]]))
                t = chainer.Variable(xp.asarray(y_train[perm[i:i +
                                                             batchsize]]))
                optimizer.update(model, x, t, j)
                sum_loss += float(model.loss.data) * len(t.data)
                if not model.pretrain:
                    sum_accuracy += float(model.accuracy.data) * len(t.data)
            if model.pretrain:
                print('Pretrain: train mean loss={}'.format(sum_loss / N))
            else:
                print('Finetune: train mean loss={}, accuracy={}'.format(
                    sum_loss / N, sum_accuracy / N))
            trainLoss.append(sum_loss / N)
            trainAccuracy.append(sum_accuracy / N)
            # evaluation
            sum_accuracy = 0
            sum_loss = 0
            model.train = False
            for i in six.moves.range(0, N_test, batchsize):
                x = chainer.Variable(xp.asarray(x_test[i:i + batchsize]),
                                     volatile='on')
                t = chainer.Variable(xp.asarray(y_test[i:i + batchsize]),
                                     volatile='on')
                loss = model(x, t, j)
                sum_loss += float(loss.data) * len(t.data)
                if not model.pretrain:
                    sum_accuracy += float(model.accuracy.data) * len(t.data)
            if model.pretrain:
                print('Pretrain: test  mean loss={}'.format(sum_loss / N_test))
            else:
                print('Finetune: test  mean loss={}, accuracy={}'.format(
                    sum_loss / N_test, sum_accuracy / N_test))
            testLoss.append(sum_loss / N_test)
            testAccuracy.append(sum_accuracy / N_test)
            model.train = True

    # Save the model and the optimizer
    savecsv(trainLoss, trainL)
    savecsv(trainAccuracy, trainA)
    savecsv(testLoss, testL)
    savecsv(testAccuracy, testA)

    print('save the model')
    serializers.save_npz('mlp.model', model)
    print('save the optimizer')
    serializers.save_npz('mlp.state', optimizer)