Skip to content

Instantly share code, notes, and snippets.

@tuna2134
Created March 11, 2022 08:04
Show Gist options
  • Save tuna2134/257b4b8f9a3098c122c62ef48d24120b to your computer and use it in GitHub Desktop.
Save tuna2134/257b4b8f9a3098c122c62ef48d24120b to your computer and use it in GitHub Desktop.
dogorcat.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "dogorcat.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyODZvkuTBRFLT7Na92l73SA",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/tuna2134/257b4b8f9a3098c122c62ef48d24120b/dogorcat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!pip install bing-image-downloader"
],
"metadata": {
"id": "QdMF9Ik72Zaf"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from bing_image_downloader import downloader\n",
"\n",
"for i in [\"cat\", \"dog\"]:\n",
" downloader.download(i, limit=100, output_dir='images/', adult_filter_off=True, force_replace=False, timeout=60, verbose=True)"
],
"metadata": {
"id": "vSeHMqTP2itB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install tensorflow-hub"
],
"metadata": {
"id": "fSiupoyd3f2p"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from glob import glob\n",
"from PIL import Image\n",
"\n",
"for i in glob(\"images/*\"):\n",
" for p in glob(f\"{i}/*\"):\n",
" image = Image.open(p)\n",
" image.resize((192, 192)).save(p)"
],
"metadata": {
"id": "XQpUzdeS399Z"
},
"execution_count": 37,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"id": "d8ECPZXOxR4s"
},
"outputs": [],
"source": [
"from tensorflow import keras\n",
"import tensorflow as tf\n",
"import tensorflow_hub as hub\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"source": [
"model = keras.models.Sequential([\n",
" hub.KerasLayer(\"https://tfhub.dev/google/imagenet/mobilenet_v1_100_192/classification/5\")\n",
"])\n",
"model.build([None, 192, 192, 3])\n",
"x_label = []\n",
"y_label = []\n",
"for i in glob(\"images/*\"):\n",
" for p in glob(f\"{i}/*\"):\n",
" if i.startswith(\"images/dogs\"):\n",
" y_label.append(1)\n",
" else:\n",
" y_label.append(2)\n",
" img_raw = tf.io.read_file(p)\n",
" img_tensor = tf.image.decode_image(img_raw)\n",
" x_label.append(img_tensor)\n",
"\n",
"model.compile(optimizer='adam', \n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
"model.fit(np.array(x_label), np.array(y_label))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 388
},
"id": "gjekMt7mxgjA",
"outputId": "58dc1f0e-fe55-4c49-b931-71fb11afbe4c"
},
"execution_count": 71,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:20: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n"
]
},
{
"output_type": "error",
"ename": "ValueError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-71-fe0264731137>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'sparse_categorical_crossentropy'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m metrics=['accuracy'])\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_label\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_label\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=broad-except\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwith_traceback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfiltered_tb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mfiltered_tb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py\u001b[0m in \u001b[0;36mconvert_to_eager_tensor\u001b[0;34m(value, ctx, dtype)\u001b[0m\n\u001b[1;32m 100\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdtypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_datatype_enum\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 101\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mensure_initialized\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 102\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEagerTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 103\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor)."
]
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "30nCRnPC-l06"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment