SHARE
TWEET

Untitled

a guest Jun 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def getLayerIndexByName(model, layername):
  2.     for idx, layer in enumerate(model.layers):
  3.         if layer.name == layername:
  4.             return idx
  5.  
  6. idx = getLayerIndexByName(resnet, 'res3a_branch2a')
  7.  
  8. input_shape = resnet.layers[idx].get_input_shape_at(0) # which is here in my case (None, 55, 55, 256)
  9.  
  10. layer_input = Input(shape=input_shape[1:]) # as keras will add the batch shape
  11.  
  12. # create the new nodes for each layer in the path
  13. x = layer_input
  14. for layer in resnet.layers[idx:]:
  15.     x = layer(x)
  16.  
  17. # create the model
  18. new_model = Model(layer_input, x)
  19.      
  20. ValueError: Input 0 is incompatible with layer res3a_branch1: expected axis -1 of input shape to have value 256 but got shape (None, 28, 28, 512).
  21.      
  22. def split(model, start, end):
  23.     confs = model.get_config()
  24.     kept_layers = set()
  25.     for i, l in enumerate(confs['layers']):
  26.         if i == 0:
  27.             confs['layers'][0]['config']['batch_input_shape'] = model.layers[start].input_shape
  28.             if i != start:
  29.                 confs['layers'][0]['name'] += str(random.randint(0, 100000000)) # rename the input layer to avoid conflicts on merge
  30.                 confs['layers'][0]['config']['name'] = confs['layers'][0]['name']
  31.         elif i < start or i > end:
  32.             continue
  33.         kept_layers.add(l['name'])
  34.     # filter layers
  35.     layers = [l for l in confs['layers'] if l['name'] in kept_layers]
  36.     layers[1]['inbound_nodes'][0][0][0] = layers[0]['name']
  37.     # set conf
  38.     confs['layers'] = layers
  39.     confs['input_layers'][0][0] = layers[0]['name']
  40.     confs['output_layers'][0][0] = layers[-1]['name']
  41.     # create new model
  42.     submodel = Model.from_config(confs)
  43.     for l in submodel.layers:
  44.         orig_l = model.get_layer(l.name)
  45.         if orig_l is not None:
  46.             l.set_weights(orig_l.get_weights())
  47.     return submodel
  48.      
  49. ValueError: Unknown layer: Scale
  50.      
  51. import resnet   # pip install resnet
  52. from keras.models import Model
  53. from keras.layers import Input
  54.  
  55. def getLayerIndexByName(model, layername):
  56.     for idx, layer in enumerate(model.layers):
  57.         if layer.name == layername:
  58.             return idx
  59.  
  60.  
  61. resnet = resnet.ResNet152(weights='imagenet')
  62.  
  63. idx = getLayerIndexByName(resnet, 'res3a_branch2a')
  64.  
  65. model1 = Model(inputs=resnet.input, outputs=resnet.get_layer('res3a_branch2a').output)
  66.  
  67. input_shape = resnet.layers[idx].get_input_shape_at(0) # get the input shape of desired layer
  68. print(input_shape[1:])
  69. layer_input = Input(shape=input_shape[1:]) # a new input tensor to be able to feed the desired layer
  70.  
  71. # create the new nodes for each layer in the path
  72. x = layer_input
  73. for layer in resnet.layers[idx:]:
  74.     x = layer(x)
  75.  
  76. # create the model
  77. model2 = Model(layer_input, x)
  78.  
  79. model2.summary()
  80.      
  81. ValueError: Input 0 is incompatible with layer res3a_branch1: expected axis -1 of input shape to have value 256 but got shape (None, 28, 28, 512)
  82.      
  83. from keras.applications.resnet50 import ResNet50
  84. from keras import models
  85. from keras import layers
  86.  
  87. resnet = ResNet50()
  88.  
  89. # this is the split point, i.e. the starting layer in our sub-model
  90. starting_layer_name = 'activation_46'
  91.  
  92. # create a new input layer for our sub-model we want to construct
  93. new_input = layers.Input(batch_shape=resnet.get_layer(starting_layer_name).get_input_shape_at(0))
  94.  
  95. layer_outputs = {}
  96. def get_output_of_layer(layer):
  97.     print(layer.name)
  98.     # if we have already applied this layer in its input(s),
  99.     # just return the output
  100.     if layer.name in layer_outputs:
  101.         return layer_outputs[layer.name]
  102.  
  103.     # if this is the starting layer, then apply it on the input tensor
  104.     if layer.name == starting_layer_name:
  105.         out = layer(new_input)
  106.         layer_outputs[layer.name] = out
  107.         return out
  108.  
  109.     # find all the connected layers which this layer
  110.     # consumes their output
  111.     prev_layers = []
  112.     for node in layer._inbound_nodes:
  113.         prev_layers.extend(node.inbound_layers)
  114.  
  115.     # get the output of connected layers
  116.     pl_outs = []
  117.     for pl in prev_layers:
  118.         pl_outs.extend([get_output_of_layer(pl)])
  119.  
  120.     # apply this layer on the collected outputs
  121.     out = layer(pl_outs[0] if len(pl_outs) == 1 else pl_outs)
  122.     layer_outputs[layer.name] = out
  123.     return out
  124.  
  125. # note that we start from the last layer of our desired sub-model.
  126. # this layer could be any layer of the original model as long as it is
  127. # reachable from the starting layer
  128. new_output = get_output_of_layer(resnet.layers[-1])
  129.  
  130. # create the sub-model
  131. model = models.Model(new_input, new_output)
  132.      
  133. from keras.applications.resnet50 import ResNet50
  134. from keras.utils import plot_model
  135.  
  136. resnet = ResNet50()
  137. plot_model(model, to_file='resnet_model.png')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top