我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用keras.applications.imagenet_utils._obtain_input_shape()。
def build(self, input_shape=None, num_outputs=1000): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") # (227, 227, 3) input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) # x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(96, (11, 11), strides=(4, 4), name='conv1')(img_input) # (55, 55, 96) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(x) # (27, 27, 96) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Conv2D(256, (5, 5), strides=(4, 4), name='conv2')(x) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool2')(x) x = BatchNormalization(axis=3, name='bn_conv2')(x) x = Conv2D(384, (3, 3), strides=(1, 1), padding=1, name='conv3')(x) x = Conv2D(384, (3, 3), strides=(1, 1), padding=1, name='conv4')(x) x = Conv2D(256, (3, 3), strides=(1, 1), padding=1, name='conv5')(x) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool3')(x) x = Dense(units=4096)(x) x = Dense(units=4096)(x) x = Dense(units=num_outputs)(x) x = Activation('softmax')(x) self.model = Model(inputs=img_input, outputs=x, name='AlexNet Model') return self.model
def build(self, input_shape=None, num_outputs=1000): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") # (227, 227, 3) input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) # x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(96, (7, 7), strides=(2, 2), name='conv1')(img_input) # (55, 55, 96) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(x) # (27, 27, 96) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Conv2D(256, (5, 5), strides=(4, 4), name='conv2')(x) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool2')(x) x = BatchNormalization(axis=3, name='bn_conv2')(x) x = Conv2D(512, (3, 3), strides=(1, 1), padding=1, name='conv3')(x) x = Conv2D(1024, (3, 3), strides=(1, 1), padding=1, name='conv4')(x) x = Conv2D(512, (3, 3), strides=(1, 1), padding=1, name='conv5')(x) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool3')(x) x = Dense(units=4096)(x) x = Dense(units=4096)(x) x = Dense(units=num_outputs)(x) x = Activation('softmax')(x) self.model = Model(inputs=img_input, outputs=x, name='ZFNet Model') return self.model
def DenseNet_FCN(input_shape=None, weight_decay=1E-4, batch_momentum=0.9, batch_shape=None, classes=21, include_top=False, activation='sigmoid'): if include_top is True: # TODO(ahundt) Softmax is pre-applied, so need different train, inference, evaluate. # TODO(ahundt) for multi-label try per class sigmoid top as follows: # x = Reshape((row * col * classes))(x) # x = Activation('sigmoid')(x) # x = Reshape((row, col, classes))(x) return densenet.DenseNetFCN(input_shape=input_shape, weights=None, classes=classes, nb_layers_per_block=[4, 5, 7, 10, 12, 15], growth_rate=16, dropout_rate=0.2) # if batch_shape: # img_input = Input(batch_shape=batch_shape) # image_size = batch_shape[1:3] # else: # img_input = Input(shape=input_shape) # image_size = input_shape[0:2] input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=16, data_format=K.image_data_format(), include_top=False) img_input = Input(shape=input_shape) x = densenet.__create_fcn_dense_net(classes, img_input, input_shape=input_shape, nb_layers_per_block=[4, 5, 7, 10, 12, 15], growth_rate=16, dropout_rate=0.2, include_top=include_top) x = top(x, input_shape, classes, activation, weight_decay) # TODO(ahundt) add weight loading model = Model(img_input, x, name='DenseNet_FCN') return model
def densenet_cifar10_model(logits=False, input_range_type=1, pre_filter=lambda x:x): assert input_range_type == 1 batch_size = 64 nb_classes = 10 img_rows, img_cols = 32, 32 img_channels = 3 img_dim = (img_channels, img_rows, img_cols) if K.image_dim_ordering() == "th" else (img_rows, img_cols, img_channels) depth = 40 nb_dense_block = 3 growth_rate = 12 nb_filter = 16 dropout_rate = 0.0 # 0.0 for data augmentation input_tensor = None include_top=True if logits is True: activation = None else: activation = "softmax" # Determine proper input shape input_shape = _obtain_input_shape(img_dim, default_size=32, min_size=8, data_format=K.image_data_format(), include_top=include_top) 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 x = __create_dense_net(nb_classes, img_input, True, depth, nb_dense_block, growth_rate, nb_filter, -1, False, 0.0, dropout_rate, 1E-4, activation) # 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='densenet') return model # Source: https://github.com/titu1994/DenseNet
def build_resnet(self, input_shape=None, num_outputs=1000, layers=None, weights_path=None): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer layers: Number of layers for every network 50, 101, 152 weights_path: URL to the weights of a pre-trained model. optimizer: An optimizer to compile the model, if None sgd+momentum by default. Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(x) nb_filters = 64 stage = 2 for e in layers: for i in range(e): if i == 0: x = block_with_shortcut(x, nb_filters, stage=stage, block='a', strides=2 if stage >= 3 else 1) else: x = block_without_shortcut(x, nb_filters, stage=stage, block='b', index=i) stage += 1 nb_filters *= 2 x = AveragePooling2D((7, 7), strides=(1, 1), name='avg_pool')(x) x = Flatten()(x) x = Dense(units=num_outputs, activation='softmax', name='fc1000')(x) self.model = Model(inputs=img_input, outputs=x, name='ResNet Model') if weights_path is not None: model.load_weights(weights_path) return self.model
def build(self, input_shape=None, num_outputs=1000): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") # (224, 224, 3) input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) # x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(img_input) # (122, 122, 64) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(x) # (56, 56, 64) x = Conv2D(192, (3, 3), strides=(1, 1), name='conv2')(x) # (56, 56, 192) x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool2')(x) # (28, 28, 192) # * Inception 3a filters=256 # * Inception 3b filters=480 x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool3')(x) # (14, 14, 480) # * Inception 4a filters=512 # * Inception 4b filters=512 # * Inception 4c filters=512 # * Inception 4d filters=528 # * Inception 4e filters=832 x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool4')(x) # (7, 7, 832) # * Inception 5a filters=832 # * Inception 5b filters=1024 x = AveragePooling2D(pool_size=(7, 7), strides=(1, 1), padding='same', name='pool5')(x) # (1, 1, 1024) x = Dropout(0.4)(x) x = Dense(units=num_outputs)(x) x = Activation('softmax')(x) self.model = Model(inputs=img_input, outputs=x, name='GoogLeNet Model') return self.model
def build(self, input_shape=None, num_outputs=1000): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) # x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(32, (3, 3), strides=(2, 2), name='conv1')(img_input) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool1')(x) x = Conv2D(64, (3, 3), strides=(2, 2), name='conv2')(x) x = BatchNormalization(axis=3, name='bn_conv2')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool2')(x) x = Conv2D(128, (3, 3), strides=(2, 2), name='conv3')(x) x = Conv2D(64, (1, 1), strides=(2, 2), name='conv4')(x) x = Conv2D(128, (3, 3), strides=(2, 2), name='conv5')(x) x = BatchNormalization(axis=3, name='bn_conv3')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool3')(x) x = Conv2D(256, (3, 3), strides=(2, 2), name='conv6')(x) x = Conv2D(128, (1, 1), strides=(2, 2), name='conv7')(x) x = Conv2D(256, (3, 3), strides=(2, 2), name='conv8')(x) x = BatchNormalization(axis=3, name='bn_conv4')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool4')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv9')(x) x = Conv2D(256, (1, 1), strides=(2, 2), name='conv10')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv11')(x) x = Conv2D(256, (1, 1), strides=(2, 2), name='conv12')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv13')(x) x = BatchNormalization(axis=3, name='bn_conv5')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool5')(x) x = Conv2D(1024, (3, 3), strides=(2, 2), name='conv14')(x) x = Conv2D(512, (1, 1), strides=(2, 2), name='conv15')(x) x = Conv2D(1024, (3, 3), strides=(2, 2), name='conv16')(x) x = Conv2D(512, (1, 1), strides=(2, 2), name='conv17')(x) x = Conv2D(1024, (3, 3), strides=(2, 2), name='conv18')(x) x = BatchNormalization(axis=3, name='bn_conv6')(x) x = Activation('relu')(x) x = Dense(units=num_outputs)(x) x = AveragePooling2D((1, 1), strides=(1, 1), name='avg_pool')(x) x = Activation('softmax')(x) self.model = Model(inputs=img_input, outputs=x, name='DarkNet Model') return self.model
def build(self, input_shape=None, num_outputs=1000): """ Args: input_shape: The input shape in the form (nb_rows, nb_cols, nb_channels) TensorFlow Format!! num_outputs: The number of outputs at final softmax layer Returns: A compile Keras model. """ if len(input_shape) != 3: raise Exception("Input shape should be a tuple like (nb_rows, nb_cols, nb_channels)") input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), include_top=True) img_input = Input(shape=input_shape) # x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (3, 3), strides=(2, 2), name='conv1')(img_input) x = Conv2D(64, (3, 3), strides=(2, 2), name='conv2')(x) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool1')(x) x = Conv2D(128, (3, 3), strides=(2, 2), name='conv3')(x) x = Conv2D(128, (3, 3), strides=(2, 2), name='conv4')(x) x = BatchNormalization(axis=3, name='bn_conv2')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool2')(x) x = Conv2D(256, (3, 3), strides=(2, 2), name='conv5')(x) x = Conv2D(256, (3, 3), strides=(2, 2), name='conv6')(x) x = BatchNormalization(axis=3, name='bn_conv3')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool3')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv7')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv8')(x) x = BatchNormalization(axis=3, name='bn_conv4')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool4')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv9')(x) x = Conv2D(512, (3, 3), strides=(2, 2), name='conv10')(x) x = BatchNormalization(axis=3, name='bn_conv5')(x) x = Activation('relu')(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool5')(x) x = Dense(units=4096)(x) x = Dense(units=4096)(x) x = Dense(units=num_outputs)(x) x = Activation('softmax')(x) self.model = Model(inputs=img_input, outputs=x, name='ImageNet Model') return self.model
def Atrous_DenseNet(input_shape=None, weight_decay=1E-4, batch_momentum=0.9, batch_shape=None, classes=21, include_top=False, activation='sigmoid'): # TODO(ahundt) pass the parameters but use defaults for now if include_top is True: # TODO(ahundt) Softmax is pre-applied, so need different train, inference, evaluate. # TODO(ahundt) for multi-label try per class sigmoid top as follows: # x = Reshape((row * col * classes))(x) # x = Activation('sigmoid')(x) # x = Reshape((row, col, classes))(x) return densenet.DenseNet(depth=None, nb_dense_block=3, growth_rate=32, nb_filter=-1, nb_layers_per_block=[6, 12, 24, 16], bottleneck=True, reduction=0.5, dropout_rate=0.2, weight_decay=1E-4, include_top=True, top='segmentation', weights=None, input_tensor=None, input_shape=input_shape, classes=classes, transition_dilation_rate=2, transition_kernel_size=(1, 1), transition_pooling=None) # if batch_shape: # img_input = Input(batch_shape=batch_shape) # image_size = batch_shape[1:3] # else: # img_input = Input(shape=input_shape) # image_size = input_shape[0:2] input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=16, data_format=K.image_data_format(), include_top=False) img_input = Input(shape=input_shape) x = densenet.__create_dense_net(classes, img_input, depth=None, nb_dense_block=3, growth_rate=32, nb_filter=-1, nb_layers_per_block=[6, 12, 24, 16], bottleneck=True, reduction=0.5, dropout_rate=0.2, weight_decay=1E-4, top='segmentation', input_shape=input_shape, transition_dilation_rate=2, transition_kernel_size=(1, 1), transition_pooling=None, include_top=include_top) x = top(x, input_shape, classes, activation, weight_decay) model = Model(img_input, x, name='Atrous_DenseNet') # TODO(ahundt) add weight loading return model