Exemplo n.º 1
0
def train(args):
    train_data_file = args.train
    valid_data_file = args.valid
    word_count_file = args.word_count
    output = args.output
    word_dictionary = WordDictionary(word_count_file=word_count_file)
    valid_data = data.sentiment140.TweetData(valid_data_file, word_dictionary=word_dictionary)
    train_data = data.sentiment140.TweetData(train_data_file, word_dictionary=word_dictionary)
    trainer = model.Trainer(train_data=train_data, valid_data=valid_data, word_dictionary=word_dictionary,
                            output=output)
    trainer.train()
Exemplo n.º 2
0
def main():
    tfv1.enable_eager_execution()
    logger.info(f"execute eagerly = {tf.executing_eagerly()}")
    logger.info(f"is gpu available = {tf.test.is_gpu_available()}")

    logger.info("get dataset...")
    train, _ = datasets.get_dataset()

    logger.info("learning...")
    network = model.MNISTModel()
    trainer = model.Trainer()
    checkpoint = model.Checkpoint(network=network, optimizer=trainer.optimizer)

    start_learning = time.time()
    start(network, trainer, train, 5, checkpoint)
    end_learning = time.time()
    logger.info(f"learning time: {end_learning - start_learning} sec")
Exemplo n.º 3
0
all_findings, all_impressions, all_img_names, rids = shuffle(all_findings,
                                                             all_impressions,
                                                             all_img_names,
                                                             rids,
                                                             random_state=1)

# Initialize InceptionV3 and load the pretrained Imagenet weights
inception_model = prepare_dataset.init_inception_model()

# Preprocess and tokenize Findings and Impressions
tokenizer, findings_vector = prepare_dataset.transform_input(all_findings, all_impressions, MAX_PARAGRAPH_LENGTH, MAX_SENTENCE_LENGTH)

# Create training and validation sets using 80-20 split
img_name_train, img_name_test, findings_train, findings_test = train_test_split(all_img_names, findings_vector, test_size = 0.2, random_state = 0)

trainer = model.Trainer(tokenizer, embedding_dim=256, units=512)

FEATURES_SHAPE = 2048
ATTENTION_FEATURES_SHAPE = 64

def load_image(image_path):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_png(img, channels=3)
    img = tf.image.resize_images(img, (299, 299))
    img = tf.keras.applications.inception_v3.preprocess_input(img)
    return img, image_path

def map_func(img_name, findings):
    img, img_path = load_image(img_name)
    img = tf.expand_dims(img, 0)
    img_tensor = inception_model(img)
Exemplo n.º 4
0
bias, conf, cat = tf.split(feature, [4, 1, 2], 3)
bias1, bias2 = tf.split(bias, [2, 2], 3)
b1_labholder, b2_labholder = tf.split(b_labholder, [2, 2], 3)

with tf.variable_scope('loss_function'):
    with tf.variable_scope('bias_loss_x_y'):
        bias1_loss = tf.reduce_sum(
            tf.reduce_mean(tf.square(bias1 - b1_labholder) * c_labholder,
                           axis=0))  #x,y
    with tf.variable_scope('bias_loss_w_h'):
        bias2_loss = tf.reduce_sum(
            tf.reduce_mean(tf.square(
                tf.sqrt(tf.abs(bias2)) - tf.sqrt(tf.abs(b2_labholder))) *
                           c_labholder,
                           axis=0))  #w,h
    conf_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(labels=c_labholder,
                                                logits=conf,
                                                name="Propose_bounding_box"))
    cat_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(labels=cat_labholder,
                                                logits=cat,
                                                name="Categorize_class"))

with tf.variable_scope('Total_losses'):
    total_loss = 3 * (bias1_loss + bias2_loss) + conf_loss + cat_loss
gradient_accum = M.Trainer(0.0002, total_loss)
zero_ops = gradient_accum.zero()
accum_ops = gradient_accum.accumulate()
step = gradient_accum.train()
Exemplo n.º 5
0
    parser.add_argument('--class_num',
                        type=int,
                        default=2,
                        help='class number')
    # parser.add_argument('--maxepoch', type=int, default=100,
    #                     help='Max number of epochs for training')

    # parser.add_argument('--im_name', type=str, default='.png',
    #                     help='Part of image name')

    return parser.parse_args()


if __name__ == "__main__":
    FLAGS = get_args()
    if FLAGS.train:
        cnn_model = model.Model(FLAGS.bsize,
                                FLAGS.lr,
                                FLAGS.keep_prob,
                                FLAGS.class_num,
                                is_training=True)
        trainer = model.Trainer(FLAGS.data, cnn_model, FLAGS.epoch,
                                FLAGS.class_num)
        writer = tf.summary.FileWriter("./log/")

        with tf.Session() as sess:
            writer.add_graph(sess.graph)
            sess.run(tf.global_variables_initializer())
            trainer.train(sess, writer)

        writer.close()
Exemplo n.º 6
0
#                  centers=10, shuffle=True, random_state=None)
T = 20
dim = 2
alpha = 1
gamma = 0.001
a = 1
b = 100

base = md.FullFactorSpheGaussianMixture(T, dim, gamma, a, b)
#base = md.StandardGaussianMixture(T, dim, gamma, 0.001)
weight = md.StickBreakingWeight(T, alpha)
#weight = md.NonBayesianWeight(T)
model = md.DPMixture(T, dim, base, weight)
    
sheduler = md.DecaySheduler(1000, 0.7, 0.001)
updater = md.Trainer(model, sheduler)
loglik = updater.fit(X, 20, 100)
plt.plot(loglik)
plt.figure()
#print np.exp(weight.logWeight()), base.expc_lambda, base.expc_mu
#for w, lmbd, mu in zip(np.exp(weight.logWeight()), base.expc_lambda, base.expc_mu):
#    print w, lmbd, mu
y = model.predict(X)
y_unique = np.unique(y)
colors = cm.rainbow(np.linspace(0.0, 1.0, y_unique.size))
for this_y, color in zip(y_unique, colors):
    this_X = X[y == this_y]
    w = np.exp(weight.logWeight())[this_y]
    plt.scatter(this_X[:, 0], this_X[:, 1], c=color, alpha=0.5,
                    label="Class %s:%f" % (this_y, w))
    if isinstance(base, md.FullFactorSpheGaussianMixture):