本实例使用的数据集是3700张,包含5个种类的花。
flower_photo:daisy(雏菊)/dandelion(蒲公英)/roses(玫瑰)/sunflower(向日葵)/tulips(郁金香)
import matplotlib.pyplot as plt import numpy as np import os import PIL # Python Imaging Library import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.models import Sequential 123456789
加载数据,并检测数据是否可用。
import pathlib dataset_url = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz' # tf.keras.utils.get_file函数的三个参数 文件名,文件路径,是否需要解压缩。 data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True) # 创建path对象 data_dir = pathlib.Path(data_dir) # 检查数据是否可用 ,data_dir.glob获取路径下的所有符合'*/*.jpg'的文件,返回一个generator image_count = len(list(data_dir.glob('*/*.jpg'))) # print(image_count) # 检测roses的数据是否正常 roses = list(data_dir.glob('roses/*')) roses0 = PIL.Image.open(str(roses[0])) roses1 = PIL.Image.open(str(roses[100])) daisy = list(data_dir.glob('daisy/*')) daisy0 = PIL.Image.open(str(daisy[0])) daisy1 = PIL.Image.open(str(daisy[100])) dandelion = list(data_dir.glob('dandelion/*')) dandelion0 = PIL.Image.open(str(dandelion[0])) dandelion1 = PIL.Image.open(str(dandelion[100]))
123456789101112131415161718192021222324对加载器定义一些变量。
batch_size = 32 img_height = 180 img_width = 180 123
划分训练集和测试集
train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset='training', seed=123, image_size=(img_height, img_width), batch_size=batch_size ) val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset='validation', seed=123, image_size=(img_height, img_width), batch_size=batch_size ) AUTOTUNE = tf.data.AUTOTUNE train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
123456789101112131415161718192021标准化输入数据
normalization_layer = layers.experimental.preprocessing.Rescaling(1. / 255) normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y)) image_batch, labels_batch = next(iter(normalized_ds)) first_image = image_batch[0] 123456
创建模型
num_classes = 5 model = Sequential([ layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=(img_height, img_width, 3)), layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(num_classes) ]) 1234567891011121314
编译模型
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) 123
训练模型
epochs = 10 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs ) 123456
评估训练的结果,并绘图。
acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs_range = range(epochs) plt.figure(figsize=(8,8)) plt.subplot(2,1,1) plt.plot(epochs_range,acc,label = 'Training Accuracy') plt.plot(epochs_range,val_acc,label='Validation Accuracy') plt.legend(loc='lower right') plt.title('Training and Validation Accuracy') plt.subplot(2,1,2) plt.plot(epochs_range,loss,label = 'Training Loss') plt.plot(epochs_range,val_loss,label='Validation Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show()
12345678910111213141516171819202122相关知识
图像识别算法有哪些
什么是图像识别
图像识别
植物花卉识别 小花儿人工智能
基于图像处理的花卉识别技术的研究与实现
Python实现识别花卉种类的示例代码
花卉识别下载
百度接口实现花卉识别
小黄养花APP下载,小黄养花植物识别APP官方版 v1.1
拥抱AI的云南花卉行业,如虎添翼!
网址: 图像识别 https://m.huajiangbk.com/newsview110569.html
上一篇: 向日葵(向日葵的花序、花粉和授粉 |
下一篇: 向日葵的象征意义及启示(生命力与 |