Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transfer Learning using Keras
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras import backend as k
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
img_width, img_height = 256, 256
### Build the network
img_input = Input(shape=(256, 256, 3))
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
model = Model(input = img_input, output = x)
model.summary()
"""
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 256, 256, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 256, 256, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 256, 256, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 128, 128, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 128, 128, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 128, 128, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 64, 64, 128) 0
=================================================================
Total params: 260,160.0
Trainable params: 260,160.0
Non-trainable params: 0.0
"""
layer_dict = dict([(layer.name, layer) for layer in model.layers])
[layer.name for layer in model.layers]
"""
['input_1',
'block1_conv1',
'block1_conv2',
'block1_pool',
'block2_conv1',
'block2_conv2',
'block2_pool']
"""
import h5py
weights_path = 'vgg19_weights.h5' # ('https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels.h5)
f = h5py.File(weights_path)
list(f["model_weights"].keys())
"""
['block1_conv1',
'block1_conv2',
'block1_pool',
'block2_conv1',
'block2_conv2',
'block2_pool',
'block3_conv1',
'block3_conv2',
'block3_conv3',
'block3_conv4',
'block3_pool',
'block4_conv1',
'block4_conv2',
'block4_conv3',
'block4_conv4',
'block4_pool',
'block5_conv1',
'block5_conv2',
'block5_conv3',
'block5_conv4',
'block5_pool',
'dense_1',
'dense_2',
'dense_3',
'dropout_1',
'global_average_pooling2d_1',
'input_1']
"""
# list all the layer names which are in the model.
layer_names = [layer.name for layer in model.layers]
"""
# Here we are extracting model_weights for each and every layer from the .h5 file
>>> f["model_weights"]["block1_conv1"].attrs["weight_names"]
array([b'block1_conv1/kernel:0', b'block1_conv1/bias:0'],
dtype='|S21')
# we are assiging this array to weight_names below
>>> f["model_weights"]["block1_conv1"]["block1_conv1/kernel:0]
<HDF5 dataset "kernel:0": shape (3, 3, 3, 64), type "<f4">
# The list comprehension (weights) stores these two weights and bias of both the layers
>>>layer_names.index("block1_conv1")
1
>>> model.layers[1].set_weights(weights)
# This will set the weights for that particular layer.
With a for loop we can set_weights for the entire network.
"""
for i in layer_dict.keys():
weight_names = f["model_weights"][i].attrs["weight_names"]
weights = [f["model_weights"][i][j] for j in weight_names]
index = layer_names.index(i)
model.layers[index].set_weights(weights)
import cv2
import numpy as np
import pandas as pd
from tqdm import tqdm
import itertools
import glob
features = []
for i in tqdm(files_location):
im = cv2.imread(i)
im = cv2.resize(cv2.cvtColor(im, cv2.COLOR_BGR2RGB), (256, 256)).astype(np.float32) / 255.0
im = np.expand_dims(im, axis =0)
outcome = model_final.predict(im)
features.append(outcome)
## collect these features and create a dataframe and train a classfier on top of it.
@sumedhpendurkar
Copy link

sumedhpendurkar commented Jun 25, 2017

list(f["model_weights"].keys())

At this line I get a error.

KeyError: "Unable to open object (Object 'model_weights' doesn't exist)"
I am sure the path is correctly set.

Copy link

ghost commented Jul 3, 2017

I too faced the same issue and It is a strange error. I checked the complete Keras code. 'model_weights' is defined but it is wrapped up under load_weights() function. Hence I directly used load_weights() instead of using h5py.File(weights_path) and the below code worked for me.

Working code:

import h5py
weights_path = '/home/ubuntu/<<model>>_weights_tf_dim_ordering_tf_kernels.h5'
model.load_weights(weights_path, by_name=True)
layer_count=0;
for layer in model.layers:
    weights = layer.get_weights()
    layer_count=layer_count+1
    print("[INFO] Model Layer Configuration with respect to each layer weights as follows : " + str(layer.get_config()), str(layer.name),str(weights))
print("[INFO] The total number layers is : " + str(layer_count))
a.index("block1_conv1")
for i in layer_dict.keys():
    index = a.index(i)
    model.layers[index].set_weights(weights)

where a = [layer.name for layer in model.layers]

@brianleegit
Copy link

brianleegit commented Dec 8, 2017

@H-Cognitum Thanks for the workaround, it saves me lot of time.

@coreyauger
Copy link

coreyauger commented May 7, 2018

@H-Cognitum Thanks 👍

@yustiks
Copy link

yustiks commented Aug 2, 2018

@H-Cognitum I tried to implement the solution, but how to write things for a?
It is not clear for me how to work with this: 'where a = [layer.name for layer in model.layers]'

@jaelim
Copy link

jaelim commented Dec 5, 2018

I think it's because 'model_weights' doesn't exist in your .h5 file. You can do a simple check with:
for key in f.keys(): print(key)
So, either load .h5 with load_weights() or work around to ignore model_weights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment