0

I am exploring the Apple Core ML framework.

I created a training model using Create ML app. Image Classification to identify the image is cat or a dog. The data set I used is referred from https://www.kaggle.com/datasets/samuelcortinhas/cats-and-dogs-image-classification?resource=download

I created my .mlmodel file from create ml by providing the training data. Now I am doing the prediction in the app and it is giving me same target and probability results and different images(either cat or dog, giving same result - ["cats": 0.6281524444894766, "dogs": 0.3718475555105234]

App Code:

override func viewDidLoad() {
        if  let img = UIImage(named: "dog_29.jpg") {
            predictImage(image: img)
        }  else {
            print("image  not captured")
        }
    }
    
    
    private func predictImage(image: UIImage) {
        

        let inputImageSize: CGFloat = 299.0
                let minLen = min(image.size.width, image.size.height)
                let resizedImage = image.resize(to: CGSize(width: inputImageSize * image.size.width / minLen, height: inputImageSize * image.size.height / minLen))

        guard let pixelBuffer = resizedImage.pixelBuffer() else {
                    fatalError()
                }
        
        do {
            let config = MLModelConfiguration()
            let model = try CatsAndDogs(configuration:config)
            let result = try model.prediction(image: pixelBuffer)
            print(result.target)
            print(result.targetProbability)

            print(result)
        } catch {
            print("image classification error")
        }
        
    }

Code of resize and getcvbuffer:

  func resize(to newSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: newSize.width, height: newSize.height), true, 1.0)
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return resizedImage
    }

  func pixelBuffer() -> CVPixelBuffer? {
        let width = self.size.width
        let height = self.size.height
        let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
                     kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
        var pixelBuffer: CVPixelBuffer?
        let status = CVPixelBufferCreate(kCFAllocatorDefault,
                                         Int(width),
                                         Int(height),
                                         kCVPixelFormatType_32ARGB,
                                         attrs,
                                         &pixelBuffer)

        guard let resultPixelBuffer = pixelBuffer, status == kCVReturnSuccess else {
            return nil
        }

        CVPixelBufferLockBaseAddress(resultPixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
        let pixelData = CVPixelBufferGetBaseAddress(resultPixelBuffer)

        let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
        guard let context = CGContext(data: pixelData,
                                      width: Int(width),
                                      height: Int(height),
                                      bitsPerComponent: 8,
                                      bytesPerRow: CVPixelBufferGetBytesPerRow(resultPixelBuffer),
                                      space: rgbColorSpace,
                                      bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue) else {
                                        return nil
        }

        context.translateBy(x: 0, y: height)
        context.scaleBy(x: 1.0, y: -1.0)

        UIGraphicsPushContext(context)
        self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        UIGraphicsPopContext()
        CVPixelBufferUnlockBaseAddress(resultPixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

        return resultPixelBuffer
    }

I also have tried various methods of resizing and getcvbuffer available on net but all giving same result.

I tried different images of cat and dog from test folder from the dataset link. Still the result is the same.

Why the predictions are not correct?

2
  • How many images did you train on? Commented Dec 11, 2024 at 19:18
  • @TimRoberts 100 images, even the images it correctly identifies with testing data in create ml , those images are giving wrong using in app. Commented Dec 12, 2024 at 5:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.