def classify(network_name, accuracy_threshold): pixel_input = {'input_image_format': 'pixel_values'} network = get_pretrained_network(network_name) nlayers = len(network['image_network']['layers']) noutputs = network['image_network']['metadata']['outputs'] preprocessors = network['preprocess'] image_model = create_image_model(network_name, None) pixel_model = create_image_model(network_name, pixel_input) read = reader_for_network(network_name, None) assert len(image_layers(pixel_model)) == nlayers # Just check if this is possible image_feature_extractor(pixel_model) ex_mod = image_feature_extractor(image_model) for image, cidx in [('dog.jpg', 254), ('bus.jpg', 779)]: point = load_points(preprocessors, [[image]]) file_pred = image_model.predict(point) img_px = np.expand_dims(read(image).numpy(), axis=0) pixel_pred = pixel_model.predict(img_px) for pred in [file_pred, pixel_pred]: check_image_prediction(pred, cidx, accuracy_threshold, 0.02) outputs = ex_mod(load_points(preprocessors, [['dog.jpg'], ['bus.jpg']])) assert outputs.shape == (2, noutputs)
def test_black_and_white(): pixel_model = create_image_model("mobilenetv2", None) image_path = os.path.join(TEST_IMAGE_DATA, "model_t.jpg") point = load_points([{"type": IMAGE, "index": 0}], [[image_path]]) pred = pixel_model.predict(point) check_image_prediction(pred, 661, 0.95, 0.02) with Image.open(image_path) as img: image_array = np.array(img) point = load_points([{"type": IMAGE, "index": 0}], [[image_array]]) pred = pixel_model.predict(point) check_image_prediction(pred, 661, 0.95, 0.02)
def classify(network_name, accuracy_threshold): network = get_pretrained_network(network_name) nlayers = len(network["image_network"]["layers"]) noutputs = network["image_network"]["metadata"]["outputs"] preprocessors = network["preprocess"] pixel_model = create_image_model(network_name, None) assert len(get_image_layers(pixel_model)) == nlayers for image, cidx in CLASSIFIER_TEST_IMAGES: image_path = os.path.join(TEST_IMAGE_DATA, image) point = load_points(preprocessors, [[image_path]]) pred = pixel_model.predict(point) check_image_prediction(pred, cidx, accuracy_threshold, 0.02) ex_mod = image_feature_extractor(pixel_model) bundle_mod = create_model(get_extractor_bundle(network_name)) read = reader_for_network(network_name, None) img_arrays = np.array( [read(im[0]).numpy() for im in CLASSIFIER_TEST_IMAGES]) bundle_outputs = bundle_mod(img_arrays) ex_outputs = ex_mod(img_arrays) assert ex_outputs.shape == (2, noutputs) assert bundle_outputs.shape == (2, noutputs) abs_out = np.abs(ex_outputs - bundle_outputs) assert np.mean(abs_out) < 1e-5, abs_out
def test_empty(): detector = create_image_model("tinyyolov4", None) image_path = os.path.join(TEST_IMAGE_DATA, "black.png") image = load_points([{"type": IMAGE, "index": 0}], [[image_path]]) boxes, scores, classes = detector.predict(image) assert len(boxes[0]) == 0 assert len(scores[0]) == 0 assert len(classes[0]) == 0
def test_scaling(): detector = create_image_model("tinyyolov4", None) image_path = os.path.join(TEST_IMAGE_DATA, "strange_car.png") image = load_points([{"type": IMAGE, "index": 0}], [[image_path]]) boxes, scores, classes = detector.predict(image) assert 550 < boxes[0, 0, 0] < 600, boxes[0, 0] assert 220 < boxes[0, 0, 1] < 270, boxes[0, 0] assert 970 < boxes[0, 0, 2] < 1020, boxes[0, 0] assert 390 < boxes[0, 0, 3] < 440, boxes[0, 0] assert scores[0] > 0.9 assert classes[0] == 2
def single_embedding(index): test_info = read_regression(EMBEDDING)[index] test_info['output_exposition'] = fake_outex(test_info) preprocessor = Preprocessor(test_info, EXTRA_PARAMS) forest = ForestPreprocessor(test_info) inputs = load_points(test_info['preprocess'], test_info['input_data']) proc_result = preprocessor(inputs) tree_result = forest(proc_result) data_with_trees = np.array(test_info['with_trees']) data_without_trees = np.array(test_info['without_trees']) assert np.allclose(proc_result, data_without_trees) assert np.allclose(tree_result, data_with_trees)
def load_and_predict(self, points): pvec = load_points(self._preprocessors, points) return self._model.predict(pvec)