torchbox.datasets package

Submodules

torchbox.datasets.mnist module

torchbox.datasets.mnist.read_mnist(rootdir, dataset='test', fmt='ubyte')

read mnist dataset

The data can be downloaded from http://yann.lecun.com/exdb/mnist/

Parameters
  • rootdir (str) – root directory path string of mnist dataset

  • dataset (str, optional) – dataset to be read, 'test' or 'train', by default ‘test’.

  • fmt (str, optional) – the dataset formation, 'ubyte' (original) or 'image' (image), by default ‘ubyte’.

Returns

XTensor

image data

YTensor

label data

Return type

list or tuple

Examples

Read and show digital MNIST images

_images/mnist.png

The results shown in the above figure can be obtained by the following codes.

import torchbox as tb

rootdir = '/mnt/d/DataSets/oi/dgi/mnist/pics/'
dataset = 'test'
X, Y = tb.read_mnist(rootdir=rootdir, dataset=dataset, fmt='image')
print(X.shape, Y.shape)

rootdir = '/mnt/d/DataSets/oi/dgi/mnist/official/'
dataset = 'train'
X, Y = tb.read_mnist(rootdir=rootdir, dataset=dataset, fmt='ubyte')
print(X.shape, Y.shape)
plt = tb.imshow([X[i] for i in range(0, 32)])
plt.show()

dataset = 'test'
X, Y = tb.read_mnist(rootdir=rootdir, dataset=dataset, fmt='ubyte')
print(X.shape, Y.shape)
plt = tb.imshow([X[i] for i in range(0, 32)])
plt.show()

# output
(10000, 28, 28) (10000,)
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)

Read and show Fasion MNIST images

_images/fashionmnist.png

The results shown in the above figure can be obtained by the following codes.

import torchbox as tb

rootdir = '/mnt/d/DataSets/oi/dgi/fashionmnist/official/'
dataset = 'train'
X, Y = tb.read_mnist(rootdir=rootdir, dataset=dataset, fmt='ubyte')
print(X.shape, Y.shape)

plt = tb.imshow([X[i] for i in range(0, 32)])
plt.show()

dataset = 'test'
X, Y = tb.read_mnist(rootdir=rootdir, dataset=dataset, fmt='ubyte')
print(X.shape, Y.shape)

plt = tb.imshow([X[i] for i in range(0, 32)])
plt.show()

torchbox.datasets.mstar module

torchbox.datasets.mstar.mstar_header(filepath)

read header information of mstar file

Parameters

filepath (str) – the mstar file path string.

Returns

header information dictionary.

Return type

dict

Examples

The following example shows how to read the header information.

import torchbox as tb

datapath = tb.data_path('mstar')
filepath = datapath + 'BTR70_HB03787.004'

header = tb.mstar_header(filepath)
for k, v in header.items():
    print(k, v)
torchbox.datasets.mstar.mstar_raw(filepath, ofmt='c')

load mstar raw data

Each file is constructed with a prepended, variable-length, Phoenix formatted (ASCII) header which contains detailed ground truth and sensor information for the specific chip. Following the Phoenix header is the data block. The data block is written in Sun floating point format and is divided into two blocks, a magnitude block followed by a phase block. Byte swapping may be required for certain host platforms. Tools for reading and manipulating the header information may be found at https://www.sdms.afrl.af.mil .

Parameters
  • filepath (str) – the data file path string.

  • ofmt (str, optional) – output data type formation, 'ap' for amplitude and angle, 'c' for complex, and 'r' for real and imaginary.

Returns

the raw data with size \({\mathbb C}^{H\times W}\) ('c'), \({\mathbb R}^{H\times W \times 2}\) ('r' or 'ap')

Return type

tensor

Examples

Read mstar raw amplitude-phase data and show in a figure.

_images/SHOW1_BTR70_HB03787.png

The results shown in the above figure can be obtained by the following codes.

import torchbox as tb
import matplotlib.pyplot as plt

filepath = datapath + 'BTR70_HB03787.004'
x = tb.mstar_raw(filepath, ofmt='ap')
print(x.shape, th.max(x), th.min(x))

plt.figure()
plt.subplot(121)
plt.imshow(x[..., 0])
plt.title('amplitude')
plt.subplot(122)
plt.imshow(x[..., 1])
plt.title('phase')
plt.show()

Read mstar raw complex-valued data and show in a figure.

_images/SHOW_BTR70_HB03787.png

The results shown in the above figure can be obtained by the following codes.

import torchbox as tb
import matplotlib.pyplot as plt

filepath = datapath + 'BTR70_HB03787.004'
x = tb.mstar_raw(filepath, ofmt='c')
print(x.shape, th.max(x.abs()), th.min(x.abs()))

plt.figure()
plt.subplot(221)
plt.imshow(x.real)
plt.title('real part')
plt.subplot(222)
plt.imshow(x.imag)
plt.title('imaginary part')
plt.subplot(223)
plt.imshow(x.abs())
plt.title('amplitude')
plt.subplot(224)
plt.imshow(x.angle())
plt.title('phase')
plt.show()

torchbox.datasets.signals module

class torchbox.datasets.signals.CosineTask(ntask=10, arange=[0.1, 50.0], frange=[3.0, 10.0], prange=[0, 3.1415926], trange=[- 0.5, 0.5, 500], tmode='uniform', seed=None)

Bases: object

mktask(ntask=10, arange=None, frange=None, prange=None, trange=None, seed=None, device=None, rett=False)
torchbox.datasets.signals.cosine(t, alpha=1.0, freq=1.0, phi=0.0, reduction=None)

generates cosine signal

Parameters
  • t (Tensor) – the discretized time steps

  • alpha (float, tensor or list, optional) – amplitudes, by default 1.

  • freq (float, tensor or list, optional) – frequencies, by default 1.

  • phi (float, tensor or list, optional) – initial phases, by default 0.

  • reduction (str or None, optional) – 'mean', 'sum' or None

Module contents