{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport datetime as dt\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nfrom os import listdir, makedirs\nfrom os.path import join, exists, expanduser\nfrom tqdm import tqdm\nfrom sklearn.metrics import log_loss, accuracy_score\nfrom keras.preprocessing import image\nfrom keras.applications.vgg16 import VGG16\n#from keras.applications.resnet50 import ResNet50\nfrom keras.applications import xception\nfrom keras.applications import inception_v3\nfrom keras.applications.vgg16 import preprocess_input, decode_predictions\nfrom sklearn.linear_model import LogisticRegression\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Dropout , Lambda, Flatten\n#from keras.optimizers import Adam ,RMSprop\nfrom sklearn.model_selection import train_test_split\nfrom keras import  backend as K\nfrom keras.preprocessing.image import ImageDataGenerator,load_img","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-02T04:24:23.298775Z","iopub.execute_input":"2021-11-02T04:24:23.299084Z","iopub.status.idle":"2021-11-02T04:24:23.309821Z","shell.execute_reply.started":"2021-11-02T04:24:23.29903Z","shell.execute_reply":"2021-11-02T04:24:23.30901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = dt.datetime.now()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:24:45.652799Z","iopub.execute_input":"2021-11-02T04:24:45.653126Z","iopub.status.idle":"2021-11-02T04:24:45.657674Z","shell.execute_reply.started":"2021-11-02T04:24:45.653091Z","shell.execute_reply":"2021-11-02T04:24:45.656811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label=pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nlabel","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:26:58.986922Z","iopub.execute_input":"2021-11-02T04:26:58.98726Z","iopub.status.idle":"2021-11-02T04:26:59.208709Z","shell.execute_reply.started":"2021-11-02T04:26:58.987228Z","shell.execute_reply":"2021-11-02T04:26:59.207788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df=pd.DataFrame(label['breed'].value_counts()).reset_index()\nlabel_df.columns=['breed_name','count']\nlabel_df=label_df.head(16)\nlabel_df","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:27:40.797196Z","iopub.execute_input":"2021-11-02T04:27:40.797827Z","iopub.status.idle":"2021-11-02T04:27:40.818755Z","shell.execute_reply.started":"2021-11-02T04:27:40.797791Z","shell.execute_reply":"2021-11-02T04:27:40.81802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df.sort_values(by=\"count\",ascending=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:27:54.658666Z","iopub.execute_input":"2021-11-02T04:27:54.659406Z","iopub.status.idle":"2021-11-02T04:27:54.672997Z","shell.execute_reply.started":"2021-11-02T04:27:54.659368Z","shell.execute_reply":"2021-11-02T04:27:54.67235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = label[label['breed'].isin(label_df['breed_name'])]","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:30:35.842726Z","iopub.execute_input":"2021-11-02T04:30:35.843009Z","iopub.status.idle":"2021-11-02T04:30:35.848985Z","shell.execute_reply.started":"2021-11-02T04:30:35.842981Z","shell.execute_reply":"2021-11-02T04:30:35.847938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label['id_ext']=label['id'].apply(lambda x:x+'.jpg')\nlabel=label.reset_index()\nlabel=label.drop(['index','id'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:30:52.441278Z","iopub.execute_input":"2021-11-02T04:30:52.441576Z","iopub.status.idle":"2021-11-02T04:30:52.454449Z","shell.execute_reply.started":"2021-11-02T04:30:52.441547Z","shell.execute_reply":"2021-11-02T04:30:52.45341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:31:06.419084Z","iopub.execute_input":"2021-11-02T04:31:06.419377Z","iopub.status.idle":"2021-11-02T04:31:06.430603Z","shell.execute_reply.started":"2021-11-02T04:31:06.419342Z","shell.execute_reply":"2021-11-02T04:31:06.429126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot=pd.get_dummies(label,columns=['breed'],prefix=None)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:31:37.213466Z","iopub.execute_input":"2021-11-02T04:31:37.214078Z","iopub.status.idle":"2021-11-02T04:31:37.224987Z","shell.execute_reply.started":"2021-11-02T04:31:37.214008Z","shell.execute_reply":"2021-11-02T04:31:37.223718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:31:50.750774Z","iopub.execute_input":"2021-11-02T04:31:50.751026Z","iopub.status.idle":"2021-11-02T04:31:50.768207Z","shell.execute_reply.started":"2021-11-02T04:31:50.750998Z","shell.execute_reply":"2021-11-02T04:31:50.76733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot.columns","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:33:43.757751Z","iopub.execute_input":"2021-11-02T04:33:43.758738Z","iopub.status.idle":"2021-11-02T04:33:43.764532Z","shell.execute_reply.started":"2021-11-02T04:33:43.758695Z","shell.execute_reply":"2021-11-02T04:33:43.763681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot.columns = label_onehot.columns.str.replace(r'breed_', '')","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:32:24.776842Z","iopub.execute_input":"2021-11-02T04:32:24.777753Z","iopub.status.idle":"2021-11-02T04:32:24.782501Z","shell.execute_reply.started":"2021-11-02T04:32:24.7777Z","shell.execute_reply":"2021-11-02T04:32:24.781601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot=label_onehot.rename(columns={'id_ext':'id'})","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:33:36.527642Z","iopub.execute_input":"2021-11-02T04:33:36.527916Z","iopub.status.idle":"2021-11-02T04:33:36.533485Z","shell.execute_reply.started":"2021-11-02T04:33:36.527888Z","shell.execute_reply":"2021-11-02T04:33:36.532537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_onehot\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:34:41.323008Z","iopub.execute_input":"2021-11-02T04:34:41.323286Z","iopub.status.idle":"2021-11-02T04:34:41.348425Z","shell.execute_reply.started":"2021-11-02T04:34:41.323256Z","shell.execute_reply":"2021-11-02T04:34:41.347599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nsample=random.choice(label_onehot['id'])\nsample","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:34:59.315421Z","iopub.execute_input":"2021-11-02T04:34:59.316237Z","iopub.status.idle":"2021-11-02T04:34:59.323469Z","shell.execute_reply.started":"2021-11-02T04:34:59.316187Z","shell.execute_reply":"2021-11-02T04:34:59.322435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=load_img(\"/kaggle/input/dog-breed-identification/train/\"+sample)\nimage","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:35:11.862608Z","iopub.execute_input":"2021-11-02T04:35:11.86303Z","iopub.status.idle":"2021-11-02T04:35:12.078601Z","shell.execute_reply.started":"2021-11-02T04:35:11.862993Z","shell.execute_reply":"2021-11-02T04:35:12.077775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(label_onehot, test_size=0.1)\ntrain_df = train_df.reset_index()\nvalidate_df = validate_df.reset_index()\n\n# validate_df = validate_df.sample(n=100).reset_index() # use for fast testing code purpose\n# train_df = train_df.sample(n=1800).reset_index() # use for fast testing code purpose\n\ntotal_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:35:33.942967Z","iopub.execute_input":"2021-11-02T04:35:33.943448Z","iopub.status.idle":"2021-11-02T04:35:33.952217Z","shell.execute_reply.started":"2021-11-02T04:35:33.943412Z","shell.execute_reply":"2021-11-02T04:35:33.951425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape,validate_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:36:13.311472Z","iopub.execute_input":"2021-11-02T04:36:13.31175Z","iopub.status.idle":"2021-11-02T04:36:13.317397Z","shell.execute_reply.started":"2021-11-02T04:36:13.311722Z","shell.execute_reply":"2021-11-02T04:36:13.316628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:36:25.21974Z","iopub.execute_input":"2021-11-02T04:36:25.220002Z","iopub.status.idle":"2021-11-02T04:36:25.23851Z","shell.execute_reply.started":"2021-11-02T04:36:25.219974Z","shell.execute_reply":"2021-11-02T04:36:25.237636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D\nfrom keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\nfrom keras.applications import VGG16\nfrom keras.models import Model\n\nimage_size = 224\ninput_shape = (image_size, image_size, 3)\n\nepochs = 4\nbatch_size = 16\n\npre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    \nfor layer in pre_trained_model.layers[:15]:\n    layer.trainable = False\n\nfor layer in pre_trained_model.layers[15:]:\n    layer.trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n# Flatten the output layer to 1 dimension\nx = GlobalMaxPooling2D()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.5\nx = Dropout(0.5)(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(16, activation='softmax')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:37:04.890829Z","iopub.execute_input":"2021-11-02T04:37:04.891778Z","iopub.status.idle":"2021-11-02T04:37:04.919994Z","shell.execute_reply.started":"2021-11-02T04:37:04.891727Z","shell.execute_reply":"2021-11-02T04:37:04.919003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:38:09.568744Z","iopub.execute_input":"2021-11-02T04:38:09.569451Z","iopub.status.idle":"2021-11-02T04:38:09.590613Z","shell.execute_reply.started":"2021-11-02T04:38:09.569404Z","shell.execute_reply":"2021-11-02T04:38:09.589782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:38:21.891748Z","iopub.execute_input":"2021-11-02T04:38:21.892168Z","iopub.status.idle":"2021-11-02T04:38:21.897338Z","shell.execute_reply.started":"2021-11-02T04:38:21.892127Z","shell.execute_reply":"2021-11-02T04:38:21.896702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/input/dog-breed-identification/train\", \n    x_col='id',\n    y_col=['afghan_hound', 'airedale', 'basenji', 'beagle',\n       'bernese_mountain_dog', 'cairn', 'entlebucher', 'great_pyrenees',\n       'japanese_spaniel', 'leonberg', 'maltese_dog', 'pomeranian', 'samoyed',\n       'scottish_deerhound', 'shih-tzu', 'tibetan_terrier'],\n    class_mode='raw',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:38:37.412414Z","iopub.execute_input":"2021-11-02T04:38:37.412705Z","iopub.status.idle":"2021-11-02T04:38:37.435875Z","shell.execute_reply.started":"2021-11-02T04:38:37.412674Z","shell.execute_reply":"2021-11-02T04:38:37.435118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/input/dog-breed-identification/train\", \n    x_col='id',\n    y_col=['afghan_hound', 'airedale', 'basenji', 'beagle',\n       'bernese_mountain_dog', 'cairn', 'entlebucher', 'great_pyrenees',\n       'japanese_spaniel', 'leonberg', 'maltese_dog', 'pomeranian', 'samoyed',\n       'scottish_deerhound', 'shih-tzu', 'tibetan_terrier'],\n    class_mode='raw',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T04:38:55.582267Z","iopub.execute_input":"2021-11-02T04:38:55.582998Z","iopub.status.idle":"2021-11-02T04:38:55.604025Z","shell.execute_reply.started":"2021-11-02T04:38:55.582961Z","shell.execute_reply":"2021-11-02T04:38:55.603274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/input/dog-breed-identification/train/\", \n    x_col='id',\n    y_col=['afghan_hound', 'airedale', 'basenji', 'beagle',\n       'bernese_mountain_dog', 'cairn', 'entlebucher', 'great_pyrenees',\n       'japanese_spaniel', 'leonberg', 'maltese_dog', 'pomeranian', 'samoyed',\n       'scottish_deerhound', 'shih-tzu', 'tibetan_terrier'],\n    class_mode='raw',\n)\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate_generator(validation_generator, total_validate//batch_size, workers=12)\nprint(\"Test: accuracy = %f  ;  loss = %f \" % (accuracy, loss))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dog(row):\n    for i in validate_df.columns[2:]:\n        if row[i]==1:\n            return i        \nvalidate_df['breed']=validate_df.apply(get_dog,axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df=validate_df[['id','breed']]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = validate_df.sample(n=9).reset_index()\n#print(sample_test)\nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['id']\n    print(filename)\n    category = row['breed']\n    img = load_img(\"/kaggle/input/dog-breed-identification/train/\"+filename, target_size=(256, 256))\n    \n    plt.subplot(3, 3,index+1)\n    plt.imshow(img)\n    plt.xlabel('(' + \"{}\".format(category) + ')')\nplt.tight_layout()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"end = dt.datetime.now()\nprint('Total time {} s.'.format((end - start).seconds))","metadata":{},"execution_count":null,"outputs":[]}]}