我正在尝试使用迁移学习和数据增强来训练模型。我的图像数据为32 x 32 x 3,我希望导入EfficientNet07,但每次运行
from tensorflow.keras.applications import EfficientNetB0
model = EfficientNetB0(weights='imagenet')
我收到错误消息:
ImportError: cannot import name 'EfficientNetB7' from 'tensorflow.keras.applications' (C:\Users\…..
我不情愿地恢复到resnet50,它工作得很好。但我真的希望与efficientnets一起工作。
其次,我希望在模型运行时使用keras.expermental.preprocessing层来增加数据,但是我得到了错误
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
img_augmentation = Sequential(
[
preprocessing.RandomRotation(factor=0.15),
preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1),
preprocessing.RandomFlip(),
preprocessing.RandomContrast(factor=0.1),
],
name="img_augmentation",
)
AttributeError: module 'tensorflow.keras.layers.experimental.preprocessing' has no attribute 'RandomRotation'
我从https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/中提取了这些代码
我已经从头到尾读了三遍。我继续使用ImageDataGenerator并编写了以下代码
def choosen_args(args):
#this function is to randomly choose the augmentation techniques from the list available in keras
key_list = []
arg_list = list(args.keys())
for i in range(len(arg_list)):
if np.random.randint(2) == 0:
pass
else:
key_list.append(arg_list[i])
arg_dict = dict((k, args[k]) for k in key_list)
return arg_dict
这是我正在使用的所有增强功能的列表-但只有少数会被选中进行最终转换
args = dict(
rotation_range = 120,
width_shift_range=0.4,
height_shift_range=0.4,
shear_range=0.2,
zoom_range=0.2,
fill_mode ='nearest',
zca_whitening=True,
zca_epsilon=1e-06,
brightness_range=(9.0, 46.0),
channel_shift_range=60.0,
horizontal_flip=True,
vertical_flip=True,
rescale=random.randint(1,11))
data_augmentation_train = ImageDataGenerator( choosen_args(args) )
data_augmentation_validation = ImageDataGenerator( choosen_args(args) )
data_augmentation_train.fit(x_train, augment = True)
data_augmentation_validation.fit(x_test, augment = True)
请帮助:
1.如何获得有效的net07并利用其权重进行迁移学习
1.我所使用的数据增强代码-如何可视化它的功能-即转换图像
1.是否有更简单的方法来进行数据扩充-如何修复导入和属性错误
1.在我把我的数据放进生成器之后-我如何把它和我的迁移学习代码的其余部分一起使用。
基本模型= tf.keras.applications.ResNet50(权重="图像网",
include_top = False, input_shape = input_size)
> x_train = tf.keras.applications.resnet50.preprocess_input(x_train)
> x_test = tf.keras.applications.resnet50.preprocess_input(x_test)
> base_model.trainable = False
inputs = keras.Input(shape=(32, 32, 3))
model = base_model_ResNet50(inputs, training=False)
model = keras.layers.GlobalAveragePooling2D()(model )
model= keras.layers.Flatten()(model)
model = keras.layers.Dropout(0.2)(model)
output = layers.Dense(100, activation="softmax")(model)
history = model.fit(data_augmentation_train.flow (x_train, labels_train, batch_size = 64), validation_data =
数据_增强_验证.流(x_测试,标签_测试))
谢谢
3条答案
按热度按时间7y4bm7vi1#
尝试导入为:
nue99wik2#
首先安装efficientnet模块:
然后将其导入为:
创建模型:
l7wslrjt3#
第一个月
Options to replace EfficientNetB0 are listed here: https://keras.io/api/applications/#usage-examples-for-image-classification-models
例如
model = tf.keras.applications.EfficientNetB7()
或model = tf.keras.applications.EfficientNetV2B3()
(FYI您可以更改图像输入的大小)
希望这能帮上忙