网站建设长尾关键词,网站地址ip域名查询,百度搜索推广是什么,seo教学最后一节作业是水果分类的任务#xff0c;一共6类#xff0c;使用之前学习的知识在代码段上进行填空。 加载ImageNet预训练的基础模型
from tensorflow import kerasbase_model keras.applications.VGG16(weightsimagenet,input_shape(224, 224, 3),include_t…最后一节作业是水果分类的任务一共6类使用之前学习的知识在代码段上进行填空。 加载ImageNet预训练的基础模型
from tensorflow import kerasbase_model keras.applications.VGG16(weightsimagenet,input_shape(224, 224, 3),include_topFalse)
冻结基础模型
# Freeze base model
base_model.trainable False向模型添加新层
# Create inputs with correct shape
inputs keras.Input(shape(224, 224, 3))x base_model(inputs, trainingFalse)# Add pooling layer or flatten layer
x keras.layers.GlobalAveragePooling2D()(x)# Add final dense layer
outputs keras.layers.Dense(6, activation softmax)(x)# Combine inputs and outputs to create model
model keras.Model(inputs, outputs)model.summary()编译模型
model.compile(losscategorical_crossentropy, optimizeradam, metrics[accuracy])扩充数据
from tensorflow.keras.preprocessing.image import ImageDataGeneratordatagen_train ImageDataGenerator(featurewise_centerTrue, # set input mean to 0 over the datasetsamplewise_centerTrue, # set each sample mean to 0rotation_range10, # randomly rotate images in the range (degrees, 0 to 180)zoom_range 0.1, # Randomly zoom image width_shift_range0.1, # randomly shift images horizontally (fraction of total width)height_shift_range0.1, # randomly shift images vertically (fraction of total height)horizontal_flipTrue, # randomly flip imagesvertical_flipFalse)
datagen_valid ImageDataGenerator(featurewise_centerTrue, # set input mean to 0 over the datasetsamplewise_centerTrue, # set each sample mean to 0rotation_range10, # randomly rotate images in the range (degrees, 0 to 180)zoom_range 0.1, # Randomly zoom image width_shift_range0.1, # randomly shift images horizontally (fraction of total width)height_shift_range0.1, # randomly shift images vertically (fraction of total height)horizontal_flipTrue, # randomly flip imagesvertical_flipFalse)加载数据集
# load and iterate training dataset
train_it datagen_train.flow_from_directory(data/fruits/train, target_size(224, 224), color_modergb,class_modecategorical,
)# load and iterate validation dataset
valid_it datagen_valid.flow_from_directory(data/fruits/valid, target_size(224, 224), color_modergb,class_modecategorical,
)训练模型 现在开始训练模型将训练和测试数据集传递给fit函数并设置所需的训练次数epochs model.fit(train_it,validation_datavalid_it,steps_per_epochtrain_it.samples/train_it.batch_size,validation_stepsvalid_it.samples/valid_it.batch_size,epochs10)其实到这里已经满足了评估需求达到了92%以上的准确率 所以可以不进行微调的部分直接运行后边的代码 就可以生成证书了