Keras实现mode.fit和model.fit_generator比较

本文对比了Keras中model.fit_generator和model.fit两种训练模型的方法。在模型部分,以AlexNet为例,用于二分类任务并调整了网络结构以适应256*256的输入图像。model.fit_generator由于使用生成器,节省内存,但不支持某些callback。而model.fit则提供了详细的使用指南,需要输入numpy数组。
摘要由CSDN通过智能技术生成

模型部分

模型部分都一样,比如我这里使用AlexNet网络来做。我做的是一个二分类任务,所以结尾部分网络有改动。输入图片尺寸是256*256的,所以输出图片尺寸有一点改动。

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, BatchNormalization
import os

# AlexNet
model = Sequential()
#第一段
model.add(Conv2D(filters=96, kernel_size=(11,11),
                 strides=(4,4), padding='valid',
                 input_shape=(256, 256, 3),
                 activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3,3), 
                       strides=(2,2), 
                       padding='valid'))
#第二段
model.add(Conv2D(filters=256, kernel_size
import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator # 设置训练集和验证集的路径 train_dir = 'path/to/train/directory' validation_dir = 'path/to/validation/directory' # 定义数据生成器 train_datagen = ImageDataGenerator(rescale=1./255) validation_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( train_dir, target_size=(150, 150), batch_size=32, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( validation_dir, target_size=(150, 150), batch_size=32, class_mode='categorical') # 构建卷积神经网络模型 model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(64, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(128, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(128, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dense(5, activation='softmax') ]) # 编译模型 model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.RMSprop(lr=1e-4), metrics=['acc']) # 训练模型 history = model.fit( train_generator, steps_per_epoch=train_generator.samples/train_generator.batch_size, epochs=30, validation_data=validation_generator, validation_steps=validation_generator.samples/validation_generator.batch_size, verbose=2) # 保存模型 model.save('flower_classification.h5')给这个代码添加SeNet
06-13
好的,我来为你添加SeNet。 首先,导入必要的库: ```python import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.layers import * from tensorflow.keras.models import Model ``` 然后,定义数据生成器: ```python train_datagen = ImageDataGenerator(rescale=1./255) validation_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( train_dir, target_size=(150, 150), batch_size=32, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( validation_dir, target_size=(150, 150), batch_size=32, class_mode='categorical') ``` 接下来,我们定义SeNet模型: ```python def SENet(input_shape, classes): input_tensor = Input(shape=input_shape) x = Conv2D(64, (3,3), padding='same', activation='relu')(input_tensor) x = Conv2D(64, (3,3), padding='same', activation='relu')(x) x = Conv2D(128, (3,3), padding='same', activation='relu')(x) x = MaxPooling2D(pool_size=(2,2))(x) x = Conv2D(256, (3,3), padding='same', activation='relu')(x) x = Conv2D(256, (3,3), padding='same', activation='relu')(x) x = Conv2D(512, (3,3), padding='same', activation='relu')(x) x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) x = Dense(classes, activation='softmax')(x) model = Model(inputs=input_tensor, outputs=x) return model ``` 然后,我们为SeNet模型编译优化器和损失函数: ```python model = SENet(input_shape=(150,150,3), classes=5) model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.RMSprop(lr=1e-4), metrics=['acc']) ``` 最后,我们训练模型,保存模型: ```python history = model.fit(train_generator, steps_per_epoch=train_generator.samples/train_generator.batch_size, epochs=30, validation_data=validation_generator, validation_steps=validation_generator.samples/validation_generator.batch_size, verbose=2) model.save('flower_classification_with_se.h5') ``` 这样,我们就成功地为这个代码添加了SeNet。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Einstellung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值