def view_classify(img: NDArray, ps: NDArray, version="MNIST"): ''' Function for viewing an image and it's predicted classes. ''' ps = ps.asnumpy().squeeze() fig, (ax1, ax2) = plt.subplots(figsize=(6, 9), ncols=2) ax1.imshow(img.asnumpy().squeeze()) ax1.axis('off') ax2.barh(np.arange(10), ps) ax2.set_aspect(0.1) ax2.set_yticks(np.arange(10)) if version == "MNIST": ax2.set_yticklabels(np.arange(10)) ax2.set_title('Class Probability') ax2.set_xlim(0, 1.1) plt.tight_layout()
def view_recon(img: NDArray, recon): ''' Function for displaying an image (as a PyTorch Tensor) and its reconstruction also a PyTorch Tensor ''' fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True) axes[0].imshow(img.asnumpy().squeeze()) axes[1].imshow(recon.asnumpy().squeeze()) for ax in axes: ax.axis('off') ax.set_adjustable('box-forced')
def stat_helper(name, array): """wrapper for executor callback""" import ctypes from mxnet.ndarray import NDArray from mxnet.base import NDArrayHandle, py_str array = ctypes.cast(array, NDArrayHandle) array = NDArray(array, writable=False) array.wait_to_read() elapsed = float(time.time()-stat_helper.start_time)*1000 # if elapsed>0.: # print (name, array.shape, ('%.1fms' % (elapsed,))) # stat_helper.start_time=time.time() array = array.asnumpy() print(name, array.shape, np.average(array), np.std(array), ('%.1fms' % (float(time.time()-stat_helper.start_time)*1000))) stat_helper.internal_shapes.append((name,array.shape))