{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:06.077410Z","iopub.execute_input":"2023-04-21T08:06:06.078501Z","iopub.status.idle":"2023-04-21T08:06:21.415452Z","shell.execute_reply.started":"2023-04-21T08:06:06.078456Z","shell.execute_reply":"2023-04-21T08:06:21.414344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pathlib\ndataset_url = \"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz\"\ndata_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)\ndata_dir = pathlib.Path(data_dir)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:21.418087Z","iopub.execute_input":"2023-04-21T08:06:21.418965Z","iopub.status.idle":"2023-04-21T08:06:25.541649Z","shell.execute_reply.started":"2023-04-21T08:06:21.418905Z","shell.execute_reply":"2023-04-21T08:06:25.540549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(list(data_dir.glob('*/*.jpg')))\nprint(image_count)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.543112Z","iopub.execute_input":"2023-04-21T08:06:25.543538Z","iopub.status.idle":"2023-04-21T08:06:25.573197Z","shell.execute_reply.started":"2023-04-21T08:06:25.543496Z","shell.execute_reply":"2023-04-21T08:06:25.571937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roses = list(data_dir.glob('roses/*'))\nPIL.Image.open(str(roses[0]))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.576321Z","iopub.execute_input":"2023-04-21T08:06:25.576721Z","iopub.status.idle":"2023-04-21T08:06:25.643584Z","shell.execute_reply.started":"2023-04-21T08:06:25.576681Z","shell.execute_reply":"2023-04-21T08:06:25.642472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIL.Image.open(str(roses[1]))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.644792Z","iopub.execute_input":"2023-04-21T08:06:25.645862Z","iopub.status.idle":"2023-04-21T08:06:25.731027Z","shell.execute_reply.started":"2023-04-21T08:06:25.645818Z","shell.execute_reply":"2023-04-21T08:06:25.730088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tulips = list(data_dir.glob('tulips/*'))\nPIL.Image.open(str(tulips[0]))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.734022Z","iopub.execute_input":"2023-04-21T08:06:25.734470Z","iopub.status.idle":"2023-04-21T08:06:25.856351Z","shell.execute_reply.started":"2023-04-21T08:06:25.734418Z","shell.execute_reply":"2023-04-21T08:06:25.855259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIL.Image.open(str(tulips[1]))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.858112Z","iopub.execute_input":"2023-04-21T08:06:25.858498Z","iopub.status.idle":"2023-04-21T08:06:25.886676Z","shell.execute_reply.started":"2023-04-21T08:06:25.858462Z","shell.execute_reply":"2023-04-21T08:06:25.885448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.888442Z","iopub.execute_input":"2023-04-21T08:06:25.888840Z","iopub.status.idle":"2023-04-21T08:06:25.894136Z","shell.execute_reply.started":"2023-04-21T08:06:25.888804Z","shell.execute_reply":"2023-04-21T08:06:25.892941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:25.895900Z","iopub.execute_input":"2023-04-21T08:06:25.896660Z","iopub.status.idle":"2023-04-21T08:06:31.334640Z","shell.execute_reply.started":"2023-04-21T08:06:25.896623Z","shell.execute_reply":"2023-04-21T08:06:31.333567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:31.339053Z","iopub.execute_input":"2023-04-21T08:06:31.339370Z","iopub.status.idle":"2023-04-21T08:06:31.584197Z","shell.execute_reply.started":"2023-04-21T08:06:31.339339Z","shell.execute_reply":"2023-04-21T08:06:31.583082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:31.586108Z","iopub.execute_input":"2023-04-21T08:06:31.587149Z","iopub.status.idle":"2023-04-21T08:06:31.592990Z","shell.execute_reply.started":"2023-04-21T08:06:31.587105Z","shell.execute_reply":"2023-04-21T08:06:31.591586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:31.594623Z","iopub.execute_input":"2023-04-21T08:06:31.595314Z","iopub.status.idle":"2023-04-21T08:06:33.018818Z","shell.execute_reply.started":"2023-04-21T08:06:31.595273Z","shell.execute_reply":"2023-04-21T08:06:33.016322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_batch, labels_batch in train_ds:\n    print(image_batch.shape)\n    print(labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:33.020759Z","iopub.execute_input":"2023-04-21T08:06:33.021490Z","iopub.status.idle":"2023-04-21T08:06:33.461573Z","shell.execute_reply.started":"2023-04-21T08:06:33.021446Z","shell.execute_reply":"2023-04-21T08:06:33.459495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:33.463237Z","iopub.execute_input":"2023-04-21T08:06:33.463570Z","iopub.status.idle":"2023-04-21T08:06:33.479410Z","shell.execute_reply.started":"2023-04-21T08:06:33.463541Z","shell.execute_reply":"2023-04-21T08:06:33.478448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalization_layer = layers.Rescaling(1./255)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:33.481086Z","iopub.execute_input":"2023-04-21T08:06:33.481496Z","iopub.status.idle":"2023-04-21T08:06:33.490515Z","shell.execute_reply.started":"2023-04-21T08:06:33.481457Z","shell.execute_reply":"2023-04-21T08:06:33.489311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 pixel values are now in `[0,1]`.\nprint(np.min(first_image), np.max(first_image))","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:33.492097Z","iopub.execute_input":"2023-04-21T08:06:33.492706Z","iopub.status.idle":"2023-04-21T08:06:38.438674Z","shell.execute_reply.started":"2023-04-21T08:06:33.492656Z","shell.execute_reply":"2023-04-21T08:06:38.437183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\n\nmodel = Sequential([\n  layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:38.441530Z","iopub.execute_input":"2023-04-21T08:06:38.442588Z","iopub.status.idle":"2023-04-21T08:06:38.608907Z","shell.execute_reply.started":"2023-04-21T08:06:38.442539Z","shell.execute_reply":"2023-04-21T08:06:38.607786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:38.610218Z","iopub.execute_input":"2023-04-21T08:06:38.610617Z","iopub.status.idle":"2023-04-21T08:06:39.447844Z","shell.execute_reply.started":"2023-04-21T08:06:38.610578Z","shell.execute_reply":"2023-04-21T08:06:39.446722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:39.449628Z","iopub.execute_input":"2023-04-21T08:06:39.450283Z","iopub.status.idle":"2023-04-21T08:06:39.481846Z","shell.execute_reply.started":"2023-04-21T08:06:39.450226Z","shell.execute_reply":"2023-04-21T08:06:39.481005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:06:39.482972Z","iopub.execute_input":"2023-04-21T08:06:39.483362Z","iopub.status.idle":"2023-04-21T08:07:14.540627Z","shell.execute_reply.started":"2023-04-21T08:06:39.483324Z","shell.execute_reply":"2023-04-21T08:07:14.539571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.models.save_model(model,'flowermodel.hdf5')","metadata":{"execution":{"iopub.status.busy":"2023-04-21T08:07:14.542297Z","iopub.execute_input":"2023-04-21T08:07:14.542679Z","iopub.status.idle":"2023-04-21T08:07:14.683192Z","shell.execute_reply.started":"2023-04-21T08:07:14.542639Z","shell.execute_reply":"2023-04-21T08:07:14.682126Z"},"trusted":true},"execution_count":null,"outputs":[]}]}