{"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 tensorflow as tf\nimport os\nimport random\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm import tqdm \n\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:11:43.443053Z","iopub.execute_input":"2021-09-29T08:11:43.443610Z","iopub.status.idle":"2021-09-29T08:11:50.848159Z","shell.execute_reply.started":"2021-09-29T08:11:43.443508Z","shell.execute_reply":"2021-09-29T08:11:50.847071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed = seed\n\nIMG_WIDTH = 64\nIMG_HEIGHT = 64\nIMG_CHANNELS = 3\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:50:50.449681Z","iopub.execute_input":"2021-09-29T09:50:50.450015Z","iopub.status.idle":"2021-09-29T09:50:50.455572Z","shell.execute_reply.started":"2021-09-29T09:50:50.449983Z","shell.execute_reply":"2021-09-29T09:50:50.454795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/petfinder-pawpularity-score/train.csv\")\ntest = pd.read_csv(\"../input/petfinder-pawpularity-score/test.csv\")\nsample_submission = pd.read_csv(\"../input/petfinder-pawpularity-score/sample_submission.csv\")\n\ntrain[\"file_path\"] = train[\"Id\"].apply(lambda identifier: \"../input/petfinder-pawpularity-score/train/\" + identifier + \".jpg\")\ntest[\"file_path\"] = test[\"Id\"].apply(lambda identifier: \"../input/petfinder-pawpularity-score/test/\" + identifier + \".jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:50:51.179951Z","iopub.execute_input":"2021-09-29T09:50:51.181087Z","iopub.status.idle":"2021-09-29T09:50:51.258259Z","shell.execute_reply.started":"2021-09-29T09:50:51.181037Z","shell.execute_reply":"2021-09-29T09:50:51.257213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g=[]\nfor n, id_ in tqdm(enumerate(train[\"file_path\"]), total=len(train[\"file_path\"])):\n    image_string = tf.io.read_file(id_)\n    image = tf.image.decode_jpeg(image_string, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    image = tf.image.central_crop(image, 1.0)\n    image = tf.image.resize(image, (IMG_HEIGHT, IMG_WIDTH))\n    g.append(image)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:50:52.053089Z","iopub.execute_input":"2021-09-29T09:50:52.053566Z","iopub.status.idle":"2021-09-29T09:52:41.107246Z","shell.execute_reply.started":"2021-09-29T09:50:52.053517Z","shell.execute_reply":"2021-09-29T09:52:41.105185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r=[]\nfor n, id_ in tqdm(enumerate(test[\"file_path\"]), total=len(test[\"file_path\"])):\n    image_string = tf.io.read_file(id_)\n    image = tf.image.decode_jpeg(image_string, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    image = tf.image.central_crop(image, 1.0)\n    image = tf.image.resize(image, (IMG_HEIGHT, IMG_WIDTH))\n    r.append(image)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:50.240594Z","iopub.execute_input":"2021-09-29T09:52:50.240917Z","iopub.status.idle":"2021-09-29T09:52:50.272061Z","shell.execute_reply.started":"2021-09-29T09:52:50.240886Z","shell.execute_reply":"2021-09-29T09:52:50.271089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imshow(r[5].numpy())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:51.322909Z","iopub.execute_input":"2021-09-29T09:52:51.323945Z","iopub.status.idle":"2021-09-29T09:52:51.575058Z","shell.execute_reply.started":"2021-09-29T09:52:51.323885Z","shell.execute_reply":"2021-09-29T09:52:51.573960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimshow(g[5].numpy())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:52.403070Z","iopub.execute_input":"2021-09-29T09:52:52.403424Z","iopub.status.idle":"2021-09-29T09:52:52.662993Z","shell.execute_reply.started":"2021-09-29T09:52:52.403390Z","shell.execute_reply":"2021-09-29T09:52:52.661914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmse(y_true, y_pred):\n    return tf.sqrt(tf.reduce_mean((y_true - y_pred) ** 2))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:53.561439Z","iopub.execute_input":"2021-09-29T09:52:53.561824Z","iopub.status.idle":"2021-09-29T09:52:53.567609Z","shell.execute_reply.started":"2021-09-29T09:52:53.561790Z","shell.execute_reply":"2021-09-29T09:52:53.566706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def block(x, filters, kernel_size, repetitions, pool_size=2, strides=2):\n    for i in range(repetitions):\n        x = tf.keras.layers.Conv2D(filters, kernel_size, activation='relu', padding='same')(x)\n    x = tf.keras.layers.MaxPooling2D(2, 2)(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:55.131546Z","iopub.execute_input":"2021-09-29T09:52:55.131935Z","iopub.status.idle":"2021-09-29T09:52:55.139011Z","shell.execute_reply.started":"2021-09-29T09:52:55.131899Z","shell.execute_reply":"2021-09-29T09:52:55.138043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    early_stop = tf.keras.callbacks.EarlyStopping(\n        monitor='val_rmse', \n        mode=\"min\",\n        min_delta=1e-4, \n        patience=10\n    )\n    reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(\n        monitor='val_rmse', \n        mode=\"min\",\n        factor=0.5,\n        patience=2, \n        min_lr=1e-6\n    )\n    callbacks = [early_stop, reduce_lr]\n    \n    loss = tf.keras.losses.MeanSquaredError()\n\n    optimizer = tf.keras.optimizers.Adam()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:52:55.949325Z","iopub.execute_input":"2021-09-29T09:52:55.950422Z","iopub.status.idle":"2021-09-29T09:52:55.956835Z","shell.execute_reply.started":"2021-09-29T09:52:55.950381Z","shell.execute_reply":"2021-09-29T09:52:55.956210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom skimage import io\n\n# Construct an instance of the ImageDataGenerator class\n# Pass the augmentation parameters through the constructor. \n\ndatagen = ImageDataGenerator(\n        rotation_range=45,     #Random rotation between 0 and 45\n        width_shift_range=0.2,   #% shift\n        height_shift_range=0.2,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        fill_mode='constant', cval=125) ","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:57:52.134578Z","iopub.execute_input":"2021-09-29T09:57:52.135094Z","iopub.status.idle":"2021-09-29T09:57:52.143336Z","shell.execute_reply.started":"2021-09-29T09:57:52.135061Z","shell.execute_reply":"2021-09-29T09:57:52.142009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(g)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:57:53.284472Z","iopub.execute_input":"2021-09-29T09:57:53.285224Z","iopub.status.idle":"2021-09-29T09:57:54.138711Z","shell.execute_reply.started":"2021-09-29T09:57:53.285180Z","shell.execute_reply":"2021-09-29T09:57:54.137996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import  keras\nINPUT_SHAPE = (64, 64, 3)   \n#models = []\n#historys = []\n#kfold = KFold(n_splits=5, shuffle=True, random_state=997)\n\nimage_inputs = tf.keras.Input(INPUT_SHAPE)\n\nimage_x = block(image_inputs, 8, 3, 2)\nimage_x = block(image_x, 16, 3, 2)\nimage_x = block(image_x, 32, 3, 2)\nimage_x = block(image_x, 64, 3, 2)\nimage_x = block(image_x, 128, 3, 2)\nimage_x = tf.keras.layers.Flatten()(image_x)\nimage_x = tf.keras.layers.Dense(64, activation=\"relu\", kernel_regularizer=tf.keras.regularizers.l2())(image_x)\n\n\nout = keras.layers.Dense(1)(image_x)   \n\nmodel = keras.Model(inputs=image_inputs, outputs=out)\n\n\n\nmodel.compile(optimizer=optimizer,\n                loss=loss,   #Check between binary_crossentropy and categorical_crossentropy\n                metrics=[tf.keras.metrics.RootMeanSquaredError(name=\"rmse\"), \"mae\", \"mape\"])\nprint(model.summary())\n    \n    \n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:54:00.104159Z","iopub.execute_input":"2021-09-29T09:54:00.105095Z","iopub.status.idle":"2021-09-29T09:54:00.266254Z","shell.execute_reply.started":"2021-09-29T09:54:00.105033Z","shell.execute_reply":"2021-09-29T09:54:00.265306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for index, (train_indices, val_indices) in enumerate(kfold.split(g)):\n #   x_train = g.loc[train_indices]\n    \n  #  x_val= train.loc[val_indices]\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(np.array(g),train[\"Pawpularity\"]),\n                         \n                         verbose = 1, \n                         epochs = 300,      #Changed to 3 from 50 for testing purposes.\n                         \n                         shuffle = True,\n                         )\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:58:15.973720Z","iopub.execute_input":"2021-09-29T09:58:15.974682Z","iopub.status.idle":"2021-09-29T10:04:56.898070Z","shell.execute_reply.started":"2021-09-29T09:58:15.974623Z","shell.execute_reply":"2021-09-29T10:04:56.896343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(np.array(g), \n                         train[\"Pawpularity\"], \n                         batch_size = 300, \n                         verbose = 1, \n                         epochs = 300,      #Changed to 3 from 50 for testing purposes.\n                         validation_split = 0.20,\n                         shuffle = True, \n                         callbacks=callbacks\n                    \n                      \n                     )\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:28:07.805982Z","iopub.execute_input":"2021-09-29T09:28:07.806595Z","iopub.status.idle":"2021-09-29T09:45:10.460152Z","shell.execute_reply.started":"2021-09-29T09:28:07.806560Z","shell.execute_reply":"2021-09-29T09:45:10.458896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(np.array(g), \n                         train[\"Pawpularity\"], \n                         batch_size = 300, \n                         verbose = 1, \n                         epochs = 300,      #Changed to 3 from 50 for testing purposes.\n                         validation_split = 0.20,\n                         shuffle = True, \n                         callbacks=callbacks\n                    \n                      \n                     )","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:54:08.222847Z","iopub.execute_input":"2021-09-29T09:54:08.223718Z","iopub.status.idle":"2021-09-29T09:57:38.746920Z","shell.execute_reply.started":"2021-09-29T09:54:08.223652Z","shell.execute_reply":"2021-09-29T09:57:38.745737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('malaria_cnn.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:39:51.166566Z","iopub.execute_input":"2021-09-29T08:39:51.166878Z","iopub.status.idle":"2021-09-29T08:39:51.238929Z","shell.execute_reply.started":"2021-09-29T08:39:51.166847Z","shell.execute_reply":"2021-09-29T08:39:51.237537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission[\"Pawpularity\"]=model.predict(\n    np.array(r),batch_size = 300, verbose = 1,  callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:49:32.409266Z","iopub.execute_input":"2021-09-29T08:49:32.409872Z","iopub.status.idle":"2021-09-29T08:49:32.487432Z","shell.execute_reply.started":"2021-09-29T08:49:32.409811Z","shell.execute_reply":"2021-09-29T08:49:32.486322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsample_submission.to_csv(\"submission.csv\", index=False)\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:49:41.456186Z","iopub.execute_input":"2021-09-29T08:49:41.457654Z","iopub.status.idle":"2021-09-29T08:49:41.482298Z","shell.execute_reply.started":"2021-09-29T08:49:41.457569Z","shell.execute_reply":"2021-09-29T08:49:41.481358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}