Пример #1
0
#traindata, tradj = load_SIFT1M()
trgcn = GCN(sample, output_n, adj=tradj)
trgcn.build(lr=0.5, PCA=True)

for i in range(3):
    for j  in range(100):
        loss = trgcn.train_fn(sample)
     
        print 'epoch {0}/300 loss: {1}'.format(i*300+j+1, loss) 

    nz = trgcn.infer_fn(sample) 
    trgcn.loss_fn.update(nz)   

B = trgcn.infer_fn(sample)
trgcn.save()
#trgcn.load('/mnt/disk1/ImageNett_model_%d' % output_n)
#np.save('%d' % output_n, np.sign(nz))
#after = trgcn.infer_fn(sample)
#tB = np.dot(gfunc1, B)
#sio.savemat('emb', {'B':B})
#B = sio.loadmat('emb')['B']
#trgcn.load('/mnt/disk1/ImageNett_model_%d' % output_n)
B = np.sign(B)
#sio.savemat('/mnt/disk1/ImageNet1M/ImageNettiny/%d.mat' % output_n, {'Z': Z})
#B = np.sign(np.dot(gfunc1, Z))
tB = np.sign(np.dot(gfunc, B))
#ttgcn.build_infer_fn()

#B = np.sign(trgcn.infer_fn(traindata))
#tB = np.sign(ttgcn.infer_fn(testdata))
Пример #2
0
def train_step(model_file):
    # Set random seed
    seed = 123
    np.random.seed(seed)
    tf.set_random_seed(seed)
    dirname = FLAGS.dataset + model_file + '/'
    print(dirname)
    # Load data
    namelist_path = FLAGS.dataset + 'sub_list_new.txt'
    adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(
        FLAGS.hemisphere, namelist=namelist_path, MPM=True)
    # neighbor_mask = 50*np.load( FLAGS.modeldir+'neighbor_mask_{}.npy'.format(FLAGS.hemisphere))
    namelist = [
        str(name).replace("\n", "")
        for name in open(namelist_path, 'r').readlines()
    ]
    # mpm = np.max(np.load( FLAGS.modeldir+'MPM_{}.npy'.format(FLAGS.hemisphere))[:,:210], axis=1)
    # train_mask = np.array([False]*len(mpm))
    # train_mask[mpm>0.5] = True
    # Some preprocessing
    adj = sparse_to_tuple(adj)

    for i in range(len(features)):
        features[i] = preprocess_features(features[i])

    support = chebyshev_polynomials(adj, FLAGS.max_degree)
    num_supports = 1 + FLAGS.max_degree

    # Define placeholders/
    placeholders = {
        'support':
        [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
        'features':
        tf.sparse_placeholder(tf.float32,
                              shape=tf.constant(features[0].shape,
                                                dtype=tf.float64)),
        'labels':
        tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
        'labels_mask':
        tf.placeholder(tf.int32),
        'dropout':
        tf.placeholder_with_default(FLAGS.dropout, shape=()),
        'num_features_nonzero':
        tf.placeholder(tf.int32),  # helper variable for sparse dropout
        'gradient_adj': [tf.sparse_placeholder(tf.float32)],
        'gradient':
        tf.placeholder(tf.bool)
    }

    # Create model
    model_name = 'model_{}'.format(FLAGS.hemisphere)
    print('model name:', model_name)
    model = GCN(placeholders,
                input_dim=features[0].shape[1],
                layer_num=FLAGS.layer_num,
                logging=True,
                name=model_name)

    # Initialize session
    sess = tf.Session()

    # Init variables
    sess.run(tf.global_variables_initializer())

    # Define model evaluation function
    def evaluate(features, support, labels, mask, adj, gradient, placeholders):
        t_test = time.time()
        feed_dict_val = construct_feed_dict(features, support, labels, mask,
                                            adj, gradient, placeholders)
        outs_val = sess.run([model.loss, model.dice, model.outputs],
                            feed_dict=feed_dict_val)
        return outs_val[0], outs_val[1], outs_val[2], (time.time() - t_test)

    def dynamic_learning_rate(epoch):
        # learning_rate = FLAGS.basic_learning_rate * 10**(-3 * epoch/FLAGS.epochs)
        learning_rate = FLAGS.basic_learning_rate * (1 - epoch / FLAGS.epochs)
        return learning_rate

    # def dynamic_training_mask(mpm, epoch):
    #     if (epoch/FLAGS.epochs)>0.5:
    #         train_mask = [True]*len(mpm)
    #     else:
    #         thr = 65-130*epoch/FLAGS.epochs
    #         train_mask = np.array([False]*len(mpm))
    #         train_mask[mpm>thr] = True
    #     return train_mask

    cost_train, acc_train, dc_train = [], [], []
    cost_val, acc_val, dc_val = [], [], []
    # Train model
    print('length', len(features))
    for epoch in range(FLAGS.epochs):
        numlist = np.arange(0, FLAGS.train_num, 1)
        np.random.shuffle(numlist)
        FLAGS.learning_rate = dynamic_learning_rate(epoch)
        # train_mask = dynamic_training_mask(mpm, epoch)
        print('_____leaning rate', FLAGS.learning_rate, 'epoch:', epoch)
        for i in numlist:
            t = time.time()
            print('training sample: ', i)
            # Construct feed dictionary
            feed_dict = construct_feed_dict(features[i], support, y_train,
                                            train_mask, adj, False,
                                            placeholders)

            # Training step
            outs = sess.run([model.opt_op, model.loss, model.dice],
                            feed_dict=feed_dict)
            cost_train.append(outs[1])
            dice_train.append(outs[2])

            # Validation
            validnum = np.random.randint(FLAGS.validate_num) + FLAGS.train_num
            cost_val, dice_val, _, tt = evaluate(features[validnum], support,
                                                 y_val, val_mask, adj, False,
                                                 placeholders)
            cost_val.append(cost_val)
            dc_val.append(dice_val)
            # Print results
            print("Epoch:", '%04d' % (epoch + 1), "train_loss=",
                  "{:.5f}".format(outs[1]), "train_dice=",
                  "{:.3f}".format(outs[2]), "val_loss=",
                  "{:.5f}".format(cost_val), "val_dice=",
                  "{:.3f}".format(dice_val), "time=", "{:.5f}".format(tt))

            # if epoch > FLAGS.early_stopping and cost_val[-1] > np.mean(cost_val[-(FLAGS.early_stopping+1):-1]):
            #     print("Early stopping...")
            #     break
        model.save(sess=sess, path=dirname)

    np.savetxt(dirname + 'cost_val.txt', cost_val, fmt='%9.5f', delimiter=',')
    np.savetxt(dirname + 'cost_train.txt',
               cost_train,
               fmt='%9.5f',
               delimiter=',')
    np.savetxt(dirname + 'dc_val.txt', dc_val, fmt='%9.5f', delimiter=',')
    np.savetxt(dirname + 'dc_train.txt', dc_train, fmt='%9.5f', delimiter=',')
    print("Optimization Finished!")

    # model.load(sess)
    # testave = []
    # # Testing
    # for i in range(len(features)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features[i], support, y_test, test_mask, neighbor_mask, placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.3f}_{:.3f}".format(test_acc, test_dice), "time=", "{:.5f}".format(test_duration))
    #     if i>=FLAGS.train_num:
    #         testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # np.savetxt(dirname+'dc_test.txt', testave, fmt='%7.4f', delimiter=',')
    # print('average_test_dice:', np.array(testave).mean())

    # Testing and resting
    # #load data
    # f = open('/DATA/232/lma/data/HCP_test/sub_list.txt','r')
    # namelist = [str(int(name)) for name in f.readlines()]
    # feature_path_list = []
    # for name in namelist:
    #     feature_path_list.append('/DATA/232/lma/data/HCP_test/{}/{}_{}_probtrackx_omatrix2/finger_print_fiber.npz'.format(name, name, FLAGS.hemisphere))

    # adj, features2, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.hemisphere,pathlist= feature_path_list)
    # # Some preprocessing
    # for i in range(len(features2)):
    #     features2[i] = preprocess_features(features2[i])
    # # prediction
    # testave = []
    # for i in range(len(features2)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features2[i], support, y_test, test_mask, neighbor_mask,placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.5f}".format(test_dice), "time=", "{:.5f}".format(test_duration))
    #     testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'test_{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # print('average_test/retest_acc:', np.array(testave).mean())

    # #Resesting
    # f = open('/DATA/232/lma/data/HCP_retest/sub_list.txt','r')
    # namelist = [str(int(name)) for name in f.readlines()]
    # feature_path_list = []
    # for name in namelist:
    #     feature_path_list.append('/DATA/232/lma/data/HCP_retest/{}/{}_{}_probtrackx_omatrix2/finger_print_fiber.npz'.format(name, name, FLAGS.hemisphere))

    # adj, features3, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.hemisphere, pathlist= feature_path_list)
    # # Some preprocessing
    # for i in range(len(features3)):
    #     features3[i] = preprocess_features(features3[i])
    # #prediction
    # testave = []
    # for i in range(len(features3)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features3[i], support, y_test, test_mask, neighbor_mask, placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.5f}".format(test_dice), "time=", "{:.5f}".format(test_duration))
    #     testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'retest_{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # print('average_test/retest_acc:', np.array(testave).mean())
    return None
Пример #3
0
train_loss = open('tmp/train_loss_record.txt', 'a')
train_loss.write('Start training, lr =  %f\n' % (FLAGS.learning_rate))
pkl = pickle.load(open('data/utils/eccv_final_version.dat', 'rb'))

train_number = data.number
for epoch in range(FLAGS.epochs):
    all_loss = np.zeros(train_number, dtype='float32')
    for iters in range(train_number):
        # Fetch training data
        img_inp, y_train, data_id = data.fetch()
        feed_dict = construct_feed_dict(img_inp, pkl, y_train, placeholders)

        # Training step
        _, dists = sess.run([model.opt_op, model.loss], feed_dict=feed_dict)
        all_loss[iters] = dists
        mean_loss = np.mean(all_loss[np.where(all_loss)])
        print 'Epoch %d, Iteration %d' % (epoch + 1, iters + 1)
        print 'Mean loss = %f, iter loss = %f, %d' % (mean_loss, dists,
                                                      data.queue.qsize())
    # Save model
    model.save(sess)
    train_loss.write('Epoch %d, loss %f\n' % (epoch + 1, mean_loss))
    train_loss.flush()
    # evaluate one model
    out1, out2, out3 = sess.run([model.output1, model.output2, model.output3],
                                feed_dict=feed_dict)
    evaluate(y_train, out1, out2, out3)

data.shutdown()
print 'CNN-GCN Optimization Finished!'