\n", "

\n", "To pass the course, we should perform all the assigments. You can submit the notebooks of all the days to

\n", "Outline of the labs for the reamining days:\n", "

- \n",
"
- Data augmentation \n", "
- CIFAR challenge \n", "

\n", "

\n", "Once again Keras makes it very easy to do data-augmentation yourself. You simply use the \"ImageDataGenerator\" that allows to do real-time data augmentation on the CPU during training your model, read up on it's properties here: https://keras.io/preprocessing/image/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "However, if we overdo it, we will ruin the performance, imagine what it means for the discriminability between \"6\" and \"9\" if you augment your data with random 360 degree rotations for instance, it will be impossible to distinguish them. \n", "

\n", "Data-augmentation makes your model invariant with respect to certain transformations and you have to think carefully which ones are harmful and which might help for your type of data.\n", "

\n", "NOTE: If you use the Theano backend, remove the import and the first line of the train_model function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from keras.optimizers import SGD\n", "import tensorflow as tf\n", "\n", "#\n", "# A function that trains a given model.\n", "#\n", "# model - Keras model.\n", "# x - Train features.\n", "# y - Train labels.\n", "# batch_size - The number of samples for each mini-batch.\n", "# nb_epoch - The number of training epochs.\n", "# device - Either '/cpu:0' or '/gpu:0'.\n", "#\n", "def train_model(model, x, y, batch_size=32, nb_epoch=10, device='/cpu:0'):\n", " with tf.device(device):\n", " # Compile the model with a specific optimizer.\n", " model.compile(loss='categorical_crossentropy', optimizer=SGD(), metrics=['accuracy'])\n", "\n", " # Train the model.\n", " model.fit_generator(datagen.flow(x, y, batch_size=batch_size),\n", " samples_per_epoch=len(x), nb_epoch=nb_epoch, verbose=1)\n", " \n", " \n", "#\n", "# Evaluate a trained model.\n", "#\n", "# model - The trained model.\n", "# x - Test features.\n", "# y - Test labels.\n", "#\n", "def test_model(model, x, y):\n", " return model.evaluate(x, y, verbose=0)\n", "\n", "datagen = ImageDataGenerator(\n", " featurewise_center=False,\n", " featurewise_std_normalization=False,\n", " rotation_range=0.,\n", " width_shift_range=0.,\n", " height_shift_range=0.,\n", " horizontal_flip=False)\n", "\n", "datagen.fit(ctrainx)\n", "\n", "# Set a number of parameters for the training.\n", "batch_size = 32\n", "nb_epoch = 10\n", "# Train and test.\n", "train_model(convnet_model, ctrainx, trainy, batch_size, nb_epoch)\n", "score = test_model(convnet_model, ctestx, testy)\n", "# Show the results.\n", "print \"Test score: %.4f, test accuracy: %.4f\" %(score[0], score[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "\n", "

\n", "

\n", "The assignment consists of the following:\n", "

- \n",
"
- Train the network without data augmentation. \n", "
- Evaluate the influence of the forms of data augmentation in isolation and combination. \n", "
- An important tool to understand failure modes and problems, as well as success is plotting what is happening. Please plot augmented images from successful settings and failing settings to underpin your argumentation for proposing which data augmentations are most useful in this scenario. When do certain translations, rotations, flips, or shifts fail and can you explain why? \n", "

\n", "Here is a corresponding notebook here for Seya, a library that uses Keras to build advanced models: https://github.com/EderSantana/seya/blob/keras1/examples/Spatial%20Transformer%20Networks.ipynb\n", "

\n", "The corresponding assignment here consists of the following elements:\n", "

- \n",
"
- Copy the appropriate code from the mentioned notebook and download the cluttered MNIST dataset. \n", "
- Apply the dataset to both the standard ConvNet and the Spatial Transformer Network. \n", "
- Compare the performance and try to explain what the networks are learning using the lessons from day 2. \n", "
- Discuss how Spatial Transformer Networks compare to the data augmentation of the previous assignment. \n", "