{"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 numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\n\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, BatchNormalization\n\n%matplotlib inline","metadata":{"id":"WJbVE-AVaGpA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cifar10 = tf.keras.datasets.cifar10\n(X_train,Y_train) , (X_test,Y_test) = cifar10.load_data()","metadata":{"id":"wRRU6txYaZ1m","outputId":"42b60633-e92f-4a9f-99d7-31ae276e4bb0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"id":"OrVy37gUa4Jc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (16,16))\nfor i in range(100):\n  plt.subplot(10,10,1+i)\n  plt.axis('off')\n  plt.imshow(X_train[i], cmap = 'gray')","metadata":{"id":"Tgzh-Ot_bCh0","outputId":"f7ca92dd-b8cf-4907-d063-9e4f900078d9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_val, y_train, y_val = train_test_split(X_train,Y_train,test_size=0.2)","metadata":{"id":"oWB7JmUKbWkQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ny_train = to_categorical(y_train, num_classes = 10)\ny_val = to_categorical(y_val, num_classes = 10)","metadata":{"id":"WQorTIBHb6Z2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\nprint(x_val.shape)\nprint(y_val.shape)\nprint(X_test.shape)\nprint(Y_test.shape)","metadata":{"id":"rmR3CUmscaOR","outputId":"5d13ca66-6672-4185-e601-e756a94b166b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    preprocessing_function = tf.keras.applications.vgg19.preprocess_input,\n    rotation_range=10,\n    zoom_range = 0.1,\n    width_shift_range = 0.1,\n    height_shift_range = 0.1,\n    shear_range = 0.1,\n    horizontal_flip = True\n)\ntrain_datagen.fit(x_train)\n\nval_datagen = ImageDataGenerator(preprocessing_function = tf.keras.applications.vgg19.preprocess_input)\nval_datagen.fit(x_val)","metadata":{"id":"JWuVD37zcdaH"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"id":"Ym3AEEqneTeA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model = tf.keras.applications.VGG19(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=(32,32,3),\n)\n\nvgg_model.summary()","metadata":{"id":"imMzq9dmej6u","outputId":"c433b16d-88d4-4a0a-a131-acc285a63af2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(vgg_model)\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation = 'relu'))\nmodel.add(Dense(1024, activation = 'relu'))\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))\n\nmodel.summary()","metadata":{"id":"PdpYiRhFhZ3U","outputId":"368ae65f-6070-4bbf-ca35-0cbdf7bc5c3e"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.SGD(lr = 0.001, momentum = 0.9)\nmodel.compile(optimizer= optimizer,\n            loss='categorical_crossentropy',\n            metrics=['accuracy'])","metadata":{"id":"2r3HsiQVjLhE","outputId":"c081f32a-73c9-430d-ab06-8a87bc010de0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_datagen.flow(x_train, y_train, batch_size = 128),\n    validation_data = val_datagen.flow(x_val,y_val, batch_size = 128),\n    epochs = 25,\n    verbose = 1,\n    callbacks = [learning_rate_reduction]\n)","metadata":{"id":"IqKsNoKejwF9","outputId":"4423c251-a1af-452d-a81d-ba3c8abac3c5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nplt.figure()\nplt.plot(acc,color = 'green',label = 'Training Acuracy')\nplt.plot(val_acc,color = 'red',label = 'Validation Accuracy')\nplt.legend()","metadata":{"id":"sca37Zc1nguf","outputId":"8b616805-2dc5-4fa9-e122-8b56a5966aac"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure()\nplt.plot(loss,color = 'green',label = 'Training Loss')\nplt.plot(val_loss,color = 'red',label = 'Validation Loss')\nplt.legend()","metadata":{"id":"Y5CCvx2Fnv6I","outputId":"8af5cfaa-3af2-4977-b76a-4bb02d418026"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = tf.keras.applications.vgg19.preprocess_input(X_test)\ny_pred = model.predict_classes(x_test)\ny_pred[:10]","metadata":{"id":"ins28-Enp6el","outputId":"015d2462-d763-465f-82e3-ba59963287d4"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\nprint('Testing Accuarcy : ', accuracy_score(Y_test, y_pred))","metadata":{"id":"mBdO_JoGn2go","outputId":"d9959897-91a9-4da5-b169-601af2751977"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(Y_test, y_pred)\ncm","metadata":{"id":"7J7Nf0vMq2iT","outputId":"5fdd49f5-9a98-4304-9ae0-9289c1bff746"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import itertools\ndef plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=30)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    #print(cm)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n            horizontalalignment=\"center\",\n            color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')","metadata":{"id":"nl6TTxEarCW1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\nplot_confusion_matrix(cm,classes)","metadata":{"id":"Bx49dDWbrWCI","outputId":"50011961-0cdf-4ccf-9291-5afd6883435a"},"execution_count":null,"outputs":[]}]}