Пример #1
0
    def test_softmax_regression_linear_non_separable_data(self):
        # Fake data is generated from 3 MVN distributions, these MVN distributions
        # are NOT well-separated.
        num_train = 200
        num_val = 30
        # Let's distribute data evenly among different classes.
        num_classes = 3
        class_sizes = ((num_train + num_val) // num_classes) * np.ones(
            num_classes, dtype=int)
        class_sizes[-1] = (num_train + num_val) - np.sum(class_sizes[0:-1])

        means = np.array([[1, 1], [-1, -1], [1, -1]])
        feature_dim = len(means[0])
        cov_mats = [np.eye(feature_dim)] * num_classes

        model = SoftmaxRegression(feature_dim, num_classes)
        np.random.seed(54321)
        all_data, all_labels = generate_fake_data(class_sizes, means, cov_mats)

        dataset = {}
        dataset['data_train'] = all_data[0:num_train]
        dataset['labels_train'] = all_labels[0:num_train]
        dataset['data_val'] = all_data[num_train:]
        dataset['labels_val'] = all_labels[num_train:]
        # train with SGD.
        num_iter = 50
        learning_rate = 0.1
        model.train_with_sgd(dataset,
                             num_iter,
                             learning_rate,
                             batch_size=100,
                             print_every=5)
        self.assertGreater(
            model.get_accuracy(dataset['data_train'], dataset['labels_train']),
            0.8)
def _benchmark_for_training(num_classes, feature_dim):
    """Measures training time for given data set parameters.

  Args:
    num_classes: int, number of classes.
    feature_dim: int, dimension of the feature vector.

  Returns:
    float, training time.
  """

    num_train = 1024
    num_val = 256
    num_total = num_train + num_val
    class_sizes = (num_total // num_classes) * np.ones(num_classes, dtype=int)

    print('Preparing data set for num_classes=%d, feature_dim=%d' %
          (num_classes, feature_dim))
    np.random.seed(12345)
    all_data = np.random.rand(num_total, feature_dim)
    all_labels = np.tile(np.arange(num_classes), class_sizes[0])
    np.random.shuffle(all_labels)

    dataset = {}
    dataset['data_train'] = all_data[0:num_train]
    dataset['labels_train'] = all_labels[0:num_train]
    dataset['data_val'] = all_data[num_train:]
    dataset['labels_val'] = all_labels[num_train:]

    model = SoftmaxRegression(feature_dim, num_classes)

    # Train with SGD.
    num_iter = 500
    learning_rate = 0.01
    batch_size = 100
    print('Start backprop')
    start_time = time.perf_counter()
    model.train_with_sgd(dataset,
                         num_iter,
                         learning_rate,
                         batch_size,
                         print_every=-1)
    training_time = time.perf_counter() - start_time
    print('Backprop time: ', training_time, 's')
    return training_time
Пример #3
0
    def test_softmax_regression_linear_separable_data(self):
        # Fake data is generated from 3 MVN distributions, these MVN distributionss
        # are tuned to be well-separated, such that it can be separated by
        # SoftmaxRegression model (which is a linear classifier).
        num_train = 200
        num_val = 30
        # Let's distribute data evenly among different classes.
        num_classes = 3
        class_sizes = ((num_train + num_val) // num_classes) * np.ones(
            num_classes, dtype=int)
        class_sizes[-1] = (num_train + num_val) - np.sum(class_sizes[0:-1])

        # 3 is chosen, such that each pair of mean is over 6 `sigma` distance
        # apart. Which makes classes harder to `touch` each other.
        # https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule
        means = np.array([[1, 1], [-1, -1], [1, -1]]) * 3
        feature_dim = len(means[0])
        cov_mats = [np.eye(feature_dim)] * num_classes

        model = SoftmaxRegression(feature_dim, num_classes)
        np.random.seed(12345)
        all_data, all_labels = generate_fake_data(class_sizes, means, cov_mats)

        dataset = {}
        dataset['data_train'] = all_data[0:num_train]
        dataset['labels_train'] = all_labels[0:num_train]
        dataset['data_val'] = all_data[num_train:]
        dataset['labels_val'] = all_labels[num_train:]
        # train with SGD.
        num_iter = 20
        learning_rate = 0.01
        model.train_with_sgd(dataset,
                             num_iter,
                             learning_rate,
                             batch_size=100,
                             print_every=5)
        self.assertGreater(
            model.get_accuracy(dataset['data_train'], dataset['labels_train']),
            0.99)
Пример #4
0
def train(model_path, data_dir, output_dir):
  """Trains a softmax regression model given data and embedding extractor.

  Args:
    model_path: string, path to embedding extractor.
    data_dir: string, directory that contains training data.
    output_dir: string, directory to save retrained tflite model and label map.
  """
  t0 = time.perf_counter()
  image_paths, labels, label_map = get_image_paths(data_dir)
  train_and_val_dataset, test_dataset = shuffle_and_split(image_paths, labels)
  # Initializes basic engine model here to avoid repeatedly initialization,
  # which is time consuming.
  engine = basic_engine.BasicEngine(model_path)
  print('Extract embeddings for data_train')
  train_and_val_dataset['data_train'] = extract_embeddings(
      train_and_val_dataset['data_train'], engine)
  print('Extract embeddings for data_val')
  train_and_val_dataset['data_val'] = extract_embeddings(
      train_and_val_dataset['data_val'], engine)
  t1 = time.perf_counter()
  print('Data preprocessing takes %.2f seconds' % (t1 - t0))

  # Construct model and start training
  weight_scale = 5e-2
  reg = 0.0
  feature_dim = train_and_val_dataset['data_train'].shape[1]
  num_classes = np.max(train_and_val_dataset['labels_train']) + 1
  model = SoftmaxRegression(
      feature_dim, num_classes, weight_scale=weight_scale, reg=reg)

  learning_rate = 1e-2
  batch_size = 100
  num_iter = 500
  model.train_with_sgd(
      train_and_val_dataset, num_iter, learning_rate, batch_size=batch_size)
  t2 = time.perf_counter()
  print('Training takes %.2f seconds' % (t2 - t1))

  # Append learned weights to input model and save as tflite format.
  out_model_path = os.path.join(output_dir, 'retrained_model_edgetpu.tflite')
  model.save_as_tflite_model(model_path, out_model_path)
  print('Model %s saved.' % out_model_path)
  label_map_path = os.path.join(output_dir, 'label_map.txt')
  save_label_map(label_map, label_map_path)
  print('Label map %s saved.' % label_map_path)
  t3 = time.perf_counter()
  print('Saving retrained model and label map takes %.2f seconds' % (t3 - t2))

  retrained_engine = basic_engine.BasicEngine(out_model_path)
  test_embeddings = extract_embeddings(test_dataset['data_test'],
                                       retrained_engine)
  saved_model_acc = np.mean(
      np.argmax(test_embeddings, axis=1) == test_dataset['labels_test'])
  print('Saved tflite model test accuracy: %.2f%%' % (saved_model_acc * 100))
  t4 = time.perf_counter()
  print('Checking test accuracy takes %.2f seconds' % (t4 - t3))
Пример #5
0
def part_B_run_and_train(model_B_path, data_dir, output_dir, scaling_factor, zero_point):
  """Trains a softmax regression model given the intermediate embeddings, true labels, label map and embedding extractor part B.
  Args:
    model_B_path: string, path to embedding extractor part B.
    data_dir: string, directory that contains the intermediate embeddings, true labels, label map.
    output_dir: string, directory to save retrained tflite model and label map.
  """
  t0 = time.perf_counter()

  # Load the the intermediate embeddings and true labels
  with open(os.path.join(data_dir, 'train_and_val_dataset.pickle'), 'rb') as handle:
    train_and_val_dataset = pickle.load(handle)
  with open(os.path.join(data_dir, 'test_dataset.pickle'), 'rb') as handle:
    test_dataset = pickle.load(handle)

  # Initializes basic engine model here to avoid repeated initialization,
  # which is time consuming.
  engine = basic_engine.BasicEngine(model_B_path)
  print('Extract embeddings for data_train')
  train_and_val_dataset['data_train'] = extract_final_embeddings(train_and_val_dataset['data_train'], engine, scaling_factor, zero_point)
  print('Extract embeddings for data_val')
  train_and_val_dataset['data_val'] = extract_final_embeddings(train_and_val_dataset['data_val'], engine, scaling_factor, zero_point)
  t1 = time.perf_counter()
  print('Data preprocessing takes %.2f seconds' % (t1 - t0))

  # Construct FC + softmax and start training
  weight_scale = 5e-2
  reg = 0.0
  feature_dim = train_and_val_dataset['data_train'].shape[1]
  print('feature_dim: %d'%feature_dim)
  num_classes = np.max(train_and_val_dataset['labels_train']) + 1
  print('num_classes: %d'%num_classes)
  model = SoftmaxRegression(feature_dim, num_classes, weight_scale=weight_scale, reg=reg)
  learning_rate = 1e-2
  batch_size = 100
  num_iter = 500
  label_map = " "
  with open(os.path.join(data_dir, 'label_map.txt'), 'r') as fp:
    label_map = fp.read()
  with open(os.path.join(data_dir, 'label_map.pickle'),'wb' ) as fp:
    pickle.dump(label_map, fp)
  model._load_ckpt(data_dir)
  model.train_with_sgd(train_and_val_dataset, num_iter, learning_rate, batch_size=batch_size, print_every=10)
  t2 = time.perf_counter()
  print('Training takes %.2f seconds' % (t2 - t1))

  # Append learned weights to input model part B and save as tflite format.
  out_model_path = os.path.join(output_dir, 'retrained_model_edgetpu.tflite')
  model.save_as_tflite_model(model_B_path, out_model_path)
  print('Model %s saved.' % out_model_path)
  os.system('mv {0} {1}'.format(os.path.join(data_dir, 'label_map.txt'), os.path.join(output_dir, 'label_map.txt')))
  print('Label map %s saved' % os.path.join(output_dir, 'label_map.txt'))
  t3 = time.perf_counter()
  print('Saving retrained model and label map takes %.2f seconds' % (t3 - t2))
  model._save_ckpt(output_dir)

  # Test
  retrained_engine = basic_engine.BasicEngine(out_model_path)
  test_embeddings = extract_final_embeddings(test_dataset['data_test'], retrained_engine, scaling_factor, zero_point)
  saved_model_acc = np.mean(np.argmax(test_embeddings, axis=1) == test_dataset['labels_test'])
  print(np.argmax(test_embeddings, axis=1))
  print(test_dataset['labels_test'])
  """
  check=[[0]*10 for i in range(10)]
  for i in range(10):
    recognized=False
    x = np.argmax(test_embeddings, axis=1)
    y = test_dataset['labels_test']
    for j in range(199):
      if x[j]==y[j]:
        recognized=True
        if recognized and x[j]==i:
          check[i][i] = check[i][i] + 1
      elif x[j]==i:
        check[y[j]][i] = check[y[j]][i] + 1
  print('--------------------')
  for i in range(10):
    print(check[i])
  """
  print('Saved tflite model test accuracy: %.2f%%' % (saved_model_acc * 100))
  t4 = time.perf_counter()
  print('Checking test accuracy takes %.2f seconds' % (t4 - t3))