{"cells":[{"metadata":{"_uuid":"b0bf6a737d05714de9248834da1e93ca8591d298","_cell_guid":"cc05273c-4bbc-4893-99bf-dc1f314758b9"},"cell_type":"markdown","source":"# Data Explorer and Model"},{"metadata":{"_uuid":"6c18dec627a662f4ce28a5b59f0adf3236489a98","_cell_guid":"c308eb4c-283c-4443-bafd-a00bb081dcb5"},"cell_type":"markdown","source":"* Visualize\n* Pre-Process\n* Create Model\n* Train\n* Evaluate"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport gc\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom skimage import io\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input/guess-the-correlation-1\"))\npath_img = '../input/guess-the-correlation-1/train_imgs/train_imgs/'\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"_uuid":"43b48119efd9f9056a38a6a23b243b6275ca160a","_cell_guid":"4f1138d9-875c-4115-9007-301fd58d3a3b"},"cell_type":"markdown","source":"## Read Dataset"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/guess-the-correlation-1/train_responses.csv')\nprint(df.shape)\ndf.head(5)","execution_count":2,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"f83acbaca989501471b7055210147d1f984d3412","_cell_guid":"58cf9bac-6b8a-415c-be25-e4900fa8a4d8","trusted":true},"cell_type":"code","source":"# Read image in Grayscale\ndef read_image(img):\n    return io.imread(path_img+img+'.png', as_grey=True)","execution_count":3,"outputs":[]},{"metadata":{"_uuid":"1cd6d8d2e961d193e8b1059d4b8c1750cc7a1d01","_cell_guid":"ff181e98-79e2-4550-ba5b-4c4587f9c505","trusted":true},"cell_type":"code","source":"img_sample_10 = df.sample(9)\n\ndef plt_df_images(images):\n    fig, m_ax = plt.subplots(3, 3, figsize = (15, 15))\n    m_ax = m_ax.flatten()\n    i = 0\n    for img in images:\n        m_ax[i].imshow(img, cmap = 'gray')\n        #m_ax[i].axis('off')\n        i = i + 1\n\nimages = [read_image(name) for name in img_sample_10.id]\nplt_df_images(images)\nprint(images[0].shape)","execution_count":4,"outputs":[]},{"metadata":{"_uuid":"e3d3d33692c02bfd0c6a0dad174e155ee0303ee4","_cell_guid":"aab00a9e-d868-4bf7-a6c2-fa29d30dd147","trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":5,"outputs":[]},{"metadata":{"_uuid":"a66856317eb46abdf02770b1d01881d046d6a4bb","_cell_guid":"6b09810e-28a3-4643-a040-69993654efdf"},"cell_type":"markdown","source":"## Transform Image in Matriz"},{"metadata":{"_uuid":"2d0631dfbd3de6648afdb76c6a509be541702802","_cell_guid":"c6396e81-477c-43a3-bc67-5d869888e054"},"cell_type":"markdown","source":"### Remove unnecessary information\n\nClean data is important. In this case, i will remove unnecessary pixels (bords) and transforma image in matriz with only minimal information of graphic."},{"metadata":{"_uuid":"72ab29d0f72acdda39b3d16ad08836b278f2fec8","_cell_guid":"f12bb87c-3c2b-47e6-8168-1f0de5fe59fa","trusted":true},"cell_type":"code","source":"def crop_img(img):\n    imp_crop = img[2:-21, 21:-2]\n    return imp_crop\n\nimages = [crop_img(read_image(name)) for name in img_sample_10.id]\nplt_df_images(images)\nprint(images[0].shape)","execution_count":6,"outputs":[]},{"metadata":{"_uuid":"11da31480c28ed4ff0bf7b53ec31260757e5e484","_cell_guid":"3cda451f-92ae-48f5-97bb-fe18f5272a24","trusted":true},"cell_type":"code","source":"def img2data(img):\n    img      = read_image(img)\n    img_crop = crop_img(img)\n    #gc.collect()\n    data     = np.array(img_crop, dtype = 'bool')\n    return data\n\nimg = img2data(df['id'][0])\nimg","execution_count":10,"outputs":[]},{"metadata":{"_uuid":"50f423c61139a51c55968c9faeb143817156ef54","_cell_guid":"035ac20d-2215-4a18-b01e-0b98f390ac0c","trusted":true},"cell_type":"code","source":"plt.imshow(img, cmap = 'gray')","execution_count":11,"outputs":[]},{"metadata":{"_uuid":"08f27af384c8ead7ca3df657edeb979022965ce9","_cell_guid":"059b9114-b4c9-4c05-9912-f33bb7ccb8c4"},"cell_type":"markdown","source":"## Build DataSet"},{"metadata":{"collapsed":true,"_uuid":"d47a6a41ab829b2668f31ff5269f238c2c70328c","_cell_guid":"a90af856-73ca-465e-8bdd-0aaedcd3c0e1","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":12,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"512e0c98000cc265dfc683337364aeef2d0484d1","_cell_guid":"e6641224-5770-4812-b9fb-6a4139eed303","trusted":false},"cell_type":"code","source":"# Read image to Matriz Transform (X)\nX = np.array([img2data(name) for name in df['id']])\nX.shape","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"1f4ff36f4d130537963391fc4d81050f351a3303","_cell_guid":"ee9d56c6-0532-4036-a48f-a24a30c4894d","trusted":false},"cell_type":"code","source":"# Read Correlation (y)\ny = df['corr']\ny[0:3]","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"2567a22fc5988c982eb860987c86a5d8444f2db5","_cell_guid":"0f3097c7-7467-46cc-bbb6-55646abeb824","trusted":false},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)\n(len(X_train), len(X_test), len(y_train), len(y_test))","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"f93fda5c9f755d73750f9d14182fd6d4c3084df3","_cell_guid":"251aae2f-f918-43a0-a4ed-6610ce9a27e2","trusted":false},"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 127, 127, 1)\nX_test = X_test.reshape(X_test.shape[0], 127, 127, 1)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"17db20a4c91d359e88ef54276213e5b422547a28","_cell_guid":"5342e6c6-9c89-4367-bc93-1dc3aa0e090a","trusted":false},"cell_type":"code","source":"X_train[0].shape","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"fceefb3bf9425f13f316dd8afdb537b416a124fc","_cell_guid":"0e61a895-3ad2-4090-be45-aed7c3d6cb09","trusted":false},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b85aac461ed141358a9792c8fe6c3d3598ce90c5","_cell_guid":"1a2c1faf-7f55-48fa-a7a1-375b21ee7184"},"cell_type":"markdown","source":"## Build Model CNN\n\nBuild a Keras CNN to first model test. (127x127) -> (-1..1)"},{"metadata":{"collapsed":true,"_uuid":"14cfca878c51c1472be7b9bc2727aa313c058e74","_cell_guid":"50d43c24-7d65-4cf9-b1aa-c05f03d6bba4","trusted":false},"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, MaxPooling2D, Flatten, AveragePooling2D\nfrom keras.layers import LSTM, Conv2D,Activation","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"d2e77fa7c18343c21e8a424adc5bf12edd757a76","_cell_guid":"069160a6-e88a-4bd4-b83d-e0e88cd73c86","trusted":false},"cell_type":"code","source":"## Base model with Minimal CNN\ndef base_model():\n  model = Sequential()\n\n  model.add(Conv2D(32, (3, 3), activation='relu', \n                   input_shape=X_train[0].shape))\n  model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))\n\n  model.add(Conv2D(64, (5, 5), activation='relu'))\n  model.add(AveragePooling2D(pool_size=(4, 4)))\n\n  model.add(Flatten())\n  model.add(Dense(256, activation='relu'))\n  model.add(Dense(128, activation='relu'))\n  model.add(Dense(1,   activation='linear'))\n\n  model.compile(loss='mean_squared_error', optimizer='adam')\n  return model\n\n# Model\nmodel = base_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"077aec08d4e5c6773e8c2eb608c0570c5e33ccd8","_cell_guid":"864f8dcc-6f41-48f2-89b4-b8757e0b4062","trusted":false},"cell_type":"code","source":"batch_size = 128\nnb_epoch   = 2\n\nhist = model.fit(X_train, y_train, \n                    batch_size=batch_size, \n                    epochs=nb_epoch, verbose=1, \n                    validation_data=(X_test, y_test) )","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"9b19de17e0daed2bffbbcf4fdcf6a6dd4e5926fa","_cell_guid":"53edf107-1096-44c7-9f84-e1a89f6e8489","trusted":false},"cell_type":"code","source":"model.save_weights('model.h5', overwrite=True)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"b654f996f51fb6d065e45fa0ec04ee701fcb828c","_cell_guid":"dc60e803-3116-4d21-8e6f-79b7244c044c","trusted":false},"cell_type":"code","source":"plt.plot(hist.history)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"a95024fd8882bb302aeac5fa9a505f81d53b118d","_cell_guid":"c0254375-e94d-4dda-b188-779a9e889b08","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"16241111dac851fbb8f22121bac80f532b939d46","_cell_guid":"2698620b-b781-41fd-adef-0cd6f7ac0388","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}