Instructions to use InspiratioNULL/CLIP-VIT-B-32-DataComp.XL-CoreML with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- OpenCLIP
How to use InspiratioNULL/CLIP-VIT-B-32-DataComp.XL-CoreML with OpenCLIP:
import open_clip model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:InspiratioNULL/CLIP-VIT-B-32-DataComp.XL-CoreML') tokenizer = open_clip.get_tokenizer('hf-hub:InspiratioNULL/CLIP-VIT-B-32-DataComp.XL-CoreML') - Notebooks
- Google Colab
- Kaggle
| ///MARK: This is the generated class file, useful for proper implementation. | |
| //Created by InspiratioNULL on 1/20/2026 | |
| // CLIP_ImageEncoder.swift | |
| // | |
| // This file was automatically generated and should not be edited. | |
| // | |
| import CoreML | |
| /// Model Prediction Input Type | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| public class CLIP_ImageEncoderInput : MLFeatureProvider { | |
| /// image as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high | |
| /// image as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high | |
| public var image: CVPixelBuffer | |
| public var featureNames: Set<String> { ["image"] } | |
| public func featureValue(for featureName: String) -> MLFeatureValue? { | |
| if featureName == "image" { | |
| return MLFeatureValue(pixelBuffer: image) | |
| } | |
| return nil | |
| } | |
| public init(image: CVPixelBuffer) { | |
| self.image = image | |
| } | |
| public convenience init(imageWith image: CGImage) throws { | |
| self.init(image: try MLFeatureValue(cgImage: image, pixelsWide: 224, pixelsHigh: 224, pixelFormatType: kCVPixelFormatType_32ARGB, options: nil).imageBufferValue!) | |
| } | |
| public convenience init(imageAt image: URL) throws { | |
| self.init(image: try MLFeatureValue(imageAt: image, pixelsWide: 224, pixelsHigh: 224, pixelFormatType: kCVPixelFormatType_32ARGB, options: nil).imageBufferValue!) | |
| } | |
| func setImage(with image: CGImage) throws { | |
| self.image = try MLFeatureValue(cgImage: image, pixelsWide: 224, pixelsHigh: 224, pixelFormatType: kCVPixelFormatType_32ARGB, options: nil).imageBufferValue! | |
| } | |
| func setImage(with image: URL) throws { | |
| self.image = try MLFeatureValue(imageAt: image, pixelsWide: 224, pixelsHigh: 224, pixelFormatType: kCVPixelFormatType_32ARGB, options: nil).imageBufferValue! | |
| } | |
| } | |
| /// Model Prediction Output Type | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| public class CLIP_ImageEncoderOutput : MLFeatureProvider { | |
| /// Source provided by CoreML | |
| private let provider : MLFeatureProvider | |
| /// var_1240 as 1 by 512 matrix of floats | |
| /// var_1240 as 1 by 512 matrix of floats | |
| public var var_1240: MLMultiArray { | |
| provider.featureValue(for: "var_1240")!.multiArrayValue! | |
| } | |
| /// var_1240 as 1 by 512 matrix of floats | |
| /// var_1240 as 1 by 512 matrix of floats | |
| public var var_1240ShapedArray: MLShapedArray<Float> { | |
| MLShapedArray<Float>(var_1240) | |
| } | |
| public var featureNames: Set<String> { | |
| provider.featureNames | |
| } | |
| public func featureValue(for featureName: String) -> MLFeatureValue? { | |
| provider.featureValue(for: featureName) | |
| } | |
| public init(var_1240: MLMultiArray) { | |
| self.provider = try! MLDictionaryFeatureProvider(dictionary: ["var_1240" : MLFeatureValue(multiArray: var_1240)]) | |
| } | |
| public init(features: MLFeatureProvider) { | |
| self.provider = features | |
| } | |
| } | |
| /// Class for model loading and prediction | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | |
| public class CLIP_ImageEncoder { | |
| public let model: MLModel | |
| /// URL of model assuming it was installed in the same bundle as this class | |
| /// URL of model assuming it was installed in the same bundle as this class | |
| public class var urlOfModelInThisBundle : URL { | |
| let bundle = Bundle(for: self) | |
| return bundle.url(forResource: "CLIP_ImageEncoder", withExtension:"mlmodelc")! | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance with an existing MLModel object. | |
| Usually the application does not use this initializer unless it makes a subclass of CLIP_ImageEncoder. | |
| Such application may want to use `MLModel(contentsOfURL:configuration:)` and `CLIP_ImageEncoder.urlOfModelInThisBundle` to create a MLModel object to pass-in. | |
| - parameters: | |
| - model: MLModel object | |
| */ | |
| public init(model: MLModel) { | |
| self.model = model | |
| } | |
| /** | |
| Construct a model with configuration | |
| - parameters: | |
| - configuration: the desired model configuration | |
| - throws: an NSError object that describes the problem | |
| */ | |
| public convenience init(configuration: MLModelConfiguration = MLModelConfiguration()) throws { | |
| try self.init(contentsOf: type(of:self).urlOfModelInThisBundle, configuration: configuration) | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance with explicit path to mlmodelc file | |
| - parameters: | |
| - modelURL: the file url of the model | |
| - throws: an NSError object that describes the problem | |
| */ | |
| public convenience init(contentsOf modelURL: URL) throws { | |
| try self.init(model: MLModel(contentsOf: modelURL)) | |
| } | |
| /** | |
| Construct a model with URL of the .mlmodelc directory and configuration | |
| - parameters: | |
| - modelURL: the file url of the model | |
| - configuration: the desired model configuration | |
| - throws: an NSError object that describes the problem | |
| */ | |
| public convenience init(contentsOf modelURL: URL, configuration: MLModelConfiguration) throws { | |
| try self.init(model: MLModel(contentsOf: modelURL, configuration: configuration)) | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance asynchronously with optional configuration. | |
| Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread. | |
| - parameters: | |
| - configuration: the desired model configuration | |
| - handler: the completion handler to be called when the model loading completes successfully or unsuccessfully | |
| */ | |
| public class func load(configuration: MLModelConfiguration = MLModelConfiguration(), completionHandler handler: @escaping (Swift.Result<CLIP_ImageEncoder, Error>) -> Void) { | |
| load(contentsOf: self.urlOfModelInThisBundle, configuration: configuration, completionHandler: handler) | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance asynchronously with optional configuration. | |
| Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread. | |
| - parameters: | |
| - configuration: the desired model configuration | |
| */ | |
| public class func load(configuration: MLModelConfiguration = MLModelConfiguration()) async throws -> CLIP_ImageEncoder { | |
| try await load(contentsOf: self.urlOfModelInThisBundle, configuration: configuration) | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance asynchronously with URL of the .mlmodelc directory with optional configuration. | |
| Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread. | |
| - parameters: | |
| - modelURL: the URL to the model | |
| - configuration: the desired model configuration | |
| - handler: the completion handler to be called when the model loading completes successfully or unsuccessfully | |
| */ | |
| public class func load(contentsOf modelURL: URL, configuration: MLModelConfiguration = MLModelConfiguration(), completionHandler handler: @escaping (Swift.Result<CLIP_ImageEncoder, Error>) -> Void) { | |
| MLModel.load(contentsOf: modelURL, configuration: configuration) { result in | |
| switch result { | |
| case .failure(let error): | |
| handler(.failure(error)) | |
| case .success(let model): | |
| handler(.success(CLIP_ImageEncoder(model: model))) | |
| } | |
| } | |
| } | |
| /** | |
| Construct CLIP_ImageEncoder instance asynchronously with URL of the .mlmodelc directory with optional configuration. | |
| Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread. | |
| - parameters: | |
| - modelURL: the URL to the model | |
| - configuration: the desired model configuration | |
| */ | |
| public class func load(contentsOf modelURL: URL, configuration: MLModelConfiguration = MLModelConfiguration()) async throws -> CLIP_ImageEncoder { | |
| let model = try await MLModel.load(contentsOf: modelURL, configuration: configuration) | |
| return CLIP_ImageEncoder(model: model) | |
| } | |
| /** | |
| Make a prediction using the structured interface | |
| It uses the default function if the model has multiple functions. | |
| - parameters: | |
| - input: the input to the prediction as CLIP_ImageEncoderInput | |
| - throws: an NSError object that describes the problem | |
| - returns: the result of the prediction as CLIP_ImageEncoderOutput | |
| */ | |
| public func prediction(input: CLIP_ImageEncoderInput) throws -> CLIP_ImageEncoderOutput { | |
| try prediction(input: input, options: MLPredictionOptions()) | |
| } | |
| /** | |
| Make a prediction using the structured interface | |
| It uses the default function if the model has multiple functions. | |
| - parameters: | |
| - input: the input to the prediction as CLIP_ImageEncoderInput | |
| - options: prediction options | |
| - throws: an NSError object that describes the problem | |
| - returns: the result of the prediction as CLIP_ImageEncoderOutput | |
| */ | |
| public func prediction(input: CLIP_ImageEncoderInput, options: MLPredictionOptions) throws -> CLIP_ImageEncoderOutput { | |
| let outFeatures = try model.prediction(from: input, options: options) | |
| return CLIP_ImageEncoderOutput(features: outFeatures) | |
| } | |
| /** | |
| Make an asynchronous prediction using the structured interface | |
| It uses the default function if the model has multiple functions. | |
| - parameters: | |
| - input: the input to the prediction as CLIP_ImageEncoderInput | |
| - options: prediction options | |
| - throws: an NSError object that describes the problem | |
| - returns: the result of the prediction as CLIP_ImageEncoderOutput | |
| */ | |
| @available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *) | |
| public func prediction(input: CLIP_ImageEncoderInput, options: MLPredictionOptions = MLPredictionOptions()) async throws -> CLIP_ImageEncoderOutput { | |
| let outFeatures = try await model.prediction(from: input, options: options) | |
| return CLIP_ImageEncoderOutput(features: outFeatures) | |
| } | |
| /** | |
| Make a prediction using the convenience interface | |
| It uses the default function if the model has multiple functions. | |
| - parameters: | |
| - image: color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high | |
| - throws: an NSError object that describes the problem | |
| - returns: the result of the prediction as CLIP_ImageEncoderOutput | |
| */ | |
| public func prediction(image: CVPixelBuffer) throws -> CLIP_ImageEncoderOutput { | |
| let input_ = CLIP_ImageEncoderInput(image: image) | |
| return try prediction(input: input_) | |
| } | |
| /** | |
| Make a batch prediction using the structured interface | |
| It uses the default function if the model has multiple functions. | |
| - parameters: | |
| - inputs: the inputs to the prediction as [CLIP_ImageEncoderInput] | |
| - options: prediction options | |
| - throws: an NSError object that describes the problem | |
| - returns: the result of the prediction as [CLIP_ImageEncoderOutput] | |
| */ | |
| public func predictions(inputs: [CLIP_ImageEncoderInput], options: MLPredictionOptions = MLPredictionOptions()) throws -> [CLIP_ImageEncoderOutput] { | |
| let batchIn = MLArrayBatchProvider(array: inputs) | |
| let batchOut = try model.predictions(from: batchIn, options: options) | |
| var results : [CLIP_ImageEncoderOutput] = [] | |
| results.reserveCapacity(inputs.count) | |
| for i in 0..<batchOut.count { | |
| let outProvider = batchOut.features(at: i) | |
| let result = CLIP_ImageEncoderOutput(features: outProvider) | |
| results.append(result) | |
| } | |
| return results | |
| } | |
| } | |