{"cells":[{"metadata":{"id":"3EL0E90uFAz8","colab_type":"code","outputId":"090cf8b7-614e-4e88-89d7-ce730fda731a","colab":{"base_uri":"https://localhost:8080/","height":592},"trusted":true},"cell_type":"code","source":"!pip install tensorflow==2.0.0-alpha0","execution_count":null,"outputs":[]},{"metadata":{"id":"KKWZcEtHHmde","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"train_file=\"../input/dogs-vs-cats/train.zip\"","execution_count":null,"outputs":[]},{"metadata":{"id":"w9xebZ0eHmZM","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(train_file, 'r') as z:\n    z.extractall()","execution_count":null,"outputs":[]},{"metadata":{"id":"qGzxvPcZHmSN","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import os\nimport cv2\n\ndef load_image(file_path):\n    return cv2.imread(file_path)\n\ndef extract_label(file_name):\n    return 1 if \"dog\" in file_name else 0\n\ntrain_path = \"./train/\"\nimage_files = os.listdir(train_path)\ntrain_images = [load_image(train_path + file) for file in image_files]\ntrain_labels = [extract_label(file) for file in image_files]","execution_count":null,"outputs":[]},{"metadata":{"id":"PvPEEnTTI8QH","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def preprocess_image(img, side=96):\n    min_side = min(img.shape[0], img.shape[1])\n    img = img[:min_side, :min_side]\n    img = cv2.resize(img, (side,side))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    return img / 255.0","execution_count":null,"outputs":[]},{"metadata":{"id":"wW7fawy2I8nT","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":221},"outputId":"cf9366e4-2784-4da0-d31c-0def9b9bd4f6","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\npreview_index = 21000\nplt.subplot(1,2,1)\nplt.imshow(train_images[preview_index])\nplt.subplot(1,2,2)\nplt.imshow(preprocess_image(train_images[preview_index]), cmap=\"gray\")","execution_count":null,"outputs":[]},{"metadata":{"id":"UAS-3qtWI88B","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"for i in range(len(train_images)):\n    train_images[i] = preprocess_image(train_images[i])","execution_count":null,"outputs":[]},{"metadata":{"id":"RaGqThI4JYuh","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"outputId":"72c972b6-21eb-49a3-e034-f2474d5dffde","trusted":true},"cell_type":"code","source":"import numpy as np\n\ntrain_images = np.expand_dims(train_images, axis=-1)\ntrain_labels = np.array(train_labels)\nprint(train_images.shape, train_labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"id":"rHyWMAU6JYqo","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":220},"outputId":"841309f3-750a-4057-937d-c434907dee44","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nprint(\"Tensorflow:\", tf.__version__)\n\nlayers = [\n    tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), padding=\"same\", activation=tf.nn.relu, input_shape=train_images.shape[1:]),\n    tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),\n     tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding=\"same\", activation=tf.nn.relu),\n    tf.keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding=\"same\", activation=tf.nn.relu),\n    tf.keras.layers.MaxPool2D(pool_size=(3,3), strides=(2,2)),\n    tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3),padding=\"same\", activation=tf.nn.relu),\n    tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),\n    tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3),padding=\"same\", activation=tf.nn.relu),\n    tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units=512, activation=tf.nn.relu),\n    tf.keras.layers.Dense(units=256, activation=tf.nn.relu),\n    tf.keras.layers.Dense(units=2, activation=tf.nn.softmax)\n]\n\nmodel = tf.keras.Sequential(layers)\nmodel.compile(optimizer=tf.optimizers.Adam(),\n              loss=tf.losses.SparseCategoricalCrossentropy(),\n              metrics=[tf.metrics.SparseCategoricalAccuracy()])\nmodel.fit(train_images, train_labels, epochs=10, batch_size=50)\nmodel.save_weights(\"model.tf\")","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"catordog.ipynb","provenance":[],"machine_shape":"hm"},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}