{"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":"# 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\nfor 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"}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.models import load_model, Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization\nfrom tensorflow.keras.applications.resnet_v2 import ResNet50V2, preprocess_input\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D, Flatten","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv('../input/dog-breed-identification/labels.csv')\ntest_df=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"img_file\"] = train_df[\"id\"] + \".jpg\"\ntest_df['img_file']=test_df['id'] + '.jpg'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file='/kaggle/input/dog-breed-identification/train/'\ntest_file='/kaggle/input/dog-breed-identification/test/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder=LabelEncoder()\nim_size=224","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df['img_file']\ny = train_df['breed']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = np.zeros((len(train_df), im_size, im_size, 3), dtype='float32')\nfor i, img_id in enumerate(X):\n    img = cv2.resize(cv2.imread(train_file+img_id,cv2.IMREAD_COLOR),((im_size,im_size)))\n    img_array = preprocess_input(np.expand_dims(np.array(img[...,::-1].astype(np.float32)).copy(), axis=0))\n    train_x[i] = img_array","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = encoder.fit_transform(train_df[\"breed\"].values)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train_x,train_y,test_size=0.1,random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import CSVLogger\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import the necessary libraries\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import SGD\n\n# Set the image size and number of classes\nimg_size = 299\nnum_classes = 120\n\n# Set the paths to the training and testing directories\ntrain_dir = '/kaggle/input/dog-breed-identification/train/'\ntest_dir = '/kaggle/input/dog-breed-identification/test/'\n\n# Define the data generators for training and testing\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\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,\n    validation_split=0.1)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_dataframe( \n                        dataframe=train_df,\n                        directory=train_file,\n                        x_col='img_file',\n                        y_col='breed',\n                        subset=\"training\",\n                        batch_size=32,\n                        seed=42,\n                        shuffle=True,\n                        target_size=(224,224),\n                        class_mode=\"categorical\"\n                        )    \n     \nvalidation_generator = train_datagen.flow_from_dataframe(\n                        dataframe=train_df,\n                        directory=train_file,\n                        x_col='img_file',\n                        y_col='breed',\n                        subset=\"validation\",\n                        batch_size=32,\n                        seed=None,\n                        shuffle=True,\n                        target_size=(224,224),\n                        class_mode=\"categorical\"\n                        )\ntest_datagen=ImageDataGenerator(rescale=1 /255.0)\n\ntest_generator=test_datagen.flow_from_dataframe(\n                            dataframe=test_df,\n                            directory='/kaggle/input/dog-breed-identification/test',\n                            x_col=\"img_file\",\n                            y_col=None,\n                            batch_size=1,\n                            seed=42,\n                            shuffle=False,\n                            class_mode=None,\n                            target_size=(224,224))\n\n# Load the Xception model\nbase_model = Xception(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3))\n\n# Add a global average pooling layer and a fully connected layer on top\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(512, activation='relu')(x)\npredictions = Dense(num_classes, activation='softmax')(x)\n\n# Create the final model\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# Freeze all the layers in the base model\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Compile the model\nmodel.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])\ncsv_log = CSVLogger('xcepbreed.log', separator=',', append=False)\n\n# Train the model\nhist=model.fit(train_generator, epochs=10, validation_data=validation_generator)\n\n# Freeze all the layers in the base model\nfor layer in base_model.layers[:126]:\n    layer.trainable = False\nfor layer in base_model.layers[126:]:\n    layer.trainable = True\n    \nmodel.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])\nhist=model.fit(train_generator, epochs=1, validation_data=test_generator)\n\n# Save the model\nmodel.save('dog_breed_model.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(hist.history).plot()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(validation_generator)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=model.predict(test_generator, steps = len(test_generator.filenames))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict[:5]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading the testset\n\ntest_images = os.listdir('/kaggle/input/dog-breed-identification/test/')\ntest_set = pd.DataFrame(test_images, columns=['img_file'])\ntest_set.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.DataFrame(columns=[\"img_file\"] + [*train_generator.class_indices.keys()])\nresults","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels = list(labels.keys())\ndf = pd.DataFrame(data=predict,\n                 columns=labels)\n\ncolumns = list(df)\ncolumns.sort()\ndf = df.reindex(columns=columns)\n\nfilenames = test_df[\"id\"]\ndf[\"id\"]  = filenames\ndf['img_file']=df['id']+'.jpg'\n\ncols = df.columns.tolist()\ncols = cols[-1:] + cols[:-1]\ndf = df[cols]\ndf.head(5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[:, 0] = df.iloc[:, -1]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.iloc[:, :-1]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.rename(columns={'img_file': 'id'}, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}