{"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\n\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create the training and testing folders\n\nos.makedirs('./training')\nos.makedirs('./testing')\nos.makedirs('./training/airplane')\nos.makedirs('./training/automobile')\nos.makedirs('./training/bird')\nos.makedirs('./training/cat')\nos.makedirs('./training/deer')\nos.makedirs('./training/dog')\nos.makedirs('./training/truck')\nos.makedirs('./training/frog')\nos.makedirs('./training/ship')\nos.makedirs('./training/horse')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label = pd.read_csv('../input/codeml-challenge5/train_label.txt', header=None)\nlabel.columns = ['class']\nd = {\n    'id': list(range(1, 50000)),\n    'class': label['class']\n}\nlabel_train = pd.DataFrame(data=d)\nlabel_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get the paths of all the training images\npaths = []\nfor dirname, _, filenames in os.walk('../input/codeml-challenge5/train_images/train_images'):\n    for filename in filenames:\n        paths.append(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# sort paths\nimport re\ndef atoi(text):\n    return int(text) if text.isdigit() else text\ndef natural_keys(text):\n    return [ atoi(c) for c in re.split('(\\d+)',text) ]\n \npaths.sort(key=natural_keys)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PIL.Image.open(paths[2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# sort images in correct directories\n\nimport shutil\nfor id in label_train['id']:\n    class_name = label_train.loc[label_train['id'] == id, 'class'].item()\n    path = '../input/codeml-challenge5/train_images/train_images/Image_{}.png'.format(id)\n    shutil.copy(path,'./training/{}'.format(class_name))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# creating tf dataset\n\ntrain_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    directory='./training',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    class_names=None,\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(32, 32),\n    shuffle=True,\n    seed=None,\n    validation_split=None,\n    subset=None,\n    interpolation=\"bilinear\",\n    follow_links=False,\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure()\nfor images, labels in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n    print(image_batch.shape)\n    print(labels_batch.shape)\n    break\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))\nimage_batch, labels_batch = next(iter(normalized_ds))\nfirst_image = image_batch[0]\nprint(np.min(first_image), np.max(first_image))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = 10\nimg_height = 32\nimg_width = 32\n\nmodel = Sequential([\n  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  train_ds,\n  epochs=epochs\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = tf.keras.preprocessing.image.load_img(\n    path='../input/codeml-challenge5/test_images/content/data/test_imagess/Image_1.png', grayscale=False, color_mode='rgb', target_size=None,\n    interpolation='nearest'\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_array = keras.preprocessing.image.img_to_array(img)\nimg_array = tf.expand_dims(img_array, 0) # Create a batch\n\npredictions = model.predict(img_array)\nscore = tf.nn.softmax(predictions[0])\n\nprint(\n    'This image most likely belongs to {} with a {:.2f} percent confidence.'.format(class_names[np.argmax(score)], 100 * np.max(score))\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get the paths of the testing images\ntest_paths = []\nfor dirname, _, filenames in os.walk('../input/codeml-challenge5/test_images/content/data/test_imagess'):\n    for filename in filenames:\n        test_paths.append(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_paths.sort(key=natural_keys)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_image_array(path):\n    img = tf.keras.preprocessing.image.load_img(\n    path=path, grayscale=False, color_mode='rgb', target_size=None, interpolation='nearest'\n    )\n    img_array = keras.preprocessing.image.img_to_array(img)\n    img_array = tf.expand_dims(img_array, 0) # Create a batch\n    return img_array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arr = get_image_array('../input/codeml-challenge5/test_images/content/data/test_imagess/Image_1.png')\narr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict(path):\n    arr = get_image_array(path)\n    predictions = model.predict(arr)\n    score = tf.nn.softmax(predictions[0])\n    \n    class_name = class_names[np.argmax(score)]\n    if(class_name == 'airplane'):\n        return 0\n    elif(class_name == 'automobile'):\n        return 1\n    elif(class_name == 'bird'):\n        return 2\n    elif(class_name == 'cat'):\n        return 3\n    elif(class_name == 'deer'):\n        return 4\n    elif(class_name == 'dog'):\n        return 5\n    elif(class_name == 'frog'):\n        return 6\n    elif(class_name == 'horse'):\n        return 7\n    elif(class_name == 'ship'):\n        return 8\n    elif(class_name == 'truck'):\n        return 9\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict('../input/codeml-challenge5/test_images/content/data/test_imagess/Image_1.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntest_y = []\n#len(test_x)\nfor path in test_paths:\n    test_y.append(predict(path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(test_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_x = list(range(0,len(test_paths)))\nsub = {\n    'id': test_x,\n    'classes': test_y\n}\n\nsubmission = pd.DataFrame(sub)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv',header=True,index=False)","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}