{"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 tensorflow as tf\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load in the data\ncifar10 = tf.keras.datasets.cifar10\n\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()\nX_train, X_test = X_train / 255.0, X_test / 255.0\ny_train, y_test = y_train.flatten(), y_test.flatten()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.imshow(X_train[2])\nplt.colorbar()\nplt.grid(False)\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = Input(shape = X_train[0].shape)\nx = Conv2D(32, (3,3), padding=\"same\", activation=\"relu\")(i)\nx = BatchNormalization()(x)\nx = Conv2D(32, (3,3), activation=\"relu\")(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D(pool_size=(2,2))(x)\nx = Dropout(0.30)(x)\nx = Conv2D(64, (3,3), padding=\"same\", activation=\"relu\")(x)\nx = BatchNormalization()(x)\nx = Conv2D(64, (3,3), activation=\"relu\")(x)\nx = MaxPooling2D(pool_size=(2,2))(x)\nx = Dropout(0.30)(x)\nx = BatchNormalization()(x)\nx = Flatten()(x)\nx = Dense(512, activation=\"relu\")(x)\nx = Dropout(0.5)(x)\nx = Dense(10, activation=\"softmax\")(x)\n\nmodel = Model(i, x)\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=15)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot loss per iteration\nimport matplotlib.pyplot as plt\nplt.plot(r.history['loss'], label='loss')\nplt.plot(r.history['val_loss'], label='val_loss')\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot accuracy per iteration\nplt.plot(r.history['accuracy'], label='acc')\nplt.plot(r.history['val_accuracy'], label='val_acc')\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\n!python -m py7zr x ../input/cifar-10/test.7z /kaggle/working/\n\n!ls ../working","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_path = \"/kaggle/working/test\"\n\nimport glob\nimport os\nimport cv2\nimport numpy as np \n        \nnum_of_test_images = len(glob.glob(test_images_path+\"/*\"))\nprint(num_of_test_images)\ni = 0\n\ntest_images = [[]]*num_of_test_images\nfor dir_name, _, filenames in os.walk('/kaggle/working/test'):\n    for filename in filenames:\n        image_index = int(filename.split(\".\")[0])-1\n        img = cv2.imread(os.path.join(dir_name,filename))\n        # Add the image to the dataset\n        test_images[image_index] = img        \n        \n# The RGB values are between 0 and 255, let's divide them so the values will be between 0 and 1\ntest_images = np.asarray(test_images[:(num_of_test_images//2)], dtype=float)/255\n\n\n\n# # If you want, you can save the dataset and load it, instead of running this block each time\n# np.save(\"../working/train_images.npy\", train_images)\n#\ntest_images.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" np.save(\"../working/test_images_2.npy\", test_images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = np.load(\"../working/test_images.npy\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nmodel_ = load_model('../working/model', compile=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = model_.predict(test_images)\ntest_predictions","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}