init()

in SemanticSegmentationSample/Common/PostProcessing.swift [17:37]


    init(model: MLModel) throws {
        struct ClassList: Codable {
            var labels: [String]
        }

        guard let userFields = model.modelDescription.metadata[MLModelMetadataKey.creatorDefinedKey] as? [String : String],
              let params = userFields["com.apple.coreml.model.preview.params"] else {
            throw PostProcessorError.missingModelMetadata
        }
        guard let jsonData = params.data(using: .utf8),
              let classList = try? JSONDecoder().decode(ClassList.self, from: jsonData) else {
            throw PostProcessorError.missingModelMetadata
        }
        let rawLabels = classList.labels

        // Filter out empty categories whose label is "--"
        let ids2Labels = Dictionary(uniqueKeysWithValues: rawLabels.enumerated().filter { $1 != "--" })

        self.numClasses = rawLabels.count
        self.ids2Labels = ids2Labels
    }