Add_assignment_3
|
@ -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()
|
|
@ -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)
|
|
@ -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)
|
|
@ -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()
|
After Width: | Height: | Size: 7.7 KiB |
After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 7.8 KiB |
After Width: | Height: | Size: 7.7 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 7.8 KiB |
After Width: | Height: | Size: 7.6 KiB |
After Width: | Height: | Size: 7.7 KiB |
After Width: | Height: | Size: 7.7 KiB |
After Width: | Height: | Size: 7.6 KiB |
After Width: | Height: | Size: 201 KiB |
After Width: | Height: | Size: 632 KiB |
|
@ -0,0 +1,5 @@
|
||||||
|
import tensorflow as tf
|
||||||
|
|
||||||
|
dataset1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([4, 10]))
|
||||||
|
|
||||||
|
print(dataset1.element_spec)
|