28.04.2020 Views

Sách Deep Learning cơ bản

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

11.1 Transfer learning 151

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import GridSearchCV

from sklearn.metrics import classification_report

from imutils import paths

from keras.applications import VGG16

from keras.applications import imagenet_utils

from keras.preprocessing.image import img_to_array

from keras.preprocessing.image import load_img

from sklearn.preprocessing import LabelEncoder

from sklearn.model_selection import train_test_split

import numpy as np

import random

import os

# Lấy các đường dẫn đến ảnh.

image_path = list(paths.list_images('dataset/'))

# Đổi vị trí ngẫu nhiên các đường dẫn ảnh

random.shuffle(image_path)

# Đường dẫn ảnh sẽ là dataset/tên_loài_hoa/tên_ảnh ví dụ dataset/Bluebell/image_0241.jpg n

labels = [p.split(os.path.sep)[-2] for p in image_path]

# Chuyển tên các loài hoa thành số

le = LabelEncoder()

labels = le.fit_transform(labels)

# Load model VGG 16 của ImageNet dataset, include_top=False để bỏ phần Fully connected lay

model = VGG16(weights='imagenet', include_top=False)

# Load ảnh và resize về đúng kích thước mà VGG 16 cần là (224,224)

list_image = []

for (j, imagePath) in enumerate(image_path):

image = load_img(imagePath, target_size=(224, 224))

image = img_to_array(image)

image = np.expand_dims(image, 0)

image = imagenet_utils.preprocess_input(image)

list_image.append(image)

list_image = np.vstack(list_image)

# Dùng pre-trained model để lấy ra các feature của ảnh

features = model.predict(list_image)

# Giống bước flatten trong CNN, chuyển từ tensor 3 chiều sau ConvNet sang vector 1 chiều

features = features.reshape((features.shape[0], 512*7*7))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!