{"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","execution":{"iopub.status.busy":"2022-03-22T15:45:33.986564Z","iopub.execute_input":"2022-03-22T15:45:33.987362Z","iopub.status.idle":"2022-03-22T15:45:34.023483Z","shell.execute_reply.started":"2022-03-22T15:45:33.987241Z","shell.execute_reply":"2022-03-22T15:45:34.022759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code for CIFAR 10 Transfer Learning using Xception, no generator\n#Loading Libraries\nimport tensorflow\nfrom tensorflow import keras\nimport numpy as np\nimport cv2\nfrom keras.applications.xception import Xception, preprocess_input\nfrom keras.layers import Dense, MaxPool2D, Conv2D, Dropout, Flatten, GlobalAveragePooling2D\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.datasets import cifar10\n\n#Loading Data\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-22T15:45:36.141745Z","iopub.execute_input":"2022-03-22T15:45:36.142286Z","iopub.status.idle":"2022-03-22T15:45:42.270502Z","shell.execute_reply.started":"2022-03-22T15:45:36.142247Z","shell.execute_reply":"2022-03-22T15:45:42.26929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#types and shape\n#print(\"After Load:\")\n#print(type(x_train), type(x_test), type(y_train), type(y_test))\n#print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)\n\n#One hot encode outputs\ny_train_one_hot=np_utils.to_categorical(y_train)\ny_test_one_hot=np_utils.to_categorical(y_test)\n\ndel y_train\ndel y_test\n\n#print(\"After One Hot Encoding:\")\n#print(type(y_train_one_hot), type(y_test_one_hot), y_train_one_hot.shape, y_test_one_hot.shape)\n\nnum_classes=y_test_one_hot.shape[1]\nprint(\"Num Classes:\",num_classes)\n\n#resize for Xception\nx_train = np.array([cv2.resize(x_train[i], dsize=(72, 72), interpolation=cv2.INTER_CUBIC) for i in range(0, len(x_train))]).astype('float16')\n\nprint(\"After Reshape:\")\nprint(type(x_train), x_train.shape)\n\n\n#Load Pretrained Model\nmodel_pretrained_wo_top=Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=(72,72,3), pooling=None)\n#model_pretrained_wo_top._make_predict_function()\n#model_pretrained_wo_top.summary()\n\n#Preprocess Input for pretrained model\nx_train=preprocess_input(x_train)\n\n\n\n#print(\"after preprocess:\")\n#print(type(x_train), x_train.shape)\n\n#Generate Features\nx_train=model_pretrained_wo_top.predict(x_train)\ndel x_train\n\nx_test = np.array([cv2.resize(x_test[i], dsize=(72, 72), interpolation=cv2.INTER_CUBIC) for i in range(0, len(x_test))]).astype('float16')\n#print(type(x_test), x_test.shape)\nx_test=preprocess_input(x_test)\n\nx_test=model_pretrained_wo_top.predict(x_test)\n\n#delete\ndel model_pretrained_wo_top\n\n#print(\"After Feature Prediction:\")\n#print(type(x_train), x_train.shape, x_train.shape[1:])\n\n#Classifier on top\nmodel = Sequential()\nmodel.add(GlobalAveragePooling2D(input_shape=(7,7,2048)))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(10, activation='softmax'))\n#model.summary()\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n#Train the classifier\nmodel.fit(x_train, y_train_one_hot, validation_data=(x_test, y_test_one_hot), batch_size=32, epochs=10, verbose=1)\n\n#Evaluate\nscores = model.evaluate(x=x_test, y=y_test_one_hot, verbose=0)\nprint(model.metrics_names)\nprint(\"Test Loss:\",scores[0])\nprint(\"Baseline Error:\", round((100-scores[1]*100),2), \"%\")","metadata":{"execution":{"iopub.status.busy":"2022-03-22T15:45:50.464457Z","iopub.execute_input":"2022-03-22T15:45:50.465092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}