1
2
3
4
$ git clone https://github.com/ScrapCodes/image-recognition-tensorflow.git
$ cd image-recognition-tensorflow
$ python
>>>
導入 TensorFlow、Keras 和其他助手庫我使用 TensorFlow 和 Keras 運行機器學習,使用 Pillow Python 庫進行圖像處理。
通過使用 pip,可以如下所示將這些項安裝在 macOS 上:
1
sudo pip install tensorflow matplotlib pillow
注意:是否需要使用 sudo 取決於如何在系統上安裝 Python 和 pip。配置了虛擬環境的系統可能不需要 sudo。
導入 Python 庫。
1
2
3
4
5
6
7
8
9
10
11
12
13
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import glob, os
import re
# Pillow
import PIL
from PIL import Image
加載數據用於預處理輸入圖像的 Python 函數。要將圖像轉換為 numpy 數組,它們必須具有相同的尺寸:
1
2
3
4
5
6
7
8
9
10
11
12
13
# Use Pillow library to convert an input jpeg to a 8 bit grey scale image array for processing.
def jpeg_to_8_bit_greyscale(path, maxsize):
img = Image.open(path).convert('L') # convert image to 8-bit grayscale
# Make aspect ratio as 1:1, by applying image crop.
# Please note, croping works for this data set, but in general one
# needs to locate the subject and then crop or scale accordingly.
WIDTH, HEIGHT = img.size
if WIDTH != HEIGHT:
m_min_d = min(WIDTH, HEIGHT)
img = img.crop((0, 0, m_min_d, m_min_d))
# Scale the image to the requested maxsize by Anti-alias sampling.
img.thumbnail(maxsize, PIL.Image.ANTIALIAS)
return np.asarray(img)
用於將數據集從圖像加載到 numpy 數組中的 Python 函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
def load_image_dataset(path_dir, maxsize):
images = []
labels = []
os.chdir(path_dir)
for file in glob.glob("*.jpg"):
img = jpeg_to_8_bit_greyscale(file, maxsize)
if re.match('chihuahua.*', file):
images.append(img)
labels.append(0)
elif re.match('muffin.*', file):
images.append(img)
labels.append(1)
return (np.asarray(images), np.asarray(labels))
我們應該將圖像縮放到比實際圖像解析度小的標準尺寸。這些圖像超過了 170×170,因此我們將它們全部縮小到 100×100 以進一步處理:
要加載數據,可執行以下函數並加載訓練和測試數據集:
1
2
3
(train_images, train_labels) = load_image_dataset('/Users/yourself/image-recognition-tensorflow/chihuahua-muffin', maxsize)
(test_images, test_labels) = load_image_dataset('/Users/yourself/image-recognition-tensorflow/chihuahua-muffin/test_set', maxsize)
最後,我們定義數據集的類名。由於此數據只有兩個類(圖像可以是 Chihuahua 或 Muffin),因此我們具有的 class_names 如下所示:
1
class_names = ['chihuahua', 'muffin']