Esempio n. 1
0
    def load_weights(self, filepath, by_name=False, exclude=None):
        """Modified version of the correspoding Keras function with
        the addition of multi-GPU support and the ability to exclude
        some layers from loading.
        exlude: list of layer names to excluce
        """
        import h5py
        from keras.engine import topology

        if exclude:
            by_name = True

        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')
        f = h5py.File(filepath, mode='r')
        if 'layer_names' not in f.attrs and 'model_weights' in f:
            f = f['model_weights']

        # In multi-GPU training, we wrap the model. Get layers
        # of the inner model because they have the weights.
        keras_model = self.keras_model
        layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model") \
            else keras_model.layers

        # Exclude some layers
        if exclude:
            layers = filter(lambda l: l.name not in exclude, layers)

        if by_name:
            topology.load_weights_from_hdf5_group_by_name(f, layers)
        else:
            topology.load_weights_from_hdf5_group(f, layers)
        if hasattr(f, 'close'):
            f.close()
Esempio n. 2
0
    def load_weight(self, filepath, by_name=False, exclude=None):
        import h5py
        from keras.engine import topology

        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')
        f = h5py.File(filepath, mode='r')
        if 'layer_names' not in f.attrs and 'model_weights' in f:
            f = f['model_weights']

        # In multi-GPU training, get layers of inner model
        keras_model = self.keras_model
        layers = keras_model.inner_model.layers if hasattr(
            keras_model, "inner_model") else keras_model.layers

        # Exclude some layers
        if exclude:
            by_name = True
            layers = filter(lambda l: l.name not in exclude, layers)

        if by_name:
            topology.load_weights_from_hdf5_group_by_name(f, layers)
        else:
            topology.load_weights_from_hdf5_group(f, layers)
        if hasattr(f, 'close'):
            f.close()

        # Update the log directory
        self.set_log_dir(filepath)
Esempio n. 3
0
 def _import_mv_weights(self, model: Model, file_name: str):
     f = h5py.File(file_name, mode='r')
     if 'layer_names' not in f.attrs and 'model_weights' in f:
         f = f['model_weights']
     load_weights_from_hdf5_group_by_name(f, [
         layer
         for layer in model.layers if fnmatch.fnmatch(layer.name, "mv_*")
     ])
Esempio n. 4
0
    def load_weights(self, filepath, by_name=False, exclude=None):
        """
        Modified version of the correspoding Keras function with
        the addition of multi-GPU support and the ability to exclude
        some layers from loading.
        exlude: list of layer names to excluce
        """
        import h5py
        from keras.engine import topology
        print('>>> load_weights()')
        if exclude:
            by_name = True

        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')

        log('    load_weights: Loading weights from: {}'.format(filepath))
        f = h5py.File(filepath, mode='r')
        if 'layer_names' not in f.attrs and 'model_weights' in f:
            f = f['model_weights']

        # In multi-GPU training, we wrap the model. Get layers
        # of the inner model because they have the weights.
        keras_model = self.keras_model
        layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model")\
            else keras_model.layers

        # Exclude some layers
        if exclude:
            layers = filter(lambda l: l.name not in exclude, layers)

        # print(' layers to load ' )
        # print('----------------' )
        # for idx,layer in enumerate(layers):
        # print('>layer {} : name : {:40s}  type: {}'.format(idx,layer.name,layer))

        if by_name:
            topology.load_weights_from_hdf5_group_by_name(f, layers)
        else:
            topology.load_weights_from_hdf5_group(f, layers)
        if hasattr(f, 'close'):
            f.close()

        log('    load_weights: Log directory set to : {}'.format(filepath))
        # Update the log directory
        self.set_log_dir(filepath)
        print('    Load weights complete : ', filepath)
        return (filepath)
Esempio n. 5
0
 def load_model(self, model_path):
     with h5py.File(model_path, 'r') as f:
         for model in self.models:
             try:
                 hdf5_group = f[model.name]
             except KeyError:
                 print('Given HDF5 file ({hdf}) does not '
                       'contain model {model}. Skipping.'.format(
                           model=model.name, hdf=model_path))
             else:
                 kwargs = {}
                 if 'skip_mismatch' in (load_weights_from_hdf5_group_by_name
                                        .__code__.co_varnames):
                     kwargs['skip_mismatch'] = True
                 load_weights_from_hdf5_group_by_name(
                     hdf5_group, model.layers, **kwargs)
                 load_optimizer_weights(model, hdf5_group)
Esempio n. 6
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000,
             layers=50):
    """Instantiates the ResNet50 architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.
    When using TensorFlow, for best performance you should
    set `"image_data_format": "channels_last"` in the config.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise 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.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    assert layers in [18, 34, 50, 101, 152]
    use_bn = (layers == 50)
    basic = (layers in [18, 34])

    if layers == 18:
        num_layers = [2, 2, 2, 2]
    elif layers == 34:
        num_layers = [3, 4, 6, 3]
    elif layers == 50:
        num_layers = [3, 4, 6, 3]
    elif layers == 101:
        num_layers = [3, 4, 23, 3]
    elif layers == 152:
        num_layers = [3, 8, 36, 3]

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=197,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top,
                                      weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    if basic:
        x = Conv2D(64, (7, 7), strides=(2, 2), padding='same',
                   name='conv1')(img_input)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
        x = ResNetBlock(3, [64, 64, 256],
                        stage=2,
                        block='a',
                        use_bn=use_bn,
                        basic=basic)(x)
    else:
        x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
        x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid',
                   name='conv1')(x)
        if use_bn:
            x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)
        x = ResNetBlock(3, [64, 64, 256],
                        stage=2,
                        block='a',
                        strides=(1, 1),
                        use_bn=use_bn,
                        basic=basic)(x)

    for i in range(num_layers[0] - 1):
        x = ResNetBlock(3, [64, 64, 256],
                        stage=2,
                        block=chr(ord('b') + i),
                        identity=True,
                        use_bn=use_bn,
                        basic=basic)(x)

    x = ResNetBlock(3, [128, 128, 512],
                    stage=3,
                    block='a',
                    use_bn=use_bn,
                    basic=basic)(x)
    for i in range(num_layers[1] - 1):
        x = ResNetBlock(3, [128, 128, 512],
                        stage=3,
                        block=chr(ord('b') + i),
                        identity=True,
                        use_bn=use_bn,
                        basic=basic)(x)

    x = ResNetBlock(3, [256, 256, 1024],
                    stage=4,
                    block='a',
                    use_bn=use_bn,
                    basic=basic)(x)
    for i in range(num_layers[2] - 1):
        x = ResNetBlock(3, [256, 256, 1024],
                        stage=4,
                        block=chr(ord('b') + i),
                        identity=True,
                        use_bn=use_bn,
                        basic=basic)(x)

    x = ResNetBlock(3, [512, 512, 2048],
                    stage=5,
                    block='a',
                    use_bn=use_bn,
                    basic=basic)(x)
    for i in range(num_layers[3] - 1):
        x = ResNetBlock(3, [512, 512, 2048],
                        stage=5,
                        block=chr(ord('b') + i),
                        identity=True,
                        use_bn=use_bn,
                        basic=basic)(x)

    if basic:
        x = GlobalAveragePooling2D()(x)
    else:
        x = AveragePooling2D((7, 7), name='avg_pool')(x)
        x = Flatten()(x)
    x = Dense(classes, activation='softmax', name='fc1000')(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='resnet50')

    # load weights
    if weights == 'imagenet' and layers == 50:
        if include_top:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
        else:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='a268eb855778b3df3c7506639542a6af')

        with h5py.File(weights_path, mode='r') as f:
            if 'layer_names' not in f.attrs and 'model_weights' in f:
                f = f['model_weights']

            import itertools
            all_layers = [
                [l] if not isinstance(l, ResNetBlock) else l.get_layers()
                for l in model.layers
            ]
            all_layers = list(itertools.chain.from_iterable(all_layers))
            load_weights_from_hdf5_group_by_name(f, all_layers)

        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

    return model
Esempio n. 7
0
    def load_weights(self,
                     filepath,
                     by_name=False,
                     exclude=None,
                     new_folder=False):
        '''
        Modified version of the correspoding Keras function with
        the addition of multi-GPU support and the ability to exclude
        some layers from loading.
        exlude: list of layer names to excluce
        '''
        import h5py

        from keras.engine import topology
        log('   >>> load_weights() from : {}'.format(filepath))
        if exclude:
            by_name = True

        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')

        f = h5py.File(filepath, mode='r')
        pp.pprint(f.__dict__)
        if 'layer_names' not in f.attrs and 'model_weights' in f:
            print('im here')
            f = f['model_weights']
        else:
            print('im not here')

        # In multi-GPU training, we wrap the model. Get layers
        # of the inner model because they have the weights.
        keras_model = self.keras_model
        layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model")\
            else keras_model.layers

        print('\n\n')
        print('--------------------------------')
        print(' List of all Layers in Model    ')
        print('--------------------------------')
        print('\n\n')
        for idx, layer in enumerate(layers):
            print('>layer {} : name : {:40s}  type: {}'.format(
                idx, layer.name, layer))

        # Exclude some layers
        if exclude:
            layers = filter(lambda l: l.name not in exclude, layers)

        print(' --------------------------------------')
        print('  layers to load (not in exclude list) ')
        print(' --------------------------------------')
        for idx, layer in enumerate(layers):
            print('    >layer {} : name : {:40s}  type: {}'.format(
                idx, layer.name, layer))
        print('\n\n')

        if by_name:
            topology.load_weights_from_hdf5_group_by_name(f, layers)
        else:
            topology.load_weights_from_hdf5_group(f, layers)

        if hasattr(f, 'close'):
            f.close()

        # Update the log directory
        print('   Weights file loaded: {} '.format(filepath))
        print('   Weights file loaded: {} '.format(filepath),
              file=sys.__stdout__)

        if self.mode == 'training':
            self.set_log_dir(filepath, new_folder)

        print(" MODEL Load weight file COMPLETE    ")

        return (filepath)
Esempio n. 8
0
 def load_weights(self, weights_path):
     from keras.engine import topology
     import h5py
     f = h5py.File(weights_path)
     layers = self.keras_model.layers
     topology.load_weights_from_hdf5_group_by_name(f, layers)