{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport cv2\n\nimport os\nprint(os.listdir(\"../input\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"561a4686fbda0349be641280ea2fde86192cebfe"},"cell_type":"code","source":"labels = pd.read_csv(\"../input/labels.csv\")\nll = labels.loc[labels[\"id\"] == \"001513dfcb2ffafc82cccf4d8bbaba97\", \"breed\"].iloc[0]\nprint(ll)\nprint(type(ll))\n#labels_dict = labels.to_dict()\n#print(labels_dict)\n#print(labels_dict.get(\"001513dfcb2ffafc82cccf4d8bbaba97\"))\nlabels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"375924b6f62d34914d1e184e8c8292e25a1fd16b"},"cell_type":"code","source":"def load_images(folder, labels_df=None):  \n    images = []\n    if folder is \"train\":\n        folder = \"../input/\"+folder\n        labels = []\n        for filename in os.listdir(folder):\n            img = cv2.imread(os.path.join(folder,filename))\n            if img is not None:\n                images.append(img)\n                l = labels_df.loc[labels_df[\"id\"] == filename.replace(\".jpg\",\"\"), \"breed\"].iloc[0]\n                labels.append(l)\n        return images,labels\n    else:\n        folder = \"../input/\"+folder\n        for filename in os.listdir(folder):\n            img = cv2.imread(os.path.join(folder,filename))\n            if img is not None:\n                images.append(img)\n        return images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e30f9035edc8122461920ac14447a295bd2833b"},"cell_type":"markdown","source":"# Resize images into 224\\*224\\*3"},{"metadata":{"trusted":true,"_uuid":"f240779e31b04356595b5485b5943f1911fcce41"},"cell_type":"code","source":"def resize_images(images, target_size=(224,224)):\n    res_img=[]\n    for img in images:\n        res  = cv2.resize(img,target_size, interpolation=cv2.INTER_LINEAR)\n        res_img.append(res)\n    return res_img","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"markdown","source":"# Resizing RGB 3 Channel Images into Gray"},{"metadata":{"trusted":true,"_uuid":"85e973a195e17cfb2e54eeac72691ba4c31e7643"},"cell_type":"code","source":"def convert_into_gray(images):\n    img_gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in images]\n    return img_gray","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5a8dec5c870fa654da82d8709e9c5c528971835"},"cell_type":"code","source":"def data_flatten(images):\n    print(type(images))\n    arr_size = [len(images), images[0].shape[0]*images[0].shape[1]]\n    data = np.empty(arr_size) #50176\n    for i,l in zip(images,range(len(images))):\n        data[l]=np.array(i).reshape(-1)\n    return data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed90f8d4840d83b4f6701edd729c1bdc39767c35"},"cell_type":"code","source":"train_images, train_labels = load_images(\"train\", labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2ab4d803742450d77028718116d528e0b4b49b6"},"cell_type":"code","source":"res_images = resize_images(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"455d5380a07789d2152caffebfae359f8f28c590"},"cell_type":"code","source":"index = 3\nprint(\"Original Image Size {}\".format(train_images[index].shape))\nprint(\"resized Image Size {}\".format(res_images[index].shape))\nplt.subplot(121), plt.imshow(train_images[index]), plt.title(\"Original {}\".format(train_labels[index]))\nplt.subplot(122), plt.imshow(res_images[index]), plt.title(\"Resized {}\".format(train_labels[index]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f716a60a39bf16ee0acb68cb8b1ac0bdf77657f5"},"cell_type":"code","source":"gray_images = convert_into_gray(res_images)\nindex = 0\ngray_images[index].shape\nplt.subplot(121), plt.imshow(res_images[index]), plt.title(\"Resized Original {}\".format(train_labels[index]))\nplt.subplot(122), plt.imshow(gray_images[index]), plt.title(\"Resized Gray {}\".format(train_labels[index]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87a3f1087a3578d2a8d05ffedf8ce0a337a017cc"},"cell_type":"code","source":"X = data_flatten(gray_images)\nprint(X.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cf0b42c03c74bef3b1ac54f66288b15b40d5b05"},"cell_type":"code","source":"y = np.asarray(train_labels)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d5082bf978ca4b300d42b1bfc6c9af12831d19c"},"cell_type":"code","source":"total_cat = len(np.unique(y))\nprint(total_cat)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2129087e05186370a984c75d049c83c74dfa0958"},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\n\nlabel_encoder = LabelEncoder()\ninteger_encoded = label_encoder.fit_transform(y)\nprint(integer_encoded[:5])\n# binary encode\nonehot_encoder = OneHotEncoder(sparse=False)\ninteger_encoded = integer_encoded.reshape(len(integer_encoded), 1)\nonehot_encoded = onehot_encoder.fit_transform(integer_encoded)\nprint(onehot_encoded[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef3ddaec6fed7107ad5f5b16869a4a237caaf515"},"cell_type":"code","source":"# Converting y vector into Categorical (m*120 size)\n#from keras.utils import np_utils, to_categorical\n#y_cat = to_categorical(y,total_cat)\n#print(y_cat.shape)\n#print(y_cat[:])\n#print(y_cat[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22555eda48e3dc28c0f17be8409a02c847c506bf"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, integer_encoded, test_size = 0.10)\n\nprint(X_train.shape)\nprint(y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36cd77f1540a20a40297b80994960fba64a98450"},"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0\n\nprint(X_train[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2337f0b70fd1b04bf15cab0ba31fbf0a79a147d5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c7b9cb7cb342c0c44866565867ce6b835723d97"},"cell_type":"code","source":"print(\"training Start...\")\nfrom sklearn.linear_model import LogisticRegression\nclassifier = LogisticRegression(random_state=0, solver='lbfgs',multi_class='multinomial')\nclassifier.fit(X_train, y_train)\n\nprint(\"training End...\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bf18cf5ccedaee6ae2b2f29bdedbd11f4b563ea"},"cell_type":"code","source":"# Predicting the Test set results\ny_pred = classifier.predict(X_test)\nprint(y_test[:10])\nprint(y_pred[:10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52bd0cd892abf6550e9d227524678d01c62aada1"},"cell_type":"code","source":"classifier.score(X_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"95c4d861cfe55f0919070372db8882b898772a99"},"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.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}