{"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":"import os\nimport pandas as pd\nimport cv2","metadata":{"id":"HpYTgMy69wG1","execution":{"iopub.status.busy":"2022-01-20T17:38:03.588230Z","iopub.execute_input":"2022-01-20T17:38:03.588558Z","iopub.status.idle":"2022-01-20T17:38:03.781340Z","shell.execute_reply.started":"2022-01-20T17:38:03.588472Z","shell.execute_reply":"2022-01-20T17:38:03.780544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = \"../input/dog-breed-identification/\"","metadata":{"id":"d6ysjl_X9_iq","execution":{"iopub.status.busy":"2022-01-20T17:38:03.783129Z","iopub.execute_input":"2022-01-20T17:38:03.783431Z","iopub.status.idle":"2022-01-20T17:38:03.788375Z","shell.execute_reply.started":"2022-01-20T17:38:03.783383Z","shell.execute_reply":"2022-01-20T17:38:03.786679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(base_dir+\"labels.csv\")","metadata":{"id":"vECcqQxr-AX3","execution":{"iopub.status.busy":"2022-01-20T17:38:03.789437Z","iopub.execute_input":"2022-01-20T17:38:03.790802Z","iopub.status.idle":"2022-01-20T17:38:03.825051Z","shell.execute_reply.started":"2022-01-20T17:38:03.789605Z","shell.execute_reply":"2022-01-20T17:38:03.824435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breed_count = dict(data.breed.value_counts().items())\nmax_cnt = max(breed_count.values())","metadata":{"id":"HdsAwh1d-FEZ","execution":{"iopub.status.busy":"2022-01-20T17:38:03.827037Z","iopub.execute_input":"2022-01-20T17:38:03.827292Z","iopub.status.idle":"2022-01-20T17:38:03.840511Z","shell.execute_reply.started":"2022-01-20T17:38:03.827259Z","shell.execute_reply":"2022-01-20T17:38:03.839811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nbal_data = list(data.values)\nfor breed in breed_count:\n  diff_data = data[data.breed==breed].sample(max_cnt-breed_count[breed],replace=False)\n  bal_data.extend(list(diff_data.values))\nnew_data = pd.DataFrame(bal_data,columns=data.columns)","metadata":{"id":"tPuqWh5Y-Hlr","execution":{"iopub.status.busy":"2022-01-20T17:38:03.841933Z","iopub.execute_input":"2022-01-20T17:38:03.842512Z","iopub.status.idle":"2022-01-20T17:38:04.174736Z","shell.execute_reply.started":"2022-01-20T17:38:03.842476Z","shell.execute_reply":"2022-01-20T17:38:04.173996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data['id'] = new_data['id'].apply(lambda x:x+'.jpg')","metadata":{"id":"EpaE-m0x-Kvv","execution":{"iopub.status.busy":"2022-01-20T17:38:04.177174Z","iopub.execute_input":"2022-01-20T17:38:04.177593Z","iopub.status.idle":"2022-01-20T17:38:04.187467Z","shell.execute_reply.started":"2022-01-20T17:38:04.177555Z","shell.execute_reply":"2022-01-20T17:38:04.186877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test = train_test_split(new_data,random_state=42,test_size=500,stratify=new_data.breed)\nx_train.shape, x_test.shape","metadata":{"id":"eO-o6P1--NwW","outputId":"a392741a-6869-40e1-8aaa-0b3725d1a1cb","execution":{"iopub.status.busy":"2022-01-20T17:38:04.188863Z","iopub.execute_input":"2022-01-20T17:38:04.189130Z","iopub.status.idle":"2022-01-20T17:38:04.904806Z","shell.execute_reply.started":"2022-01-20T17:38:04.189096Z","shell.execute_reply":"2022-01-20T17:38:04.904106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.breed.value_counts(),x_test.breed.value_counts()","metadata":{"id":"zY2AQmVy-QUG","outputId":"0fff8546-1bc4-478c-809b-da9f6800ab1e","execution":{"iopub.status.busy":"2022-01-20T17:38:04.905890Z","iopub.execute_input":"2022-01-20T17:38:04.906115Z","iopub.status.idle":"2022-01-20T17:38:04.919743Z","shell.execute_reply.started":"2022-01-20T17:38:04.906080Z","shell.execute_reply":"2022-01-20T17:38:04.918971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D\nfrom tensorflow.keras.models import Model,Sequential\nfrom tensorflow.keras.layers import Dense,Flatten,Activation,Dropout,BatchNormalization,Input,Softmax, concatenate\nfrom tensorflow.keras.callbacks import EarlyStopping,TensorBoard,ReduceLROnPlateau,ModelCheckpoint","metadata":{"id":"HoPIdrGX-TS1","execution":{"iopub.status.busy":"2022-01-20T17:38:04.921099Z","iopub.execute_input":"2022-01-20T17:38:04.921388Z","iopub.status.idle":"2022-01-20T17:38:09.600546Z","shell.execute_reply.started":"2022-01-20T17:38:04.921351Z","shell.execute_reply":"2022-01-20T17:38:09.599794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\ndef f1_score_micro1(y_true, y_pred):\n  y_prediton = (tf.math.argmax(y_pred, axis = -1))\n  y_actual = (tf.math.argmax(y_true, axis = -1))\n  f1 = f1_score(y_actual,y_prediton,average='micro', zero_division = 0)\n  return f1\ndef f1_score_micro(y_true, y_pred):\n  return tf.py_function(f1_score_micro1, (y_true, y_pred), tf.float32)","metadata":{"id":"5e7m0MNk-XX9","execution":{"iopub.status.busy":"2022-01-20T17:38:09.603888Z","iopub.execute_input":"2022-01-20T17:38:09.604140Z","iopub.status.idle":"2022-01-20T17:38:09.615351Z","shell.execute_reply.started":"2022-01-20T17:38:09.604106Z","shell.execute_reply":"2022-01-20T17:38:09.614755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import log_loss\ndef log_loss1(y_true, y_pred):\n  f1 = log_loss(y_true, y_pred)\n  return f1\ndef logloss(y_true, y_pred):\n  return tf.py_function(log_loss1, (y_true, y_pred), tf.float32)","metadata":{"id":"UPEYG7v8-Zxz","execution":{"iopub.status.busy":"2022-01-20T17:38:09.617430Z","iopub.execute_input":"2022-01-20T17:38:09.617875Z","iopub.status.idle":"2022-01-20T17:38:09.625895Z","shell.execute_reply.started":"2022-01-20T17:38:09.617837Z","shell.execute_reply":"2022-01-20T17:38:09.625176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_dir='./logs/fit'\ntensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir,histogram_freq=1, write_graph=True)","metadata":{"id":"tfRcmTA2-dWV","execution":{"iopub.status.busy":"2022-01-20T17:38:09.627134Z","iopub.execute_input":"2022-01-20T17:38:09.627583Z","iopub.status.idle":"2022-01-20T17:38:09.811385Z","shell.execute_reply.started":"2022-01-20T17:38:09.627548Z","shell.execute_reply":"2022-01-20T17:38:09.810457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nasnet = tf.keras.applications.NASNetLarge(\n    input_shape=(331, 331, 3),\n    include_top=False,\n    weights=\"imagenet\",\n)","metadata":{"id":"D7NPJ_2J5TRn","outputId":"e108ff17-4a34-48be-da49-3603dfe0bf4c","execution":{"iopub.status.busy":"2022-01-20T17:38:09.812500Z","iopub.execute_input":"2022-01-20T17:38:09.812747Z","iopub.status.idle":"2022-01-20T17:38:21.033395Z","shell.execute_reply.started":"2022-01-20T17:38:09.812712Z","shell.execute_reply":"2022-01-20T17:38:21.032632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\ndatagen=ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,validation_split=0.1)\n\ntrain_generator=datagen.flow_from_dataframe(\ndataframe=x_train,\ndirectory=base_dir+\"train/\",\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"training\",\nbatch_size=64,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(331, 331),\ncolor_mode='rgb')","metadata":{"outputId":"24dc2696-1d34-46f4-bc13-7a63556fce95","id":"Pn7eV_PoO_Jt","execution":{"iopub.status.busy":"2022-01-20T17:38:21.034586Z","iopub.execute_input":"2022-01-20T17:38:21.034847Z","iopub.status.idle":"2022-01-20T17:38:37.743015Z","shell.execute_reply.started":"2022-01-20T17:38:21.034813Z","shell.execute_reply":"2022-01-20T17:38:37.742269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\ndataframe=x_train,\ndirectory=base_dir+\"train/\",\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"validation\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(331, 331),\ncolor_mode='rgb',)","metadata":{"outputId":"4e76c0d0-4ae6-4dec-853a-711e5364788e","id":"Aa5ndWXvO_Ju","execution":{"iopub.status.busy":"2022-01-20T17:38:37.744182Z","iopub.execute_input":"2022-01-20T17:38:37.744829Z","iopub.status.idle":"2022-01-20T17:38:40.666331Z","shell.execute_reply.started":"2022-01-20T17:38:37.744783Z","shell.execute_reply":"2022-01-20T17:38:40.665562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nasnet.trainable = False","metadata":{"id":"zkDPSzw0Oer4","execution":{"iopub.status.busy":"2022-01-20T17:38:40.667418Z","iopub.execute_input":"2022-01-20T17:38:40.668165Z","iopub.status.idle":"2022-01-20T17:38:40.703529Z","shell.execute_reply.started":"2022-01-20T17:38:40.668128Z","shell.execute_reply":"2022-01-20T17:38:40.702709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class learng_rate_schedule(tf.keras.callbacks.Callback):\n    def __init__(self,verbose=1):\n        super(learng_rate_schedule, self).__init__()\n        self.verbose=verbose\n    def on_epoch_begin(self,epoch,verbose=1):\n        history=self.model.history.history\n        lr=float(K.get_value(self.model.optimizer.lr))\n        if epoch%3==2:\n            lr=lr*0.95\n        elif (len(history.get('val_logloss',[]))>=2 and epoch%3!=2):\n            if history['val_logloss'][-1]<=history['val_logloss'][-2]:\n                lr=lr*0.9\n            else:\n                lr=lr\n        else:\n            lr=lr\n        K.set_value(self.model.optimizer.lr, K.get_value(lr))\n        if self.verbose > 0:\n            print('\\nEpoch %05d: LearningRateScheduler reducing learning rate to %s.' % (epoch + 1, lr))\n        \n    def on_epoch_end(self, epoch, logs):\n        logs = logs\n        logs['lr'] = K.get_value(self.model.optimizer.lr)\n\n\nfilepath=log_dir+\"/save/\"\ncheckpoint = ModelCheckpoint(filepath=filepath, monitor='val_logloss',  verbose=1, save_best_only=True, mode='min')\n","metadata":{"id":"1kPHWtCh_itu","execution":{"iopub.status.busy":"2022-01-20T17:38:40.704902Z","iopub.execute_input":"2022-01-20T17:38:40.705141Z","iopub.status.idle":"2022-01-20T17:38:40.714877Z","shell.execute_reply.started":"2022-01-20T17:38:40.705108Z","shell.execute_reply":"2022-01-20T17:38:40.714185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nasnet.output","metadata":{"id":"4SZYj-kFA_oK","outputId":"d2a4d5e0-2218-4c14-ae6c-f7b2c3b4fec8","execution":{"iopub.status.busy":"2022-01-20T17:38:40.716563Z","iopub.execute_input":"2022-01-20T17:38:40.717070Z","iopub.status.idle":"2022-01-20T17:38:40.727824Z","shell.execute_reply.started":"2022-01-20T17:38:40.717034Z","shell.execute_reply":"2022-01-20T17:38:40.727090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras.backend as K\nK.clear_session()\nlayer3=Conv2D(128,(7,7),activation='relu',padding=\"valid\")(nasnet.output)\ndrop = Dropout(0.2)(layer3)\nlayer4=Conv2D(128,(3,3),activation='relu',padding=\"valid\")(drop)\ndrop1 = Dropout(0.2)(layer4)\nlayer5=Conv2D(128,(3,3),activation='relu',padding=\"valid\")(drop1)\nlayer6=Flatten()(layer5)\ndrop2 = Dropout(0.2)(layer6)\noutput_layer=Dense(120,activation='softmax')(drop2)\nmodel3=Model(inputs=nasnet.input,outputs=output_layer)\nmodel3.compile(optimizer='adam',\n loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[f1_score_micro,logloss]\n )","metadata":{"id":"TDb3fi9D-hzi","execution":{"iopub.status.busy":"2022-01-20T17:38:40.728865Z","iopub.execute_input":"2022-01-20T17:38:40.730331Z","iopub.status.idle":"2022-01-20T17:38:40.857573Z","shell.execute_reply.started":"2022-01-20T17:38:40.730295Z","shell.execute_reply":"2022-01-20T17:38:40.856934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size","metadata":{"id":"fQICDMPODGr9","execution":{"iopub.status.busy":"2022-01-20T17:38:40.858753Z","iopub.execute_input":"2022-01-20T17:38:40.858994Z","iopub.status.idle":"2022-01-20T17:38:40.863331Z","shell.execute_reply.started":"2022-01-20T17:38:40.858962Z","shell.execute_reply":"2022-01-20T17:38:40.862654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model3.fit_generator(generator=train_generator,\n                      steps_per_epoch=STEP_SIZE_TRAIN,\n                      validation_data=valid_generator,\n                      validation_steps=STEP_SIZE_VALID,\n                      epochs=10, callbacks = [checkpoint,tensorboard_callback])","metadata":{"id":"sUBIQ4T5_Lfu","outputId":"89c2fca5-04bb-48f7-8092-768c886fcb40","execution":{"iopub.status.busy":"2022-01-20T17:38:40.864507Z","iopub.execute_input":"2022-01-20T17:38:40.864928Z","iopub.status.idle":"2022-01-20T19:20:32.461764Z","shell.execute_reply.started":"2022-01-20T17:38:40.864884Z","shell.execute_reply":"2022-01-20T19:20:32.460934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive('./logs', 'zip', \"./\")","metadata":{"id":"q5pG0fnaDHam","execution":{"iopub.status.busy":"2022-01-20T19:20:32.463081Z","iopub.execute_input":"2022-01-20T19:20:32.463353Z","iopub.status.idle":"2022-01-20T19:21:06.032163Z","shell.execute_reply.started":"2022-01-20T19:20:32.463318Z","shell.execute_reply":"2022-01-20T19:21:06.031432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}