Running swift-models on Colab

I’m exploring Examples from swift-models repository, particularly trying to run Mobilenet-Imagenette on Google Colab. I just opened a blank notebook as mentioned on tensorflow/swift repo and haven’t installed any packages.

What are the necessary steps I should follow to start using swift-models on colab? As I can see from many notebooks, there are some initial %install '.package...' commands.

Without any installation, trying to run following code throws error: use of unresolved identifier for ImagenetteBatchers

import Datasets
import ImageClassificationModels
import TensorFlow

let batchers = ImagenetteBatchers(
    inputSize: .resized320, 
    outputSize: 224, 
    batchSize: 64,
    numWorkers: 8
)
1 Like

To install the necessary packages, we have some specific commands that must be run in the very first cell before any other Swift code has been executed (if it has, restart your kernel to make sure it runs first). For running MobileNet with Imagenette, I believe you’ll need the following:

%install-location $cwd/swift-packages
%install '.package(url: "https://github.com/tensorflow/swift-models.git", .branch("master"))' Datasets ImageClassificationModels

which will grab and build the Datasets and ImageClassificationModels modules from swift-models and make them ready for use in the remaining notebook. You can then run the remainder of your code.

There are many examples of this in notebooks, but one I can think of right now is the FastStyleTransfer notebook demo project that was contributed to swift-models recently.

Speaking of Colab, we’re about to roll out the 0.8 toolchain to Colab and will have some exciting announcements at the March 20 Swift for TensorFlow open design meeting, so make sure to sign up to the mailing list to get the public meeting invitation.

2 Likes

Thanks! Now I got to know how to install dependencies. However, it seems like there are some issues with ImageClassificationModels. While installing the same, it caused following error:

/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNet.swift:36:99: error: extra argument 'useBias' in call
        self.conv = Conv2D(filterShape: filterShape, strides: strides, padding: padding, useBias: false)
                                                                                                  ^~~~~
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNetV2.swift:44:22: error: extra argument 'useBias' in call
            useBias: false)
                     ^~~~~
[2/55] Compiling SwiftProtobuf BinaryDecoder.swift
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNet.swift:36:99: error: extra argument 'useBias' in call
        self.conv = Conv2D(filterShape: filterShape, strides: strides, padding: padding, useBias: false)
                                                                                                  ^~~~~
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNetV2.swift:44:22: error: extra argument 'useBias' in call
            useBias: false)
                     ^~~~~
[3/55] Compiling SwiftProtobuf BinaryDecodingError.swift
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNet.swift:36:99: error: extra argument 'useBias' in call
        self.conv = Conv2D(filterShape: filterShape, strides: strides, padding: padding, useBias: false)
                                                                                                  ^~~~~
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNetV2.swift:44:22: error: extra argument 'useBias' in call
            useBias: false)
                     ^~~~~
[4/55] Compiling SwiftProtobuf BinaryDecodingOptions.swift
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNet.swift:36:99: error: extra argument 'useBias' in call
        self.conv = Conv2D(filterShape: filterShape, strides: strides, padding: padding, useBias: false)
                                                                                                  ^~~~~
/content/swift-packages/package/.build/checkouts/swift-models/Models/ImageClassification/ResNetV2.swift:44:22: error: extra argument 'useBias' in call
            useBias: false)

Should I open an issue for this?

So excited for this one, guessing it could be the ability of creating new swift notebooks directly from Colab.

This means you do not have recent nightlies, on which swift-models master depend.

How can I install them on Colab? I’ve it installed on my local machine, but don’t know whether I could change environment variables (PATH) of Colab.

Not sure, maybe @bradlarson can tell us. But once the 0.8 release is there, this code will work.

As Sylvain said, this is due to recent changes in the swift-apis that made bias optional for convolution layers. This was recently updated in the ImageClassificationModels at head, because we expect that Colab will be migrated to version 0.8 of the toolchain within the next few days. When Colab is running 0.8, we’ll announce it in the mailing list.

If you still want to experiment with the models in Colab before the toolchain is updated, you can change .branch("master") in the above to .branch("tensorflow-0.7"). It won’t have all the updates in the 0.7->0.8 period, but should work just fine with the 0.7 toolchain in Colab.

Regarding creating Swift notebooks directly from Colab, they recently added the ability to use the link https://colab.research.google.com/notebook#create=true&language=swift to create blank Swift notebooks. Support in the Colab creation menu is still being worked on, to my understanding.

1 Like

when i try to run course-v3 (swift) using colab I’ve got this error
unrecognized runtime “swift”; defaulting to “python3”.

please help me to get the solution

Unfortunately, as a consequence of investment in Swift for TensorFlow winding down at Google, Colab no longer supports running Swift code. I’m sorry about that, I know it was an extremely convenient way to experiment with differentiable Swift on accelerators.

You can still spin up a local Jupyter notebook environment and use the Swift Jupyter kernel to run these Swift notebooks with an existing Swift for TensorFlow toolchain or experiment with the latest advances in differentiable Swift using some of the latest toolchains from Swift.org.

1 Like

Thank you Brad