예제 #1
0
def test(model, numerical_inputs, feature_inputs, char_inputs,
         actor_char_inputs, test_labels):
    max_itr = len(feature_inputs) // model.batch_size
    accuracies = []
    for i in range(max_itr):
        numerical_input_batch = preprocess.get_batch(numerical_inputs,
                                                     i * model.batch_size,
                                                     model.batch_size)
        feature_input_batch = preprocess.get_batch(feature_inputs,
                                                   i * model.batch_size,
                                                   model.batch_size)
        char_input_batch = preprocess.get_batch(char_inputs,
                                                i * model.batch_size,
                                                model.batch_size)
        actor_char_input_batch = preprocess.get_batch(actor_char_inputs,
                                                      i * model.batch_size,
                                                      model.batch_size)
        label_batch = preprocess.get_batch(test_labels, i * model.batch_size,
                                           model.batch_size)
        logits = model.call(numerical_input_batch,
                            tf.convert_to_tensor(feature_input_batch),
                            char_input_batch,
                            actor_char_input_batch,
                            is_training=False)
        accuracies.append(model.accuracy(logits, label_batch))
    print("Testing accuracy: ", tf.reduce_mean(accuracies))
예제 #2
0
def trainEpochs(encoder,
                decoder,
                n_epochs,
                print_every=1000,
                learning_rate=0.01):

    start = time.time()
    print_loss_total = 0  # Reset every print_every

    encoder_optimizer = optim.RMSprop(encoder.parameters(), lr=learning_rate)
    decoder_optimizer = optim.RMSprop(decoder.parameters(), lr=learning_rate)
    criterion = nn.NLLLoss().cuda()

    for epoch in range(1, n_epochs + 1):

        training_batch = get_batch(fra, eng)
        input_variable = Variable(torch.LongTensor(training_batch[0])).cuda()
        target_variable = Variable(torch.LongTensor(training_batch[1])).cuda()
        loss = train(input_variable, target_variable, encoder, decoder,
                     encoder_optimizer, decoder_optimizer, criterion)

        print_loss_total += loss

        if epoch % print_every == 0:
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (%d %d%%) %.4f' %
                  (timeSince(start, epoch / n_epochs), epoch,
                   epoch / n_epochs * 100, print_loss_avg))
예제 #3
0
def train(model, train_inputs, train_labels):
    '''
    Trains the model on all of the inputs and labels for one epoch. You should shuffle your inputs 
    and labels - ensure that they are shuffled in the same order using tf.gather.
    To increase accuracy, you may want to use tf.image.random_flip_left_right on your
    inputs before doing the forward pass. You should batch your inputs.
    :param model: the initialized model to use for the forward pass and backward pass
    :param train_inputs: train inputs (all inputs to use for training), 
    shape (num_inputs, width, height, num_channels)
    :param train_labels: train labels (all labels to use for training), 
    shape (num_labels, num_classes)
    :return: Optionally list of losses per batch to use for visualize_loss
    '''
    num_ex, _, _, _ = train_inputs.shape
    num_batches = num_ex // model.batch_size

    for i in range(num_batches):
        batch_inputs, batch_labels = get_batch(train_inputs, train_labels,
                                               model.batch_size,
                                               i * model.batch_size)

        with tf.GradientTape() as tape:
            predictions = model.call(batch_inputs)
            loss = model.loss(predictions, batch_labels)

        model.loss_list.append(loss)
        gradients = tape.gradient(loss, model.trainable_variables)
        model.optimizer.apply_gradients(
            zip(gradients, model.trainable_variables))

    print(model.accuracy(predictions, batch_labels))
    return model.loss_list
예제 #4
0
def test(model, test_inputs, test_labels):
    """
    Tests the model on the test inputs and labels. You should NOT randomly 
    flip images or do any extra preprocessing.
    :param test_inputs: test data (all images to be tested), 
    shape (num_inputs, width, height, num_channels)
    :param test_labels: test labels (all corresponding labels),
    shape (num_labels, num_classes)
    :return: test accuracy - this should be the average accuracy across
    all batches
    """
    num_ex, _, _, _ = test_inputs.shape
    num_batches = num_ex // model.batch_size

    accur = 0
    for i in range(num_batches):
        batch_inputs, batch_labels = get_batch(test_inputs, test_labels,
                                               model.batch_size,
                                               i * model.batch_size)

        predictions = model.call(batch_inputs, is_testing=True)
        a = model.accuracy(predictions, batch_labels)
        print(a)
        accur += a

    return accur / num_batches
예제 #5
0
def test(model, encoder, epoch, num_batches):
    if not os.path.exists("./output/{}".format(epoch)):
        os.makedirs("./output/{}".format(epoch))

    if not os.path.exists("./output/{}/0".format(epoch)):
        os.makedirs("./output/{}/0".format(epoch))
    l_imgs, ab_imgs = get_batch(0)
    logits = model.call(l_imgs)
    encoder.decode(logits, l_imgs, ab_imgs, epoch, 0)

    batch_id = np.random.randint(1, num_batches - 1)
    if not os.path.exists("./output/{}/{}".format(epoch, batch_id)):
        os.makedirs("./output/{}/{}".format(epoch, batch_id))
    l_imgs, ab_imgs = get_batch(batch_id)
    logits = model.call(l_imgs)
    encoder.decode(logits, l_imgs, ab_imgs, epoch, batch_id)
예제 #6
0
def test(model, test_boxes, test_size):
    order = np.arange(test_size)
    np.random.shuffle(order)
    batch_inputs, _, order = get_batch('test', model.S, model.B, model.C, 448,
                                       test_boxes, model.batch_size, order)
    for i in range(model.batch_size):
        input = batch_inputs[i]
        predictions = model(tf.reshape(input, [1, 448, 448, 3]))
        img = add_predictions_to_inputs(
            input,
            tf.reshape(predictions, [model.S, model.S, model.B * 5 + model.C]))
        imwrite('./output/{}.png'.format(i), img)
예제 #7
0
def train(model, numerical_inputs, feature_inputs, char_inputs,
          actor_char_inputs, train_labels):
    max_itr = len(feature_inputs) // model.batch_size

    losses = []
    for i in range(max_itr):
        print("Batch " + str(i + 1) + " / " + str(max_itr))
        numerical_input_batch = preprocess.get_batch(numerical_inputs,
                                                     i * model.batch_size,
                                                     model.batch_size)
        feature_input_batch = preprocess.get_batch(feature_inputs,
                                                   i * model.batch_size,
                                                   model.batch_size)
        char_input_batch = preprocess.get_batch(char_inputs,
                                                i * model.batch_size,
                                                model.batch_size)
        actor_char_input_batch = preprocess.get_batch(actor_char_inputs,
                                                      i * model.batch_size,
                                                      model.batch_size)
        label_batch = preprocess.get_batch(train_labels, i * model.batch_size,
                                           model.batch_size)
        with tf.GradientTape() as tape:
            logits = model.call(numerical_input_batch,
                                tf.convert_to_tensor(feature_input_batch),
                                char_input_batch,
                                actor_char_input_batch,
                                is_training=None)
            loss = model.loss(logits, label_batch)
            losses.append(loss)
            print("Batch loss: {}".format(loss))
            print("Batch accuracy: {}".format(
                model.accuracy(logits, label_batch)))
        gradients = tape.gradient(loss, model.trainable_variables)
        model.optimizer.apply_gradients(
            zip(gradients, model.trainable_variables))
    return losses
예제 #8
0
def train(model, encoder, manager, num_batches):
    best_loss = float("inf")
    for batch_idx in range(num_batches):
        print("batch {} out of {}".format(batch_idx, num_batches))
        l_imgs, ab_imgs = get_batch(batch_idx)
        labels = get_batch_labels(ab_imgs, encoder)
        with tf.GradientTape() as tape:
            logits = model.call(l_imgs)
            loss = model.loss_function(logits, labels)

        print("loss: {}".format(loss))

        if epoch > 100 and loss < best_loss:
            manager.save()
            best_loss = loss

        gradients = tape.gradient(loss, model.trainable_variables)
        model.optimizer.apply_gradients(
            zip(gradients, model.trainable_variables))
예제 #9
0
def train(model, train_boxes, train_size):
    order = np.arange(train_size)
    np.random.shuffle(order)
    batch_num = 0
    while True:
        if len(order) < model.batch_size:
            break
        batch_inputs_list, batch_labels_list, order = get_batch(
            'train', model.S, model.B, model.C, 448, train_boxes,
            model.batch_size, order)
        batch_inputs = tf.stack(batch_inputs_list)
        batch_labels = tf.stack(batch_labels_list)

        with tf.GradientTape() as tape:
            predictions = model(batch_inputs)
            loss = model.loss(predictions, batch_labels)
            print("Loss after batch {}: {}".format(batch_num, loss))

        gradients = tape.gradient(loss, model.trainable_variables)
        model.optimizer.apply_gradients(
            zip(gradients, model.trainable_variables))
        batch_num += 1
    print()
예제 #10
0
def main():
    in_z = 0
    volpath = os.path.join(args.datapath, 'train')
    segpath = volpath

    batch_size = args.batch_size
    orig_dim = 256
    sqr = transforms.Square()
    aff = transforms.Affine()
    crop = transforms.RandomCrop(224)
    scale = transforms.Scale(orig_dim)
    rotate = transforms.Rotate(0.5, 30)
    noise = transforms.Noise(0.02)
    flip = transforms.Flip()
    transform_plan = [sqr, scale, aff, rotate, crop, flip, noise]
    lr = 1e-4
    series_names = ['Mag']
    seg_series_names = ['AV']

    model = models.Net23(2)

    model.cuda()
    optimizer = optim.RMSprop(model.parameters(), lr=lr)

    out_z, center_crop_sz = utils.get_out_size(orig_dim, in_z,\
            transform_plan, model)

    t0 = time.time()

    counter = 0
    print_interval = args.log_interval
    model.train()
    for i in range(200000000000000000000000000000000):
        weight = torch.FloatTensor([0.2, 0.8]).cuda()
        vol, seg, inds = preprocess.get_batch(volpath, segpath, batch_size, in_z,\
                out_z, center_crop_sz, series_names, seg_series_names,\
                transform_plan, 8, nrrd=True)
        vol = torch.unsqueeze(vol, 1)
        vol = Variable(vol).cuda()
        seg = Variable(seg).cuda()

        out = model(vol).squeeze()

        loss = F.cross_entropy(out, seg, weight=weight)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        counter += 1

        sys.stdout.write('\r{:.2f}%'.format(counter * batch_size /
                                            print_interval))
        sys.stdout.flush()

        if counter * batch_size >= print_interval and i > 0:

            seg_hot = utils.get_hot(seg.data.cpu().numpy(), out.size()[-3])
            seg_hot = np.transpose(seg_hot, axes=[1, 0, 2, 3])
            out_hot = np.argmax(out.data.cpu().numpy(), axis=1)
            out_hot = utils.get_hot(out_hot, out.size()[-3])
            out_hot = np.transpose(out_hot, axes=[1, 0, 2, 3])

            dce2 = utils.dice(seg_hot[:, 1:], out_hot[:, 1:])

            vol_ind = utils.get_liver(seg)
            v = vol.data.cpu().numpy()[vol_ind].squeeze()
            real_seg = seg[vol_ind].data.cpu().numpy()
            out_plt = out.data.cpu().numpy()[vol_ind]
            out_plt = np.argmax(out_plt, axis=0)
            fake_seg = out_plt
            masked_real = utils.makeMask(v, real_seg, out.size()[-3], 0.5)
            masked_fake = utils.makeMask(v, fake_seg, out.size()[-3], 0.5)

            fig = plt.figure(1)
            fig.suptitle('Volume {} ; Dice = {:.2f}'.format(\
                    inds[vol_ind],dce2))
            v = fig.add_subplot(1, 2, 1)
            v.set_title('real')
            plt.imshow(masked_real)
            sreal = fig.add_subplot(1, 2, 2)
            sreal.set_title('fake')
            plt.imshow(masked_fake)
            outfile = os.path.join(args.datapath, 'out.png')
            plt.savefig(outfile, dpi=200)
            plt.clf()

            print(('\rIteration {}: Block completed in {:.2f} sec ; Loss = {:.2f}')\
                    .format(i*batch_size,time.time()-t0, loss.data.cpu()[0]))
            checkpoint_file = os.path.join(args.datapath,\
                    'model_checkpoint.pth')
            torch.save(model.state_dict(), checkpoint_file)
            counter = 0

            t0 = time.time()
예제 #11
0
    'MS': '~/Desktop/user_ml/michelle_song.1.csv',
    'TH': '~/Desktop/user_ml/tony_hsieh.1.csv',
    'Y': '~/Desktop/user_ml/yiyi_lyu.1.csv',
    'YF': '~/Desktop/user_ml/yvonne_feng.1.csv',
    #'K': '~/Desktop/user_ml/kevin_chiu.1.csv',
}

# file path of training & testing data
regular_data_file = data_files[target_name]
compare_data_files = data_files.copy()
del compare_data_files[target_name]

save_dir = "save_{}/".format(dt.datetime.now().strftime("%m%d%H%M"))

# get 'samples' train_data and 'samples' test_data and 'samples' validation data
data = preprocess.get_batch(regular_data_file, samples + samples + samples,
                            time_steps, step_dims)
train_data = data[:samples]
test_data = data[samples:samples * 2]
validation_data = data[samples * 2:]

# get one data for other people
cmp_data = {}
for name, file in compare_data_files.items():
    cmp_data[name] = preprocess.get_batch(file, samples, time_steps, step_dims)

##### Defining graph #####
# TODO: use shape [None, time_steps, step_dims]
seq_input = tf.placeholder(tf.float32, [1, time_steps, step_dims])

encoder_inputs = tf.unstack(seq_input)
print(len(encoder_inputs),
예제 #12
0
model.cuda()

optimizer = optim.RMSprop(model.parameters(), lr=lr)

out_z, center_crop_sz = utils.get_out_size(original_size, 0,\
        transform_plan, model)

t0 = time.time()

counter = 0
print_interval = 200
model.train()
weight = torch.FloatTensor([0.1, 0.45, 0.45]).cuda()
for i in range(num_steps):
    vol, seg, inds = preprocess.get_batch(volpath, segpath, batch_size, 0,\
            out_z, center_crop_sz, series_names, seg_series_names,\
            transform_plan, 8, nrrd=True)
    vol = torch.unsqueeze(vol, 1)
    vol = Variable(vol).cuda()
    seg = Variable(seg).cuda().long()

    out = model(vol).squeeze()

    loss = F.cross_entropy(out, seg, weight=weight)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    counter += 1

    sys.stdout.write('\r{:.2f}%'.format(counter * batch_size / print_interval))
예제 #13
0
파일: main.py 프로젝트: curme/TinyZoo
    feed_dict[feed['seq_len']] = [39] * size
    return feed_dict


if __name__ == '__main__':

    feed, cost, train_step, decoded, ler = network(len(id_char_map) + 1)

    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())

        for i in range(100000):

            size = 10
            batch = get_batch(size)
            feed_dict = assemble_feed(feed, size, batch)
            sess.run(train_step, feed_dict=feed_dict)

            if i % 10 == 9:

                size = 10
                batch = get_batch(size)
                feed_dict = assemble_feed(feed, size, batch)

                cost_result, error, result = sess.run([cost, ler, decoded],
                                                      feed_dict=feed_dict)
                temp = lambda x: '_' if x == len(id_char_map) else id_char_map[
                    x]
                str_raw = [
                    ''.join([temp(i) for i in seq])