xsingit / resnet152

Adaptation of ResNet-152 to match Keras API with added large input option

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ResNet-152 for Keras

Adaptation of ResNet-152 to match Keras API with added large input option. Original code from flyyufelix, Keras 2.0 modified version of original code from mvoelk.

Weights are ported from Caffe by flyyufelix. Check out his blog for more info. Weights for feature extraction are adapted version of these weights with the top layer not included.

Weights can be found in the release section of this repo but the code is setup to download and cache the weights when the model is instantiated so there is no need to download them directly.

The weights are in TensorFlow format but the code is compatible with both TensorFlow and Theano backends and will convert weights into Theano format if needed.

Examples

This version is modified to work the same as the ResNet50 model currently available in keras.applications

Examples as provided by fchollet are compatible as shown below:

Classify images

from resnet152 import ResNet152
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input, decode_predictions

model = ResNet152(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print(‘Predicted:’, decode_predictions(preds))
#Predicted: [[('n02504458', 'African_elephant', 0.57481891)]

Extract pool5 features from images

from resnet152 import ResNet152
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input

model = ResNet152(include_top=False, weights='imagenet')
    
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
    
features = model.predict(x)

Extract 14x14x2048 features from res5c

from resnet152 import ResNet152
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from keras.models import Model

base_model = ResNet152(weights='imagenet', large_input=True)
model = Model(inputs=base_model.input, outputs=base_model.get_layer('res5c').output)
    
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(448,448))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
    
res5c_features = model.predict(x)

References

About

Adaptation of ResNet-152 to match Keras API with added large input option


Languages

Language:Python 100.0%