Note: SwiftCV package requires OpenCV installed in order to compile.
# Uncomment line below when using Colab (this installs OpenCV4)
# %system SwiftCV/install/install_colab.sh
%install-location $cwd/swift-install
%install '.package(path: "$cwd/SwiftCV")' SwiftCV
%install '.package(path: "$cwd/FastaiNotebook_08_data_block")' FastaiNotebook_08_data_block
Installing packages: .package(path: "/home/saeta/fastai_docs/dev_swift/SwiftCV") SwiftCV .package(path: "/home/saeta/fastai_docs/dev_swift/FastaiNotebook_08_data_block") FastaiNotebook_08_data_block With SwiftPM flags: [] Working in: /tmp/tmpvc9knc87/swift-install [1/13] Compiling FastaiNotebook_08_data_block 01_matmul.swift [2/13] Compiling FastaiNotebook_08_data_block 03_minibatch_training.swift [3/13] Compiling FastaiNotebook_08_data_block 02_fully_connected.swift [4/13] Compiling FastaiNotebook_08_data_block 05b_early_stopping.swift [5/13] Compiling FastaiNotebook_08_data_block 05_anneal.swift [6/13] Compiling FastaiNotebook_08_data_block 06_cuda.swift [7/13] Compiling FastaiNotebook_08_data_block 02a_why_sqrt5.swift [8/13] Compiling FastaiNotebook_08_data_block 00_load_data.swift [9/13] Compiling FastaiNotebook_08_data_block 08_data_block.swift [10/13] Compiling FastaiNotebook_08_data_block 04_callbacks.swift [11/13] Compiling FastaiNotebook_08_data_block 01a_fastai_layers.swift [12/13] Compiling FastaiNotebook_08_data_block 07_batchnorm.swift [13/14] Merging module FastaiNotebook_08_data_block [14/15] Compiling jupyterInstalledPackages jupyterInstalledPackages.swift [15/16] Merging module jupyterInstalledPackages [16/16] Linking libjupyterInstalledPackages.so Initializing Swift... Installation complete!
%include "EnableIPythonDisplay.swift"
import Foundation
import SwiftCV
import Path
import FastaiNotebook_08_data_block
// display opencv version
print(cvVersion())
4.1.0
func readImage(_ path:String)->Mat {
let cvImg = imread(path)
return cvtColor(cvImg, nil, ColorConversionCode.COLOR_BGR2RGB)
}
let path = downloadImagenette(sz:"")
let allNames = fetchFiles(path: path/"train/n03425413", recurse: false, extensions: ["jpeg", "jpg"])
let fNames = Array(allNames[0..<256])
let ns = fNames.map {$0.string}
let imgpath = ns[2]
var cvImg = readImage(imgpath)
Downloading https://s3.amazonaws.com/fast-ai-imageclas/imagenette.tgz...
cvImg.size
▿ 2 elements - 0 : 500 - 1 : 375
print(type(of:cvImg.dataPtr))
UnsafeMutablePointer<Int8>
let ptr = UnsafeRawPointer(cvImg.dataPtr).assumingMemoryBound(to: UInt8.self)
ptr[2]
111
time(repeating:10) {_ = readImage(imgpath)}
average: 3.2026426999999997 ms, min: 2.935113 ms, max: 3.296784 ms
cvImg.rows
500
import Python
import TensorFlow
let plt = Python.import("matplotlib.pyplot")
let np = Python.import("numpy")
IPythonDisplay.shell.enable_matplotlib("inline")
('inline', 'module://ipykernel.pylab.backend_inline')
func show_img(_ img: Mat, _ w: Int = 7, _ h: Int = 5) {
show_img(Tensor<UInt8>(cvMat: img)!, w, h)
}
show_img(cvImg)
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_NEAREST)}
average: 0.1347015 ms, min: 0.08907 ms, max: 0.22734 ms
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_LINEAR)}
average: 0.5949585999999999 ms, min: 0.529687 ms, max: 0.700341 ms
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)}
average: 0.5734125999999999 ms, min: 0.556511 ms, max: 0.622225 ms
time(repeating:10) {_ = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_AREA)}
average: 1.5651732000000003 ms, min: 1.542578 ms, max: 1.603373 ms
cvImg = resize(cvImg, nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)
func readResized(_ fn:String)->Mat {
return resize(readImage(fn), nil, Size(224, 224), 0, 0, InterpolationFlag.INTER_CUBIC)
}
var imgs = ns[0..<10].map(readResized)
time(repeating:10) {_ = readResized(imgpath)}
average: 3.9041422 ms, min: 3.708008 ms, max: 4.496613 ms
public protocol Countable {
var count:Int {get}
}
extension Mat :Countable {}
extension Array:Countable {}
public extension Sequence where Element:Countable {
var totalCount:Int { return map{ $0.count }.reduce(0, +) }
}
func collateMats(_ imgs:[Mat])->Tensor<Float> {
let c = imgs.totalCount
let ptr = UnsafeMutableRawPointer.allocate(byteCount: c, alignment: 1)
defer {ptr.deallocate()}
var p = ptr
for img in imgs {
p.copyMemory(from: img.dataPtr, byteCount: img.count)
p += img.count
}
let r = UnsafeBufferPointer(start: ptr.bindMemory(to: UInt8.self, capacity: c), count: c)
cvImg = imgs[0]
let shape = TensorShape([imgs.count, cvImg.rows, cvImg.cols, cvImg.channels])
let res = Tensor(shape: shape, scalars: r)
return Tensor<Float>(res)/255.0
}
var t = collateMats(imgs)
t.shape
▿ [10, 224, 224, 3] ▿ dimensions : 4 elements - 0 : 10 - 1 : 224 - 2 : 224 - 3 : 3
show_img(t[2])
time(repeating:10) {_ = collateMats(imgs)}
average: 1.9515162 ms, min: 1.213406 ms, max: 2.94692 ms
time { _ = ns.map(readResized) }
average: 1095.155756 ms, min: 1095.155756 ms, max: 1095.155756 ms
show_img(
resize(cvImg, nil, Size(100, 50), 0, 0, InterpolationFlag.INTER_AREA)
)
let zoomMat = getRotationMatrix2D(Size(cvImg.cols, cvImg.rows / 2), 0, 1)
show_img(
warpAffine(cvImg, nil, zoomMat, Size(600, 600))
)
let rotMat = getRotationMatrix2D(Size(cvImg.cols / 2, cvImg.rows / 2), 20, 1)
show_img(
warpAffine(cvImg, nil, rotMat, Size(cvImg.cols, cvImg.rows))
)
show_img(
copyMakeBorder(cvImg, nil, 40, 40, 40, 40, BorderType.BORDER_CONSTANT, RGBA(0, 127, 0, 0))
)
show_img(
GaussianBlur(cvImg, nil, Size(25, 25))
)
show_img(
flip(cvImg, nil, FlipMode.HORIZONTAL)
)
show_img(
transpose(cvImg, nil)
)