{"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":"markdown","source":"wdas.**\n* Loss: Focal loss\n* Metrics: w","metadata":{"execution":{"iopub.status.busy":"2022-09-08T09:01:23.610492Z","iopub.execute_input":"2022-09-08T09:01:23.610797Z","iopub.status.idle":"2022-09-08T09:01:23.615693Z","shell.execute_reply.started":"2022-09-08T09:01:23.610745Z","shell.execute_reply":"2022-09-08T09:01:23.614791Z"}}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"!nvidia-smi \n!lscpu","metadata":{"execution":{"iopub.status.busy":"2022-09-11T06:04:44.770523Z","iopub.execute_input":"2022-09-11T06:04:44.770842Z","iopub.status.idle":"2022-09-11T06:04:46.780881Z","shell.execute_reply.started":"2022-09-11T06:04:44.770796Z","shell.execute_reply":"2022-09-11T06:04:46.779952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport json\nfrom keras.models import Sequential, Model\nfrom keras.layers import Dense, Flatten, Activation, Dropout, GlobalAveragePooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers, applications\nfrom keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping\nfrom keras import backend as K ","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-09-11T05:45:29.684168Z","iopub.execute_input":"2022-09-11T05:45:29.684648Z","iopub.status.idle":"2022-09-11T05:45:31.330143Z","shell.execute_reply.started":"2022-09-11T05:45:29.684576Z","shell.execute_reply":"2022-09-11T05:45:31.329269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/imet-2019-fgvc6/train.csv\")\ntrain_df[\"attribute_ids\"]=train_df[\"attribute_ids\"].apply(lambda x:x.split(\" \"))\ntrain_df[\"id\"]=train_df[\"id\"].apply(lambda x:x+\".png\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:31.333621Z","iopub.execute_input":"2022-09-11T05:45:31.333874Z","iopub.status.idle":"2022-09-11T05:45:31.707304Z","shell.execute_reply.started":"2022-09-11T05:45:31.333824Z","shell.execute_reply":"2022-09-11T05:45:31.706529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = pd.read_csv(\"../input/imet-2019-fgvc6/labels.csv\")\nprint(label_df.shape)\nlabel_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:31.708482Z","iopub.execute_input":"2022-09-11T05:45:31.708885Z","iopub.status.idle":"2022-09-11T05:45:31.732695Z","shell.execute_reply.started":"2022-09-11T05:45:31.708836Z","shell.execute_reply":"2022-09-11T05:45:31.731658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example of images with tags\n\ni = 1\nplt.figure(figsize=[30,30])\nfor img_name in os.listdir(\"../input/imet-2019-fgvc6/train/\")[5:10]:   \n    img = cv2.imread(\"../input/imet-2019-fgvc6/train/%s\" % img_name)[...,[2, 1, 0]]\n    plt.subplot(5, 1, i)\n    plt.imshow(img)\n    ids = train_df[train_df[\"id\"] == img_name][\"attribute_ids\"]\n    title_val = []\n    for tag_id in ids.values[0]:\n        att_name = label_df[label_df['attribute_id'].astype(str) == tag_id]['attribute_name'].values[0]\n        title_val.append(att_name)\n    plt.title(title_val)\n    i += 1\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:31.734164Z","iopub.execute_input":"2022-09-11T05:45:31.734654Z","iopub.status.idle":"2022-09-11T05:45:40.66793Z","shell.execute_reply.started":"2022-09-11T05:45:31.734429Z","shell.execute_reply":"2022-09-11T05:45:40.667176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_classes = 1103\nbatch_size = 300\nimg_size = 75\nnb_epochs = 25","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:40.66885Z","iopub.execute_input":"2022-09-11T05:45:40.669071Z","iopub.status.idle":"2022-09-11T05:45:40.673834Z","shell.execute_reply.started":"2022-09-11T05:45:40.669034Z","shell.execute_reply":"2022-09-11T05:45:40.67303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbls = list(map(str, range(nb_classes)))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:40.675524Z","iopub.execute_input":"2022-09-11T05:45:40.676335Z","iopub.status.idle":"2022-09-11T05:45:40.684047Z","shell.execute_reply.started":"2022-09-11T05:45:40.676224Z","shell.execute_reply":"2022-09-11T05:45:40.683272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#time\n\ntrain_datagen=ImageDataGenerator(\n    rescale=1./255, \n    validation_split=0.25,\n    horizontal_flip = True,    \n    zoom_range = 0.3,\n    width_shift_range = 0.3,\n    height_shift_range=0.3\n    )\n\ntrain_generator=train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=\"../input/imet-2019-fgvc6/train\",\n    x_col=\"id\",\n    y_col=\"attribute_ids\",\n    batch_size=batch_size,\n    shuffle=True,\n    class_mode=\"categorical\",\n    classes=lbls,\n    target_size=(img_size,img_size),\n    subset='training')\n\nvalid_generator=train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=\"../input/imet-2019-fgvc6/train\",\n    x_col=\"id\",\n    y_col=\"attribute_ids\",\n    batch_size=batch_size,\n    shuffle=True,\n    class_mode=\"categorical\",    \n    classes=lbls,\n    target_size=(img_size,img_size),\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:45:40.685664Z","iopub.execute_input":"2022-09-11T05:45:40.686266Z","iopub.status.idle":"2022-09-11T05:51:27.986194Z","shell.execute_reply.started":"2022-09-11T05:45:40.686214Z","shell.execute_reply":"2022-09-11T05:51:27.98534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss\n\ngamma = 2.0\nepsilon = K.epsilon()\ndef focal_loss(y_true, y_pred):\n    pt = y_pred * y_true + (1-y_pred) * (1-y_true)\n    pt = K.clip(pt, epsilon, 1-epsilon)\n    CE = -K.log(pt)\n    FL = K.pow(1-pt, gamma) * CE\n    loss = K.sum(FL, axis=1)\n    return loss","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:51:27.987431Z","iopub.execute_input":"2022-09-11T05:51:27.987904Z","iopub.status.idle":"2022-09-11T05:51:27.993973Z","shell.execute_reply.started":"2022-09-11T05:51:27.987852Z","shell.execute_reply":"2022-09-11T05:51:27.993211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Metric\n\ndef f2_score(y_true, y_pred):\n    beta = 2\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)), axis=1)\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)), axis=1)\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)), axis=1)\n    \n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    \n    return K.mean(((1+beta**2)*precision*recall) / ((beta**2)*precision+recall+K.epsilon()))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:51:27.995478Z","iopub.execute_input":"2022-09-11T05:51:27.995936Z","iopub.status.idle":"2022-09-11T05:51:28.005947Z","shell.execute_reply.started":"2022-09-11T05:51:27.99573Z","shell.execute_reply":"2022-09-11T05:51:28.004665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = applications.InceptionResNetV2(weights=None, \n                          include_top=False, \n                          input_shape=(img_size, img_size, 3))\nmodel.load_weights('../input/inceptionresnetv2/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:51:28.007559Z","iopub.execute_input":"2022-09-11T05:51:28.008174Z","iopub.status.idle":"2022-09-11T05:52:09.352134Z","shell.execute_reply.started":"2022-09-11T05:51:28.007939Z","shell.execute_reply":"2022-09-11T05:52:09.351237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:52:09.354071Z","iopub.execute_input":"2022-09-11T05:52:09.354614Z","iopub.status.idle":"2022-09-11T05:52:09.358501Z","shell.execute_reply.started":"2022-09-11T05:52:09.354558Z","shell.execute_reply":"2022-09-11T05:52:09.357531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze some layers\n# for layer in model.layers[:-4]:\n#     layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:52:09.359883Z","iopub.execute_input":"2022-09-11T05:52:09.360482Z","iopub.status.idle":"2022-09-11T05:52:09.369441Z","shell.execute_reply.started":"2022-09-11T05:52:09.360338Z","shell.execute_reply":"2022-09-11T05:52:09.368804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding custom layers \nx = model.output\nx = Flatten()(x)\nx = Dense(1024, activation=\"relu\")(x)\nx = Dropout(0.5)(x)\npredictions = Dense(nb_classes, activation=\"softmax\")(x)\nmodel_final = Model(input = model.input, output = predictions)\n\nmodel_final.compile(optimizers.rmsprop(lr=0.001, decay=1e-6),loss=focal_loss,metrics=[f2_score])","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-11T05:52:09.371236Z","iopub.execute_input":"2022-09-11T05:52:09.371723Z","iopub.status.idle":"2022-09-11T05:52:09.60956Z","shell.execute_reply.started":"2022-09-11T05:52:09.371517Z","shell.execute_reply":"2022-09-11T05:52:09.608563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_final.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-11T05:52:09.611122Z","iopub.execute_input":"2022-09-11T05:52:09.611555Z","iopub.status.idle":"2022-09-11T05:52:09.615842Z","shell.execute_reply.started":"2022-09-11T05:52:09.611368Z","shell.execute_reply":"2022-09-11T05:52:09.614688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Callbacks\n\ncheckpoint = ModelCheckpoint(\"model_1.h5\", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:52:09.617258Z","iopub.execute_input":"2022-09-11T05:52:09.617784Z","iopub.status.idle":"2022-09-11T05:52:09.626419Z","shell.execute_reply.started":"2022-09-11T05:52:09.617733Z","shell.execute_reply":"2022-09-11T05:52:09.625735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nhistory = model_final.fit_generator(generator=train_generator,                   \n                                    steps_per_epoch=500,\n                                    validation_data=valid_generator,                    \n                                    validation_steps=200,\n                                    epochs=nb_epochs,\n                                    callbacks = [checkpoint, early],\n                                    max_queue_size=16,\n                                    workers=2,\n                                    use_multiprocessing=True,\n                                    verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:52:09.628937Z","iopub.execute_input":"2022-09-11T05:52:09.629525Z","iopub.status.idle":"2022-09-11T05:59:22.016929Z","shell.execute_reply.started":"2022-09-11T05:52:09.62947Z","shell.execute_reply":"2022-09-11T05:59:22.015878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('history.json', 'w') as f:\n    json.dump(history.history, f)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df[['loss', 'val_loss']].plot()\nhistory_df[['f2_score', 'val_f2_score']].plot()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.022813Z","iopub.execute_input":"2022-09-11T05:59:22.025724Z","iopub.status.idle":"2022-09-11T05:59:22.126268Z","shell.execute_reply.started":"2022-09-11T05:59:22.025612Z","shell.execute_reply":"2022-09-11T05:59:22.058128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sam_sub_df = pd.read_csv('../input/imet-2019-fgvc6/sample_submission.csv')\nsam_sub_df[\"id\"]=sam_sub_df[\"id\"].apply(lambda x:x+\".png\")\nprint(sam_sub_df.shape)\nsam_sub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.060452Z","iopub.status.idle":"2022-09-11T05:59:22.062356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_dataframe(  \n        dataframe=sam_sub_df,\n        directory = \"../input/imet-2019-fgvc6/test\",    \n        x_col=\"id\",\n        target_size = (img_size,img_size),\n        batch_size = 1,\n        shuffle = False,\n        class_mode = None\n        )","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.064242Z","iopub.status.idle":"2022-09-11T05:59:22.064934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_generator.reset()\npredict = model_final.predict_generator(test_generator, steps = len(test_generator.filenames))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.065822Z","iopub.status.idle":"2022-09-11T05:59:22.066566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predict)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-11T05:59:22.067391Z","iopub.status.idle":"2022-09-11T05:59:22.068061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport operator\npredicted_class_indices_3=[]\nfor i in range(len(predict)):         \n    d = {}\n    for index, value in enumerate(predict[i]):               \n        if value > 0.03:            \n            d[index] = value \n    sorted_d = sorted(d.items(), key=operator.itemgetter(1), reverse=True)\n    \n    # Take only first 10 items\n    predicted_class_indices_3.append([i[0] for i in sorted_d[:10]])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.068936Z","iopub.status.idle":"2022-09-11T05:59:22.069662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npredictions_3=[]\n\nfor i in range(len(predicted_class_indices_3)):\n    labels = (train_generator.class_indices)\n    labels = dict((v,k) for k,v in labels.items())\n    predictions = [labels[k] for k in predicted_class_indices_3[i]]\n    predictions_3.append(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.07052Z","iopub.status.idle":"2022-09-11T05:59:22.071209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_3 = []\nfor i in range(len(predictions_3)):\n    str3 = \" \".join(predictions_3[i])\n    predict_3.append(str3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.072158Z","iopub.status.idle":"2022-09-11T05:59:22.072899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id\":filenames,\n                      \"attribute_ids\":predict_3})\nresults['id'] = results['id'].map(lambda x: str(x)[:-4])\nresults.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T05:59:22.073739Z","iopub.status.idle":"2022-09-11T05:59:22.074434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"results.head()","metadata":{}}]}