예제 #1
0
def run_BIGRU(timesteps, numclasses, train_x, train_y, train_lens, valid_x, valid_y, valid_lens, test_x, test_y, test_lens, modeldir, batchsize, maxepochs, patience, expsnum):
    K.set_session(utils.get_session())

    print('start class_weights...')

    class_weights =  class_weight.compute_class_weight('balanced', np.unique(train_y), train_y)

    print(class_weights)

    train_y = utils.one_hot(train_y, numclasses)
    valid_y = utils.one_hot(valid_y, numclasses)
    test_y = utils.one_hot(test_y, numclasses)

    preds = []
    tests = []
    epochs = []

    print('start...')

    for exp in range(1, 1 + expsnum):
        modelfile = modeldir + str(exp) +".hdf5"
        predict_y, test_y, stopped_epoch = train_model(train_x, train_y, valid_x, valid_y, test_x, test_y, timesteps, numclasses, class_weights, modelfile, batchsize, maxepochs, patience)
        preds.extend(predict_y)
        tests.extend(test_y)
        epochs.append(stopped_epoch)

    test_yo = np.argmax(tests, 1)
    pred_yo = np.argmax(preds, 1)

    target_names = ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5']
    logfile = modeldir + "bigru.log"
    utils.savemetrics(logfile, epochs, test_yo, pred_yo, target_names)
예제 #2
0
def test_GRU(timesteps, numclasses, test_x, test_y, test_lens, modeldir, batchsize, expsnum, **kwargs):
    K.set_session(gpuutils.get_session())

    test_y = encode_target(test_y, numclasses)

    if len(np.shape(test_x))<3:
        test_x = np.expand_dims(test_x, 2)


    preds = []
    tests = []
    paramnums = []
    durations = []

    for exp in range(1, 1 + expsnum):
        modelfile = modeldir + str(exp) + ".hdf5"
        predict_y, test_y, paramnum, duration = test_model(test_x, test_y, timesteps, numclasses, modelfile)
        preds.extend(predict_y)
        tests.extend(test_y)
        paramnums.append(paramnum)
        durations.append(duration*100.0)

    test_yo = np.argmax(tests, 1)
    pred_yo = np.argmax(preds, 1)

    target_names = ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5']
    logfile = modeldir + "gru.benchmark"
    savebenchmark(logfile, paramnums, durations, test_yo, pred_yo, target_names)
예제 #3
0
def run_LSTM(timesteps, numclasses, train_x, train_y, train_lens, valid_x,
             valid_y, valid_lens, test_x, test_y, test_lens, modeldir,
             batchsize, maxepochs, patience, expsnum, **kwargs):
    K.set_session(gpuutils.get_session())

    if len(np.shape(train_x)) < 3:
        train_x = np.expand_dims(train_x, 2)

    if len(np.shape(valid_x)) < 3:
        valid_x = np.expand_dims(valid_x, 2)

    if len(np.shape(test_x)) < 3:
        test_x = np.expand_dims(test_x, 2)

    valid_y = encode_target(valid_y, numclasses)
    test_y = encode_target(test_y, numclasses)

    preds = []
    tests = []
    epochs = []

    print('start...')

    unique_y = list(range(numclasses))

    if kwargs.get('balancedgenerator') == True:
        train_dataprovider = gpuutils.SourceGenerator(
            gpuutils.BalancedGenerator(train_x, train_y, unique_y,
                                       int(batchsize / numclasses), True))
    else:
        train_dataprovider = gpuutils.SourceGenerator(
            gpuutils.RandomGenerator(train_x, train_y, unique_y, batchsize,
                                     True))

    timesteps = np.shape(train_x)[1]
    inputdim = np.shape(train_x)[2]
    train_steps_per_epoch = int(np.shape(train_x)[0] / batchsize)

    for exp in range(1, 1 + expsnum):
        modelfile = modeldir + str(exp) + ".hdf5"
        predict_y, test_y, stopped_epoch = train_model(
            timesteps, inputdim, train_dataprovider, train_steps_per_epoch,
            valid_x, valid_y, test_x, test_y, timesteps, numclasses, modelfile,
            batchsize, maxepochs, patience)
        preds.extend(predict_y)
        tests.extend(test_y)
        epochs.append(stopped_epoch)

    test_yo = np.argmax(tests, 1)
    pred_yo = np.argmax(preds, 1)

    target_names = ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5']
    logfile = modeldir + "lstm.log"
    savemetrics(logfile, epochs, test_yo, pred_yo, target_names)
예제 #4
0
	def onJsonInput(self, jsonInput):
		#build the result object
		result = {'prediction':-1}

		#prepare the input
		x_raw = [jsonInput['pixels']]
		x_raw = np.reshape(x_raw, (1, 28, 28))

		ue.log('image shape: ' + str(x_raw.shape))
		#ue.log(stored)

		#convert pixels to N_samples, height, width, N_channels input tensor
		x = np.reshape(x_raw, (len(x_raw), 28, 28, 1))

		ue.log('input shape: ' + str(x.shape))

		#run run the input through our network
		if self.model is None:
			ue.log("Warning! No 'model' found. Did training complete?")
			return result

		#restore our saved session and model
		K.set_session(self.session)

		with self.session.as_default():
			output = self.model.predict(x)

			ue.log(output)

			#convert output array to prediction
			index, value = max(enumerate(output[0]), key=operator.itemgetter(1))

			result['prediction'] = index
			result['pixels'] = jsonInput['pixels'] #unnecessary but useful for round tripping

		return result
예제 #5
0
def test_BIGRU(timesteps, numclasses, test_x, test_y, test_lens, modeldir,  expsnum):
    K.set_session(utils.get_session())

    test_y = utils.one_hot(test_y, numclasses)

    preds = []
    tests = []
    paramnums = []
    durations = []

    for exp in range(1, 1 + expsnum):
        modelfile = modeldir + str(exp) + ".hdf5"
        predict_y, test_y, paramnum, duration = test_model(test_x, test_y, timesteps, numclasses, modelfile)
        preds.extend(predict_y)
        tests.extend(test_y)
        paramnums.append(paramnum)
        durations.append(duration)

    test_yo = np.argmax(tests, 1)
    pred_yo = np.argmax(preds, 1)

    target_names = ['Class 1', 'Class 2', 'Class 3', 'Class 4', 'Class 5']
    logfile = modeldir + "bigru.benchmark"
    utils.savebenchmark(logfile, paramnums, durations, test_yo, pred_yo, target_names)
예제 #6
0
import tensorflow as tf
from tensorflow.contrib.keras import backend as K
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.visible_device_list = "0"
K.set_session(tf.Session(config=config))

from AlphagoZero.training.evaluator import run_evaluate

run_evaluate()
예제 #7
0
파일: main.py 프로젝트: hbcbh1999/pregel
placeholder_dict = datapipeline.get_placeholder_dict()

feed_dict_train = datapipeline.get_feed_dict(mode=TRAIN)

feed_dict_val = datapipeline.get_feed_dict(mode=VALIDATION)

feed_dict_test = datapipeline.get_feed_dict(mode=TEST)

sparse_model_params = SparseModelParams(
    num_elements=datapipeline.num_elements,
    feature_size=datapipeline.feature_size
)

model = select_model(model_name=model_params.model_name)(
    model_params = model_params,
    sparse_model_params = sparse_model_params,
    placeholder_dict = placeholder_dict
)

sess = tf.Session()
K.set_session(sess)
sess.run(tf.global_variables_initializer())

for epoch in range(model_params.epochs):
    start_time = time()
    loss, accuracy, opt = sess.run([model.loss, model.accuracy, model.optimizer_op], feed_dict=feed_dict_train)
    loss_val, accuracy_val = sess.run([model.loss, model.accuracy], feed_dict=feed_dict_test)
    print(accuracy_val)
    # print(accuracy)
예제 #8
0
파일: convnet.py 프로젝트: wn9081/im2latex
def run_convnet(params):
    HYPER = make_hyper(params)
    image_folder = params.image_folder
    raw_data_folder = params.raw_data_folder
    image_features_folder = params.vgg16_folder
    logger = HYPER.logger

    df = get_df(params)
    if df.shape[0] == 0:
        logger.info('No images remaining to process. All done.')
    else:
        logger.info('Processing %d images.', df.shape[0])

    logger.info('\n#################### Args: ####################\n%s',
                params.pformat())
    logger.info(
        '##################################################################\n')
    logger.info(
        '\n#########################  HYPER Params: #########################\n%s',
        HYPER.pformat())
    logger.info(
        '##################################################################\n')

    b_it = BatchImageIterator2(raw_data_folder,
                               image_folder,
                               HYPER,
                               image_processor=ImagenetProcessor(HYPER),
                               df=df,
                               num_steps=params.num_steps,
                               num_epochs=params.num_epochs)

    graph = tf.Graph()
    with graph.as_default():

        config = tf.ConfigProto(log_device_placement=False)
        config.gpu_options.allow_growth = True
        tf_session = tf.Session(config=config)
        with tf_session.as_default():
            K.set_session(tf_session)

            tf_im = tf.placeholder(dtype=HYPER.dtype,
                                   shape=((HYPER.B, ) + HYPER.image_shape),
                                   name='image')
            with tf.device(
                    '/gpu:1'):  # change this to gpu:0 if you only have one gpu
                tf_a_batch = build_vgg_context(HYPER, tf_im)
                tf_a_list = tf.unstack(tf_a_batch, axis=0)

            t_n = tfc.printVars('Trainable Variables',
                                tf.trainable_variables())
            g_n = tfc.printVars('Global Variables', tf.global_variables())
            l_n = tfc.printVars('Local Variables', tf.local_variables())
            assert t_n == g_n
            assert g_n == l_n

            print '\nUninitialized params'
            print tf_session.run(tf.report_uninitialized_variables())

            print 'Flushing graph to disk'
            tf_sw = tf.summary.FileWriter(tfc.makeTBDir(HYPER.tb), graph=graph)
            tf_sw.flush()

            print '\n'
            start_time = time.clock()
            for step, b in enumerate(b_it, start=1):
                # if b.epoch > 1 or (params.num_steps >= 0 and step > params.num_steps):
                #     break
                feed_dict = {tf_im: b.im, K.learning_phase(): 0}
                a_list = tf_session.run(tf_a_list, feed_dict=feed_dict)
                assert len(a_list) == len(b.image_name)
                for i, a in enumerate(a_list):
                    ## print 'Writing %s, shape=%s'%(b.image_name[i], a.shape)
                    with open(
                            os.path.join(
                                image_features_folder,
                                os.path.splitext(b.image_name[i])[0] + '.pkl'),
                            'wb') as f:
                        pickle.dump(a, f, pickle.HIGHEST_PROTOCOL)
                if step % 10 == 0:
                    print('Elapsed time for %d steps = %d seconds' %
                          (step, time.clock() - start_time))
            print('Elapsed time for %d steps = %d seconds' %
                  (step, time.clock() - start_time))
            print 'done'
예제 #9
0
# Define the SSD model.
reuse = True if 'ssd_net' in locals() else None
ssd_net = ssd_vgg_300.SSDNet()
with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
    #predictions, localisations, logits, end_points = ssd_net.net(image_4d, is_training=False, reuse=reuse)
    predictions, localisations, logits, end_points = ssd_net.net(
        vis_img_input, is_training=False, reuse=reuse)

# Restore SSD model.
ckpt_filename = 'checkpoints/ssd_300_vgg.ckpt'
# ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'
isess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(isess, ckpt_filename)
K.set_session(isess)
K.manual_variable_initialization(True)

# SSD default anchor boxes.
ssd_anchors = ssd_net.anchors(net_shape)

# ## Post-processing pipeline
#
# The SSD outputs need to be post-processed to provide proper detections. Namely, we follow these common steps:
#
# * Select boxes above a classification threshold;
# * Clip boxes to the image shape;
# * Apply the Non-Maximum-Selection algorithm: fuse together boxes whose Jaccard score > threshold;
# * If necessary, resize bounding boxes to original image shape.

# In[7]:
예제 #10
0
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.keras import backend as K
from tensorflow.contrib.keras import layers


K.set_session(tf.get_default_session())

img = tf.placeholder(tf.float32, shape=(None, 784))
x = layers.Dense(128, activation='relu')(img)
print x.trainable_weights
x = layers.Dropout(0.5)(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dropout(0.5)(x)
preds = layers.Dense(10, activation='softmax')(x)



labels = tf.placeholder(tf.int32)

loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=preds, labels=labels))

mnist_data = input_data.read_data_sets("MNIST_data", one_hot=False)
optimize = tf.train.RMSPropOptimizer(0.001).minimize(loss)

acc = tf.reduce_mean(tf.cast(tf.not_equal(tf.cast(tf.argmax(preds, 1), tf.int32), labels), tf.float32))

init_op = tf.global_variables_initializer()

with tf.Session().as_default() as sess:
예제 #11
0
def run(model_params, data_dir, dataset_name, experiment=None):
    datapipeline = DataPipelineAE(model_params=model_params,
                                  data_dir=data_dir,
                                  dataset_name=dataset_name)

    feed_dict_train = datapipeline.get_feed_dict(mode=TRAIN)

    feed_dict_val = datapipeline.get_feed_dict(mode=VALIDATION)

    feed_dict_test = datapipeline.get_feed_dict(mode=TEST)

    sparse_model_params = datapipeline.get_sparse_model_params()
    autoencoder_model_params = datapipeline.get_autoencoder_model_params()
    placeholder_dict = datapipeline.get_placeholder_dict()

    if (experiment):
        experiment.add_config(sparse_model_params.get_variables())
        experiment.add_config(autoencoder_model_params.get_variables())

    sess = tf.Session()
    K.set_session(sess)

    train_loss_runs = []
    validation_loss_runs = []
    test_aucscore_runs = []
    test_apr_runs = []

    for num_exp in range(model_params.num_exp):

        model = select_model(model_name=model_params.model_name)(
            model_params=model_params,
            sparse_model_params=sparse_model_params,
            placeholder_dict=placeholder_dict,
            autoencoder_model_params=autoencoder_model_params)

        if (model_params.tensorboard_logs_dir):
            train_writer = tf.summary.FileWriter(
                model_params.tensorboard_logs_dir + model_params.model_name +
                "/" + TRAIN, sess.graph)
            val_writer = tf.summary.FileWriter(
                model_params.tensorboard_logs_dir + model_params.model_name +
                "/" + VALIDATION, sess.graph)

        sess.run([
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        ])

        train_loss_list = []
        validation_loss_list = []
        test_aucscore_list = []
        test_apr_list = []
        for epoch in range(model_params.epochs):
            loss, accuracy, opt, summary = sess.run([
                model.loss, model.accuracy, model.optimizer_op,
                model.summary_op
            ],
                                                    feed_dict=feed_dict_train)

            loss_val, accuracy_val, summary_val = sess.run(
                [model.loss, model.accuracy, model.summary_op],
                feed_dict=feed_dict_val)

            if (model_params.tensorboard_logs_dir):
                train_writer.add_summary(summary, epoch)
                val_writer.add_summary(summary_val, epoch)

            embedding, predictions_test, labels_test, mask_test, loss_test, accuracy_test, summary_test = sess.run(
                [
                    model.embeddings, model.logits, model.labels, model.mask,
                    model.loss, model.accuracy, model.summary_op
                ],
                feed_dict=feed_dict_test)

            auc_score = compute_auc_score(labels=labels_test,
                                          predictions=predictions_test,
                                          mask=mask_test)
            test_aucscore_list.append(auc_score)

            apr = compute_average_precision_recall(
                labels=labels_test,
                predictions=predictions_test,
                mask=mask_test)
            test_apr_list.append(apr)

            train_loss_list.append(loss)
            validation_loss_list.append(loss_val)

            print(
                "For epoch:run {}:{}, training_loss = {}, validation_loss = {}, test_auc = {}, test_apr = {}"
                .format(epoch, num_exp, loss, loss_val, auc_score, apr))

        train_loss_runs.append(train_loss_list)
        validation_loss_runs.append(validation_loss_list)
        test_aucscore_runs.append(test_aucscore_list)
        test_apr_runs.append(test_apr_list)

    plot_loss_curves(train_loss_runs,
                     validation_loss_runs,
                     dataset_name=dataset_name,
                     model_params=model_params)
    print_stats(train_loss_runs,
                validation_loss_runs,
                test_metrics=[test_aucscore_runs, test_apr_runs],
                test_metrics_labels=[AUCSCORE, AVERAGE_PRECISION_RECALL_SCORE])
예제 #12
0
    K.clear_session()
    return cg


def h5_to_pb(h5, pb):
    write_graph(constantize(h5), pb)


def copy_perms(source, target):
    st = os.stat(source)
    os.chown(target, st[stat.ST_UID], st[stat.ST_GID])


if __name__ == "__main__":
    # disable gpu for conversion
    config = tf.ConfigProto(allow_soft_placement=True,
                            device_count={
                                'CPU': 1,
                                'GPU': 0
                            })
    session = tf.Session(config=config)
    K.set_session(session)

    import sys
    if len(sys.argv) < 3:
        print('usage: {} <src_fname> <dst_fname>'.format(sys.argv[0]))
        sys.exit(1)
    h5_to_pb(sys.argv[1], sys.argv[2])
    copy_perms(sys.argv[1], sys.argv[2])
    print('saved the constant graph (ready for inference) at: ', sys.argv[2])
예제 #13
0
def run(model_params, data_dir, dataset_name, experiment=None):
    datapipeline = DataPipeline(model_params=model_params,
                                data_dir=data_dir,
                                dataset_name=dataset_name)

    placeholder_dict = datapipeline.get_placeholder_dict()

    feed_dict_train = datapipeline.get_feed_dict(mode=TRAIN)

    feed_dict_val = datapipeline.get_feed_dict(mode=VALIDATION)

    feed_dict_test = datapipeline.get_feed_dict(mode=TEST)

    sparse_model_params = SparseModelParams(
        num_elements=datapipeline.num_elements,
        feature_size=datapipeline.feature_size)

    if (experiment):
        experiment.add_config(sparse_model_params.get_variables())

    sess = tf.Session()
    K.set_session(sess)

    train_loss_runs = []
    validation_loss_runs = []
    test_accuracy_runs = []

    model = None

    for _ in range(model_params.num_exp):

        model = select_model(model_name=model_params.model_name)(
            model_params=model_params,
            sparse_model_params=sparse_model_params,
            placeholder_dict=placeholder_dict)

        if (model_params.tensorboard_logs_dir):
            train_writer = tf.summary.FileWriter(
                model_params.tensorboard_logs_dir + model_params.model_name +
                "/" + TRAIN, sess.graph)
            val_writer = tf.summary.FileWriter(
                model_params.tensorboard_logs_dir + model_params.model_name +
                "/" + VALIDATION, sess.graph)
        sess.run(tf.global_variables_initializer())

        train_loss_list = []
        validation_loss_list = []
        test_accuracy_list = []

        for epoch in range(model_params.epochs):
            loss, accuracy, opt, summary = sess.run([
                model.loss, model.accuracy, model.optimizer_op,
                model.summary_op
            ],
                                                    feed_dict=feed_dict_train)

            loss_val, accuracy_val, summary_val = sess.run(
                [model.loss, model.accuracy, model.summary_op],
                feed_dict=feed_dict_val)

            if (model_params.tensorboard_logs_dir):
                train_writer.add_summary(summary, epoch)
                val_writer.add_summary(summary_val, epoch)

            train_loss_list.append(loss)
            validation_loss_list.append(loss_val)

            accuracy_test = sess.run([model.accuracy],
                                     feed_dict=feed_dict_test)
            test_accuracy_list.append(accuracy_test)

        train_loss_runs.append(train_loss_list)
        validation_loss_runs.append(validation_loss_list)
        test_accuracy_runs.append(test_accuracy_list)

    plot_loss_curves(train_loss_runs,
                     validation_loss_runs,
                     dataset_name=dataset_name,
                     model_params=model_params)
    print_stats(train_loss_runs,
                validation_loss_runs,
                test_metrics=[test_accuracy_runs],
                test_metrics_labels=[ACCURACY])

    activations, khot_labels, mask = sess.run(
        [model.activations, model.labels, model.mask],
        feed_dict=feed_dict_train)
    embedd_and_plot(node_representation=activations[-2],
                    labels=khot_labels,
                    mask=mask)