def transform(data, label): data = nd.moveaxis(data, 2, 0).astype("float32") / 255 label = label return data, label
def data_xform(data): """Move channel axis to the beginning, cast to float32, and normalize to [0, 1].""" return nd.moveaxis(data, 2, 0).astype('float32') / 255
def to_tensor(X): X = X.astype(np.float32) / 255.0 X = nd.moveaxis(X, 3, 1) return X
def accuracy(predictions, targets): predictions = nd.argmax(predictions, 1) return nd.mean(nd.equal(predictions, targets)).asscalar() * 100 x = [] y = [] names = [] for wbc_type in os.listdir('dataset'): if 'normal' in wbc_type: label = 0 elif 'tuberculosis' in wbc_type: label = 1 img = mx.image.imread('dataset/' + wbc_type).astype('float32') img = mx.image.imresize(img, 128, 128) names.append(wbc_type) x.append(nd.moveaxis(img, 2, 0).asnumpy()) y.append(label) x = nd.array(x) y = nd.array(y) x /= 255.0 print(x.shape) print(y.shape) plt.imshow(mx.nd.moveaxis(x[0], 0, 2).asnumpy()) plt.show() predictions = model.predict(x) print('Test Accuracy:', accuracy(predictions, y))
def moveaxis(tensor, source, target): return np.moveaxis(tensor, source, target)
def data_xform(data): return nd.moveaxis(data, 2, 0).astype('float32') / 255