Exemplo n.º 1
0
def ResNetBibdGcClassifier(dataloader, sample_weight_array, log_interval=200):
    begin = time.time()

    # Create the net
    net = ResNet_bibd_gc()

    # Set device of net
    net.to(device)

    # Define the optimizer and loss function
    optimizer = torch.optim.SGD(net.parameters(),
                                lr=LEARNING_RATE,
                                momentum=0.5)
    criterion = nn.CrossEntropyLoss()

    for epoch in range(1, N_EPOCH + 1):
        train(net,
              optimizer,
              criterion,
              dataloader,
              sample_weight_array,
              epoch,
              log_interval=log_interval)
        sys.stdout.write('\n')
        sys.stdout.flush()

    # Set net to evaluation mode
    net.eval()

    end = time.time()
    print('    ResNetBibdGcClassifier trained in {}.'.format(
        format_time(end - begin)))

    return net
Exemplo n.º 2
0
def mlpClassifier(dataloader, sample_weight_array, log_interval=200):
    begin = time.time()

    # Create the net
    input_dim = 28 * 28 * 1
    output_dim = 10
    net = Mlp(input_dim, output_dim)

    # Set device of net
    net.to(device)

    # Define the optimizer and loss function
    optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.5)
    criterion = nn.CrossEntropyLoss()

    n_epoch = 1
    for epoch in range(1, n_epoch + 1):
        train(net,
              optimizer,
              criterion,
              dataloader,
              sample_weight_array,
              epoch,
              log_interval=100)
        sys.stdout.write('\n')
        sys.stdout.flush()

    # Set net to evaluation mode
    net.eval()

    end = time.time()
    print('    MlpClassifier trained in {}.'.format(format_time(end - begin)))

    return net
Exemplo n.º 3
0
def BResNetVClassifier(dataloader, sample_weight_array, log_interval=200):
    begin = time.time()

    # Create the net
    net = create_resnet('18', sparsification='bibd', num_groups=4, name=BASE_CLASSIFIER_NAME)

    # Set device of net
    net.to(device)

    # Define the optimizer and loss function
    optimizer = torch.optim.SGD(net.parameters(), lr=LEARNING_RATE, momentum=0.5)
    criterion = nn.CrossEntropyLoss()

    for epoch in range(1, N_EPOCH + 1):
        train(net, optimizer, criterion, dataloader, sample_weight_array, epoch, log_interval=log_interval)
        sys.stdout.write('\n')
        sys.stdout.flush()

    # Set net to evaluation mode
    net.eval()

    end = time.time()
    print('    {} trained in {}.'.format(net.name, format_time(end - begin)))

    return net
Exemplo n.º 4
0
    def train(self, train_dataloader, validation_dataloader, classifier_num=5):
        # Initialize the sample weight array
        n = len(validation_dataloader)
        self.__sample_weight_array = np.repeat(1 / n, n)

        for i in range(classifier_num):
            print('Training {} #{}...'.format(self.base_classifier_name,
                                              i + 1))

            # Train the classifier
            begin_time = time.time()
            trained_classifier = self.__base_classifier(
                train_dataloader, self.__sample_weight_array, log_interval=10)
            end_time = time.time()
            print('    Training time: {}'.format(
                format_time(end_time - begin_time)))
            self.__base_classifier_list.append(trained_classifier)

            # Calculate the error
            print('Calculating the error of this base classifier...')
            error = 0
            for index, (x, y) in enumerate(validation_dataloader):
                # TODO: The logic here should stay outside of this class
                x = x.to('cuda')
                y = y.to('cuda')

                output = trained_classifier(x)
                predicted = output.max(1)[1]
                if not predicted.eq(y).cpu().sum():
                    error += self.__sample_weight_array[index]
            print('    Error: {}'.format(error))

            # Calculate the weight for the current hypothesis
            # TODO: Make 10 as a parameter
            weight = math.log((1 - error) / error) + math.log(10 - 1)
            print('    Weight: {}'.format(weight))
            self.__weight_list.append(weight)

            # Update the
            print("Updating the weights...")
            for index, (x, y) in enumerate(validation_dataloader):
                # print(y)
                # TODO: The logic here should stay outside of this class
                x = x.to('cuda')
                y = y.to('cuda')

                output = self.__base_classifier_list[-1](x)
                predicted = output.data.max(1)[1]
                if predicted.eq(y.data).cpu().sum():
                    # TODO: Make 10 as a parameter
                    self.__sample_weight_array[index] *= (1 - error) * (
                        10 - 1) / error

            # Normalize the sample weight array
            self.__sample_weight_array /= self.__sample_weight_array.sum()

        return
Exemplo n.º 5
0
def main():
    fmt_str = '{: <' + str(config.OLED_WIDTH_CHARS // 2) + '}{: >' + str(
        config.OLED_WIDTH_CHARS // 2) + '}'
    while True:
        try:
            t = time_utils.time()
            output = []
            if config.OLED_WIDTH_CHARS > 8:
                output.append('time {}'.format(
                    time_utils.format_time(time_utils.localtime())))
            else:
                output.append('{}'.format(
                    time_utils.format_time(time_utils.localtime())))
            res = qry.transform_response(qry.doit(t))
            for item in res[:N_ITEMS]:
                buss_name = item['trip']['route']['shortName']
                print(item)
                ts_of_departure = (item['serviceDay'] +
                                   item['realtimeDeparture']) - t
                if ts_of_departure < 0:
                    output('{} XXX'.format(buss_name))
                minutes_to_departure = ts_of_departure // 60
                seconds_to_departure = ts_of_departure % 60
                output.append(fmt_str.format(buss_name, minutes_to_departure))
            if output:
                oled.fill(0)
                y = 4
                for line in output:
                    print(line)
                    oled.text(line, 0, y)
                    y += 9
                oled.show()
        except Exception as ex:
            print(ex)
            utils.oled_ml_msg(str(ex), oled)
        utime.sleep(DELAY_BETWEEN_CHECKS)
Exemplo n.º 6
0
    # Copy data to GPU if needed
    data = data.to(device)
    target = target.to(device)

    category = classifier.predict(data)
    target_category = target.cpu().numpy().item()
    correct += 1 if category == target_category else 0
accuracy = correct / len(test_dataloader.dataset)
print('\nTest dataset: AdaBoostClassifier accuracy: {}/{} ({:.2f}%)\n'.format(
    correct, len(test_dataloader.dataset), accuracy * 100.0))

# Test the base classifier
for i in range(CLASSIFIER_NUM):
    correct = 0
    for batch_index, (data, target) in enumerate(test_dataloader):
        # Copy data to GPU if needed
        data = data.to(device)
        target = target.to(device)

        category = classifier.predict_using_base_classifier(i, data)
        target_category = target.cpu().numpy().item()
        correct += 1 if category == target_category else 0
    accuracy = correct / len(test_dataloader.dataset)
    print(
        'Test dataset: Base ResNetBibdGcClassifier #{} accuracy: {}/{} ({:.2f}%)'
        .format(i + 1, correct, len(test_dataloader.dataset),
                accuracy * 100.0))

end_time = time.time()
print('Total time usage: {}'.format(format_time(end_time - begin_time)))
Exemplo n.º 7
0
    def run_model(self,
                  model,
                  train_loader,
                  validation_loader,
                  name=None,
                  log_interval=500):
        # Store the name of the model
        if name is None:
            name = model.name
        self.model_name_array = np.append(self.model_name_array, name)

        length = len(name) + 19 + 8
        print(bcolors.OKBLUE + '<' * length + bcolors.ENDC)
        print('    ' + 'Running the model: {}'.format(name) + '    ')
        print(bcolors.OKBLUE + '-' * length + bcolors.ENDC)

        # Define the optimizer and loss function
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
        criterion = nn.CrossEntropyLoss()

        # The loss and accuracy list for this model
        loss_list, acc_list = [], []

        begin_time = time.time()  # Begin time
        for epoch in range(1, self.n_epoch + 1):
            self.__train(model,
                         optimizer,
                         criterion,
                         train_loader,
                         epoch,
                         log_interval=log_interval)
            self.__validate(model, criterion, validation_loader, loss_list,
                            acc_list)
        end_time = time.time()  # End time

        # Store the time
        self.time_array = np.append(self.time_array, end_time - begin_time)

        # Store the loss and accuracy data
        self.loss_ndarray.resize(
            (self.loss_ndarray.shape[0] + 1, self.loss_ndarray.shape[1]),
            refcheck=False)
        self.loss_ndarray[self.loss_ndarray.shape[0] - 1] = np.array(loss_list)
        self.acc_ndarray.resize(
            (self.acc_ndarray.shape[0] + 1, self.acc_ndarray.shape[1]),
            refcheck=False)
        self.acc_ndarray[self.acc_ndarray.shape[0] - 1] = np.array(acc_list)

        # Print total time usage
        time_str = format_time(end_time - begin_time)
        length = len(name) + len(time_str) + 23 + 4
        print(bcolors.OKGREEN + '-' * length + bcolors.ENDC)
        print('  ' + 'Time usage for model ' + name + ': ' + time_str + '  ')
        print(bcolors.OKGREEN + '>' * length + bcolors.ENDC)
        print()

        torch.cuda.empty_cache()
        # model = model.cpu()
        del model, train_loader, validation_loader, optimizer, criterion
        torch.cuda.empty_cache()
        # print('CUDA memory cache released.')

        # print('CUDA memory allocated: {:.2f}MB'.format(torch.cuda.memory_allocated() / 1024))
        # print('CUDA memory cached: {:.2f}MB'.format(torch.cuda.memory_cached() / 1024))

        # This is PyTorch 1.3.1, which does not support torch.cuda.memory_summary()
        # print(torch.cuda.memory_summary())

        return