{"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\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt \nimport cv2, os\nfrom PIL import Image\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:49:00.997853Z","iopub.execute_input":"2023-02-18T13:49:00.998437Z","iopub.status.idle":"2023-02-18T13:49:11.067011Z","shell.execute_reply.started":"2023-02-18T13:49:00.998323Z","shell.execute_reply":"2023-02-18T13:49:11.06534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import kaggle","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:49:26.943457Z","iopub.execute_input":"2023-02-18T13:49:26.944249Z","iopub.status.idle":"2023-02-18T13:49:27.3367Z","shell.execute_reply.started":"2023-02-18T13:49:26.944205Z","shell.execute_reply":"2023-02-18T13:49:27.335349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/jsonfile/kaggle.json /root/.kaggle","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:52:45.86997Z","iopub.execute_input":"2023-02-18T13:52:45.870471Z","iopub.status.idle":"2023-02-18T13:52:46.990663Z","shell.execute_reply.started":"2023-02-18T13:52:45.870431Z","shell.execute_reply":"2023-02-18T13:52:46.989229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! kaggle competitions download -c dog-breed-identification","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:53:26.737775Z","iopub.execute_input":"2023-02-18T13:53:26.738254Z","iopub.status.idle":"2023-02-18T13:53:32.29761Z","shell.execute_reply.started":"2023-02-18T13:53:26.738214Z","shell.execute_reply":"2023-02-18T13:53:32.296413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q dog-breed-identification.zip","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:53:45.347308Z","iopub.execute_input":"2023-02-18T13:53:45.347925Z","iopub.status.idle":"2023-02-18T13:53:55.967547Z","shell.execute_reply.started":"2023-02-18T13:53:45.347882Z","shell.execute_reply":"2023-02-18T13:53:55.966024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('train')[:10]","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:54:20.478454Z","iopub.execute_input":"2023-02-18T13:54:20.478964Z","iopub.status.idle":"2023-02-18T13:54:20.501058Z","shell.execute_reply.started":"2023-02-18T13:54:20.478927Z","shell.execute_reply":"2023-02-18T13:54:20.499713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:55:02.737167Z","iopub.execute_input":"2023-02-18T13:55:02.738196Z","iopub.status.idle":"2023-02-18T13:55:02.761372Z","shell.execute_reply.started":"2023-02-18T13:55:02.738143Z","shell.execute_reply":"2023-02-18T13:55:02.759217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:55:13.110092Z","iopub.execute_input":"2023-02-18T13:55:13.11064Z","iopub.status.idle":"2023-02-18T13:55:13.133029Z","shell.execute_reply.started":"2023-02-18T13:55:13.11059Z","shell.execute_reply":"2023-02-18T13:55:13.131826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape # 10,222 images","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:55:32.750489Z","iopub.execute_input":"2023-02-18T13:55:32.750979Z","iopub.status.idle":"2023-02-18T13:55:32.760545Z","shell.execute_reply.started":"2023-02-18T13:55:32.750944Z","shell.execute_reply":"2023-02-18T13:55:32.759332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:55:44.711578Z","iopub.execute_input":"2023-02-18T13:55:44.712023Z","iopub.status.idle":"2023-02-18T13:55:44.723979Z","shell.execute_reply.started":"2023-02-18T13:55:44.711985Z","shell.execute_reply":"2023-02-18T13:55:44.72285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.breed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:57:23.431197Z","iopub.execute_input":"2023-02-18T13:57:23.431691Z","iopub.status.idle":"2023-02-18T13:57:23.451841Z","shell.execute_reply.started":"2023-02-18T13:57:23.431656Z","shell.execute_reply":"2023-02-18T13:57:23.450337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.breed.value_counts().plot.bar()\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:57:44.08816Z","iopub.execute_input":"2023-02-18T13:57:44.088716Z","iopub.status.idle":"2023-02-18T13:57:46.035835Z","shell.execute_reply.started":"2023-02-18T13:57:44.088673Z","shell.execute_reply":"2023-02-18T13:57:46.034439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = tf.keras.preprocessing.image.load_img(\"train/0021f9ceb3235effd7fcde7f7538ed62.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:58:55.782372Z","iopub.execute_input":"2023-02-18T13:58:55.782829Z","iopub.status.idle":"2023-02-18T13:58:55.789131Z","shell.execute_reply.started":"2023-02-18T13:58:55.78279Z","shell.execute_reply":"2023-02-18T13:58:55.787628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image","metadata":{"execution":{"iopub.status.busy":"2023-02-18T13:59:24.041051Z","iopub.execute_input":"2023-02-18T13:59:24.041552Z","iopub.status.idle":"2023-02-18T13:59:24.148965Z","shell.execute_reply.started":"2023-02-18T13:59:24.041515Z","shell.execute_reply":"2023-02-18T13:59:24.14759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[\"filename\"] = labels[\"id\"] + \".jpg\"","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:00:16.716848Z","iopub.execute_input":"2023-02-18T14:00:16.718562Z","iopub.status.idle":"2023-02-18T14:00:16.730023Z","shell.execute_reply.started":"2023-02-18T14:00:16.718472Z","shell.execute_reply":"2023-02-18T14:00:16.728596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:00:38.497364Z","iopub.execute_input":"2023-02-18T14:00:38.497778Z","iopub.status.idle":"2023-02-18T14:00:38.511909Z","shell.execute_reply.started":"2023-02-18T14:00:38.497747Z","shell.execute_reply":"2023-02-18T14:00:38.510656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idg2 = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1/255.0, \n    validation_split=0.1, \n    rotation_range = 30, \n    horizontal_flip=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:00:54.911422Z","iopub.execute_input":"2023-02-18T14:00:54.911907Z","iopub.status.idle":"2023-02-18T14:00:54.91845Z","shell.execute_reply.started":"2023-02-18T14:00:54.91187Z","shell.execute_reply":"2023-02-18T14:00:54.917134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idg = idg2.flow_from_dataframe(labels, directory=\"train/\", x_col='filename', y_col='breed', target_size=(224, 224), batch_size=64, subset='training')\nval_idg   = idg2.flow_from_dataframe(labels, directory=\"train/\", x_col='filename', y_col='breed', target_size=(224, 224), batch_size=64, subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:01:19.355569Z","iopub.execute_input":"2023-02-18T14:01:19.356002Z","iopub.status.idle":"2023-02-18T14:01:19.574455Z","shell.execute_reply.started":"2023-02-18T14:01:19.355969Z","shell.execute_reply":"2023-02-18T14:01:19.573015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nmodel = tf.keras.models.Sequential()\n#Convolution\n#32 is number of kernals of 3x3, we can use 64 128 256 etc in next layers\n#input shape can be 128, 224 later\nmodel.add(tf.keras.layers.Conv2D(32,(3,3),input_shape=(224,224,3),activation='relu'))\n#Max Pooling size reduces divided by 2\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))      \n\n\n#model.add(Dropout(0.5))\n\nmodel.add(tf.keras.layers.Conv2D(32,(3,3), activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))\n#model.add(Dropout(0.25))\n\nmodel.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n#clf.add(Dropout(0.10))\n#Flattening\nmodel.add(tf.keras.layers.Flatten())\n        \n#Adding An ANN\n#lets take 128 hidden nodes in hidden layer\n#clf.add(Dense(units=128,activation='relu'))\nmodel.add(tf.keras.layers.Dense(units=64, activation='relu')) \nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(units=120,activation='softmax'))\n#stochastic gradient descent -Adam -optimizer\n#loss func categorical cross entropy\n#metrics = accuracy\nmodel.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:02:26.051085Z","iopub.execute_input":"2023-02-18T14:02:26.051618Z","iopub.status.idle":"2023-02-18T14:02:26.186143Z","shell.execute_reply.started":"2023-02-18T14:02:26.051575Z","shell.execute_reply":"2023-02-18T14:02:26.184754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TensorBoard\n\n# Tell Colab about TensorBoard\n%load_ext tensorboard\n# TensorBoard please read the log data from folder named 'logs'\n%tensorboard --logdir logs","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:02:53.289314Z","iopub.execute_input":"2023-02-18T14:02:53.289753Z","iopub.status.idle":"2023-02-18T14:02:59.84686Z","shell.execute_reply.started":"2023-02-18T14:02:53.289719Z","shell.execute_reply":"2023-02-18T14:02:59.845314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\nearly_stopping_monitor=EarlyStopping(patience=6)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:03:39.467022Z","iopub.execute_input":"2023-02-18T14:03:39.467522Z","iopub.status.idle":"2023-02-18T14:03:39.474451Z","shell.execute_reply.started":"2023-02-18T14:03:39.467478Z","shell.execute_reply":"2023-02-18T14:03:39.473078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf_callback = tf.keras.callbacks.TensorBoard(log_dir=\"logs\", histogram_freq=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:04:19.539564Z","iopub.execute_input":"2023-02-18T14:04:19.540035Z","iopub.status.idle":"2023-02-18T14:04:19.547151Z","shell.execute_reply.started":"2023-02-18T14:04:19.54Z","shell.execute_reply":"2023-02-18T14:04:19.545916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(train_idg, validation_data=val_idg, epochs=30,callbacks = [tf_callback] )","metadata":{"execution":{"iopub.status.busy":"2023-02-18T14:04:28.02944Z","iopub.execute_input":"2023-02-18T14:04:28.029855Z"},"trusted":true},"execution_count":null,"outputs":[]}]}