{"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 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.vgg16 import VGG16\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.applications import Xception\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\nimport time","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":"5ee1c1c9675ee3798e52afa86aed72759103694a"},"cell_type":"code","source":"# sam = pd.read_csv(\"../input/sample_submission.csv\")\n# sam.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"286b0da7cd5e6c57cdec0f6cf5e670d25e78755c"},"cell_type":"code","source":"breed_ls = list(df.groupby('breed').count().sort_values(by='id', ascending=False).index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"69b59e62cae8fa4b601b2441b62097e7846c8baf"},"cell_type":"code","source":"import random\nimport cv2\nfrom keras.preprocessing import image\n\ndef getRandomImageList(breed_name, no_of_samples=60):\n    global df\n    random_images = []\n    for index, row in df.iterrows():\n        if row['breed'] == breed_name:\n            random_images.append(row['id'])\n    random_images = random.sample(random_images, no_of_samples)\n    return random_images\n\ndef readImgResize(name,path,dim=128):\n    img = cv2.imread(path+name)\n    img = cv2.resize(img,(dim,dim))\n    return image.img_to_array(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b5a2c9090c15f5c8a967aa005f4bbf7cdb14d3c","scrolled":true},"cell_type":"code","source":"INPUT_SIZE = 128\nnum_class=NUM_CLASSES=120\nsamples = 65\n\n\nimage_label = []\nnum = 0\nimport tqdm\nfor i,breed in tqdm.tqdm( enumerate(breed_ls[:num_class])):\n\n    ls = getRandomImageList(breed,samples) \n    image_label.extend(ls)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3c6525b8756c833c77f8a6ade78bedaba90f119"},"cell_type":"code","source":"new_df = pd.DataFrame({\"id\":image_label})\nnew_df = pd.merge(new_df, df, how='inner', on=['id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01edfd94281b33f3477dc2ff0e51538ed6ca31a7"},"cell_type":"code","source":"\nfrom sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ncat = le.fit_transform(new_df.breed)\nfrom keras.utils.np_utils import to_categorical\nmat = to_categorical(cat)\n\ntraining_data = np.zeros(shape=(len(new_df.id),INPUT_SIZE,INPUT_SIZE,3))\n\nfor i,j in tqdm.tqdm(enumerate(new_df[\"id\"])):\n    training_data[i]=readImgResize(j+\".jpg\",path=\"../input/train/\")\n    \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1b27a83b5b4350dfa588c4fddd2d98017ea3cc3"},"cell_type":"code","source":"# from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten,GlobalAvgPool2D\nfrom keras.layers import Conv2D, MaxPooling2D\nimport keras\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split( training_data, mat, test_size=0.05, random_state=11)\ndel training_data, mat, new_df, df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6cd33f583c9d7ffd4172eff6ac5b29fdbf472ce2"},"cell_type":"code","source":"from matplotlib import pyplot as plt\nplt.figure(figsize=(15,7))\nfor i in range(6):\n    idx = random.randint(0,len(X_train))\n    name = le.inverse_transform(y_train[idx].argmax())\n    plt.subplot(2,3,i+1)\n    plt.imshow(X_train[idx]/255)\n    plt.xlabel(str(name))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f2c6265be7de97ab3da7318d2b81f9216f2c1df","scrolled":true},"cell_type":"code","source":"from keras.models import Model\nfrom keras.optimizers import adam\n\ndef getModel(model_name,epochs=5):\n    im_size = 128\n    global X_train\n    global y_train\n    \n    global X_test\n    global y_test\n    \n    base_model = model_name(weights='imagenet', include_top=False, input_shape=(im_size, im_size, 3))\n\n    # Add a new top layer\n    x = base_model.output\n    x = Flatten()(x)\n    predictions = Dense(num_class, activation='softmax')(x)\n\n    # This is the model we wi`l train\n    model = Model(inputs=base_model.input, outputs=predictions)\n\n    # First: train only the top layers (which were randomly initialized)\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    model.compile(adam(lr=0.000001),loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n    callbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', patience=3, verbose=1)]\n    model.fit(X_train/255, y_train, epochs=epochs, validation_data=(X_test/255, y_test), verbose=1)\n    \n    return(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4158cf87e26d994467d105640f6bb6a05cd6f3db"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c389e08c5ca4a5ebfedbc99a9cf30bec9e1297e"},"cell_type":"code","source":"xcep = getModel(Xception,epochs=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8c763d9c9bf246d8935080299dab548ccf4a2dc"},"cell_type":"code","source":"resnet = getModel(ResNet50,5)#overfitting\ninception = getModel(InceptionV3,35)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c43298429fa2a03ac71970dbb20a776d25dc7db4"},"cell_type":"code","source":"del X_train,X_test,y_train,y_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca90c77b59ae69fd6d8df4d231be4bf7c33037f2"},"cell_type":"code","source":"def getAvgPred(data):\n    global xcep\n    global resnet\n    global inception\n    pred1 = xcep.predict(data)\n    pred2 = resnet.predict(data)\n    pred3 = inception.predict(data)\n    return (pred1+pred2+pred3)/3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64ab9b4704a67bafe34d7c2d22ba56baad6f0df7"},"cell_type":"code","source":"te = os.listdir(\"../input/test/\")\nte_in = np.zeros((len(te),128,128,3))\nfor num , i in enumerate(te):\n    img = readImgResize(i,path=\"../input/test/\")/255\n    te_in[num]=img\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d21eea5e9a9474e4578ce9840d8ed382798e55a6"},"cell_type":"code","source":"pred = getAvgPred(te_in)\nsubmission = 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()]\nsubmission.sort_values(['id'])\nsubmission.to_csv(\"submission.csv\",index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf9cd1874b7d607038b35c6774e045bbf5cf7817"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bdf52c00bbbf342dad68dce8546367b9703a136a"},"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}