Real-ESRGAN on Core ML: Tiling Large Images Without Seams
Running Real-ESRGAN through Core ML on large photos requires fixed-size inference, overlap, mirrored borders and deliberate compute-unit selection.

Key takeaways
The converted Real-ESRGAN models use fixed 256 × 256 inputs and produce 4× output for the cited models.
A 16-pixel overlap margin leaves a 224-pixel central contribution, preventing visible seams from missing context.
Mirrored edge handling avoids the black-border artefacts associated with zero padding.
Core ML compute units can be selected explicitly, and virtual machines without a Neural Engine fall back to slower CPU execution.
Running Real-ESRGAN through Core ML is relatively straightforward until the input is no longer a small test image. A 4,000 × 3,000 photo becomes a 192-megapixel image at 4× enlargement, while the converted model expects a fixed 256 × 256 input. The practical solution is to divide the source into overlapping regions and manage the boundaries carefully.
Fixed shapes make each inference predictable
The implementation converts two Real-ESRGAN networks with coremltools: RRDBNet for x4plus and SRVGGNetCompact for general-x4v3. Both become FP16 ML Programs with an input named x and an output named y, using a 256 × 256 input shape.
The fixed dimensions are intentional. They make memory use per call easier to predict, while avoiding an unresolved question about which flexible-shape configurations would continue to run on the Neural Engine. The Swift code compiles the model once with MLModel.compileModel and then loads it with an explicitly selected MLComputeUnits configuration.
Available choices include CPU plus Neural Engine, CPU plus GPU, all available units, and CPU only. On macOS 14 and later, MLModel.availableComputeDevices can be used to check for a Neural Engine. The author’s macOS virtual machine has none, so its runs used the CPU and were much slower.
Overlap is what removes the tile grid
Simply processing adjacent 256-pixel squares and joining their enlarged results creates a visible grid. The reason is that each output pixel depends on surrounding input pixels. At a tile edge, some of that context is absent, so the network receives padding rather than the original image.
The tiling scheme adds a 16-pixel margin around each tile but keeps only the central 224 pixels for the result. Tiles therefore begin 224 pixels apart and read 16 pixels beyond their nominal area on every side. The overlap gives the model additional context, while the central region is used for compositing.
Image edges need a separate treatment. Instead of filling missing pixels with black, the implementation mirrors the source image. Its index-reflection helper makes mirror(-3, 4) return 3 and mirror(4, 4) return 2, bouncing at the boundary without duplicating the edge pixel. This presents plausible image content to the network; zero padding can resemble a black border that the model then enhances.
Memory still matters
The model consumes NCHW float32 data normalized to the 0…1 range. To limit repeated allocations, the implementation reuses one MLMultiArray for every tile. It derives the enlarged image dimensions from the source dimensions and scale, then allocates space for each tile result using three channels and the enlarged tile width and height.
The supplied implementation ends during the tiling stage, before its complete output-copying and compositing logic is shown. The demonstrated constraints nevertheless establish the core engineering pattern: keep inference inputs bounded, preserve neighbourhood context, mirror at image boundaries and choose compute units explicitly.
Why it matters for your role
Pick the building you work in.

For software engineers
Large-image super-resolution needs more than a successful model conversion. Engineers must design bounded inference calls, reuse memory and preserve receptive-field context at tile boundaries.

For QA engineers
A tiled implementation should be checked for visible grid artefacts, edge behaviour and differences between compute-unit configurations, especially when testing on environments without a Neural Engine.
Based on reporting by DEV Community (source article).
Written with AI assistance and reviewed under our editorial standards. Spotted an error? Tell the desk.









Comments