import cv2
#正则匹配使用:
import re
import os
#此库用于拷贝,删除,移动,复制以及解压缩
import shutil
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import resnet
from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Input,GlobalAvgPool2D,Dense,Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint,TensorBoard,EarlyStopping
import matplotlib.pyplot as plt
dataAll=ImageDataGenerator(validation_split=0.3)
genIteratorForTrain=dataAll.flow_from_directory("D:/17flowers",batch_size=32,subset="training",class_mode='sparse')
#(32, 256, 256, 3)
print(genIteratorForTrain.next()[0].shape)
#(32,)
print(genIteratorForTrain.next()[1].shape)
#提取出所需的图片和label
#嵌套一层生成器:
#使用tf.data.Dataset不能使用无限生成器:
def Gen():
for index,data in enumerate(genIteratorForTrain):
#每批32的情况下,输出的批数,数量
if index>genIteratorForTrain.__len__():
return
else:
print(index)
yield data
#输出图片的总数量
Gen()
print(genIteratorForTrain.n)
ds = tf.data.Dataset.from_generator(
Gen,
output_types=(tf.float32, tf.float32),
#批次大小用None替代,自动推算,或是不出现,自动适配
#output_shapes = ([None, 256, 256, 3],[None])
)
def mapfun(obj1,obj2):
return obj1,obj2
#最大可用线程设置,拉满算力:
ds.map(mapfun,num_parallel_calls=tf.data.experimental.AUTOTUNE)
#(32, 256, 256, 3)批次自动按ImageDataGenerator进行设置
print(next(iter(ds))[0].shape)
#(32,)
print(next(iter(ds))[1].shape)
#17花朵分类的输入形状为(256, 256, 3)
shape_in=genIteratorForTrain.next()[0].shape[1:]
model=Dense(100)
#定义一个输入层:
inputs=Input(shape=shape_in)
#此处x形状为(None, 7, 7, 2048)
x=model(inputs)
#此处x形状为(None, 2048)
x=GlobalAvgPool2D()(x)
#此处x形状为(None, 2048)
x=Dense(units=17)(x)
#此处x形状为(None, 2048)
x=Dropout(rate=0.5)(x)
model2=Model(inputs = inputs, outputs = x)
model2.compile(optimizer=Adam(lr=0.001),loss=SparseCategoricalCrossentropy(from_logits=True),metrics=['sparse_categorical_accuracy'])
history=model2.fit(x=ds,epochs=10,batch_size=32)
在训练模型时,第一轮的训练输出日志中显示的训练进度为 “ 44/Unknown ”, 后面的训练轮次中就可以看到正常的进度显示,