{"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\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-10T12:47:49.419573Z","iopub.execute_input":"2023-01-10T12:47:49.419981Z","iopub.status.idle":"2023-01-10T12:47:49.713972Z","shell.execute_reply.started":"2023-01-10T12:47:49.419949Z","shell.execute_reply":"2023-01-10T12:47:49.712154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Here we will import and split the dataset\ndf = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:57:44.961872Z","iopub.execute_input":"2023-01-10T12:57:44.962214Z","iopub.status.idle":"2023-01-10T12:57:44.979777Z","shell.execute_reply.started":"2023-01-10T12:57:44.962185Z","shell.execute_reply":"2023-01-10T12:57:44.978876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_val = train_test_split(df, test_size=0.2, random_state=42)\ndf_train = df_train.reset_index(drop=True)\ndf_val = df_val.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:57:45.421825Z","iopub.execute_input":"2023-01-10T12:57:45.423107Z","iopub.status.idle":"2023-01-10T12:57:45.432206Z","shell.execute_reply.started":"2023-01-10T12:57:45.423042Z","shell.execute_reply":"2023-01-10T12:57:45.431039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = data.sample(frac=0.8,random_state=200)\nval_data = data.drop(train_data.index)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:59:50.524002Z","iopub.execute_input":"2023-01-10T12:59:50.524377Z","iopub.status.idle":"2023-01-10T12:59:50.534958Z","shell.execute_reply.started":"2023-01-10T12:59:50.524346Z","shell.execute_reply":"2023-01-10T12:59:50.533608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define data generators\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   rotation_range = 40,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True)\nval_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:59:51.037603Z","iopub.execute_input":"2023-01-10T12:59:51.038014Z","iopub.status.idle":"2023-01-10T12:59:51.045009Z","shell.execute_reply.started":"2023-01-10T12:59:51.037981Z","shell.execute_reply":"2023-01-10T12:59:51.043496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create the generators\ntrain_generator=train_datagen.flow_from_dataframe(dataframe=df_train, \n                                                 directory=\"../input/dog-breed-identification/train\",\n                                                 x_col='id', y_col='breed', \n                                                 class_mode=\"categorical\",\n                                                 target_size=(224,224), \n                                                 batch_size=32)\nval_generator=val_datagen.flow_from_dataframe(dataframe=df_val, \n                                              directory=\"../input/dog-breed-identification/train\",\n                                              x_col='id', y_col='breed', \n                                              class_mode=\"categorical\",\n                                              target_size=(224,224), \n                                              batch_size=32)\n\n# rest of the model architecture\nbase_model = keras.applications.ResNet50(weights='imagenet',\n                                include_top=False,\n                                input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:59:51.307097Z","iopub.execute_input":"2023-01-10T12:59:51.307459Z","iopub.status.idle":"2023-01-10T12:59:52.788518Z","shell.execute_reply.started":"2023-01-10T12:59:51.30743Z","shell.execute_reply":"2023-01-10T12:59:52.787597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze the layers\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:52:13.475075Z","iopub.execute_input":"2023-01-10T12:52:13.475444Z","iopub.status.idle":"2023-01-10T12:52:13.485614Z","shell.execute_reply.started":"2023-01-10T12:52:13.475411Z","shell.execute_reply":"2023-01-10T12:52:13.484554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model(x, training=False)\n#add a global spatial average pooling layer\nx = keras.layers.GlobalAveragePooling2D()(x)\n#add dropout for regularization\nx = keras.layers.Dropout(0.2)(x)\n#add a fully connected layer with 120 output (as we have 120 breeds)\nx = keras.layers.Dense(120, activation='softmax')(x)\nmodel = keras.Model(inputs, x)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:38:16.00819Z","iopub.execute_input":"2023-01-10T12:38:16.008539Z","iopub.status.idle":"2023-01-10T12:38:16.393865Z","shell.execute_reply.started":"2023-01-10T12:38:16.008514Z","shell.execute_reply":"2023-01-10T12:38:16.39268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:38:24.331636Z","iopub.execute_input":"2023-01-10T12:38:24.333208Z","iopub.status.idle":"2023-01-10T12:38:24.349104Z","shell.execute_reply.started":"2023-01-10T12:38:24.333154Z","shell.execute_reply":"2023-01-10T12:38:24.347525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train the model\nepochs = 10\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch = train_generator.n // train_generator.batch_size,\n    validation_data = val_generator, \n    validation_steps = val_generator.n // val_generator.batch_size,\n    epochs = epochs\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T12:38:28.739475Z","iopub.execute_input":"2023-01-10T12:38:28.739837Z","iopub.status.idle":"2023-01-10T12:38:28.774469Z","shell.execute_reply.started":"2023-01-10T12:38:28.739807Z","shell.execute_reply":"2023-01-10T12:38:28.77317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}