{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Transfer Learning with CIFAR-10 Dataset"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications.vgg19 import VGG19\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom keras.models import Sequential\nfrom keras.utils import to_categorical\nfrom keras.layers import Dense, Flatten\nfrom keras.datasets import cifar10\nimport matplotlib.pyplot as plt\nimport cv2\nimport numpy as np\nfrom glob import glob","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# VGG19"},{"metadata":{},"cell_type":"markdown","source":"## Data Read"},{"metadata":{"trusted":true},"cell_type":"code","source":"#%%\n(x_train, y_train),(x_test, y_test) = cifar10.load_data()\nprint(\"x_train shape\",x_train.shape)\nprint(\"train sample:\",x_train.shape[0])\n\nnumberOfClass = 10\n\ny_train = to_categorical(y_train, numberOfClass)\ny_test = to_categorical(y_test, numberOfClass)\n\ninput_shape = x_train.shape[1:]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Visualization"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(x_train[5511].astype(np.uint8))\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Increase dimension for vgg19"},{"metadata":{"trusted":true},"cell_type":"code","source":"def resize_img(img):\n    numberOfImage = img.shape[0]\n    new_array = np.zeros((numberOfImage, 64,64,3))\n    for i in range(numberOfImage):\n        new_array[i] = cv2.resize(img[i,:,:,:],(64,64))\n    return new_array\n\nx_train = resize_img(x_train)\nx_test = resize_img(x_test)\nprint(\"increased dim x_train: \",x_train.shape)\n\nplt.figure()\nplt.imshow(x_train[5511].astype(np.uint8))\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Transfer Learning Section"},{"metadata":{"trusted":true},"cell_type":"code","source":"#vgg19\n\nvgg = VGG19(include_top = False, weights = \"imagenet\", input_shape = (64,64,3))\n\nprint(vgg.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"vgg_layer_list = vgg.layers\n#print(vgg_layer_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nfor layer in vgg_layer_list:\n    model.add(layer)\n    \nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Concatenation"},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = False\n\n# fully con layers\nmodel.add(Flatten())\nmodel.add(Dense(128))\nmodel.add(Dense(numberOfClass, activation= \"softmax\"))\n\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model Compiling"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss = \"categorical_crossentropy\",\n              optimizer = \"rmsprop\",\n              metrics = [\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train Section"},{"metadata":{"trusted":true},"cell_type":"code","source":"#epochs = 75, batch_size = 1000\nhist = model.fit(x_train, y_train, validation_split = 0.2, epochs = 120, batch_size = 955)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Results Visualization"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.title('vgg19 - Loss')\nplt.plot(hist.history[\"loss\"], label = \"train loss\")\nplt.plot(hist.history[\"val_loss\"], label = \"val loss\")\nplt.legend()\nplt.show()\n\nplt.figure()\nplt.title('vgg19 - Accuracy')\nplt.plot(hist.history[\"accuracy\"], label = \"train acc\")\nplt.plot(hist.history[\"val_accuracy\"], label = \"val acc\")\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}