| 19 | } |
| 20 | |
| 21 | Specification::NeuralNetwork* buildBasicNeuralNetworkModel(Specification::Model& m, bool isUpdatable, const TensorAttributes *inTensorAttr, const TensorAttributes *outTensorAttr, int numberOfLayers, bool areWeightsQuantized, bool isBiasQuantized) { |
| 22 | auto inTensor = m.mutable_description()->add_input(); |
| 23 | inTensor->set_name(inTensorAttr->name); |
| 24 | auto inTensorShape = inTensor->mutable_type()->mutable_multiarraytype(); |
| 25 | inTensorShape->set_datatype(Specification::ArrayFeatureType_ArrayDataType_FLOAT32); |
| 26 | for (int i = 0; i < inTensorAttr->dimension; i++) { |
| 27 | inTensorShape->add_shape(1); |
| 28 | } |
| 29 | |
| 30 | auto outTensor = m.mutable_description()->add_output(); |
| 31 | outTensor->set_name(outTensorAttr->name); |
| 32 | auto outTensorShape = outTensor->mutable_type()->mutable_multiarraytype(); |
| 33 | outTensorShape->set_datatype(Specification::ArrayFeatureType_ArrayDataType_FLOAT32); |
| 34 | for (int i = 0; i < outTensorAttr->dimension; i++) { |
| 35 | outTensorShape->add_shape(1); |
| 36 | } |
| 37 | |
| 38 | m.set_specificationversion(MLMODEL_SPECIFICATION_VERSION_IOS13); |
| 39 | |
| 40 | auto neuralNet = m.mutable_neuralnetwork(); |
| 41 | |
| 42 | for (int i = 0; i < numberOfLayers; i++) { |
| 43 | auto layer = neuralNet->add_layers(); |
| 44 | |
| 45 | std::string layerName = "inner_layer" + std::to_string(i); |
| 46 | std::string input = "output" + std::to_string(i-1); |
| 47 | std::string output = "output" + std::to_string(i); |
| 48 | |
| 49 | layer->set_name((numberOfLayers == 1) ? "inner_layer" : layerName.c_str()); |
| 50 | layer->add_input((i == 0) ? inTensorAttr->name : input.c_str()); |
| 51 | layer->add_output((i == numberOfLayers-1) ? outTensorAttr->name : output.c_str()); |
| 52 | |
| 53 | Specification::InnerProductLayerParams *innerProductParams = layer->mutable_innerproduct(); |
| 54 | innerProductParams->set_inputchannels(1); |
| 55 | innerProductParams->set_outputchannels(1); |
| 56 | |
| 57 | // set weight |
| 58 | auto* weights = innerProductParams->mutable_weights(); |
| 59 | if (areWeightsQuantized) { |
| 60 | auto *quant_params = weights->mutable_quantization(); |
| 61 | quant_params->set_numberofbits(1); |
| 62 | auto *linear_quant_params = quant_params->mutable_linearquantization(); |
| 63 | linear_quant_params->add_scale(1.0f); |
| 64 | linear_quant_params->add_bias(0.0f); |
| 65 | weights->set_int8rawvalue("x01"); // this is [1] |
| 66 | } else { |
| 67 | weights->add_floatvalue(1.0); |
| 68 | } |
| 69 | |
| 70 | // set bias |
| 71 | innerProductParams->set_hasbias(true); |
| 72 | auto* bias = innerProductParams->mutable_bias(); |
| 73 | if (isBiasQuantized) { |
| 74 | auto *quant_params = bias->mutable_quantization(); |
| 75 | quant_params->set_numberofbits(1); |
| 76 | auto *linear_quant_params = quant_params->mutable_linearquantization(); |
| 77 | linear_quant_params->add_scale(1.0f); |
| 78 | linear_quant_params->add_bias(0.0f); |
no test coverage detected