Esempio n. 1
0
    def _train_and_test(self,
                        model_path,
                        output_model_path,
                        training_datapoints,
                        test_datapoints,
                        keep_classes=False):
        engine = ImprintingEngine(model_path, keep_classes)
        image_shape = self._get_image_shape(model_path)
        data_dir = test_utils.test_data_path('imprinting')
        # train.
        for training_datapoint in training_datapoints:
            engine.train(
                test_utils.prepare_images(training_datapoint['image_names'],
                                          data_dir, image_shape),
                training_datapoint['label_id'])
        engine.save_model(output_model_path)

        # Test.
        engine = ClassificationEngine(output_model_path)
        self.assertEqual(1, engine.get_num_of_output_tensors())
        if not keep_classes:
            self.assertEqual(len(training_datapoints),
                             engine.get_output_tensor_size(0))
        for test_datapoint in test_datapoints:
            self._classify_image(engine, data_dir,
                                 test_datapoint['image_name'],
                                 test_datapoint['label_id'],
                                 test_datapoint['score'])
Esempio n. 2
0
def run_benchmark(model):
    """Measures training time for given model with random data.

  Args:
    model: string, file name of the input model.

  Returns:
    float, training time.
  """
    input_size = input_tensor_size(model)
    engine = ImprintingEngine(test_utils.test_data_path(model),
                              keep_classes=False)

    np.random.seed(12345)
    data_by_category = {}
    # 10 Categories, each has 20 images.
    for i in range(0, 10):
        data_by_category[i] = []
        for j in range(0, 20):
            data_by_category[i].append(np.random.randint(0, 255, input_size))

    start = time.perf_counter()
    for class_id, tensors in enumerate(data_by_category.values()):
        engine.train(tensors, class_id)
    with tempfile.NamedTemporaryFile() as f:
        engine.save_model(f.name)
    training_time = time.perf_counter() - start

    print('Model: %s' % model)
    print('Training time: %.2fs' % training_time)
    return training_time
Esempio n. 3
0
 def _train_and_test_run_inference(self,
                                   model_path,
                                   training_datapoints,
                                   test_datapoints,
                                   keep_classes=False):
     engine = ImprintingEngine(model_path, keep_classes)
     image_shape = self._get_image_shape(model_path)
     data_dir = test_utils.test_data_path('imprinting')
     # train.
     for training_datapoint in training_datapoints:
         engine.train(
             test_utils.prepare_images(training_datapoint['image_names'],
                                       data_dir, image_shape),
             training_datapoint['label_id'])
     # Test with running inference.
     for test_datapoint in test_datapoints:
         self._classify_image_by_inference(engine, image_shape, data_dir,
                                           test_datapoint['image_name'],
                                           test_datapoint['label_id'],
                                           test_datapoint['score'])
Esempio n. 4
0
def _benchmark_for_training(model, data_set):
    """Measures training time for given model and data set.

  Args:
    model: string, file name of the input model.
    data_set: string, name of the folder storing images. Labels file is also
      named as '[data_set].csv'.

  Returns:
    float, training time.
  """
    shape = _get_shape(model)
    engine = ImprintingEngine(test_utils.test_data_path('imprinting', model),
                              keep_classes=False)
    output_model_path = '/tmp/model_for_benchmark.tflite'

    data_dir = test_utils.test_data_path(data_set)

    # The labels file is named as '[data_set].csv'.
    image_list_by_category = test_utils.prepare_classification_data_set(
        test_utils.test_data_path(data_set + '.csv'))

    start_time = time.monotonic()
    for category, image_list in image_list_by_category.items():
        category_dir = os.path.join(data_dir, category)
        image_list_by_category[category] = test_utils.prepare_images(
            image_list, category_dir, shape)
    end_time = time.monotonic()
    print('Image pre-processing time: ', end_time - start_time, 's')
    start_time = end_time
    for class_id, tensors in enumerate(image_list_by_category.values()):
        engine.train(tensors, class_id)
    engine.save_model(output_model_path)
    training_time = time.monotonic() - start_time
    print('Model: ', model)
    print('Data set : ', data_set)
    print('Training time : ', training_time, 's')
    # Remove the model.
    subprocess.call(['rm', output_model_path])
    return training_time