{"cells":[{"metadata":{"_uuid":"a694cc38e7afe6434b30e8ea48848f88517881db"},"cell_type":"markdown","source":"# Import general dependencies"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport os\nimport cv2\nimport glob\n\n\n%matplotlib inline\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"37a2e608f6c0325c60991e0d577f2c861e243d8e"},"cell_type":"markdown","source":"# Import Data"},{"metadata":{"trusted":true,"_uuid":"523883c375aeda54acedbebd2ed3fe927537b390"},"cell_type":"code","source":"def train_impath():\n    labels = pd.read_csv('../input/labels.csv')\n    lab = labels.iloc[:,0].tolist()\n\n    data_dir = []\n    for l in lab:\n        t_dir = \"../input/train/\"+l+\".jpg\"\n        data_dir.append(t_dir)\n    return data_dir","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f4677687bea97d9a15ad9dfd147259da1a9ebe9"},"cell_type":"code","source":"def test_impath():\n    labels = pd.read_csv('../input/sample_submission.csv')\n    lab = labels.iloc[:,0].tolist()\n\n    data_dir = []\n    for l in lab:\n        t_dir = \"../input/test/\"+l+\".jpg\"\n        data_dir.append(t_dir)\n    return data_dir","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a165556249f08b23edec9f008e356d642d41a41"},"cell_type":"code","source":"train_dir = train_impath()\ntest_dir = test_impath()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"597403bb81404bc7ea49660cde052ca4aea646b5"},"cell_type":"code","source":"# data: str-> name of the dataSet\ndef im_read(data_dir):\n    df = []\n    for path in data_dir[0:100]:\n        img = cv2.imread(path,0)\n        img = cv2.resize(img,(80,80)).reshape(80,80,1)\n        df.append(img.tolist())\n    return np.array(df, dtype=float).reshape(-1,80,80,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"702b642e4c81b6cb3fc063324049baef79fb231f"},"cell_type":"code","source":"t = im_read(train_dir) #test dataSet\nts = im_read(test_dir) #train dataSet","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b8eeda995271057c0fee5c73c3e96711d3a4af5"},"cell_type":"markdown","source":"# Data preparation"},{"metadata":{"trusted":true,"_uuid":"ec3a7755ad50e8a1884f5959eb568bec4f4d47c3"},"cell_type":"code","source":"lebels = pd.read_csv('../input/labels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92929a290a8c77dbbef6cf6b61779763cfef68e7"},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder, LabelEncoder\nle = LabelEncoder()\nenc = OneHotEncoder()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7202da2b98469bc0f5c094d0a6601d4d8369b6b2"},"cell_type":"code","source":"#Labels\nleb = lebels.iloc[:,1].value_counts().index.sort_values()\nleb = [[l] for l in leb]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21b4d7556491652d05d4be5a76041812cda29b6e"},"cell_type":"code","source":"#Lebel Encoding\nle = le.fit(leb)\nlebels.breed = le.transform(lebels.breed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9bc9211e682a8848ecf3688294ec0463291cb6d"},"cell_type":"code","source":"#OneHotEncoding\nleb_enc = le.transform(leb)\nleb_enc = [[l] for l in leb_enc]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab7147764985fe03bbb05211bd4c40f5015240fd"},"cell_type":"code","source":"breed = lebels.breed.tolist()\nbreed = [[b] for b in breed]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2cab2b436f02e34081d05e8c9ef9b8aa397f2cf8"},"cell_type":"code","source":"enc = enc.fit(leb_enc)\nbreed_enc = enc.transform(breed).toarray()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa5b313e7d5250adf58c62e0216478d0314fe20a"},"cell_type":"code","source":"X = t.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6410ac705c08a416366d91be740addc8a82fa493"},"cell_type":"code","source":"del t","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1c510cf046339f3cde0d070f78150b7733108ed"},"cell_type":"code","source":"Y = breed_enc[0:100]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0fbaa666500df61e0d1db8425d0e1ce1b836ddb"},"cell_type":"code","source":"X_ts = ts.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d8841b5c459b6ac0a32cb981bdb184bf980dd32"},"cell_type":"code","source":"del ts","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62f2e92d1dd4c67f6f65da94f0b07aea5878e9ed"},"cell_type":"code","source":"#import packages\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout, Conv2D, MaxPool2D,Flatten, LeakyReLU","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"69d64f35f2583adf407eb817619dd9e0aeb8973a"},"cell_type":"code","source":"# Set the CNN model \n# my CNN architechture is In -> [[Conv2D->relu]*2 -> MaxPool2D -> Dropout]*2 -> Flatten -> Dense -> Dropout -> Out\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='linear', input_shape = (80,80,1)))\nmodel.add(LeakyReLU(alpha=.001))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n# model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same', \n#                  activation ='linear', input_shape = (200,200,1)))\n# model.add(LeakyReLU(alpha=.001))\n# model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n# model.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation = \"linear\"))\nmodel.add(LeakyReLU(alpha=.001))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(256, activation = \"linear\"))\nmodel.add(LeakyReLU(alpha=.001))\n#model.add(Dropout(0.5))\nmodel.add(Dense(120, activation = \"relu\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"476ac049aebb9af372f337711dcbde4ebe0ae84b"},"cell_type":"code","source":"from keras import optimizers\nopt = optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)\nmodel.compile(optimizer = opt , loss = 'mse', metrics=['mae','accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2679d20cb2355ec286c7e89d6c8f4e786650b415"},"cell_type":"code","source":"model.fit(X, Y, epochs=1, batch_size=100,validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cd38025c6f5a0aeeaa0d5db83baf84bc14d1dc9"},"cell_type":"code","source":"Y_ts = model.predict(X_ts)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80b2a2e87db44f8877953c4a9f58e141a8cc8498"},"cell_type":"code","source":"labels = pd.read_csv('../input/labels.csv')\ncolumns = labels.iloc[:,1].value_counts().index.sort_values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1afa896cbb442bd01cf61b65ed36ce7864d72c3e"},"cell_type":"code","source":"ans = pd.DataFrame(Y_ts, columns = columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b2912360c0096293b1e6e155015696903fce2e9"},"cell_type":"code","source":"sub = pd.read_csv('../input/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"676fcc2e2cf52651a2ccdfe80fd226890ab5c8a8"},"cell_type":"code","source":"ids = pd.DataFrame(sub.id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e5e29f8f4c1b6ea5179ed1f17b32800b55c94b5"},"cell_type":"code","source":"result = pd.concat([ids, ans], axis=1, sort=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"903a86914993b5809579b077b966b2afdeb3383e"},"cell_type":"code","source":"result.to_csv('sub1.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}