3

I am using CoreML with the DeepLabV3 model to remove the background from an image: https://developer.apple.com/machine-learning/models/

This is working well for removing the background from photos where the subject it a person/dog/car, but for other cases, such as skylines and some objects on a table (please see example images), it is unable to detect the object from the background.

Should I be using a different method for this?

Thank you

var imageSegmentationModel = DeepLabV3()
var request :  VNCoreMLRequest?

func setUpModel() {
        if let visionModel = try? VNCoreMLModel(for: imageSegmentationModel.model) {
                request = VNCoreMLRequest(model: visionModel, completionHandler: visionRequestDidComplete)
                request?.imageCropAndScaleOption = .scaleFill
        }
        else {
                fatalError()
        }
}

func predict() {
        DispatchQueue.global(qos: .userInitiated).async {
            guard let request = self.request else { fatalError() }
            let handler = VNImageRequestHandler(cgImage: (self.originalImage?.cgImage)!, options: [:])
            do {
                try handler.perform([request])
            }catch {
                print(error)
            }
        }
   }

func visionRequestDidComplete(request: VNRequest, error: Error?) {
        DispatchQueue.main.async {
            if let observations = request.results as? [VNCoreMLFeatureValueObservation],
                let segmentationmap = observations.first?.featureValue.multiArrayValue {
                
                self.maskImage = segmentationmap.image(min: 0, max: 255)
                print(self.maskImage!.size)
                
                self.maskImage = self.maskImage?.resizedImage(for: self.originalImage!.size)
                if let image:UIImage = self.maskOriginalImage(){
                    print("Success")
                    self.outputImageView.image = image
                }
            }
        }
            
    }

func maskOriginalImage() -> UIImage? {
        if(self.maskImage != nil && self.originalImage != nil){
            let maskReference = self.maskImage?.cgImage!
            let imageMask = CGImage(maskWidth: maskReference!.width,
                                    height: maskReference!.height,
                                    bitsPerComponent: maskReference!.bitsPerComponent,
                                    bitsPerPixel: maskReference!.bitsPerPixel,
                                    bytesPerRow: maskReference!.bytesPerRow,
                                    provider: maskReference!.dataProvider!, decode: nil, shouldInterpolate: true)
            
            let maskedReference = self.originalImage?.cgImage!.masking(imageMask!)
            return UIImage(cgImage: maskedReference!)
            
        }
        return nil
    }

Good Image Example

Bad Image Example

Tom Coomer
  • 6,227
  • 12
  • 45
  • 82
  • Ypu should take a look at [Highlighting Areas of Interest in an Image Using Saliency](https://developer.apple.com/documentation/vision/highlighting_areas_of_interest_in_an_image_using_saliency) – Laszlo Sep 27 '22 at 17:37

3 Answers3

4

If you look at the supported labels for the DeepLabV3 model, they include ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningTable", "dog", "horse", "motorbike", "person", "pottedPlant", "sheep", "sofa", "train", "tvOrMonitor"]. The model is not trained to segment a macbook.

You can either train your own model to include desriable objects. Or look at the use of saliency which does a decent job at detecting the primary object in an image.

Harry J
  • 1,842
  • 3
  • 12
  • 28
0

you can see in output label of Squeeznet model these 999 labels are the object on which this model is trained.

If you wish you can train your Custom Model my Using native Swifty "CreateML" Which comes integrated with Xcode

Hritik Singh
  • 101
  • 7
0

You can use salient object detection neural network.

GPIT
  • 13
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '22 at 00:32