Thuộc tính hình ảnh python

Dictionary Representation of A Graph Object:

{'data': [{'x': [1, 2, 3], 'y': [1, 3, 2], 'type': 'bar'}], 'layout': {'height': 600, 'width': 800}}



JSON Representation of A Graph Object:

{"data":[{"x":[1,2,3],"y":[1,3,2],"type":"bar"}],"layout":{"height":600,"width":800}}



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

Tải và tiền xử lý hình ảnh Sắp xếp ngăn nắp với các bộ sưu tập Lưu và phân loại nội dung dựa trên sở thích của bạn

Xem trên TensorFlow. org
Chạy trong Google Colab
Xem nguồn trên GitHub
Tải xuống sổ ghi chép

Hướng dẫn này chỉ ra cách tải và xử lý sơ bộ tập dữ liệu hình ảnh theo ba cách

  • Trước tiên, bạn sẽ sử dụng các tiện ích tiền xử lý Keras cấp cao [chẳng hạn như
    import pathlib
    dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
    data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                       fname='flower_photos',
                                       untar=True]
    data_dir = pathlib.Path[data_dir]
    
    3] và các lớp [chẳng hạn như
    import pathlib
    dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
    data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                       fname='flower_photos',
                                       untar=True]
    data_dir = pathlib.Path[data_dir]
    
    4] để đọc thư mục hình ảnh trên đĩa
  • Tiếp theo, bạn sẽ viết đường dẫn đầu vào của riêng mình từ đầu bằng cách sử dụng tf. dữ liệu
  • Cuối cùng, bạn sẽ tải xuống một bộ dữ liệu từ danh mục lớn có sẵn trong Bộ dữ liệu TensorFlow

Cài đặt

import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import tensorflow_datasets as tfds
2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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.
print[tf.__version__]
2.11.0

Tải xuống bộ dữ liệu hoa

Hướng dẫn này sử dụng bộ dữ liệu gồm vài nghìn bức ảnh về hoa. Bộ dữ liệu hoa chứa năm thư mục con, mỗi thư mục một lớp

flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
Lưu ý. tất cả hình ảnh đều được cấp phép CC-BY, người sáng tạo được liệt kê trong GIẤY PHÉP. tập tin txt. ______1

Sau khi tải xuống [218 MB], bây giờ bạn sẽ có sẵn một bản sao của ảnh hoa. Có tổng số 3.670 hình ảnh

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
0

Mỗi thư mục chứa hình ảnh của loại hoa đó. Đây là một số hoa hồng

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
1

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
2

Tải dữ liệu bằng tiện ích Keras

Hãy tải những hình ảnh này ra khỏi đĩa bằng tiện ích

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
3 hữu ích

Tạo tập dữ liệu

Xác định một số thông số cho bộ nạp

2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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

Bạn nên sử dụng phân tách xác thực khi phát triển mô hình của mình. Bạn sẽ sử dụng 80% hình ảnh để đào tạo và 20% để xác thực

_______41____42____43
2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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.
4

Bạn có thể tìm tên lớp trong thuộc tính

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
6 trên các bộ dữ liệu này

2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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.
5
2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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

Trực quan hóa dữ liệu

Dưới đây là chín hình ảnh đầu tiên từ tập dữ liệu huấn luyện

2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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

Bạn có thể đào tạo một mô hình bằng cách sử dụng các bộ dữ liệu này bằng cách chuyển chúng tới

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
7 [hiển thị sau trong hướng dẫn này]. Nếu muốn, bạn cũng có thể lặp lại thủ công tập dữ liệu và truy xuất hàng loạt hình ảnh

2022-12-14 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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 03:36:54.932173: 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 03:36:54.932271: 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 03:36:54.932280: 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

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
8 là một tenxơ có dạng
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
9. Đây là lô gồm 32 hình ảnh có hình dạng
image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
0 [kích thước cuối cùng đề cập đến các kênh màu RGB].
image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
1 là một tensor của hình
image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
2, đây là các nhãn tương ứng với 32 hình ảnh

Bạn có thể gọi

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
3 trên một trong hai tenxơ này để chuyển đổi chúng thành
image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
4

Chuẩn hóa dữ liệu

Các giá trị kênh RGB nằm trong phạm vi

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
5. Điều này không lý tưởng cho mạng lưới thần kinh;

Tại đây, bạn sẽ chuẩn hóa các giá trị nằm trong phạm vi

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
6 bằng cách sử dụng
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
4

print[tf.__version__]
0

Có hai cách để sử dụng lớp này. Bạn có thể áp dụng nó vào tập dữ liệu bằng cách gọi

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
8

print[tf.__version__]
1
print[tf.__version__]
2

Hoặc, bạn có thể bao gồm lớp bên trong định nghĩa mô hình của mình để đơn giản hóa việc triển khai. Bạn sẽ sử dụng cách tiếp cận thứ hai ở đây

Ghi chú. Nếu bạn muốn chia tỷ lệ giá trị pixel thành
image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
9, bạn có thể viết
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
00
Lưu ý. Bạn đã thay đổi kích thước hình ảnh trước đây bằng cách sử dụng đối số
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
01 của
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
3. Nếu bạn cũng muốn đưa logic thay đổi kích thước vào mô hình của mình, bạn có thể sử dụng lớp
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
03.

Định cấu hình tập dữ liệu cho hiệu suất

Hãy đảm bảo sử dụng tính năng tìm nạp trước vào bộ đệm để bạn có thể lấy dữ liệu từ đĩa mà không bị chặn I/O. Đây là hai phương pháp quan trọng bạn nên sử dụng khi tải dữ liệu

  • import pathlib
    dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
    data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                       fname='flower_photos',
                                       untar=True]
    data_dir = pathlib.Path[data_dir]
    
    04 giữ hình ảnh trong bộ nhớ sau khi chúng được tải ra khỏi đĩa trong kỷ nguyên đầu tiên. Điều này sẽ đảm bảo tập dữ liệu không trở thành nút cổ chai trong khi đào tạo mô hình của bạn. Nếu tập dữ liệu của bạn quá lớn để vừa với bộ nhớ, bạn cũng có thể sử dụng phương pháp này để tạo bộ đệm ẩn trên đĩa có hiệu suất cao
  • import pathlib
    dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
    data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                       fname='flower_photos',
                                       untar=True]
    data_dir = pathlib.Path[data_dir]
    
    05 chồng chéo quá trình tiền xử lý dữ liệu và thực thi mô hình trong khi đào tạo

Độc giả quan tâm có thể tìm hiểu thêm về cả hai phương pháp, cũng như cách lưu trữ dữ liệu vào đĩa trong phần Tìm nạp trước của Hiệu suất tốt hơn với tf. hướng dẫn API dữ liệu

print[tf.__version__]
3

Đào tạo người mẫu

Để đầy đủ, bạn sẽ trình bày cách đào tạo một mô hình đơn giản bằng cách sử dụng bộ dữ liệu bạn vừa chuẩn bị

Mô hình Tuần tự bao gồm ba khối tích chập [

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
06] với lớp tổng hợp tối đa [
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
07] trong mỗi khối. Có một lớp được kết nối đầy đủ [
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
08] với 128 đơn vị trên cùng được kích hoạt bởi chức năng kích hoạt ReLU [
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
09]. Mô hình này chưa được điều chỉnh theo bất kỳ cách nào—mục tiêu là hiển thị cho bạn cơ chế sử dụng bộ dữ liệu bạn vừa tạo. Để tìm hiểu thêm về phân loại hình ảnh, hãy truy cập Hướng dẫn phân loại hình ảnh

print[tf.__version__]
4

Chọn trình tối ưu hóa

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
10 và hàm mất mát
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
11. Để xem độ chính xác của quá trình đào tạo và xác thực cho từng giai đoạn đào tạo, hãy chuyển đối số
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
12 cho
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
13

______55Ghi chú. Bạn sẽ chỉ luyện tập trong một vài kỷ nguyên nên phần hướng dẫn này sẽ diễn ra nhanh chóng. ______56____57Lưu ý. Bạn cũng có thể viết vòng lặp huấn luyện tùy chỉnh thay vì sử dụng
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
14. Để tìm hiểu thêm, hãy truy cập phần hướng dẫn Viết vòng lặp đào tạo từ đầu.

Bạn có thể nhận thấy độ chính xác xác thực thấp so với độ chính xác đào tạo, cho thấy mô hình của bạn đang bị quá khớp. Bạn có thể tìm hiểu thêm về overfitting và cách giảm nó trong hướng dẫn này

sử dụng tf. dữ liệu để kiểm soát tốt hơn

Tiện ích tiền xử lý Keras ở trên—

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
3—là một cách thuận tiện để tạo một
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
16 từ một thư mục hình ảnh

Để kiểm soát hạt mịn hơn, bạn có thể viết đường dẫn đầu vào của riêng mình bằng cách sử dụng

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
17. Phần này cho biết cách thực hiện việc đó, bắt đầu bằng đường dẫn tệp từ tệp TGZ mà bạn đã tải xuống trước đó

print[tf.__version__]
8
print[tf.__version__]
9
2.11.0
0

Cấu trúc cây của các tệp có thể được sử dụng để biên dịch danh sách

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
6

2.11.0
1
2.11.0
2

Chia tập dữ liệu thành tập huấn luyện và xác nhận

2.11.0
3

Bạn có thể in độ dài của mỗi tập dữ liệu như sau

2.11.0
4
2.11.0
5

Viết hàm ngắn chuyển đổi đường dẫn tệp thành cặp

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
19

2.11.0
6
2.11.0
7
2.11.0
8

Sử dụng

image_count = len[list[data_dir.glob['*/*.jpg']]]
print[image_count]
8 để tạo tập dữ liệu gồm các cặp
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
21

2.11.0
9
flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
0
flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
1

Định cấu hình tập dữ liệu cho hiệu suất

Để đào tạo một mô hình với tập dữ liệu này, bạn sẽ muốn dữ liệu

  • Để được xáo trộn tốt
  • để được theo lô
  • Hàng loạt sẽ có sẵn càng sớm càng tốt

Có thể thêm các tính năng này bằng cách sử dụng API

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
17. Để biết thêm chi tiết, hãy truy cập hướng dẫn Hiệu suất đường ống đầu vào

flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
2

Trực quan hóa dữ liệu

Bạn có thể hình dung tập dữ liệu này tương tự như tập dữ liệu bạn đã tạo trước đây

flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
3____74

Tiếp tục đào tạo mô hình

Bây giờ bạn đã tạo thủ công một

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
16 tương tự với cái được tạo bởi
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
3 ở trên. Bạn có thể tiếp tục đào tạo mô hình với nó. Như trước đây, bạn sẽ chỉ luyện tập trong một vài kỷ nguyên để giữ cho thời gian chạy ngắn

print[tf.__version__]
6
flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
6

Sử dụng bộ dữ liệu TensorFlow

Cho đến nay, hướng dẫn này đã tập trung vào tải dữ liệu ra khỏi đĩa. Bạn cũng có thể tìm thấy một bộ dữ liệu để sử dụng bằng cách khám phá danh mục lớn các bộ dữ liệu dễ tải xuống tại Bộ dữ liệu TensorFlow

Vì trước đây bạn đã tải bộ dữ liệu Flowers ra khỏi đĩa, bây giờ hãy nhập nó bằng Bộ dữ liệu TensorFlow

Tải xuống bộ dữ liệu Flowers bằng Bộ dữ liệu TensorFlow

flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
7

Bộ dữ liệu hoa có năm lớp

flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
8
flowers_photos/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/
9

Lấy một hình ảnh từ tập dữ liệu

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
0
import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
1

Như trước đây, hãy nhớ gộp, xáo trộn và định cấu hình các bộ đào tạo, xác thực và kiểm tra để đạt được hiệu suất

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
2

Bạn có thể tìm thấy một ví dụ hoàn chỉnh về cách làm việc với bộ dữ liệu Flowers và bộ dữ liệu TensorFlow bằng cách truy cập hướng dẫn Tăng cường dữ liệu

Bước tiếp theo

Hướng dẫn này cho thấy hai cách tải hình ảnh ra khỏi đĩa. Đầu tiên, bạn đã học cách tải và xử lý trước tập dữ liệu hình ảnh bằng cách sử dụng các lớp và tiện ích tiền xử lý Keras. Tiếp theo, bạn đã học cách viết một quy trình nhập liệu từ đầu bằng cách sử dụng

import pathlib
dataset_url = "//storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file[origin=dataset_url,
                                   fname='flower_photos',
                                   untar=True]
data_dir = pathlib.Path[data_dir]
17. Cuối cùng, bạn đã học cách tải xuống tập dữ liệu từ Bộ dữ liệu TensorFlow

Chủ Đề