Example #1
0
def arbre(d, pi, pip, depth, sol=[], debug=False, alpha=0, elagage=True):
    """
    Méthode arborescente utilisant une fonction évaluation
    basée sur la borne inférieure de chaque noeud.
    Chaque noeud représentant une permutation de taches et les tâches restantes.
    Retourne une permutation et sa durée.
    -------------
    d = jeu de donnée
    pi = permutation effectuée
    pip = tâches restantes à ordonner
    depth = profondeur dans l'arbre
    sol = solution courante
    alpha = critère d'élagation
    elagage = booléen (oui = élagage, non = aucun élagage)
    """

    # Si feuille de l'arbre, je retourne la permutation
    # et la durée de la permutation.
    if depth == d[:,0].size:
        return pi+list(pip), evaluation(d, pi+list(pip), [])

    eval_sol = +inf
    for k in pip:
        if debug: print("{}{}, {}, {}, {}, {}, {}".format(pi+[k], pip[pip != k], evaluation(d, pi+[k], pip[pip != k]) < (1-alpha)*eval_sol, sol, eval_sol, evaluation(d, pi+[k], pip[pip != k]), (1-alpha)*eval_sol))
        if not elagage or evaluation(d, pi+[k], pip[pip != k]) < (1-alpha)*eval_sol:
            rsol, reval_sol = arbre(d, pi+[k], pip[pip != k], depth+1, sol, debug, alpha)
            if reval_sol < eval_sol:
                eval_sol = reval_sol
                sol = rsol
    return sol, eval_sol
Example #2
0
def main(args):
    model = load_model(args)
    print "loaded " + args.model

    raw_corpus = corpus.read_corpus(args.corpus)
    list_words, vocab_map, embeddings, padding_id = corpus.load_embeddings(corpus.load_embedding_iterator(args.embeddings))
    print("loaded embeddings")
    ids_corpus = corpus.map_corpus(vocab_map, raw_corpus)

    evaluation(args, padding_id, ids_corpus, vocab_map, embeddings, model)
Example #3
0
    def train(self):
        P = np.random.normal(0, 0.1, (self.N, self.K))
        Q = np.random.normal(0, 0.1, (self.M, self.K))

        train_mat = sequence2mat(sequence=self.train_list, N=self.N, M=self.M)
        test_mat = sequence2mat(sequence=self.test_list, N=self.N, M=self.M)

        records_list = []
        for step in range(self.max_iteration):
            for u in range(self.N):
                Ru = train_mat[u, :]
                P[u, :] = self.update(Q,
                                      Ru,
                                      lamda_regularizer=self.lamda_regularizer,
                                      alpha=self.alpha)

            for i in range(self.M):
                Ri = train_mat[:, i]
                Q[i, :] = self.update(P,
                                      Ri.T,
                                      lamda_regularizer=self.lamda_regularizer,
                                      alpha=self.alpha)

            pred_mat = self.prediction(P, Q)
            mae, rmse, recall, precision = evaluation(pred_mat, train_mat,
                                                      test_mat)
            records_list.append(np.array([mae, rmse, recall, precision]))

            print(' step:%d \n mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f' %
                  (step, mae, rmse, recall, precision))

        print(' end. \n mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f' %
              (records_list[-1][0], records_list[-1][1], records_list[-1][2],
               records_list[-1][3]))
        return P, Q, np.array(records_list)
Example #4
0
    def train(self):
        P = np.random.normal(0, 0.1, (self.N, self.K))
        Q = np.random.normal(0, 0.1, (self.M, self.K))

        train_mat = sequence2mat(sequence=self.train_list, N=self.N, M=self.M)
        test_mat = sequence2mat(sequence=self.test_list, N=self.N, M=self.M)

        records_list = []
        for step in range(self.max_iteration):
            los = 0.0
            for data in self.train_list:
                u, i, r = data
                P[u], Q[i], ls = self.update(P[u],
                                             Q[i],
                                             r=r,
                                             learning_rate=self.learning_rate)
                los += ls
            pred_mat = self.prediction(P, Q)
            mae, rmse, recall, precision = evaluation(pred_mat, train_mat,
                                                      test_mat)
            records_list.append(np.array([los, mae, rmse, recall, precision]))

            if step % 10 == 0:
                print(
                    ' step:%d \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
                    % (step, los, mae, rmse, recall, precision))

        print(
            ' end. \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
            % (records_list[-1][0], records_list[-1][1], records_list[-1][2],
               records_list[-1][3], records_list[-1][4]))
        return P, Q, np.array(records_list)
Example #5
0
    def train(self):
        train_mat = sequence2mat(sequence=self.train_list, N=self.N, M=self.M)
        test_mat = sequence2mat(sequence=self.test_list, N=self.N, M=self.M)

        avg = np.sqrt(train_mat.mean() / self.K)
        P = avg * np.random.normal(0, 1., (self.N, self.K))
        Q = avg * np.random.normal(0, 1., (self.M, self.K))

        records_list = []
        for step in range(self.max_iteration):
            P, Q = self.update(P, Q, R=train_mat)
            user = np.array(self.train_list)[:, 0].astype(np.int16)
            item = np.array(self.train_list)[:, 1].astype(np.int16)
            rating_true = np.array(self.train_list)[:, 2]
            rating_pred = np.sum(P[user, :] * Q[item, :], axis=1)
            los = np.sum((rating_true - rating_pred)**2)
            pred_mat = self.prediction(P, Q)
            mae, rmse, recall, precision = evaluation(pred_mat, train_mat,
                                                      test_mat)
            records_list.append(np.array([los, mae, rmse, recall, precision]))

            if step % 10 == 0:
                print(
                    ' step:%d \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
                    % (step, los, mae, rmse, recall, precision))

        print(
            ' end. \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
            % (records_list[-1][0], records_list[-1][1], records_list[-1][2],
               records_list[-1][3], records_list[-1][4]))
        return P, Q, np.array(records_list)
Example #6
0
def johnson(d, debug=False):
    """
    Méthode approchée avec garantie de performance (Johnson)
    d = Jeu de donnée
    """

    assert np.size(d, 1) > 1, "2 machines au moins"
    if debug and np.size(d, 1) != 2: print("Attention: Seul les deux premières machines sont pris en compte.")

    n, m = np.size(d, 0), 3
    A, B, C = range(0,m)
    X = list(range(0,n))
    G = list()
    D = list()

    while X:
        # Chaque tâche ayant été traité voit ses durées mettre à NaN.
        # np.nanargmin récupère ensuite la position dans la matrice
        # où la durée minimum est située.
        pos = np.nanargmin([[d[i,j] if i in X else np.nan for j in [A,B]] for i in range(n)])

        # Je déduit ensuite la ligne et la colonne correspondant à cette position
        # i.e. la machine et la tâche.
        i, j = pos//(m-1), pos%(n-1)

        # Si la durée de la tâche appartient à la machine A
        # je la met dans ma liste de gauche G, sinon je l'insère
        # dans ma liste de droite en première position.
        G.append(i) if j == A else D.insert(0, i)

        # Ma tâche en cours est traitée
        X.remove(i)

    return G + D, evaluation(d, G+D, [])
Example #7
0
def testing(dataset, uid, mode, cached=True, groups=None, time_window=DEFAULT_TIME_WINDOW):
    debug('Processing: {}'.format(uid))
    dataset = np.array(dataset)
    clfs = classifier_list()
    try:
        # debug(dataset.shape)
        # debug(dataset[0])
        ncol = dataset.shape[1]
        X = dataset[:,3:ncol] # Remove index 0 (uid), index 1 (time), and index 2 (activities)
        y = dataset[:,2]
        texts = []
        info = {}
        info['uid'] = uid
        for name, clf in clfs.items():
            debug(name)
            info['clf_name'] = name
            output = evaluation(X, y, clf, k_fold=K_FOLD, info=info, cached=cached, mode=mode, groups=groups, time_window=time_window)
            acc = output['acc']
            time_train = output['time_train']
            time_test = output['time_test']
            text = '{},{},{},{},{}'.format(uid, name, acc, time_train, time_test)
            texts.append(text)
        return texts
    except Exception as ex:
        debug('Error on testing', ex)
        return None
Example #8
0
    def train(self): 
        P = np.random.normal(0, 0.1, (self.N, self.K))
        Q = np.random.normal(0, 0.1, (self.M, self.K))
        Y = np.random.normal(0, 0.1, (self.M, self.K))
        bu = np.zeros([self.N])
        bi = np.zeros([self.M])

        train_mat = sequence2mat(sequence = self.train_list, N = self.N, M = self.M)
        test_mat = sequence2mat(sequence = self.test_list, N = self.N, M = self.M)

        aveg_rating = np.mean(train_mat[train_mat>0])

        records_list = []
        for step in range(self.max_iteration):
            los=0.0
            for data in self.train_list:
                u,i,r = data
                P[u],Q[i],bu[u],bi[i],Y, ls = self.update(p=P[u], q=Q[i], bu=bu[u], bi=bi[i], Y=Y, 
                                                          aveg_rating=aveg_rating, r=r,Ru = train_mat[u], 
                                                          learning_rate=self.learning_rate, 
                                                          lamda_regularizer=self.lamda_regularizer)
                los += ls
            pred_mat = self.prediction(P, Q, Y, bu, bi, aveg_rating, train_mat)
            mae, rmse, recall, precision = evaluation(pred_mat, train_mat, test_mat)
            records_list.append(np.array([los, mae, rmse, recall, precision]))

            if step % 10 ==0:
                print(' step:%d \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
                      %(step,los,mae,rmse,recall,precision))

        print(' end. \n loss:%.4f,mae:%.4f,rmse:%.4f,recall:%.4f,precision:%.4f'
              %(records_list[-1][0],records_list[-1][1],records_list[-1][2],records_list[-1][3],records_list[-1][4]))
        return P, Q, Y, bu, bi, np.array(records_list)
Example #9
0
def main(args):
    #load data
    if args.mode == 'train':
        data_loader = data_load.data_loader(args.max_length, args.train_idx, args.train_audio2vec, args.train_oracle, args.mapping, target_data_path=args.target)
    else:
        data_loader = data_load.data_loader(args.max_length, args.test_idx, args.test_audio2vec, args.test_oracle, args.mapping, target_data_path=None)

    #add some feature to args
    args.idx_size = data_loader.idx_size
    args.phn_size = data_loader.vocab_size
    args.feat_dim = data_loader.feat_dim

    #build model graph
    if args.mode == 'train':
        g = model(args)
    else:
        g = model(args, is_training=False)
    print("Graph loaded")

    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)

    #create sess
    with tf.Session(graph=g.graph) as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver(max_to_keep=3)

        if (args.mode != 'train') or (args.load == 'load'):
            print('load_model')
            saver.restore(sess, tf.train.latest_checkpoint(args.save_dir))

        if args.mode == 'train':
            print('training')
            train(sess, g, args, saver, data_loader)
        else:
            print('evaluating')
            evaluation(sess, g, args, data_loader)
Example #10
0
def evalOnly( currentSet , scale , params , modelType, shuffle = 0, KFold = False ):
  ngramMax = params["ngramMax"]
  kInter  = params["kInter"]

  text, oText, y, y1, y2 = get_data ( currentSet, ngramMax, kInter , shuffle) #Get the data after being shuffled
                                                                              #"shuffle" times
  XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest( text , y )  
  global objectEv
  objectEv = evaluation( XTrain = XTrain, XTest = XTest, indices_Train = indices_Train, indices_Test = indices_Test,
                      y = y, y1 = y1, y2 = y2 , scale = scale , modelType = modelType )
      
  cParams = ( params["param1"], params["param2"], params["nComp"] , KFold )

  cKappa = evaluateParametersInd( cParams )[0]
  return cKappa
Example #11
0
def evaluateParams( currentSet, ngramMax , kInter,  scale, param3List, modelType , level):
  text, originalText, y, y1, y2 = get_data ( currentSet, ngramMax, kInter )
  XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest( text , y ) #Split Data into trainining and test data

  global objectEv
  #Initialize object to evaluate parameters
  objectEv = evaluation( XTrain = XTrain, XTest = XTest, indices_Train = indices_Train, indices_Test = indices_Test,
                      y = y, y1 = y1, y2 = y2 , scale = scale , modelType = modelType , level = level)
  
  #Different values for testing. "level" paramters control the
  #the number of values to be tested.
  posVal1LG  = [ 3**(i/2.0) for i in range(-5, 6, level) ]
  posVal2LG  = [ i / 10.0   for i in range( 6,14, level) ]
  posVal2GB  = [ 2**(i/2.0) for i in range(-7, 1, level) ]
  posVal1SV1 = [ 3**(i/2.0) for i in range(-1, 6, level) ]
  posVal1SV2 = [ 3**(i/2.0) for i in range(-7,-1, level) ]

  #human kappa
  hKappa  = skllMetrics.kappa( y1, y2 )
  hKappa1 = skllMetrics.kappa( y , y1 )
  hKappa2 = skllMetrics.kappa( y , y2 )
  
  print ngramMax, kInter
  
  for param3 in param3List :
    if   modelType == "LR": posVal1, posVal2 = posVal1LG , posVal2LG if param3 > 0 else [ "NA" ]
    elif modelType == "GB": posVal1, posVal2 = [ 1500 ]  , posVal2GB
    elif modelType == "SV": posVal1, posVal2 = posVal1SV1, posVal1SV2

    listParams = [ ( param1, param2, param3 ) for param1 in posVal1 for param2 in posVal2 ]
    if   scale == 2:
      currentKappa, param1, param2, threshold1, threshold2 = get_best_values ( listParams = listParams , scale=scale )
      print ngramMax, kInter, param3, param1, param2 , threshold1, threshold2, currentKappa, hKappa, hKappa1, hKappa2

    elif scale == 1:
      currentKappa, param1, param2, threshold = get_best_values ( listParams = listParams , scale = scale)
      print ngramMax, kInter,  param3, param1, param2 , threshold, currentKappa, hKappa, hKappa1, hKappa2

    elif scale == 3:
      currentKappa, param1, param2, threshold1, threshold2, threshold3 = get_best_values ( listParams = listParams , scale = scale )
      print ngramMax, kInter, param3, param1, param2 , threshold1, threshold2, threshold3, currentKappa

    elif scale == 4:
      currentKappa, param1, param2, threshold1, threshold2, threshold3, threshold4 = get_best_values ( listParams = listParams , scale = scale )
      print ngramMax, kInter, param3, param1, param2 , threshold1, threshold2, threshold3, threshold4, currentKappa
  return
 def train(self):
     self.P = np.random.normal(0, 0.1, (self.N, self.K))
     self.Q = np.random.normal(0, 0.1, (self.M, self.K))
     
     train_mat = sequence_to_mat(sequence = self.train_list, N = self.N, M = self.M)
     test_mat = sequence_to_mat(sequence = self.test_list, N = self.N, M = self.M)
     
     record_list = []
     for step in range(self.max_iteration):
         loss = 0.0
         for data in self.train_list:
             u, i, r = data
             self.P[u], self.Q[i], ls = self.update(self.P[u], self.Q[i], r)
             loss += ls
         pred_mat = self.prediction()
         recall, precision = evaluation(pred_mat, train_mat, test_mat)
         record_list.append(np.array([loss, recall, precision]))
         
         if(step % 10 == 0):
             print('step:%d \n loss:%.4f, recall:%.4f, precision:%.4f'%(step, loss, recall, precision))
     
     return np.array(record_list)
Example #13
0
def evalOnly(currentSet, scale, params, modelType, shuffle=0, KFold=False):
    ngramMax = params["ngramMax"]
    kInter = params["kInter"]

    text, oText, y, y1, y2 = get_data(
        currentSet, ngramMax, kInter,
        shuffle)  #Get the data after being shuffled
    #"shuffle" times
    XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest(text, y)
    global objectEv
    objectEv = evaluation(XTrain=XTrain,
                          XTest=XTest,
                          indices_Train=indices_Train,
                          indices_Test=indices_Test,
                          y=y,
                          y1=y1,
                          y2=y2,
                          scale=scale,
                          modelType=modelType)

    cParams = (params["param1"], params["param2"], params["nComp"], KFold)

    cKappa = evaluateParametersInd(cParams)[0]
    return cKappa
Example #14
0
# data of covtype
# covtype_data = pd.read_csv('./data/covtype.csv')
# cols = covtype_data.columns
# col1 = cols[0:-1]
# data = covtype_data[col1]
# data = np.array(data)
# real_labels = np.array(covtype_data[cols[-1]])  # type
# k = 7  # change k smaller if the code throws error

# uncomment one of above paragraphs to get data

samples = random.sample(range(data.shape[0]), 10000)
data = data[samples, :]
real_labels = real_labels[samples]

evaluate = evaluation(data, real_labels, k, DEL_NUM=100)

# 初始的训练
evaluate.run()

# 测试三种算法基础效果
print("--------------------------------")
evaluate.test_basic()
print("--------------------------------")

# 测试删除效果
evaluate.test_deletion()
print("--------------------------------")

# 测试删除时间
evaluate.test_deletion_time()
Example #15
0
# coding=utf8
from lstm_with_tag_relation import *
from evaluation import *


def print_usage():
    print(u'usage: python main.py train for training\n'
          u'       python main.py eval for evaluation')


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print_usage()
    elif sys.argv[1] == u'train':
        train()
    elif sys.argv[1] == u'eval':
        evaluation(sys.argv[2], [
            subset_evaluator, hamming_evaluator, accuracy_evaluator,
            precision_evaluator, recall_evaluator, one_error_evaluator,
            coverage_evaluator, ranking_loss_evaluator
        ])
    elif sys.argv[1] == u'batch_eval':
        batch_evaluation(sys.argv[2], int(sys.argv[3]))
    elif sys.argv[1] == u'p_relation':
        print_relation(sys.argv[2])
    elif sys.argv[1] == u'lsq':
        lsq(sys.argv[2],
            sample_size=int(sys.argv[3]) if len(sys.argv) >= 4 else None)
    else:
        print_usage()
Example #16
0
def main():
    parser = argparse.ArgumentParser(description='Train StarGAN')
    parser.add_argument('--source_path',
                        default="source/celebA/",
                        help="data resource Directory")
    parser.add_argument('--att_list_path',
                        default="att_list.txt",
                        help="attribute list")
    parser.add_argument('--batch_size', '-b', type=int, default=16)
    parser.add_argument('--max_iter', '-m', type=int, default=200000)
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=0,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--eval_folder',
                        '-e',
                        default='test',
                        help='Directory to output the evaluation result')

    parser.add_argument('--eval_interval',
                        type=int,
                        default=1000,
                        help='Interval of evaluating generator')

    parser.add_argument("--learning_rate_g",
                        type=float,
                        default=0.0001,
                        help="Learning rate for generator")
    parser.add_argument("--learning_rate_d",
                        type=float,
                        default=0.0001,
                        help="Learning rate for discriminator")
    parser.add_argument("--load_gen_model",
                        default='',
                        help='load generator model')
    parser.add_argument("--load_dis_model",
                        default='',
                        help='load discriminator model')

    parser.add_argument('--gen_class',
                        default='StarGAN_Generator',
                        help='Default generator class')
    parser.add_argument('--dis_class',
                        default='StarGAN_Discriminator',
                        help='Default discriminator class')

    parser.add_argument("--n_dis",
                        type=int,
                        default=6,
                        help='The number of loop of WGAN Discriminator')
    parser.add_argument("--lambda_gp",
                        type=float,
                        default=10.0,
                        help='lambda for gradient penalty of WGAN')
    parser.add_argument("--lambda_adv",
                        type=float,
                        default=1.0,
                        help='lambda for adversarial loss')
    parser.add_argument("--lambda_cls",
                        type=float,
                        default=1.0,
                        help='lambda for classification loss')
    parser.add_argument("--lambda_rec",
                        type=float,
                        default=10.0,
                        help='lambda for reconstruction loss')

    parser.add_argument("--flip",
                        type=int,
                        default=1,
                        help='flip images for data augmentation')
    parser.add_argument("--resize_to",
                        type=int,
                        default=128,
                        help='resize the image to')
    parser.add_argument("--crop_to",
                        type=int,
                        default=178,
                        help='crop the resized image to')
    parser.add_argument("--load_dataset",
                        default='celebA_train',
                        help='load dataset')
    parser.add_argument("--discriminator_layer_n",
                        type=int,
                        default=6,
                        help='number of discriminator layers')

    parser.add_argument("--learning_rate_anneal",
                        type=float,
                        default=10e-8,
                        help='anneal the learning rate')
    parser.add_argument("--learning_rate_anneal_start",
                        type=int,
                        default=100000,
                        help='time to anneal the learning')

    args = parser.parse_args()
    print(args)
    record_setting(args.out)
    max_iter = args.max_iter

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

    with open(args.att_list_path, "r") as f:
        att_list = []
        att_name = []
        for line in f:
            line = line.strip().split(" ")
            if len(line) == 3:
                att_list.append(int(line[0]))  #attID
                att_name.append(line[1])  #attname
    print("attribute list:", ",".join(att_name))

    #load dataset
    train_dataset = getattr(celebA,
                            args.load_dataset)(args.source_path,
                                               att_name,
                                               flip=args.flip,
                                               resize_to=args.resize_to,
                                               crop_to=args.crop_to)
    train_iter = chainer.iterators.MultiprocessIterator(train_dataset,
                                                        args.batch_size,
                                                        n_processes=4)

    #test_dataset = getattr(celebA, args.load_dataset)(root_celebA, flip=args.flip, resize_to=args.resize_to, crop_to=args.crop_to)
    test_batchsize = 8
    test_iter = chainer.iterators.SerialIterator(train_dataset, test_batchsize)

    #set generator and discriminator
    nc_size = len(att_list)  #num of attribute
    gen = getattr(net, args.gen_class)(args.resize_to, nc_size)
    dis = getattr(net,
                  args.dis_class)(n_down_layers=args.discriminator_layer_n)

    if args.load_gen_model != '':
        serializers.load_npz(args.load_gen_model, gen)
        print("Generator model loaded")

    if args.load_dis_model != '':
        serializers.load_npz(args.load_dis_model, dis)
        print("Discriminator model loaded")

    if not os.path.exists(args.eval_folder):
        os.makedirs(args.eval_folder)

    # select GPU
    if args.gpu >= 0:
        gen.to_gpu()
        dis.to_gpu()
        print("use gpu {}".format(args.gpu))

    # Setup an optimizer
    def make_optimizer(model, alpha=0.0001, beta1=0.5, beta2=0.999):
        optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1)
        optimizer.setup(model)
        return optimizer

    opt_gen = make_optimizer(gen, alpha=args.learning_rate_g)
    opt_dis = make_optimizer(dis, alpha=args.learning_rate_d)

    # Set up a trainer
    updater = Updater(models=(gen, dis),
                      iterator={
                          'main': train_iter,
                          'test': test_iter
                      },
                      optimizer={
                          'opt_gen': opt_gen,
                          'opt_dis': opt_dis,
                      },
                      device=args.gpu,
                      params={
                          'n_dis': args.n_dis,
                          'lambda_adv': args.lambda_adv,
                          'lambda_cls': args.lambda_cls,
                          'lambda_rec': args.lambda_rec,
                          'lambda_gp': args.lambda_gp,
                          'image_size': args.resize_to,
                          'eval_folder': args.eval_folder,
                          'nc_size': nc_size,
                          'learning_rate_anneal': args.learning_rate_anneal,
                          'learning_rate_anneal_start':
                          args.learning_rate_anneal_start,
                          'dataset': train_dataset
                      })

    model_save_interval = (4000, 'iteration')
    trainer = training.Trainer(updater, (max_iter, 'iteration'), out=args.out)
    trainer.extend(extensions.snapshot_object(gen,
                                              'gen{.updater.iteration}.npz'),
                   trigger=model_save_interval)
    trainer.extend(extensions.snapshot_object(dis,
                                              'dis{.updater.iteration}.npz'),
                   trigger=model_save_interval)

    log_keys = [
        'epoch', 'iteration', 'lr_g', 'lr_d', 'loss_dis_adv', 'loss_gen_adv',
        'loss_dis_cls', 'loss_gen_cls', 'loss_gen_rec', 'loss_gp'
    ]
    trainer.extend(
        extensions.LogReport(keys=log_keys, trigger=(20, 'iteration')))
    trainer.extend(extensions.PrintReport(log_keys), trigger=(20, 'iteration'))
    trainer.extend(extensions.ProgressBar(update_interval=50))

    trainer.extend(evaluation(gen, args.eval_folder,
                              image_size=args.resize_to),
                   trigger=(args.eval_interval, 'iteration'))

    #trainer.extend(CommandsExtension())
    # Run the training
    trainer.run()
Example #17
0
            begin_inds = begin_inds + args.batch_size
            end_inds = end_inds + args.batch_size
        hashcode_unseen_val[begin_inds:valid_unseen_x.shape[0], :] = sess.run(
            hash_layer,
            feed_dict={
                input_image:
                valid_unseen_x[begin_inds:valid_unseen_x.shape[0], :, :, :],
                is_training:
                False
            })
        hashcode_unseen_val = np.sign(hashcode_unseen_val)

        St1 = np.dot(test_unseen_y, np.transpose(valid_unseen_y))
        Wt1 = np.float32(St1 > 0)
        [p1, r1, map1, wap1, acg1,
         ndcg1] = evaluation(hashcode_unseen_test, hashcode_unseen_val, Wt1,
                             St1, top_nums)

        print_to_file(args.log, 'Retrieval evaluation on unseen dataset:')
        for i in range(len(top_nums)):
            tmp_str = (
                'top_%d, precission: %f, recall: %f, map: %f, wap: %f, acg: %f, ndcg: %f'
                % (top_nums[i], p1[i], r1[i], map1[i], wap1[i], acg1[i],
                   ndcg1[i]))
            print_to_file(args.log, tmp_str)
        print_to_file(args.log, '\n')

        steps = int(math.floor(nb_train_seen / args.batch_size))

        train_fuse_x = np.vstack((train_seen_x, train_unseen_x))
        steps3 = int(math.floor(train_fuse_x.shape[0] / batch_size))
Example #18
0
def trainFull( currentSet , scale, listParams, shuffle = 0  ):
  
  shuffle += 20
  paramsDict = getParams( currentSet ) #Load parameters

  #Train LR model with loaded parameters
  modelType = "LR" 
  ngramMax, kInter, param3, param1, param2 = paramsDict[modelType]  
  text, originalText, y, y1, y2 = get_data ( currentSet, ngramMax, kInter , shuffle)
  XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest( text , y )  
  global objectEv
  objectEv = evaluation( XTrain = XTrain, XTest = XTest, indices_Train = indices_Train, indices_Test = indices_Test,
                      y = y, y1 = y1, y2 = y2 , scale = scale , modelType = modelType )       
  modelLG = mainModel( param1 = param1, param2 = param2, param3 = param3 ) 
  if scale > 1:
    yProb1, yProb2 = objectEv.cv_estimateCl2 ( modelLG , False)
    yp1 = np.maximum( yProb1, yProb2 * 2 )
  else:
    yp1 = objectEv.cv_estimateCl1( modelLG, False )

  #Train SV model with loaded parameters
  modelType = "SV"
  ngramMax, kInter, param3, param1, param2 = paramsDict[modelType]
  text, originalText, y, y1, y2 = get_data ( currentSet, ngramMax, kInter , shuffle)
  XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest( text , y )  
  objectEv = evaluation( XTrain = XTrain, XTest = XTest, indices_Train = indices_Train, indices_Test = indices_Test,
                      y = y, y1 = y1, y2 = y2 , scale = scale , modelType = modelType )       
  modelSV = mainModel( param1 = param1, param2 = param2, param3 = param3 ) 
  if scale > 1:
    yp2 = objectEv.cv_estimateSV2 ( modelSV )
  else:
    yp2 = objectEv.cv_estimateCl1( modelSV, True )
  
  #Train GB model with loaded parameters
  modelType = "GB"
  ngramMax, kInter, param3, param1, param2 = paramsDict[modelType]    
  text, originalText, y, y1, y2 = get_data ( currentSet, ngramMax, kInter , shuffle)
  XTrain, XTest, indices_Train, indices_Test = get_XTrain_XTest( text , y )
  objectEv = evaluation( XTrain = XTrain, XTest = XTest, indices_Train = indices_Train, indices_Test = indices_Test,
                      y = y, y1 = y1, y2 = y2 , scale = scale , modelType = modelType )       
  modelGB = mainModel( param1 = param1+1, param2 = param2, param3 = param3 )   
  if scale > 1:
    yL = objectEv.cv_estimateGB2( modelGB , param1 + 1)
  else:
    yL = objectEv.cv_estimateGB1( modelGB , param1 + 1)
  yp3 = yL[ param1 ]
  
  #Calculates the kappas obtained by using the weights and thresholds 
  #in the list of parameters
  prevCoef1 = -1
  prevCoef2 = -1
  prevCoef3 = -1
  listKappas = list()
  for cParams in listParams:
    if scale > 1:
      coef1, coef2, coef3, threshold1, threshold2 = cParams
      if  prevCoef1 != coef1 or prevCoef2 != coef2 or prevCoef3 != coef3:
        yF = coef1 * yp1 + coef2 * yp2 + coef3 * yp3
      yPred = ( yF >= threshold1 ) * 1        
      yPred = ( yF >= threshold2 ) * 1 + yPred
    else:
      coef1, coef2, coef3, threshold = cParams
      if  prevCoef1 != coef1 or prevCoef2 != coef2 or prevCoef3 != coef3:
        yF = coef1 * yp1 + coef2 * yp2 + coef3 * yp3
      yPred = yF > threshold
    ckappa = skllMetrics.kappa( objectEv.y, yPred )
    listKappas.append( ckappa )
    prevCoef1, prevCoef2, prevCoef3 = coef1, coef2, coef3
  
  return listKappas
Example #19
0
    
        hashcode_unseen_val = np.zeros([valid_unseen_x.shape[0], args.hash_bits], np.float32)
        begin_inds = 0
        end_inds = args.batch_size
        while end_inds < valid_unseen_x.shape[0]:
            tmp_hashcode = sess.run(hash_layer, feed_dict={input_image: valid_unseen_x[begin_inds:end_inds, :, :, :], is_training:False})
            hashcode_unseen_val[begin_inds:end_inds, :] = tmp_hashcode
            begin_inds = begin_inds + args.batch_size
            end_inds = end_inds + args.batch_size
        hashcode_unseen_val[begin_inds:valid_unseen_x.shape[0], :] = sess.run(hash_layer,
                        feed_dict={input_image: valid_unseen_x[begin_inds:valid_unseen_x.shape[0], :, :, :], is_training:False})
        hashcode_unseen_val = np.sign(hashcode_unseen_val)

        St1 = np.dot(test_unseen_y, np.transpose(valid_unseen_y))
        Wt1 = np.float32(St1 > 0)
        [p1, r1, map1, wap1, acg1, ndcg1] = evaluation(hashcode_unseen_test, hashcode_unseen_val, Wt1, St1, top_nums)

        print_to_file(args.log, 'Retrieval evaluation on unseen dataset:')
        for i in range(len(top_nums)):
            tmp_str = ('top_%d, precission: %f, recall: %f, map: %f, wap: %f, acg: %f, ndcg: %f' %
                       (top_nums[i], p1[i], r1[i], map1[i], wap1[i], acg1[i], ndcg1[i]))
            print_to_file(args.log, tmp_str)
        print_to_file(args.log, '\n')

        steps = int(math.floor(nb_train_seen / args.batch_size))

        train_fuse_x = np.vstack((train_seen_x, train_unseen_x))
        steps3 = int(math.floor(train_fuse_x.shape[0] / batch_size))

       
        for i_epoch in range(args.num_epochs):
Example #20
0
def main(data_index=None,
         cut_shape=None,
         data_type=['MCIc', 'MCInc'],
         pre_dir='/home/anzeng/rhb/fmri_data',
         num_batches=256 * 5,
         voxnet_point=None,
         test_size=6,
         brain_map=[217]):
    # fr = open(cfg.output, 'w')
    tf.reset_default_graph()

    time_dim = 80  # 挑选时间片个数
    batch_size = 8

    dataset = fMRI_data(data_type,
                        data_index=data_index,
                        varbass=False,
                        dir=pre_dir)
    #SVM index

    #########################
    svm_index = {}
    train_len = 0
    test_len = 0
    for d_type in data_type:
        t_dir = os.path.join(pre_dir, d_type)
        t_len = os.listdir(t_dir)
        t_len = len(t_len)
        train_index = list(range(t_len))
        test_index = data_index[d_type]['test']
        for x in test_index:
            train_index.remove(x)
        _index = {'train': train_index, 'test': test_index}
        train_len += len(train_index)
        test_len += len(test_index)
        svm_index[d_type] = _index
    print(train_len)
    print(test_len)
    print(svm_index)
    svm_dataset = fMRI_data(data_type,
                            data_index=svm_index,
                            varbass=False,
                            dir=pre_dir)
    ##########################
    xyz = 32
    input_shape = [None, xyz, xyz, xyz, 1]
    # for i in range(3):
    #     input_shape.append(cut_shape[2 * i + 1] + 1 - cut_shape[2 * i])
    # input_shape.append(1)
    # print(input_shape)
    voxnet = VoxNet(input_shape=input_shape, voxnet_type='cut')
    FCNs = Classifier_FCN(tf.placeholder(tf.float32, [None, time_dim, 50]),
                          nb_classes=2)

    data_value = [[1], [1]]

    # 创建数据
    p = dict()  # placeholders

    p['labels'] = tf.placeholder(tf.float32, [None, 2])
    p['data_value'] = tf.placeholder(tf.float32, [2, 1])

    p['Weight'] = tf.matmul(p['labels'], p['data_value'])
    p['cross_loss'] = tf.nn.softmax_cross_entropy_with_logits(
        logits=FCNs[-2], labels=p['labels'])
    p['Weight'] = tf.reshape(p['Weight'], [-1])
    p['x_loss'] = tf.multiply(p['Weight'], p['cross_loss'])
    p['loss'] = tf.reduce_mean(p['x_loss'])
    p['l2_loss'] = tf.add_n([tf.nn.l2_loss(w) for w in FCNs.kernels])

    p['prediction'] = tf.argmax(FCNs[-1], 1)
    p['y_true'] = tf.argmax(p['labels'], 1)
    p['correct_prediction'] = tf.equal(p['prediction'], p['y_true'])
    p['accuracy'] = tf.reduce_mean(tf.cast(p['correct_prediction'],
                                           tf.float32))

    p['learning_rate'] = tf.placeholder(tf.float32)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        p['train'] = tf.train.AdamOptimizer(p['learning_rate'],
                                            epsilon=1e-3).minimize(p['loss'])
    p['weights_decay'] = tf.train.GradientDescentOptimizer(
        p['learning_rate']).minimize(p['l2_loss'])

    # p['test_error'] = tf.placeholder(tf.float32)
    # 超参数设置

    initial_learning_rate = 0.01
    min_learning_rate = 0.000001
    learning_rate_decay_limit = 0.0001

    num_batches_per_epoch = len(dataset.train) / float(batch_size)
    learning_decay = 10 * num_batches_per_epoch
    weights_decay_after = 5 * num_batches_per_epoch

    checkpoint_num = 0
    learning_step = 0
    min_loss = 1e308

    if voxnet_point:
        cfg.voxnet_checkpoint = voxnet_point

    accuracy_filename = os.path.join(cfg.fcn_checkpoint_dir, 'accuracies.txt')
    if not os.path.isdir(cfg.fcn_checkpoint_dir):
        os.mkdir(cfg.fcn_checkpoint_dir)

    if not os.path.exists(accuracy_filename):
        with open(accuracy_filename, 'a') as f:
            f.write('')
    with open(accuracy_filename, 'a') as f:
        f.write(str(brain_map) + '\n')

    #返回值
    test_evaluation = evaluation()
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        voxnet.npz_saver.restore(session, cfg.voxnet_checkpoint)
        #voxnet赋值
        input_shape[0] = 1
        voxnet_data = np.ones(input_shape, np.float32)
        input_shape[0] = -1
        for batch_index in range(num_batches):
            start = time.time()
            # learning_rate = max(min_learning_rate,
            #                     initial_learning_rate * 0.5 ** (learning_step / learning_decay))
            learning_rate = 0.0001
            learning_step += 1

            if batch_index > weights_decay_after and batch_index % 256 == 0:
                session.run(p['weights_decay'], feed_dict=feed_dict)

            voxs, labels = dataset.train.oversampling.get_time_batch(
                session,
                voxnet,
                cut_shape,
                time_dim=time_dim,
                batch_size=batch_size)
            feed_dict = {
                FCNs[0]: voxs,
                voxnet[0]: voxnet_data,
                voxnet.keep_prob: 1.0,
                FCNs.keep_prob: 0.7,
                p['labels']: labels,
                p['learning_rate']: learning_rate,
                FCNs.training: True,
                p['data_value']: data_value
            }

            session.run(p['train'], feed_dict=feed_dict)

            if batch_index and batch_index % 32 == 0:

                print("{} batch: {}".format(datetime.datetime.now(),
                                            batch_index))
                print('learning rate: {}'.format(learning_rate))
                # fr.write("{} batch: {}".format(datetime.datetime.now(), batch_index))
                # fr.write('learning rate: {}'.format(learning_rate))

                feed_dict[FCNs.training] = False
                loss = session.run(p['loss'], feed_dict=feed_dict)
                print('loss: {}'.format(loss))

                if (batch_index and loss > 1.5 * min_loss
                        and learning_rate > learning_rate_decay_limit):
                    min_loss = loss
                    learning_step *= 1.2
                    print("decreasing learning rate...")
                min_loss = min(loss, min_loss)

            if batch_index and batch_index % 16 == 0:
                num_accuracy_batches = 20
                train_evaluation = evaluation()
                for x in range(num_accuracy_batches):
                    voxs, labels = dataset.train.random_sampling.get_time_batch(
                        session,
                        voxnet,
                        cut_shape,
                        time_dim=time_dim,
                        batch_size=batch_size)
                    feed_dict = {
                        FCNs[0]: voxs,
                        voxnet[0]: voxnet_data,
                        voxnet.keep_prob: 1.0,
                        FCNs.keep_prob: 1.0,
                        p['labels']: labels,
                        FCNs.training: False
                    }
                    predictions, y_true = session.run(
                        [p['prediction'], p['y_true']], feed_dict=feed_dict)
                    train_evaluation += evaluation(y_true=y_true,
                                                   y_predict=predictions)
                print('training accuracy \n' + str(train_evaluation))
                num_accuracy_batches = test_size
                test_evaluation = evaluation()
                for x in range(num_accuracy_batches):
                    voxs, labels = dataset.test.random_sampling.get_time_batch(
                        session,
                        voxnet,
                        cut_shape,
                        time_dim=time_dim,
                        batch_size=batch_size)
                    feed_dict = {
                        FCNs[0]: voxs,
                        voxnet[0]: voxnet_data,
                        voxnet.keep_prob: 1.0,
                        FCNs.keep_prob: 1.0,
                        p['labels']: labels,
                        FCNs.training: False
                    }
                    predictions, y_true = session.run(
                        [p['prediction'], p['y_true']], feed_dict=feed_dict)
                    test_evaluation += evaluation(y_true=y_true,
                                                  y_predict=predictions)
                    print(test_evaluation)
                print('test accuracy \n' + str(test_evaluation))
                with open(accuracy_filename, 'a') as f:
                    f.write('checkpoint_num:' + str(checkpoint_num) + ':\n')
                    f.write('train:\n' + str(train_evaluation) + '\n')
                    f.write('test:\n' + str(test_evaluation) + '\n')
                if batch_index % 64 or train_evaluation.ACC >= 0.8 == 0:
                    ######SVM分类器####################
                    svm_feature = np.zeros((train_len + test_len, 128))
                    svm_label = np.zeros(train_len + test_len)
                    for x in range(train_len):
                        voxs, labels = svm_dataset.train.random_sampling.get_time_batch(
                            session,
                            voxnet,
                            cut_shape,
                            time_dim=time_dim,
                            batch_size=1)
                        feed_dict = {
                            FCNs[0]: voxs,
                            voxnet[0]: voxnet_data,
                            voxnet.keep_prob: 1.0,
                            FCNs.keep_prob: 1.0,
                            p['labels']: labels,
                            FCNs.training: False
                        }
                        feature, y_true = session.run(
                            [FCNs['gap'], p['y_true']], feed_dict=feed_dict)
                        feature = np.reshape(feature, [1, 128])
                        svm_feature[x] = feature
                        # print(svm_feature[x])
                        svm_label[x] = y_true
                    for x in range(test_len):
                        voxs, labels = svm_dataset.test.random_sampling.get_time_batch(
                            session,
                            voxnet,
                            cut_shape,
                            time_dim=time_dim,
                            batch_size=1)
                        feed_dict = {
                            FCNs[0]: voxs,
                            voxnet[0]: voxnet_data,
                            voxnet.keep_prob: 1.0,
                            FCNs.keep_prob: 1.0,
                            p['labels']: labels,
                            FCNs.training: False
                        }
                        feature, y_true = session.run(
                            [FCNs['gap'], p['y_true']], feed_dict=feed_dict)
                        feature = np.reshape(feature, [1, 128])
                        svm_feature[train_len + x] = feature
                        svm_label[train_len + x] = y_true
                    # print(svm_feature[0:train_len])
                    # print(svm_label[0:train_len])
                    clf = svm.SVC(C=1.0, kernel='rbf', gamma='auto')
                    clf.fit(svm_feature[0:train_len], svm_label[0:train_len])
                    predictions = clf.predict(svm_feature)
                    svm_train_evaluation = evaluation(
                        y_true=svm_label[:train_len],
                        y_predict=predictions[:train_len])
                    svm_test_evaluation = evaluation(
                        y_true=svm_label[train_len:],
                        y_predict=predictions[train_len:])
                    print('svm_train:\n' + str(svm_train_evaluation))
                    print('svm_test:\n' + str(svm_test_evaluation))
                    with open(accuracy_filename, 'a') as f:
                        f.write('svm_train:\n' + str(svm_train_evaluation) +
                                '\n')
                        f.write('svm_test:\n' + str(svm_test_evaluation) +
                                '\n')
                    #################################################

                # fr.write('test accuracy: {}'.format(test_accuracy))

                if batch_index % 128 == 0 or train_evaluation.ACC >= 0.85:
                    print('saving checkpoint {}...'.format(checkpoint_num))
                    filename = 'cx-{}.npz'.format(checkpoint_num)
                    filename = os.path.join(cfg.fcn_checkpoint_dir, filename)
                    FCNs.npz_saver.save(session, filename)
                    print('checkpoint saved!')
                    checkpoint_num += 1
                    if train_evaluation.ACC >= 0.85:
                        break
            end = time.time()
            print('time:', (end - start) / 60)
    return test_evaluation
Example #21
0
def train(etd_factor, segment=False):
    env = ENV()
    # 环境初始化
    net_map, task_list, test_task_list = env.net_map, env.task_list, env.test_task_list
    neighbors_list = env.neighbor_list
    # NET_STATES = np.array([random.randint(1, 3) for _ in node_list])
    # 创建邻接节点的list
    # neighbors_list = []
    # for k in node_list:
    #     tmp_ = []
    #     for n in node_list:
    #         if net_map[k, n] != 0:
    #             tmp_.append(n)
    #     neighbors_list.append(tmp_)
    # 创建Agent
    # agent = Agent(n_actions=n_action, n_features=n_features)
    agent = DDPG(2, n_features, DPG_bounds, env)
    # task

    # 记录评估
    evaluation_his = []
    x = []
    with open('record.txt', 'w+') as fp:
        fp.write(
            'Iteration\t\tCost\t\tCounter\t\tDelay\t\tEnergyConsumption\n')
        fp.write('-' * 50)
        fp.write('\n')

    time_counter = 1
    netUpdateFlag = False
    step = 0
    for i in range(iterations):
        task_index = np.random.randint(0, len(task_list))
        task = task_list[task_index]
        step_counter = 0
        observation = _init_observation(task, env)
        des_node = task[3]['des_node']

        tmp_path = env.path_list[des_node - 40]

        ec = 0
        delay = 0
        # Tabu = []
        while True:
            present_node = one_hot_decode(observation[4:4 + NODE_NUM])
            if time_counter % change_rounds == 0:
                netUpdateFlag = True

            if segment:
                if observation[0] > 0:
                    action = agent.choose_action(observation)
                else:
                    action = tmp_path[present_node]
            else:
                # try process
                # 确定该节点的有效邻接节点
                action = agent.choose_action(
                    observation)  # TODO(Wezi): check the action dimension
            result, ec_, delay_ = env.perceive(
                observation, action, etd_factor,
                netUpdateFlag)  # result = [r,s']
            netUpdateFlag = False
            ec += ec_
            delay += delay_
            agent.store_transition(observation, action, result[0], result[1])
            # print(result[0])
            # if action <= max(node_list):
            #     step_counter += 1
            time_counter += 1
            step += 1
            step_counter += 1
            observation = result[1]
            if step > 200 and (step % 10 == 0):
                # DQN学习过程
                agent.learn()

            if one_hot_decode(observation[4:54]) == des_node:
                break

            # 保存模型
            if i % 200 == 0 and i >= 400:
                agent.saveModel(i)

        if i >= 300 and i % 100 == 0:
            res_cost, res_counter, latency, res_ec = evaluation(agent,
                                                                env,
                                                                test_task_list,
                                                                neighbors_list,
                                                                change_rounds,
                                                                segment=False)
            with open('record.txt', 'a+') as fp:
                fp.write('%d\t\t%f\t\t%f\t\t%f\t\t%f\n' %
                         (i, res_cost, res_counter, latency, res_ec))
            evaluation_his.append([res_cost, res_counter, latency, res_ec])
            x.append(i)

        # if i > 500 and (i % 1000 == 0):
        #     print(agent_list[0].DQN.fetch_eval(np.array(initial_observation)))

        print("the %d time cost %d rounds!" % (i + 1, step_counter + 1),
              end='')
        print("the ec:\t%f\tthe delay:\t%f" % (ec, delay))
    # 记录
    cost_his = [each[0] for each in evaluation_his]
    counter_his = [each[1] for each in evaluation_his]
    latency_his = [each[2] for each in evaluation_his]
    ec_his = [each[3] for each in evaluation_his]

    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax1.plot(x, cost_his)
    ax1.set_title('cost_his')
    ax2 = fig.add_subplot(222)
    ax2.plot(x, counter_his)
    ax2.set_title('round_his')
    ax3 = fig.add_subplot(223)
    ax3.plot(x, latency_his)
    ax3.set_title('latency_his')
    ax4 = fig.add_subplot(224)
    ax4.plot(x, ec_his)
    ax4.set_title('ec_his')
        f.write('\n')
        f.write("Bleu_2:" + str(scores['Bleu_2']))
        f.write('\n')
        f.write("Bleu_3:" + str(scores['Bleu_3']))
        f.write('\n')
        f.write("Bleu_4:" + str(scores['Bleu_4']))
        f.write('\n')
        f.write("ROUGE_L:" + str(scores['ROUGE_L']))
        f.write('\n')
        f.write("CIDEr:" + str(scores['CIDEr']))
        f.write('\n')
        f.write("METEOR:" + str(scores['METEOR']))
        f.write('\n')
        f.write("metric:" + str(
            1 * scores['METEOR'] ))
        f.write('\n')
        f.write('\n')
    print 'CIDEr: ', scores['CIDEr']

if __name__ == '__main__':
    args = parse_args()
    if args.task == 'train':
        with tf.device('/gpu:' + str(args.gpu_id)):
            train()
    elif args.task == 'test':
        with tf.device('/gpu:' + str(args.gpu_id)):
            test()
    elif args.task == 'evaluate':
        with tf.device('/gpu:' + str(args.gpu_id)):
            evaluation()
Example #23
0
def main():
    # For reproducability
    np.random.seed(1)
    # 3 Decimal points in all numpy print statements
    np.set_printoptions(precision=3)
    # Default values for args
    task = 'creation'
    dataset = 'clean'
    # Otherwise use provided args
    if len(sys.argv[1:]) != 0:
        task = sys.argv[1:2][0].lower()
        dataset = sys.argv[2:3][0].lower()
    # Load data: Arrays of 2000x8
    if dataset == 'clean':
        data = np.loadtxt('wifi_db/clean_dataset.txt')
    elif dataset == 'noisy':
        data = np.loadtxt('wifi_db/noisy_dataset.txt')
    else:
        # Whole path to unknown to us dataset text file
        data = np.loadtxt(sys.argv[2:3][0])

    print("Running {} on {} dataset...".format(task, dataset))
    # Check task to perform
    if task == 'creation':
        # Initialisation of depth variable to be incremented by Decision Tree algorithm
        depth_val = 0
        # Method to produce a Tree
        tree, depth_val = decision_tree_learning(data, depth_val)
        print("Depth of created tree:", depth_val)
    elif task == 'visualization':
        # Initialisation of depth variable to be incremented by Decision Tree algorithm
        depth_val = 0
        # Method to produce a Tree
        tree, depth_val = decision_tree_learning(data, depth_val)
        print("Depth of created tree:", depth_val)
        # Visualization of Tree created above
        visualization.visualizeTree(tree, depth_val)
    elif task == 'evaluation':
        avg_recall, avg_precision, avg_f1_score, avg_class_rate = evaluation(
            data)
        print(
            '----------- Performance Metrics after K Fold Validation -----------'
        )
        print("Average Recall for each Class:", avg_recall)
        print("Average Precision for each Class:", avg_precision)
        print("Average F1 Score for each Class:", avg_f1_score)
        print(
            "Average Classification Rate of K Fold Validation: {:.3f}".format(
                avg_class_rate))
    elif task == 'pruning':
        avg_recall, avg_precision, avg_f1_score, avg_class_rate = evaluation(
            data, pruning=True)
        print(
            '----------- Performance Metrics after K Fold Validation -----------'
        )
        print("Average Recall for each Class:", avg_recall)
        print("Average Precision for each Class:", avg_precision)
        print("Average F1 Score for each Class:", avg_f1_score)
        print(
            "Average Classification Rate of K Fold Validation: {:.3f}".format(
                avg_class_rate))
Example #24
0
def main(args):
    time1 = datetime.now()
    raw_corpus = corpus.read_corpus(args.corpus)
    list_words, vocab_map, embeddings, padding_id = corpus.load_embeddings(
        corpus.load_embedding_iterator(args.embeddings))
    print("loaded embeddings")
    ids_corpus = corpus.map_corpus(vocab_map, raw_corpus)
    annotations = corpus.read_annotations(args.train)
    print("got annotations")

    training_batches = corpus.create_batches(ids_corpus, annotations,
                                             args.batch_size, padding_id)
    print("got batches")

    time2 = datetime.now()
    print "time to preprocess: " + str(time2 - time1)

    if args.model == 'cnn':
        args.margin = 0.2

    if args.load_model:
        if args.model == 'lstm':
            print("loading " + args.load_model)
            lstm = nn.LSTM(input_size=args.embedding_size,
                           hidden_size=args.hidden_size)
            lstm.load_state_dict(torch.load(args.load_model))
            optimizer = Adam(lstm.parameters())
            if args.cuda:
                lstm.cuda()
        else:
            print("loading " + args.load_model)
            cnn = nn.Conv1d(in_channels=args.embedding_size,
                            out_channels=args.hidden_size,
                            kernel_size=3,
                            padding=1)
            cnn.load_state_dict(torch.load(args.load_model))
            optimizer = Adam(cnn.parameters())
            if args.cuda:
                cnn.cuda()
    else:
        if args.model == 'lstm':
            print "training lstm"
            lstm = nn.LSTM(input_size=args.embedding_size,
                           hidden_size=args.hidden_size)
            optimizer = Adam(lstm.parameters())
            if args.cuda:
                lstm.cuda()
        else:
            print "training cnn"
            cnn = nn.Conv1d(in_channels=args.embedding_size,
                            out_channels=args.hidden_size,
                            kernel_size=3,
                            padding=1)
            optimizer = Adam(cnn.parameters())
            if args.cuda:
                cnn.cuda()

    if args.save_model:
        if args.model == 'lstm':
            lstm_model_nums = []
            for d in os.listdir("lstm_models"):
                if "lstm_model" in d:
                    num = int(d[len("lstm_models") - 1:])
                    lstm_model_nums.append(num)
            if len(lstm_model_nums) > 0:
                new_model_num = max(lstm_model_nums) + 1
            else:
                new_model_num = 0
            print("creating new model " + "lstm_models/lstm_model" +
                  str(new_model_num))
            os.makedirs("lstm_models/lstm_model" + str(new_model_num))
        else:
            cnn_model_nums = []
            for d in os.listdir("cnn_models"):
                if "cnn_model" in d:
                    num = int(d[len("cnn_models") - 1:])
                    cnn_model_nums.append(num)
            if len(cnn_model_nums) > 0:
                new_model_num = max(cnn_model_nums) + 1
            else:
                new_model_num = 0
            print("creating new model " + "cnn_models/cnn_model" +
                  str(new_model_num))
            os.makedirs("cnn_models/cnn_model" + str(new_model_num))

    # lstm tutorial: http://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html
    # lstm documentation: http://pytorch.org/docs/master/nn.html?highlight=nn%20lstm#torch.nn.LSTM

    count = 1
    hidden_states = []
    total_loss = 0.0
    time_begin = datetime.now()
    for epoch in range(10):
        print "epoch = " + str(epoch)
        for batch in training_batches:
            optimizer.zero_grad()
            if count % 10 == 0:
                print(count)
                print "average loss: " + str((total_loss / float(count)))
                print("time for 10 batches: " +
                      str(datetime.now() - time_begin))
                time_begin = datetime.now()
            titles, bodies, triples = batch
            title_length, title_num_questions = titles.shape
            body_length, body_num_questions = bodies.shape
            title_embeddings, body_embeddings = corpus.get_embeddings(
                titles, bodies, vocab_map, embeddings)

            # title
            if args.model == 'lstm':
                if args.cuda:
                    title_inputs = [
                        autograd.Variable(
                            torch.FloatTensor(title_embeddings).cuda())
                    ]
                    title_inputs = torch.cat(title_inputs).view(
                        title_length, title_num_questions, -1)
                    # title_inputs = torch.cat(title_inputs).view(title_num_questions, title_length, -1)

                    title_hidden = (autograd.Variable(
                        torch.zeros(1, title_num_questions,
                                    args.hidden_size).cuda()),
                                    autograd.Variable(
                                        torch.zeros(
                                            (1, title_num_questions,
                                             args.hidden_size)).cuda()))
                else:
                    title_inputs = [
                        autograd.Variable(torch.FloatTensor(title_embeddings))
                    ]
                    title_inputs = torch.cat(title_inputs).view(
                        title_length, title_num_questions, -1)

                    title_hidden = (autograd.Variable(
                        torch.zeros(1, title_num_questions, args.hidden_size)),
                                    autograd.Variable(
                                        torch.zeros((1, title_num_questions,
                                                     args.hidden_size))))
            else:
                if args.cuda:
                    title_inputs = [
                        autograd.Variable(
                            torch.FloatTensor(title_embeddings).cuda())
                    ]
                else:
                    title_inputs = [
                        autograd.Variable(torch.FloatTensor(title_embeddings))
                    ]
                title_inputs = torch.cat(title_inputs).transpose(0,
                                                                 1).transpose(
                                                                     1, 2)

            if args.model == 'lstm':
                title_out, title_hidden = lstm(title_inputs, title_hidden)
            else:
                title_out = cnn(title_inputs)
                title_out = F.tanh(title_out)
                title_out = title_out.transpose(1, 2).transpose(0, 1)

            # average all words of each question from title_out
            # title_out (max sequence length) x (batch size) x (hidden size)
            average_title_out = average_questions(title_out, titles,
                                                  padding_id)

            # body
            if args.model == 'lstm':
                if args.cuda:
                    body_inputs = [
                        autograd.Variable(
                            torch.FloatTensor(body_embeddings).cuda())
                    ]
                    body_inputs = torch.cat(body_inputs).view(
                        body_length, body_num_questions, -1)
                    # body_inputs = torch.cat(body_inputs).view(body_num_questions, body_length, -1)

                    body_hidden = (autograd.Variable(
                        torch.zeros(1, body_num_questions,
                                    args.hidden_size).cuda()),
                                   autograd.Variable(
                                       torch.zeros((1, body_num_questions,
                                                    args.hidden_size)).cuda()))
                else:
                    body_inputs = [
                        autograd.Variable(torch.FloatTensor(body_embeddings))
                    ]
                    body_inputs = torch.cat(body_inputs).view(
                        body_length, body_num_questions, -1)

                    body_hidden = (autograd.Variable(
                        torch.zeros(1, body_num_questions, args.hidden_size)),
                                   autograd.Variable(
                                       torch.zeros((1, body_num_questions,
                                                    args.hidden_size))))
            else:
                if args.cuda:
                    body_inputs = [
                        autograd.Variable(
                            torch.FloatTensor(body_embeddings).cuda())
                    ]
                else:
                    body_inputs = [
                        autograd.Variable(torch.FloatTensor(body_embeddings))
                    ]
                body_inputs = torch.cat(body_inputs).transpose(0, 1).transpose(
                    1, 2)

            if args.model == 'lstm':
                body_out, body_hidden = lstm(body_inputs, body_hidden)
            else:
                body_out = cnn(body_inputs)
                body_out = F.tanh(body_out)
                body_out = body_out.transpose(1, 2).transpose(0, 1)

            average_body_out = average_questions(body_out, bodies, padding_id)
            count += 1

            # average body and title
            # representations of the questions as found by the LSTM
            hidden = (average_title_out + average_body_out) * 0.5
            if args.cuda:
                triples_vectors = hidden[torch.LongTensor(
                    triples.ravel()).cuda()]
            else:
                triples_vectors = hidden[torch.LongTensor(triples.ravel())]
            triples_vectors = triples_vectors.view(triples.shape[0],
                                                   triples.shape[1],
                                                   args.hidden_size)

            query = triples_vectors[:, 0, :].unsqueeze(1)
            examples = triples_vectors[:, 1:, :]

            cos_similarity = F.cosine_similarity(query, examples, dim=2)
            if args.cuda:
                targets = autograd.Variable(
                    torch.zeros(triples.shape[0]).type(
                        torch.LongTensor).cuda())
            else:
                targets = autograd.Variable(
                    torch.zeros(triples.shape[0]).type(torch.LongTensor))
            # outputs a Variable
            # By default, the losses are averaged over observations for each minibatch
            if args.cuda:
                loss = F.multi_margin_loss(cos_similarity,
                                           targets,
                                           margin=args.margin).cuda()
            else:
                loss = F.multi_margin_loss(cos_similarity,
                                           targets,
                                           margin=args.margin)
            total_loss += loss.cpu().data.numpy()[0]
            loss.backward()

            optimizer.step()

        result_headers = ['Epoch', 'MAP', 'MRR', 'P@1', 'P@5']
        with open(os.path.join(sys.path[0], args.results_file),
                  'a') as evaluate_file:
            writer = csv.writer(evaluate_file, dialect='excel')
            writer.writerow(result_headers)

        if args.model == 'lstm':
            evaluation(args, padding_id, ids_corpus, vocab_map, embeddings,
                       lstm, epoch)
        else:
            evaluation(args, padding_id, ids_corpus, vocab_map, embeddings,
                       cnn, epoch)

        if args.save_model:
            # saving the model
            if args.model == 'lstm':
                print "Saving lstm model epoch " + str(
                    epoch) + " to lstm_model" + str(new_model_num)
                torch.save(
                    lstm.state_dict(), "lstm_models/lstm_model" +
                    str(new_model_num) + '/' + "epoch" + str(epoch))
            else:
                print "Saving cnn model epoch " + str(
                    epoch) + " to cnn_model" + str(new_model_num)
                torch.save(
                    cnn.state_dict(), "cnn_models/cnn_model" +
                    str(new_model_num) + '/' + "epoch" + str(epoch))
    return args_in


if __name__ == "__main__":
    args = parse_args()
    # args.dir = ['/Users/wangzhe/data/safe_belt/part1/prediction/', '/Users/wangzhe/data/safe_belt/part1/test_annos/']
    # args.cls = 4
    len(sys.argv)
    print("Your Folder's path: {}".format(args.dir))
    print("Overlap Ratio: {}".format(args.overlapRatio))
    print("Threshold: {}".format(args.threshold))
    print("Num of Categories: {}".format(args.cls))
    print("Precision: {}".format(args.precision))
    print("Recall: {}".format(args.recall))
    print("FPPIW: {}".format(args.FPPIW))

    print("Calculating......")

    cfg['file_dir'] = args.dir
    cfg['overlapRatio'] = args.overlapRatio
    cfg['cls'] = args.cls
    cfg['precision'] = args.precision
    cfg['recall'] = args.recall
    cfg['threshold'] = args.threshold
    cfg['FPPIW'] = args.FPPIW
    cfg['roc'] = args.roc
    cfg['pr'] = args.pr

    eval = evaluation(cfg)
    eval.run()
Example #26
0
#post processed
times1 = timestack_periods(sys.argv[1] + '_timestack1.png_canny.png')
times2 = timestack_periods(sys.argv[1] + '_timestack2.png_canny.png')
times3 = timestack_periods(sys.argv[1] + '_timestack3.png_canny.png')
# #pre-processed lines
times4 = timestack_periods(sys.argv[1] + '_line.mp4_timestack1.png')
times5 = timestack_periods(sys.argv[1] + '_line.mp4_timestack2.png')
times6 = timestack_periods(sys.argv[1] + '_line.mp4_timestack3.png')
# #pre-processed edges
times7 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack1.png')
times8 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack2.png')
times9 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack3.png')

#evaluation / histogram of each time
correct1, percentage1, hist1 = evaluation(
    times1, sys.argv[2], sys.argv[3],
    sys.argv[1] + '_timestack1.png_canny.png')
correct2, percentage2, hist2 = evaluation(
    times2, sys.argv[2], sys.argv[3],
    sys.argv[1] + '_timestack2.png_canny.png')
correct3, percentage3, hist3 = evaluation(
    times3, sys.argv[2], sys.argv[3],
    sys.argv[1] + '_timestack3.png_canny.png')
correct4, percentage4, hist4 = evaluation(
    times4, sys.argv[2], sys.argv[3], sys.argv[1] + '_line.mp4_timestack1.png')
correct5, percentage5, hist5 = evaluation(
    times5, sys.argv[2], sys.argv[3], sys.argv[1] + '_line.mp4_timestack2.png')
correct6, percentage6, hist6 = evaluation(
    times6, sys.argv[2], sys.argv[3], sys.argv[1] + '_line.mp4_timestack3.png')
correct7, percentage7, hist7 = evaluation(
    times7, sys.argv[2], sys.argv[3], sys.argv[1] + '_edge.mp4_timestack1.png')
Example #27
0
def main():
    parser = argparse.ArgumentParser(description='Train Completion Network')
    parser.add_argument('--batch_size', '-b', type=int, default=8)
    parser.add_argument('--max_iter', '-m', type=int, default=500000)
    parser.add_argument('--gpu',
                        '-g',
                        type=int,
                        default=-1,
                        help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--out',
                        '-o',
                        default='result',
                        help='Directory to output the result')
    parser.add_argument('--eval_folder',
                        '-e',
                        default='test',
                        help='Directory to output the evaluation result')

    parser.add_argument('--eval_interval',
                        type=int,
                        default=100,
                        help='Interval of evaluating generator')

    parser.add_argument("--learning_rate",
                        type=float,
                        default=0.0002,
                        help="Learning rate")

    parser.add_argument("--load_model",
                        default='',
                        help='completion model path')

    parser.add_argument("--lambda1",
                        type=float,
                        default=6.0,
                        help='lambda for hole loss')
    parser.add_argument("--lambda2",
                        type=float,
                        default=0.05,
                        help='lambda for perceptual loss')
    parser.add_argument("--lambda3",
                        type=float,
                        default=120.0,
                        help='lambda for style loss')
    parser.add_argument("--lambda4",
                        type=float,
                        default=0.1,
                        help='lambda for tv loss')

    parser.add_argument("--flip",
                        type=int,
                        default=1,
                        help='flip images for data augmentation')
    parser.add_argument("--resize_to",
                        type=int,
                        default=256,
                        help='resize the image to')
    parser.add_argument("--crop_to",
                        type=int,
                        default=256,
                        help='crop the resized image to')
    parser.add_argument("--load_dataset",
                        default='place2_train',
                        help='load dataset')
    #parser.add_argument("--layer_n", type=int, default=7, help='number of layers')

    parser.add_argument("--learning_rate_anneal",
                        type=float,
                        default=0,
                        help='anneal the learning rate')
    parser.add_argument("--learning_rate_anneal_interval",
                        type=int,
                        default=1000,
                        help='time to anneal the learning')

    args = parser.parse_args()
    print(args)

    max_iter = args.max_iter

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

    #load completion model
    model = getattr(net, "PartialConvCompletion")(ch0=3,
                                                  input_size=args.crop_to)

    #load vgg_model
    print("loading vgg16 ...")
    vgg = VGG16Layers()
    print("ok")

    if args.load_model != '':
        serializers.load_npz(args.load_model, model)
        print("Completion model loaded")

    if not os.path.exists(args.eval_folder):
        os.makedirs(args.eval_folder)

    # select GPU
    if args.gpu >= 0:
        model.to_gpu()
        vgg.to_gpu()
        print("use gpu {}".format(args.gpu))

    # Setup an optimizer
    def make_optimizer(model, name="Adam", learning_rate=0.0002):
        #optimizer = chainer.optimizers.AdaDelta()
        #optimizer = chainer.optimizers.SGD(lr=alpha)
        if name == "Adam":
            optimizer = chainer.optimizers.Adam(alpha=learning_rate, beta1=0.5)
        elif name == "SGD":
            optimizer = chainer.optimizer.SGD(lr=learning_rate)
        optimizer.setup(model)
        return optimizer

    opt_model = make_optimizer(model, "Adam", args.learning_rate)

    train_dataset = getattr(datasets,
                            args.load_dataset)(paths.train_place2,
                                               mask_path="mask/256",
                                               flip=args.flip,
                                               resize_to=args.resize_to,
                                               crop_to=args.crop_to)
    train_iter = chainer.iterators.MultiprocessIterator(train_dataset,
                                                        args.batch_size,
                                                        n_processes=4)

    #val_dataset = getattr(datasets, args.load_dataset)(flip=0, resize_to=args.resize_to, crop_to=args.crop_to)
    #val_iter = chainer.iterators.MultiprocessIterator(
    #    val_dataset, args.batchsize, n_processes=4)

    #test_dataset = horse2zebra_Dataset_train(flip=args.flip, resize_to=args.resize_to, crop_to=args.crop_to)

    test_iter = chainer.iterators.SerialIterator(train_dataset, 8)

    # Set up a trainer
    updater = Updater(
        models=(vgg, model),
        iterator={
            'main': train_iter,
            #'dis' : train2_iter,
            'test': test_iter
        },
        optimizer={
            'model': opt_model,
        },
        device=args.gpu,
        params={
            'lambda1': args.lambda1,
            'lambda2': args.lambda2,
            'lambda3': args.lambda3,
            'lambda4': args.lambda4,
            'image_size': args.crop_to,
            'eval_folder': args.eval_folder,
            #'learning_rate_anneal' : args.learning_rate_anneal,
            #'learning_rate_anneal_interval' : args.learning_rate_anneal_interval,
            'dataset': train_dataset
        })

    model_save_interval = (4000, 'iteration')
    trainer = training.Trainer(updater, (max_iter, 'iteration'), out=args.out)
    #trainer.extend(extensions.snapshot_object(
    #    gen_g, 'gen_g{.updater.iteration}.npz'), trigger=model_save_interval)
    trainer.extend(extensions.snapshot_object(model,
                                              'model{.updater.iteration}.npz'),
                   trigger=model_save_interval)

    log_keys = [
        'epoch', 'iteration', 'L_valid', 'L_hole', 'L_perceptual', 'L_style',
        'L_tv'
    ]
    trainer.extend(
        extensions.LogReport(keys=log_keys, trigger=(20, 'iteration')))
    trainer.extend(extensions.PrintReport(log_keys), trigger=(20, 'iteration'))
    trainer.extend(extensions.ProgressBar(update_interval=50))

    trainer.extend(evaluation(model, args.eval_folder,
                              image_size=args.crop_to),
                   trigger=(args.eval_interval, 'iteration'))

    # Run the training
    trainer.run()
Example #28
0
    total_loss = 0
    optimizer.zero_grad()
    loss = model.loss()
    loss.backward()
    optimizer.step()

# print(final_adj_in)
hit_t = []
mrr_t = []
model.eval()
for time in testTimes:
    test = get_quadruple_t(testList, time)
    # add time decay factor
    ent_output, rel_output = model()
    # print(torch.sum(torch.sum(ent_output)))
    hit10, mrr = evaluation(test,
                            quadrupleDict,
                            ent_output.detach().numpy(),
                            rel_output.detach().numpy(),
                            filter=True,
                            head=0)
    hit_t.append(hit10)
    mrr_t.append(mrr)

with open('result.txt', 'w') as f:
    f.write("hit\n")
    f.write("%s\n" % hit_t)
    f.write("%s\n" % (sum(hit_t) / float(len(hit_t))))
    f.write("mr\n")
    f.write("%s\n" % mrr_t)
    f.write("%s\n" % (sum(mrr_t) / float(len(mrr_t))))
def train(model, Model_Parameters, train_idx_data, val_idx_data,
          test_idx_data):
    np.random.seed(Model_Parameters['random_seed'])
    # build the model and return the optimizer
    model.build()

    num_epochs = Model_Parameters['train_epochs']
    batch_size = Model_Parameters['batch_size']
    num_train = len(train_idx_data)
    best_val_f1 = -np.inf
    best_test_f1 = -np.inf
    loss_train_record = []
    loss_val_record = []
    loss_test_record = []
    f1_train_record = []
    f1_val_record = []
    f1_test_record = []

    saver = tf.train.Saver()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    # begin training
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        if Model_Parameters['restore_mode_path']:
            saver.restore(sess, Model_Parameters['restore_mode_path'])
        start = time.time()
        counter_early_stop = 0

        #decay_ratio = 0.9
        for epoch in xrange(num_epochs):
            print 'Iteration: ', epoch
            permutation_train_idx = np.random.permutation(num_train)

            improve_flag = False
            total_loss = 0
            char_loss = 0
            word_loss = 0
            tp_chunk_all = 0
            gold_chunks_all = 0
            pred_chunks_all = 0
            batch_count = 0
            train_data_count = 0
            # counting var for freq calculation
            total_loss_freq = 0
            batch_count_freq = 0
            tp_chunk_freq = 0
            gold_chunks_freq = 0
            pred_chunks_freq = 0
            start_epoch = time.time()
            while train_data_count < num_train:
                # get batch
                batch_data = []
                for i in range(batch_size):
                    index = i + train_data_count
                    if index >= len(permutation_train_idx):
                        continue
                        # index %= len(permutation_train_idx)
                    batch_data.append(
                        train_idx_data[permutation_train_idx[index]])

                # increment counting variables
                train_data_count += batch_size
                batch_count += 1
                batch_count_freq += 1
                # get feed_dict
                feed_dict_ = get_feed_dict(model, Model_Parameters, batch_data)
                # training
                sess.run([model.total_train_op], feed_dict=feed_dict_)

                pred_tags, loss_list = predict(model, sess, feed_dict_)

                total_loss += sum(loss_list)
                total_loss_freq += sum(loss_list)
                if len(loss_list) > 1:
                    char_loss += loss_list[0]
                    word_loss += loss_list[1]
                # count chunks
                for idx in xrange(len(pred_tags)):
                    seq_length = feed_dict_[model.sequence_lengths][idx]
                    y_real = feed_dict_[model.tag_input_ids][idx][:seq_length]
                    y_pred = pred_tags[idx]
                    assert (len(y_real) == len(y_pred))
                    tp_chunk_batch, gold_chunk_batch, pred_chunk_batch = eval_conll(
                        y_real, y_pred, Model_Parameters['id_to_word_tag'])

                    tp_chunk_all += tp_chunk_batch
                    gold_chunks_all += gold_chunk_batch
                    pred_chunks_all += pred_chunk_batch

            # things to do between epoch
            prec = 0 if pred_chunks_all == 0 else 1. * tp_chunk_all / pred_chunks_all
            recl = 0 if gold_chunks_all == 0 else 1. * tp_chunk_all / gold_chunks_all
            f1 = 0 if prec + recl == 0 else (2. * prec * recl) / (prec + recl)

            cost_time_total = time.time() - start
            cost_time_epoch = time.time() - start_epoch
            print '\n*****Epoch: %i, precision: %6.2f%%, recall: %6.2f%%, f1 score: %6.2f%%, total cost time: %i, epoch cost time: %i, total loss: %6.6f, char loss: %6.6f, word loss: %6.6f' % (
                epoch, 100. * prec, 100. * recl, 100. * f1, cost_time_total,
                cost_time_epoch, total_loss / batch_count,
                char_loss / batch_count, word_loss / batch_count)
            print '*****Epoch Evaluating on val set: ',
            val_f1, val_loss, val_trues, val_preds = evaluation(
                sess, model, Model_Parameters, val_idx_data)
            if val_f1 > best_val_f1:
                improve_flag = True
                counter_early_stop = 0
                best_val_f1 = val_f1
                print '*****New best F1 score on val data!'
                if Model_Parameters['save_model_path']:
                    print ' Saving current model to disk ...'
                    saver.save(sess,
                               Model_Parameters['save_model_path'] +
                               'model.ckpt',
                               global_step=epoch)
            print '*****Evaluting on test set: ',
            test_f1, test_loss, test_trues, test_preds = evaluation(
                sess, model, Model_Parameters, test_idx_data)
            if test_f1 > best_test_f1:
                best_test_f1 = test_f1
                print '*****New best F1 score on test data!'
                if Model_Parameters['save_predict_path']:
                    print '**test prediction results saved!'
                    cPickle.dump(
                        test_trues,
                        open(
                            Model_Parameters['save_predict_path'] +
                            'test_true_results_' + str(round(test_f1, 4)) +
                            '.pkl', 'wb'))
                    cPickle.dump(
                        test_preds,
                        open(
                            Model_Parameters['save_predict_path'] +
                            'test_pred_results_' + str(round(test_f1, 4)) +
                            '.pkl', 'wb'))

            if Model_Parameters['fig_name']:
                loss_train_record.append(total_loss / batch_count)
                loss_val_record.append(val_loss)
                loss_test_record.append(test_loss)
                f1_train_record.append(100. * f1)
                f1_val_record.append(100. * val_f1)
                f1_test_record.append(100. * test_f1)
                plotting_storing(Model_Parameters, loss_train_record,
                                 loss_val_record, loss_test_record,
                                 f1_train_record, f1_val_record,
                                 f1_test_record)

            if Model_Parameters['weight_decay']:
                #if Model_Parameters['lr_method'] == 'sgd' or Model_Parameters['lr_method'] == 'momentum':
                model.params['lr_rate'] *= Model_Parameters['weight_decay']
                print('new_learning_rate', model.params['lr_rate'])

            if not improve_flag:
                counter_early_stop += 1
                if counter_early_stop >= Model_Parameters['patiences']:
                    print 'Early stopping at iteration: %d!' % (epoch)
                    break
            print '-------------------------------------------------------------'

    return
Example #30
0
def train_maze(env,
               EPSILON=0.10,
               EPISODE_EVALUATION=500,
               EPISODES=5000,
               findOptimal=False):
    '''
    env: Maze environment
    EPSILON: Determines epsilon for epsilon greedy algorithm. Higher the value, the less greedy [0,1]
    EPISODE_EVALUATION: Determines the number of episodes before we will evaluate how well the maze is doing
    EPISODES: Number of episodes that the learning algorith will run
    '''
    tmpEval = []
    q_table = np.random.uniform(low=0, high=3, size=(env.snum, env.anum))
    if findOptimal is False:
        q_optimal = np.load('Q_maze.npy', allow_pickle=True)

    else:
        # Throwaway thing, just to speed up the coding
        q_optimal = np.copy(q_table)
    RMS = []

    print(q_table.shape)

    for episode in range(EPISODES):
        done = False
        state = env.reset()

        # Stop training to do an evaluation
        if episode % EPISODE_EVALUATION == 0:
            print(f"\nEpisode {episode}")
            avg_step, avg_reward = evaluation(env, q_table)
            print(f"Avg Step: {avg_step} \nAvg Reward: {avg_reward}")
            tmpEval.append((episode, avg_step, avg_reward))
            time.sleep(1)
            continue

        while not done:
            values = q_table[state]
            action = get_action_egreedy(values, EPSILON)
            reward, new_state, done = env.step(state, action)

            if not done:
                max_future_q = np.max(q_table[new_state])
                current_q = q_table[(state, action)]
                new_q = (1 -
                         env.LEARNING_RATE) * current_q + env.LEARNING_RATE * (
                             reward + env.DISCOUNT_RATE * max_future_q)
                q_table[(
                    state, action
                )] = new_q  # This was the critical step. You are updating the current Q state, not the future one!
            elif done:  # Not too sure about how to give reward on the maze, and what the condition should be
                q_table[(new_state,
                         action)] = reward  ##Not too sure what o do here
                RMS.append((episode, RMSE(q_table, q_optimal)))
                # Automatically, this is an optimal state! This is the recursive definition at the end goal I believe.
            state = new_state

    realEval = np.array(tmpEval)
    RMS = np.array(RMS)
    print(RMS.shape)
    fig, axs = plt.subplots(1, 3)
    fig.suptitle(
        "Evaluation metrics for the Maze puzzle with current Q-Learning strategy"
    )

    axs[0].plot(realEval[..., 0], realEval[..., 1])
    axs[0].set_xlabel("Episodes")
    axs[0].set_ylabel("Steps")
    axs[0].set_xlim((0, EPISODES))
    axs[0].set_ylim((0, 100))
    axs[0].set_title("Number of Steps")
    #TODO: Implement curve fitting, to make the plots more reasonable

    axs[1].plot(realEval[..., 0], realEval[..., 2])
    axs[1].set_xlabel("Episodes")
    axs[1].set_ylabel("Reward")
    axs[1].set_xlim((0, EPISODES))
    axs[1].set_ylim((0, 3))
    axs[1].set_title("Reward (0.0 - 3.0)")

    axs[2].plot(RMS[..., 0], RMS[..., 1])
    axs[2].set_xlabel("Episodes")
    axs[2].set_ylabel("RMSE")
    axs[2].set_xlim((0, EPISODES))
    axs[2].set_ylim((0, np.max(RMS[..., 1])))
    axs[2].set_title("Root mean squared error")

    plt.show()
    print("Finished")

    return q_table, realEval, RMS
Example #31
0
def train(sess, g, args, saver, data_loader):
    print('training unsupervised')

    f_log = open(os.path.join(args.save_dir, 'log'), 'w')

    summary_dir = os.path.join(args.save_dir, 'summary')
    if not os.path.exists(summary_dir):
        os.makedirs(summary_dir)
    summary_writer = tf.summary.FileWriter(summary_dir, g.graph)
    max_acc = 0.0
    for step in list(range(1, args.step + 1)):
        lr = args.discriminator_lr
        #train discriminator for several iter
        for i in range(args.discriminator_iterations):
            #sample source and real data
            x, x_len = data_loader.get_idx_batch(args.batch_size)
            y, y_len = data_loader.get_target_batch(args.batch_size)

            feed_dict = {
                g.inputs: x,
                g.seq_length: x_len,
                g.real_sample: y,
                g.real_seq_length: y_len,
                g.learning_rate: lr
            }

            _, discriminator_loss, summary, gs = sess.run([
                g.train_discriminator_op, g.discriminator_loss,
                g.discriminator_summary, g.discriminator_global_step
            ],
                                                          feed_dict=feed_dict)

            #write summary
            summary_writer.add_summary(summary, gs)

        #train generator and reconstructor
        x, x_len = data_loader.get_idx_batch(args.batch_size)
        y, y_len = data_loader.get_target_batch(args.batch_size)

        lr = args.generator_lr

        feed_dict = {
            g.inputs: x,
            g.seq_length: x_len,
            g.real_sample: y,
            g.real_seq_length: y_len,
            g.learning_rate: lr
        }
        _, generator_loss, summary, gs = sess.run([
            g.train_generator_op, g.generator_loss, g.generator_summary,
            g.generator_global_step
        ],
                                                  feed_dict=feed_dict)

        # loss
        #write summary
        summary_writer.add_summary(summary, gs)
        #evaluate mapping id
        mapping_id = sess.run([g.mapping_id], feed_dict=feed_dict)

        if step % 100 == 0:
            log = 'Step: {0:3d} generator_loss: {1:5f} discriminator_loss: {2:5f}'.format(
                step, generator_loss, discriminator_loss)
            print(log)
            f_log.write(log)
            f_log.write('\n')
            f_log.flush()

            print_mapping_id_accuracy(mapping_id[0],
                                      data_loader.idx2phn,
                                      f_log=f_log)
        if step % 200 == 0:
            print(mapping_id[0])
            print('evaluating step {0}'.format(step))
            acc = evaluation(sess, g, args, data_loader, f_log=f_log)
            if acc > max_acc:
                max_acc = acc
                saver.save(sess, os.path.join(args.save_dir, 'model'))
    f_log.close()
    evaluation(sess, g, args, data_loader, f_log=open(result_file, 'w'))
Example #32
0
#9 timestack images to calculate periods of
#post processed
times1 = timestack_periods(sys.argv[1] + '_timestack1.png_canny.png')
times2 = timestack_periods(sys.argv[1] + '_timestack2.png_canny.png')
times3 = timestack_periods(sys.argv[1] + '_timestack3.png_canny.png')
#pre-processed lines
times4 = timestack_periods(sys.argv[1] + '_line.mp4_timestack1.png')
times5 = timestack_periods(sys.argv[1] + '_line.mp4_timestack2.png')
times6 = timestack_periods(sys.argv[1] + '_line.mp4_timestack3.png')
#pre-processed edges
times7 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack1.png')
times8 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack2.png')
times9 = timestack_periods(sys.argv[1] + '_edge.mp4_timestack3.png')

#evaluation / histogram of each time
correct1, percentage1 = evaluation(times1, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_timestack1.png_canny.png')
correct2, percentage2 = evaluation(times2, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_timestack2.png_canny.png')
correct3, percentage3 = evaluation(times3, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_timestack3.png_canny.png')
correct4, percentage4 = evaluation(times4, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_line.mp4_timestack1.png')
correct5, percentage5 = evaluation(times5, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_line.mp4_timestack2.png')
correct6, percentage6 = evaluation(times6, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_line.mp4_timestack3.png')
correct7, percentage7 = evaluation(times7, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_edge.mp4_timestack1.png')
correct8, percentage8 = evaluation(times8, sys.argv[2], sys.argv[3],
                                   sys.argv[1] + '_edge.mp4_timestack2.png')
correct9, percentage9 = evaluation(times9, sys.argv[2], sys.argv[3],
Example #33
0
                    bds[i]) + '.fits'
                AName = drMixture + drNum + rlpath + 'A_bd' + str(
                    bds[i]) + '.fits'
                SName = drSources + drNum + rlpath + 'S_bd' + str(
                    bds[i]) + '.fits'
                Ae = fits.getdata(AeName)
                A = fits.getdata(AName)
                Se = fits.getdata(SeName)
                Se = np.reshape(Se, (Se.shape[0], np.size(Se[0])))
                S = fits.getdata(SName)
                S = np.reshape(S, (S.shape[0], np.size(S[0])))
                noise = fits.getdata(noiseName)
                #             noise = fits.getdata(drNoise+'/n'+str(n)+'/noise_db'+str(db[0])+'_sigS'+str(sigS)+'_r'+str(r)+'.fits')
                criteria, decomposition, delta_rl[
                    db, n, i, r], Ae_ord, Se_ord = evaluation((Ae, Se),
                                                              (A, S, noise),
                                                              verbose=0)

                SDR_rl[db, n, i, r] = criteria['SDR_S']
                SIR_rl[db, n, i, r] = criteria['SIR_S']
                SNR_rl[db, n, i, r] = criteria['SNR_S']
                SAR_rl[db, n, i, r] = criteria['SAR_S']

                #             deltaRl[j,i,r]=abs(abs(linalg.inv(Ae.T.dot(Ae)).dot(Ae.T).dot(A)) - np.eye(n)).sum() / (n*n)
                #             fits.writeto(AeName,Ae_ord,clobber=True)
                #             fits.writeto(SeName,Se_ord,clobber=True)
                if not os.path.exists(drEvalResult + subdr + rlpath):
                    os.makedirs(drEvalResult + subdr + rlpath)
                fits.writeto(drEvalResult + subdr + rlpath + 'estA_bd' +
                             str(bds[i]) + '.fits',
                             Ae_ord,
Example #34
0
def main(argv):
    del argv  # Unused.

    # Configure parameters.
    config = mask_rcnn_params.default_config()
    config = params_io.override_hparams(config, FLAGS.config)

    if FLAGS.use_tpu:
        tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(
            FLAGS.tpu, zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)
        tpu_grpc_url = tpu_cluster_resolver.get_master()
        tf.Session.reset(tpu_grpc_url)
    else:
        tpu_cluster_resolver = None

    # Check data path
    if (FLAGS.mode in ('train', 'train_and_eval')
            and not config.training_file_pattern):
        raise RuntimeError(
            'You must specify `training_file_pattern` for training.')
    if FLAGS.mode in ('eval', 'train_and_eval'):
        if not config.validation_file_pattern:
            raise RuntimeError('You must specify `validation_file_pattern` '
                               'for evaluation.')
        if not config.val_json_file:
            raise RuntimeError(
                'You must specify `val_json_file` for evaluation.')

    # The following is for spatial partitioning. `features` has one tensor while
    # `labels` has 4 + (`max_level` - `min_level` + 1) * 2 tensors. The input
    # partition is performed on `features` and all partitionable tensors of
    # `labels`, see the partition logic below.
    # Note: In the below code, TPUEstimator uses both `shard` and `replica` (with
    # the same meaning).
    if FLAGS.input_partition_dims:
        labels_partition_dims = {
            'gt_boxes': None,
            'gt_classes': None,
            'cropped_gt_masks': None,
        }
        # TODO(b/119617317): The Input Partition Logic. We partition only the
        # partition-able tensors. Spatial partition requires that the
        # to-be-partitioned tensors must have a dimension that is a multiple of
        # `partition_dims`. Depending on the `partition_dims` and the `image_size`
        # and the `max_level` in config, some high-level anchor labels (i.e.,
        # `cls_targets` and `box_targets`) cannot be partitioned. For example, when
        # `partition_dims` is [1, 4, 2, 1], image size is 1536, `max_level` is 9,
        # `cls_targets_8` has a shape of [batch_size, 6, 6, 9], which cannot be
        # partitioned (6 % 4 != 0). In this case, the level-8 and level-9 target
        # tensors are not partition-able, and the highest partition-able level is 7.
        image_size = config.image_size
        for level in range(config.min_level, config.max_level + 1):

            def _can_partition(spatial_dim):
                partitionable_index = np.where(
                    spatial_dim % np.array(FLAGS.input_partition_dims) == 0)
                return len(partitionable_index[0]) == len(
                    FLAGS.input_partition_dims)

            assert len(image_size) == 2
            spatial_dim = [d // (2**level) for d in image_size]
            if _can_partition(spatial_dim[0]) and _can_partition(
                    spatial_dim[1]):
                labels_partition_dims['box_targets_%d' %
                                      level] = FLAGS.input_partition_dims
                labels_partition_dims['score_targets_%d' %
                                      level] = FLAGS.input_partition_dims
            else:
                labels_partition_dims['box_targets_%d' % level] = None
                labels_partition_dims['score_targets_%d' % level] = None
        num_cores_per_replica = np.prod(FLAGS.input_partition_dims)
        image_partition_dims = [
            FLAGS.input_partition_dims[i] for i in [1, 2, 3, 0]
        ] if FLAGS.transpose_input else FLAGS.input_partition_dims
        features_partition_dims = {
            'images': image_partition_dims,
            'source_ids': None,
            'image_info': None,
        }
        input_partition_dims = [features_partition_dims, labels_partition_dims]
        num_shards = FLAGS.num_cores // num_cores_per_replica
    else:
        num_cores_per_replica = None
        input_partition_dims = None
        num_shards = FLAGS.num_cores
    params = dict(
        config.values(),
        num_shards=num_shards,
        use_tpu=FLAGS.use_tpu,
        mode=FLAGS.mode,
        # The following are used by the host_call function.
        model_dir=FLAGS.model_dir,
        iterations_per_loop=FLAGS.iterations_per_loop,
        transpose_input=FLAGS.transpose_input)

    tpu_config = tf.contrib.tpu.TPUConfig(
        FLAGS.iterations_per_loop,
        num_shards=num_shards,
        num_cores_per_replica=num_cores_per_replica,
        input_partition_dims=input_partition_dims,
        per_host_input_for_training=tf.contrib.tpu.InputPipelineConfig.
        PER_HOST_V2)

    run_config = tf.contrib.tpu.RunConfig(
        cluster=tpu_cluster_resolver,
        evaluation_master=FLAGS.eval_master,
        model_dir=FLAGS.model_dir,
        log_step_count_steps=FLAGS.iterations_per_loop,
        tpu_config=tpu_config,
    )

    if FLAGS.mode == 'train':
        if FLAGS.model_dir:
            save_config(config, FLAGS.model_dir)

        tf.logging.info(params)
        train_estimator = tf.contrib.tpu.TPUEstimator(
            model_fn=mask_rcnn_model.mask_rcnn_model_fn,
            use_tpu=FLAGS.use_tpu,
            train_batch_size=config.train_batch_size,
            config=run_config,
            params=params)
        train_estimator.train(input_fn=dataloader.InputReader(
            config.training_file_pattern,
            mode=tf.estimator.ModeKeys.TRAIN,
            use_fake_data=FLAGS.use_fake_data,
            use_instance_mask=config.include_mask),
                              max_steps=config.total_steps)

        if FLAGS.eval_after_training:
            # Run evaluation after training finishes.
            eval_params_dict = dict(
                params,
                use_tpu=FLAGS.use_tpu,
                input_rand_hflip=False,
                is_training_bn=False,
                transpose_input=False,
            )

            eval_estimator = tf.contrib.tpu.TPUEstimator(
                model_fn=mask_rcnn_model.mask_rcnn_model_fn,
                use_tpu=FLAGS.use_tpu,
                train_batch_size=config.train_batch_size,
                eval_batch_size=config.eval_batch_size,
                predict_batch_size=config.eval_batch_size,
                config=run_config,
                params=eval_params_dict)

            output_dir = os.path.join(FLAGS.model_dir, 'eval')
            tf.gfile.MakeDirs(output_dir)
            # Summary writer writes out eval metrics.
            summary_writer = tf.summary.FileWriter(output_dir)
            eval_results = evaluation.evaluate(eval_estimator,
                                               config.validation_file_pattern,
                                               config.eval_samples,
                                               config.eval_batch_size,
                                               config.include_mask,
                                               config.val_json_file)
            evaluation.write_summary(eval_results, summary_writer,
                                     config.total_steps)

            summary_writer.close()

    elif FLAGS.mode == 'eval':
        output_dir = os.path.join(FLAGS.model_dir, 'eval')
        tf.gfile.MakeDirs(output_dir)
        # Summary writer writes out eval metrics.
        summary_writer = tf.summary.FileWriter(output_dir)

        eval_params_dict = dict(
            params,
            use_tpu=FLAGS.use_tpu,
            input_rand_hflip=False,
            is_training_bn=False,
            transpose_input=False,
        )

        eval_estimator = tf.contrib.tpu.TPUEstimator(
            model_fn=mask_rcnn_model.mask_rcnn_model_fn,
            use_tpu=FLAGS.use_tpu,
            train_batch_size=config.train_batch_size,
            eval_batch_size=config.eval_batch_size,
            predict_batch_size=config.eval_batch_size,
            config=run_config,
            params=eval_params_dict)

        def terminate_eval():
            tf.logging.info(
                'Terminating eval after %d seconds of no checkpoints' %
                FLAGS.eval_timeout)
            return True

        # Run evaluation when there's a new checkpoint
        for ckpt in tf.contrib.training.checkpoints_iterator(
                FLAGS.model_dir,
                min_interval_secs=FLAGS.min_eval_interval,
                timeout=FLAGS.eval_timeout,
                timeout_fn=terminate_eval):
            # Terminate eval job when final checkpoint is reached
            current_step = int(os.path.basename(ckpt).split('-')[1])

            tf.logging.info('Starting to evaluate.')
            try:
                eval_results = evaluation.evaluate(
                    eval_estimator, config.validation_file_pattern,
                    config.eval_samples, config.eval_batch_size,
                    config.include_mask, config.val_json_file)
                evaluation.write_summary(eval_results, summary_writer,
                                         config.total_steps)

                if current_step >= config.total_steps:
                    tf.logging.info(
                        'Evaluation finished after training step %d' %
                        current_step)
                    break

            except tf.errors.NotFoundError:
                # Since the coordinator is on a different job than the TPU worker,
                # sometimes the TPU worker does not finish initializing until long after
                # the CPU job tells it to start evaluating. In this case, the checkpoint
                # file could have been deleted already.
                tf.logging.info(
                    'Checkpoint %s no longer exists, skipping checkpoint' %
                    ckpt)
        summary_writer.close()

        # Export saved model.
        eval_estimator.export_saved_model(
            export_dir_base=FLAGS.model_dir,
            serving_input_receiver_fn=functools.partial(
                serving_inputs.serving_input_fn,
                batch_size=1,
                desired_image_size=config.image_size,
                padding_stride=(2**config.max_level),
                input_type='image_bytes'))

    elif FLAGS.mode == 'train_and_eval':
        if FLAGS.model_dir:
            save_config(config, FLAGS.model_dir)

        output_dir = os.path.join(FLAGS.model_dir, 'eval')
        tf.gfile.MakeDirs(output_dir)
        summary_writer = tf.summary.FileWriter(output_dir)
        train_estimator = tf.contrib.tpu.TPUEstimator(
            model_fn=mask_rcnn_model.mask_rcnn_model_fn,
            use_tpu=FLAGS.use_tpu,
            train_batch_size=config.train_batch_size,
            config=run_config,
            params=params)
        eval_params_dict = dict(
            params,
            use_tpu=FLAGS.use_tpu,
            input_rand_hflip=False,
            is_training_bn=False,
        )
        eval_estimator = tf.contrib.tpu.TPUEstimator(
            model_fn=mask_rcnn_model.mask_rcnn_model_fn,
            use_tpu=FLAGS.use_tpu,
            train_batch_size=config.train_batch_size,
            eval_batch_size=config.eval_batch_size,
            predict_batch_size=config.eval_batch_size,
            config=run_config,
            params=eval_params_dict)

        num_cycles = int(config.total_steps / config.num_steps_per_eval)
        for cycle in range(num_cycles):
            tf.logging.info('Start training cycle %d.' % cycle)
            train_estimator.train(input_fn=dataloader.InputReader(
                config.training_file_pattern,
                mode=tf.estimator.ModeKeys.TRAIN,
                use_instance_mask=config.include_mask),
                                  steps=config.num_steps_per_eval)

            tf.logging.info('Start evaluation cycle %d.' % cycle)
            eval_results = evaluation(eval_estimator, config)

            current_step = int(cycle * config.num_steps_per_eval)
            evaluation.write_summary(eval_results, summary_writer,
                                     current_step)

        tf.logging.info('Starting training cycle %d.' % num_cycles)
        train_estimator.train(input_fn=dataloader.InputReader(
            config.training_file_pattern,
            mode=tf.estimator.ModeKeys.TRAIN,
            use_instance_mask=config.include_mask),
                              max_steps=config.total_steps)

        eval_results = evaluation.evaluate(eval_estimator,
                                           config.validation_file_pattern,
                                           config.eval_samples,
                                           config.eval_batch_size,
                                           config.include_mask,
                                           config.val_json_file)
        evaluation.write_summary(eval_results, summary_writer,
                                 config.total_steps)
        summary_writer.close()

        # Export saved model.
        eval_estimator.export_saved_model(
            export_dir_base=FLAGS.model_dir,
            serving_input_receiver_fn=functools.partial(
                serving_inputs.serving_input_fn,
                batch_size=1,
                desired_image_size=config.image_size,
                padding_stride=(2**config.max_level),
                input_type='image_bytes'))

    else:
        tf.logging.info('Mode not found.')