def _test_oxford17_flowers_single(self, model_path, keep_classes, expected): top_k_range = len(expected) ret = self._transfer_learn_and_evaluate( test_utils.test_data_path(model_path), keep_classes, test_utils.test_data_path('oxford_17flowers'), 0.25, top_k_range) for i in range(top_k_range): self.assertGreaterEqual(ret[i], expected[i])
def segment_image(model_file, image_file, mask_file): interpreter = make_interpreter(test_data_path(model_file)) interpreter.allocate_tensors() image = Image.open(test_data_path(image_file)).resize( common.input_size(interpreter), Image.ANTIALIAS) common.set_input(interpreter, image) interpreter.invoke() result = segment.get_output(interpreter) if len(result.shape) > 2: result = np.argmax(result, axis=2) reference = np.asarray(Image.open(test_data_path(mask_file))) return array_iou(result, reference)
def test_imprinting_engine_load_extractor_with_wrong_format(self): expected_message = ( 'Unsupported model architecture. Input model must have ' 'an L2Norm layer.') with self.assertRaisesRegex(ValueError, expected_message): ImprintingEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite'))
def test_incremental_training(self): train_points = [TrainPoint(images=['cat_train_0.bmp'], class_id=0)] retrain_points = [ TrainPoint(images=['dog_train_0.bmp'], class_id=1), TrainPoint(images=['hotdog_train_0.bmp', 'hotdog_train_1.bmp'], class_id=2) ] test_points = [ TestPoint(image='cat_test_0.bmp', class_id=0, score=0.99), TestPoint(image='dog_test_0.bmp', class_id=1, score=0.99), TestPoint(image='hotdog_test_0.bmp', class_id=2, score=0.99) ] for model_path in _MODEL_LIST: with self.subTest(model_path=model_path): model = self._train_and_test( test_utils.test_data_path(model_path), train_points, [], keep_classes=False) with test_utils.temporary_file( suffix='.tflite') as new_model_file: new_model_file.write(model) # Retrain based on cat only model. self._train_and_test(new_model_file.name, retrain_points, test_points, keep_classes=True)
def test_softmax_regression_serialize_model(self): feature_dim = 1024 num_classes = 5 model = SoftmaxRegression(feature_dim, num_classes) in_model_path = test_utils.test_data_path( 'mobilenet_v1_1.0_224_quant_embedding_extractor.tflite') model.serialize_model(in_model_path)
def detection_task(num_inferences): tid = threading.get_ident() print('Thread: %d, %d inferences for detection task' % (tid, num_inferences)) model_name = 'ssd_mobilenet_v1_coco_quant_postprocess_edgetpu.tflite' interpreter = make_interpreter( test_utils.test_data_path(model_name), device=':1') interpreter.allocate_tensors() print('Thread: %d, using device 1' % tid) with test_utils.test_image('cat.bmp') as img: for _ in range(num_inferences): _, scale = common.set_resized_input( interpreter, img.size, lambda size, image=img: image.resize(size, Image.ANTIALIAS)) interpreter.invoke() ret = detect.get_objects( interpreter, score_threshold=0.7, image_scale=scale) self.assertEqual(len(ret), 1) self.assertEqual(ret[0].id, 16) # cat expected_bbox = detect.BBox( xmin=int(0.1 * img.size[0]), ymin=int(0.1 * img.size[1]), xmax=int(0.7 * img.size[0]), ymax=int(1.0 * img.size[1])) self.assertGreaterEqual( detect.BBox.iou(expected_bbox, ret[0].bbox), 0.85) print('Thread: %d, done detection task' % tid)
def classify_image(model_file, image_file, image_quantization=None): """Runs image classification and returns result with the highest score. Args: model_file: string, model file name. image_file: string, image file name. image_quantization: (scale: float, zero_point: float), assumed image quantization parameters. Returns: Classification result with the highest score as (index, score) tuple. """ interpreter = make_interpreter(test_data_path(model_file)) interpreter.allocate_tensors() image = test_image(image_file, common.input_size(interpreter)) input_type = common.input_details(interpreter, 'dtype') if np.issubdtype(input_type, np.floating): # This preprocessing is specific to MobileNet V1 with floating point input. image = (input_type(image) - 127.5) / 127.5 if np.issubdtype(input_type, np.integer) and image_quantization: image = rescale_image( image, image_quantization, common.input_details(interpreter, 'quantization'), input_type) common.set_input(interpreter, image) interpreter.invoke() return classify.get_classes(interpreter)[0]
def test_imprinting_engine_saving_without_training(self): model_list = [ 'mobilenet_v1_1.0_224_l2norm_quant.tflite', 'mobilenet_v1_1.0_224_l2norm_quant_edgetpu.tflite' ] for model in model_list: engine = ImprintingEngine(test_utils.test_data_path(model), keep_classes=False) with self.assertRaisesRegex(RuntimeError, 'Model is not trained.'): engine.serialize_model()
def classification_task(num_inferences): tid = threading.get_ident() print('Thread: %d, %d inferences for classification task' % (tid, num_inferences)) labels = read_label_file(test_utils.test_data_path('imagenet_labels.txt')) model_name = 'mobilenet_v1_1.0_224_quant_edgetpu.tflite' interpreter = make_interpreter( test_utils.test_data_path(model_name), device=':0') interpreter.allocate_tensors() size = common.input_size(interpreter) print('Thread: %d, using device 0' % tid) with test_utils.test_image('cat.bmp') as img: for _ in range(num_inferences): common.set_input(interpreter, img.resize(size, Image.NEAREST)) interpreter.invoke() ret = classify.get_classes(interpreter, top_k=1) self.assertEqual(len(ret), 1) self.assertEqual(labels[ret[0].id], 'Egyptian cat') print('Thread: %d, done classification task' % tid)
def _get_ref_result(ref_model, input_tensors): interpreter = make_interpreter(test_utils.test_data_path(ref_model)) interpreter.allocate_tensors() input_details = interpreter.get_input_details() assert len(input_details) == 1 output_details = interpreter.get_output_details() assert len(output_details) == 1 interpreter.tensor(input_details[0]['index'])()[0][:, :] = input_tensors[0] interpreter.invoke() return np.array(interpreter.tensor(output_details[0]['index'])())
def _make_runner(model_paths, devices): print('Using devices: ', devices) print('Using models: ', model_paths) if len(model_paths) != len(devices): raise ValueError('# of devices and # of model_paths should match') interpreters = [ make_interpreter(test_utils.test_data_path(m), d) for m, d in zip(model_paths, devices) ] for interpreter in interpreters: interpreter.allocate_tensors() return pipeline.PipelinedModelRunner(interpreters)
def test_training_l2_norm_model_keep_classes(self): train_points = [ TrainPoint(images=['cat_train_0.bmp'], class_id=1001), TrainPoint(images=['dog_train_0.bmp'], class_id=1002), TrainPoint(images=['hotdog_train_0.bmp', 'hotdog_train_1.bmp'], class_id=1003) ] test_points = [ TestPoint(image='cat_test_0.bmp', class_id=1001, score=0.99), TestPoint(image='hotdog_test_0.bmp', class_id=1003, score=0.92) ] for model_path in _MODEL_LIST: with self.subTest(model_path=model_path): self._train_and_test(test_utils.test_data_path(model_path), train_points, test_points, keep_classes=True)
def _default_test_model_path(self): return test_utils.test_data_path( 'mobilenet_v1_1.0_224_quant_edgetpu.tflite')
def test_image(image_file, size): return Image.open(test_data_path(image_file)).resize(size, Image.NEAREST)