-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TransferLearn_WeatherClassification_InceptionNet
- Loading branch information
1 parent
412aff9
commit 8b073b4
Showing
1 changed file
with
1 addition
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
22-07-01-CNN_Art/april-transfer-learning-weather-recognition.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-02T17:12:03.690743Z","iopub.execute_input":"2022-07-02T17:12:03.691670Z","iopub.status.idle":"2022-07-02T17:12:03.697048Z","shell.execute_reply.started":"2022-07-02T17:12:03.691613Z","shell.execute_reply":"2022-07-02T17:12:03.695409Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = (299, 299)\nDATASET_PATH = '../input/weather-dataset/dataset'\nSEED = 100","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:15:01.569245Z","iopub.execute_input":"2022-07-02T17:15:01.569598Z","iopub.status.idle":"2022-07-02T17:15:01.574645Z","shell.execute_reply.started":"2022-07-02T17:15:01.569568Z","shell.execute_reply":"2022-07-02T17:15:01.573611Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"markdown","source":"https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory?version=nightly","metadata":{}},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n DATASET_PATH,\n batch_size=32,\n image_size=IMAGE_SIZE,\n shuffle=True,\n seed=SEED,\n validation_split=0.3,\n subset='training',\n)\n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n DATASET_PATH,\n batch_size=32,\n image_size=IMAGE_SIZE,\n shuffle=True,\n seed=SEED,\n validation_split=0.3,\n subset='validation',\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:15:41.761504Z","iopub.execute_input":"2022-07-02T17:15:41.762639Z","iopub.status.idle":"2022-07-02T17:15:46.359646Z","shell.execute_reply.started":"2022-07-02T17:15:41.762584Z","shell.execute_reply":"2022-07-02T17:15:46.358673Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nclass_names","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:16:09.374929Z","iopub.execute_input":"2022-07-02T17:16:09.375502Z","iopub.status.idle":"2022-07-02T17:16:09.382217Z","shell.execute_reply.started":"2022-07-02T17:16:09.375465Z","shell.execute_reply":"2022-07-02T17:16:09.381139Z"},"trusted":true},"execution_count":7,"outputs":[]},{"cell_type":"code","source":"def preprocess(image, label):\n image = tf.keras.applications.inception_v3.preprocess_input(image)\n return image, label\n\ntrain_ds = train_ds.map(preprocess)\nval_ds = val_ds.map(preprocess)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:20:15.221089Z","iopub.execute_input":"2022-07-02T17:20:15.221449Z","iopub.status.idle":"2022-07-02T17:20:15.280485Z","shell.execute_reply.started":"2022-07-02T17:20:15.221419Z","shell.execute_reply":"2022-07-02T17:20:15.279574Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"code","source":"feature_extractor = tf.keras.applications.inception_v3.InceptionV3(\n include_top=False,\n weights='imagenet',\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:21:26.120116Z","iopub.execute_input":"2022-07-02T17:21:26.120490Z","iopub.status.idle":"2022-07-02T17:21:29.943904Z","shell.execute_reply.started":"2022-07-02T17:21:26.120459Z","shell.execute_reply":"2022-07-02T17:21:29.942910Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"markdown","source":"```\nx = 5\nx = math.pow(x, 2)\nx = math.sqrt(x)\n\nmath.sqrt( math.pow(5, 2) )\n```","metadata":{}},{"cell_type":"code","source":"x = tf.keras.layers.GlobalAveragePooling2D()(feature_extractor.outputs[0])\nx = tf.keras.layers.Flatten()(x)\noutput = tf.keras.layers.Dense(len(class_names), activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs = feature_extractor.inputs, outputs = [output])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:32:41.163477Z","iopub.execute_input":"2022-07-02T17:32:41.164124Z","iopub.status.idle":"2022-07-02T17:32:41.234728Z","shell.execute_reply.started":"2022-07-02T17:32:41.164087Z","shell.execute_reply":"2022-07-02T17:32:41.233774Z"},"trusted":true},"execution_count":16,"outputs":[]},{"cell_type":"code","source":"feature_extractor.trainable = False\n\nmodel.compile(\n loss='sparse_categorical_crossentropy',\\\n optimizer='adam',\n metrics = ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:33:57.925166Z","iopub.execute_input":"2022-07-02T17:33:57.925803Z","iopub.status.idle":"2022-07-02T17:33:57.956305Z","shell.execute_reply.started":"2022-07-02T17:33:57.925767Z","shell.execute_reply":"2022-07-02T17:33:57.955424Z"},"trusted":true},"execution_count":17,"outputs":[]},{"cell_type":"code","source":"model.fit(train_ds, validation_data=val_ds, epochs=2)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:34:03.937806Z","iopub.execute_input":"2022-07-02T17:34:03.938749Z","iopub.status.idle":"2022-07-02T17:36:10.414530Z","shell.execute_reply.started":"2022-07-02T17:34:03.938701Z","shell.execute_reply":"2022-07-02T17:36:10.413543Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"code","source":"url = 'https://i.pinimg.com/originals/21/8b/2d/218b2dea0005efbfef1b78901714b30d.jpg'\n\nfile_path = tf.keras.utils.get_file(origin=url)\nimg = tf.keras.utils.load_img(\n file_path,\n target_size=IMAGE_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:37:03.443522Z","iopub.execute_input":"2022-07-02T17:37:03.444438Z","iopub.status.idle":"2022-07-02T17:37:04.211466Z","shell.execute_reply.started":"2022-07-02T17:37:03.444398Z","shell.execute_reply":"2022-07-02T17:37:04.210587Z"},"trusted":true},"execution_count":19,"outputs":[]},{"cell_type":"code","source":"print(type(img))\nimg","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:37:32.826906Z","iopub.execute_input":"2022-07-02T17:37:32.827456Z","iopub.status.idle":"2022-07-02T17:37:32.863550Z","shell.execute_reply.started":"2022-07-02T17:37:32.827418Z","shell.execute_reply":"2022-07-02T17:37:32.862665Z"},"trusted":true},"execution_count":22,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nimg = np.asarray(img)\nimg, _ = preprocess(img, '')","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:38:03.818122Z","iopub.execute_input":"2022-07-02T17:38:03.818462Z","iopub.status.idle":"2022-07-02T17:38:03.825582Z","shell.execute_reply.started":"2022-07-02T17:38:03.818431Z","shell.execute_reply":"2022-07-02T17:38:03.824525Z"},"trusted":true},"execution_count":23,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:38:18.982840Z","iopub.execute_input":"2022-07-02T17:38:18.983413Z","iopub.status.idle":"2022-07-02T17:38:19.277459Z","shell.execute_reply.started":"2022-07-02T17:38:18.983369Z","shell.execute_reply":"2022-07-02T17:38:19.276493Z"},"trusted":true},"execution_count":25,"outputs":[]},{"cell_type":"code","source":"img = img.reshape([1, *IMAGE_SIZE, 3])\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:38:51.299652Z","iopub.execute_input":"2022-07-02T17:38:51.300105Z","iopub.status.idle":"2022-07-02T17:38:51.309291Z","shell.execute_reply.started":"2022-07-02T17:38:51.300064Z","shell.execute_reply":"2022-07-02T17:38:51.308348Z"},"trusted":true},"execution_count":26,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(img)\nprint(pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:39:07.675690Z","iopub.execute_input":"2022-07-02T17:39:07.676544Z","iopub.status.idle":"2022-07-02T17:39:09.506493Z","shell.execute_reply.started":"2022-07-02T17:39:07.676510Z","shell.execute_reply":"2022-07-02T17:39:09.505325Z"},"trusted":true},"execution_count":27,"outputs":[]},{"cell_type":"code","source":"pred[0].argmax()","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:39:29.321396Z","iopub.execute_input":"2022-07-02T17:39:29.322091Z","iopub.status.idle":"2022-07-02T17:39:29.328035Z","shell.execute_reply.started":"2022-07-02T17:39:29.322052Z","shell.execute_reply":"2022-07-02T17:39:29.327110Z"},"trusted":true},"execution_count":29,"outputs":[]},{"cell_type":"code","source":"class_names[6]","metadata":{"execution":{"iopub.status.busy":"2022-07-02T17:39:35.860730Z","iopub.execute_input":"2022-07-02T17:39:35.861172Z","iopub.status.idle":"2022-07-02T17:39:35.867912Z","shell.execute_reply.started":"2022-07-02T17:39:35.861132Z","shell.execute_reply":"2022-07-02T17:39:35.866955Z"},"trusted":true},"execution_count":30,"outputs":[]}]} |