Skip to content

Instantly share code, notes, and snippets.

@aphor
Last active May 2, 2019 20:03
Show Gist options
  • Save aphor/209998c1daa55db11755d547a6133f31 to your computer and use it in GitHub Desktop.
Save aphor/209998c1daa55db11755d547a6133f31 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Keras capsule network example with plaidml-keras-0.6.0rc2\n",
"https://pypi.org/project/plaidml-keras/0.6.0rc2/\n",
"https://pypi.org/project/plaidml/0.6.0rc2/\n",
"https://github.com/keras-team/keras/blob/f0eb8d538c82798944346b4b2df917a06bf5e9d4/examples/cifar10_cnn_capsule.py"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"logging.getLogger().setLevel(logging.DEBUG)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import plaidml.keras\n",
"plaidml.keras.install_backend()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"This example trains a simple CNN-Capsule Network on the CIFAR10 data set.\n",
"\n",
"Without Data Augmentation:\n",
"It gets to 75% validation accuracy in 10 epochs, 79% after 15 epochs,\n",
"and overfitting after 20 epochs\n",
"\n",
"With Data Augmentation:\n",
"It gets to 75% validation accuracy in 10 epochs, 79% after 15 epochs,\n",
"and 83% after 30 epochs.\n",
"\n",
"The highest achieved validation accuracy is 83.79% after 50 epochs.\n",
"This is a fast implementation that takes just 20s/epoch on a GTX 1070 GPU.\n",
"\n",
"The paper \"Dynamic Routing Between Capsules\": https://arxiv.org/abs/1710.09829\n",
"\"\"\"\n",
"#from __future__ import print_function\n",
"\n",
"from keras import activations\n",
"from keras import backend as K\n",
"from keras import layers\n",
"from keras import utils\n",
"from keras.datasets import cifar10\n",
"from keras.models import Model\n",
"from keras.preprocessing.image import ImageDataGenerator"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def squash(x, axis=-1):\n",
" \"\"\"The Squashing Function.\n",
" The nonlinear activation function used in Capsule Network\n",
" # Arguments\n",
" x: Input Tensor.\n",
" axis: Integer axis along which the squashing function is to be applied.\n",
"\n",
" # Returns\n",
" Tensor with scaled value of the input tensor\n",
" \"\"\"\n",
" s_squared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon()\n",
" scale = K.sqrt(s_squared_norm) / (0.5 + s_squared_norm)\n",
" return scale * x\n",
"\n",
"\n",
"def margin_loss(y_true, y_pred):\n",
" \"\"\"Margin loss\n",
"\n",
" # Arguments\n",
" y_true: tensor of true targets.\n",
" y_pred: tensor of predicted targets.\n",
"\n",
" # Returns\n",
" Tensor with one scalar loss entry per sample.\n",
" \"\"\"\n",
" lamb, margin = 0.5, 0.1\n",
" return K.sum(y_true * K.square(K.relu(1 - margin - y_pred)) + lamb * (\n",
" 1 - y_true) * K.square(K.relu(y_pred - margin)), axis=-1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class Capsule(layers.Layer):\n",
" \"\"\"Capsule Network\n",
"\n",
" A Capsule Network Layer implementation in Keras\n",
" There are two versions of Capsule Networks.\n",
" One is similar to dense layer (for the fixed-shape input),\n",
" and the other is similar to time distributed dense layer\n",
" (for inputs of varied length).\n",
"\n",
" The input shape of Capsule must be (batch_size,\n",
" input_num_capsule,\n",
" input_dim_capsule\n",
" )\n",
" and the output shape is (batch_size,\n",
" num_capsule,\n",
" dim_capsule\n",
" )\n",
" The Capsule implementation is from https://github.com/bojone/Capsule/\n",
"\n",
"\n",
" # Arguments\n",
" num_capsule: An integer, the number of capsules.\n",
" dim_capsule: An integer, the dimensions of the capsule.\n",
" routings: An integer, the number of routings.\n",
" share_weights: A boolean, sets weight sharing between layers.\n",
" activation: A string, the activation function to be applied.\n",
" \"\"\"\n",
"\n",
" def __init__(self,\n",
" num_capsule,\n",
" dim_capsule,\n",
" routings=3,\n",
" share_weights=True,\n",
" activation='squash',\n",
" **kwargs):\n",
" super(Capsule, self).__init__(**kwargs)\n",
" self.num_capsule = num_capsule\n",
" self.dim_capsule = dim_capsule\n",
" self.routings = routings\n",
" self.share_weights = share_weights\n",
" if activation == 'squash':\n",
" self.activation = squash\n",
" else:\n",
" self.activation = activations.get(activation)\n",
"\n",
" def build(self, input_shape):\n",
" input_dim_capsule = input_shape[-1]\n",
" if self.share_weights:\n",
" self.kernel = self.add_weight(\n",
" name='capsule_kernel',\n",
" shape=(1, input_dim_capsule,\n",
" self.num_capsule * self.dim_capsule),\n",
" initializer='glorot_uniform',\n",
" trainable=True)\n",
" else:\n",
" input_num_capsule = input_shape[-2]\n",
" self.kernel = self.add_weight(\n",
" name='capsule_kernel',\n",
" shape=(input_num_capsule, input_dim_capsule,\n",
" self.num_capsule * self.dim_capsule),\n",
" initializer='glorot_uniform',\n",
" trainable=True)\n",
"\n",
" def call(self, inputs, **kwargs):\n",
" \"\"\"Following the routing algorithm from Hinton's paper,\n",
" but replace b = b + <u,v> with b = <u,v>.\n",
"\n",
" This change can improve the feature representation of the capsule.\n",
"\n",
" However, you can replace\n",
" b = K.batch_dot(outputs, hat_inputs, [2, 3])\n",
" with\n",
" b += K.batch_dot(outputs, hat_inputs, [2, 3])\n",
" to get standard routing.\n",
" \"\"\"\n",
"\n",
" if self.share_weights:\n",
" hat_inputs = K.conv1d(inputs, self.kernel)\n",
" else:\n",
" hat_inputs = K.local_conv1d(inputs, self.kernel, [1], [1])\n",
"\n",
" batch_size = K.shape(inputs)[0]\n",
" input_num_capsule = K.shape(inputs)[1]\n",
" hat_inputs = K.reshape(hat_inputs,\n",
" (batch_size, input_num_capsule,\n",
" self.num_capsule, self.dim_capsule))\n",
" hat_inputs = K.permute_dimensions(hat_inputs, (0, 2, 1, 3))\n",
"\n",
" b = K.zeros_like(hat_inputs[:, :, :, 0])\n",
" print(self.routings)\n",
" for i in range(self.routings):\n",
" c = K.softmax(b)\n",
" o = self.activation(K.batch_dot(c, hat_inputs, [2, 2]))\n",
" if i < self.routings - 1:\n",
" b = K.batch_dot(o, hat_inputs, [2, 3])\n",
" if K.backend() == 'theano':\n",
" o = K.sum(o, axis=1)\n",
" return o\n",
"\n",
" def compute_output_shape(self, input_shape):\n",
" return None, self.num_capsule, self.dim_capsule"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"batch_size = 128\n",
"num_classes = 10\n",
"epochs = 100\n",
"(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
"\n",
"x_train = x_train.astype('float32')\n",
"x_test = x_test.astype('float32')\n",
"x_train /= 255\n",
"x_test /= 255\n",
"y_train = utils.to_categorical(y_train, num_classes)\n",
"y_test = utils.to_categorical(y_test, num_classes)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:plaidml:Opening device \"metal_amd_radeon_rx_580.0\"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_1 (InputLayer) (None, None, None, 3) 0 \n",
"_________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, None, None, 64) 1792 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, None, None, 64) 36928 \n",
"_________________________________________________________________\n",
"average_pooling2d_1 (Average (None, None, None, 64) 0 \n",
"_________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, None, None, 128) 73856 \n",
"_________________________________________________________________\n",
"conv2d_4 (Conv2D) (None, None, None, 128) 147584 \n",
"_________________________________________________________________\n",
"reshape_1 (Reshape) (None, None, 128) 0 \n",
"_________________________________________________________________\n",
"capsule_1 (Capsule) (None, 10, 16) 20480 \n",
"_________________________________________________________________\n",
"lambda_1 (Lambda) (None, 10, 16) 0 \n",
"=================================================================\n",
"Total params: 280,640\n",
"Trainable params: 280,640\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# A simple Conv2D model\n",
"input_image = layers.Input(shape=(None, None, 3))\n",
"x = layers.Conv2D(64, (3, 3), activation='relu')(input_image)\n",
"x = layers.Conv2D(64, (3, 3), activation='relu')(x)\n",
"x = layers.AveragePooling2D((2, 2))(x)\n",
"x = layers.Conv2D(128, (3, 3), activation='relu')(x)\n",
"x = layers.Conv2D(128, (3, 3), activation='relu')(x)\n",
"\n",
"# Now, we reshape it to (batch_size, input_num_capsule, input_dim_capsule)\n",
"# then connect a capsule layer.\n",
"# The output of final model is the lengths of 10 capsules, which have 16 dimensions.\n",
"# The length of the output vector of the capsule expresses the probability of\n",
"# existence of the entity, so the problem becomes a 10 two-classification problem.\n",
"\n",
"x = layers.Reshape((-1, 128))(x)\n",
"capsule = Capsule(10, 16, 3, True)(x)\n",
"output = layers.Lambda(lambda z: K.sqrt(K.sum(K.square(z), 2)), output_shape=(10, 16))(capsule)\n",
"model = Model(inputs=input_image, outputs=output)\n",
"\n",
"# Margin loss is used\n",
"model.compile(loss=margin_loss, optimizer='adam', metrics=['accuracy'])\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Compare the performance with and without data augmentation\n",
"data_augmentation = False"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not using data augmentation.\n",
"Train on 50000 samples, validate on 10000 samples\n",
"Epoch 1/100\n"
]
},
{
"ename": "Unknown",
"evalue": "Applying function, tensor with mismatching dimensionality: I1, expected=7, got=2",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUnknown\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-03a94f542f3a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mepochs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mvalidation_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_test\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m shuffle=True)\n\u001b[0m",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/keras/engine/training.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)\u001b[0m\n\u001b[1;32m 1037\u001b[0m \u001b[0minitial_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minitial_epoch\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1038\u001b[0m \u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1039\u001b[0;31m validation_steps=validation_steps)\n\u001b[0m\u001b[1;32m 1040\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1041\u001b[0m def evaluate(self, x=None, y=None,\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/keras/engine/training_arrays.py\u001b[0m in \u001b[0;36mfit_loop\u001b[0;34m(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)\u001b[0m\n\u001b[1;32m 197\u001b[0m \u001b[0mins_batch\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mins_batch\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 198\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 199\u001b[0;31m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mins_batch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 200\u001b[0m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mto_list\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mouts\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mo\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mout_labels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mouts\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/plaidml/keras/backend.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 168\u001b[0m tensors = [\n\u001b[1;32m 169\u001b[0m \u001b[0mplaidml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_device\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_invoker\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_output_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_output_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m ]\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/plaidml/keras/backend.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 168\u001b[0m tensors = [\n\u001b[1;32m 169\u001b[0m \u001b[0mplaidml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_device\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_invoker\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_output_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_output_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m ]\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/plaidml/__init__.py\u001b[0m in \u001b[0;36mget_output_shape\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1428\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1429\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mget_output_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1430\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_Shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_ctx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_lib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplaidml_alloc_invoker_output_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1431\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1432\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mset_output\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/plaidml/__init__.py\u001b[0m in \u001b[0;36m_check_err\u001b[0;34m(self, result, func, args)\u001b[0m\n\u001b[1;32m 762\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfunc\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplaidml_map_buffer_current\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 763\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 764\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraise_last_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 765\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 766\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/src/capnet_eval.git/lib/python3.6/site-packages/plaidml/library.py\u001b[0m in \u001b[0;36mraise_last_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 130\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mraise_last_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 131\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlast_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 132\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 133\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_logger_callback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0munused_arg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mUnknown\u001b[0m: Applying function, tensor with mismatching dimensionality: I1, expected=7, got=2"
]
}
],
"source": [
"if not data_augmentation:\n",
" print('Not using data augmentation.')\n",
" model.fit(\n",
" x_train,\n",
" y_train,\n",
" batch_size=batch_size,\n",
" epochs=epochs,\n",
" validation_data=(x_test, y_test),\n",
" shuffle=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if data_augmentation:\n",
" print('Using real-time data augmentation.')\n",
" # This will do preprocessing and real-time data augmentation:\n",
" datagen = ImageDataGenerator(\n",
" featurewise_center=False, # set input mean to 0 over the dataset\n",
" samplewise_center=False, # set each sample mean to 0\n",
" featurewise_std_normalization=False, # divide inputs by dataset std\n",
" samplewise_std_normalization=False, # divide each input by its std\n",
" zca_whitening=False, # apply ZCA whitening\n",
" zca_epsilon=1e-06, # epsilon for ZCA whitening\n",
" rotation_range=0, # randomly rotate images in 0 to 180 degrees\n",
" width_shift_range=0.1, # randomly shift images horizontally\n",
" height_shift_range=0.1, # randomly shift images vertically\n",
" shear_range=0., # set range for random shear\n",
" zoom_range=0., # set range for random zoom\n",
" channel_shift_range=0., # set range for random channel shifts\n",
" # set mode for filling points outside the input boundaries\n",
" fill_mode='nearest',\n",
" cval=0., # value used for fill_mode = \"constant\"\n",
" horizontal_flip=True, # randomly flip images\n",
" vertical_flip=False, # randomly flip images\n",
" # set rescaling factor (applied before any other transformation)\n",
" rescale=None,\n",
" # set function that will be applied on each input\n",
" preprocessing_function=None,\n",
" # image data format, either \"channels_first\" or \"channels_last\"\n",
" data_format=None,\n",
" # fraction of images reserved for validation (strictly between 0 and 1)\n",
" validation_split=0.0)\n",
"\n",
" # Compute quantities required for feature-wise normalization\n",
" # (std, mean, and principal components if ZCA whitening is applied).\n",
" datagen.fit(x_train)\n",
"\n",
" # Fit the model on the batches generated by datagen.flow().\n",
" model.fit_generator(\n",
" datagen.flow(x_train, y_train, batch_size=batch_size),\n",
" epochs=epochs,\n",
" validation_data=(x_test, y_test),\n",
" workers=1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment