{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","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 in \nimport tensorflow as tf\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_epochs = 10\nbatch_size = 32\nlearning_rate = 0.001\ndata_dir = '/kaggle/input/dogs-vs-cats-redux-kernels-edition'\nTRAIN_DIR = data_dir + '/train/'\nTEST_DIR = data_dir + '/test/'\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def _decode_and_resize(filename, label):\n    image_string = tf.io.read_file(filename)\n    image_decoded = tf.image.decode_jpeg(image_string)\n    image_resized = tf.image.resize(image_decoded, [256, 256]) / 255.0\n    return image_resized, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_cat_filenames = tf.constant([TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i])\ntrain_dog_filenames = tf.constant([TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i])\ntrain_filenames = tf.concat([train_dogs, train_cats], axis=-1)\ntrain_labels = tf.concat([\n    tf.zeros(train_cat_filenames.shape, dtype=tf.int32), \n    tf.ones(train_dog_filenames.shape, dtype=tf.int32)], \n    axis=-1)\n\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_filenames, train_labels))\ntrain_dataset = train_dataset.map(_decode_and_resize)\n# 取出前buffer_size个数据放入buffer，并从其中随机采样，采样后的数据用后续数据替换\ntrain_dataset = train_dataset.shuffle(buffer_size=23000)    \ntrain_dataset = train_dataset.batch(batch_size)\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(256, 256, 3)),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(32, 5, activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(2, activation='softmax')\n])\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),\n    loss=tf.keras.losses.sparse_categorical_crossentropy,\n    metrics=[tf.keras.metrics.sparse_categorical_accuracy]\n)\n\nmodel.fit(train_dataset, epochs=num_epochs)","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":1}