{"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 os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom PIL import Image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator \nfrom tensorflow.keras.utils import to_categorical, plot_model\nfrom tensorflow.keras import models, layers, regularizers\nimport cv2\nimport numpy as np\nfrom sklearn.metrics import accuracy_score\nfrom collections import Counter","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:26:04.139571Z","iopub.execute_input":"2022-03-09T06:26:04.139866Z","iopub.status.idle":"2022-03-09T06:26:11.340983Z","shell.execute_reply.started":"2022-03-09T06:26:04.139781Z","shell.execute_reply":"2022-03-09T06:26:11.339985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path='../input/dog-vs-cat-classification/train/train' \ntest_path='../input/dog-vs-cat-classification/test/test'","metadata":{"execution":{"iopub.status.busy":"2022-03-08T07:46:20.69582Z","iopub.execute_input":"2022-03-08T07:46:20.696515Z","iopub.status.idle":"2022-03-08T07:46:20.701891Z","shell.execute_reply.started":"2022-03-08T07:46:20.696481Z","shell.execute_reply":"2022-03-08T07:46:20.698982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_bar_chart_diagram(path_data):\n  dic={}\n  for emotion in  os.listdir(path_data):\n    dem=0\n    for x in os.listdir(path_data+\"/\"+emotion):\n      dem+=1\n    dic[emotion]=dem\n  print(dic)\n  barlist=plt.bar(range(len(dic)), list(dic.values()),tick_label=list(dic.keys()))\n#set color\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:43:58.061309Z","iopub.execute_input":"2022-03-08T02:43:58.062015Z","iopub.status.idle":"2022-03-08T02:43:58.068414Z","shell.execute_reply.started":"2022-03-08T02:43:58.061981Z","shell.execute_reply":"2022-03-08T02:43:58.067681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar_chart_diagram(train_path)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:44:00.600628Z","iopub.execute_input":"2022-03-08T02:44:00.601206Z","iopub.status.idle":"2022-03-08T02:44:01.324554Z","shell.execute_reply.started":"2022-03-08T02:44:00.601169Z","shell.execute_reply":"2022-03-08T02:44:01.323764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\ntarget_size = (248,248)\n\ntrain_datagen = ImageDataGenerator(rescale=1./255.0,\n                                   validation_split=0.2,\n                                   zoom_range=[0.8,1.5],\n                                  horizontal_flip=True,\n                                   shear_range=10,\n                                   brightness_range=[0.2,2.0],\n                                   rotation_range=45\n                                  )\ntest_datagen=ImageDataGenerator(rescale = 1./255.0)\n\ntrain_generator = train_datagen.flow_from_directory(\n        train_path,\n        target_size=target_size,\n        batch_size=batch_size,\n        subset=\"training\",\n        class_mode='binary')\n\nval_generator = train_datagen.flow_from_directory(\n        train_path,\n        target_size=target_size,\n        batch_size=batch_size,\n        subset=\"validation\",\n        class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:44:04.5134Z","iopub.execute_input":"2022-03-08T02:44:04.513672Z","iopub.status.idle":"2022-03-08T02:44:17.086659Z","shell.execute_reply.started":"2022-03-08T02:44:04.513642Z","shell.execute_reply":"2022-03-08T02:44:17.085915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input_shape = (248,248,3)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:45:04.794473Z","iopub.execute_input":"2022-03-08T02:45:04.794755Z","iopub.status.idle":"2022-03-08T02:45:04.800105Z","shell.execute_reply.started":"2022-03-08T02:45:04.794709Z","shell.execute_reply":"2022-03-08T02:45:04.79888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow as tf\n# Xception=tf.keras.applications.Xception(weights='imagenet',input_shape=input_shape, include_top=False)\n# for layer in MobileNetV3.layers:\n#     layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:45:13.696892Z","iopub.execute_input":"2022-03-08T02:45:13.697166Z","iopub.status.idle":"2022-03-08T02:45:19.133986Z","shell.execute_reply.started":"2022-03-08T02:45:13.697136Z","shell.execute_reply":"2022-03-08T02:45:19.133211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Build Model\n# model = models.Sequential()\n# # load the model\n# model.add(Xception)\n\n# model.add(layers.Flatten())\n# model.add(layers.Dense(1024, activation='relu'))\n# model.add(layers.Dropout(0.75))\n# model.add(layers.Dense(512, activation='relu'))\n# model.add(layers.Dropout(0.50))\n# model.add(layers.Dense(256, activation='relu'))\n# model.add(layers.Dropout(0.25))\n# model.add(layers.Dense(128,activation='relu'))\n# model.add(layers.Dropout(0.25))\n# model.add(layers.Dense(1, activation='sigmoid'))\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:45:25.545137Z","iopub.execute_input":"2022-03-08T02:45:25.545704Z","iopub.status.idle":"2022-03-08T02:45:25.924153Z","shell.execute_reply.started":"2022-03-08T02:45:25.545664Z","shell.execute_reply":"2022-03-08T02:45:25.923396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam( learning_rate=0.0001), metrics=['accuracy'],) ","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:45:38.30228Z","iopub.execute_input":"2022-03-08T02:45:38.30256Z","iopub.status.idle":"2022-03-08T02:45:38.316782Z","shell.execute_reply.started":"2022-03-08T02:45:38.30253Z","shell.execute_reply":"2022-03-08T02:45:38.316048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# earlystop =tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n#                           min_delta=0, \n#                           patience=15, \n#                           verbose=1, \n#                           restore_best_weights=True)\n                        \n# reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', \n#                               factor=0.5, \n#                               patience=5, \n#                               verbose=1, \n#                               min_delta=0.0001)\n# # checkpoint=tf.keras.callbacks.ModelCheckpoint(\n# #      checkpoint_path, verbose=1, save_weights_only=True,\n# #     period=5\n# # )\n\n\n# callback=[earlystop,reduce_lr]\n# history = model.fit(train_generator, epochs=50 ,verbose=1, validation_data=val_generator,callbacks=callback)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T02:45:40.109024Z","iopub.execute_input":"2022-03-08T02:45:40.109903Z","iopub.status.idle":"2022-03-08T07:13:46.335011Z","shell.execute_reply.started":"2022-03-08T02:45:40.109854Z","shell.execute_reply":"2022-03-08T07:13:46.3343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# models.save_model(model ,'./dog_cat_classifi_mobilev4.h5') ","metadata":{"execution":{"iopub.status.busy":"2022-03-08T07:18:31.122534Z","iopub.execute_input":"2022-03-08T07:18:31.123262Z","iopub.status.idle":"2022-03-08T07:18:35.505069Z","shell.execute_reply.started":"2022-03-08T07:18:31.123221Z","shell.execute_reply":"2022-03-08T07:18:35.504277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model1=models.load_model(\"../input/model-dog-cat-classification/dog_cat_classifi.h5\")\n# model2=models.load_model(\"../input/modelv2/dog_cat_classifi_mobilev3 (1).h5\")\n# model3=models.load_model(\"../input/model-v3/dog_cat_classifi_mobilev3.h5\")\nmodel4=models.load_model(\"../input/modelv4/dog_cat_classifi_mobilev4 (2).h5\")","metadata":{"execution":{"iopub.status.busy":"2022-03-08T08:20:47.290399Z","iopub.execute_input":"2022-03-08T08:20:47.290671Z","iopub.status.idle":"2022-03-08T08:21:24.167905Z","shell.execute_reply.started":"2022-03-08T08:20:47.29064Z","shell.execute_reply":"2022-03-08T08:21:24.166878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\ntest_filenames = os.listdir(\"../input/dog-vs-cat-classification/test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})\npredict=[]\nfor image in test_filenames:\n    np_predict=[]\n    path_image=os.path.join(\"../input/dog-vs-cat-classification/test/test\",image)\n    image=cv2.imread(path_image)\n    image=cv2.resize(image,( 248,248))\n    im=image.reshape((1,248,248,3))/255\n#     np_predict.append(int(np.around(model1.predict(im)[0][0])))\n#     np_predict.append(int(np.around(model2.predict(im)[0][0])))\n#     np_predict.append(int(np.around(model3.predict(im)[0][0])))\n#     np_predict.append(int(np.around(model4.predict(im)[0][0])))\n    predict.append(int(np.around(model4.predict(im)[0][0])))\n#     predict.append(int(np.around(model1.predict(im)[0][0]*0.25+model2.predict(im)[0][0]*0.25+model3.predict(im)[0][0]*0.25+model3.predict(im)[0][0]*0.25)))","metadata":{"execution":{"iopub.status.busy":"2022-03-08T08:21:50.893381Z","iopub.execute_input":"2022-03-08T08:21:50.893668Z","iopub.status.idle":"2022-03-08T08:45:35.845775Z","shell.execute_reply.started":"2022-03-08T08:21:50.893635Z","shell.execute_reply":"2022-03-08T08:45:35.845031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = predict\ntest_df=test_df.sort_values(by='id')","metadata":{"execution":{"iopub.status.busy":"2022-03-08T08:45:35.847423Z","iopub.execute_input":"2022-03-08T08:45:35.847674Z","iopub.status.idle":"2022-03-08T08:45:35.868031Z","shell.execute_reply.started":"2022-03-08T08:45:35.847642Z","shell.execute_reply":"2022-03-08T08:45:35.867178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission2.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-08T08:45:35.86927Z","iopub.execute_input":"2022-03-08T08:45:35.869541Z","iopub.status.idle":"2022-03-08T08:45:35.888792Z","shell.execute_reply.started":"2022-03-08T08:45:35.869505Z","shell.execute_reply":"2022-03-08T08:45:35.888164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-03-08T08:45:54.313512Z","iopub.execute_input":"2022-03-08T08:45:54.31401Z","iopub.status.idle":"2022-03-08T08:45:54.332288Z","shell.execute_reply.started":"2022-03-08T08:45:54.31397Z","shell.execute_reply":"2022-03-08T08:45:54.331561Z"},"trusted":true},"execution_count":null,"outputs":[]}]}