{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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# '/kaggle/input'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.layers import Dense,Dropout,Input,MaxPooling2D,ZeroPadding2D,Conv2D,Flatten,BatchNormalization\nfrom keras.models import Sequential\nfrom keras.losses import categorical_crossentropy\nfrom keras.optimizers import Adam,SGD\nfrom keras.preprocessing.image import img_to_array,load_img,ImageDataGenerator\nfrom keras.utils import to_categorical\n\nimport sys\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\npd.options.display.max_colwidth=150","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df1=pd.read_csv('../input/dog-breed-identification/labels.csv')\ndf1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# path of the dogs images\nimg_file='../input/dog-breed-identification/train/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=df1.assign(img_path=lambda x: img_file + x['id'] +'.jpg')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_pixel=np.array([img_to_array(dtype='int8',img=load_img(img, target_size=(128, 128))) for img in df['img_path'].values.tolist()])\nsys.getsizeof(img_pixel)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_label=df.breed\nimg_label=pd.get_dummies(df.breed).astype('int8')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X=img_pixel\ny=img_label.values\nprint(X.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)\n\ndel df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=30,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\n\ntest_datagen=ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_set=train_datagen.flow(X_train,y=y_train,batch_size=32)\ntesting_set=test_datagen.flow(X_test,y=y_test,batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32,input_shape=(128,128,3),kernel_size=(3,3),activation='relu',padding='same'))\n\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(256,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(512,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(120,activation='softmax'))\n\nmodel.summary()\n\nmodel.compile(optimizer='Adam',\n          loss='categorical_crossentropy', \n           metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit_generator(training_set,\n                      steps_per_epoch = X_train.shape[0]/32,\n                      validation_data = testing_set,\n                      validation_steps = 4,\n                      epochs = 500,\n                      verbose = 1)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}