{"cells":[{"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Load necessary packages\nfrom keras.datasets import cifar10\nfrom keras.utils import np_utils\nfrom matplotlib import pyplot as plt\nimport numpy as np\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the data\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Lets determine the dataset characteristics\nprint('Training Images: {}'.format(X_train.shape))\nprint('Testing Images: {}'.format(X_test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Now for a single image \nprint(X_train[0].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create a grid of 3x3 images\nfor i in range(0,9):\n    plt.subplot(330 + 1 + i)\n    img = X_train[i]\n    plt.imshow(img)\n    \n# show the plot\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Building a convolutional neural network for object recognition on CIFAR-10\n\n# fix random seed for reproducibility\nseed = 6\nnp.random.seed(seed) \n\n# load the data\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n\n# normalize the inputs from 0-255 to 0.0-1.0\nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_train = X_train / 255.0\nX_test = X_test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# class labels shape\nprint(y_train.shape)\nprint(y_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# hot encode outputs\nY_train = np_utils.to_categorical(y_train)\nY_test = np_utils.to_categorical(y_test)\nnum_classes = Y_test.shape[1]\n\nprint(Y_train.shape)\nprint(Y_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# start building the model - import necessary layers\nfrom keras.models import Sequential\nfrom keras.layers import Dropout, Activation, Conv2D, GlobalAveragePooling2D\nfrom keras.optimizers import SGD\n\ndef allcnn(weights=None):\n    # define model type - Sequential\n    model = Sequential()\n\n    # add model layers - Convolution2D, Activation, Dropout\n    model.add(Conv2D(96, (3, 3), padding = 'same', input_shape=(32, 32, 3)))\n    model.add(Activation('relu'))\n    model.add(Conv2D(96, (3, 3), padding = 'same'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(96, (3, 3), padding = 'same', strides = (2,2)))\n    model.add(Dropout(0.5))\n\n    model.add(Conv2D(192, (3, 3), padding = 'same'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(192, (3, 3), padding = 'same'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(192, (3, 3), padding = 'same', strides = (2,2)))\n    model.add(Dropout(0.5))\n\n    model.add(Conv2D(192, (3, 3), padding = 'same'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(192, (1, 1), padding = 'valid'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(10, (1, 1), padding = 'valid'))\n\n    # add GlobalAveragePooling2D layer with Softmax activation\n    model.add(GlobalAveragePooling2D())\n    model.add(Activation('softmax'))\n    \n    # load the weights\n    if weights:\n        model.load_weights(weights)\n    \n    # return model\n    return model\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# define hyper parameters\n\nlearning_rate = 0.01\nweight_decay = 1e-6\nmomentum = 0.9\n\n# define weights and build model\nweights = '../input/weights/all_cnn_weights_0.9088_0.4994.hdf5'\nmodel = allcnn(weights)\n\n# define optimizer and compile model\nsgd = SGD(lr=learning_rate, decay=weight_decay, momentum=momentum, nesterov=True)\nmodel.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])\n\n# print model summary\nprint (model.summary())\n\n# test the model with pretrained weights\nscores = model.evaluate(X_test, Y_test, verbose=1)\nprint(\"Accuracy: %.2f%%\" % (scores[1]*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# make dictionary of class labels and names\nclasses = range(0,10)\n\nnames = ['airplane',\n        'automobile',\n        'bird',\n        'cat',\n        'deer',\n        'dog',\n        'frog',\n        'horse',\n        'ship',\n        'truck']\n\n# zip the names and classes to make a dictionary of class_labels\nclass_labels = dict(zip(classes, names))\n\n# generate batch of 9 images to predict\nbatch = X_test[100:109]\nlabels = np.argmax(Y_test[100:109],axis=-1)\n\n# make predictions\npredictions = model.predict(batch, verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print our predictions\nprint (predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# these are individual class probabilities, should sum to 1.0 (100%)\nfor image in predictions:\n    print(np.sum(image))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# use np.argmax() to convert class probabilities to class labels\nclass_result = np.argmax(predictions,axis=-1)\nprint (class_result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create a grid of 3x3 images\nfig, axs = plt.subplots(3, 3, figsize = (15, 6))\nfig.subplots_adjust(hspace = 1)\naxs = axs.flatten()\n\nfor i, img in enumerate(batch):\n\n    # determine label for each prediction, set title\n    for key, value in class_labels.items():\n        if class_result[i] == key:\n            title = 'Prediction: {}\\nActual: {}'.format(class_labels[key], class_labels[labels[i]])\n            axs[i].set_title(title)\n            axs[i].axes.get_xaxis().set_visible(False)\n            axs[i].axes.get_yaxis().set_visible(False)\n            \n    # plot the image\n    axs[i].imshow(img)\n    \n# show the plot\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred=model.predict(X_test, verbose = 1)\nclass_result_y_pred = np.argmax(y_pred,axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_result_y_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i2c = np.load(\"../input/inc2int-to-category/datasets_704654_1230900_int2categroy.npy\")\ni2c","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_result_y_pred = [i2c[i] for i in class_result_y_pred]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_result_y_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idx=np.arange(1,10000+1)\nanswers = pd.DataFrame({\"id\":idx,\"label\":class_result_y_pred})\nanswers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"answers.to_csv(\"answers.csv\",index=0)","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}