コード例 #1
0
def mnist_testing(mnist_net, batch_size, model_type="mlprelu"):
    # Test
    correct = 0
    total = 0
    device = utils.training_device()
    _, testLoader = utils.dataLoader(batch_size=batch_size)
    with torch.no_grad():
        for data in testLoader:
            inputs, labels = data[0].to(device), data[1].to(device)

            if model_type != "cnn":  # need to change shape
                inputs = inputs.view(inputs.shape[0], -1)

            # modify if you change MNIST layers structure
            # _, _, outputs = mnist_net(inputs)
            repre = list(mnist_net(inputs))
            outputs = repre[-1]

            _, predicted = torch.max(outputs.data,
                                     1)  # 找出分數最高的對應channel,即 top-1
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

        print(
            'Accuracy of the MNIST network on the 10000 test images: %d %%\n' %
            (100 * correct / total))

    return 100 * correct / total
コード例 #2
0
def train(face_path, noface_path, width=20, height=20, stride=1, increment=1):
    _faces, _nofaces = dataLoader(face_path,
                                  noface_path,
                                  width=width,
                                  height=height)
    print("faces:", _faces.shape, "nofaces:", _nofaces.shape)
    haar_features = generateHaarFeatures(width=width,
                                         height=height,
                                         stride=stride,
                                         increment=increment)
    print("haar features length: ", len(haar_features))
    faces = integralImage(_faces)
    nofaces = integralImage(_nofaces)
    square_faces = integralImage(_faces * _faces)
    square_nofaces = integralImage(_nofaces * _nofaces)

    process_num = cpu_count() * config.PerCPUProcessNum
    pool_thread = Pool(processes=process_num)

    print("Start training cascade classifier")
    cascade = cascadeClassifier(faces, nofaces, square_faces, square_nofaces,
                                haar_features, pool_thread)

    with open('haar.pickle', 'wb') as fw:
        pickle.dump(cascade, fw)

    try:
        with open('cascade_2th_dataset.json', 'w') as fw:
            json.dump(cascade, fw)
    except:
        for i, casc in enumerate(cascade):
            classifiers, alphas, threshold = casc
            for j, clas in enumerate(classifiers):
                feature, polarity, theta, error_m = clas
                print(i, j, feature, polarity, theta, error_m)
    print("Training end")
コード例 #3
0
ファイル: train.py プロジェクト: zymITsky/NLPTK
def train():
    #initial config
    config = Config()
    if config.init_from:
        ckpt = tf.train.get_checkpoint_state(config.init_from)
        assert ckpt, 'No Checketpoint Found'
        assert ckpt.model_checkpoint_path, 'No model path found in checkpoint'
        #with open(os.path.join(config.save_dir,'config.pkl'),'r')as rf:
        #    config = cPickle.load(rf)
    else:
        if not os.path.isdir(config.save_dir):
            os.makedirs(config.save_dir)
        with open(os.path.join(config.save_dir, 'config.pkl'), 'wb') as wf:
            cPickle.dump(config, wf)
        if not os.path.isdir(config.log_dir):
            os.makedirs(config.log_dir)

    dataloader = dataLoader(config.batch_size,config.vocab_size,config.data_dir,\
            config.seq_length,config.vali_rate,trainable = False)
    #config.vocab_size = dataloader.vocab_size
    gpu_option = tf.GPUOptions(allow_growth=True)
    sessconfig = tf.ConfigProto(gpu_options=gpu_option)
    with tf.Session(config=sessconfig) as sess:
        initializer = tf.random_uniform_initializer(-1 * config.init_scale,
                                                    1 * config.init_scale)
        with tf.variable_scope('model', reuse=None, initializer=initializer):
            model = LstmModel(config)

        summaries = tf.summary.merge_all()
        writer = tf.summary.FileWriter(
            os.path.join(config.log_dir, time.strftime("%Y-%m-%d-%H-%M-%S")))
        writer.add_graph(sess.graph)
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver(tf.global_variables())
        if config.init_from:
            print 'load model'
            saver.restore(sess, ckpt.model_checkpoint_path)
        #train
        for e in range(config.num_epoch):
            lr_decay = config.lr_decay**max(e - config.max_decay_epoch, 0.0)
            model.assign_new_lr(sess, config.lr * lr_decay)
            num_batches = dataloader.train_num
            for i, (x, y, mask) in enumerate(dataloader.get_batches('train')):
                start = time.time()
                feed = {
                    model.input_data: x,
                    model.targets: y,
                    model.mask: mask
                }
                state = sess.run(model._initial_state)
                for j, (c, h) in enumerate(model._initial_state):
                    feed[c] = state[j].c
                    feed[h] = state[j].h
                loss, acc, summ, _ = sess.run(
                    [model.cost, model.accuracy, summaries, model.train_op],
                    feed)
                #print len(midoput)
                #print midoput[0].shape
                writer.add_summary(summ, e * config.batch_size + i)
                end = time.time()
                print("{}/{} (epoch {}), train_loss={:.5f},acc={:.5f},time/batch ={:.4f}"\
                        .format(e * num_batches + i,\
                        config.num_epoch*num_batches,e,loss, acc,end - start))
                if (config.num_epoch*num_batches+i)%config.check_point_every==0\
                        or (e==config.num_epoch-1 and i ==num_batches-1):
                    checkpoint_path = os.path.join(config.save_dir,
                                                   'model.ckpt')
                    saver.save(sess,
                               checkpoint_path,
                               global_step=e * num_batches + i)
            validation(dataloader, sess, model)
        sample(dataloader, sess, model)
コード例 #4
0
# hyperparameters
lr = 0.0001
n_epochs = 20
batch_size = 32

# define model from pytorch
model = torchvision.models.resnet18(pretrained=True)

# a model will be returned with num_classes for the classification
# only trainable parameters are the last layer
model = prepareModel(model, num_classes)

# loss function
criterion = nn.CrossEntropyLoss()

# optimizer
optimizer = optim.Adam(model.parameters(), lr=lr)

# batch the data
loaders = dataLoader(model, batch_size)

model = train(n_epochs, loaders, model, optimizer, criterion, 
              save_path='best.pth.tar')

test(loaders['test'], model, criterion)

# create the dict whose keys are indexes and values are class names
idx_to_class = {val:key for key, val in loaders['train'].dataset.class_to_idx.items()}

# predict an example image
predict('data/nevus.jpg', model, idx_to_class)
コード例 #5
0
def evaluate_one_file(filename):
    # evaluate on one file pair
    data = dataLoader(filename)
    imgL = data.imgL
    pc = data.pc
    print("Processing data " + filename + "...\n")

    print("Upsampling(accelerated) begins...")
    start_acc = time.time()
    disp_lidar = bf_vanilla_accelerated(imgL, pc)
    end_acc = time.time()
    elapse_acc = end_acc - start_acc
    print("Upsampling(accelerated) on raw points takes " + str(elapse_acc) +
          " seconds...\n")

    print("Refinement begins...")
    start_refine = time.time()
    edge_map, disp_bf = measure_dispersion(imgL, pc)
    end_refine = time.time()
    elapse_refine = end_refine - start_refine
    print("Refinement takes " + str(elapse_refine) + " seconds...\n")

    disp_psmnet = cv2.imread("../data/prediction/" + filename + ".png",
                             -1) / 256.0
    disp_gt = cv2.imread("../data/gt/disp_occ_0/" + filename + ".png",
                         -1) / 256.0
    obj_map = cv2.imread("../data/gt/obj_map/" + filename + ".png", -1) / 256.0
    disp_refined = replace_boundary(disp_psmnet, disp_bf)

    rtn = []
    error1, error1_fg, error1_bg, error_map1, count1_above_15 = compute_error(
        disp_gt, disp_refined, obj_map)
    rtn.append((error1, error1_fg, error1_bg, error_map1, count1_above_15))
    error2, error2_fg, error2_bg, error_map2, count2_above_15 = compute_error(
        disp_gt, disp_psmnet, obj_map)
    rtn.append((error2, error2_fg, error2_bg, error_map2, count2_above_15))
    error3, error3_fg, error3_bg, error_map3, count3_above_15 = compute_error(
        disp_gt, disp_lidar, obj_map)
    rtn.append((error3, error3_fg, error3_bg, error_map3, count3_above_15))
    print("All: LiDAR points upsampling... " + str(error3))
    print("All: before refinement... " + str(error2))
    print("All: after refinement... " + str(error1))
    print("FG: LiDAR points upsampling... " + str(error3_fg))
    print("FG: before refinement... " + str(error2_fg))
    print("FG: after refinement... " + str(error1_fg))
    print("BG: LiDAR points upsampling... " + str(error3_bg))
    print("BG: before refinement... " + str(error2_bg))
    print("BG: after refinement... " + str(error1_bg))
    print("BIG ERROR COUNT: LiDAR points upsampling... " +
          str(count3_above_15))
    print("BIG ERROR COUNT: before refinement... " + str(count2_above_15))
    print("BIG ERROR COUNT: after refinement... " + str(count1_above_15))

    f = plt.figure()

    ax1 = f.add_subplot(4, 2, 1)
    plt.imshow(error_map2, 'rainbow', vmin=-5, vmax=20)
    plt.axis('off')
    ax1.set_title("Error predicted: " + str(100 * error2)[:4] + "%",
                  fontsize=8)

    ax2 = f.add_subplot(4, 2, 2)
    plt.imshow(disp_psmnet, 'rainbow', vmin=10, vmax=80)
    plt.axis('off')
    ax2.set_title("Disparity predicted", fontsize=8)

    ax3 = f.add_subplot(4, 2, 3)
    plt.imshow(error_map1, 'rainbow', vmin=-5, vmax=20)
    plt.axis('off')
    ax3.set_title("Error refined:   " + str(100 * error1)[:4] + "%",
                  fontsize=8)

    ax4 = f.add_subplot(4, 2, 4)
    plt.imshow(disp_refined, 'rainbow', vmin=10, vmax=80)
    plt.axis('off')
    ax4.set_title("Disparity refined", fontsize=8)

    ax5 = f.add_subplot(4, 2, 5)
    plt.imshow(error_map3, 'rainbow', vmin=-5, vmax=20)
    plt.axis('off')
    ax5.set_title("Error upsampled: " + str(100 * error3)[:4] + "%",
                  fontsize=8)

    ax6 = f.add_subplot(4, 2, 6)
    plt.imshow(disp_lidar, 'rainbow', vmin=10, vmax=80)
    plt.axis('off')
    ax6.set_title("Disparity upsampled", fontsize=8)

    ax7 = f.add_subplot(4, 2, 7)
    plt.imshow(edge_map)
    plt.axis('off')
    ax7.set_title("Edges", fontsize=10)

    ax8 = f.add_subplot(4, 2, 8)
    plt.imshow(cv2.cvtColor(imgL, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    ax8.set_title("Image", fontsize=10)

    plt.tight_layout()
    plt.savefig("../output/" + "compare_" + filename + ".png", dpi=600)
    plt.close()

    points, colors = reproject_to_3D(disp_lidar, imgL)
    save_ply("../output/" + filename + "_upsampled.ply", points, colors)
    points, colors = reproject_to_3D(disp_psmnet, imgL)
    save_ply("../output/" + filename + "_predicted.ply", points, colors)
    points, colors = reproject_to_3D(disp_refined, imgL)
    save_ply("../output/" + filename + "_refined.ply", points, colors)
    points, colors = reproject_to_3D(disp_gt, imgL)
    save_ply("../output/" + filename + "_gt.ply", points, colors)

    return rtn
コード例 #6
0
# 1.configuration
category = sys.argv[1]
epochs = int(sys.argv[2])
batch_size = int(sys.argv[3])
gpu_s = int(sys.argv[4])
gpu_e = int(sys.argv[5])

norm_image_size = (368, 368)
belief_map_size = (46, 46)
keypoints_count = len(utils.keypoints_order[category])

# 2.load data for training
train_data = utils.dataLoader(
    category=category,
    path_to_excel_file=
    '/home/panziqi/project/fashion_ai/annotations/train/train.xlsx',
    images_prefix='/home/public/FashionAI/keypoint/season1/train/',
    norm_image_size=norm_image_size,
    belief_map_size=belief_map_size)

# 3.train
cpm = cpm.CPM(network_name='CPM_' + category,
              stage_count=4,
              norm_image_size=norm_image_size,
              belief_map_size=belief_map_size,
              keypoints_count=keypoints_count)
cpm.train(
    train_data=train_data,
    log_folder='/home/panziqi/project/fashion_ai/version_softmax/log/train/',
    params_folder='/home/panziqi/project/fashion_ai/version_softmax/params/',
    epochs=epochs,
コード例 #7
0
ファイル: main.py プロジェクト: sky-lzy/DailyCode
def train_val(
        train_im_dir='data/train',
        val_im_dir='data/train',  # data path configs
        norm_height=32,
        norm_width=128,  # image normalization configs
        n_epochs=20,
        batch_size=4,
        lr=1e-4,  # training configs
        model_save_epoch=5,
        model_save_dir='models',  # model saving configs
        load_pretrain=False,
        pretrain_path=None,  # pretrained model configs
        device='cpu'):
    '''
    The main training procedure
    ----------------------------
    :param train_im_dir: path to directory with training images and ground-truth file
    :param val_im_dir: path to directory with validation images and ground-truth file
    :param norm_height: image normalization height
    :param norm_width: image normalization width
    :param n_epochs: number of training epochs
    :param batch_size: training and validation batch size
    :param lr: learning rate
    :param model_save_epoch: save model after each {model_save_epoch} epochs
    :param model_save_dir: path to save the model
    :param load_pretrain: whether to load a pretrained model
    :param pretrain_path: path of the pretrained model
    :param device: 'cpu' or 'cuda'
    '''

    # step 1: initialize training and validation data loaders
    #         please see ListDataset and dataLoader (line 19 and line 92) in utils.py for details
    trainloader = dataLoader(train_im_dir,
                             norm_height,
                             norm_width,
                             batch_size,
                             training=True)
    valloader = dataLoader(val_im_dir,
                           norm_height,
                           norm_width,
                           batch_size,
                           training=False)

    # step 2: initialize the label converter
    #         please see LabelConverter (line 112) in utils.py for details
    label_converter = LabelConverter()

    # step 3: initialize the model
    model = CRNN()
    model = model.to(device)
    if load_pretrain:
        try:
            checkpoint = torch.load(
                pretrain_path,
                map_location=torch.device('cuda')
                if torch.cuda.is_available() else torch.device('cpu'))
            model.load_state_dict(checkpoint['state_dict'])
            print(f'[Info] load pretrained model from {pretrain_path}')
        except Exception as e:
            print(
                f'[Warning] load pretrain model failed, the reason is:\n    {e}'
            )
            print('[Warning] the model will be trained from scratch!')

    # step 4: define CTC loss function and optimizer
    # -- CTC loss function in PyTorch is nn.CTCLoss()
    #    note that the first input of nn.CTCLoss() is logarithmized probabilities
    #    please refer to the following document to look up its usage
    #    https://pytorch.org/docs/stable/generated/torch.nn.CTCLoss.html#torch.nn.CTCLoss
    criterion = nn.CTCLoss()
    optimizer = optim.Adam(model.parameters(), lr)

    # step 5: training & validation

    # two lists to save training loss and validation accuracy for each epoch
    losses, accuracies = [], []

    for epoch in range(n_epochs):
        # train
        print('\nEpoch [{}/{}] start ...'.format(epoch + 1, n_epochs))
        train_loss = train_one_epoch(model, trainloader, optimizer, criterion,
                                     label_converter, device)
        losses.append(train_loss)

        # validation
        accuracy = val_one_epoch(model, valloader, label_converter, device)
        accuracies.append(accuracy)

        # show information of the epoch
        print('train loss = {:.3f}, validation word accuracy = {:.1f}%'.format(
            train_loss, 100 * accuracy))

        # save model
        if (epoch + 1) % model_save_epoch == 0:
            model_save_path = os.path.join(
                model_save_dir, 'model_epoch{}.pth'.format(epoch + 1))
            torch.save({'state_dict': model.state_dict()}, model_save_path)
            print('[Info] model saved in {}'.format(model_save_path))

    # draw the loss and accuracy curve
    plot_loss_and_accuracies(losses, accuracies)
コード例 #8
0
ファイル: train.py プロジェクト: NightFury13/Seq2Seq_ChatBot

class colors:
    ok = '\033[92m'
    fail = '\033[91m'
    close = '\033[0m'

#------- Global Variables ---------#
content_filepath   	= 'new_data/out_TheSimpsons.tsv'
model_outpath		= 'models'
chars 			= '0123456789+/-*=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!.,?(){}[]&#_ ' # Char-Level Vocabulary.
TRUNCATE_SIZE 		= 20000
#----------------------------------#

#--------- Load the context-response pairs -----------#
dataset = dataLoader(content_filepath) # access data as dataset.contexts, dataset.responses
print("[Data-Loader] : Loaded", len(dataset.contexts), "context-response pairs")
#-----------------------------------------------------#

#-------- Parameters for the model and dataset--------#
questions = dataset.contexts[:TRUNCATE_SIZE]
expected = dataset.responses[:TRUNCATE_SIZE]

RNN = recurrent.LSTM
HIDDEN_SIZE = 512
BATCH_SIZE = 10
LAYERS = 3
X_MAXLEN = len(max(questions, key=len))
Y_MAXLEN = len(max(expected, key=len))

ctable = CharacterTable(chars, X_MAXLEN)
コード例 #9
0
def mnist_training(batch_size,
                   mnist_epochs,
                   Retrain=False,
                   lr=0.001,
                   opt="adam",
                   model_type="mlprelu"):

    time1 = time.time()

    device = utils.training_device()
    conv_idx = list()
    if model_type == "mlptanh":
        mnist_net = model.MLP_tanh().to(device)
        dimensions = [
            500, 256, 10
        ]  # You have to adjust this if you change MNIST model dimension
    elif model_type == "mlprelu":
        mnist_net = model.MLP_relu().to(device)
        dimensions = [
            500, 256, 10
        ]  # You have to adjust this if you change MNIST model dimension
    elif model_type == "mlpsigmoid":
        mnist_net = model.MLP_sigmoid().to(device)
        dimensions = [500, 256, 10]
    elif model_type == "cnn":
        mnist_net = model.CNN().to(device)
        conv_idx = [0, 1]  # which layer is convolutional layer
        dimensions = [10 * 24 * 24, 20 * 10 * 10, 256, 10]
    elif model_type == "mlptanhparsing":
        mnist_net = model.MLP_tanh_parsing().to(device)
        dimensions = [500, 500, 256, 256, 10, 10]
    elif model_type == "mlptanhparsing2":
        mnist_net = model.MLP_tanh_parsing2().to(device)
        dimensions = [256, 256, 256, 256, 10, 10]
    elif model_type == "mlpreluparsing":
        mnist_net = model.MLP_relu_parsing().to(device)
        dimensions = [500, 500, 256, 256, 10, 10]
    elif model_type == "mlpsigmoidparsing":
        mnist_net = model.MLP_sigmoid_parsing().to(device)
        dimensions = [500, 500, 256, 256, 10, 10]
    else:
        print("model type error!")
        exit(0)

    # create storage container of hidden layer representations
    num_layers = len(dimensions)

    label_y = [np.empty(shape=[0, 10]) for i in range(mnist_epochs)]

    all_repre = []
    for layer_idx in range(num_layers):
        all_repre.append([])
        for epoch in range(mnist_epochs):
            all_repre[layer_idx].append(
                np.empty(shape=[0, dimensions[layer_idx]]))

    # save training model config
    with open("mnist_net_config.pkl", "wb") as f:
        pickle.dump((batch_size, mnist_epochs, num_layers, dimensions), f)

    # load privious representation record
    # if Retrain == False and os.path.exists("mnist_net.pkl"):
    #     print("Loading MNIST model...")
    #     mnist_net = torch.load("mnist_net.pkl")
    #     with open("all_representation.pkl", "rb") as f:
    #         load_all_repre, load_label_y = pickle.load(f)

    #     return mnist_net, load_all_repre, load_label_y, dimensions, None

    logger.info(f"Training Device : {device}")
    trainLoader, testLoader = utils.dataLoader(batch_size=batch_size)

    # Loss and Optimizer
    criterion = nn.CrossEntropyLoss()

    if opt == "sgd":
        optimizer = torch.optim.SGD(mnist_net.parameters(),
                                    lr=lr,
                                    momentum=0.01)
    elif opt == "adam":
        optimizer = torch.optim.Adam(mnist_net.parameters(), lr=lr)

    acc = list()
    # Training
    for epoch in range(mnist_epochs):

        running_loss = 0
        loss_temp = None
        for i, data in enumerate(trainLoader, 0):
            # 輸入資料
            inputs, labels = data[0].to(device), data[1].to(device)

            # 使用 view() 將 inputs 的維度壓到符合模型的輸入。
            if model_type != "cnn":
                inputs = inputs.view(inputs.shape[0], -1)

            # 梯度清空
            optimizer.zero_grad()

            # Forward
            repre = list(mnist_net(inputs))
            # t1, t2, outputs = mnist_net(inputs)

            outputs = None
            # layer transformation
            for idx in range(len(repre)):

                if idx == len(repre) - 1:  # the last representation
                    outputs = repre[idx]

                if idx in conv_idx and model_type == "cnn":  # this layer is convolutional layer
                    repre[idx] = repre[idx].view(
                        -1,
                        len(repre[idx][0]) * len(repre[idx][0][0]) *
                        len(repre[idx][0][0][0]))
                    repre[idx] = repre[idx].cpu().detach().numpy()
                else:  # ordinary MLP
                    repre[idx] = repre[idx].cpu().detach().numpy()

            labels_np = labels.cpu().detach().numpy()

            # transform label to one-hot encoding and save it.
            label_y[epoch] = np.concatenate(
                (label_y[epoch], np.eye(10)[labels_np]), axis=0)

            # store all representations to additional list
            for layer_idx in range(num_layers):
                all_repre[layer_idx][epoch] = np.concatenate(
                    (all_repre[layer_idx][epoch], repre[layer_idx]), axis=0)

            # backward
            loss = criterion(outputs, labels)
            loss.backward()
            loss_temp = loss
            # 更新參數
            optimizer.step()

            running_loss += loss.item()

        logger.info(
            f"MNIST Training, epoch-{epoch} elapsed time: {time.time()-time1}, loss : {loss_temp}"
        )
        acc.append(mnist_testing(mnist_net, batch_size, model_type=model_type))

        if not os.path.exists("modelCheckPoint"):
            os.mkdir("modelCheckPoint")

        model_name = f"modelCheckPoint/{model_type}_{opt}_{epoch}.pkl"
        if Retrain == True or not os.path.exists(model_name):
            torch.save(mnist_net, model_name)
            # with open("all_representation.pkl", "wb") as f:
            #     pickle.dump((all_repre, label_y), f)

    return mnist_net, all_repre, label_y, dimensions, acc