Convolutional Neural Network - CNN
Cùng xây dựng model để giải quyết bài toán phân biệt dog or cat nào!
pool để giảm sample trong input
Cấu trúc dataset:
Bộ dữ liệu gồm có 10000 ảnh: 8000 ảnh train( 4000 mỗi loại dog, cat) và 2000 ảnh test( 1000 mỗi loại)
Phần 1: Xây dựng mô hình:
Trước tiên cài đặt các thư viện cần thiết như Keras, Tensorflow, nếu các bạn đã install thì skip nhé :)
# Importing the Keras libraries and packages from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense
It's time to start now!
* Lưu ý: vì trong bài chỉ có 2 class nên dùng binary_crossentropy, nếu nhiều class thì thay bằng categorical_crossentropy.
pool để giảm sample trong input
* Tương tự như chú ý ở phần 1: class_mode có thể tùy chỉnh theo từng bài toán mà có thể đặt là binary hay categorial
Phần 3: Đoán thử ảnh
from keras.preprocessing import image
test_image = image.load_img('random.jpg', target_size=(64,64)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image,axis=0) result = classifier.predict(test_image) training_set.class_indices if result[0][0] == 1: prediction = 'dog'else: prediction = 'cat'print(prediction)
Phần 4: Lưu và tải mô hình
from keras.models import save_model
classifier.save('dog_and_cat_model.h5')
from keras.models import load_model import numpy as np new_model = load_model('dog_and_cat_model.h5')
Mình muốn giới thiệu với các bạn một series các bài học về ML tìm được trên youtube tại ĐÂY
Nhận xét
Đăng nhận xét