Xem các phiên từ Hội nghị chuyên đề WiML về các mô hình khuếch tán với KerasCV, ML trên thiết bị, v.v. Xem theo yêu cầu
- TenorFlow
- Học hỏi
- Lõi TensorFlow
- hướng dẫn
Trong hướng dẫn này, bạn sẽ học cách phân loại hình ảnh chó và mèo bằng cách sử dụng phương pháp học chuyển đổi từ mạng được đào tạo trước
Mô hình được đào tạo trước là một mạng đã lưu trước đó đã được đào tạo trên một tập dữ liệu lớn, thường là trên một tác vụ phân loại hình ảnh quy mô lớn. Bạn có thể sử dụng nguyên trạng mô hình được đào tạo trước hoặc sử dụng học chuyển đổi để tùy chỉnh mô hình này cho một nhiệm vụ nhất định
Trực giác đằng sau việc học chuyển đổi để phân loại hình ảnh là nếu một mô hình được đào tạo trên một tập dữ liệu đủ lớn và tổng quát, thì mô hình này sẽ đóng vai trò là mô hình chung của thế giới trực quan một cách hiệu quả. Sau đó, bạn có thể tận dụng các bản đồ tính năng đã học này mà không cần phải bắt đầu lại từ đầu bằng cách đào tạo một mô hình lớn trên một tập dữ liệu lớn
Trong sổ ghi chép này, bạn sẽ thử hai cách để tùy chỉnh mô hình được đào tạo trước
Khai thác tính năng. Sử dụng các biểu diễn mà mạng trước đó đã học để trích xuất các tính năng có ý nghĩa từ các mẫu mới. Bạn chỉ cần thêm một bộ phân loại mới, sẽ được đào tạo từ đầu, bên trên mô hình được đào tạo trước để bạn có thể sử dụng lại các bản đồ đặc trưng đã học trước đây cho tập dữ liệu
Bạn không cần phải [tái] đào tạo toàn bộ mô hình. Mạng tích chập cơ sở đã chứa các tính năng thường hữu ích để phân loại ảnh. Tuy nhiên, phần phân loại cuối cùng của mô hình được đào tạo trước là dành riêng cho nhiệm vụ phân loại ban đầu và sau đó là dành riêng cho tập hợp các lớp mà mô hình được đào tạo
tinh chỉnh. Giải phóng một số lớp trên cùng của cơ sở mô hình đã đóng băng và cùng huấn luyện cả lớp phân loại mới được thêm vào và lớp cuối cùng của mô hình cơ sở. Điều này cho phép chúng tôi "tinh chỉnh" các biểu diễn tính năng bậc cao hơn trong mô hình cơ sở để làm cho chúng phù hợp hơn với nhiệm vụ cụ thể
Bạn sẽ tuân theo quy trình học máy nói chung
- Kiểm tra và hiểu dữ liệu
- Xây dựng đường dẫn đầu vào, trong trường hợp này là sử dụng Keras ImageDataGenerator
- Soạn mô hình
- Tải vào mô hình cơ sở được đào tạo trước [và các trọng số được đào tạo trước]
- Chồng các lớp phân loại lên trên
- Đào tạo người mẫu
- Đánh giá mô hình
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
tiền xử lý dữ liệu
Tải xuống dữ liệu
Trong hướng dẫn này, bạn sẽ sử dụng tập dữ liệu chứa vài nghìn hình ảnh về mèo và chó. Tải xuống và giải nén tệp zip chứa hình ảnh, sau đó tạo
Found 1000 files belonging to 2 classes.8 để đào tạo và xác thực bằng tiện ích
Found 1000 files belonging to 2 classes.9. Bạn có thể tìm hiểu thêm về tải hình ảnh trong hướng dẫn này
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
______5validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
Found 1000 files belonging to 2 classes.
Hiển thị chín hình ảnh và nhãn đầu tiên từ tập huấn luyện
________số 8Vì tập dữ liệu ban đầu không chứa tập kiểm tra, bạn sẽ tạo một. Để làm như vậy, hãy xác định có bao nhiêu lô dữ liệu có sẵn trong bộ xác thực bằng cách sử dụng
class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
0, sau đó chuyển 20% trong số đó sang bộ kiểm tra2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.0
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.1
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.2
Định cấu hình tập dữ liệu cho hiệu suất
Sử dụng tìm nạp trước vào bộ đệm để tải hình ảnh từ đĩa mà không bị chặn I/O. Để tìm hiểu thêm về phương pháp này, hãy xem hướng dẫn hiệu suất dữ liệu
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.0
Sử dụng tăng cường dữ liệu
Khi bạn không có tập dữ liệu hình ảnh lớn, bạn nên giới thiệu tính đa dạng mẫu một cách giả tạo bằng cách áp dụng các phép biến đổi ngẫu nhiên nhưng thực tế cho các hình ảnh đào tạo, chẳng hạn như xoay và lật ngang. Điều này giúp hiển thị mô hình với các khía cạnh khác nhau của dữ liệu huấn luyện và giảm tình trạng thừa. Bạn có thể tìm hiểu thêm về tăng cường dữ liệu trong hướng dẫn này
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.1Lưu ý. Các lớp này chỉ hoạt động trong quá trình đào tạo, khi bạn gọi
class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
1. Chúng không hoạt động khi mô hình được sử dụng ở chế độ suy luận trong class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
2 hoặc class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
1. Hãy liên tục áp dụng các lớp này cho cùng một hình ảnh và xem kết quả
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.2____13
Thay đổi tỷ lệ giá trị pixel
Trong giây lát, bạn sẽ tải xuống
class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
4 để sử dụng làm mô hình cơ sở của mình. Mô hình này mong đợi các giá trị pixel trong class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
5, nhưng tại thời điểm này, các giá trị pixel trong hình ảnh của bạn là trong class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
6. Để thay đổi tỷ lệ chúng, hãy sử dụng phương pháp tiền xử lý đi kèm với mô hình2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.4Lưu ý. Ngoài ra, bạn có thể thay đổi tỷ lệ các giá trị pixel từ
class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
6 thành class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
5 bằng cách sử dụng class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
9. ______15Lưu ý. Nếu sử dụng 2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.00 khác, hãy đảm bảo kiểm tra tài liệu API để xác định xem họ mong đợi pixel ở
class_names = train_dataset.class_names
plt.figure[figsize=[10, 10]]
for images, labels in train_dataset.take[1]:
for i in range[9]:
ax = plt.subplot[3, 3, i + 1]
plt.imshow[images[i].numpy[].astype["uint8"]]
plt.title[class_names[labels[i]]]
plt.axis["off"]
5 hay 2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.02 hay sử dụng hàm
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.03 đi kèm.
Tạo mô hình cơ sở từ các kết nối được đào tạo trước
Bạn sẽ tạo mô hình cơ sở từ mô hình MobileNet V2 được phát triển tại Google. Điều này được đào tạo trước trên bộ dữ liệu ImageNet, một bộ dữ liệu lớn bao gồm 1. Hình ảnh 4M và 1000 lớp. ImageNet là một bộ dữ liệu đào tạo nghiên cứu với nhiều loại như
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.04 và
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.05. Cơ sở kiến thức này sẽ giúp chúng tôi phân loại chó và mèo từ tập dữ liệu cụ thể của chúng tôi
Trước tiên, bạn cần chọn lớp MobileNet V2 mà bạn sẽ sử dụng để trích xuất tính năng. Lớp phân loại cuối cùng [trên "trên cùng", vì hầu hết các sơ đồ của mô hình học máy đi từ dưới lên trên] không hữu ích lắm. Thay vào đó, bạn sẽ làm theo thông lệ chung để phụ thuộc vào lớp cuối cùng trước khi thao tác làm phẳng. Lớp này được gọi là "lớp cổ chai". Các tính năng của lớp cổ chai giữ lại tính tổng quát hơn so với lớp cuối cùng/lớp trên cùng
Đầu tiên, khởi tạo mô hình MobileNet V2 được tải sẵn với các trọng số được đào tạo trên ImageNet. Bằng cách chỉ định đối số include_top=False, bạn tải một mạng không bao gồm các lớp phân loại ở trên cùng, lý tưởng cho việc trích xuất tính năng
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.6
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.7
Trình trích xuất tính năng này chuyển đổi từng ảnh
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.06 thành một khối tính năng
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.07. Hãy xem những gì nó làm với một loạt hình ảnh ví dụ
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.8
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.9
Khai thác tính năng
Trong bước này, bạn sẽ đóng băng cơ sở tích chập được tạo từ bước trước và để sử dụng làm công cụ trích xuất tính năng. Ngoài ra, bạn thêm một trình phân loại lên trên nó và đào tạo trình phân loại cấp cao nhất
Đóng băng cơ sở tích chập
Điều quan trọng là đóng băng cơ sở tích chập trước khi bạn biên dịch và huấn luyện mô hình. Đóng băng [bằng cách đặt lớp. có thể đào tạo = Sai] ngăn các trọng số trong một lớp nhất định được cập nhật trong quá trình đào tạo. MobileNet V2 có nhiều lớp, do đó, đặt cờ
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.08 của toàn bộ mô hình thành Sai sẽ đóng băng tất cả chúng
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
0Lưu ý quan trọng về các lớp BatchNormalization
Nhiều mô hình chứa
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.09 lớp. Lớp này là một trường hợp đặc biệt và cần thực hiện các biện pháp phòng ngừa trong bối cảnh tinh chỉnh, như được trình bày sau trong hướng dẫn này
Khi bạn đặt
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.10, lớp
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.11 sẽ chạy ở chế độ suy luận và sẽ không cập nhật số liệu thống kê trung bình và phương sai của nó
Khi bạn giải phóng một mô hình có chứa các lớp BatchNormalization để tinh chỉnh, bạn nên giữ các lớp BatchNormalization ở chế độ suy luận bằng cách chuyển
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.12 khi gọi mô hình cơ sở. Mặt khác, các bản cập nhật được áp dụng cho các trọng số không thể đào tạo sẽ phá hủy những gì mô hình đã học được
Để biết thêm chi tiết, xem hướng dẫn học Chuyển tiếp
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
1_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
2Thêm đầu phân loại
Để tạo dự đoán từ khối tính năng, hãy tính trung bình trên các vị trí không gian
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.13 không gian, sử dụng lớp
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.14 để chuyển đổi các tính năng thành một vectơ 1280 phần tử trên mỗi hình ảnh
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
3_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
4Áp dụng lớp
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.15 để chuyển đổi các tính năng này thành một dự đoán duy nhất cho mỗi hình ảnh. Bạn không cần chức năng kích hoạt ở đây vì dự đoán này sẽ được coi là
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.16 hoặc giá trị dự đoán thô. Số dương dự đoán lớp 1, số âm dự đoán lớp 0
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
5_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
6Xây dựng một mô hình bằng cách kết hợp các lớp tăng cường dữ liệu, thay đổi kích thước,
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.17 và trích xuất tính năng bằng API chức năng Keras. Như đã đề cập trước đây, hãy sử dụng
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.18 vì mô hình của chúng tôi chứa lớp
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.11
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
7_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
8Biên dịch mô hình
Biên dịch mô hình trước khi đào tạo nó. Vì có hai lớp, hãy sử dụng tổn thất
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.20 với
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.21 vì mô hình cung cấp đầu ra tuyến tính
_URL = '//storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file['cats_and_dogs.zip', origin=_URL, extract=True]
PATH = os.path.join[os.path.dirname[path_to_zip], 'cats_and_dogs_filtered']
train_dir = os.path.join[PATH, 'train']
validation_dir = os.path.join[PATH, 'validation']
BATCH_SIZE = 32
IMG_SIZE = [160, 160]
train_dataset = tf.keras.utils.image_dataset_from_directory[train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
9Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.0
Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.1
2. 5 triệu tham số trong MobileNet bị đóng băng, nhưng có 1. 2 nghìn tham số có thể huấn luyện trong lớp Dense. Chúng được phân chia giữa hai đối tượng
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.22, trọng số và độ lệch
Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.2____53
Đào tạo người mẫu
Sau khi đào tạo trong 10 kỷ nguyên, bạn sẽ thấy độ chính xác ~94% trên bộ xác thực
Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.4____55____56____57
Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.8
Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.9
đường cong học tập
Chúng ta hãy xem các đường cong học tập về độ chính xác/mất xác thực và đào tạo khi sử dụng mô hình cơ sở MobileNetV2 làm công cụ trích xuất tính năng cố định
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
02022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.09 và
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.24 ảnh hưởng đến độ chính xác trong quá trình đào tạo. Chúng bị tắt khi tính toán mất xác thực.
Ở mức độ thấp hơn, đó cũng là do các chỉ số đào tạo báo cáo mức trung bình cho một kỷ nguyên, trong khi các chỉ số xác thực được đánh giá sau kỷ nguyên, vì vậy, các chỉ số xác thực thấy một mô hình đã được đào tạo lâu hơn một chút
tinh chỉnh
Trong thử nghiệm trích xuất tính năng, bạn chỉ đào tạo một vài lớp trên mô hình cơ sở MobileNetV2. Trọng số của mạng được đào tạo trước không được cập nhật trong quá trình đào tạo
Một cách để tăng hiệu suất hơn nữa là đào tạo [hoặc "tinh chỉnh"] trọng số của các lớp trên cùng của mô hình được đào tạo trước cùng với việc đào tạo bộ phân loại mà bạn đã thêm. Quá trình đào tạo sẽ buộc các trọng số được điều chỉnh từ bản đồ tính năng chung sang các tính năng được liên kết cụ thể với tập dữ liệu
Ghi chú. Điều này chỉ nên được thực hiện sau khi bạn đã đào tạo trình phân loại cấp cao nhất với mô hình được đào tạo trước được đặt thành không thể đào tạo. Nếu bạn thêm một trình phân loại được khởi tạo ngẫu nhiên lên trên một mô hình được đào tạo trước và cố gắng huấn luyện tất cả các lớp cùng nhau, cường độ của các bản cập nhật độ dốc sẽ quá lớn [do các trọng số ngẫu nhiên từ trình phân loại] và mô hình được đào tạo trước của bạn sẽ .Ngoài ra, bạn nên cố gắng tinh chỉnh một số lượng nhỏ các lớp trên cùng thay vì toàn bộ mô hình MobileNet. Trong hầu hết các mạng tích chập, lớp càng cao thì càng chuyên biệt. Một vài lớp đầu tiên tìm hiểu các tính năng rất đơn giản và chung chung, khái quát cho hầu hết các loại hình ảnh. Khi bạn lên cao hơn, các tính năng ngày càng cụ thể hơn đối với tập dữ liệu mà mô hình được đào tạo. Mục tiêu của việc tinh chỉnh là điều chỉnh các tính năng chuyên biệt này để hoạt động với tập dữ liệu mới, thay vì ghi đè lên quá trình học tập chung
Hủy đóng băng các lớp trên cùng của mô hình
Tất cả những gì bạn cần làm là giải phóng
2022-12-14 02:23:03.432706: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432824: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 02:23:03.432835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.17 và đặt các lớp dưới cùng thành không thể huấn luyện. Sau đó, bạn nên biên dịch lại mô hình [cần thiết để những thay đổi này có hiệu lực] và tiếp tục đào tạo
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
1validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
2validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
3Biên dịch mô hình
Vì bạn đang đào tạo một mô hình lớn hơn nhiều và muốn đọc lại các trọng số đã được đào tạo trước, điều quan trọng là sử dụng tỷ lệ học tập thấp hơn ở giai đoạn này. Nếu không, mô hình của bạn có thể quá khớp rất nhanh
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
4Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.0
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
6Downloading data from //storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip 68606236/68606236 [==============================] - 0s 0us/step Found 2000 files belonging to 2 classes.2
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
8Tiếp tục huấn luyện người mẫu
Nếu bạn được đào tạo để hội tụ sớm hơn, bước này sẽ cải thiện độ chính xác của bạn thêm một vài điểm phần trăm
validation_dataset = tf.keras.utils.image_dataset_from_directory[validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE]
9Found 1000 files belonging to 2 classes.0
Chúng ta hãy xem các đường cong học tập về độ chính xác/mất xác thực và đào tạo khi tinh chỉnh một vài lớp cuối cùng của mô hình cơ sở MobileNetV2 và đào tạo bộ phân loại trên đầu trang của nó. Mất xác nhận cao hơn nhiều so với mất đào tạo, vì vậy bạn có thể bị quá khớp
Bạn cũng có thể bị thừa trang vì tập huấn luyện mới tương đối nhỏ và giống với tập dữ liệu MobileNetV2 ban đầu
Sau khi tinh chỉnh, mô hình gần đạt độ chính xác 98% trên bộ xác thực
Found 1000 files belonging to 2 classes.1
Found 1000 files belonging to 2 classes.2
Đánh giá và dự đoán
Cuối cùng, bạn có thể xác minh hiệu suất của mô hình trên dữ liệu mới bằng bộ kiểm tra
Found 1000 files belonging to 2 classes.3____24
Và bây giờ bạn đã sẵn sàng sử dụng mô hình này để dự đoán xem thú cưng của bạn là mèo hay chó
Found 1000 files belonging to 2 classes.5
Found 1000 files belonging to 2 classes.6
Tóm lược
Sử dụng mô hình được đào tạo trước để trích xuất tính năng. Khi làm việc với một tập dữ liệu nhỏ, một thực tế phổ biến là tận dụng các tính năng được học bởi một mô hình được đào tạo trên tập dữ liệu lớn hơn trong cùng một miền. Điều này được thực hiện bằng cách khởi tạo mô hình được đào tạo trước và thêm bộ phân loại được kết nối đầy đủ ở trên cùng. Mô hình được đào tạo trước bị "đóng băng" và chỉ các trọng số của bộ phân loại được cập nhật trong quá trình đào tạo. Trong trường hợp này, cơ sở tích chập đã trích xuất tất cả các tính năng được liên kết với từng hình ảnh và bạn vừa đào tạo một trình phân loại xác định lớp hình ảnh được cung cấp cho tập hợp các tính năng được trích xuất đó
Tinh chỉnh một mô hình được đào tạo trước. Để cải thiện hơn nữa hiệu suất, người ta có thể muốn sử dụng lại các lớp cấp cao nhất của các mô hình được đào tạo trước sang tập dữ liệu mới thông qua tinh chỉnh. Trong trường hợp này, bạn đã điều chỉnh trọng số của mình sao cho mô hình của bạn đã học được các tính năng cấp cao dành riêng cho tập dữ liệu. Kỹ thuật này thường được khuyến nghị khi tập dữ liệu huấn luyện lớn và rất giống với tập dữ liệu gốc mà mô hình huấn luyện trước đã được huấn luyện trên đó