I am trying to train a model using transfer learning with data augmentation. My image data is 32 x 32 x 3 and I want to import EfficientNet07, but every time I run
from tensorflow.keras.applications import EfficientNetB0
model = EfficientNetB0(weights='imagenet')
I get error message:
ImportError: cannot import name 'EfficientNetB7' from 'tensorflow.keras.applications' (C:\Users\…..
I unwillingly reverted to Resnet50, that works fine. But I really wish to work with efficientnets.
Secondly, I was hoping to use keras.expermental.preprocessing layers to add data augmentation while the model runs, but I get error
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'
I took all this code from https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/
and have read it top to bottom thrice. I went on to use ImageDataGenerator and wrote this code
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
#this is the list of all augmentation I am using - but only few will get selected for final transformation
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)
Kindly help with:
- How to get efficientnet07 and use its weights for transfer learninig
- The data augmentation code I have used - how do I visualize what it does - that is, transforms my images
- Is there an easier way to do data augmentation - how do I fix the Import and attribute errors
- After I fit my data in generator - how do I use it with rest of my transfer learning code.
base_model = tf.keras.applications.ResNet50(weights='imagenet',
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 =data_augmentation_validation.flow(x_test,labels_test))
Thank you