Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Transformation performance test"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def transform_apply(tensor, transform):\n",
- " feat_tensor = []\n",
- " org_shape = tensor.shape\n",
- " for plane in tensor[0]:\n",
- " feat_tensor.append(transform(plane))\n",
- " ret = np.concatenate(feat_tensor)\n",
- " ret = ret.reshape(org_shape)\n",
- " return ret"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "BD_transforms = [\n",
- " lambda f:f, \n",
- " lambda f:np.rot90(f,1), \n",
- " lambda f:np.rot90(f,2), \n",
- " lambda f:np.rot90(f,3), \n",
- " lambda f:np.fliplr(f), \n",
- " lambda f:np.flipud(f), \n",
- " lambda f:np.transpose(f), \n",
- " lambda f:np.fliplr(np.rot90(f, 1))\n",
- "]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "BD_fasttrans = [\n",
- " lambda f: f,\n",
- " lambda f: np.rot90(f, 1, axes=(2,3)),\n",
- " lambda f: np.rot90(f, 2, axes=(2,3)),\n",
- " lambda f: np.rot90(f, 3, axes=(2,3)),\n",
- " lambda f: f[:, :, :, ::-1],\n",
- " lambda f: f[:, :, ::-1, :],\n",
- " lambda f: np.transpose(f, axes=(0, 1, 3, 2)),\n",
- " lambda f: np.rot90(f, 1, axes=(2,3))[:, :, :, ::-1]\n",
- "]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "org 2.79942035675e-05\n",
- "fast 5.63967943192e-08\n",
- "times 496.379340448\n",
- "\n",
- "True\n",
- "org 0.00023686504364\n",
- "fast 4.14942979813e-06\n",
- "times 57.0837573266\n",
- "\n",
- "True\n",
- "org 0.000192616319656\n",
- "fast 3.43266010284e-06\n",
- "times 56.1128436505\n",
- "\n",
- "True\n",
- "org 0.000235383987427\n",
- "fast 4.21041965485e-06\n",
- "times 55.9051132007\n",
- "\n",
- "True\n",
- "org 6.21682882309e-05\n",
- "fast 3.42023849487e-07\n",
- "times 181.765945048\n",
- "\n",
- "True\n",
- "org 6.14371061325e-05\n",
- "fast 3.40197086334e-07\n",
- "times 180.592687593\n",
- "\n",
- "True\n",
- "org 5.45855045319e-05\n",
- "fast 5.8767914772e-07\n",
- "times 92.8831739966\n",
- "\n",
- "True\n",
- "org 0.000263137102127\n",
- "fast 4.56223011017e-06\n",
- "times 57.6772972368\n",
- "\n"
- ]
- }
- ],
- "source": [
- "for i in range(0, 8):\n",
- " t = np.random.randint(low=0, high=2, size=[1,49,19,19])\n",
- " transform = BD_transforms[i]\n",
- " fasttrans = BD_fasttrans[i]\n",
- " \n",
- " # sanity check\n",
- " r1 = transform_apply(t, transform)\n",
- " r2 = fasttrans(t)\n",
- " sanity = (r1 == r2).all()\n",
- " print sanity\n",
- " assert sanity, \"The transformation is not same\"\n",
- " \n",
- " # time check\n",
- " t1 = %timeit -oq transform_apply(t, transform)\n",
- " t2 = %timeit -oq fasttrans(t)\n",
- " \n",
- " print \"org \", t1.best\n",
- " print \"fast \", t2.best\n",
- " print \"times\", t1.best / t2.best \n",
- " print"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment