{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"\n!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport os\nfrom glob import glob\nimport random\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport PIL.Image\nimport tensorflow as tf\nimport shutil","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_path = '../working/train/'\npets = os.listdir(images_path)\nprint(pets[0])\nprint(len(pets))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# multipleImages = glob('../working/train/**')\n# def plotImages2():\n#     r = random.sample(multipleImages, 9)\n#     plt.figure(figsize=(20,20))\n#     plt.subplot(331)\n#     plt.imshow(cv2.imread(r[0])); plt.axis('off')\n#     plt.subplot(332)\n#     plt.imshow(cv2.imread(r[1])); plt.axis('off')\n#     plt.subplot(333)\n#     plt.imshow(cv2.imread(r[2])); plt.axis('off')\n#     plt.subplot(334)\n#     plt.imshow(cv2.imread(r[3])); plt.axis('off')\n#     plt.subplot(335)\n#     plt.imshow(cv2.imread(r[4])); plt.axis('off')\n#     plt.subplot(336)\n#     plt.imshow(cv2.imread(r[5])); plt.axis('off')\n#     plt.subplot(337)\n#     plt.imshow(cv2.imread(r[6])); plt.axis('off')\n#     plt.subplot(338)\n#     plt.imshow(cv2.imread(r[7])); plt.axis('off')\n#     plt.subplot(339)\n#     plt.imshow(cv2.imread(r[8])); plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plotImages2()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create dir and move pics to their own folder\nos.mkdir(os.path.join(images_path, \"dogs\"))\nos.mkdir(os.path.join(images_path, \"cats\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cats = os.path.join(images_path, \"cats\")\ndogs = os.path.join(images_path, \"dogs\")\n\nfor file in os.listdir(images_path):\n    print(file)\n    label = str.split(file, \".\")[0]\n    if label == \"dog\":\n        shutil.move(os.path.join(images_path, file), dogs)\n        \n    if label == \"cat\":\n        shutil.move(os.path.join(images_path, file), cats)\n   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names = np.array([\"dogs\", \"cats\"])\nbatch_size = 128\nimg_height = 118\nimg_width = 118","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_count = len(pets)\nprint(image_count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(images_path+'*')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"list_ds = tf.data.Dataset.list_files(str(images_path+'*/*'), shuffle=False)\nlist_ds = list_ds.shuffle(image_count, reshuffle_each_iteration=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for f in list_ds.take(5):\n    print(f.numpy())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train test split\nval_size = int(image_count * 0.2)\ntrain_ds = list_ds.skip(val_size)\nval_ds = list_ds.take(val_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(tf.data.experimental.cardinality(train_ds).numpy())\nprint(tf.data.experimental.cardinality(val_ds).numpy())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_label(file_path):\n    parts = tf.strings.split(file_path, os.path.sep)\n    one_hot = parts[-2] == class_names\n    return tf.argmax(one_hot)\n\ndef decode_img(img):\n    \n    # convert the compressed string to a 3D uint8 tensor\n    img = tf.image.decode_jpeg(img, channels=3)\n    # resize the image to the desired size\n    return tf.image.resize(img, [img_height, img_width])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for f in list_ds.take(1):\n    parts = tf.strings.split(f, os.path.sep)\n    print(parts)\n    one_hot = parts[-2] == class_names\n    print(one_hot)\n#     path = tf.strings.split(parts[2], os.path.sep)\n# #     print(path[3])\n#     one_hot = path[3] == class_names\n#     print(one_hot)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_path(file_path):\n    label = get_label(file_path)\n    # load the raw data from the file as a string\n    img = tf.io.read_file(file_path)\n    img = decode_img(img)\n    return img, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\n\ntrain_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Set `num_parallel_calls` so multiple images are loaded/processed in parallel.\ntrain_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)\nval_ds = val_ds.map(process_path, num_parallel_calls=AUTOTUNE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image, label = next(iter(train_ds))\nprint(label.numpy())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image, label in train_ds.take(5):\n    print(\"Image shape: \", image.numpy().shape)\n    print(\"Label: \", label.numpy())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def configure_for_performance(ds):\n    ds = ds.cache()\n    ds = ds.shuffle(buffer_size=1000)\n    ds = ds.batch(batch_size)\n    ds = ds.prefetch(buffer_size=AUTOTUNE)\n    return ds\n\ntrain_ds = configure_for_performance(train_ds)\nval_ds = configure_for_performance(val_ds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds))\nprint(label_batch)\n\nplt.figure(figsize=(10, 10))\nfor i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(image_batch[i].numpy().astype(\"uint8\"))\n    label = label_batch[i]\n    plt.title(class_names[label])\n    plt.axis(\"off\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras import layers\n\nnormalization_layer = tf.keras.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]\n# Notice the pixels values are now in `[0,1]`.\nprint(np.min(first_image), np.max(first_image)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = 2\n\nmodel = tf.keras.Sequential([\n  layers.experimental.preprocessing.Rescaling(1./255),\n  layers.Conv2D(32, 3, activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(1, activation='sigmoid')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(\n  optimizer='adam',\n  loss=tf.losses.BinaryCrossentropy(from_logits=False),\n  metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=30\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}