{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import  tqdm\nprint(os.listdir(\"../input\"))\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten,GlobalAvgPool2D\nfrom keras.layers import Conv2D, MaxPooling2D\nimport keras\nfrom keras.preprocessing import image\nfrom keras.applications import InceptionV3\nfrom keras.applications.vgg16 import preprocess_input, decode_predictions\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import log_loss, accuracy_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eddb455c5cc8b609ec2e27d550bcdf52fadbb094"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6436f302c700e89c7a39126dcb469d01535124af"},"cell_type":"code","source":"df = pd.read_csv(\"../input/labels.csv\")\ndf.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17500e00680d6c108b0e9bd0bf5f26ad916e14ed"},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ncat = le.fit_transform(df.breed)\nfrom keras.utils.np_utils import to_categorical\nmat = to_categorical(cat)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faf4f2e6f46176c3aa559a412ef1ba2c08cdf61d"},"cell_type":"code","source":"import cv2\ndef readImgResize(name,path):\n    img = cv2.imread(path+name)\n    img = cv2.resize(img,(150,150))\n    return image.img_to_array(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cdfb71d51d9babcecad59cd80a9c36bc39c4305"},"cell_type":"code","source":"training_data = np.zeros(shape=(len(df.id),150,150,3))\nfor i,j in tqdm(enumerate(df.id)):\n    training_data[i]=readImgResize(j+\".jpg\",path=\"../input/train/\")\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71231904f3cfb5a537f511d9fabe7379351f8df4"},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split( training_data, mat, test_size=0.05, random_state=11)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2880ac07563e890f2ec009f2b60bd8251075095b"},"cell_type":"code","source":"del training_data, mat","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e74d658b8fb59a89255007af35f5b613fefcf4a"},"cell_type":"code","source":"from keras.models import Model\nfrom keras.optimizers import adam\nnum_class = 120\nim_size = 150\nbase_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(im_size, im_size, 3))\n\n# Add a new top layer\nx = base_model.output\nx = Flatten()(x)\npredictions = Dense(num_class, activation='softmax')(x)\n\n# This is the model we wi`l train\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# First: train only the top layers (which were randomly initialized)\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel.compile(adam(lr=0.00001),loss='categorical_crossentropy', \n              metrics=[\"accuracy\"])\n\ncallbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', patience=3, verbose=1)]\n# # model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98f4d5951ed32683ea7c5843fb49f0047bd562a7","scrolled":true},"cell_type":"code","source":"model.fit(X_train/255, y_train, epochs=30, validation_data=(X_test/255, y_test), verbose=1)\nmodel.save(\"breed_inception.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc4e8284102e7132377e0e174f2dcd3e6b749667"},"cell_type":"code","source":"from keras.models import load_model\ndel X_train,X_test\nte = os.listdir(\"../input/test/\")\nte_in = np.zeros((len(te),150,150,3))\nfor num , i in enumerate(te):\n    img = readImgResize(i,path=\"../input/test/\")/255\n    te_in[num]=img\n    \npred = model.predict(te_in)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60d35128a99a0dcf0a46bd777760a9f05bc48d71"},"cell_type":"code","source":"submission = pd.DataFrame(pred , columns =le.classes_.tolist())\nsubmission[\"id\"]=[i.split(\".\")[0] for i in os.listdir(\"../input/test/\")]\nsubmission = submission[[\"id\"]+submission.columns[:-1].tolist()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2e4015abceaa0ba34a3899dae7a1fcefc39e622"},"cell_type":"code","source":"submission.sort_values(by=['id'])\nsubmission.to_csv(\"submission.csv\",index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3fa8c6fd99946cd405fb807a1340ea91f30f8fa"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1426669d3d79bcb1f98adfdf18b7b7e9fd55112"},"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}