def test_builder_with_validation(self): builder = KNearestNeighborsClassifierBuilder( input_name='input', output_name='output', number_of_dimensions=10, default_class_label='defaultLabel', k=3, weighting_scheme='inverse_distance', index_type='kd_tree', leaf_size=50) builder.author = 'CoreML Team' builder.license = 'MIT' builder.description = 'test_builder_with_validation' # Save the updated spec coreml_model = MLModel(builder.spec) coreml_model_path = '/tmp/__test_builder_with_validation.mlmodel' coreml_model.save(coreml_model_path) self.assertTrue(os.path.isfile(coreml_model_path)) try: stdout, stderr, return_code = self._compile_mlmodel( coreml_model_path) self.assertEqual(return_code, 0) finally: self._delete_mlmodel_and_mlmodelc(coreml_model_path)
def test_can_init_and_save_model_from_builder_with_updated_spec(self): builder = KNearestNeighborsClassifierBuilder( input_name="input", output_name="output", number_of_dimensions=10, default_class_label="defaultLabel", k=3, weighting_scheme="inverse_distance", index_type="kd_tree", leaf_size=50, ) builder.author = "CoreML Team" builder.license = "MIT" builder.description = "test_builder_with_validation" # Save the updated spec coreml_model = MLModel(builder.spec) self.assertIsNotNone(coreml_model) coreml_model_path = "/tmp/__test_builder_with_validation.mlmodel" try: coreml_model.save(coreml_model_path) self.assertTrue(os.path.isfile(coreml_model_path)) finally: self._delete_mlmodel_and_mlmodelc(coreml_model_path)
def create_builder(self, default_class_label='default_label'): builder = KNearestNeighborsClassifierBuilder( input_name='input', output_name='output', number_of_dimensions=4, default_class_label=default_class_label) return builder
def create_builder(self, default_class_label="default_label"): builder = KNearestNeighborsClassifierBuilder( input_name="input", output_name="output", number_of_dimensions=4, default_class_label=default_class_label, ) return builder
def test_can_init_and_save_model_from_builder_default_parameters(self): builder = KNearestNeighborsClassifierBuilder( input_name='input', output_name='output', number_of_dimensions=4, default_class_label='defaultLabel') # Save the updated spec coreml_model = MLModel(builder.spec) self.assertIsNotNone(coreml_model) coreml_model_path = '/tmp/__test_builder_with_validation.mlmodel' try: coreml_model.save(coreml_model_path) self.assertTrue(os.path.isfile(coreml_model_path)) finally: self._delete_mlmodel_and_mlmodelc(coreml_model_path)
def test_builder_with_compilation_default_parameters(self): builder = KNearestNeighborsClassifierBuilder(input_name='input', output_name='output', number_of_dimensions=4, default_class_label='defaultLabel') # Save the updated spec coreml_model = MLModel(builder.spec) coreml_model_path = '/tmp/__test_builder_with_validation.mlmodel' coreml_model.save(coreml_model_path) self.assertTrue(os.path.isfile(coreml_model_path)) try: stdout, stderr, return_code = self._compile_mlmodel(coreml_model_path) self.assertEqual(return_code, 0) finally: self._delete_mlmodel_and_mlmodelc(coreml_model_path)
del base_spec.description.output[:] # Add a new output for the feature vector. output = base_spec.description.output.add() output.name = "features" output.type.multiArrayType.shape.append(1000) output.type.multiArrayType.dataType = ft.ArrayFeatureType.FLOAT32 # Connect the last layer to this new output. base_spec.neuralNetwork.layers[-1].output[0] = "features" # Create the k-NN model. knn_builder = KNearestNeighborsClassifierBuilder( input_name="features", output_name="label", number_of_dimensions=1000, default_class_label="???", number_of_neighbors=3, weighting_scheme="inverse_distance", index_type="linear") knn_spec = knn_builder.spec knn_spec.description.input[0].shortDescription = "Input vector" knn_spec.description.output[0].shortDescription = "Predicted label" knn_spec.description.output[ 1].shortDescription = "Probabilities for each possible label" knn_builder.set_number_of_neighbors_with_bounds(3, allowed_range=(1, 10)) # Use the same name as in the neural network models, so that we # can use the same code for evaluating both types of model. knn_spec.description.predictedProbabilitiesName = "labelProbability"