{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n   # for filename in filenames:\n   #     print(os.path.join(dirname, filename))\n#\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-27T15:14:31.549774Z","iopub.execute_input":"2022-03-27T15:14:31.550051Z","iopub.status.idle":"2022-03-27T15:14:31.55462Z","shell.execute_reply.started":"2022-03-27T15:14:31.550024Z","shell.execute_reply":"2022-03-27T15:14:31.553712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/happy-whale-and-dolphin/train.csv')\ntrain_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:14:35.587108Z","iopub.execute_input":"2022-03-27T15:14:35.587419Z","iopub.status.idle":"2022-03-27T15:14:35.707069Z","shell.execute_reply.started":"2022-03-27T15:14:35.587386Z","shell.execute_reply":"2022-03-27T15:14:35.706121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:14:44.892756Z","iopub.execute_input":"2022-03-27T15:14:44.893065Z","iopub.status.idle":"2022-03-27T15:14:44.938671Z","shell.execute_reply.started":"2022-03-27T15:14:44.893036Z","shell.execute_reply":"2022-03-27T15:14:44.937724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get InceptionV3","metadata":{}},{"cell_type":"code","source":"!wget --no-check-certificate \\\n    https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \\\n    -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:14:49.264224Z","iopub.execute_input":"2022-03-27T15:14:49.264927Z","iopub.status.idle":"2022-03-27T15:14:50.645968Z","shell.execute_reply.started":"2022-03-27T15:14:49.264891Z","shell.execute_reply":"2022-03-27T15:14:50.645022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\n\n# Set the weights file you downloaded into a variable\nlocal_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n# Initialize the base model.\n# Set the input shape and remove the dense layers.\npre_trained_model = InceptionV3(input_shape = (75, 75, 3), \n                                include_top = False, \n                                weights = None)\n\n# Load the pre-trained weights you downloaded.\npre_trained_model.load_weights(local_weights_file)\n\n# Freeze the weights of the layers.\nfor layer in pre_trained_model.layers:\n  layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:15:34.535238Z","iopub.execute_input":"2022-03-27T15:15:34.535559Z","iopub.status.idle":"2022-03-27T15:15:36.841763Z","shell.execute_reply.started":"2022-03-27T15:15:34.535523Z","shell.execute_reply":"2022-03-27T15:15:36.84109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choose `mixed_7` as the last layer of your base model\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:16:23.933306Z","iopub.execute_input":"2022-03-27T15:16:23.933791Z","iopub.status.idle":"2022-03-27T15:16:23.940295Z","shell.execute_reply.started":"2022-03-27T15:16:23.93375Z","shell.execute_reply":"2022-03-27T15:16:23.939458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras import Model\n\n# Flatten the output layer to 1 dimension\nx = layers.Flatten()(last_output)\n# Add a fully connected layer with 1,024 hidden units and ReLU activation\nx = layers.Dense(1024, activation='relu')(x)\n# Add a dropout rate of 0.2\nlayers.Dropout(0.2)(x)                  \n# Add a final sigmoid layer for classification\nx = layers.Dense  (15587, activation='softmax')(x)           \n\n# Append the dense network to the base model\nmodel = Model(pre_trained_model.input, x) \n\n# Print the model summary. See your dense network connected at the end.\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:16:32.359577Z","iopub.execute_input":"2022-03-27T15:16:32.360196Z","iopub.status.idle":"2022-03-27T15:16:32.671561Z","shell.execute_reply.started":"2022-03-27T15:16:32.360155Z","shell.execute_reply":"2022-03-27T15:16:32.67077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the training parameters\nmodel.compile(optimizer = RMSprop(learning_rate=0.0001), \n              loss = 'categorical_crossentropy', \n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:16:45.724965Z","iopub.execute_input":"2022-03-27T15:16:45.725255Z","iopub.status.idle":"2022-03-27T15:16:45.744434Z","shell.execute_reply.started":"2022-03-27T15:16:45.725223Z","shell.execute_reply":"2022-03-27T15:16:45.743816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preparing data","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   height_shift_range = 0.1,\n                                   width_shift_range = 0.1,\n                                validation_split = 0.2,\n                                horizontal_flip = True)\n\nvalid_datagen = ImageDataGenerator(rescale = 1./255,\n                                  validation_split = 0.2)\n\ntrain_dir = '/kaggle/input/happy-whale-and-dolphin/train_images/'\n\ntrain_set = train_datagen.flow_from_dataframe(train_df, train_dir,\n                                              seed = 101,\n                                            target_size = (75,75),\n                                            batch_size = 32,\n                                            x_col='image',\n                                            y_col='individual_id',\n                                            class_mode = 'categorical',\n                                            subset = 'training')\n\nvalid_set = valid_datagen.flow_from_dataframe(train_df, \n                                              train_dir,\n                                                seed = 101,\n                                                target_size = (75,75),\n                                              x_col='image',\n                                                y_col='individual_id',\n                                                batch_size = 32,\n                                                class_mode = 'categorical',\n                                                subset = 'validation')","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:17:10.168432Z","iopub.execute_input":"2022-03-27T15:17:10.168735Z","iopub.status.idle":"2022-03-27T15:17:59.097213Z","shell.execute_reply.started":"2022-03-27T15:17:10.168701Z","shell.execute_reply":"2022-03-27T15:17:59.096383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(train_set,\n                 epochs = 20,\n                 validation_data = valid_set,\n                 batch_size = 40000,\n                 verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:18:15.827687Z","iopub.execute_input":"2022-03-27T15:18:15.828023Z","iopub.status.idle":"2022-03-27T18:20:28.156304Z","shell.execute_reply.started":"2022-03-27T15:18:15.827987Z","shell.execute_reply":"2022-03-27T18:20:28.15167Z"},"trusted":true},"execution_count":null,"outputs":[]}]}