How to create MLFeatureProvider class for vision framework - swift

I am new to CoreML, and am having difficulties with turning a MLMultiArray (named modelInput) into the required type MLFeatureProvider to feed as a parameter when using myMLModel.prediction(from: modelInput). The error reads:
Argument type 'MLMultiArray' does not conform to expected type 'MLFeatureProvider'
From what I've read, I believe I have to create a class that subclasses 'MLFeatureProvider' that allows me to initialize modelInput as an 'MLFeatureProvider'. But I'm stuck on how to do this.
Are these files generated by Xcode, as suggested by this article? Or must I create these on my own?
Any input is appreciated.
//function inside of Predictor class
func makePrediction(){
let model: MLModel = configureModel(url: url)
let poseMultiArrays = [MLMultiArray] = getPoseMultiArrays()
let modelInput = MLMultiArray(concatenating: poseMultiArrays, axis: 0, dataType: .float)
//Perform prediction
var prediction: MLFeatureProvider?
do{
prediction = try? model.prediction(from: modelInput) //< The error occurs here
}catch{print(error)}
}

You can certainly make an MLFeatureProvider subclass, but you don't need to.
First off, your code snippet is not using the Vision framework but Core ML. Xcode automatically generates a class for you that creates the MLFeatureProvider. It's a good idea to use that class instead of using the MLModel directly, since the automatically generated class hides all the boilerplate from you.
If you still want to create your own MLFeatureProvider, the easiest solution is to first make an MLFeatureValue object containing the MLMultiArray, and then pass that into an MLDictionaryFeatureProvider.

Related

Get placeholder text of XCUIApplication().view.searchFields.element

This is a swift question.
I have a question on getting the placeholder (or id, etc.) from a line XCUIApplication().someView.searchField.element.
I need to achieve this in order to get the text attributes of the elements that are tap()-ed in a test code.
Below is the exact code, gotten from the https://github.com/BalestraPatrick/Tweetometer/blob/a11c20244a37eacd4a36586f679f3ba184c66f86/Carthage/Checkouts/IGListKit/Examples/Examples-iOS/IGListKitExamples-UITests/SearchViewControllerUITests.swift.
import XCTest
final class SearchViewControllerUITests: UITestCase {
var collectionViews: XCUIElementQuery!
...
func test_whenSearchingForText_thatResultsGetFiltered() {
let searchField = collectionViews.searchFields.element <- ex) this element's placeholder.
searchField.tap()
searchField.typeText("tac")
let tacos = collectionViews.cells.staticTexts["tacos"]
let small = collectionViews.cells.staticTexts["small"]
XCTAssertTrue(tacos.exists)
XCTAssertFalse(small.exists)
}
}
The full code is available in the above link, but I am very new to Swift, so that I have no idea where to find the view and the element's site of declaration (something like .xml in android code?)
Actually, I am curious if this is possible in a static way (i.e. only looking at the code, not really exploring through the GUI).
Thanks in advance for any kind of hint!

Hiding property setters by class in Swift

I would like to hide some property setters and initializers on my Swift model objects. These are reference data that the server provides, and under no circumstances should they be created or modified by the application. This is simple enough in Swift.
However, there is application in my project (a separate target) that needs to break this rule. It is a tool I use to populate the data in bulk, so of course needs to be able to initialize new model objects and set their properties.
What are my options for accomplishing this? I would rather not use a completely new project since it will mean a lot of code duplication. Is there some language-level way to keep this mutability hidden from one application but available to another?
If you declare a property with the let keyword. It can then only be set in the init of the type.
You can also declare a private setter to make the property readonly from the caller of the type but read/write inside the type
struct Foo {
private(set) var bar: Bool = true
func toggle() {
bar.toggle()
}
}
var foo = Foo()
let barState = foo.bar // This works
foo.toggle() // This works too
foo.bar.toggle() // This will make a compile time error

Circular Reference between 2 classes

I have a problem trying to solve this circular reference.
First I have two NSManagedObject ACoreData and BCoreData
I want to separate the model from the DataBase layer to the UI Model.
So, I create A and B classes which those will be in the UI.
I have created a protocol (Convertible) that ACoreData and BCoreData will implement to convert to the ui objects.
So far so good, but now I have a problem. Each time I call convert from ACoreData it will create a new A and it will assign the BCoreData converted, but then the BCoreData object will call convert again for the A object. I will end up with a loop calling convert() each other.
This is the code:
protocol Convertible{
associatedtype T
func convert() -> T
}
class ACoreData: Convertible{
var b: BCoreData?
func convert() -> A {
var a = A()
a.b = self.b?.convert()
return a
}
}
class BCoreData: Convertible{
var a: ACoreData?
func convert() -> B {
var b = B()
b.a = self.a?.convert()
return b
}
}
class A{
var b: B?
}
class B{
var a: A?
}
Do you know how can I solve this problem to avoid the loop in this circle reference?
Thanks in advance.
I suggest that the best solution is don't do this. You're adding a lot of complexity to your code for no real advantage. Weigh the benefits you see for keeping managed objects away from the UI against the additional complexity of needing to convert to/from managed objects all the time, copying values from one to the other any time one of them changes, the memory hit of keeping duplicate copies of data around, and probably other stuff I haven't thought of yet. "Clean" architecture is going to cost you a lot in terms of maintainability and performance.
If you don't want the UI to know about managed objects, define a protocol that your managed objects adopt. Make the UI work with "things that implement the protocol" instead of managed objects. Keeping the UI unaware of the details of the data store does not require duplicate data models.

How to make basic bindings in ReactiveCocoa 3 and 4

I've been reading up on ReactiveCocoa v3 lately and I'm struggling with just setting up basic stuff. I've already read the changelog, the tests, the few SO questions and the articles by Colin Eberhardt on the subject. However, I'm still missing examples on basic bindings.
Let's say I have an app that presents the menu of the day. The app is using RAC3 and the MVVM pattern.
Model (Menu)
The model has one simple method for fetching todays menu. As for now, this don't do any network requests, it basically just creates a model object. The mainCourse property is a String.
class func fetchTodaysMenu() -> SignalProducer<Menu, NoError> {
return SignalProducer {
sink, dispoable in
let newMenu = Menu()
newMenu.mainCourse = "Some meat"
sendNext(sink, newMenu)
sendCompleted(sink)
}
}
ViewModel (MenuViewModel)
The view model exposes different String variables for letting the view controller show the menu. Let's just add one property for showing the main course.
var mainCourse = MutableProperty("")
And we add a binding for this property:
self.mainCourse <~ Menu.fetchTodaysMenu()
|> map { menu in
return menu.mainCourse!
}
ViewController (MenuViewController)
Last but not least, I want to present this main course in a view. I'll add a UILabel for this.
var headline = UILabel()
And finally I want to set the text property of that UILabel by observing my view model. Something like:
self.headline.text <~ viewModel.headline.producer
Which unfortunately does not work.
Questions
The method fetchTodaysMenu() returns a SignalProducer<Menu, NoError>, but what if I want this method to return a SignalProducer<Menu, NSError> instead? This would make my binding in my view model fail as the method now may return an error. How do I handle this?
As mentioned, the current binding in my view controller does not work. I've been playing around with creating a MutableProperty that represents the text property of the UILabel, but I never got it right. I also think it feels clumsy or verbose to have to create extra variables for each property I want to bind. This was not needed in RAC2. I intentionally also tried to avoid using DynamicProperty, but maybe I shouldn't? I basically just want to find the right way of doing RAC(self.headline, text) = RACObserve(self.viewModel, mainCourse);.
Any other feedback/guidance on how to make this basic setup is highly appreciated.
So, after writing this question Colin Eberhardt made a part 3 of his RAC3 blog post series which includes a interesting and very relevant example of using MVVM and RAC3. The post can be found here and the source code here.
Based on his work, I've managed to answer my own questions:
By taking a slightly different approach, I'm able to make the fetchTodaysMenu() return a SignalProducer<Menu, NSError> as wanted. Here's how what I then would do in my view model:
MenuService.fetchTodaysMenu()
|> observeOn(QueueScheduler.mainQueueScheduler)
|> start(next: { response in
self.mainCourse.put(response.mainCourse!)
}, error: {
println("Error \($0)")
})
It seems like there's no UIKit bindings yet as of RAC3 beta 4. Colin made some UIKit extensions himself to help him make these bindings I was looking for as well. These can be found here. Adding them to my project, made be able to do exactly what I wanted to:
self.mainCourse.rac_text <~ self.viewModel.mainCourse
Update May 25, 2015
After been working a lot more with ReactiveCocoa 3, I would like to answer 1) once again. By using catch, it's possible to do this in a more declarative manner. I ended up implementing a small helper function for this:
public func ignoreError<T: Any, E: ErrorType>(signalProducer: SignalProducer<T, E>) -> SignalProducer<T, NoError> {
return signalProducer
|> catch { _ in
SignalProducer<T, NoError>.empty
}
}
The function transforms any NSError to NoError making it possible to bind as I wanted to by doing MenuService.fetchTodaysMenu() |> ignoreError.
I open sourced my project as this might be a good starting point for others looking into ReactiveCocoa 3.0:
https://github.com/s0mmer/TodaysReactiveMenu
Update March 5, 2016
As highlighted in the comments, since Swift 2, the ignoreError function would now look like:
public func ignoreError() -> SignalProducer<Value, NoError> {
return flatMapError { _ in
SignalProducer<Value, NoError>.empty
}
}
Also, an extension library called Rex has also been made, where something similar has been added.

Access class property from instance?

I am not sure is this is correct behaviour or if its unintended. I have setup StealthFighter so that it returns a class type computed property variable called ammunition.
func globalTests() {
println("globalTests")
println("AMMUNITION: \(StealthFighter.ammunition)")
var myStealthFighter = StealthFighter()
println("MISSILES: \(myStealthFighter.missiles)")
println("AMMUNITION: \(myStealthFighter.ammunition)") // ERROR
}
class StealthFighter {
class var ammunition:Int {
return 500;
}
var missiles: Int = 5
}
When directly accessing the class StealthFighter this works fine and returns 500 as expected. But if I create and instance myStealthFighter and then try and access the class property on the instance I get the error: 'StealthFighter' does not have a member named 'ammunition' I can't find any mention of this, I am assuming from this that class properties are accessible only via the class? and not on any instances created from it? I just want to make sure I am understanding this correctly ...
EDIT:
So I have probably worded the type variable name wrong as it should probably be maxAmmunition to signify that StealthFighters can only take 500 rounds. I can see the point, if you want the maxAmmunition for the class then you ask the class.
As #Kreiri and #0x7fffffff points out it does seem that you can ask the instance what the class ammunition (or maxAmmunition) is by using dynamicType.
println("CLASS - AMMUNITION: \(StealthFighter.ammunition)")
var myStealthFighter = StealthFighter()
println("INSTA - AMMUNITION: \(myStealthFighter.dynamicType.ammunition)")
.
// OUTPUT
// CLASS - AMMUNITION: 500
// INSTA - AMMUNITION: 500
Your assumption is correct. Type variables are only meant to be accessed directly from the class. If you want to get at them from an instance, you can do so by accessing the dynamicType property on your instance, like so.
let theFighter = StealthFighter()
let missiles = theFighter.dynamicType.missiles
println(missiles)
However, I don't think that this is the correct approach for you to be taking here. Assuming that you want to have one class "StealthFighter", and possibly multiple instances of that class, each with the ability to have its own number of missiles independent of the others, you should probably make this an instance variable by simply ditching the class keyword.
dynamicType allows access instance’s runtime type as a value, so accessing class property from instance would look like this:
var myStealthFighter = StealthFighter()
myStealthFighter.dynamicType.ammunition
Works in playground, at least.
These properties are known as Type properties in swift. It should be called on its type ie class name, not on instance. Type properties holds same value across all the instances of the class just like static constant in C.
Querying and Setting Type Properties
Type properties are queried and set with dot syntax, just like instance properties. However, type properties are queried and set on the type, not on an instance of that type
Excerpt from : swift programming language
Swift 4:
var myStealthFighter = StealthFighter()
type(of: myStealthFighter).ammunition
Yes. This is a correct behaviour. These Type Properties can only be accessed over the Type and are not available on the instance itself.
In the Swift Book from Apple it is described in the section "Type Properties" (Page 205).
Swift Type Properties
“Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time"