{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"01bc373a-ac51-6b39-4d05-f2c14f95c053"},"source":"This script using ImageDataGenerator in Keras to do Image Augmentation and Data Amplification."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ad35fe66-b5a4-9a45-331c-491b9de8c5b2"},"outputs":[],"source":"import numpy as np \nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator # for data augmentation\nfrom matplotlib import pyplot\nimport csv"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2f311a62-c6e4-6106-80c3-2ebcdd31d721"},"outputs":[],"source":"# data preparation of training data\ntrain = open(\"../input/train.csv\").read()\ntrain = train.split(\"\\n\")[1:-1]\ntrain = [i.split(\",\") for i in train]\nX_train = np.array([[int(i[j]) for j in range(1,len(i))] for i in train])\ny_train = np.array([int(i[0]) for i in train])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"dfecb798-cb85-1239-001d-9917cd729133"},"outputs":[],"source":"# for the visualization, in this notebook, we only use the first 9 images\nX_train = X_train[0:9]\ny_train = y_train[0:9]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9c358936-9ee2-0703-48a6-77ef8d716aef"},"outputs":[],"source":"for i in range(0, 9):\n        pyplot.subplot(3,3,i+1)\n        pyplot.imshow(X_train[i].reshape((28, 28)))\npyplot.show()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2a40b690-1114-69e6-20db-b9800837d749"},"outputs":[],"source":"X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)\nX_train = X_train.astype('float32')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"cdb85e18-c566-3cf8-45be-24a8dd58f931"},"outputs":[],"source":"filename = \"new_image_data.csv\"\nnew_data = [] #store new images\nnew_label = [] #store the lable of new images"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ce0fd23e-ff7d-6ca7-5b12-1dfbabaf4f74"},"outputs":[],"source":"# more kinds of augmentation can be found at https://keras.io/preprocessing/image/\ndatagen = ImageDataGenerator(zoom_range=0.1,\n                             width_shift_range=0.3,\n                             height_shift_range=0.3,\n                             rotation_range=50)\ndatagen.fit(X_train)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"53f38598-5d39-26ac-0ae2-603022a8a376"},"outputs":[],"source":"#output new images into CSV\ndef write_to_csv(original_data, label, filename):\n    for i in range(0, len(original_data)):\n        pre_process = original_data[i].reshape((28*28,1))\n        single_pic = []\n        single_pic.append(label[0][i])\n        for j in range(0,len(pre_process)):\n            temp_pix = pre_process[j][0]\n            single_pic.append(temp_pix)\n        with open(filename,\"a\") as f:\n            f_csv = csv.writer(f, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)\n            f_csv.writerow(single_pic)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ab809d93-bc18-891a-52fb-bbba7b9ee925"},"outputs":[],"source":"number_of_batches = 100\nbatches = 0\n\nfor X_batch, Y_batch in datagen.flow(X_train, y_train, batch_size=10):\n    new_data.append(X_batch)\n    new_label.append(Y_batch) \n#         loss = model.train(X_batch, Y_batch)\n    batches += 1\n    if batches >= number_of_batches:\n        # we need to break the loop by hand because\n        # the generator loops indefinitely\n        break"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3b739df0-5c16-4dbc-6978-d348f25825cd"},"outputs":[],"source":"#show the new images\nfor i in range(0, 4):\n        pyplot.subplot(2,2,i+1)\n        pyplot.imshow(new_data[1][i].reshape((28, 28)))\n    # show the plot\npyplot.show()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6daca3f3-7de9-d07b-0d65-30f4aea71067"},"outputs":[],"source":"#write new images into CSV\nfor i in range(0,len(new_data)):\n    write_to_csv(new_data[i], new_label, filename)"}],"metadata":{"_change_revision":0,"_is_fork":false,"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.5.2"}},"nbformat":4,"nbformat_minor":0}