{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Assignment - AEs\n\nHi there! In this assignment, you will use autoencoders (or something else, if you want) to produce \"super-resolution\" version of images.\n\nSpecifically, you must upscale 32x32 images in color to 64x64 images in color (i.e. 4 times as many pixels).\n\nI have normalized the images in [0, 1] (for all color channels).\n\nTo get you started, I have provided a complete working example.\n\nWhen you are done, submit your results on the Kaggle webpage for this competition. If you do not like to show your score to everyone, you may either:\n\n1. Send your  .csv  to me, and I will then do it for you.\n1. Use an anonymous username on Kaggle.\n\nHowever, I suggest you use your real name, after all it is just meant as an exercise and it is more fun that way. You can submit 5 times every day, so you can experiment with some stuff without being \"locked in\"."},{"metadata":{},"cell_type":"markdown","source":"# Details\n\nThe metric used to score this assignment MSE (on the pixel level).\n\nMaking use of convolutional layers and autoencoders is heravily encouraged.\n\nBe sure to create a validation set to tune your model."},{"metadata":{},"cell_type":"markdown","source":"# Kaggle\n\nThe assignment uses Kaggle. You can use it to submit your predictions. It will then give you a score (based on some of the test data). The final score will be calculated using the other part of the test data (so you have no change of looking at the test data for the competition).\n\nYou can visit the webpage here: https://www.kaggle.com/c/aml-ae. You need to create an account (free, just need an email)."},{"metadata":{"trusted":false},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"# Load data (must be in same folder as this file, which it will be if you simply unzip the assignment).\n# Note that we don't have any y_test! This way you cannot \"cheat\"!\n\nx_train = np.load('x_train.npy')\nx_test = np.load('x_test.npy')\ny_train = np.load('y_train.npy')\n\nprint(x_train.shape, x_test.shape, y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"encoder = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(16, kernel_size=(3, 3), activation='relu', padding='same', strides=2, input_shape=(32, 32, 3)),\n    tf.keras.layers.Conv2D(8, kernel_size=(3, 3), activation='relu', padding='same', strides=2),\n], name='encoder')\n\nencoder.summary()\n\ndecoder = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2DTranspose(8, 3, strides=2, activation='relu', padding='same', input_shape=(8, 8, 8)),\n    tf.keras.layers.Conv2DTranspose(16, 3, strides=2, activation='relu', padding='same'),\n    tf.keras.layers.Conv2DTranspose(32, 3, strides=2, activation='relu', padding='same'),\n    tf.keras.layers.Conv2D(3, kernel_size=(3,3), activation='sigmoid', padding='same'),\n], name='decoder')\n\ndecoder.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"autoencoder = tf.keras.models.Sequential([encoder, decoder], name='autoencoder')\n\nautoencoder.compile(loss='mse', optimizer='adam')\n\nautoencoder.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"autoencoder.fit(x_train, y_train, epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"y_test_hat = autoencoder.predict(x_test)\ny_test_hat = y_test_hat.reshape(-1) # For Kaggle evaluation, we flatten the data.\ny_test_hat_pd = pd.DataFrame({\n    'Id': list(range(len(y_test_hat))),\n    'Predicted': y_test_hat,\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"# After you make your predictions, you should submit them on the Kaggle webpage for our competition.\n# You may also (and I recommend you do it) send your code to me (at tsdj@sam.sdu.dk).\n# Then I can provide feecback if you'd like (so ask away!).\n# Even better, you can upload your notebook on Kaggle!\n\n# Below is a small check that your output has the right type and shape\nassert isinstance(y_test_hat_pd, pd.DataFrame)\nassert all(y_test_hat_pd.columns == ['Id', 'Predicted'])\nassert len(y_test_hat_pd) == 10715136\n\n# If you pass the checks, the file is saved.\ny_test_hat_pd.to_csv('y_test_hat.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}