diff --git a/assignment_3/01_Res50_Predict.py b/assignment_3/01_Res50_Predict.py new file mode 100644 index 0000000..9a6f514 --- /dev/null +++ b/assignment_3/01_Res50_Predict.py @@ -0,0 +1,31 @@ +# TensorFlow and tf.keras +import tensorflow as tf +from tensorflow.keras.applications.imagenet_utils import decode_predictions +# Helper libraries +import numpy as np +import cv2 + +# load an image +Testing_img_path ="D:\ISE\Internet of Things\RPi_Lab 3\cat2.jpg" +input_image = cv2.imread(Testing_img_path) + +# Preprae data +input_image = cv2.resize(input_image, (224, 224)) +input_image = np.expand_dims(input_image, axis=0) # Change the shape of image array like input image when trained + +# Load the ResNet50 model +loadpath = "Resnet50.h5" +resnet_model = tf.keras.models.load_model(loadpath) + +# get the predicted probabilities for each class +predictions = resnet_model.predict(input_image) +print("Predicted Top 3 is:", decode_predictions(predictions, top=3)[0]) +Answer = decode_predictions(predictions, top=1)[0][0][1] +print("Predicted = ", Answer) + + +# Visualize, check the answer +img = cv2.imread(Testing_img_path) + +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/assignment_3/01_Res50_export.py b/assignment_3/01_Res50_export.py new file mode 100644 index 0000000..46cdeb6 --- /dev/null +++ b/assignment_3/01_Res50_export.py @@ -0,0 +1,10 @@ +# TensorFlow and tf.keras +import tensorflow as tf +from keras.applications import resnet50 + +# Load the ResNet50 model +resnet50_model = resnet50.ResNet50(weights='imagenet') + +# Save model and Export +savepath = "Resnet50.h5" +resnet50_model.save(savepath) \ No newline at end of file diff --git a/assignment_3/02_detect_number_train.py b/assignment_3/02_detect_number_train.py new file mode 100644 index 0000000..1f79db7 --- /dev/null +++ b/assignment_3/02_detect_number_train.py @@ -0,0 +1,30 @@ +# TensorFlow and tf.keras +import tensorflow as tf +from tensorflow import keras + +#load data set from tf.keras.datasets.mnist (Train number and Test number) +mnist = tf.keras.datasets.mnist # 28x28 image of hand-written digits 0-9 +(x_train, y_train),(x_test, y_test) = mnist.load_data() + +# Build Model +model = keras.Sequential([ + keras.layers.Flatten(input_shape=(28, 28)), # takes our 28x28 and makes it 1x784 + keras.layers.Dense(128, activation='relu'), # a simple fully-connected layer, 128 units, relu activation + keras.layers.Dense(128, activation='relu'), # a simple fully-connected layer, 128 units, relu activation + keras.layers.Dense(10, activation='softmax') # our output layer. 10 units for 10 classes. Softmax for probability distribution +]) +# Compile model +model.compile(optimizer='adam', + loss='sparse_categorical_crossentropy', + metrics=['accuracy']) +# Train the model +model.fit(x_train, y_train, epochs=5) + +# Evaluate accuracy +test_loss, test_acc = model.evaluate(x_test, y_test) +print("Test loss:", test_loss) +print("Test accuracy:", test_acc) + +# ***Save model*** +savepath = "num_reader.h5" +model.save(savepath) \ No newline at end of file diff --git a/assignment_3/09_detect_number_predict.py b/assignment_3/09_detect_number_predict.py new file mode 100644 index 0000000..9ca79cb --- /dev/null +++ b/assignment_3/09_detect_number_predict.py @@ -0,0 +1,30 @@ +# TensorFlow and tf.keras +import tensorflow as tf +from tensorflow import keras + +# Helper libraries +import numpy as np +import cv2 + +# load an image +Testing_img_path = "./Testing_images/0.jpg" +input_image = cv2.imread(Testing_img_path,cv2.IMREAD_GRAYSCALE) + +# Preprae data +input_image = cv2.resize(input_image,(28, 28)) +input_image = cv2.bitwise_not(input_image) # invert Black to White like data input when trained (Line is White) +input_image = np.expand_dims(input_image, axis=0) # Change the shape of image array like input image when trained + +# ***load model*** +loadpath = "num_reader.h5" +model = tf.keras.models.load_model(loadpath) + +# Make predictions +predictions = model.predict(input_image) +print(np.argmax(predictions)) + +# Visualize, check the answer +Original_input_image_testing = cv2.imread(Testing_img_path) +cv2.imshow("This is what your computer has seen.",Original_input_image_testing) +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/assignment_3/Resnet50.h5 b/assignment_3/Resnet50.h5 new file mode 100644 index 0000000..bccb41c Binary files /dev/null and b/assignment_3/Resnet50.h5 differ diff --git a/assignment_3/Testing_images/0.jpg b/assignment_3/Testing_images/0.jpg new file mode 100644 index 0000000..423a1e2 Binary files /dev/null and b/assignment_3/Testing_images/0.jpg differ diff --git a/assignment_3/Testing_images/1.jpg b/assignment_3/Testing_images/1.jpg new file mode 100644 index 0000000..c959a0e Binary files /dev/null and b/assignment_3/Testing_images/1.jpg differ diff --git a/assignment_3/Testing_images/2.jpg b/assignment_3/Testing_images/2.jpg new file mode 100644 index 0000000..10ffd48 Binary files /dev/null and b/assignment_3/Testing_images/2.jpg differ diff --git a/assignment_3/Testing_images/3.jpg b/assignment_3/Testing_images/3.jpg new file mode 100644 index 0000000..669f131 Binary files /dev/null and b/assignment_3/Testing_images/3.jpg differ diff --git a/assignment_3/Testing_images/4.jpg b/assignment_3/Testing_images/4.jpg new file mode 100644 index 0000000..eecce67 Binary files /dev/null and b/assignment_3/Testing_images/4.jpg differ diff --git a/assignment_3/Testing_images/5.jpg b/assignment_3/Testing_images/5.jpg new file mode 100644 index 0000000..b0874b5 Binary files /dev/null and b/assignment_3/Testing_images/5.jpg differ diff --git a/assignment_3/Testing_images/6.jpg b/assignment_3/Testing_images/6.jpg new file mode 100644 index 0000000..1d6fd72 Binary files /dev/null and b/assignment_3/Testing_images/6.jpg differ diff --git a/assignment_3/Testing_images/7.jpg b/assignment_3/Testing_images/7.jpg new file mode 100644 index 0000000..bbb4aba Binary files /dev/null and b/assignment_3/Testing_images/7.jpg differ diff --git a/assignment_3/Testing_images/8.jpg b/assignment_3/Testing_images/8.jpg new file mode 100644 index 0000000..f87e56c Binary files /dev/null and b/assignment_3/Testing_images/8.jpg differ diff --git a/assignment_3/Testing_images/9.jpg b/assignment_3/Testing_images/9.jpg new file mode 100644 index 0000000..a722b6c Binary files /dev/null and b/assignment_3/Testing_images/9.jpg differ diff --git a/assignment_3/Testing_images/MNIST Number.png b/assignment_3/Testing_images/MNIST Number.png new file mode 100644 index 0000000..8144b87 Binary files /dev/null and b/assignment_3/Testing_images/MNIST Number.png differ diff --git a/assignment_3/cat2.jpg b/assignment_3/cat2.jpg new file mode 100644 index 0000000..d8e9188 Binary files /dev/null and b/assignment_3/cat2.jpg differ diff --git a/assignment_3/num_reader.h5 b/assignment_3/num_reader.h5 new file mode 100644 index 0000000..dea5fcc Binary files /dev/null and b/assignment_3/num_reader.h5 differ diff --git a/assignment_3/tf_test.py b/assignment_3/tf_test.py new file mode 100644 index 0000000..db78a80 --- /dev/null +++ b/assignment_3/tf_test.py @@ -0,0 +1,5 @@ +import tensorflow as tf + +dataset1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([4, 10])) + +print(dataset1.element_spec) \ No newline at end of file