def download_imagenet(self) -> str: filename = 'resnet-101.h5' resource = 'https://github.com/exemai/sf-retinanet/releases/download/0.0.1/resnet101-keras.h5' md5sum = '05dc86924389e5b401a9ea0348a3213c' model_path = get_file(filename, resource, cache_subdir='models', md5_hash=md5sum) return model_path
def download_imagenet(self) -> str: filename = 'resnet-152.h5' resource = 'https://github.com/exemai/sf-retinanet/releases/download/0.0.1/resnet152-keras.h5' md5sum = '6ee11ef2b135592f8031058820bb9e71' model_path = get_file(filename, resource, cache_subdir='models', md5_hash=md5sum) return model_path
def download_imagenet(self) -> str: filename = 'resnet-50.h5' resource = 'https://github.com/exemai/sf-retinanet/releases/download/0.0.1/resnet50-keras.h5' md5sum = '3e9f4e4f77bbe2c9bec13b53ee1c2319' model_path = get_file(filename, resource, cache_subdir='models', md5_hash=md5sum) return model_path
def download_imagenet(backbone): filename = resnet_filename.format(backbone[6:]) resource = resnet_resource.format(backbone[6:]) if backbone == 'resnet50': checksum = '3e9f4e4f77bbe2c9bec13b53ee1c2319' elif backbone == 'resnet101': checksum = '05dc86924389e5b401a9ea0348a3213c' elif backbone == 'resnet152': checksum = '6ee11ef2b135592f8031058820bb9e71' else: raise ValueError("Il backbone '{}' non รจ riconosciuto.".format(backbone)) return imagenet_utils.get_file( filename, resource, cache_subdir='models', md5_hash=checksum )
def download_imagenet(self): """ Downloads ImageNet weights and returns path to weights file. """ resnet_filename = 'ResNet-{}-model.keras.h5' resnet_resource = 'https://github.com/fizyr/keras-models/releases/download/v0.0.1/{}'.format( resnet_filename) depth = int(self.backbone.replace('resnet', '')) filename = resnet_filename.format(depth) resource = resnet_resource.format(depth) if depth == 50: checksum = '3e9f4e4f77bbe2c9bec13b53ee1c2319' elif depth == 101: checksum = '05dc86924389e5b401a9ea0348a3213c' elif depth == 152: checksum = '6ee11ef2b135592f8031058820bb9e71' return imagenet_utils.get_file(filename, resource, cache_subdir='models', md5_hash=checksum)
from keras_retinanet.models import ResNet50RetinaNet from keras_retinanet.preprocessing import PascalVocIterator def create_model(): image = keras.layers.Input((512, 512, 3)) im_info = keras.layers.Input((3,)) gt_boxes = keras.layers.Input((None, 5)) return ResNet50RetinaNet([image, im_info, gt_boxes]) if __name__=='__main__': # create the model model = create_model() # load pretrained weights weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path, by_name=True) # compile model (note: set loss to None since loss is added inside layer) model.compile(loss=None, optimizer='adam') # print model summary print(model.summary()) # create an image data generator object image_data_generator = keras.preprocessing.image.ImageDataGenerator( rescale=1/255 ) # create a generator for training data train_generator = PascalVocIterator(
def ResNet152(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet152 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # 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. """ WEIGHTS_PATH = None 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') # 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 x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(img_input) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPool2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for i in range(1, 8): x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i)) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for i in range(1, 36): x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i)) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AvgPool2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAvgPool2D()(x) elif pooling == 'max': x = GlobalMaxPool2D()(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='resnet152') # load weights if weights == 'imagenet': weights_path = get_file('resnet152_weights_tf.h5', WEIGHTS_PATH, cache_subdir='models') if include_top: model.load_weights(weights_path) else: f = h5py.File(weights_path) layer_names = [name for name in f.attrs['layer_names']] for i, layer in enumerate(model.layers): g = f[layer_names[i]] weights = [g[name] for name in g.attrs['weight_names']] layer.set_weights(weights) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.image_data_format() == 'channels_first' and K.backend( ) == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model