def test_make_mvn_dist_fn(): from indl.dists import make_mvn_dist_fn input_dim = 4 dist_dim = 3 batch_size = 8 # Test with placeholder inputs = tfkl.Input(shape=(input_dim, )) # First the callable make_dist_fn, dist_params = make_mvn_dist_fn(inputs, dist_dim, shift_std=0.1) assert hasattr(make_dist_fn, '__call__') # tensorflow.python.keras.engine.keras_tensor.KerasTensor # assert isinstance(dist_params[0], tf.Tensor) # assert isinstance(dist_params[1], tf.Tensor) assert K.is_keras_tensor(dist_params[0]) assert K.is_keras_tensor(dist_params[1]) test_dist = make_dist_fn((dist_params[0], dist_params[1])) assert isinstance(test_dist, tfd.Distribution) # Then test using it to make a distribution _dist_layer = tfpl.DistributionLambda( make_distribution_fn=make_dist_fn, # convert_to_tensor_fn=lambda s: s.sample(n_samples), ) q_dist = _dist_layer(dist_params) _run_assertions_on_qdist(q_dist, inputs, input_dim, dist_dim, batch_size)
def __call__(self, inputs, initial_state=None, constants=None, **kwargs): inputs, initial_state, constants = self._standardize_args( inputs, initial_state, constants, self._num_constants) if initial_state is None and constants is None: return super(ExternalAttentionRNNWrapper, self).__call__(inputs, **kwargs) # If any of `initial_state` or `constants` are specified and are Keras # tensors, then add them to the inputs and temporarily modify the # input_spec to include them. additional_inputs = [] additional_specs = [] if initial_state is not None: kwargs['initial_state'] = initial_state additional_inputs += initial_state self.state_spec = [ InputSpec(shape=K.int_shape(state)) for state in initial_state ] additional_specs += self.state_spec if constants is not None: kwargs['constants'] = constants additional_inputs += constants self.constants_spec = [ InputSpec(shape=K.int_shape(constant)) for constant in constants ] self._num_constants = len(constants) additional_specs += self.constants_spec # at this point additional_inputs cannot be empty is_keras_tensor = K.is_keras_tensor(additional_inputs[0]) for tensor in additional_inputs: if K.is_keras_tensor(tensor) != is_keras_tensor: raise ValueError( 'The initial state or constants of an ExternalAttentionRNNWrapper' ' layer cannot be specified with a mix of' ' Keras tensors and non-Keras tensors' ' (a "Keras tensor" is a tensor that was' ' returned by a Keras layer, or by `Input`)') if is_keras_tensor: # Compute the full input spec, including state and constants full_input = inputs + additional_inputs full_input_spec = self.input_spec + additional_specs # Perform the call with temporarily replaced input_spec original_input_spec = self.input_spec self.input_spec = full_input_spec output = super(ExternalAttentionRNNWrapper, self).__call__(full_input, **kwargs) self.input_spec = self.input_spec[:len(original_input_spec)] return output else: return super(ExternalAttentionRNNWrapper, self).__call__(inputs, **kwargs)
def test_make_encd_variational(n_times=None): from indl.model.beta_vae import make_encd_variational params = { 'encd_rnn2_units': 14, # For constructing input 'zd_size': 4, 'qzd_init_std': 0.1, 'qzd_off_diag': False, 'q_samples': 1 } inputs = tf.keras.Input(shape=(n_times, params['encd_rnn2_units'])) qzd = make_encd_variational(params, inputs) assert K.is_keras_tensor( qzd) # isinstance(qzd, tfd.MultivariateNormalDiag) z_var = tf.keras.Model(inputs=inputs, outputs=qzd) batch_size = 16 timesteps = n_times or 20 enc_z = tf.random.uniform( (batch_size, timesteps, params['encd_rnn2_units'])) dummy_qzd = z_var(enc_z) assert isinstance(dummy_qzd, tfd.MultivariateNormalDiag) dummy_sample = dummy_qzd.sample(params['q_samples']) assert dummy_sample.shape.as_list() == [ params['q_samples'], batch_size, timesteps, params['zd_size'] ]
def __init__(self, include_top=True, input_tensor=None, input_shape=None, pooling=None, classes=8631): super(ResNet50Model, self).__init__() self.include_top = include_top input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=K.image_data_format(), require_flatten=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 = ResNet50Component(include_top=include_top)(img_input) # x = AveragePooling2D((7, 7), name='avg_pool')(x) x = Flatten()(x) if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input super(ResNet50Model, self).__init__(inputs, x, name='vggface_resnet50')
def nn_base(input_tensor=None, input_shape = (None, None, 3), trainable=False): 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 bn_axis = 3 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', trainable = trainable)(x) 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), trainable = trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', trainable = trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', trainable = trainable) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', trainable = trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', trainable = trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', trainable = trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', trainable = trainable) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', trainable = trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', trainable = trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', trainable = trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', trainable = trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', trainable = trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', trainable = trainable) return x
def test_make_encs_variational(): from indl.model.beta_vae import make_encs_variational params = { 'encs_rnn_units': 12, # Only needed to emulate previous layer's outputs 'dropout_rate': 0.01, 'zs_size': 10, 'qzs_off_diag': False, 'qzs_init_std': 0.1, 'q_samples': 2 } encoded_s = tf.keras.Input(shape=(params['encs_rnn_units'], )) qzs = make_encs_variational(params, encoded_s) # make_f_variational is a lightweight wrapper around `make_variational` which has its own tests. # There isn't much left to test here so let's just assert its type. assert K.is_keras_tensor( qzs) # isinstance(qzs, tfd.MultivariateNormalDiag) make_encs_var = tf.keras.Model(inputs=encoded_s, outputs=qzs, name="make_encs_var_model") batch_size = 16 encoded_s = tf.random.uniform((batch_size, params['encs_rnn_units'])) dummy_qzs = make_encs_var(encoded_s) assert isinstance(dummy_qzs, tfd.MultivariateNormalDiag) dummy_sample = dummy_qzs.sample(params['q_samples']) assert dummy_sample.shape.as_list() == [ params['q_samples'], batch_size, params['zs_size'] ]
def nn_base(input_tensor=None, trainable=False): # Determine proper input shape if K.image_data_format() == 'channels_first': input_shape = (3, None, None) else: input_shape = (None, None, 3) 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 = ZeroPadding2D((3, 3))(img_input) x = Convolution2D(64, (7, 7), strides=( 2, 2), name='conv1', trainable=trainable)(x) x = FixedBatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', trainable=trainable) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', trainable=trainable) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', trainable=trainable) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', trainable=trainable) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', trainable=trainable) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', trainable=trainable) return x
def vgg_network(input_tensor=None): """ Generating VGG16 network. :param input_tensor: get the uses tensorflow. :return: VGG16 network. """ # Determine proper input shape if K.image_data_format() == 'channels_first': input_shape = (3, None, None) else: input_shape = (None, None, 3) 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 # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) # x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) return x
def VGG16_wBN(input_tensor=None, input_shape=None, classes=1000, conv_dropout=0.1, dropout=0.3, activation='relu'): """Instantiates the VGG16 architecture with Batch Normalization # Arguments input_tensor: Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: shape tuple classes: optional number of classes to classify images, 1000 as per the imagenet # Returns A Keras model instance. """ img_input = Input(shape=input_shape) if input_tensor is None else ( Input(tensor=input_tensor, shape=input_shape) if not K.is_keras_tensor(input_tensor) else input_tensor ) # Block 1 x = conv_block(32, dropout=conv_dropout, activation=activation, block=1, layer=1)(img_input) x = conv_block(32, dropout=conv_dropout, activation=activation, block=1, layer=2)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = conv_block(64, dropout=conv_dropout, activation=activation, block=2, layer=1)(x) x = conv_block(64, dropout=conv_dropout, activation=activation, block=2, layer=2)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = conv_block(128, dropout=conv_dropout, activation=activation, block=3, layer=1)(x) x = conv_block(128, dropout=conv_dropout, activation=activation, block=3, layer=2)(x) x = conv_block(128, dropout=conv_dropout, activation=activation, block=3, layer=3)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = conv_block(256, dropout=conv_dropout, activation=activation, block=4, layer=1)(x) x = conv_block(256, dropout=conv_dropout, activation=activation, block=4, layer=2)(x) x = conv_block(256, dropout=conv_dropout, activation=activation, block=4, layer=3)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = conv_block(256, dropout=conv_dropout, activation=activation, block=5, layer=1)(x) x = conv_block(256, dropout=conv_dropout, activation=activation, block=5, layer=2)(x) x = conv_block(256, dropout=conv_dropout, activation=activation, block=5, layer=3)(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) # Flatten x = GlobalAveragePooling2D()(x) # FC Layers x = dense_block(512, dropout=dropout, activation=activation, name='fc1')(x) x = dense_block(512, dropout=dropout, activation=activation, name='fc2')(x) # Classification block x = Dense(classes, activation='softmax', name='predictions')(x) # Ensure that the model takes into account any potential predecessors of `input_tensor`. inputs = get_source_inputs(input_tensor) if input_tensor is not None else img_input # Create model. return Model(inputs, x, name='vgg16_bn')
def model_mobilenet(input_tensor=None, input_shape=None, classes=1000, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, pooling=None, weights=None, data_format="channels_last"): input_shape = deduce_input_shape(input_shape, require_flatten=include_top, weights=weights, data_format=data_format) if data_format == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if input_tensor is not None: if not backend.is_keras_tensor(input_tensor): raise ValueError("input_tensor must be a Keras layer tensor.") img_input = input_tensor else: img_input = layers.Input(shape=input_shape) # construct the convolutional network net = cnn_mobilenet(img_input, alpha=alpha, depth_multiplier=depth_multiplier, data_format=data_format) if include_top: # add the classification network net = top_block(net, classes=classes, alpha=alpha, dropout=dropout, data_format=data_format) else: # add global pooling if required net = pool_block(net, pooling, data_format) if input_tensor is not None: inputs = utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model_name = 'mobilenet_%0.2f_%s' % (alpha, rows) model = models.Model(inputs, net, name=model_name) if weights == 'imagenet': weight_path = get_weights_path(alpha, rows, include_top) model.load_weights(weight_path) elif weights is not None: model.load_weights(weights) return model
def shuffle_net(input_tensor, scale_factor=1, pooling='avg', input_shape=(224, 224, 3), groups=3, botteleneck_ratio=0.25): out_dim_stage_two = {1: 144, 2: 200, 3: 240, 4: 272, 8: 384} num_shuffle_units = [3, 7, 3] if pooling not in ['max', 'avg']: raise ValueError('Invalid value for pooling') # calculate output channels for each stage exp = np.insert(np.arange(0, len(num_shuffle_units)), 0, 0) out_channels_in_stage = 2**exp out_channels_in_stage *= out_dim_stage_two[groups] out_channels_in_stage[0] = 24 # fist stage has always 24 output channels out_channels_in_stage *= scale_factor out_channels_in_stage = out_channels_in_stage.astype(int) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if K.is_keras_tensor(input_tensor): img_input = input_tensor else: img_input = layers.Input(tensor=input_tensor, shape=input_shape) x = layers.Conv2D(filters=out_channels_in_stage[0], kernel_size=(3, 3), padding='same', use_bias=False, strides=(2, 2), activation='relu', name='conv1')(img_input) x = layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='max_pool1')(x) # create stages containing shuffle-net units beginning at stage 2 for stage in range(len(num_shuffle_units)): repeat = num_shuffle_units[stage] x = _block(x, out_channels_in_stage, repeat=repeat, bottleneck_ratio=botteleneck_ratio, groups=groups, stage=stage + 2) return x
def DenseNet(blocks, input_tensor=None, input_shape=None, pooling=None, activation=lambda: layers.Activation('relu')): if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) # x = layers.Activation(activation, name='conv1/activation')(x) x = activation()(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = layers.MaxPooling2D(3, strides=2, name='pool1')(x) x = dense_block(x, blocks[0], name='conv2', activation=activation) x = transition_block(x, 0.5, name='pool2', activation=activation) x = dense_block(x, blocks[1], name='conv3', activation=activation) x = transition_block(x, 0.5, name='pool3', activation=activation) x = dense_block(x, blocks[2], name='conv4', activation=activation) x = transition_block(x, 0.5, name='pool4', activation=activation) x = dense_block(x, blocks[3], name='conv5', activation=activation) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) # x = layers.Activation(activation, name='activation')(x) x = activation()(x) if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input return inputs, x
def nn_base(input_tensor=None, trainable=False): # Determine proper input shape if K.image_dim_ordering() == 'th': input_shape = (3, None, None) else: input_shape = (None, None, 3) 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_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) # x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) return x
def _run_assertions_on_qdist(q_dist, inputs, input_dim, dist_dim, batch_size): # assert isinstance(q_dist, (tfd.MultivariateNormalDiag, tfd.MultivariateNormalTriL)) assert K.is_keras_tensor(q_dist) # Test in a model with a data tensor model = tf.keras.Model(inputs=inputs, outputs=q_dist) dummy_inputs = tf.random.uniform((batch_size, input_dim)) dummy_q = model(dummy_inputs) assert isinstance(dummy_q, (tfd.MultivariateNormalDiag, tfd.MultivariateNormalTriL)) assert dummy_q.stddev().shape.as_list() == [batch_size, dist_dim] assert np.all(dummy_q.stddev().numpy() > 0) assert dummy_q.sample().shape.as_list() == [batch_size, dist_dim] assert ~np.any(np.isnan(dummy_q.sample().numpy())) # Implied convert_to_tensor != mean assert tf.not_equal(tf.add(dummy_q, 0), dummy_q.mean()).numpy().any()
def hilbert(x): ''' Implements the hilbert transform. Args: x: The input sequence. A tensor of shape (None, channel, sample, filter) Returns: xc: A complex sequence of the same shape. ''' if len(x.shape) != 4: raise NotImplementedError if x.dtype != 'float32': x = tf.cast(x, dtype=tf.float32) if K.is_keras_tensor(x): if K.image_data_format() == 'channels_first': filter_num = K.int_shape(input_tensor)[1] channel = K.int_shape(input_tensor)[2] N = K.int_shape(input_tensor)[3] else: channel = K.int_shape(input_tensor)[1] N = K.int_shape(input_tensor)[2] filter_num = K.int_shape(input_tensor)[3] x = tf.transpose(x, [0, 1, 3, 2]) x = tf.reshape(x, [-1, channel * filter_num, N]) x = tf.complex(x, tf.zeros_like(x)) Xf = tf.signal.fft(x) if N % 2 == 0: part0 = tf.ones(1) part1 = 2 * tf.ones(N // 2 - 1) part2 = tf.ones(1) part3 = tf.zeros(N // 2 - 1) h = tf.concat([part0, part1, part2, part3], axis=0) else: part0 = tf.ones(1) part1 = 2 * tf.ones((N + 1) // 2 - 1) part2 = tf.zeros((N + 1) // 2 - 1) h = tf.concat([part0, part1, part2], axis=0) hs = tf.expand_dims(h, 0) hs = tf.expand_dims(hs, 0) tf_hc = tf.complex(hs, tf.zeros_like(hs)) xc = Xf * tf_hc out = tf.signal.ifft(xc) return tf.transpose(tf.reshape(out, [-1, channel, filter_num, N]), [0, 1, 3, 2])
def test_jaccard_distance(): # all_right, almost_right, half_right, all_wrong y_true = np.array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1., 0.]]) y_pred = np.array([[0, 0, 1, 0], [0, 0, 0.9, 0], [0, 0, 0.1, 0], [1, 1, 0.1, 1.]]) r = jaccard_distance( K.variable(y_true), K.variable(y_pred), ) if K.is_keras_tensor(r): assert K.int_shape(r) == (4, ) all_right, almost_right, half_right, all_wrong = K.eval(r) assert all_right == 0, 'should converge on zero' assert all_right < almost_right assert almost_right < half_right assert half_right < all_wrong
def model_resnet50(input_tensor=None, input_shape=None, classes=1000, include_top=True, pooling=None, weights=None, data_format="channels_last"): ch_axis = 3 if data_format == 'channels_last' else 1 input_shape = deduce_input_shape(input_shape, require_flatten=include_top, weights=weights, data_format=data_format) if input_tensor is not None: if not backend.is_keras_tensor(input_tensor): raise ValueError("input_tensor must be a Keras layer tensor.") img_input = input_tensor else: img_input = layers.Input(shape=input_shape) net = cnn_resnet50(img_input, ch_axis=ch_axis) if include_top: net = top_resnet(net, classes) else: # add global pooling if required net = pool_resnet(net, pooling, data_format) if input_tensor is not None: inputs = utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, net, name='resnet50') if weights == 'imagenet': resource = KERAS_TEAM['RESNET50'] resource = resource['WITH_TOP'] if include_top else resource['NO_TOP'] origin = resource.uri resource_path = utils.get_file(resource.name, origin, cache_subdir="models", file_hash=resource.file_hash) model.load_weights(resource_path) pass elif weights is not None: model.load_weights(weights) return model
def model_vgg16(input_tensor=None, input_shape=None, classes=1000, include_top=True, pooling=None, weights=None): # provide a way to compute the default input shape input_shape = deduce_input_shape(input_shape, require_flatten=include_top, weights=weights) if input_tensor is not None: if not backend.is_keras_tensor(input_tensor): raise ValueError("input_tensor must be a Keras layer tensor.") img_input = input_tensor else: img_input = layers.Input(shape=input_shape) net = cnn_vgg16(img_input) if include_top: # add the top classification network net = top_vgg(net, classes) else: # add global pooling if required net = pool_cnn(net, pooling) if input_tensor is not None: inputs = utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, net, name='vgg16') if weights == 'imagenet': resource = FCHOLLET['VGG16'] resource = resource['WITH_TOP'] if include_top else resource['NO_TOP'] origin = resource.uri resource_path = utils.get_file(resource.name, origin, cache_subdir="models", file_hash=resource.file_hash) model.load_weights(resource_path) elif weights is not None: model.load_weights(weights) return model
def featurenet_3D_backbone(input_tensor=None, input_shape=None, n_filters=32, **kwargs): """Construct the deepcell backbone with five convolutional units Args: input_tensor (tensor): Input tensor to specify input size n_filters (int): Number of filters for convolutional layers Returns: tuple: List of backbone layers, list of backbone names """ if input_tensor is None: img_input = Input(shape=input_shape) else: if not is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Build out backbone c1 = featurenet_3D_block(img_input, n_filters) # 1/2 64x64 c2 = featurenet_3D_block(c1, n_filters) # 1/4 32x32 c3 = featurenet_3D_block(c2, n_filters) # 1/8 16x16 c4 = featurenet_3D_block(c3, n_filters) # 1/16 8x8 c5 = featurenet_3D_block(c4, n_filters) # 1/32 4x4 backbone_features = [c1, c2, c3, c4, c5] backbone_names = ['C1', 'C2', 'C3', 'C4', 'C5'] output_dict = {} for name, feature in zip(backbone_names, backbone_features): output_dict[name] = feature if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input model = Model(inputs=inputs, outputs=backbone_features) return model, output_dict
def ResNet50(input_tensor=None, input_shape=None): model_name = 'resnet50' if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1_conv')(img_input) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) p0 = layers.Activation('relu', name='conv1_relu')(x) x = layers.MaxPooling2D(3, strides=2, padding='SAME', name='pool1_pool')(p0) p1 = stack1(x, 64, 3, stride1=1, name='conv2') p2 = stack1(p1, 128, 4, stride1=2, name='conv3') p3 = stack1(p2, 256, 6, stride1=1, dilation=2, name='conv4') p4 = stack1(p3, 512, 3, stride1=1, name='conv5') x = p4 # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name=model_name) return model
def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, input_tensor=None, input_shape=None, pooling=None, classes=1000, **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt 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`. # Arguments stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. 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. 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 input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding3D(padding=3, name='conv1_pad')(img_input) x = layers.Conv3D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) if preact is False: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding3D(padding=1, name='pool1_pad')(x) x = layers.MaxPooling3D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if preact is True: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling3D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling3D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling3D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = keras_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name=model_name) return model
def ResNet50(include_top=True, input_tensor=None, input_shape=None, pooling=None, classes=10, squeeze=False, squeeze_type='normal', **kwargs): """Instantiates the ResNet50 architecture. # Arguments include_top: whether to include the fully-connected layer at the top of the network. 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 32. 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 block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, 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. """ # global backend, layers, models, keras_utils # backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs) # Determine proper input shape input_shape = (32, 32, 3) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # K.learning_phase() if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) # x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x, training = False) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) # conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)): x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', squeeze=squeeze, squeeze_type=squeeze_type) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', squeeze=squeeze, squeeze_type=squeeze_type) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', squeeze=squeeze, squeeze_type=squeeze_type) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', squeeze=squeeze, squeeze_type=squeeze_type) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', squeeze=squeeze, squeeze_type=squeeze_type) # Output shape: (1, 1, depth) # x = AveragePooling2D((7, 7), name='avg_pool')(x) # print("Output shape :") # print(x.get_shape()) if include_top: x = Flatten()(x) # print("After flatten ") # print(x.get_shape()) x = Dense(classes, activation='softmax', name='fc1000')(x) # print("After Dense ") # print(x.get_shape()) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(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') return model
def SEDenseNet(input_shape=None, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1, nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=0.0, weight_decay=1e-4, subsample_initial_block=False, include_top=True, weights=None, input_tensor=None, classes=10, activation='softmax'): """Instantiate the SE DenseNet architecture # Arguments input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(32, 32, 3)` (with `channels_last` dim ordering) or `(3, 32, 32)` (with `channels_first` dim ordering). It should have exactly 3 inputs channels, and width and height should be no smaller than 8. E.g. `(200, 200, 3)` would be one valid value. depth: number or layers in the DenseNet nb_dense_block: number of dense blocks to add to end (generally = 3) growth_rate: number of filters to add per dense block nb_filter: initial number of filters. -1 indicates initial number of filters is 2 * growth_rate nb_layers_per_block: number of layers in each dense block. Can be a -1, positive integer or a list. If -1, calculates nb_layer_per_block from the network depth. If positive integer, a set number of layers per dense block. If list, nb_layer is used as provided. Note that list size must be (nb_dense_block + 1) bottleneck: flag to add bottleneck blocks in between dense blocks reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression. dropout_rate: dropout rate weight_decay: weight decay rate subsample_initial_block: Set to True to subsample the initial convolution and add a MaxPool2D before the dense blocks are added. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or 'imagenet' (pre-training on ImageNet).. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. 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. activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'. Note that if sigmoid is used, classes must be 1. # Returns A Keras model instance. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `cifar10` ' '(pre-training on CIFAR-10).') 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') if activation not in ['softmax', 'sigmoid']: raise ValueError('activation must be one of "softmax" or "sigmoid"') if activation == 'sigmoid' and classes != 1: raise ValueError( 'sigmoid activation can only be used when classes = 1') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=32, min_size=8, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = __create_dense_net(classes, img_input, include_top, depth, nb_dense_block, growth_rate, nb_filter, nb_layers_per_block, bottleneck, reduction, dropout_rate, weight_decay, subsample_initial_block, 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='se-densenet') return model
def QuantizedResNet50FusedBN(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, batch_size=None, nbits=24, fbits=9, rounding_method='nearest', quant_mode='hybrid', overflow_mode=False, stop_gradient=False, verbose=True): """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`. # 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 verbose: print('\nBuilding model : Quantized ResNet50') pbar=tqdm(total=19) layer_quantizer=build_layer_quantizer(nbits,fbits,rounding_method,overflow_mode,stop_gradient) if verbose: pbar.set_postfix_str('Preparation') pbar.update() if not os.path.exists(weights): raise ValueError('The `weights` argument must be the path to the weights file to be loaded. File not found!') 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=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(batch_shape=(batch_size,)+input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, batch_shape=(batch_size,)+input_shape) else: img_input = input_tensor if verbose: pbar.set_postfix_str('building stage 1') x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = QuantizedConv2D(64, kernel_size=(7, 7), strides=(2, 2), padding='valid', quantizers=layer_quantizer, name='conv1', quant_mode=quant_mode)(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) if verbose: pbar.update() pbar.set_postfix_str('building stage 2 block a') x = conv_block_fused_BN(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 2 block b') x = identity_block_fused_BN(x, 3, [64, 64, 256], stage=2, block='b', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 2 block c') x = identity_block_fused_BN(x, 3, [64, 64, 256], stage=2, block='c', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block a') x = conv_block_fused_BN(x, 3, [128, 128, 512], stage=3, block='a', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block b') x = identity_block_fused_BN(x, 3, [128, 128, 512], stage=3, block='b', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block c') x = identity_block_fused_BN(x, 3, [128, 128, 512], stage=3, block='c', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block d') x = identity_block_fused_BN(x, 3, [128, 128, 512], stage=3, block='d', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 4 block a') x = conv_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='a', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block b') x = identity_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='b', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block c') x = identity_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='c', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block d') x = identity_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='d', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block e') x = identity_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='e', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 3 block f') x = identity_block_fused_BN(x, 3, [256, 256, 1024], stage=4, block='f', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 5 block a') x = conv_block_fused_BN(x, 3, [512, 512, 2048], stage=5, block='a', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 5 block b') x = identity_block_fused_BN(x, 3, [512, 512, 2048], stage=5, block='b', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building stage 5 block c') x = identity_block_fused_BN(x, 3, [512, 512, 2048], stage=5, block='c', layer_quantizer=layer_quantizer, quant_mode=quant_mode) if verbose: pbar.update() pbar.set_postfix_str('building output block') if include_top: x = layers.AveragePooling2D((7, 7), name='avg_pool')(x) x = QuantizedFlatten()(x) x = QuantizedDense(classes, activation='softmax', name='fc1000', quantizers=layer_quantizer, quant_mode=quant_mode, last_layer=True)(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) else: warnings.warn('The output shape of `ResNet50(include_top=False)` ' 'has been changed since Keras 2.2.0.') if verbose: pbar.update() # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = backend.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='quantized_resnet50_fusedBN') if verbose: pbar.set_postfix_str('Model Built') pbar.close() # load weights if weights is not None: model.load_weights(weights) return model
def RESNET50(include_top=True, weights='vggface', input_tensor=None, input_shape=None, pooling=None, classes=8631): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, 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), use_bias=False, strides=(2, 2), padding='same', name='conv1/7x7_s2')(img_input) x = BatchNormalization(axis=bn_axis, name='conv1/7x7_s2/bn')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = resnet_conv_block(x, 3, [64, 64, 256], stage=2, block=1, strides=(1, 1)) x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=2) x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=3) x = resnet_conv_block(x, 3, [128, 128, 512], stage=3, block=1) x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=2) x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=3) x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=4) x = resnet_conv_block(x, 3, [256, 256, 1024], stage=4, block=1) x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=2) x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=3) x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=4) x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=5) x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=6) x = resnet_conv_block(x, 3, [512, 512, 2048], stage=5, block=1) x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=2) x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=3) x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='classifier')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(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='vggface_resnet50') # load weights if weights == 'vggface': # if include_top: # weights_path = get_file('rcmalli_vggface_tf_resnet50.h5', # utils.RESNET50_WEIGHTS_PATH, # cache_subdir=utils.VGGFACE_DIR) # else: # weights_path = get_file('rcmalli_vggface_tf_notop_resnet50.h5', # utils.RESNET50_WEIGHTS_PATH_NO_TOP, # cache_subdir=utils.VGGFACE_DIR) weights_path = os.path.join(WEIGHTS_DIR, 'rcmalli_vggface_tf_notop_resnet50.h5') model.load_weights(weights_path) # 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='classifier') # 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
def __DenseNet121(nb_dense_block=4, growth_rate=32, nb_filter=64, reduction=0.5, dropout_rate=0.0, weight_decay=1e-4, load_weights=True, include_top=False, input_tensor=None, pooling='avg', input_shape=(224, 224, 3), classes=1000): '''Instantiate the DenseNet 121 architecture, # Arguments nb_dense_block: number of dense blocks to add to end growth_rate: number of filters to add per dense block nb_filter: initial number of filters reduction: reduction factor of transition blocks. dropout_rate: dropout rate weight_decay: weight decay factor classes: optional number of classes to classify images weights_path: path to pre-trained weights # Returns A Keras model instance. ''' eps = 1.1e-5 # compute compression factor compression = 1.0 - reduction 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 # endif # endif # Handle Dimension Ordering for different backends global concat_axis if K.image_data_format() == 'channels_last': concat_axis = 3 else: concat_axis = 1 # endif # From architecture for ImageNet (Table 1 in the paper) nb_filter = 64 nb_layers = [6,12,24,16] # For DenseNet-121 # Initial convolution x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Convolution2D(nb_filter, kernel_size=(7, 7), strides=(2, 2), name='conv1', use_bias=False)(x) x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x) x = Scale(axis=concat_axis, name='conv1_scale')(x) x = Activation('relu', name='relu1')(x) x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) # Add dense blocks for block_idx in range(nb_dense_block - 1): stage = block_idx+2 x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) # Add transition_block x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay) nb_filter = int(nb_filter * compression) # endfor final_stage = stage + 1 x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay) x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x) x = Scale(axis=concat_axis, name='conv'+str(final_stage)+'_blk_scale')(x) x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x) if include_top: x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='pool'+str(final_stage))(x) # endif # endif fc_ll = x x = Dense(classes, name='fc6')(x) x = Activation('softmax', name='prob')(x) model = Model(img_input, x, name='densenet121') if load_weights: weights_path = get_file('densenet121_weights_tf.h5', DENSENET_121_WEIGHTS_PATH, cache_subdir='models') model.load_weights(weights_path) # endif if include_top == False: model = Model(img_input, fc_ll) # endif return model
def nn_base(width_coefficient, depth_divisor, input_tensor=None, trainable=False, dropout_rate=0.2, drop_connect_rate=0.2): # open json contaning information about blocks with open('./block_args.json', 'r') as f: blocks_json = json.load(f) # determine input shape. As the code will only support # tensorflow backend, there is only one option for input # shape input_shape = (None, None, 3 ) # 3 because the model will receive RGB images 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 # because the backend is tensorflow, # K.image_dim_ordering() == 'tf bn_axis = 3 # get swish activation function activation = get_swish() # build stem x = Conv2D(round_filters(32, width_coefficient, depth_divisor), 3, padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='stem_conv')(img_input) x = BatchNormalization(axis=bn_axis, name='stem_bn')(x) x = Activation(activation, name='stem_activation')(x) # build blocks total_num_blocks = sum(blocks_json[block]['num_repeat'] for block in blocks_json) block_num = 0 # go through every big block defined in block_args.json for idx, block in enumerate(blocks_json): # transform this specific block in an object block = Struct(**blocks_json[block]) # ensure num_repeat is bigger than zero assert block.num_repeat > 0 # update block input and output filters based on depth multiplier. block.input_filters = round_filters(block.input_filters, width_coefficient, depth_divisor) # the first block needs to take care of stride and filter size increase. drop_rate = drop_connect_rate * float(block_num) / total_num_blocks x = mb_conv_block(block, drop_rate=drop_rate, activation=activation, prefix='block{}a_'.format(idx + 1))(x) block_num += 1 if block.num_repeat > 1: block.input_filters = block.output_filters block.strides = [1, 1] for bidx in range(block.num_repeat - 1): drop_rate = drop_connect_rate * float( block_num) / total_num_blocks block_prefix = 'block{}{}_'.format( idx + 1, string.ascii_lowercase[bidx + 1]) x = mb_conv_block(block, prefix=block_prefix, drop_rate=drop_rate, activation=activation) block_num += 1 # build top x = Conv2D(round_filters(1280, width_coefficient, depth_divisor), 1, padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='top_conv')(x) x = BatchNormalization(axis=bn_axis, name='top_bn')(x) x = Activation(activation, name='top_activation')(x) return x
def EfficientNet(input_shape, block_args_list, global_params, input_tensor=None, include_top=True, pooling=None): batch_norm_momentum = global_params.batch_norm_momentum batch_norm_epsilon = global_params.batch_norm_epsilon if global_params.data_format == 'channels_first': channel_axis = 1 else: channel_axis = -1 # Stem part # Stem part if input_tensor is None: inputs = KL.Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): inputs = KL.Input(tensor=input_tensor, shape=input_shape) else: inputs = input_tensor x = inputs x = KL.Conv2D( filters=round_filters(32, global_params), kernel_size=[3, 3], strides=[2, 2], kernel_initializer=conv_kernel_initializer, padding='same', use_bias=False )(x) x = KL.BatchNormalization( axis=channel_axis, momentum=batch_norm_momentum, epsilon=batch_norm_epsilon )(x) x = Swish()(x) # Blocks part block_idx = 1 n_blocks = sum([block_args.num_repeat for block_args in block_args_list]) drop_rate = global_params.drop_connect_rate or 0 drop_rate_dx = drop_rate / n_blocks for block_args in block_args_list: assert block_args.num_repeat > 0 # Update block input and output filters based on depth multiplier. block_args = block_args._replace( input_filters=round_filters(block_args.input_filters, global_params), output_filters=round_filters(block_args.output_filters, global_params), num_repeat=round_repeats(block_args.num_repeat, global_params) ) # The first block needs to take care of stride and filter size increase. x = MBConvBlock(block_args, global_params, drop_connect_rate=drop_rate_dx * block_idx)(x) block_idx += 1 if block_args.num_repeat > 1: block_args = block_args._replace(input_filters=block_args.output_filters, strides=[1, 1]) for _ in xrange(block_args.num_repeat - 1): x = MBConvBlock(block_args, global_params, drop_connect_rate=drop_rate_dx * block_idx)(x) block_idx += 1 # Head part x = KL.Conv2D( filters=round_filters(1280, global_params), kernel_size=[1, 1], strides=[1, 1], kernel_initializer=conv_kernel_initializer, padding='same', use_bias=False )(x) x = KL.BatchNormalization( axis=channel_axis, momentum=batch_norm_momentum, epsilon=batch_norm_epsilon )(x) x = Swish()(x) if include_top: x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x) if global_params.dropout_rate > 0: x = KL.Dropout(global_params.dropout_rate)(x) x = KL.Dense(global_params.num_classes, kernel_initializer=dense_kernel_initializer)(x) x = KL.Activation('softmax')(x) else: if pooling == 'avg': x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x) elif pooling == 'max': x = KL.GlobalMaxPooling2D(data_format=global_params.data_format)(x) outputs = x model = KM.Model(inputs, outputs) return model
def build_resnet(repetitions=(2, 2, 2, 2), include_top=True, input_tensor=None, input_shape=None, classes=1000, block_type='usual', name=None): """ TODO """ # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format='channels_last', require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape, name='data') else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # get parameters for model layers no_scale_bn_params = get_bn_params(scale=False) bn_params = get_bn_params() conv_params = get_conv_params() init_filters = 64 if block_type == 'basic': conv_block = basic_conv_block identity_block = basic_identity_block else: conv_block = usual_conv_block identity_block = usual_identity_block # resnet bottom x = BatchNormalization(name='bn_data', **no_scale_bn_params)(img_input) x = ZeroPadding2D(padding=(3, 3))(x) x = Conv2D(init_filters, (7, 7), strides=(2, 2), name='conv0', **conv_params)(x) x = BatchNormalization(name='bn0', **bn_params)(x) x = Activation('relu', name='relu0')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='valid', name='pooling0')(x) # resnet body for stage, rep in enumerate(repetitions): for block in range(rep): filters = init_filters * (2**stage) # first block of first stage without strides because we have maxpooling before if block == 0 and stage == 0: x = conv_block(filters, stage, block, strides=(1, 1))(x) elif block == 0: x = conv_block(filters, stage, block, strides=(2, 2))(x) else: x = identity_block(filters, stage, block)(x) x = BatchNormalization(name='bn1', **bn_params)(x) x = Activation('relu', name='relu1')(x) # resnet top if include_top: x = GlobalAveragePooling2D(name='pool1')(x) x = Dense(classes, name='fc1')(x) x = Activation('softmax', name='softmax')(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=name) return model
def SEResNet(input_shape=None, initial_conv_filters=64, depth=[3, 4, 6, 3], filters=[64, 128, 256, 512], width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000): """ Instantiate the Squeeze and Excite ResNet architecture. 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 are compatible with both TensorFlow and Theano. The dimension ordering convention used by the model is the one specified in your Keras config file. # Arguments initial_conv_filters: number of features for the initial convolution depth: number or layers in the each block, defined as a list. ResNet-50 = [3, 4, 6, 3] ResNet-101 = [3, 6, 23, 3] ResNet-152 = [3, 8, 36, 3] filter: number of filters per block, defined as a list. filters = [64, 128, 256, 512 width: width multiplier for the network (for Wide ResNets) bottleneck: adds a bottleneck conv to reduce computation weight_decay: weight decay (l2 norm) include_top: whether to include the fully-connected layer at the top of the network. weights: `None` (random initialization) or `imagenet` (trained on ImageNet) 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 `tf` dim ordering) or `(3, 224, 224)` (with `th` dim ordering). It should have exactly 3 inputs channels, and width and height should be no smaller than 8. 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. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') 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 len(depth) == len(filters), "The length of filter increment list must match the length " \ "of the depth list." # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=32, data_format=K.image_data_format(), require_flatten=False) if input_tensor is None: img_input = Input(shape=input_shape) else: if not is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = _create_se_resnet(classes, img_input, include_top, initial_conv_filters, filters, depth, width, bottleneck, weight_decay, pooling) # 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='resnext') # load weights return model