FB:FB16079804
Hello,
I've made the FastAI's Cat vs Dog model into model that distinguishes lemons from limes and it all works fine in a notebook.
I am now looking to transform this model into Core ML for my iOS app using TorchScript and Apple official guidelines for coremltools.
Model converts but I cannot see the Preview Tab in. Xcode. Have anyone of you tried to convert to Core ML? I guess my input types are not matching with coremltools expectations for preview but I am stuck . Here is my code.
import torch
import coremltools as ct
from fastai.vision.all import *
import json
from torchvision import transforms
# Load your Fastai model (replace with your actual path)
learn = load_learner('lemonmodel.pkl')
# Example input image (you can use any image from your dataset)
input_image = PILImage.create('example.jpg')
# Preprocess the image (assuming you used these transforms during training)
to_tensor = transforms.ToTensor()
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
input_tensor = to_tensor(input_image)
input_tensor = normalize(input_tensor) # Apply normalization
# Add a batch dimension
input_tensor = input_tensor.unsqueeze(0)
# Ensure float32 type
input_tensor = input_tensor.float()
# Trace the model
trace = torch.jit.trace(learn.model, input_tensor)
# Define the Core ML input type (considering your model's input shape)
_input = ct.ImageType(
name="input_1",
shape=input_tensor.shape,
bias=[-0.485/0.229, -0.456/0.224, -0.406/0.225],
scale=1./(255*0.226)
)
# Convert the model to Core ML format
mlmodel = ct.convert(
trace,
inputs=[_input],
minimum_deployment_target=ct.target.iOS14 # Optional, set deployment target
)
# Set model type as 'imageClassifier' for the Preview tab
mlmodel.type = 'imageClassifier'
# Correct structure for preview parameters** (assuming two classes: 'lemon' and 'lime')
labels_json = {
"imageClassifier": {
"labels": ["lemon", "lime"],
"input": {
"shape": list(input_tensor.shape), # Provide the actual input shape
"mean": [0.485, 0.456, 0.406], # Match normalization mean
"std": [0.229, 0.224, 0.225] # Match normalization std
},
"output": {
"shape": [1, 2] # Output shape for your model (2 classes)
}
}
}
# Setting up the metadata with correct 'preview' params
mlmodel.user_defined_metadata['com.apple.coreml.model.preview.params'] = json.dumps(labels_json)
# Save the model as .mlmodel
mlmodel.save("LemonClassifierGemini.mlmodel")
mlmodel = ct.convert(
trace,
inputs=[_input],
minimum_deployment_target=ct.target.iOS14 # Optional, set deployment target
)
# Set model type as 'imageClassifier' for the Preview tab**
mlmodel.type = 'imageClassifier'
# Correct structure for preview parameters** (assuming two classes: 'lemon' and 'lime')
labels_json = {
"imageClassifier": {
"labels": ["lemon", "lime"],
"input": {
"shape": list(input_tensor.shape), # Provide the actual input shape
"mean": [0.485, 0.456, 0.406], # Match normalization mean
"std": [0.229, 0.224, 0.225] # Match normalization std
},
"output": {
"shape": [1, 2] # Output shape for your model (2 classes)
}
}
}
# Setting up the metadata with correct 'preview' params**
mlmodel.user_defined_metadata['com.apple.coreml.model.preview.params'] = json.dumps(labels_json)
# Save the model as .mlmodel
mlmodel.save("LemonClassifierGemini.mlmodel")
My model is :
Input batch shape: torch.Size([32, 3, 192, 192])
Labels batch shape: torch.Size([32])
Validation Loss: None, Validation Metric: None
Predictions shape: torch.Size([63, 2])
Targets shape: torch.Size([63])
Code for the model :
searches = 'lemon','lime'
path = Path('lemon_or_not')
for o in searches:
dest = (path/o)
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f'{o} photo'))
time.sleep(5)
resize_images(path/o, max_size=400, dest=path/o)
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(path, bs=32)
dls.show_batch(max_n=6)
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
is_lemon,_,probs = learn.predict(PILImage.create('lemon.jpg'))
print(f"This is a: {is_lemon}.")
print(f"Probability it's a lemon: {probs[0]:.4f}")
This is a: lemon.
Probability it's a lemon: 1.0000
learn.export('lemonmodel.pkl')
I am stuck to why it doest show the Preview Tab.
Xcode
RSS for tagBuild, test, and submit your app using Xcode, Apple's integrated development environment.
Posts under Xcode tag
200 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
I am a complete newbie when it comes to Swift and MacOS development. So apologies, I don't even know what is the right thing to search for.
I have an app which uses ScreenCaptureKit. I had a preview working which showed the different windows available, it initially required me to give my app permissions for screen and system audio recording which I did.
However now whenever I rebuild the app it asks for permission again and fails - despite the permission already being given.
Hello
I want to be able to save Date in @AppStorage, and it works however I was wondering what was the difference between these two extensions, which one is better and why?
extension Date: RawRepresentable {
static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
return formatter
}()
public var rawValue: String {
Date.dateFormatter.string(from: self)
}
public init?(rawValue: String) {
self = Date.dateFormatter.date(from: rawValue) ?? Date()
}
}
and
extension Date: RawRepresentable {
private static let formatter = ISO8601DateFormatter()
public var rawValue: String {
Date.formatter.string(from: self)
}
public init?(rawValue: String) {
self = Date.formatter.date(from: rawValue) ?? Date()
}
}
Thank You!
Hi, after a software update of our previously successfully notarizated installion .pkg we now receive a cryptic notarization issue refusing the entire .pkg:
{
"logFormatVersion": 1,
"jobId": "5cff2d71-7228-4fb4-a39d-329084cd2713",
"status": "Invalid",
"statusSummary": "Archive contains critical validation errors",
"statusCode": 4000,
"archiveFilename": "my_installer.pkg.zip",
"uploadDate": "2024-12-04T23:17:14.016Z",
"sha256": "2f26d0376506abe130ac904d7cb0d0cd5428666624428da9f44da7756352844f",
"ticketContents": null,
"issues": [
{
"severity": "error",
"code": null,
"path": "my_installer.pkg.zip",
"message": "Package my_installer.pkg.zip has no signed executables or bundles. No tickets can be generated.",
"docUrl": null,
"architecture": null
},
{
"severity": "warning",
"code": null,
"path": "my_installer.pkg.zip/my_installer.pkg",
"message": "The contents of the package at my_installer.pkg.zip/my_installer.pkg could not be extracted.",
"docUrl": null,
"architecture": null
}
]
}
What could be the reason for that? We've also submitted the .pkg (not zipped) with the same result. We build it on different macOS versions, including Sonoma 14.7 with latest developer tools installed, without any change in outcome.
But when extracting it via the undocumented:
pkgutil --expand-full
and .zipping the raw contents (without re-packaging it as .pkg) and sending it again, notarization succeeds for all components.
However the installation for the .pkg still fails with the notarization dialog. I was under the assumption that it is sufficient to notarize the .pkg contents but this does not seem to be true, or?
Any hints or help is highly appreciated, Thanks!
In Xcode 16, even if Objective-C is selected, the Navigator in the documentation viewer displays Swift information.
I don't know when this bug was introduced, but it's there in the last Xcode 16 betas at least.
I hope as many developers as possible submit this bug to Apple to get their attention.
P.S. Yes, I know there's Dash. For many years, Dash was a much better option than Xcode's built-in viewer. However, in version 5, the Dash developer introduced some unfortunate UI changes that made Xcode a better option to view the documentation in certain cases. Unfortunately, the Dash developer doesn't seem to be interested in user feedback anymore. He's been ignoring suggestions and user requests for years by now.
In Xcode 16, when I use File > New > Project to create a new iOS app project, Xcode automatically creates an asset catalog file called Assets.xcassets in the project.
However, if I later use File > New > File from Template, for example to add an asset catalog to an embedded Swift package within a project, Xcode suggests the default filename Media.xcassets for the file, instead of Assets.xcassets.
Why is the default name for this file different in each context?
Thank you for explaining this troubling consistency issue which causes me to lie awake at night.
Hi!
Trying to move some code into a local package - with Xcode 16 I am unable to move my code to the new package as drag and drop will always result in a copy. This contradicts the demo in the original intro session at WWDC19, and also the current documentation.. Of course I can delete the original files but this feels somewhat wrong...
Am I missing something? Did the behaviour of moving files in the Project navigator change in the recent Xcode releases?
I created a new iOS project (storyboard if it matters) and added a bunch of C files to it. Some portion of the C files depend on libcurl. I would like to be able to build for both simulator and device if possible. Google claims that Xcode can provide the dependency as part of the inbuilt libraries however I do not see libcurl.4.tbd (or any version) as an option to choose. Is this feature no longer available or is there something I am missing here?
For context here is a screen shot of my build error situation
Hello!
I am having trouble setting start times for songs when using the ApplicationMusicPlayer.
When I initialize a new MusicPlayer.Queue.Entry using the following constructor, I am seeing strange results:
init(
_ playableMusicItem: PlayableMusicItem,
startTime: TimeInterval? = nil,
endTime: TimeInterval? = nil
)
It appears that any value I provide for startTime is also applied to the endTime. For example:
MusicPlayer.Queue.Entry(playable, startTime: TimeInterval(30), endTime: TimeInterval(183))
provides the following console output:
MusicPlayer.Queue.Entry(id: "3D6A3DA3-595E-4657-8DBA-DDD245BBB7EF", transientItem: PlayableMusicItem, startTime: 30.0, endTime: 30.0)
I have also tried setting the endTime to nil with the same result. Does anyone have any experience setting start times for songs using the MusicKit ApplicationMusicPlayer?
Any feedback is greatly appreciated!
My macOS app includes a system extension that is activated once the app is ran. The system extension requires Full Disk Access.
When navigating to System Settings → Privacy & Security -> Full Disk Access, I can see my extension listed, but it has the default system "lego" icon, instead of my app's icon.
My app icon is working fine everywhere else. I tried to add an Asset Catalog to my extension on Xcode and include the icon, but it didn't do anything.
Is this the default behavior for extensions, or can you include an icon?
My preferred way of setting up an app project no longer works in Xcode 16.
I like to have my apps massively modularized - a separate SPM module for every significant feature. I use a project grouped together with a package that contains all the feature modules. The project's app target imports a consolidated AppFeature from the package that is all the logic of the app.
In Xcode 15 and before, I had a process for creating this kind of setup (described below -- unfortunately the forums don't support collapsible sections). Where I used to be able to drag a directory from Finder into the Xcode files navigator (step 6 in my process), it now rejects the drag.
What I'm looking for is a way to have a single window open that has my Swift package and below that a separate folder for each executable target. Creating new modules in the package automatically creates new schemes in Xcode. Executable targets in the project can reference any module in the package. Source control treats the entire thing as one repository.
I've tried all the approaches I can think of to accomplish the same goal, but no luck. The projects I've already built work fine in Xcode 16 -- I just can't make a new one. Unfortunately I can't revert to Xcode 15 for this purpose since it apparently doesn't run on my work machine with macOS 15.
Here's my process that worked great till now:
In Terminal, create the project folder Foo:
$ mkdir Foo
$ cd Foo
Create the package:
$ swift package init
Creating library package: Foo
Creating Package.swift
...
Create a directory for the project:
$ mkdir App
In Xcode, create a new app project called Foo and put it in the App folder
Open the Foo project
Drag the top level Foo directory from Finder into the Xcode project and drop it immediately under the project name
Close the Xcode project
Create a file called Package.swift and place it in the App folder. Edit it to have an empty package content. This ensures Xcode won’t display that folder in the source navigator under the package header.
import PackageDescription
let package = Package(
name: "",
products: [],
dependencies: [],
targets: []
)
Open the Xcode project. You should have top-level project name* Foo*. Under that will be the package, also named Foo. Then there will be the app target, also named Foo. You can rename the app target folder Foo to something like iOS if you want to have other targets for other platforms like macOS.
I am making a SpriteKit game and I am trying to change the cursor image from the default pointer to a png image that I have imported into the project, but it’s not really working. when I run the project I can see the cursor image change for a brief second and then return to the default image. Here is my code:
print(NSCursor.current)
if let image = NSImage(named: customImage) {
print("The image exists")
cursor = NSCursor(image: image, hotSpot: .zero)
cursor.push()
print(cursor)
}
print(NSCursor.current)
The above code is all contained in the didMove(:) function in GameScene. From the print statements I can see that the memory address of the NSCursor.current changes to that of cursor. HOWEVER, in the mouseMoved(:) call back function I print out the mouse location and the current cursor. I can see from these print stamens that the cursor memory address has again changed and no longer matches the custom cursor address… so I am not sure what is going on…
Also, fyi the cursor is a global property within game scene so it should persist. Also, image is not nil. This is verified by the print statements I see
Thanks
I am using TXLiteAVSDK_Player package dependencies and now when I want to archive and distribute an app I get this two warnings :
Upload Symbols Failed The archive did not include a dSYM for the TXFFmpeg.framework with the UUIDs [1xxxxxx]. Ensure that the archive's dSYM folder includes a DWARF file for TXFFmpeg.framework with the expected UUIDs.
Upload Symbols Failed The archive did not include a dSYM for the TXSoundTouch.framework with the UUIDs [Axxxx]. Ensure that the archive's dSYM folder includes a DWARF file for TXSoundTouch.framework with the expected UUIDs.
My system is macOS sonoma 14.5 and Xcode is 16.0. How can I eliminate this warning?
I'm building an iOS app using Xcode, and I frequently modify code within a package. To do this, I drag and drop a locally cloned version of the package into my project as a local package. However, when working across multiple branches of my app, I need different versions of the package as well. To achieve this, I use Git worktrees to create branch-specific copies of the package. Unfortunately, when I drag and drop these worktree copies into Xcode, the IDE doesn't seem to recognize them. Could you kindly guide me on resolving this?
Hi community:
I'm coming across two error types on Xcode cloud during ui tests.
encountered an error (Early unexpected exit, operation never finished bootstrapping - no restart will be attempted. (Underlying Error: Test crashed with signal kill before starting test execution. If you believe this error represents a bug, please attach the result bundle at /Volumes/workspace/resultbundlexcresult))
encountered an error (Failed to prepare device 'Clone 7 of iPhone 14 Pro' for impending launch. If you believe this error represents a bug, please attach the result bundle at /Volumes/workspace/ resultbundle.xcresult. (Underlying Error: Unable to boot the Simulator. launch failed to respond. (Underlying Error: Failed to start launch_ sim: could not bind to session, launchd_sim may have crashed or quit responding)))
Any ideas on how to solve them?
Thanks
So yesterday I noticed that some of images in my app stopped appearing. All of them were placed in the on-demand resources, that is I've assigned tags to them. Up until yesterday they were working correctly but since then whenever I try to load the assets I get the error message saying:
Error Domain=NSCocoaErrorDomain Code=4099 "Connection invalidated to streaming unzip service."
My code for loading the asset is pretty straightforward and it was working perfectly fine before:
let resourceRequest = NSBundleResourceRequest(tags: ["vinyl"])
defer {
resourceRequest.endAccessingResources()
}
do {
if await !resourceRequest.conditionallyBeginAccessingResources() {
try await resourceRequest.beginAccessingResources()
}
} catch {
assertionFailure("Vinyl image was not available, error: \(error)")
}
I'm obviously using it in asynchronous context, but the old method which used completion handler gives me the same error.
Has anyone encountered this issue as well? I just updated my devices to iOS 15.5, maybe that update broke on-demand resources?
Tested on Xcode 13.4, iOS 15.5
I'm currently setting up a new build environment.
Our old build server was a small M1 Mac Mini set up to build our, up-to-now, only app.
Since we now have a second app (and probably more to come) and have some issues building all on the same mac (gem versions, flutter versions, etc.) I now set up a virtualized build environment.
I'm currently running a MacOS 15 beta 3 VM on a M2 Mac Mini also on MacOS 15 beta 3.
The VM has all basic tools inside, including Xcode 16 beta 3 and is logged in with our AppleID for our build server.
The VM is spun up with a non-persistent storage before the build, installs the gem's/flutter in the correct version, checks out the code and builds it.
Now I'm getting an error on the build, that I have to select a development team, but he team is already selected and the build is working fine on the old M1 Mac Mini.
It is suggested to login to Xcode, so I logged in to Xcode in the VM, which itself was successful, but when loading the teams I'm getting the following error:
Decoding Error
There was a failure decoding response: (HTTP 401, 60 bytes)
The data couldn't be read because it isn't in the correct format..
The account info is correct, it's the same account that is logged in to MacOS and it's the same account from the old build system.
I guess this somehow has to do with the MacOS VM (before MacOS 15 it was not possible to signin to an apple id, this is also the reason why I use the beta).
Is there any chance to fix this and get it working?
We are not able to use Xcode cloud builds (or any other cloud services) due to corporate policies.
We are also not able to buy a dedicated Mac Mini for every app (too expensive and too much time needed for maintenance).
Greetings all,
Would anyone be able to assist and tell me why on my desktop Button( auto completion shows 1 choice
() (action: @escaptin () -> Void, label: @escaping () -> Label)
while on my laptop, Button( auto completion shows more choices, starting with
I (_ configuration:)
I (action:label:)
etc....
How do I get my desktop to act like my laptop?
I have an old app that I just got a notice will be pulled form the App Store if I don't upgrade. I tried to open in Xcode but it says I need to use Xcode 10.1 to convert to Swift 4.
Exact message - "Use Xcode 10.1 to migrate the code to Swift 4."
I downloaded Xcode 10.1 , now the OS (Sequoia ) says can't do it, have to use the latest version of Xcode.
Exact message - "The version of Xcode installed on this Mac is not compatible with macOS Sequoia. Download the latest version for free from the App Store."
Any experience with this and suggestions would be greatly appreciated.
I try to do unit test for an ES system extension's code using Unit Testing Bundle.
the code tries to get NSEndpointSecurityMachServiceName in info.plist by [bundle objectForInfoDictionaryKey:] but failed.
I have added NSEndpointSecurityMachService to info.plist of Test Bundle, and I did check info.plist file in bundle, NSEndpointSecurityMachServiceName was listed there.