Transfer learning locally using ipython notebook: error in weight path

Hi,

I’m trying to train the model locally by using ipython notebook downloaded from the project. But i’m facing an error in the weights path.


ValueError Traceback (most recent call last)
C:\Users\MLTEAM~1\AppData\Local\Temp/ipykernel_8328/2519843456.py in
12 INPUT_SHAPE = (32, 32, 3)
13
—> 14 base_model = tf.keras.applications.MobileNetV2(
15 input_shape = INPUT_SHAPE, alpha=0.35,
16 weights = WEIGHTS_PATH

C:\Program Files\Python39\lib\site-packages\keras\applications\mobilenet_v2.py in MobileNetV2(input_shape, alpha, include_top, weights, input_tensor, pooling, classes, classifier_activation, **kwargs)
190 raise ValueError(f’Unknown argument(s): {kwargs}’)
191 if not (weights in {‘imagenet’, None} or tf.io.gfile.exists(weights)):
–> 192 raise ValueError('The weights argument should be either ’
193 ’None (random initialization), imagenet
194 '(pre-training on ImageNet), ’

ValueError: The weights argument should be either None (random initialization), imagenet (pre-training on ImageNet), or the path to the weights file to be loaded. Received `weights=./transfer-learning-weights/keras/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.35_96.h5

Weights path is mentioned as below

WEIGHTS_PATH = './transfer-learning-weights/keras/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.35_96.h5'

I understand that the weights file is not present locally. Is there any way to download that weights file from edge impulse?

Thanks and Regards,
Ramson Jehu K

Hello @Ramson,

You can download the weights from this url: https://cdn.edgeimpulse.com/transfer-learning-weights/keras/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.35_96.h5
See also @janjongboom comment’s on this thread if you want to download other weights from different models: Keras pretrain weight

Regards,

Louis

2 Likes

hello louis,

Thank you so much for the url. that solved my issue.

But Now i’m facing a different issue with the data augmentation.

ValueError: in user code:
File "C:\Users\MLTEAM~1\AppData\Local\Temp/ipykernel_2912/2041261749.py", line 41, in augment_image  *
        image = tf.image.random_flip_left_right(image)
ValueError: 'image' (shape (3072,)) must be at least three-dimensional.

This is the code snippet im running

import math

from pathlib import Path

import tensorflow as tf

from tensorflow.keras import Model

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, InputLayer, Dropout, Conv1D, Flatten, Reshape, MaxPooling1D, BatchNormalization, Conv2D, GlobalMaxPooling2D, Lambda

from tensorflow.keras.optimizers import Adam, Adadelta

from tensorflow.keras.losses import categorical_crossentropy

WEIGHTS_PATH = 'mobilenet_v2_weights_tf_dim_ordering_tf_kernels_0.35_96.h5'

INPUT_SHAPE = (32, 32, 3)

base_model = tf.keras.applications.MobileNetV2(

    input_shape = INPUT_SHAPE, alpha=0.35,

    weights = WEIGHTS_PATH

)

base_model.trainable = False

model = Sequential()

model.add(InputLayer(input_shape=INPUT_SHAPE, name='x_input'))

# Don't include the base model's top layers

last_layer_index = -3

model.add(Model(inputs=base_model.inputs, outputs=base_model.layers[last_layer_index].output))

model.add(Dense(16, activation='relu'))

model.add(Dropout(0.1))

model.add(Flatten())

model.add(Dense(classes, activation='softmax'))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),

                loss='categorical_crossentropy',

                metrics=['accuracy'])

# Implements the data augmentation policy

def augment_image(image, label):

    # Flips the image randomly

    image = tf.image.random_flip_left_right(image)

    # Increase the image size, then randomly crop it down to

    # the original dimensions

    resize_factor = random.uniform(1, 1.2)

    new_height = math.floor(resize_factor * INPUT_SHAPE[0])

    new_width = math.floor(resize_factor * INPUT_SHAPE[1])

    image = tf.image.resize_with_crop_or_pad(image, new_height, new_width)

    image = tf.image.random_crop(image, size=INPUT_SHAPE)

    # Vary the brightness of the image

    image = tf.image.random_brightness(image, max_delta=0.2)

    return image, label

train_dataset = train_dataset.map(augment_image, tf.data.experimental.AUTOTUNE)

BATCH_SIZE = 32

train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=False)

validation_dataset = validation_dataset.batch(BATCH_SIZE, drop_remainder=False)

model.fit(train_dataset, validation_data=validation_dataset, epochs=20, verbose=2, callbacks=callbacks)

print('')

print('Initial training done.', flush=True)

# How many epochs we will fine tune the model

FINE_TUNE_EPOCHS = 10

# What percentage of the base model's layers we will fine tune

FINE_TUNE_PERCENTAGE = 65

print('Fine-tuning best model for {} epochs...'.format(FINE_TUNE_EPOCHS), flush=True)

# Determine which layer to begin fine tuning at

model_layer_count = len(model.layers)

fine_tune_from = math.ceil(model_layer_count * ((100 - FINE_TUNE_PERCENTAGE) / 100))

# Allow the entire base model to be trained

model.trainable = True

# Freeze all the layers before the 'fine_tune_from' layer

for layer in model.layers[:fine_tune_from]:

    layer.trainable = False

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.000045),

                loss='categorical_crossentropy',

                metrics=['accuracy'])

model.fit(train_dataset,

                epochs=FINE_TUNE_EPOCHS,

                verbose=2,

                validation_data=validation_dataset,

                callbacks=callbacks,

                class_weight=None

            )

I guess it has something to do with reshape. But im not sure what to do. Please guide

Thanks and Regards,
Ramson Jehu K

Hi,

Just an update on the last comment.

I reshaped the training and test dataset from 2d to 4d which kinda worked out for me.

X_train = X_train.reshape(X_train.shape[0],32,32,3)
X_test = X_test.reshape(X_test.shape[0],32,32,3)

Thanks,
Ramson Jehu K

1 Like

Hello @Ramson,

Thanks for letting the community know how you fixed your issue.

Regards,

Louis

1 Like