Working with cifar-10 in Swift

I’m having hard time reading cifar-10 dataset in swift, this is what I’m doing

public extension String {
    /**
     * Download a URL string
     */
    func download() -> String
    {
        let fname = Path(self).name
        request.urlretrieve(self, fname)
       return String(fname)!
    }
    /**
     * Pickle a filename
     */
    func load(_ encoding: String = "utf-8") -> PythonObject
    {
        return pickle.load(gzip.open(self), encoding: encoding)
    }
    /**
     * Untar filename
     */
    func untar()
    {
        let tar = tarfile.open(self, mode: "r:gz")
        tar.extractall()
        tar.close()
    }
}
let cifar10 = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"

cifar10.download().untar()

let data_batch_1 = pickle.load(Python.open("cifar-10-batches-py/data_batch_1", mode: "rb"), encoding:"bytes")

Now this data_batch_1 looks like this

{b'batch_label': b'training batch 1 of 5', b'labels': [6, ...., dtype=uint8), b'filenames':...}

How I could read this data_batch_1 dic?

data_batch_1.data
Fatal error: Could not access PythonObject member 'data': file /swift-base/swift/stdlib/public/Python/Python.swift, line 537
Current stack trace:
	frame #4: 0x00007f033275f287 $__lldb_expr505`main at <Cell 33>:1:1

Here is a dirty way

let keys1 = Python.list(data_batch_1.keys()) //# [b'batch_label', b'labels', b'data', b'filenames']

let batch_label = data_batch_1[keys1[0]]
let labels = data_batch_1[keys1[1]]
let data = data_batch_1[keys1[2]]
let filenames = data_batch_1[keys1[3]]

Appreciate if there is cleaner way to load all batches

There is an example on the official page of swift-models

1 Like