def main():
    # Load net
    cnn = CNN()
    loss_func = nn.MultiLabelSoftMarginLoss()
    optimizer = optim.Adam(cnn.parameters(), lr=learning_rate)
    if torch.cuda.is_available():
        cnn.cuda()
        loss_func.cuda()

    # Load data
    train_dataloader = dataset.get_train_data_loader()
    test_dataloader = dataset.get_test_data_loader()

    # Train model
    for epoch in range(num_epochs):
        cnn.train()
        for i, (images, labels) in enumerate(train_dataloader):
            images = Variable(images)
            labels = Variable(labels.long())
            if torch.cuda.is_available():
                images = images.cuda()
                labels = labels.cuda()
            predict_labels = cnn(images)
            loss = loss_func(predict_labels, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if (i + 1) % 100 == 0:
                print("epoch:", epoch, "step:", i, "loss:", loss.item())

        # Save and test model
        if (epoch + 1) % 10 == 0:
            filename = "model" + str(epoch + 1) + ".pkl"
            torch.save(cnn.state_dict(), filename)
            cnn.eval()
            correct = 0
            total = 0
            for (image, label) in test_dataloader:
                vimage = Variable(image)
                if torch.cuda.is_available():
                    vimage = vimage.cuda()
                output = cnn(vimage)
                predict_label = ""
                for k in range(4):
                    predict_label += config.CHAR_SET[np.argmax(
                        output[0, k * config.CHAR_SET_LEN:(k + 1) *
                               config.CHAR_SET_LEN].data.cpu().numpy())]
                true_label = one_hot.vec2text(label.numpy()[0])
                total += label.size(0)
                if predict_label == true_label:
                    correct += 1
                if total % 200 == 0:
                    print(
                        'Test Accuracy of the model on the %d test images: %f %%'
                        % (total, 100 * correct / total))
            print('Test Accuracy of the model on the %d test images: %f %%' %
                  (total, 100 * correct / total))
            print("save and test model...")
    torch.save(cnn.state_dict(), "./model.pkl")  # current is model.pkl
    print("save last model")
Exemple #2
0
def train(model_name='model.pkl'):
    cnn = CNN()
    cnn.train()
    print('init net')
    criterion = nn.MultiLabelSoftMarginLoss()
    optimizer = torch.optim.Adam(cnn.parameters(),
                                 lr=setting.TRAIN_LEARNING_RATE)

    # Train the Model
    train_dataloader = dataset.get_train_data_loader()
    for epoch in range(setting.TRAIN_NUM_EPOCHS):
        for i, (images, labels) in enumerate(train_dataloader):
            images = Variable(images)
            labels = Variable(labels.float())
            predict_labels = cnn(images)
            loss = criterion(predict_labels, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        print('epoch: % -3s loss: %s' % (epoch, loss.item()))
    torch.save(cnn.state_dict(), model_name)  # current is model.pkl
    print('save last model')
Exemple #3
0
    # loop until all data are read

        mf.input(network,n_tfiles,n_vfiles)

        with tf.device('/gpu:0'):
            # restore()
            if(iter==0):
                # Define the train computation graph
                network.define_train_operations()

            # Train the network
            sess = tf.Session(config=tf.ConfigProto(allow_soft_placement = True)) # session with log about gpu exec
            #sess= tf.Session()
            try:
                print(iter)
                network.train(sess,iter)
                iter += 1
                flag = 0
                # save()
            except KeyboardInterrupt:
                print()
                flag = 1
            finally:
                flag = 1
                sess.close()
except KeyboardInterrupt:
    flag=1
except Exception as e:
    flag=1
    # template = "An exception of type {0} occurred. Arguments:\n{1!r}"
    # message = template.format(type(e).__name__, e.args)
def train():
    """
    Performs training and evaluation of MLP cnn.
    NOTE: You should the cnn on the whole test set each eval_freq iterations.
    """
    # YOUR TRAINING CODE GOES HERE
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    train_data = datasets.CIFAR10('data',
                                  train=True,
                                  download=True,
                                  transform=transform)
    test_data = datasets.CIFAR10('data',
                                 train=False,
                                 download=True,
                                 transform=transform)
    train_on_gpu = torch.cuda.is_available()
    num_train = len(train_data)
    train_loader = torch.utils.data.DataLoader(train_data,
                                               batch_size=FLAGS.batch_size,
                                               shuffle=True,
                                               num_workers=0)
    test_loader = torch.utils.data.DataLoader(test_data,
                                              batch_size=FLAGS.batch_size,
                                              shuffle=False,
                                              num_workers=0)
    classes = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]
    cnn = CNN(3, 10)
    if train_on_gpu:
        cnn.cuda()
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(cnn.parameters(), lr=FLAGS.learning_rate)

    for epoch in range(1, FLAGS.max_steps):
        class_correct = list(0. for i in range(10))
        class_total = list(0. for i in range(10))
        train_loss = 0.0
        test_loss = 0.0
        cnn.train()
        for data, target in train_loader:
            if train_on_gpu:
                data, target = data.cuda(), target.cuda()
            optimizer.zero_grad()
            output = cnn(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
            _, pred = torch.max(output, 1)
            correct_tensor = pred.eq(target.data.view_as(pred))
            train_loss += loss.item() * data.size(0)
            correct = np.squeeze(
                correct_tensor.numpy()) if not train_on_gpu else np.squeeze(
                    correct_tensor.cpu().numpy())
            for i in range(len(target.data)):
                label = target.data[i]
                class_correct[label] += correct[i].item()
                class_total[label] += 1

        if epoch % FLAGS.eval_freq == 0:
            test_correct = list(0. for i in range(10))
            test_total = list(0. for i in range(10))
            cnn.eval()
            for data, target in test_loader:
                if train_on_gpu:
                    data, target = data.cuda(), target.cuda()
                output = cnn(data)
                _, pred = torch.max(output, 1)
                correct_tensor = pred.eq(target.data.view_as(pred))
                correct = np.squeeze(correct_tensor.numpy()
                                     ) if not train_on_gpu else np.squeeze(
                                         correct_tensor.cpu().numpy())
                loss = criterion(output, target)
                test_loss += loss.item() * data.size(0)
                for i in range(len(target.data)):
                    label = target.data[i]
                    test_correct[label] += correct[i].item()
                    test_total[label] += 1

            train_loss = train_loss / len(train_loader.dataset)
            test_loss = test_loss / len(test_loader.dataset)
            plot_epoch.append(epoch)
            plot_train_loss.append(train_loss)
            plot_test_loss.append(test_loss)
            print(
                'Epoch: {} \tTraining Loss: {:.6f} \tTest Loss: {:.6f}'.format(
                    epoch, train_loss, test_loss))

            percent_train = accuracy(class_correct, class_total) * 100
            percent_test = accuracy(test_correct, test_total) * 100
            plot_train_accuracy.append(percent_train)
            plot_test_accuracy.append(percent_test)
            print('train accuracy: ', percent_train, 'test accuracy: ',
                  percent_test)

    fig1 = plt.subplot(2, 1, 1)
    fig2 = plt.subplot(2, 1, 2)
    fig1.plot(plot_epoch,
              plot_train_accuracy,
              c='red',
              label='training data accuracy')
    fig1.plot(plot_epoch,
              plot_test_accuracy,
              c='blue',
              label='test data accuracy')
    fig1.legend()
    fig2.plot(plot_epoch, plot_train_loss, c='green', label='train CE loss')
    fig2.plot(plot_epoch, plot_test_loss, c='yellow', label='test CE loss')
    fig2.legend()
    plt.show()