{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport cv2\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input/train/train\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"022b4d9277e90bd530a3e7520eaf628c110cc50c"},"cell_type":"markdown","source":"## Visualizing"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Display 10 images\nDATA_DIR = \"../input/train/train\"\nfor file_name in os.listdir(DATA_DIR)[0:10]:\n    img_array = cv2.imread(os.path.join(DATA_DIR, file_name), cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img_array, cmap='gray')\n    plt.show()\n    print(file_name.split('.')[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d48cc15a6e6721dccf2c3a432afbc03ae31475e"},"cell_type":"code","source":"#create data\nDATA_DIR = \"../input/train/train\"\nIMG_SIZE = 150\ndef create_data():\n    x = []\n    y = []\n    for file_name in os.listdir(DATA_DIR):\n        img_array = cv2.imread(os.path.join(DATA_DIR, file_name), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n        x.append(new_array)\n        if file_name.split('.')[0] == 'cat':\n            y.append(0)\n        else:\n            y.append(1)\n    return x, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3374cc6f6e12eee6151f4b9ed0292f6dd094cae4"},"cell_type":"code","source":"import random\nx, y = create_data()\nx = np.array(x)\ny = np.array(y)\nprint(x.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fb887fccca1da0763467c8cb7e8abfa64ee8985"},"cell_type":"code","source":"# export data to file\nimport pickle\n\npickle_out = open('X.pickle', 'wb')\npickle.dump(x, pickle_out)\npickle_out.close()\n\npickle_out = open('y.pickle', 'wb')\npickle.dump(y, pickle_out)\npickle_out.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00876892f0991fe603615ca13009dcdb899819bb"},"cell_type":"code","source":"# import data from file so faster than function create_data()\npickle_in = open('X.pickle', 'rb')\nX = pickle.load(pickle_in)\npickle_in.close()\n\npickle_in = open('y.pickle', 'rb')\ny = pickle.load(pickle_in)\npickle_in.close()\nprint(X.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e50d1be8829071be4aa49f76756814522438ef7b"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(X, y, test_size =0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1da4d9bed60e7965c425004239abae1a37072b02"},"cell_type":"code","source":"# use neural network\nx_train = x_train / 255.\nx_test = x_test / 255.\n\nmodel = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))\n\nmodel.compile(\n    optimizer='adam',\n    loss='binary_crossentropy',\n    metrics=['accuracy']\n)\nmodel.fit(x_train, y_train, epochs=100, validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2a297eabaf48e448ba4adc0d8f61d327f396bf2"},"cell_type":"code","source":"# test data\nval_loss, val_acc = model.evaluate(x_test, y_test)\nprint(val_loss, val_acc)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56a789e0bcbaea92549c80a4401ddb14cdc3b466","scrolled":true},"cell_type":"code","source":"# Display 10 images\nDATA_DIR = \"../input/test1/test1\"\nfor file_name in os.listdir(DATA_DIR)[0:10]:\n    img_array = cv2.imread(os.path.join(DATA_DIR, file_name), cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img_array, cmap='gray')\n    plt.show()\n    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    new_array = np.array(new_array).reshape(1, IMG_SIZE, IMG_SIZE) / 255.\n    print(new_array.shape)\n    pred = model.predict([new_array]) \n    if pred[0] == 0:\n        print('cat')\n    else:\n        print('dog')\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c61ad015f6e1a6aa5d5ddc0c5b614fd698ce16bc"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddb092ef091f81e0140b835f4b657ea4043d61b7"},"cell_type":"code","source":"model2 = tf.keras.models.Sequential()\nmodel2.add(tf.keras.layers.Flatten())\nmodel2.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel2.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))\nmodel2.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax))\n\nmodel2.compile(\n    optimizer='adam',\n    loss='sparse_category_crossentropy',\n    metrics=['accuracy']\n)\nmodel.fit(x_train, y_train, epochs=100, validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ef7157f641a20af7feb974a720133372d3a12b0"},"cell_type":"code","source":"pred = model.predict([x_test])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0895a20b503433aa3e084e330303a168335546b5"},"cell_type":"code","source":"print(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b84d33e899f5ba5521206bbe9093c738cec17fbc"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}