{"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":{"id":"JDl3hpbAalj-","outputId":"4dc7bb31-3253-4a3d-931a-862c3fb34bdc","execution":{"iopub.status.busy":"2022-12-10T11:40:18.583544Z","iopub.execute_input":"2022-12-10T11:40:18.583895Z","iopub.status.idle":"2022-12-10T11:40:18.592767Z","shell.execute_reply.started":"2022-12-10T11:40:18.583866Z","shell.execute_reply":"2022-12-10T11:40:18.591508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-12-10T11:40:18.620172Z","iopub.execute_input":"2022-12-10T11:40:18.620884Z","iopub.status.idle":"2022-12-10T11:40:35.906487Z","shell.execute_reply.started":"2022-12-10T11:40:18.620850Z","shell.execute_reply":"2022-12-10T11:40:35.905260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"code","source":"import glob\nimport py7zr","metadata":{"execution":{"iopub.status.busy":"2022-12-10T11:40:35.909206Z","iopub.execute_input":"2022-12-10T11:40:35.909643Z","iopub.status.idle":"2022-12-10T11:40:36.058290Z","shell.execute_reply.started":"2022-12-10T11:40:35.909596Z","shell.execute_reply":"2022-12-10T11:40:36.057268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_path = '/kaggle/data/'\nif not os.path.exists(temp_path):\n    os.mkdir(temp_path)\ntrain_file_path = '../input/cifar-10/train.7z'\narchive = py7zr.SevenZipFile(train_file_path, mode='r')\narchive.extractall(path=temp_path)\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2022-12-10T11:40:36.059698Z","iopub.execute_input":"2022-12-10T11:40:36.060150Z","iopub.status.idle":"2022-12-10T11:41:22.765540Z","shell.execute_reply.started":"2022-12-10T11:40:36.060114Z","shell.execute_reply":"2022-12-10T11:41:22.764482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = glob.glob('/kaggle/data/train/*.png')\nlen(images)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T11:41:22.768523Z","iopub.execute_input":"2022-12-10T11:41:22.769305Z","iopub.status.idle":"2022-12-10T11:41:22.963334Z","shell.execute_reply.started":"2022-12-10T11:41:22.769264Z","shell.execute_reply":"2022-12-10T11:41:22.962330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50\n","metadata":{"id":"vFvrM-itNEXs"}},{"cell_type":"code","source":"train_images_path = \"/kaggle/data/train\"","metadata":{"id":"TAEzMqtUNMlY","execution":{"iopub.status.busy":"2022-12-10T11:41:22.964710Z","iopub.execute_input":"2022-12-10T11:41:22.965092Z","iopub.status.idle":"2022-12-10T11:41:22.971507Z","shell.execute_reply.started":"2022-12-10T11:41:22.965050Z","shell.execute_reply":"2022-12-10T11:41:22.969540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport cv2\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np \nimport pandas as pd \nfrom tensorflow import keras\n\n# Model architecture\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import Sequential, datasets,models,layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D, DepthwiseConv2D, BatchNormalization\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.models import Model\n# Data processing\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\n# model\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\nfrom tensorflow.keras import optimizers","metadata":{"id":"FsKAPO9JNMlY","execution":{"iopub.status.busy":"2022-12-10T11:41:22.973268Z","iopub.execute_input":"2022-12-10T11:41:22.974085Z","iopub.status.idle":"2022-12-10T11:41:29.537594Z","shell.execute_reply.started":"2022-12-10T11:41:22.974050Z","shell.execute_reply":"2022-12-10T11:41:29.536527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_train_images = len(os.listdir(train_images_path))\n\n\ntrain_images = [[]]*num_of_train_images\n\nfor filename in os.listdir(train_images_path):\n    image_index = int(filename.split(\".\")[0])-1\n    img = cv2.imread(os.path.join(train_images_path,filename)) \n    train_images[image_index] = img\n\n\ntrain_images = np.asarray(train_images, dtype=float)/255","metadata":{"id":"Kbi2Lb9aNMlZ","execution":{"iopub.status.busy":"2022-12-10T11:41:29.539263Z","iopub.execute_input":"2022-12-10T11:41:29.540116Z","iopub.status.idle":"2022-12-10T11:41:34.275585Z","shell.execute_reply.started":"2022-12-10T11:41:29.540076Z","shell.execute_reply":"2022-12-10T11:41:34.274475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/cifar-10/trainLabels.csv\")\nLABELS = {'airplane':0,'automobile':1,'bird':2,'cat':3,'deer':4,'dog':5,'frog':6 ,'horse':7 ,'ship':8,'truck':9}\n\ntrain_labels = [LABELS[i] for i in train_df['label']]\n\ntrain_labels = np.array(train_labels)\n\ntrain_labe_one_hot = to_categorical(train_labels,10)","metadata":{"id":"qwmelg-LNMla","execution":{"iopub.status.busy":"2022-12-10T11:41:34.277108Z","iopub.execute_input":"2022-12-10T11:41:34.277538Z","iopub.status.idle":"2022-12-10T11:41:34.352896Z","shell.execute_reply.started":"2022-12-10T11:41:34.277499Z","shell.execute_reply":"2022-12-10T11:41:34.351780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"id":"KEabvVD5NMlb","execution":{"iopub.status.busy":"2022-12-10T11:41:34.354241Z","iopub.execute_input":"2022-12-10T11:41:34.355286Z","iopub.status.idle":"2022-12-10T11:41:34.383525Z","shell.execute_reply.started":"2022-12-10T11:41:34.355256Z","shell.execute_reply":"2022-12-10T11:41:34.382460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\nconv_base.summary()","metadata":{"id":"wiVWdH9yS3lW","execution":{"iopub.status.busy":"2022-12-10T11:41:34.387796Z","iopub.execute_input":"2022-12-10T11:41:34.389699Z","iopub.status.idle":"2022-12-10T11:41:43.020001Z","shell.execute_reply.started":"2022-12-10T11:41:34.389659Z","shell.execute_reply":"2022-12-10T11:41:43.019032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train_images, train_labe_one_hot, random_state=0, test_size=0.05)","metadata":{"id":"0M1PsoYzNMlg","execution":{"iopub.status.busy":"2022-12-10T11:41:43.021419Z","iopub.execute_input":"2022-12-10T11:41:43.022289Z","iopub.status.idle":"2022-12-10T11:41:43.383808Z","shell.execute_reply.started":"2022-12-10T11:41:43.022249Z","shell.execute_reply":"2022-12-10T11:41:43.382856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)","metadata":{"id":"ynGIcGXpTRIb","outputId":"54fe0dbb-110c-4b82-d010-71e7355639c4","execution":{"iopub.status.busy":"2022-12-10T11:41:43.385376Z","iopub.execute_input":"2022-12-10T11:41:43.385782Z","iopub.status.idle":"2022-12-10T11:41:43.392084Z","shell.execute_reply.started":"2022-12-10T11:41:43.385740Z","shell.execute_reply":"2022-12-10T11:41:43.390912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(10, activation='softmax'))\n\nmodel.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='categorical_crossentropy', metrics=['acc'])\n\nhistory = model.fit(x_train, y_train, epochs=5, batch_size=20, validation_data=(x_val, y_val))","metadata":{"id":"nVmbikFeNMld","outputId":"ccf10c40-3b64-4f47-da96-0973ed86f3e6","execution":{"iopub.status.busy":"2022-12-10T11:41:43.394083Z","iopub.execute_input":"2022-12-10T11:41:43.394977Z","iopub.status.idle":"2022-12-10T12:12:06.044525Z","shell.execute_reply.started":"2022-12-10T11:41:43.394939Z","shell.execute_reply":"2022-12-10T12:12:06.043578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = history\nimport matplotlib.pyplot as plt\n\n# plot the loss.\\\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nplt.savefig('LossVal_loss')\n\n# plot the accuracy\nplt.plot(r.history['acc'], label='train acc')\nplt.plot(r.history['val_acc'], label='val acc')\nplt.legend()\nplt.show()\n\n","metadata":{"id":"4WW1tJlfnWjI","outputId":"58b40f54-cd34-4def-9d56-c9c25d7d57cc","execution":{"iopub.status.busy":"2022-12-10T12:22:02.029085Z","iopub.execute_input":"2022-12-10T12:22:02.029478Z","iopub.status.idle":"2022-12-10T12:22:02.447258Z","shell.execute_reply.started":"2022-12-10T12:22:02.029422Z","shell.execute_reply":"2022-12-10T12:22:02.446349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=  model.predict(train_images)\npred=np.argmax(pred,axis=1)\n","metadata":{"id":"LPsRXpA6ncwq","execution":{"iopub.status.busy":"2022-12-10T12:22:06.735495Z","iopub.execute_input":"2022-12-10T12:22:06.736566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"id":"NYv82SVyooy4","outputId":"e8156537-eabd-452d-84ac-97330e9c13b2","execution":{"iopub.status.busy":"2022-12-10T12:14:31.097366Z","iopub.execute_input":"2022-12-10T12:14:31.097750Z","iopub.status.idle":"2022-12-10T12:14:31.107652Z","shell.execute_reply.started":"2022-12-10T12:14:31.097715Z","shell.execute_reply":"2022-12-10T12:14:31.106343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rev_labels = {val : key for key, val in LABELS.items()}","metadata":{"id":"lEhGGqHStW2Z","execution":{"iopub.status.busy":"2022-12-10T12:14:31.109203Z","iopub.execute_input":"2022-12-10T12:14:31.109800Z","iopub.status.idle":"2022-12-10T12:14:31.116104Z","shell.execute_reply.started":"2022-12-10T12:14:31.109764Z","shell.execute_reply":"2022-12-10T12:14:31.115234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor i in range(30):\n    plt.subplot(6,5,i+1)\n    plt.imshow(train_images[i])\n    img = train_images[i]\n    img = np.expand_dims(img, axis=0)    \n    pred=  model.predict(img)\n    pred=np.argmax(pred,axis=1)\n    plt.title(rev_labels[pred[0]])","metadata":{"id":"nQoGTBJtoor2","outputId":"d8ab2053-26b7-4a54-f237-1b69381d814f","execution":{"iopub.status.busy":"2022-12-10T12:14:31.117749Z","iopub.execute_input":"2022-12-10T12:14:31.118647Z","iopub.status.idle":"2022-12-10T12:14:35.870424Z","shell.execute_reply.started":"2022-12-10T12:14:31.118611Z","shell.execute_reply":"2022-12-10T12:14:35.869505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"MVbCVe3aooZy"},"execution_count":null,"outputs":[]}]}