



Preview text:
lOMoAR cPSD| 58778885 Bài tập 1:
Viết chương trình cho phép kiểm tra các file hình ảnh của thư viện Fashion của MNIST như ví dụ sau:
# Example of Classification with Big Data Set # Load the dataset import os
# Check if we have the classes labels =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr ain') print(labels)
# Check the number of images of each class files_0 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 0:', files_0[:10])
print('Number of files 0:', len(files_0)) files_1 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 1:', files_1[:10])
print('Number of files 0:', len(files_0)) files_2 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 2:', files_2[:10])
print('Number of files 0:', len(files_0)) files_3 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 3:', files_3[:10])
print('Number of files 0:', len(files_0)) files_4 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 4:', files_4[:10])
print('Number of files 0:', len(files_0)) lOMoAR cPSD| 58778885 files_5 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 5:', files_5[:10]) print('Number of files 0:', len(files_0)) files_6 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 6:', files_6[:10]) print('Number of files 0:', len(files_0)) files_7 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 7:', files_7[:10]) print('Number of files 0:', len(files_0)) files_8 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 8:', files_8[:10]) print('Number of files 0:', len(files_0)) files_9 =
os.listdir('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/0') print('Class 9:', files_9[:10]) print('Number of files 0:', len(files_0)) Bài tập 2:
Viết chương trình cho phép load và hiển thị file hình ảnh của thư viện Fashion của MNIST như ví dụ sau:
# Example of Classification with Big Data Set
# Visualize the images of the dataset import os import cv2 import numpy as np
import matplotlib.pyplot as plt image_data =
cv2.imread('/home/rai_lab/NeuralNetwork/TeachingWeeks13/Fashion_Mnist_Images/tr
ain/4/0011.png',cv2.IMREAD_UNCHANGED) np.set_printoptions(linewidth=200) print(image_data) plt.imshow(image_data)
#plt.imshow(image_data,cmap = 'gray') # use color filtering plt.show() lOMoAR cPSD| 58778885 Bài tập 3:
Viết file MNIST.py để định nghĩa các hàm cho phép download, tạo training & validation
& test data, xử lý dữ liệu (scale, flattening, shuffling) như ví dụ bên dưới: import os import cv2 import numpy as np import matplotlib.pyplot as plt def
load_MNIST_dataset(dataset,path):
labels = os.listdir(os.path.join(path,dataset)) #Create list for samples and labels X = [] y = []
for label in labels: for file in
os.listdir(os.path.join(path,dataset,label)):
image = cv2.imread(os.path.join(path,dataset,label,file), cv2.IMREAD_UNCHANGED) X.append(image) y.append(label)
return np.array(X), np.array(y).astype('uint8') def create_data_MNIST(path): # load both sets separately:
X, y = load_MNIST_dataset('train',path)
X_test, y_test = load_MNIST_dataset('test',path) lOMoAR cPSD| 58778885 return X, y, X_test, y_test
# Scale range from 0-255 to min-max def scale(X,min,max):
X_scaled = (X.astype(np.float32) - 127.5) / 127.5 return X_scaled def flatten(X):
X_reshaped = X.reshape((X.shape[0],-1)) return X_reshaped def shuffle(X,y):
# Create keys to allow shuffling 2 vectors X and y
simultaneously keys = np.array(range(X.shape[0])) np.random.shuffle(keys) X_shuffled = X[keys] y_shuffled = y[keys] return X_shuffled, y_shuffled Bài tập 4:
Thực hiện huấn luyện và test trên dataset Fashion của MNIST. Bài tập 5:
Viết file python cho phép huấn luyện dataset Fashion của MNIST và save mô hình. Viết
file python cho phép load mô hình đã huấn luyện và đưa ra dự đoán trên 1 file ảnh bất kỳ.