SoFunction
Updated on 2024-11-15

The keras import weights approach

The function to load weights is defined in the keras source code engine:

load_weights(self, filepath, by_name=False)

The default by_name is False, which loads the weights according to the topology of the network, and is suitable for directly using the network model that comes with keras, such as VGG16.

VGG19/resnet50, etc., the source code is described as follows.

If `by_name` is False (default) weights are loaded
based on the network's topology, meaning the architecture
should be the same as when the weights were saved.
Note that layers that don't have weights are not taken
into account in the topological ordering, so adding or
removing layers is fine as long as they don't have weights.

If you change by_name to True, then load the weights according to the name of the layer, and load the weights when the name of the layer is the same, it is suitable to use for the changed

The relevant structure of the model or used in cases where nodes are added but the main structure of the original network is utilized, the source code is described below:

If `by_name` is True, weights are loaded into layers
only if they share the same name. This is useful
for fine-tuning or transfer-learning models where
some of the layers have changed.

When performing edge detection, the main structure of the VGG network is utilized, and an inverse convolutional layer is added to the network, at which point the loading weights should use the

model.load_weights(filepath,by_name=True)

Additional knowledge:Implementation of mnist handwritten numbers under Keras

I've been using tensorflow before, and was recommended to use keras by my classmates, so I used the mnist handwritten numeric dataset in the previous document to practice.

The code is as follows.

import struct
import numpy as np
import os
 
import keras
from  import Sequential 
from  import Dense
from  import SGD
 
def load_mnist(path, kind):
  labels_path = (path, '%s-labels.idx1-ubyte' % kind)
  images_path = (path, '%s-images.idx3-ubyte' % kind)
  with open(labels_path, 'rb') as lbpath:
    magic, n = ('>II', (8))
    labels = (lbpath, dtype=np.uint8)
  with open(images_path, 'rb') as imgpath:
    magic, num, rows, cols = (">IIII", (16))
    images = (imgpath, dtype=np.uint8).reshape(len(labels), 784) #28*28=784
  return images, labels
 
#loading train and test data
X_train, Y_train = load_mnist('.\\data', kind='train')
X_test, Y_test = load_mnist('.\\data', kind='t10k')
 
#turn labels to one_hot code
Y_train_ohe = .to_categorical(Y_train, num_classes=10)
 
#define models
model = Sequential()
 
(Dense(input_dim=X_train.shape[1],output_dim=50,init='uniform',activation='tanh'))
(Dense(input_dim=50,output_dim=50,init='uniform',activation='tanh'))
(Dense(input_dim=50,output_dim=Y_train_ohe.shape[1],init='uniform',activation='softmax')) 
 
sgd = SGD(lr=0.001, decay=1e-7, momentum=0.9, nesterov=True)
(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"])
 
#start training
(X_train,Y_train_ohe,epochs=50,batch_size=300,shuffle=True,verbose=1,validation_split=0.3)
 
#count accuracy
y_train_pred = model.predict_classes(X_train, verbose=0)
 
train_acc = (Y_train == y_train_pred, axis=0) / X_train.shape[0] 
print('Training accuracy: %.2f%%' % (train_acc * 100))
 
y_test_pred = model.predict_classes(X_test, verbose=0)
test_acc = (Y_test == y_test_pred, axis=0) / X_test.shape[0] 
print('Test accuracy: %.2f%%' % (test_acc * 100))

The training results are as follows:

Epoch 45/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2174 - acc: 0.9380 - val_loss: 0.2341 - val_acc: 0.9323
Epoch 46/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2061 - acc: 0.9404 - val_loss: 0.2244 - val_acc: 0.9358
Epoch 47/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.1994 - acc: 0.9413 - val_loss: 0.2295 - val_acc: 0.9347
Epoch 48/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2003 - acc: 0.9413 - val_loss: 0.2224 - val_acc: 0.9350
Epoch 49/50
42000/42000 [==============================] - 1s 18us/step - loss: 0.2013 - acc: 0.9417 - val_loss: 0.2248 - val_acc: 0.9359
Epoch 50/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.1960 - acc: 0.9433 - val_loss: 0.2300 - val_acc: 0.9346
Training accuracy: 94.11%
Test accuracy: 93.61%

This above keras import weights way is all I have shared with you.