def main(path):

    train_images, train_labels = utils.load_training_data(path)
    test_images, test_labels = utils.load_test_data(path)

    n_classes = 10
    model = KMeans(n_clusters=n_classes, n_init=1, max_iter=1)
    model.fit(train_images)

    # which images are assigned to each cluster:
    # 1. check all data points assigned to each cluster
    # 2. check actual labels of the data points assigned to each cluster
    # 3. assign the mode of actual labels to be the label for that cluster

    cluster_label_dict = {}
    for cluster_num in range(n_classes):
        idx = utils.cluster_indices(cluster_num, model.labels_)
        original_labels = np.take(train_labels, idx)
        mode = stats.mode(original_labels)[0][0]
        cluster_label_dict.update({cluster_num: mode})

    # prediction
    predicted_cluster = model.predict(test_images)
    predicted_labels = np.vectorize(cluster_label_dict.get)(predicted_cluster)

    accuracy = utils.classification_accuracy(predicted_labels, test_labels)
    print(" K means clustering accuracy for cifar 10 = {}".format(accuracy))

    # visualise clusters
    cluster_centroids = model.cluster_centers_
    utils.visualize(cluster_centroids)
Ejemplo n.º 2
0
 def __init__(self, hp):
     super(DataGenerator, self).__init__()
     self.hp = hp
     self.train_data_path = hp['train_data_path']
     self.train_images, self.train_labels = utils.load_training_data(
         self.train_data_path)
     self.total_train_images = self.train_images.shape[0]
Ejemplo n.º 3
0
    def test_removeOutliersFromDataset(self):
        data = load_training_data()
        data2 = remove_outliers_from_dataset(data)

        self.assertNotEqual(data, data2)
        self.assertEqual(len(data['t']), 956)
        self.assertEqual(len(data2['t']), 901)
Ejemplo n.º 4
0
def fetch_data():
    """
    Saves and returns image training data

    """
    # Load training data from face_profiles/
    face_profile_data, face_profile_name_index, face_profile_names = utils.load_training_data(
    )

    # Build the classifier
    face_profile = build_svc(face_profile_data, face_profile_name_index,
                             face_profile_names)

    data_dir = os.path.join(os.path.dirname(__file__), "../temp")
    data_path = os.path.join(data_dir, "SVM.pkl")

    if not os.path.exists(data_dir):
        os.mkdir(data_dir)

    # Save the classifier
    with open(data_path, 'wb') as file:
        dump(face_profile, file)

    print("\nTraining data is successfully saved\n")
    return face_profile
Ejemplo n.º 5
0
    def test_loadData(self):
        test_data = load_test_data()
        training_data = load_training_data()

        self.assertEqual(len(training_data), 7, msg='Column number incorrect')
        self.assertEqual(len(test_data), 7, msg='Column number incorrect')

        self.assertEqual(len(training_data['t']),
                         956,
                         msg='Row number incorrect')
        self.assertEqual(len(test_data['p']), 506, msg='Row number incorrect')
def main(data_folder_path, model_folder_path):
    if not isinstance(data_folder_path, Path):
        data_folder_path = Path(data_folder_path)

    if not isinstance(model_folder_path, Path):
        model_folder_path = Path(model_folder_path)

    training_label_path = data_folder_path.joinpath('training_label.txt')
    training_nolabel_path = data_folder_path.joinpath('training_nolabel.txt')
    testing_path = data_folder_path.joinpath('testing_data.txt')

    print("loading training data ...")
    train_x, y = load_training_data(str(training_label_path))
    train_x_no_label = load_training_data(str(training_nolabel_path))

    print("loading testing data ...")
    test_x = load_testing_data(str(testing_path))

    # model = train_word2vec(train_x[:100])
    model = train_word2vec(train_x + train_x_no_label + test_x)

    print("saving model ...")
    model.save(str(model_folder_path.joinpath('w2v_all.model')))
Ejemplo n.º 7
0
def train_non_private_model(args):
    """Train a non-private baseline model on the given dataset for comparison."""
    # Dataloaders
    trainloader = utils.load_training_data(args)
    evalloader = utils.load_evaluation_data(args)
    # Logs
    file = open(os.path.join(args.non_private_model_path,
                             'logs-(initial-lr:{:.2f})-(num-epochs:{:d}).txt'.format(
                                 args.lr, args.num_epochs)), 'w')
    utils.augmented_print("##########################################", file)
    utils.augmented_print(
        "Training a non-private model on '{}' dataset!".format(args.dataset),
        file)
    utils.augmented_print("Initial learning rate: {:.2f}".format(args.lr), file)
    utils.augmented_print(
        "Number of training epochs: {:d}".format(args.num_epochs), file)
    # Non-private model
    model = models.Private_Model('model(non-private)', args)
    if args.cuda:
        model.cuda()
    # Optimizer
    optimizer = optim.SGD(model.parameters(), lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay)
    decay_steps = [int(args.num_epochs * 0.5), int(args.num_epochs * 0.75),
                   int(args.num_epochs * 0.9)]
    # Training steps
    for epoch in range(args.num_epochs):
        if epoch in decay_steps:
            for param_group in optimizer.param_groups:
                param_group['lr'] *= 0.1
        utils.train(model, trainloader, optimizer, args)
        eval_loss, acc = utils.evaluate(model, evalloader, args)
        acc_detailed = utils.evaluate_detailed(model, evalloader, args)
        utils.augmented_print(
            "Epoch {:d} | Evaluation Loss: {:.4f} | Accuracy: {:.2f}% | Detailed Accuracy(%): {}".format(
                epoch + 1, eval_loss, acc,
                np.array2string(acc_detailed, precision=2, separator=', ')),
            file, flush=True)
    # Checkpoints
    state = {'epoch': args.num_epochs, 'accuracy': acc, 'eval_loss': eval_loss,
             'state_dict': model.state_dict()}
    filename = "checkpoint-{}.pth.tar".format(model.name)
    filepath = os.path.join(args.non_private_model_path, filename)
    torch.save(state, filepath)
    utils.augmented_print("##########################################", file)
    file.close()
Ejemplo n.º 8
0
train_with_label = sys.argv[1]
train_no_label = sys.argv[2]
#testing_data = os.path.join(path_prefix, 'testing_data.txt')

model_dir = 'model/'
w2v_path = os.path.join(model_dir, 'w2v_all.model')  # 處理word to vec model的路徑

# 定義句子長度、要不要固定embedding、batch大小、要訓練幾個epoch、learning rate的值、model的資料夾路徑
sen_len = 20
fix_embedding = True  # fix embedding during training
batch_size = 32
epoch = 5
lr = 1e-3

print("loading data ...")  # 把'training_label.txt'跟'training_nolabel.txt'讀進來
train_x, y = load_training_data(train_with_label)
#train_x_no_label = load_training_data(train_no_label)
#train_x_no_label = train_x_no_label[:160000]
# 對input跟labels做預處理
#preprocess = Preprocess(train_x, sen_len, train_x_no_label, w2v_path=w2v_path)
preprocess = Preprocess(train_x, sen_len, w2v_path=w2v_path)
embedding = preprocess.make_embedding(load=True)
#print(preprocess.embedding.most_similar("love"))
train_x = preprocess.sentence_word2idx()
#train_x, train_x_no_label = preprocess.sentence_word2idx()
y = preprocess.labels_to_tensor(y)
# 製作一個model的對象3model = LSTM_Net(embedding, embedding_dim=250, hidden_dim=150, num_layers=1, dropout=0.1, fix_embedding=fix_embedding)
model = LSTM_Net(embedding,
                 embedding_dim=256,
                 hidden_dim=128,
                 num_layers=3,
Ejemplo n.º 9
0
import utils as ut
#Support Vector Machine
import svm

import logging
import warnings

print(__doc__)

###############################################################################
# Building SVC from database

FACE_DIM = (50, 50)  # h = 50, w = 50

# Load training data from face_profiles/
face_profile_data, face_profile_name_index, face_profile_names = ut.load_training_data(
    "./Paragon/Drivers/Backend/m_lowNeurals/vInter/face_profiles/")

print("\n", face_profile_name_index.shape[0], " samples from ",
      len(face_profile_names), " people are loaded")

# Build the classifier
clf, pca = svm.build_SVC(face_profile_data, face_profile_name_index, FACE_DIM)

###############################################################################
# Facial Recognition In Live Tracking

DISPLAY_FACE_DIM = (600, 600)  # the displayed video stream screen dimension
SKIP_FRAME = 2  # the fixed skip frame
frame_skip_rate = 0  # skip SKIP_FRAME frames every other frame
SCALE_FACTOR = 1  # used to resize the captured frame for face detection for faster processing speed
face_cascade = cv2.CascadeClassifier(
Ejemplo n.º 10
0
def train_word2vec(x):
    # 訓練 word to vector 的 word embedding
    model = word2vec.Word2Vec(x,
                              size=250,
                              window=5,
                              min_count=5,
                              workers=12,
                              iter=10,
                              sg=1)
    return model


if __name__ == "__main__":
    path_prefix = sys.argv[1]
    print("loading training data ...")
    train_x, y = load_training_data(
        os.path.join(path_prefix, 'training_label.txt'))
    train_x_no_label = load_training_data(
        os.path.join(path_prefix, 'training_nolabel.txt'))

    print("loading testing data ...")
    test_x = load_testing_data(os.path.join(path_prefix, 'testing_data.txt'))

    #model = train_word2vec(train_x + train_x_no_label + test_x)
    model = train_word2vec(train_x + test_x)

    print("saving model ...")
    #model.save(os.path.join(path_prefix, 'model/w2v_all.model'))
    model.save(os.path.join(path_prefix, 'w2v_all.model'))
Ejemplo n.º 11
0
def main():
    original_train = pd.read_csv(args.train_dataset)
    test = pd.read_csv(args.test_dataset)

    print("Original Training Dataset ", original_train.shape)
    print("Test Dataset ", test.shape)

    augment_train_images(args.train_images_folder)

    augmented_train = pd.read_csv('train_augmented.csv')
    print(augmented_train.shape)
    print(augmented_train.head())

    print("Original Dataset ")
    print(original_train['target'].value_counts())
    print("Augmented Dataset ")
    print(augmented_train['target'].value_counts())

    train_fnames = os.listdir(args.train_images_folder)
    test_fnames = os.listdir(args.test_images_folder)

    train_fnames = [x for x in train_fnames if x.endswith('.jpg')]
    test_fnames = [x for x in test_fnames if x.endswith('.jpg')]

    print("Training Images Count: ", len(train_fnames))
    print("Test Images Count: ", len(test_fnames))

    train_data, train_labels = utils.load_training_data(
        train_fnames, augmented_train, args.train_images_folder)
    train_data = np.array(train_data)
    train_labels = np.array(train_labels)
    print("Shape of training data: ", train_data.shape)
    print("Shape of training labels: ", train_labels.shape)

    le = LabelEncoder()
    train_labels = le.fit_transform(train_labels)
    np.save('classes.npy', le.classes_)

    X_train, X_val, y_train, y_val = train_test_split(
        train_data,
        train_labels,
        test_size=args.test_split_size,
        random_state=42)
    y_train = to_categorical(y_train, num_classes=8)
    y_val = to_categorical(y_val, num_classes=8)

    print("Shape of test_x: ", X_train.shape)
    print("Shape of train_y: ", y_train.shape)
    print("Shape of test_x: ", X_val.shape)
    print("Shape of test_y: ", y_val.shape)

    train_datagenerator = ImageDataGenerator(rescale=1. / 255)

    val_datagenerator = ImageDataGenerator(rescale=1. / 255)

    train_datagenerator.fit(X_train)
    val_datagenerator.fit(X_val)

    num_classes = 8
    model = models.vgg_model(num_classes)

    print(model.summary())

    epochs = 30
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(X_train,
                        y_train,
                        batch_size=30,
                        epochs=epochs,
                        validation_data=(X_val, y_val))

    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(epochs)

    plt.figure(1)
    plt.plot(epochs, acc)
    plt.plot(epochs, val_acc)
    plt.title('Training and validation accuracy')
    plt.savefig('statics/accuracy.png')
    plt.show()

    plt.figure(2)
    plt.plot(epochs, loss)
    plt.plot(epochs, val_loss)
    plt.title('Training and validation loss')
    plt.savefig('statics/validation.png')
    plt.show()

    model.save('dance-form.h5')
Ejemplo n.º 12
0
def main(args):
    # initial parameters
    embedding_size = args.embedding_size
    mention_context_size = args.mention_context_size
    type_context_size = args.type_context_size
    embedding_file = args.embedding_path
    hidden_units = args.hidden_units
    learning_rate = args.learning_rate
    margin = args.margin
    batch_size = args.batch_size
    n_epochs = args.num_epochs

    relation_size = 82
    nkerns = [500]
    filter_size = [1, 1]
    pool = [1, 1]
    l1 = 0.000001
    l2 = 0.000002

    newbob = False
    network_file = args.model_path
    test_file = args.test
    test_result_file = args.test_result
    label_file = args.ontology_path
    label_file_norm = args.norm_ontology_path
    relation_file = args.relation_path
    train_type_flag = args.seen_types

    tup_representation_size = embedding_size * 2

    # load word vectors
    word_vectors, vector_size = load_word_vec(embedding_file)

    # read train and dev file
    print("start loading train and dev file ... ")
    doc_id_list_test, type_list_test, trigger_list_test, left_word_list_test, relation_list_test, \
        right_word_list_test = load_training_data(test_file)

    print("start loading arg and relation files ... ")
    all_type_list, all_type_structures = load_types_1(label_file_norm)
    rel_index, index_rel = read_relation_index(relation_file)
    type_size = len(all_type_list)

    # using a matrix to represent each relation
    relation_matrix = random_init_rel_vec_factor(
        relation_file, tup_representation_size * tup_representation_size)

    train_types = get_types_for_train(train_type_flag, label_file)

    # prepare data structure
    print("start preparing data structures ... ")
    curSeed = 23455
    rng = numpy.random.RandomState(curSeed)
    seed = rng.get_state()[1][0]
    print("seed: ", seed)

    result_index_test_matrix, result_vector_test_matrix, input_context_test_matrix, input_trigger_test_matrix, \
        relation_binary_test_matrix, pos_neg_test_matrix = input_matrix_1_test(
            type_list_test, trigger_list_test, left_word_list_test, relation_list_test, right_word_list_test,
            embedding_size, mention_context_size, relation_size, label_file, word_vectors, rel_index, train_type_flag)

    input_type_matrix, input_type_structure_matrix = type_matrix(
        all_type_list, all_type_structures, embedding_file, type_context_size)

    time1 = time.time()
    dt = theano.config.floatX
    test_set_content = theano.shared(
        numpy.matrix(input_context_test_matrix, dtype=dt))
    test_set_trigger = theano.shared(
        numpy.matrix(input_trigger_test_matrix, dtype=dt))
    test_set_relation_binary = theano.shared(
        numpy.matrix(relation_binary_test_matrix, dtype=dt))
    test_set_posneg = theano.shared(numpy.matrix(pos_neg_test_matrix,
                                                 dtype=dt))
    test_set_y = theano.shared(
        numpy.array(result_index_test_matrix, dtype=numpy.dtype(numpy.int32)))
    test_set_y_vector = theano.shared(
        numpy.matrix(result_vector_test_matrix, dtype=dt))

    train_set_type = theano.shared(numpy.matrix(input_type_matrix, dtype=dt))
    train_set_type_structure = theano.shared(
        numpy.matrix(input_type_structure_matrix, dtype=dt))

    train_types = theano.shared(numpy.matrix(train_types, dtype=dt))

    # compute number of minibatches for training, validation and testing
    n_test_batches = input_trigger_test_matrix.shape[0]
    n_test_batches /= batch_size

    # allocate symbolic variables for the data
    index = T.lscalar()  # index to a [mini]batch
    x_content = T.matrix(
        'x_content')  # the data is presented as rasterized images
    x_trigger = T.matrix(
        'x_trigger')  # the data is presented as rasterized images
    x_relation_binary = T.matrix('x_relation_binary')
    x_pos_neg_flag = T.matrix('x_pos_neg_flag')
    x_type = T.matrix('x_type')
    x_type_structure = T.matrix('x_type_structure')
    y = T.ivector('y')  # the labels are presented as 1D vector of
    y_vector = T.matrix('y_vector')  # the labels are presented as 1D vector of
    x_train_types = T.matrix('x_train_types')

    # [int] labels
    i_shape = [tup_representation_size,
               mention_context_size]  # this is the size of context matrizes

    time2 = time.time()
    print("time for preparing data structures: ", time2 - time1)

    # build actual model

    print('start building the model ... ')
    time1 = time.time()

    rel_w = theano.shared(value=relation_matrix, borrow=True)  ## 26*400

    # Construct the mention structure input Layer
    layer0_input = x_content.reshape((batch_size, 1, i_shape[0], i_shape[1]))
    layer0_input_binary_relation = x_relation_binary.reshape(
        (batch_size, 1, relation_size, i_shape[1]))  ## 100*1*26*5

    # compose amr relation matrix to each tuple
    compose_layer = ComposeLayerMatrix(
        input=layer0_input,
        input_binary_relation=layer0_input_binary_relation,
        rel_w=rel_w,
        rel_vec_size=tup_representation_size)
    layer1_input = compose_layer.output

    # initialize the convolution weight matrix
    filter_shape = (nkerns[0], 1, tup_representation_size, filter_size[1])
    pool_size = (pool[0], pool[1])

    fan_in = numpy.prod(filter_shape[1:])
    fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
               numpy.prod(pool_size))

    w_bound = numpy.sqrt(6. / (fan_in + fan_out))

    conv_w = theano.shared(numpy.asarray(rng.uniform(low=-w_bound,
                                                     high=w_bound,
                                                     size=filter_shape),
                                         dtype=theano.config.floatX),
                           borrow=True)

    b_values = numpy.zeros((filter_shape[0], ), dtype=theano.config.floatX)
    conv_b = theano.shared(value=b_values, borrow=True)

    # conv with pool layer
    layer1_conv = LeNetConvPoolLayer(rng,
                                     W=conv_w,
                                     b=conv_b,
                                     input=layer1_input,
                                     image_shape=(batch_size, 1, i_shape[0],
                                                  i_shape[1]),
                                     filter_shape=filter_shape,
                                     poolsize=pool_size)

    layer1_output = layer1_conv.output
    layer1_flattened = layer1_output.flatten(2)

    trigger_features_shaped = x_trigger.reshape((batch_size, embedding_size))

    layer2_input = T.concatenate([layer1_flattened, trigger_features_shaped],
                                 axis=1)

    # Construct the type structure input Layer
    layer_type_input = x_type_structure.reshape(
        (type_size, 1, tup_representation_size, type_context_size))
    filter_shape_type = (nkerns[0], 1, tup_representation_size, filter_size[1])
    pool_size_type = (pool[0], pool[1])

    # initialize the implicit relation tensor
    type_tensor_shape = (tup_representation_size, tup_representation_size,
                         tup_representation_size)
    type_tensor_w = theano.shared(numpy.asarray(rng.uniform(
        low=-w_bound, high=w_bound, size=type_tensor_shape),
                                                dtype=theano.config.floatX),
                                  borrow=True)

    # compose relation tensor to each tuple
    compose_type_layer = ComposeLayerTensor(input=layer_type_input,
                                            tensor=type_tensor_w)
    layer_type_input1 = compose_type_layer.output

    # conv with pool layer
    layer1_conv_type = LeNetConvPoolLayer(rng,
                                          W=conv_w,
                                          b=conv_b,
                                          input=layer_type_input1,
                                          image_shape=(type_size, 1,
                                                       tup_representation_size,
                                                       type_context_size),
                                          filter_shape=filter_shape_type,
                                          poolsize=pool_size_type)

    layer1_type_output = layer1_conv_type.output
    layer1_type_flattened = layer1_type_output.flatten(2)

    types_shaped = x_type.reshape((type_size, embedding_size))

    layer2_type_input = T.concatenate([layer1_type_flattened, types_shaped],
                                      axis=1)
    layer2_type_input_size = nkerns[0]**pool[1] + embedding_size

    # ranking based max margin loss layer
    train_types_signal = x_train_types.reshape((type_size, 1))
    pos_neg_flag = x_pos_neg_flag.reshape((batch_size, 1))

    layer3 = MaxRankingMarginCosine1(rng=rng,
                                     input=layer2_input,
                                     input_label=layer2_type_input,
                                     true_label=y_vector,
                                     n_in=layer2_type_input_size,
                                     margin=margin,
                                     batch_size=batch_size,
                                     type_size=type_size,
                                     train_type_signal=train_types_signal,
                                     pos_neg_flag=pos_neg_flag)
    cost = layer3.loss

    # create a list of all model parameters to be fit by gradient descent
    param_list = [
        compose_layer.params, layer1_conv.params, compose_type_layer.params
    ]

    params = []
    for p in param_list:
        params += p

    # the cost we minimize during training is the NLL of the model
    lambd1 = T.scalar('lambda1', dt)
    lambd2 = T.scalar('lambda2', dt)

    # L1 and L2 regularization possible
    reg2 = 0
    reg1 = 0
    for p in param_list:
        reg2 += T.sum(p[0]**2)
        reg1 += T.sum(abs(p[0]))

    cost += lambd2 * reg2
    cost += lambd1 * reg1

    lr = T.scalar('lr', dt)

    start = index * batch_size
    end = (index + 1) * batch_size

    testVariables = {}
    testVariables[x_content] = test_set_content[start:end]
    testVariables[x_trigger] = test_set_trigger[start:end]
    testVariables[x_relation_binary] = test_set_relation_binary[start:end]
    testVariables[x_type] = train_set_type
    testVariables[x_type_structure] = train_set_type_structure
    testVariables[y] = test_set_y[start:end]
    testVariables[y_vector] = test_set_y_vector[start:end]
    testVariables[x_train_types] = train_types
    testVariables[x_pos_neg_flag] = test_set_posneg[start:end]

    print("length of train variables ", len(testVariables))

    # create a list of gradients for all model parameters
    grads = T.grad(cost, params)

    # train_model is a function that updates the model parameters by SGD Since this model has many parameters,
    # it would be tedious to manually create an update rule for each model parameter. We thus create the updates
    # list by automatically looping over all (params[i],grads[i]) pairs.
    updates = []
    for param_i, grad_i in zip(params, grads):
        updates.append((param_i, param_i - lr * grad_i))

    test_model_confidence = theano.function([index],
                                            layer3.results(y),
                                            on_unused_input='ignore',
                                            givens=testVariables)

    time2 = time.time()
    print("time for building the model: ", time2 - time1)

    print("loading saved network")
    netfile = open(network_file)

    relW = cPickle.load(netfile)
    compose_layer.params[0].set_value(relW, borrow=True)

    convolW = cPickle.load(netfile)
    convolB = cPickle.load(netfile)
    layer1_conv.params[0].set_value(convolW, borrow=True)
    layer1_conv.params[1].set_value(convolB, borrow=True)
    layer1_conv_type.params[0].set_value(convolW, borrow=True)
    layer1_conv_type.params[1].set_value(convolB, borrow=True)

    typeW = cPickle.load(netfile)
    compose_type_layer.params[0].set_value(typeW, borrow=True)
    netfile.close()

    print("finish loading network")

    test_batch_size = 100
    all_batches = len(result_index_test_matrix) / test_batch_size

    confidence_prob = []
    confidence_value = []
    confidence_list = []
    confidence = [test_model_confidence(i) for i in xrange(all_batches)]
    for r in range(0, len(confidence)):
        for r1 in range(0, test_batch_size):
            hypo_result = confidence[r][0].item(r1)
            confidence_prob.append(confidence[r][2][r1])
            confidence_value.append(confidence[r][1][r1])
            confidence_list.append(hypo_result)

    y_pred = confidence_list

    f = open(test_result_file, "w")
    for i in range(0, len(y_pred)):
        f.write(str(y_pred[i]) + "\t" + str(confidence_value[i]) + "\t")
        for j in range(0, type_size):
            f.write(str(confidence_prob[i][j]) + " ")
        f.write("\n")

    f.close()
Ejemplo n.º 13
0
def train(epochs, batch_size, input_dir, model_save_dir):

    # Make an instance of the VGG class
    vgg_model = VGG_MODEL(image_shape)

    # Get High-Resolution(HR) [148,148,3] in this case and corresponding Low-Resolution(LR) images
    x_train_lr, x_train_hr = utils.load_training_data(input_dir, [148, 148, 3])

    #Based on the the batch size, get the total number of batches
    batch_count = int(x_train_hr.shape[0] / batch_size)

    #Get the downscaled image shape based on the downscale factor
    image_shape_downscaled = utils.get_downscaled_shape(
        image_shape, downscale_factor)

    # Initialize the generator network with the input image shape as the downscaled image shape (shape of LR images)
    generator = networks.Generator(input_shape=image_shape_downscaled)

    # Initialize the discriminator with the input image shape as the original image shape (HR image shape)
    discriminator = networks.Discriminator(image_shape)

    # Get the optimizer to tweak parameters based on loss
    optimizer = vgg_model.get_optimizer()

    # Compile the three models - generator, discriminator and gan(comb of both gen and disc - this network will train generator and will not tweak discriminator)
    generator.compile(loss=vgg_model.vgg_loss, optimizer=optimizer)
    discriminator.compile(loss="binary_crossentropy", optimizer=optimizer)
    gan = networks.GAN_Network(generator, discriminator,
                               image_shape_downscaled, optimizer,
                               vgg_model.vgg_loss)

    # Run training for the number of epochs defined
    for e in range(1, epochs + 1):
        print('-' * 15, 'Epoch %d' % e, '-' * 15)
        for _ in tqdm(range(batch_count)):

            # Get the next batch of LR and HR images
            image_batch_lr, image_batch_hr = utils.get_random_batch(
                x_train_lr, x_train_hr, x_train_hr.shape[0], batch_size)

            generated_images_sr = generator.predict(image_batch_lr)
            print(generated_images_sr.shape)

            real_data_Y = np.ones(
                batch_size) - np.random.random_sample(batch_size) * 0.2
            fake_data_Y = np.random.random_sample(batch_size) * 0.2

            discriminator.trainable = True
            print(real_data_Y.shape)
            d_loss_real = discriminator.train_on_batch(image_batch_hr,
                                                       real_data_Y)
            d_loss_fake = discriminator.train_on_batch(generated_images_sr,
                                                       fake_data_Y)
            discriminator_loss = 0.5 * np.add(d_loss_fake, d_loss_real)

            rand_nums = np.random.randint(0,
                                          x_train_hr.shape[0],
                                          size=batch_size)
            image_batch_hr = x_train_hr[rand_nums]
            image_batch_lr = x_train_lr[rand_nums]

            gan_Y = np.ones(
                batch_size) - np.random.random_sample(batch_size) * 0.2
            discriminator.trainable = False
            gan_loss = gan.train_on_batch(image_batch_lr,
                                          [image_batch_hr, gan_Y])

        print("discriminator_loss : %f" % discriminator_loss)
        print("gan_loss :", gan_loss)
        gan_loss = str(gan_loss)

        if e % 50 == 0:
            generator.save_weights(model_save_dir + 'gen_model%d.h5' % e)
            discriminator.save_weights(model_save_dir + 'dis_model%d.h5' % e)

    networks.save_model(gan)
Ejemplo n.º 14
0
path_prefix = "/home/shannon/Downloads/dataset"
model_dir = "./model"
train_w_filename = os.path.join(path_prefix, 'training_label.txt')
train_wo_filename = os.path.join(path_prefix, 'training_label_semi.txt')
if len(sys.argv) > 2:
    train_w_filename = sys.argv[1]
    train_wo_filename = sys.argv[2]
w2v_model_filename = os.path.join(model_dir, 'w2v_labeled.model')

# checking device to cpu or gpu
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("device:", device)

# loading data
print("loading training data ...")
train_x, y = load_training_data(train_w_filename)
#train_x_no_label, y_no_label = load_training_data(train_wo_filename)
#train_x = train_x+train_x_no_label
#y = y+y_no_label

# parameters
sen_len = 32  #32
fix_embedding = True  # fix embedding during training
batch_size = 16  #1024
epoch = 20
lr = 0.0002  #0.0002 0.00005

# preprocessing data
print("preprocessing training data ...")
preprocess = Preprocess(train_x, sen_len, w2v_path=w2v_model_filename)
embedding_matrix = preprocess.make_embedding(load=True)
Ejemplo n.º 15
0
import pandas as pd
import argparse
from gensim.models import word2vec
from utils import load_training_data, load_testing_data


def train_word2vec(x):
    # 訓練 word to vector 的 word embedding
    model = word2vec.Word2Vec(x,
                              size=250,
                              window=5,
                              min_count=10,
                              workers=12,
                              iter=10,
                              sg=1)
    return model


if __name__ == "__main__":
    path_prefix = './'
    print("loading training data ...")
    train_x, y = load_training_data('./training_label.txt')
    train_x_no_label = load_training_data('./training_nolabel.txt')

    print("loading testing data ...")
    test_x = load_testing_data('./testing_data.txt')

    model = train_word2vec(train_x + train_x_no_label + test_x)

    print("saving model ...")
    model.save(os.path.join(path_prefix, 'w2v_all.model'))
Ejemplo n.º 16
0
def train_word2vec(x):
    # 訓練word to vector 的 word embedding
    model = word2vec.Word2Vec(x,
                              size=250,
                              window=5,
                              min_count=5,
                              workers=12,
                              iter=10,
                              sg=1)
    return model


if __name__ == "__main__":
    print("loading training data ...")
    if len(sys.argv) == 3:
        train_x, y = u.load_training_data(sys.argv[1])
        train_x_no_label = u.load_training_data(sys.argv[2])
    else:
        train_x, y = u.load_training_data(
            os.path.join(path_prefix, 'training_label.txt'))
        train_x_no_label = u.load_training_data(
            os.path.join(path_prefix, 'training_nolabel.txt'))

    print("loading testing data ...")
    test_x = u.load_testing_data(os.path.join(path_prefix, 'testing_data.txt'))

    print('start training...')
    #model = train_word2vec(train_x + train_x_no_label + test_x)
    model = train_word2vec(train_x + test_x)

    print("saving model ...")
Ejemplo n.º 17
0
import numpy as np
import utils
import matplotlib.pyplot as plt
import time

train_images, train_labels = utils.load_training_data(
    "cifar-10-batches-py")  # 50000,3072
test_images, test_labels = utils.load_test_data(
    "cifar-10-batches-py")  # 10000,3072

mean_channel_train_images = utils.cifar_10_color(train_images)  # 50000,3
mean_channel_test_images = utils.cifar_10_color(test_images)  # 10000,3

# task 1
tic = time.time()
mu, sigma, prior = utils.naive_bayes_learn(mean_channel_train_images,
                                           train_labels)  # 10x3 10x3  10x1
prediction = utils.cifar10_classifier_naivebayes(mean_channel_test_images, mu,
                                                 sigma, prior)
accuracy = utils.classification_accuracy(prediction, test_labels)
print("naive bayes accuracy = {}".format(accuracy))
toc = time.time()
print("Time taken for Naive Bayes = ", toc - tic)
print("----------------------------------")

# # task 2
tic = time.time()
mu, covariance, prior = utils.bayes_learn(mean_channel_train_images,
                                          train_labels)
prediction = utils.cifar10_classifier_bayes(mean_channel_test_images, mu,
                                            covariance, prior)
Ejemplo n.º 18
0
import utils as ut
import svm
import sys
import logging
import warnings


print(__doc__)

###############################################################################
# Building SVC from database

FACE_DIM = (50,50) # h = 50, w = 50

# Load training data from face_profiles/
face_profile_data, face_profile_name_index, face_profile_names  = ut.load_training_data("../face_profiles/")

print "\n", face_profile_name_index.shape[0], " samples from ", len(face_profile_names), " people are loaded"

# Build the classifier
clf, pca = svm.build_SVC(face_profile_data, face_profile_name_index, FACE_DIM)


###############################################################################
# Facial Recognition In Live Tracking


DISPLAY_FACE_DIM = (200, 200) # the displayed video stream screen dimention 
SKIP_FRAME = 2      # the fixed skip frame
frame_skip_rate = 0 # skip SKIP_FRAME frames every other frame
SCALE_FACTOR = 4 # used to resize the captured frame for face detection for faster processing speed
Ejemplo n.º 19
0
# ----------
# script to train a simple cnn (later maybe AlexNet) with the German Trafic Sign Dataset from https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/published-archive.html
#
# run image_preprocessing.py once before running this script
# ----------

from utils import load_training_data, get_basic_model, get_complex_model, compute_confusion_matrix, plot_confusion_matrix, plot_history, show_images, classes
from matplotlib import pyplot as plt

# load the dataset
train_image_dir = "data/train"
X_data, y_data = load_training_data(train_image_dir)

# splitting
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_data,
                                                    y_data,
                                                    test_size=0.2,
                                                    random_state=15)
X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                  y_train,
                                                  test_size=0.2,
                                                  random_state=33)
print("Train set size: {0}, Val set size: {1}, Test set size: {2}".format(
    len(X_train), len(X_val), len(X_test)))

# base model
basic_model = get_basic_model()
history = basic_model.fit(X_train,
                          y_train,
                          epochs=10,
Ejemplo n.º 20
0
            node.op = 'Sub'
            if 'use_locking' in node.attr:
                del node.attr['use_locking']

with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def,
                        name=node_name_prefix,
                        input_map=None,
                        return_elements=None,
                        op_dict=None,
                        producer_op_list=None)

    for op in graph.get_operations():
        print(op.name, op.values())

    x = graph.get_tensor_by_name(input_node_name)
    keep_prob = graph.get_tensor_by_name(keep_prob_node_name)
    y = graph.get_tensor_by_name(prediction_node_name)

    dataset = utils.load_training_data(batch_size=1)
    with tf.Session(graph=graph) as sess:
        image_batch, label_batch, path_batch = dataset.next_batch(
            augmented=False)

        visualization(image_batch[0])

        sess.run(tf.global_variables_initializer())
        pred = sess.run(y, feed_dict={x: image_batch, keep_prob: .5})
        print("path: {}, prediction: {}, label: {}".format(
            path_batch[0], pred[0], label_batch[0]))
Ejemplo n.º 21
0
def main(args):
    # initial parameters
    embedding_size = args.embedding_size
    mention_context_size = args.mention_context_size
    type_context_size = args.type_context_size
    embedding_file = args.embedding_path
    hidden_units = args.hidden_units
    learning_rate = args.learning_rate
    margin = args.margin
    batch_size = args.batch_size
    n_epochs = args.num_epochs

    relation_size = 82
    nkerns = [500]
    filter_size = [1, 1]
    pool = [1, 1]
    l1 = 0.000001
    l2 = 0.000002

    newbob = False
    network_file = args.model_path
    train_file = args.train
    dev_file = args.dev
    test_file = args.test
    label_file = args.ontology_path
    label_file_norm = args.norm_ontology_path
    relation_file = args.relation_path
    train_type_flag = args.seen_types

    tup_representation_size = embedding_size * 2

    # load word vectors
    word_vectors, vector_size = load_word_vec(embedding_file)

    # read train and dev file
    print ("start loading train and dev file ... ")
    doc_id_list_train, type_list_train, trigger_list_train, left_word_list_train, relation_list_train, \
        right_word_list_train = load_training_data(train_file)
    doc_id_list_dev, type_list_dev, trigger_list_dev, left_word_list_dev, relation_list_dev, \
        right_word_list_dev = load_training_data(dev_file)
    doc_id_list_test, type_list_test, trigger_list_test, left_word_list_test, relation_list_test, \
        right_word_list_test = load_training_data(test_file)

    print ("start loading arg and relation files ... ")
    all_type_list, all_type_structures = load_types_1(label_file_norm)
    rel_index, index_rel = read_relation_index(relation_file)
    type_size = len(all_type_list)

    # using a matrix to represent each relation
    relation_matrix = random_init_rel_vec_factor(relation_file, tup_representation_size*tup_representation_size)

    train_types = get_types_for_train(train_type_flag, label_file)

    # prepare data structure
    print ("start preparing data structures ... ")
    curSeed = 23455
    rng = numpy.random.RandomState(curSeed)
    seed = rng.get_state()[1][0]
    print ("seed: ", seed)

    result_index_train_matrix, result_vector_train_matrix, input_context_train_matrix, input_trigger_train_matrix, \
        relation_binary_train_matrix, pos_neg_train_matrix = input_matrix_1(
            type_list_train, trigger_list_train, left_word_list_train, relation_list_train, right_word_list_train,
            embedding_size, mention_context_size, relation_size, label_file, word_vectors, rel_index, train_type_flag)

    result_index_dev_matrix, result_vector_dev_matrix, input_context_dev_matrix, input_trigger_dev_matrix, \
        relation_binary_dev_matrix, pos_neg_dev_matrix = input_matrix_1_test(
            type_list_dev, trigger_list_dev, left_word_list_dev, relation_list_dev, right_word_list_dev, embedding_size,
            mention_context_size, relation_size, label_file, word_vectors, rel_index, train_type_flag)

    result_index_test_matrix, result_vector_test_matrix, input_context_test_matrix, input_trigger_test_matrix, \
        relation_binary_test_matrix, pos_neg_test_matrix = input_matrix_1_test(
            type_list_test, trigger_list_test, left_word_list_test, relation_list_test, right_word_list_test,
            embedding_size, mention_context_size, relation_size, label_file, word_vectors, rel_index, train_type_flag)

    input_type_matrix, input_type_structure_matrix = type_matrix(
        all_type_list, all_type_structures, embedding_file, type_context_size)

    time1 = time.time()
    dt = theano.config.floatX
    train_set_content = theano.shared(numpy.matrix(input_context_train_matrix, dtype=dt))
    valid_set_content = theano.shared(numpy.matrix(input_context_dev_matrix, dtype=dt))
    test_set_content = theano.shared(numpy.matrix(input_context_test_matrix, dtype=dt))

    train_set_trigger = theano.shared(numpy.matrix(input_trigger_train_matrix, dtype=dt))
    valid_set_trigger = theano.shared(numpy.matrix(input_trigger_dev_matrix, dtype=dt))
    test_set_trigger = theano.shared(numpy.matrix(input_trigger_test_matrix, dtype=dt))

    train_set_relation_binary = theano.shared(numpy.matrix(relation_binary_train_matrix, dtype=dt))
    valid_set_relation_binary = theano.shared(numpy.matrix(relation_binary_dev_matrix, dtype=dt))
    test_set_relation_binary = theano.shared(numpy.matrix(relation_binary_test_matrix, dtype=dt))

    train_set_posneg = theano.shared(numpy.matrix(pos_neg_train_matrix, dtype=dt))
    valid_set_posneg = theano.shared(numpy.matrix(pos_neg_dev_matrix, dtype=dt))
    test_set_posneg = theano.shared(numpy.matrix(pos_neg_test_matrix, dtype=dt))

    train_set_y = theano.shared(numpy.array(result_index_train_matrix, dtype=numpy.dtype(numpy.int32)))
    valid_set_y = theano.shared(numpy.array(result_index_dev_matrix, dtype=numpy.dtype(numpy.int32)))
    test_set_y = theano.shared(numpy.array(result_index_test_matrix, dtype=numpy.dtype(numpy.int32)))

    train_set_y_vector = theano.shared(numpy.matrix(result_vector_train_matrix, dtype=dt))
    valid_set_y_vector = theano.shared(numpy.matrix(result_vector_dev_matrix, dtype=dt))
    test_set_y_vector = theano.shared(numpy.matrix(result_vector_test_matrix, dtype=dt))

    train_set_type = theano.shared(numpy.matrix(input_type_matrix, dtype=dt))
    train_set_type_structure = theano.shared(numpy.matrix(input_type_structure_matrix, dtype=dt))

    train_types = theano.shared(numpy.matrix(train_types, dtype=dt))

    # compute number of minibatches for training, validation and testing
    n_train_batches = input_trigger_train_matrix.shape[0]
    n_valid_batches = input_trigger_dev_matrix.shape[0]
    n_test_batches = input_trigger_test_matrix.shape[0]
    n_train_batches /= batch_size
    n_valid_batches /= batch_size
    n_test_batches /= batch_size

    # allocate symbolic variables for the data
    index = T.lscalar()  # index to a [mini]batch
    x_content = T.matrix('x_content')  # the data is presented as rasterized images
    x_trigger = T.matrix('x_trigger')  # the data is presented as rasterized images
    x_relation_binary = T.matrix('x_relation_binary')
    x_pos_neg_flag = T.matrix('x_pos_neg_flag')
    x_type = T.matrix('x_type')
    x_type_structure = T.matrix('x_type_structure')
    y = T.ivector('y')  # the labels are presented as 1D vector of
    y_vector = T.matrix('y_vector')  # the labels are presented as 1D vector of
    x_train_types = T.matrix('x_train_types')

    # [int] labels
    i_shape = [tup_representation_size, mention_context_size]  # this is the size of context matrizes

    time2 = time.time()
    print ("time for preparing data structures: ", time2 - time1)

    # build actual model

    print ('start building the model ... ')
    time1 = time.time()

    rel_w = theano.shared(value=relation_matrix, borrow=True)  ## 26*400

    # Construct the mention structure input Layer
    layer0_input = x_content.reshape((batch_size, 1, i_shape[0], i_shape[1]))
    layer0_input_binary_relation = x_relation_binary.reshape((batch_size, 1, relation_size, i_shape[1]))  ## 100*1*26*5

    # compose amr relation matrix to each tuple
    compose_layer = ComposeLayerMatrix(input=layer0_input, input_binary_relation=layer0_input_binary_relation,
                                      rel_w=rel_w, rel_vec_size=tup_representation_size)
    layer1_input = compose_layer.output

    # initialize the convolution weight matrix
    filter_shape = (nkerns[0], 1, tup_representation_size, filter_size[1])
    pool_size = (pool[0], pool[1])

    fan_in = numpy.prod(filter_shape[1:])
    fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) / numpy.prod(pool_size))

    w_bound = numpy.sqrt(6. / (fan_in + fan_out))

    conv_w = theano.shared(numpy.asarray(
        rng.uniform(low=-w_bound, high=w_bound, size=filter_shape),
        dtype=theano.config.floatX),
        borrow=True)

    b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
    conv_b = theano.shared(value=b_values, borrow=True)

    # conv with pool layer
    layer1_conv = LeNetConvPoolLayer(rng, W=conv_w, b=conv_b, input=layer1_input,
                                     image_shape=(batch_size, 1, i_shape[0], i_shape[1]),
                                     filter_shape=filter_shape, poolsize=pool_size)

    layer1_output = layer1_conv.output
    layer1_flattened = layer1_output.flatten(2)

    trigger_features_shaped = x_trigger.reshape((batch_size, embedding_size))

    layer2_input = T.concatenate([layer1_flattened, trigger_features_shaped], axis=1)

    # Construct the type structure input Layer
    layer_type_input = x_type_structure.reshape((type_size, 1, tup_representation_size, type_context_size))
    filter_shape_type = (nkerns[0], 1, tup_representation_size, filter_size[1])
    pool_size_type = (pool[0], pool[1])

    # initialize the implicit relation tensor
    type_tensor_shape = (tup_representation_size, tup_representation_size, tup_representation_size)
    type_tensor_w = theano.shared(numpy.asarray(rng.uniform(low=-w_bound, high=w_bound, size=type_tensor_shape),
                                                dtype=theano.config.floatX), borrow=True)

    # compose relation tensor to each tuple
    compose_type_layer = ComposeLayerTensor(input=layer_type_input, tensor=type_tensor_w)
    layer_type_input1 = compose_type_layer.output

    # conv with pool layer
    layer1_conv_type = LeNetConvPoolLayer(rng, W=conv_w, b=conv_b, input=layer_type_input1,
                                          image_shape=(type_size, 1, tup_representation_size, type_context_size),
                                          filter_shape=filter_shape_type, poolsize=pool_size_type)

    layer1_type_output = layer1_conv_type.output
    layer1_type_flattened = layer1_type_output.flatten(2)

    types_shaped = x_type.reshape((type_size, embedding_size))

    layer2_type_input = T.concatenate([layer1_type_flattened, types_shaped], axis=1)
    layer2_type_input_size = nkerns[0] ** pool[1] + embedding_size

    # ranking based max margin loss layer
    train_types_signal = x_train_types.reshape((type_size, 1))
    pos_neg_flag = x_pos_neg_flag.reshape((batch_size, 1))

    layer3 = MaxRankingMarginCosine1(rng=rng, input=layer2_input, input_label=layer2_type_input, true_label=y_vector,
                                     n_in=layer2_type_input_size, margin=margin, batch_size=batch_size,
                                     type_size=type_size, train_type_signal=train_types_signal,
                                     pos_neg_flag=pos_neg_flag)
    cost = layer3.loss

    # create a list of all model parameters to be fit by gradient descent
    param_list = [compose_layer.params, layer1_conv.params, compose_type_layer.params]

    params = []
    for p in param_list:
        params += p

    # the cost we minimize during training is the NLL of the model
    lambd1 = T.scalar('lambda1', dt)
    lambd2 = T.scalar('lambda2', dt)

    # L1 and L2 regularization possible
    reg2 = 0
    reg1 = 0
    for p in param_list:
        reg2 += T.sum(p[0] ** 2)
        reg1 += T.sum(abs(p[0]))

    cost += lambd2 * reg2
    cost += lambd1 * reg1

    lr = T.scalar('lr', dt)

    start = index * batch_size
    end = (index + 1) * batch_size

    validVariables = {}
    validVariables[x_content] = valid_set_content[start: end]
    validVariables[x_trigger] = valid_set_trigger[start: end]
    validVariables[x_relation_binary] = valid_set_relation_binary[start: end]
    validVariables[x_type] = train_set_type
    validVariables[x_type_structure] = train_set_type_structure
    validVariables[y] = valid_set_y[start: end]
    validVariables[y_vector] = valid_set_y_vector[start: end]
    validVariables[x_train_types] = train_types
    validVariables[x_pos_neg_flag] = valid_set_posneg[start: end]

    testVariables = {}
    testVariables[x_content] = test_set_content[start: end]
    testVariables[x_trigger] = test_set_trigger[start: end]
    testVariables[x_relation_binary] = test_set_relation_binary[start: end]
    testVariables[x_type] = train_set_type
    testVariables[x_type_structure] = train_set_type_structure
    testVariables[y] = test_set_y[start: end]
    testVariables[y_vector] = test_set_y_vector[start: end]
    testVariables[x_train_types] = train_types
    testVariables[x_pos_neg_flag] = test_set_posneg[start: end]

    trainVariables = {}
    trainVariables[x_content] = train_set_content[start: end]
    trainVariables[x_trigger] = train_set_trigger[start: end]
    trainVariables[x_relation_binary] = train_set_relation_binary[start: end]
    trainVariables[x_type] = train_set_type
    trainVariables[x_type_structure] = train_set_type_structure
    trainVariables[y] = train_set_y[start: end]
    trainVariables[y_vector] = train_set_y_vector[start: end]
    trainVariables[x_train_types] = train_types
    trainVariables[x_pos_neg_flag] = train_set_posneg[start: end]

    print ("length of train variables ", len(trainVariables))

    # create a list of gradients for all model parameters
    grads = T.grad(cost, params)

    # train_model is a function that updates the model parameters by SGD Since this model has many parameters,
    # it would be tedious to manually create an update rule for each model parameter. We thus create the updates
    # list by automatically looping over all (params[i],grads[i]) pairs.
    updates = []
    for param_i, grad_i in zip(params, grads):
        updates.append((param_i, param_i - lr * grad_i))

    test_model_confidence = theano.function([index], layer3.results(y), on_unused_input='ignore', givens=testVariables)
    eval_model_confidence = theano.function([index], layer3.results(y), on_unused_input='ignore', givens=validVariables)
    train_model = theano.function([index, lr, lambd1, lambd2], [cost, layer3.loss], updates=updates,
                                  on_unused_input='ignore', givens=trainVariables)

    time2 = time.time()
    print ("time for building the model: ", time2 - time1)

    # Train the  MODEL
    print ('start training ... ')
    time1 = time.time()

    num_examples_per_epoch = len(trigger_list_train)
    validation_frequency = num_examples_per_epoch / batch_size  # validate after each epoch
    best_params = []
    best_micro_fscore = -1
    last_fscore = -1
    best_macro_fscore = -1
    best_iter = 0
    best_micro_fscore_eval = -1
    best_macro_fscore_eval = -1
    best_iter_eval = 0

    start_time = time.clock()

    epoch = 0
    done_looping = False

    max_no_improvement = 5
    no_improvement = 0

    while (epoch < n_epochs) and (not done_looping):
        print ('epoch = ', epoch)
        epoch += 1
        this_n_train_batches = num_examples_per_epoch / batch_size
        for minibatch_index in xrange(this_n_train_batches):
            iter = (epoch - 1) * this_n_train_batches + minibatch_index
            if iter % 100 == 0:
                print ('training @ iter = ', iter)
            cost_ij, loss = train_model(minibatch_index, learning_rate, l1, l2)

            print ("cost: " + str(cost_ij))
            print ("loss:   " + str(loss))

            if (iter + 1) % validation_frequency == 0:
                # test
                confidence_eval = [test_model_confidence(i) for i in xrange(n_test_batches)]
                confidence_list_eval = []
                for r in range(0, len(confidence_eval)):
                    for r1 in range(0, batch_size):
                        hypo_result_eval = confidence_eval[r][0].item(r1)
                        confidence_list_eval.append(hypo_result_eval)

                y_pred_eval = confidence_list_eval
                y_true_eval = result_index_test_matrix[:n_test_batches * batch_size]
                y_true_eval_2 = []
                for i in range(len(y_true_eval)):
                    y_true_eval_2.append(int(y_true_eval[i]))

                labels1 = []
                for l in range(1, 380):
                    labels1.append(l)
                this_micro_fscore_eval = f1_score(y_true_eval_2, y_pred_eval, labels=labels1, average='micro')
                this_macro_fscore_eval = f1_score(y_true_eval_2, y_pred_eval, labels=labels1, average='macro')
                print('EVAL: ***   epoch %i, best_validation %f, best_validation_m1 %f, learning_rate %f,  '
                      'minibatch %i/%i, validation fscore %f %%' % (epoch, best_micro_fscore_eval * 100.,
                                                                    best_macro_fscore_eval * 100, learning_rate,
                                                                    minibatch_index + 1, this_n_train_batches,
                                                                    this_micro_fscore_eval * 100.))

                if this_micro_fscore_eval > best_micro_fscore_eval:
                    best_micro_fscore_eval = this_micro_fscore_eval
                    best_macro_fscore_eval = this_macro_fscore_eval
                    best_iter_eval = iter

                # validate
                confidence = [eval_model_confidence(i) for i in xrange(n_valid_batches)]

                confidence_list = []
                for r in range(0, len(confidence)):
                    for r1 in range(0, batch_size):
                        hypo_result = confidence[r][0].item(r1)
                        confidence_list.append(hypo_result)

                y_pred = confidence_list
                y_true = result_index_dev_matrix[:n_valid_batches * batch_size]
                y_true_2 = []
                for i in range(len(y_true)):
                    y_true_2.append(int(y_true[i]))

                labels = []
                for l in range(1, 380):
                    labels.append(l)
                this_micro_fscore = f1_score(y_true_2, y_pred, labels=labels, average='micro')
                this_macro_fscore = f1_score(y_true_2, y_pred, labels=labels, average='macro')

                print('epoch %i, best_validation %f, best_validation_m1 %f, learning_rate %f, minibatch %i/%i, '
                      'validation fscore %f %%' % (epoch, best_micro_fscore * 100., best_macro_fscore * 100,
                                                   learning_rate, minibatch_index + 1, this_n_train_batches,
                                                   this_micro_fscore * 100.))

                # if we got the best validation score until now
                if this_micro_fscore > best_micro_fscore:
                    best_micro_fscore = this_micro_fscore
                    best_macro_fscore = this_macro_fscore
                    best_iter = iter

                    best_params = []
                    for p in param_list:
                        p_param = []
                        for part in p:
                            p_param.append(part.get_value(borrow=False))
                        best_params.append(p_param)
                    no_improvement = 0
                else:
                    if this_micro_fscore > last_fscore:
                        no_improvement -= 1
                        no_improvement = max(no_improvement, 0)
                    else:
                        no_improvement += 1
                        updatestep = minibatch_index + this_n_train_batches * (epoch - 1)
                        if newbob:  # learning rate schedule depending on dev result
                            learning_rate /= 1.2
                            print ("reducing learning rate to ", learning_rate)
                last_fscore = this_micro_fscore
            if newbob:  # learning rate schedule depending on dev result
                if no_improvement > max_no_improvement or learning_rate < 0.0000001:
                    done_looping = True
                    break

        if not newbob:
            if epoch + 1 > 10:
                learning_rate /= 1.2
                print ("reducing learning rate to ", learning_rate)
            if epoch + 1 > 50:
                done_looping = True
                break

    end_time = time.clock()
    print('Optimization complete.')
    print('Best validation score of %f %% obtained for c=%i, nk=%i, f=%i, h=%i  at iteration %i,' %
          (best_micro_fscore * 100., mention_context_size, nkerns[0], filter_size[1], hidden_units, best_iter + 1))

    time2 = time.time()
    print ("time for training: ", time2 - time1)

    print('Saving net.')
    save_file = open(network_file, 'wb')
    for p in best_params:
        for p_part in p:
            cPickle.dump(p_part, save_file, -1)
    save_file.close()
Ejemplo n.º 22
0
# 3份数据的路径
train_with_label = './data/training_label.txt'
train_no_label = './data/training_nolabel.txt'
testing_data = './data/testing_data.txt'
# word2vec模型的路径
w2v_path = os.path.join('./data/w2v_all.model')

sen_len = 20  # 统一句子长度
fix_embedding = True  # 训练时固定embedding模型
batch_size = 128
epoch = 5
lr = 0.001

print("loading data ...")  # 读取'training_label.txt'和'training_nolabel.txt'
train_x, y = utils.load_training_data(train_with_label)
train_x_no_label = utils.load_training_data(train_no_label)

# 对input和labels做预处理
preprocess = Preprocess(train_x, sen_len, w2v_path=w2v_path)
embedding = preprocess.make_embedding(load=True)
train_x = preprocess.sentence_word2idx()
y = preprocess.labels_to_tensor(y)

# 定义model
model = LSTM_Net(embedding,
                 embedding_dim=250,
                 hidden_dim=150,
                 num_layers=1,
                 dropout=0.5,
                 fix_embedding=fix_embedding)
Ejemplo n.º 23
0
def main():
    try:
        import sklearn

        if sklearn.__version__ < "0.20":
            gs.fatal("Package python3-scikit-learn 0.20 or newer is not installed")

    except ImportError:
        gs.fatal("Package python3-scikit-learn 0.20 or newer is not installed")

    try:
        import pandas as pd
        
    except ImportError:
        gs.fatal("Package python3-pandas 0.25 or newer is not installed")

    # parser options ---------------------------------------------------------------------------------------------------
    group = options["group"]
    training_map = options["training_map"]
    training_points = options["training_points"]
    field = options["field"]
    model_save = options["save_model"]
    model_name = options["model_name"]
    hyperparams = {
        "penalty": options["penalty"],
        "alpha": options["alpha"],
        "l1_ratio": options["l1_ratio"],
        "C": options["c"],
        "epsilon": options["epsilon"],
        "min_samples_leaf": options["min_samples_leaf"],
        "n_estimators": options["n_estimators"],
        "learning_rate": options["learning_rate"],
        "subsample": options["subsample"],
        "max_depth": options["max_depth"],
        "max_features": options["max_features"],
        "n_neighbors": options["n_neighbors"],
        "weights": options["weights"],
        "hidden_layer_sizes": options["hidden_units"],
    }
    cv = int(options["cv"])
    group_raster = options["group_raster"]
    importances = flags["f"]
    preds_file = options["preds_file"]
    classif_file = options["classif_file"]
    fimp_file = options["fimp_file"]
    param_file = options["param_file"]
    norm_data = flags["s"]
    random_state = int(options["random_state"])
    load_training = options["load_training"]
    save_training = options["save_training"]
    n_jobs = int(options["n_jobs"])
    balance = flags["b"]
    category_maps = option_to_list(options["category_maps"])

    # define estimator -------------------------------------------------------------------------------------------------
    hyperparams, param_grid = process_param_grid(hyperparams)
    estimator, mode = predefined_estimators(
        model_name, random_state, n_jobs, hyperparams
    )

    # remove dict keys that are incompatible for the selected estimator
    estimator_params = estimator.get_params()
    param_grid = {
        key: value for key, value in param_grid.items() if key in estimator_params
    }
    scoring, search_scorer = scoring_metrics(mode)

    # checks of input options ------------------------------------------------------------------------------------------
    if (
        mode == "classification"
        and balance is True
        and model_name not in check_class_weights()
    ):

        gs.warning(model_name + " does not support class weights")
        balance = False

    if mode == "regression" and balance is True:
        gs.warning("Balancing of class weights is only possible for classification")
        balance = False

    if classif_file:
        if cv <= 1:
            gs.fatal(
                "Output of cross-validation global accuracy requires cross-validation cv > 1"
            )
        if not os.path.exists(os.path.dirname(classif_file)):
            gs.fatal("Directory for output file {} does not exist".format(classif_file))

    # feature importance file selected but no cross-validation scheme used
    if importances:
        if sklearn.__version__ < "0.22":
            gs.fatal("Feature importances calculation requires scikit-learn version >= 0.22")

    if fimp_file:
        if importances is False:
            gs.fatal('Output of feature importance requires the "f" flag to be set')
        if not os.path.exists(os.path.dirname(fimp_file)):
            gs.fatal("Directory for output file {} does not exist".format(fimp_file))

    # predictions file selected but no cross-validation scheme used
    if preds_file:
        if cv <= 1:
            gs.fatal(
                "Output of cross-validation predictions requires cross-validation cv > 1"
            )
        if not os.path.exists(os.path.dirname(preds_file)):
            gs.fatal("Directory for output file {} does not exist".format(preds_file))

    # define RasterStack -----------------------------------------------------------------------------------------------
    stack = RasterStack(group=group)

    if category_maps is not None:
        stack.categorical = category_maps

    # extract training data --------------------------------------------------------------------------------------------
    if load_training != "":
        X, y, cat, class_labels, group_id = load_training_data(load_training)

        if class_labels is not None:
            a = pd.DataFrame({"response": y, "labels": class_labels})
            a = a.drop_duplicates().values
            class_labels = {k: v for (k, v) in a}

    else:
        gs.message("Extracting training data")

        if group_raster != "":
            stack.append(group_raster)

        if training_map != "":
            X, y, cat = stack.extract_pixels(training_map)
            y = y.flatten()

            with RasterRow(training_map) as src:
                class_labels = {v: k for (k, v, m) in src.cats}

                if "" in class_labels.values():
                    class_labels = None

        elif training_points != "":
            X, y, cat = stack.extract_points(training_points, field)
            y = y.flatten()
            
            if y.dtype in (np.object_, np.object):
                from sklearn.preprocessing import LabelEncoder
                le = LabelEncoder()
                y = le.fit_transform(y)
                class_labels = {k: v for (k, v) in enumerate(le.classes_)}
            else:
                class_labels = None

        # take group id from last column and remove from predictors
        if group_raster != "":
            group_id = X[:, -1]
            X = np.delete(X, -1, axis=1)
            stack.drop(group_raster)
        else:
            group_id = None

        # check for labelled pixels and training data
        if y.shape[0] == 0 or X.shape[0] == 0:
            gs.fatal(
                "No training pixels or pixels in imagery group "
                "...check computational region"
            )

        from sklearn.utils import shuffle

        if group_id is None:
            X, y, cat = shuffle(X, y, cat, random_state=random_state)
        else:
            X, y, cat, group_id = shuffle(
                X, y, cat, group_id, random_state=random_state
            )

        if save_training != "":
            save_training_data(
                save_training, X, y, cat, class_labels, group_id, stack.names
            )

    # cross validation settings ----------------------------------------------------------------------------------------
    # inner resampling method (cv=2)
    from sklearn.model_selection import GridSearchCV, StratifiedKFold, GroupKFold, KFold

    if any(param_grid) is True:
        if group_id is None and mode == "classification":
            inner = StratifiedKFold(n_splits=2, random_state=random_state)
        elif group_id is None and mode == "regression":
            inner = KFold(n_splits=2, random_state=random_state)
        else:
            inner = GroupKFold(n_splits=2)
    else:
        inner = None

    # outer resampling method (cv=cv)
    if cv > 1:
        if group_id is None and mode == "classification":
            outer = StratifiedKFold(n_splits=cv, random_state=random_state)
        elif group_id is None and mode == "regression":
            outer = KFold(n_splits=cv, random_state=random_state)
        else:
            outer = GroupKFold(n_splits=cv)

    # modify estimators that take sample_weights -----------------------------------------------------------------------
    if balance is True:
        from sklearn.utils import compute_class_weight

        class_weights = compute_class_weight(class_weight="balanced", classes=(y), y=y)
        fit_params = {"sample_weight": class_weights}

    else:
        class_weights = None
        fit_params = {}

    # preprocessing ----------------------------------------------------------------------------------------------------
    from sklearn.pipeline import Pipeline
    from sklearn.compose import ColumnTransformer
    from sklearn.preprocessing import StandardScaler, OneHotEncoder

    # standardization
    if norm_data is True and category_maps is None:
        scaler = StandardScaler()
        trans = ColumnTransformer(
            remainder="passthrough",
            transformers=[("scaling", scaler, np.arange(0, stack.count))],
        )

    # one-hot encoding
    elif norm_data is False and category_maps is not None:
        enc = OneHotEncoder(handle_unknown="ignore", sparse=False)
        trans = ColumnTransformer(
            remainder="passthrough", transformers=[("onehot", enc, stack.categorical)]
        )

    # standardization and one-hot encoding
    elif norm_data is True and category_maps is not None:
        scaler = StandardScaler()
        enc = OneHotEncoder(handle_unknown="ignore", sparse=False)
        trans = ColumnTransformer(
            remainder="passthrough",
            transformers=[
                ("onehot", enc, stack.categorical),
                ("scaling", scaler, np.setxor1d(
                    range(stack.count), stack.categorical).astype('int')),
            ],
        )

    # combine transformers
    if norm_data is True or category_maps is not None:
        estimator = Pipeline([("preprocessing", trans), ("estimator", estimator)])
        param_grid = wrap_named_step(param_grid)
        fit_params = wrap_named_step(fit_params)

    if any(param_grid) is True:
        estimator = GridSearchCV(
            estimator=estimator,
            param_grid=param_grid,
            scoring=search_scorer,
            n_jobs=n_jobs,
            cv=inner,
        )

    # estimator training -----------------------------------------------------------------------------------------------
    gs.message(os.linesep)
    gs.message(("Fitting model using " + model_name))
    if balance is True and group_id is not None:
        estimator.fit(X, y, groups=group_id, **fit_params)
    elif balance is True and group_id is None:
        estimator.fit(X, y, **fit_params)
    else:
        estimator.fit(X, y)

    # message best hyperparameter setup and optionally save using pandas
    if any(param_grid) is True:
        gs.message(os.linesep)
        gs.message("Best parameters:")

        optimal_pars = [
            (k.replace("estimator__", "").replace("selection__", "") + " = " + str(v))
            for (k, v) in estimator.best_params_.items()
        ]

        for i in optimal_pars:
            gs.message(i)

        if param_file != "":
            param_df = pd.DataFrame(estimator.cv_results_)
            param_df.to_csv(param_file)

    # cross-validation -------------------------------------------------------------------------------------------------
    if cv > 1:
        from sklearn.metrics import classification_report
        from sklearn import metrics

        if (
            mode == "classification"
            and cv > np.histogram(y, bins=np.unique(y))[0].min()
        ):
            gs.message(os.linesep)
            gs.fatal(
                "Number of cv folds is greater than number of "
                "samples in some classes"
            )

        gs.message(os.linesep)
        gs.message("Cross validation global performance measures......:")

        if (
            mode == "classification"
            and len(np.unique(y)) == 2
            and all([0, 1] == np.unique(y))
        ):
            scoring["roc_auc"] = metrics.roc_auc_score

        from sklearn.model_selection import cross_val_predict

        preds = cross_val_predict(
            estimator, X, y, group_id, cv=outer, n_jobs=n_jobs, fit_params=fit_params
        )

        test_idx = [test for train, test in outer.split(X, y)]
        n_fold = np.zeros((0,))

        for fold in range(outer.get_n_splits()):
            n_fold = np.hstack((n_fold, np.repeat(fold, test_idx[fold].shape[0])))

        preds = {"y_pred": preds, "y_true": y, "cat": cat, "fold": n_fold}

        preds = pd.DataFrame(data=preds, columns=["y_pred", "y_true", "cat", "fold"])
        gs.message(os.linesep)
        gs.message("Global cross validation scores...")
        gs.message(os.linesep)
        gs.message("Metric \t Mean \t Error")

        for name, func in scoring.items():
            score_mean = (
                preds.groupby("fold")
                .apply(lambda x: func(x["y_true"], x["y_pred"]))
                .mean()
            )

            score_std = (
                preds.groupby("fold")
                .apply(lambda x: func(x["y_true"], x["y_pred"]))
                .std()
            )

            gs.message(
                name + "\t" + str(score_mean.round(3)) + "\t" + str(score_std.round(3))
            )

        if mode == "classification":
            gs.message(os.linesep)
            gs.message("Cross validation class performance measures......:")

            report_str = classification_report(
                y_true=preds["y_true"],
                y_pred=preds["y_pred"],
                sample_weight=class_weights,
                output_dict=False,
            )

            report = classification_report(
                y_true=preds["y_true"],
                y_pred=preds["y_pred"],
                sample_weight=class_weights,
                output_dict=True,
            )
            report = pd.DataFrame(report)

            gs.message(report_str)

            if classif_file != "":
                report.to_csv(classif_file, mode="w", index=True)

        # write cross-validation predictions to csv file
        if preds_file != "":
            preds.to_csv(preds_file, mode="w", index=False)
            text_file = open(preds_file + "t", "w")
            text_file.write('"Real", "Real", "integer", "integer"')
            text_file.close()

    # feature importances ----------------------------------------------------------------------------------------------
    if importances is True:
        from sklearn.inspection import permutation_importance

        fimp = permutation_importance(
            estimator,
            X,
            y,
            scoring=search_scorer,
            n_repeats=5,
            n_jobs=n_jobs,
            random_state=random_state,
        )

        feature_names = deepcopy(stack.names)
        feature_names = [i.split("@")[0] for i in feature_names]

        fimp = pd.DataFrame(
            {
                "feature": feature_names,
                "importance": fimp["importances_mean"],
                "std": fimp["importances_std"],
            }
        )

        gs.message(os.linesep)
        gs.message("Feature importances")
        gs.message("Feature" + "\t" + "Score")

        for index, row in fimp.iterrows():
            gs.message(
                row["feature"] + "\t" + str(row["importance"]) + "\t" + str(row["std"])
            )

        if fimp_file != "":
            fimp.to_csv(fimp_file, index=False)

    # save the fitted model
    import joblib

    joblib.dump((estimator, y, class_labels), model_save)
Ejemplo n.º 24
0
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
# 通過 torch.cuda.is_available() 的回傳值進行判斷是否有使用 GPU 的環境,如果有的話 device 就設為 "cuda",沒有的話就設為 "cpu"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

train_prefix = sys.argv[1]
no_prefix = sys.argv[2]
train_with_label = os.path.join(train_prefix, 'training_label.txt')
train_no_label = os.path.join(no_prefix, 'training_nolabel.txt')
w2v_path = './w2v_all.model'

print("loading data ...")
train_x, y = load_training_data(train_with_label)
train_x_no_label = load_training_data(train_no_label)

# Variables 
batch_size = 128
fix_embedding = True
epoch = 6
lr = 0.001
sen_len = 30

# 對 input 跟 labels 做預處理
preprocess = Preprocess(train_x, sen_len, w2v_path=w2v_path)
embedding = preprocess.make_embedding(load=True)
train_x = preprocess.sentence_word2idx()
y = preprocess.labels_to_tensor(y)
Ejemplo n.º 25
0
train_wo_filename = os.path.join(path_prefix, 'training_nolabel.txt')
testing_filename = os.path.join(path_prefix, 'testing_data.txt')


def train_word2vec(x):
    # training word to vector by word embedding
    model = word2vec.Word2Vec(x,
                              size=200,
                              window=5,
                              min_count=5,
                              workers=12,
                              iter=8,
                              sg=1)
    return model


if __name__ == "__main__":
    print("loading training data ...")
    train_x, y = load_training_data(train_w_filename)
    train_x_no_label = load_training_data(train_wo_filename)

    print("loading testing data ...")
    test_x = load_testing_data(testing_filename)

    #embedding_x = train_x + train_x_no_label + test_x #1578614
    embedding_x = train_x + test_x  #400000
    print("embedding 2d list ... lenght=", len(embedding_x))
    model = train_word2vec(embedding_x)

    print("saving model ...")
    model.save(os.path.join('model/w2v_200.model'))
Ejemplo n.º 26
0
import matplotlib.pyplot as plt
import utils as ut
import svm
import sys
import logging
import warnings

print(__doc__)

###############################################################################
# Building SVC from database

FACE_DIM = (50, 50)  # h = 50, w = 50

# Load training data from face_profiles/
face_profile_data, face_profile_name_index, face_profile_names = ut.load_training_data(
    "../face_profiles/")

print "\n", face_profile_name_index.shape[0], " samples from ", len(
    face_profile_names), " people are loaded"

# Build the classifier
clf, pca = svm.build_SVC(face_profile_data, face_profile_name_index, FACE_DIM)

###############################################################################
# Facial Recognition In Live Tracking

DISPLAY_FACE_DIM = (200, 200)  # the displayed video stream screen dimention
SKIP_FRAME = 2  # the fixed skip frame
frame_skip_rate = 0  # skip SKIP_FRAME frames every other frame
SCALE_FACTOR = 4  # used to resize the captured frame for face detection for faster processing speed
face_cascade = cv2.CascadeClassifier(
    def build(self):

        # load training df
        self.tdf = utils.load_training_data(self.dataloc, stage=2)

        # drop missing image
        drop_idx = [
            i for i, row in self.tdf['filename'].iteritems()
            if fnmatch.fnmatch(row, '*ID_33fcf4d99*')
        ]
        self.tdf = self.tdf.drop(drop_idx)

        # set up training fraction
        ## train and validate dataframes
        shuff = self.tdf.sample(frac=self.training_fraction,
                                random_state=self.random_state)
        self.train_df = shuff.iloc[:int(0.90 * len(shuff))]
        self.validate_df = shuff.iloc[int(0.90 * len(shuff)):]
        len(shuff), len(self.train_df), len(self.validate_df)

        # set up generators
        self.categories = utils.define_categories(self.train_df)

        self.train_generator = utils.Three_Channel_Generator(
            self.train_df.reset_index(),
            ycols=self.categories,
            desired_size=self.img_size,
            batch_size=self.batch_size,
            random_transform=self.random_transform,
            rgb=True)

        self.validate_generator = utils.Three_Channel_Generator(
            self.validate_df.reset_index(),
            ycols=self.categories,
            desired_size=self.img_size,
            batch_size=self.batch_size,
            random_transform=False,
            rgb=True)

        # load model
        self.model = models(self.model_name,
                            input_image_size=self.img_size,
                            number_of_output_categories=len(self.categories))

        if self.weights_path is not None:
            self.model.load_weights(self.weights_path)

        # setup callbacks
        earlystop = EarlyStopping(patience=10)

        learning_rate_reduction = ReduceLROnPlateau(
            monitor='categorical_accuracy',
            patience=2,
            verbose=1,
            factor=0.5,
            min_lr=0.00001)

        checkpoint_name = "model_weights_vgg19_{}.h5".format(self.datestamp)
        checkpoint = ModelCheckpoint(checkpoint_name,
                                     monitor='val_acc',
                                     verbose=0,
                                     save_best_only=True,
                                     save_weights_only=True,
                                     mode='auto')

        self.callbacks = [earlystop, checkpoint]
Ejemplo n.º 28
0
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
import numpy as np
import os
import sys
import music21
from utils import create_RNN_model, load_dictionaries, load_training_data, music_to_network_input

seq_length = 32
notes, durations, offsets = load_training_data()
network_input, network_output = music_to_network_input(notes, durations,
                                                       offsets, seq_length)

notes_to_int, int_to_notes, duration_to_int, int_to_duration, offset_to_int, int_to_offset = load_dictionaries(
)
notes_num = len(notes_to_int)
dur_num = len(duration_to_int)
off_num = len(offset_to_int)

model = create_RNN_model(notes_num,
                         dur_num,
                         off_num,
                         embedding_size=300,
                         rnn_units=256,
                         add_attention=False)

model.summary()

weights_folder = os.path.join(os.getcwd(), 'weights')
Ejemplo n.º 29
0
import torch.nn as nn
import utils
import cv2
import os
import time
from PIL import Image

from torch.autograd import Variable
from torch.utils.data import DataLoader, TensorDataset
import torch.optim as optim
from torchsummary import summary

cuda = True if torch.cuda.is_available() else False
device = torch.device("cuda" if cuda else "cpu")

Data_Train = utils.load_training_data("./faces")
Data_Train = [np.swapaxes(image, 0, 2) for image in Data_Train]
Data_Train = [np.swapaxes(image, 1, 2) for image in Data_Train]
Data_Train = torch.stack([torch.Tensor(i) for i in Data_Train])
Data_Train = TensorDataset(Data_Train)


        
class Generator(nn.Module):
    def __init__(self, noise_dim=100, input_size=65):
        super().__init__()
        self.noise_dim = noise_dim
        self.input_size = input_size
        
        
        
Ejemplo n.º 30
0
# simple SGD (later there will be step decay added to the train function)
sgd = optimizers.SGD(lr=args.base_lr, decay=0, momentum=0.9, nesterov=True)
# compile
model.compile(loss=args.loss, optimizer=sgd, metrics=[args.loss])
print model.summary()  # print a summary

if args.cnn_weights is not None:  # load model weights if specified
    model.load_weights(args.cnn_weights, by_name=True)

###############################################################################
# load / prepare data
data = load_training_data(args.survey,
                          args.targets,
                          args.colors,
                          args.split,
                          args.test_size,
                          args.train_size,
                          args.aux_inputs,
                          seed=args.seed,
                          drop_lowq=args.drop_lowq)
X_train, X_test, y_train, y_test = data

if args.scale_targets:  # scale targets to std == 1
    scales = y_train.std(axis=0)
    y_train /= scales
    y_test /= scales

# create data generators for on the fly augmentations
dg_train = DataGenerator(X_train,
                         y_train,
                         batch_size=args.batch_size,
Ejemplo n.º 31
0
                'variation3': variations.variation3()}
    model = versions[args.version]

    if not args.iscontinue:
        try:
            os.system("rm -r {}".format(working_dir))
        except:
            pass

    if not os.path.isdir(model.working_dir):
        os.mkdir(model.working_dir)

    model.create_train_data()
    model.create_test_data()

    train_data_path = os.path.join(model.working_dir, 'train_data.txt')
    training_data = utils.load_training_data(model.working_dir,
                                             train_data_path)

    # create lexicon
    utils.create_lexicon_file(model.working_dir, train_data_path)

    # create wfst
    utils.create_wfst(training_data, model.working_dir)

    # run the evaluation with different configurations
    utils.find_best_config(training_data,
                           model.working_dir,
                           ngrams=ngrams,
                           smooth_methods=smooth_methods)