{"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\nfrom tensorflow import keras\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom keras.datasets import cifar10\nfrom keras.applications.xception import Xception, preprocess_input\nfrom tensorflow.keras.utils import to_categorical\nimport cv2\nfrom keras.layers import Dense, Conv2D, BatchNormalization, MaxPooling2D, Activation,Dropout, Flatten,GlobalAveragePooling2D\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-05-09T13:38:43.607163Z","iopub.execute_input":"2022-05-09T13:38:43.607806Z","iopub.status.idle":"2022-05-09T13:38:43.615545Z","shell.execute_reply.started":"2022-05-09T13:38:43.607763Z","shell.execute_reply":"2022-05-09T13:38:43.614233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:38:43.683482Z","iopub.execute_input":"2022-05-09T13:38:43.683948Z","iopub.status.idle":"2022-05-09T13:38:44.307861Z","shell.execute_reply.started":"2022-05-09T13:38:43.683918Z","shell.execute_reply":"2022-05-09T13:38:44.306953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs=2\nbatch_size=50\nn_class=10\nnr=nc=72","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:38:44.309568Z","iopub.execute_input":"2022-05-09T13:38:44.310132Z","iopub.status.idle":"2022-05-09T13:38:44.31489Z","shell.execute_reply.started":"2022-05-09T13:38:44.31009Z","shell.execute_reply":"2022-05-09T13:38:44.313828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train,n_class)\ny_test = to_categorical(y_test,n_class)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:38:44.31653Z","iopub.execute_input":"2022-05-09T13:38:44.317142Z","iopub.status.idle":"2022-05-09T13:38:44.326746Z","shell.execute_reply.started":"2022-05-09T13:38:44.317104Z","shell.execute_reply":"2022-05-09T13:38:44.326019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_pretrained_wo_top = Xception(include_top=False, weights=\"imagenet\", input_shape=(nr,nc,3))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:38:44.330244Z","iopub.execute_input":"2022-05-09T13:38:44.330812Z","iopub.status.idle":"2022-05-09T13:39:20.069265Z","shell.execute_reply.started":"2022-05-09T13:38:44.330771Z","shell.execute_reply":"2022-05-09T13:39:20.068442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def myGen(x,y,batch_size):\n    while 1:\n        for i in range(x.shape[0]//batch_size):\n            ip = x[i*batch_size:(i+1)*batch_size]\n            op = y[i*batch_size:(i+1)*batch_size]\n            temp = preprocess_input(x_train)\n            temp=preprocess_input(np.array([cv2.resize(ip[i],dsize=(nr,nc),interpolation=cv2.INTER_CUBIC) for i in range(batch_size)]).astype('float32'))\n            temp = model_pretrained_wo_top.predict(temp)\n            yield temp, op","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:39:20.070826Z","iopub.execute_input":"2022-05-09T13:39:20.071098Z","iopub.status.idle":"2022-05-09T13:39:20.079279Z","shell.execute_reply.started":"2022-05-09T13:39:20.071061Z","shell.execute_reply":"2022-05-09T13:39:20.078106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = myGen(x_train,y_train,batch_size)\ntest_gen = myGen(x_test,y_test,batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:39:20.080776Z","iopub.execute_input":"2022-05-09T13:39:20.081055Z","iopub.status.idle":"2022-05-09T13:39:20.091807Z","shell.execute_reply.started":"2022-05-09T13:39:20.081017Z","shell.execute_reply":"2022-05-09T13:39:20.090982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\nmodel.add(GlobalAveragePooling2D(input_shape=model_pretrained_wo_top.output_shape[1:]))\n#model.add(Dropout(0.3))\nmodel.add(Dense(10, activation=\"softmax\"))\n\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\nmodel.fit_generator(generator=train_gen,steps_per_epoch=50,epochs=n_epochs,verbose=1,\n                    validation_data=test_gen, validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T13:39:20.093926Z","iopub.execute_input":"2022-05-09T13:39:20.094242Z","iopub.status.idle":"2022-05-09T13:40:29.108844Z","shell.execute_reply.started":"2022-05-09T13:39:20.094205Z","shell.execute_reply":"2022-05-09T13:40:29.108131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}