예제 #1
0
def model_fn(features, labels, mode, params):
    training = (mode == tf.estimator.ModeKeys.TRAIN)
    input_ids = features["text"]
    author_id = features["author"]
    category_ids = features["categories"]
    label_id = features["label"]
    cnn = CnnModel(params, input_ids, author_id, category_ids, training)
    logits, predict_label_ids, l2_loss, author_loss = cnn.build_network()
    squeeze_label_ids = tf.squeeze(label_id, axis=1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        # Predictions
        #words = tf.contrib.lookup.index_to_string_table_from_file(params['vocab'])
        #input_words = words.lookup(tf.to_int64(input_ids))
        predictions = {
            'true_label_ids': squeeze_label_ids,
            'predict_label_ids': predict_label_ids,
        }
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)
    else:
        # Loss
        loss = tf.cast(cnn.build_loss(squeeze_label_ids, logits, l2_loss,
                                      author_loss),
                       dtype=tf.float32)
        train_op = optimization.create_optimizer(loss, params['learning_rate'],
                                                 params['train_steps'],
                                                 params['num_warmup_steps'])
        if mode == tf.estimator.ModeKeys.EVAL:
            # Metrics
            metrics = {
                'acc':
                tf.metrics.accuracy(squeeze_label_ids, predict_label_ids),
                # 分别计算各个类的P, R 然后按类求平均值
                'precision':
                precision(squeeze_label_ids,
                          predict_label_ids,
                          params['label_size'],
                          average='macro'),
                'recall':
                recall(squeeze_label_ids,
                       predict_label_ids,
                       params['label_size'],
                       average='macro'),
                'f1':
                f1(squeeze_label_ids,
                   predict_label_ids,
                   params['label_size'],
                   average='macro'),
            }
            return tf.estimator.EstimatorSpec(mode,
                                              loss=loss,
                                              eval_metric_ops=metrics)
        elif mode == tf.estimator.ModeKeys.TRAIN:
            return tf.estimator.EstimatorSpec(mode,
                                              loss=loss,
                                              train_op=train_op)
예제 #2
0
def main():
    # Instanciate the model
    cnn = CnnModel()
    # Load data
    tr_imgs, gt_imgs = load_images(train_data_filename, train_labels_filename,
                                   num_images)
    # Train the model
    history = cnn.train_model(gt_imgs, tr_imgs, nb_epochs)
    # Save the weights
    cnn.save_weights(PATH_WEIGHTS)

    # Generate plots
    if history is not None:
        plot_metric_history(f1_scores=f1_scores(history))
예제 #3
0
def train_and_evaluate():
    train_loader = get_train_loader()
    test_loader = get_t_loader()
    net = CnnModel().to(configurations.DEVICE)
    criterion = nn.BCELoss()
    optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    print_every = 5

    for (epoch, step) in _train(net, train_loader, criterion, optimizer, configurations.epochs):
        if step % print_every == 0:
            print("Epoch: {}/{}...".format(epoch + 1, configurations.epochs),
                  "Step: {}...".format(step))
            _evaluate(net, test_loader, criterion, epoch, step)

    return net
예제 #4
0
    def get_reward(states):
        cnn = CnnModel.from_states(states).cuda()
        cnn.train()

        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(cnn.parameters(), lr=0.001)

        log_interval = (Config.N_CNN_EPOCH // 20 + 1)
        totalLoss = .0

        Logger.stage('train', 'start')
        for epoch in range(Config.N_CNN_EPOCH):

            for data in Environment._train_loader:
                inputs, labels = data
                inputs, labels = inputs.to(Environment._device), labels.to(
                    Environment._device)

                optimizer.zero_grad()
                loss = criterion(cnn(inputs), labels)
                loss.backward()

                optimizer.step()
                totalLoss += loss.item()

            if (epoch + 1) % log_interval == 0:
                acc = Environment._test_cnn_model(cnn, log=False)
                Logger.print(
                    f'[Epoch {epoch}] loss: {totalLoss / len(Environment._train_loader)} acc: {acc}'
                )
                totalLoss = .0

        acc = Environment._test_cnn_model(cnn, log=True)
        Logger.stage('accuracy', f'finished {acc}')
        return acc
예제 #5
0
def main(argv):
    # We add all test images to an array, used later for generating a submission
    image_filenames_test = [
        PATH_TEST_DATA + 'test_' + str(i + 1) + '/' + 'test_' + str(i + 1) +
        '.png' for i in range(TEST_SIZE)
    ]
    image_filenames_predict = [
        PATH_PREDICTION_DIR + 'pred_' + str(i + 1) + '_unet.png'
        for i in range(TEST_SIZE)
    ]
    print('Loading Model...')
    if len(argv) != 2:
        print(len(argv))
        raise Exception('Please pass only one argument to the script')
    else:
        if argv[1] == '-unet':

            # Run the UNET model
            model_unet = keras.models.load_model(PATH_UNET)
            gen_image_predictions_unet(model_unet, PATH_PREDICTION_DIR,
                                       *image_filenames_test)

            # Generates the submission
            generate_submission(model_unet, PATH_SUBMISSION, True,
                                *image_filenames_predict)

        elif argv[1] == '-normal':
            # Run the normal CNN model
            model = CnnModel()
            model.load_weights(PATH_WEIGHTS)

            # Generates the submission
            generate_submission(model, PATH_SUBMISSION, False,
                                *image_filenames_test)

        else:
            raise Exception(
                'Please pass only "unet" or "normal" as argument to the script'
            )
예제 #6
0
def main(_):
    if FLAG.model == "cnn":
        params = CnnParams()
        from cnn_model import CnnModel
        model = CnnModel(params)
    elif FLAG.model == "rnn":
        params = RnnParams()
        from rnn_model import RnnModel
        model = RnnModel(params)
    elif FLAG.model == "rnn_attention":
        from rnn_attention_model import RnnAttentionModel
        params = RnnAttentionParams()
        model = RnnAttentionModel(params)
    else:
        raise Exception("model only can be in [cnn, rnn, rnn_attention]")
    model.train(params)
    model.test(params)
예제 #7
0
from helpers import *
from cnn_model import CnnModel
from helpers import *
from keras import backend as K
from sys import exit

if K.backend() != 'theano':
    print('Error: Please change to THEANO BACKEND and continue')
    exit()

K.set_image_dim_ordering('th')

model = CnnModel()
model.load('weights.h5')

model.model.summary()

submission_filename = 'submission.csv'
image_filenames = []
for i in range(1, 2):
    image_filename = 'testing/test_' + str(i) + '/test_' + str(i) + '.png'
    image_filenames.append(image_filename)

render_submission(model, submission_filename, *image_filenames)
예제 #8
0
# -*- coding: utf-8 -*-

from helpers import *
from cnn_model import CnnModel
from helpers import *
import keras.backend as K
from sys import exit

if K.backend() != 'theano':
    print('Error: for reproducibility, this code is intented to be run with Theano backend.')
    exit()

# Set image_img_ordering to 'th', since the model has been trained with Theano
K.set_image_dim_ordering('th')

model = CnnModel()
model.load('weights.h5')

model.model.summary()

submission_filename = 'submission.csv'
image_filenames = []
for i in range(1, 51):
    image_filename = 'test_set_images/test_'+str(i)+'/test_' + str(i) + '.png'
    image_filenames.append(image_filename)
    

generate_submission(model, submission_filename, *image_filenames)
예제 #9
0
files = os.listdirectory(image_directory)
n = len(files)
print("Loading " + str(n) + " images")
imgs = np.asarray([ld_img(image_directory + files[i]) for i in range(n)])

groundtruth_directory = root_directory + "groundtruth/"
print("Loading " + str(n) + " images")
groundtruth_imgs = np.asarray([ld_img(groundtruth_directory + files[i]) for i in range(n)])
#print(imgs,groundtruth_imgs)



#from naive_model import NaiveModel
from cnn_model import CnnModel
#from logistic_model import LogisticModel

#model = NaiveModel()
model = CnnModel()
#model = LogisticModel()


np.random.seed(1) # Ensure reproducibility

model.model.summary()
model.train(groundtruth_imgs, imgs)


# Save weights to disk
model.save('saved_weights.h5')

예제 #10
0
n = len(files)
print("Loading " + str(n) + " images")
imgs = np.asarray([ld_img(image_directory + files[i]) for i in range(n)])

groundtruth_directory = root_directory + "groundtruth/"
print("Loading " + str(n) + " images")
groundtruth_imgs = np.asarray([ld_img(groundtruth_directory + files[i]) for i in range(n)])



#from naive_model import NaiveModel
from cnn_model import CnnModel
#from logistic_model import LogisticModel

#model = NaiveModel()
model = CnnModel()
#model = LogisticModel()



np.random.seed(1) # Ensure reproducibility

# Fast (partial) cross validation
quick_cross_validation(model, groundtruth_imgs, imgs, 4, 1)



np.random.seed(1) # Ensure reproducibility

# Full k-fold cross validation
k_fold_cross_validation(model, groundtruth_imgs, imgs, 4, 1)