{
"info": {
"author": "Elhanan Ilani",
"author_email": "elhanan.ilani@gmail.com",
"bugtrack_url": null,
"classifiers": [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
"Programming Language :: C++",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Scientific/Engineering"
],
"description": "# simnets-tf\n## SimNets implementation in TensorFlow\n\n### Binary installation\nBinary installation requires a cuda toolkit installation >= 7.5.
\nDownload the .whl file from the GitHub release tab, then type:\n```\npython -m pip install \n```\nall requirements should be installed automatically.\n\n### Building from Source\nBuilding from source requires:\n1. A working c++ compiler with c++11 support (gcc >= 4.7)\n2. Cuda toolkit installed (for nvcc)\n3. CMake >= 3.0 (apt install cmake)\n4. TensorFlow installed for the Python interpreter you intend to use\n\nImportant: The following command should run without error:\n```\npython -c 'import tensorflow as tf'\n```\nTo build the project type the following commands:
\n Python 2.7:
\n ```\n git clone --recursive https://github.com/HUJI-Deep/simnets-tf.git\n cd simnets-tf\n mkdir build\n cd build\n cmake .. -DCMAKE_BUILD_TYPE=Release -DSIMNETS_PYTHON_VERSION=2.7 -DCMAKE_INSTALL_PREFIX=install\n make -j simnet_ops\n ```\n\n Python 3.5:
\n ```\n git clone --recursive https://github.com/HUJI-Deep/simnets-tf.git\n cd simnets-tf\n mkdir build\n cd build\n cmake .. -DCMAKE_BUILD_TYPE=Release -DSIMNETS_PYTHON_VERSION=3.5 -DCMAKE_INSTALL_PREFIX=install\n make -j simnet_ops\n ```\n To test the code you can now type:\n ```\n make test_simnet_ops\n ```\n This should run for about two minutes and return without any errors.
\n Now you can create a .whl file:\n ```\n make create_wheel\n ```\n\n Finally, to install the simnets-tf package type (remember to use the right interpreter):\n ```\n cd install/dist\n python -m pip install \n ```\n The installation is successful if the following runs (again, remember to use the right interpreter):\n ```\n python -c 'import simnets'\n ```\n\n ### Usage example\n #### Keras\n ```python\nimport simnets.keras as sk\nimport keras\nfrom keras.datasets import mnist\nfrom keras.models import Model\nfrom keras.layers import Input, Flatten, AveragePooling2D, Lambda\nfrom keras import backend as K\nimport numpy as np\n\nbatch_size = 64\nnum_classes = 10\nsim_kernel = 2\nsim_channels = 32\nmex_channels = sim_channels\nepochs = 3\n\n# input image dimensions\nimg_rows, img_cols = 28, 28\n\n# the data, shuffled and split between train and test sets\n(x_train, y_train), (x_test, y_test) = mnist.load_data()\n\n\nx_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\nx_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\ninput_shape = (1, img_rows, img_cols)\n\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train = x_train / 255.0 - 0.5\nx_test = x_test / 255.0 - 0.5\n\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')\n\n# convert class vectors to binary class matrices\ny_train = keras.utils.to_categorical(y_train, num_classes)\ny_test = keras.utils.to_categorical(y_test, num_classes)\n\n\ndef sum_pooling_layer(x, pool_size):\n x = AveragePooling2D(pool_size=pool_size, padding='valid')(x)\n x = Lambda(lambda x: x * pool_size[0] * pool_size[1])(x)\n return x\n\n\na = Input(shape=(1, img_rows, img_cols))\nb = sk.Similarity(sim_channels,\n ksize=[2, 2], strides=[2, 2], similarity_function='L2',\n normalization_term=True, padding=[2, 2], out_of_bounds_value=np.nan, ignore_nan_input=True)(a)\nwhile b.shape[-2:] != (1, 1):\n mex_channels *= 2\n b = sk.Mex(mex_channels,\n blocks=[int(b.shape[-3]), 1, 1], strides=[int(b.shape[-3]), 1, 1],\n softmax_mode=True, normalize_offsets=True,\n use_unshared_regions=True, unshared_offset_region=[2])(b)\n b = sum_pooling_layer(b, pool_size=(2, 2))\n\nb = sk.Mex(num_classes,\n blocks=[mex_channels, 1, 1], strides=[mex_channels, 1, 1],\n softmax_mode=True, normalize_offsets=True,\n use_unshared_regions=True, shared_offset_region=[1])(b)\nb = Flatten()(b)\nmodel = Model(inputs=[a], outputs=[b])\n\nprint(model.summary())\n\ndef softmax_loss(y_true, y_pred):\n return K.categorical_crossentropy(y_pred, y_true, True)\n\nmodel.compile(loss=softmax_loss,\n optimizer=keras.optimizers.nadam(lr=1e-2, epsilon=1e-6),\n metrics=['accuracy'])\n\nsk.perform_unsupervised_init(model, 'kmeans', layers=None, data=x_train, batch_size=100)\n\nmodel.fit(x_train, y_train,\n batch_size=batch_size,\n epochs=epochs,\n verbose=1,\n validation_data=(x_test, y_test))\nscore = model.evaluate(x_test, y_test, verbose=0)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])\n\n```\n\n#### Low level\n```python\nimport tensorflow as tf\nfrom tensorflow.examples.tutorials.mnist import input_data\nfrom simnets import similarity\nfrom simnets.unsupervised import similarity_unsupervised_init\nimport matplotlib.pyplot as plt\n\nmnist = input_data.read_data_sets('MNIST_data', one_hot=True)\nsess = tf.InteractiveSession()\n\nx = tf.placeholder(tf.float32, shape=[None, 784])\nxr = tf.reshape(x, [-1, 1, 28, 28])\ny_ = tf.placeholder(tf.float32, shape=[None, 10])\n\nshape = [10, 1, 28, 28]\n\ntemplates = tf.Variable(tf.truncated_normal(shape, stddev=0.1))\nweights_var = tf.Variable(tf.truncated_normal(shape, stddev=0.1))\nweights = tf.abs(weights_var)\n\nsim = similarity(xr, templates, weights, similarity_function='L2', ksize=[28,28], strides=[28,28], padding=[0,0])\ny = tf.reshape(sim, [-1, 10])\n\nkmo_init, kmo = similarity_unsupervised_init('kmeans', sim, templates, weights_var)\n\ncross_entropy = tf.reduce_mean(\n tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n\ntrain_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n\nsess.run(tf.global_variables_initializer())\n\n\nfor idx in range(300):\n batch = mnist.train.next_batch(100)\n if idx == 0:\n kmo_init.run(feed_dict={x: batch[0], y_: batch[1]})\n kmo.run(feed_dict={x: batch[0], y_: batch[1]})\n if (idx + 1) % 100 == 0:\n print('kmeans', idx+1, '/', 1000)\n\ndef normalize(img):\n return (img - img.min()) / (img.max() - img.min())\n\ntemplates_np = tf.get_default_session().run(templates)\nplt.figure(1)\nfor i in range(10):\n plt.subplot(4,3, i+1)\n plt.imshow(normalize(templates_np[i,0,...]))\nplt.show()\n\nfor idx in range(1000):\n batch = mnist.train.next_batch(100)\n train_step.run(feed_dict={x: batch[0], y_: batch[1]})\n if (idx + 1) % 100 == 0:\n print(idx+1, '/', 1000)\n\ncorrect_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))\naccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\nprint('Accuracy:', accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))\n\n```\n\n\n\n",
"description_content_type": "",
"docs_url": null,
"download_url": "",
"downloads": {
"last_day": -1,
"last_month": -1,
"last_week": -1
},
"home_page": "https://github.com/HUJI-Deep/simnets-tf",
"keywords": "tensorflow simnets machine-learning",
"license": "MIT",
"maintainer": "",
"maintainer_email": "",
"name": "simnets",
"package_url": "https://pypi.org/project/simnets/",
"platform": "",
"project_url": "https://pypi.org/project/simnets/",
"project_urls": {
"Homepage": "https://github.com/HUJI-Deep/simnets-tf"
},
"release_url": "https://pypi.org/project/simnets/0.0.1/",
"requires_dist": [
"tensorflow-gpu",
"keras"
],
"requires_python": "",
"summary": "SimNets implementation in tensorflow",
"version": "0.0.1"
},
"last_serial": 3970906,
"releases": {
"0.0.1": [
{
"comment_text": "",
"digests": {
"md5": "275cc8a46c62b234ab709e883da2a858",
"sha256": "2930edc4f0d8fe4abdf6d979438e682fbf53734ab8d301f29a58b0545838a397"
},
"downloads": -1,
"filename": "simnets-0.0.1-cp27-none-manylinux1_x86_64.whl",
"has_sig": false,
"md5_digest": "275cc8a46c62b234ab709e883da2a858",
"packagetype": "bdist_wheel",
"python_version": "cp27",
"requires_python": null,
"size": 1760478,
"upload_time": "2018-06-17T12:13:51",
"url": "https://files.pythonhosted.org/packages/4c/b7/4ba60509e5e87ad0e758dc7babc58cbaf28fe82562bdd2c3b8df5655d63f/simnets-0.0.1-cp27-none-manylinux1_x86_64.whl"
}
]
},
"urls": [
{
"comment_text": "",
"digests": {
"md5": "275cc8a46c62b234ab709e883da2a858",
"sha256": "2930edc4f0d8fe4abdf6d979438e682fbf53734ab8d301f29a58b0545838a397"
},
"downloads": -1,
"filename": "simnets-0.0.1-cp27-none-manylinux1_x86_64.whl",
"has_sig": false,
"md5_digest": "275cc8a46c62b234ab709e883da2a858",
"packagetype": "bdist_wheel",
"python_version": "cp27",
"requires_python": null,
"size": 1760478,
"upload_time": "2018-06-17T12:13:51",
"url": "https://files.pythonhosted.org/packages/4c/b7/4ba60509e5e87ad0e758dc7babc58cbaf28fe82562bdd2c3b8df5655d63f/simnets-0.0.1-cp27-none-manylinux1_x86_64.whl"
}
]
}