def make_model(env): num_frames = len(env.observation_space.spaces) height, width = env.observation_space.spaces[0].shape input_shape = height, width, num_frames # input state ph_state = layers.Input(shape=input_shape) # convolutional layers conv1 = layers.Conv2D(32, (8, 8), strides=(4, 4))(ph_state) conv1 = layers.Activation('relu')(conv1) conv2 = layers.Conv2D(64, (4, 4), strides=(2, 2))(conv1) conv2 = layers.Activation('relu')(conv2) conv3 = layers.Conv2D(64, (3, 3), strides=(1, 1))(conv2) conv3 = layers.Activation('relu')(conv3) conv_flat = layers.Flatten()(conv3) feature = layers.Dense(512)(conv_flat) feature = layers.Activation('relu')(feature) # actor (policy) and critic (value) streams size_logits = size_value = env.action_space.n logits_init = initializers.RandomNormal(stddev=1e-3) logits = layers.Dense(size_logits, kernel_initializer=logits_init)(feature) value = layers.Dense(size_value)(feature) return models.Model(inputs=ph_state, outputs=[logits, value])
def build(self, input_shape): if self.data_format == 'channels_first': channel_axis = 1 else: channel_axis = -1 if input_shape[channel_axis] is None: raise ValueError('The channel dimension of the inputs ' 'should be defined. Found `None`.') input_dim = int(input_shape[channel_axis]) kernel_shape = self.kernel_size + (input_dim, self.filters) self.kernel = self.add_weight(shape=kernel_shape, initializer=self.kernel_initializer, name='kernel', regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list()[-1]]), initializer=initializers.RandomNormal(0, 1), name='sn', trainable=False) if self.use_bias: self.bias = self.add_weight(shape=(self.filters,), initializer=self.bias_initializer, name='bias', regularizer=self.bias_regularizer, constraint=self.bias_constraint) else: self.bias = None # Set input spec. self.input_spec = InputSpec(ndim=self.rank + 2, axes={channel_axis: input_dim}) self.built = True
def resnet(num_blocks, img_input=None, classes=10, training=None): """Instantiates the ResNet architecture. Arguments: num_blocks: integer, the number of conv/identity blocks in each block. The ResNet contains 3 blocks with each block containing one conv block followed by (layers_per_block - 1) number of idenity blocks. Each conv/idenity block has 2 convolutional layers. With the input convolutional layer and the pooling layer towards the end, this brings the total size of the network to (6*num_blocks + 2) classes: optional number of classes to classify images into training: Only used if training keras model with Estimator. In other scenarios it is handled automatically. Returns: A Keras model instance. """ if backend.image_data_format() == 'channels_first': x = layers.Lambda(lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(img_input) bn_axis = 1 else: # channel_last x = img_input bn_axis = 3 x = layers.ZeroPadding2D(padding=(1, 1), name='conv1_pad')(x) x = layers.Conv2D(16, (3, 3), strides=(1, 1), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1',)(x, training=training) x = layers.Activation('relu')(x) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[16, 16], stage=2, conv_strides=(1, 1), training=training) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[32, 32], stage=3, conv_strides=(2, 2), training=training) x = resnet_block(x, size=num_blocks, kernel_size=3, filters=[64, 64], stage=4, conv_strides=(2, 2), training=training) rm_axes = [1, 2] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense(classes, activation='softmax', kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='fc10')(x) inputs = img_input # Create model. model = tf.keras.models.Model(inputs, x, name='resnet56') return model
def get_initializers(self, params): default_params = {'bias_param':0, 'reg': {'l1': 0, 'l2': 0}, 'group_l': {'l1': 0, 'l2': 0}} backends_li = ['keras', 'pytorch'] dist_dict = {'normal': {'mean': 0, 'stddev': 1}, 'uniform': {'minval': 0, 'maxval': 0}} for backend in backends_li:# prepares a nested default config dict() for dist, pars in dist_dict.items(): deep_set(default_params, ['backend', backend, 'distrib', dist], pars, accessor=lambda default_params, k: default_params.setdefault(k, dict())) self.default_backend_dist_params = default_params for key, vals in params.items(): sub_dict = default_params.copy() if key not in ['hyper_params', 'model_nas_params']: params[key].update({'bias_param':sub_dict['bias_param'] if 'bias_param' not in vals.keys() else params[key]['bias_param']})# else params[key]['bias_param']}) params[key].update({'bias':keras.initializers.Constant(value=params[key]['bias_param'])})#sub_dict['bias_param'] if 'bias' not in vals.keys() else params[key]['bias'])}) if 'regularizers' not in vals.keys(): params[key].update({'regularizers':sub_dict['reg']})#add default regularization if 'group_lasso' not in vals.keys(): params[key].update({'group_lasso':sub_dict['group_l']})#add default regularization if 'kernel_params' in vals.keys(): custom_params = vals['kernel_params'] if 'backend' not in custom_params.keys() or 'backend' == 'keras': rel_li = ['backend', 'keras', 'distrib', custom_params['distrib'] ] if 'distrib' in custom_params else ['backend', 'keras', 'distrib', 'normal'] rel_dict = deep_get(sub_dict, rel_li).copy() rel_dict.update(custom_params) params[key].update({'kernel': initializers.RandomNormal(mean=rel_dict['mean'], stddev=rel_dict['stddev']) if 'normal' in rel_li else initializers.RandomUniform(minval=rel_dict['minval'], maxval=rel_dict['maxval'])}) else:#for non-keras backend if not custom_params['backend'] in self.default_backend_dist_params['backend'].keys(): raise ValueError('Backend: {} and its distributions are not yet defined in Generalised Model'.format( custom_params['backend'])) rel_li = ['backend', custom_params['backend'], 'distrib', custom_params['distrib']] rel_dict = deep_get(sub_dict, rel_li).copy() rel_dict.update(custom_params) params[key].update({'kernel': initializers.RandomNormal(mean=rel_dict['mean'], stddev=rel_dict['stddev']) if 'normal' in rel_li else initializers.RandomUniform(minval=rel_dict['minval'], maxval=rel_dict['maxval'])}) self._model_params.update(params)
def __init__(self, rank, filters, kernel_size, strides=1, padding='valid', data_format=None, dilation_rate=1, activation=None, use_bias=True, use_wscale=False, lr_mul=1.0, kernel_initializer='glorot_uniform', bias_initializer='zeros', singular_vector_initializer=initializers.RandomNormal(0, 1), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, power_iter=1, trainable=True, name=None, **kwargs): super(SNConv, self).__init__( rank=rank, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=initializers.get(kernel_initializer), bias_initializer=initializers.get(bias_initializer), kernel_regularizer=regularizers.get(kernel_regularizer), bias_regularizer=regularizers.get(bias_regularizer), activity_regularizer=regularizers.get(activity_regularizer), kernel_constraint=constraints.get(kernel_constraint), bias_constraint=constraints.get(bias_constraint), name=name, **kwargs) self.use_wscale = use_wscale self.lr_mul = lr_mul self.singular_vector_initializer = singular_vector_initializer self.power_iter = power_iter self._trainable_var = None self.trainable = trainable
def build_model(num_classes, mode='normal', save_labels=False): if 'trivial' in mode: base_model = test_utils.trivial_model(num_classes) return base_model if 'resnet50' in mode: base_model = resnet_model.resnet50(num_classes) base_model = tf.keras.models.Model(inputs=base_model.input, outputs=base_model.layers[-3].output) x = tf.keras.layers.Dropout(0.5)(base_model.output) y = tf.keras.layers.Dense( 256, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(), bias_regularizer=_gen_l2_regularizer(), name='features' )(x) if 'features' not in mode: probs = tf.keras.layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(), bias_regularizer=_gen_l2_regularizer(), activation='softmax', name='logits' )(y) model = tf.keras.models.Model(inputs=base_model.input, outputs=probs, name='imagenet') else: model = tf.keras.models.Model(inputs=base_model.input, outputs=y, name='imagenet') if save_labels: label = tf.keras.layers.Input(shape=(),dtype=tf.int32) ori_label = tf.keras.layers.Input(shape=(),dtype=tf.int32) model = tf.keras.models.Model(inputs=[model.input,label,ori_label], outputs=[model.output,label,ori_label],name='imagenet') return model
def build(self, input_shape): self.embeddings = self.add_weight( shape=(self.input_dim, self.output_dim), initializer=self.embeddings_initializer, name='embeddings', regularizer=self.embeddings_regularizer, constraint=self.embeddings_constraint, dtype=self.dtype) self.u = self.add_weight(shape=tuple([1, self.embeddings.shape.as_list()[-1]]), initializer=initializers.RandomNormal(0, 1), name='sn', trainable=False) self.built = True
def trivial(num_classes, batch_size=None, use_l2_regularizer=True): input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) x = img_input if backend.image_data_format() == 'channels_first': x = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(x) bn_axis = 1 else: # channels_last bn_axis = 3 x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(x) x = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1')(x) rm_axes = [1, 2 ] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')(x) # A softmax that is followed by the model loss must be done cannot be done # in float16 due to numeric issues. So we pass dtype=float32. x = layers.Activation('softmax', dtype='float32')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def __init__(self, filters, kernel_size, strides=(1, 1, 1), padding='valid', data_format=None, dilation_rate=(1, 1, 1), activation=None, use_bias=True, use_wscale=False, lr_mul=1.0, kernel_initializer='glorot_uniform', bias_initializer='zeros', singular_vector_initializer=initializers.RandomNormal(0, 1), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, power_iter=1, **kwargs): super(SNConv3D, self).__init__( rank=3, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activations.get(activation), use_bias=use_bias, use_wscale=use_wscale, lr_mul=lr_mul, kernel_initializer=initializers.get(kernel_initializer), bias_initializer=initializers.get(bias_initializer), singular_vector_initializer=initializers.get( singular_vector_initializer), kernel_regularizer=regularizers.get(kernel_regularizer), bias_regularizer=regularizers.get(bias_regularizer), activity_regularizer=regularizers.get(activity_regularizer), kernel_constraint=constraints.get(kernel_constraint), bias_constraint=constraints.get(bias_constraint), power_iter=power_iter, **kwargs)
def make_model(env): original_matrix = np.genfromtxt(PATH_TO_CSV, delimiter=",", skip_header=39) stateCnt = original_matrix.shape[1] #input_shape = (10, stateCnt) # dataframe1 = pd.read_csv(PATH_TO_CSV, header=None, usecols=range(0,67), skiprows=39, engine='python') #dataframe.assign(s=dataframe.s.shift(-1)).drop(dataframe.index[-1]) #dataframe[dataframe.columns[-1]] = dataframe[dataframe.columns[-1]].shift(-1) # dataframe1 = dataframe1[-1000:] # dataset1 = dataframe1.values # dataset1 = dataset1.astype('float64') #UNCOMMENT THIS TO SCALE ACCORDING TO CURRENT DATA # from sklearn import preprocessing # scaler1 = preprocessing.MinMaxScaler() # X1 = dataset1[:,0:67] #scaler1.fit(X1) # X1 = scaler1.transform(X1) # input_shape = X1.reshape(-1,10,67) #input_shape = observation_space # input_shape = np.reshape(-1,10,67) # num_frames = len(env.observation_space.spaces) # height, width = env.observation_space.spaces[0].shape # input_shape = height, width, num_frames input_shape=(10, stateCnt) ph_state = layers.Input(shape=input_shape) # conv1 = layers.Conv2D(32, (8, 8), strides=(4, 4))(ph_state) conv1 = layers.Conv1D(filters=2, kernel_size=1)(ph_state) conv1 = layers.Activation('relu')(conv1) # conv2 = layers.Conv2D(64, (4, 4), strides=(2, 2))(conv1) # conv2 = layers.Activation('relu')(conv2) # conv3 = layers.Conv2D(64, (3, 3), strides=(1, 1))(conv2) # conv3 = layers.Activation('relu')(conv3) conv_flat = layers.Flatten()(conv1) feature = layers.Dense(512)(conv_flat) feature = layers.Activation('relu')(feature) # actor (policy) and critic (value) streams size_logits = 4 size_value = 4 logits_init = initializers.RandomNormal(stddev=1e-3) logits = layers.Dense(size_logits, kernel_initializer=logits_init)(feature) value = layers.Dense(size_value)(feature) return models.Model(inputs=ph_state, outputs=[logits, value])
def build(self, input_shape): assert len(input_shape) >= 2 input_dim = int(input_shape[-1]) self.kernel = self.add_weight(shape=(input_dim, self.units), initializer=self.kernel_initializer, name='kernel', regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) if self.use_bias: self.bias = self.add_weight(shape=(self.units,), initializer=self.bias_initializer, name='bias', regularizer=self.bias_regularizer, constraint=self.bias_constraint) else: self.bias = None self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list()[-1]]), initializer=initializers.RandomNormal(0, 1), name='sn', trainable=False) self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim}) self.built = True
def create_model(): model = tf.keras.Sequential() model.add(Input(shape=(32, 32, 3))) model.add(ConversionLayer()) model.add(ConvBuildingBlock(3, [16, 16], 2, 'block_0', (1, 1), True)) for i in range(2): model.add( IdentityBuildingBlock(3, [16, 16], 2, (1, 1), 'block_%d' % (i + 1), True)) model.add(ConvBuildingBlock(3, [32, 32], 3, 'block_0', (2, 2), True)) for i in range(2): model.add( IdentityBuildingBlock(3, [32, 32], 3, (2, 2), 'block_%d' % (i + 1), True)) model.add(ConvBuildingBlock(3, [64, 64], 4, 'block_0', (2, 2), True)) for i in range(2): model.add( IdentityBuildingBlock(3, [64, 64], 4, (2, 2), 'block_%d' % (i + 1), True)) model.add(Lambda(lambda x: backend.mean(x, [1, 2]))) model.add( Dense(100, activation='softmax', kernel_initializer=initializers.RandomNormal(stddev=0.01))) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy']) return model
def __init__(self, args, architecture='ResNet50', data='CIFAR10'): super().__init__(name=architecture) self.args = args if self.args.bit64: raise NotImplementedError() self.architecture = architecture self.data = data if data == 'CIFAR10': self.num_classes = 10 self.expected_shape = (32, 32, 3) elif data == 'ImageNet': self.num_classes = 1000 self.expected_shape = (224, 224, 3) self.flatten = layers.Flatten(name='features') self.dense = Dense( self.num_classes, activation=None, name='logits', kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY))
def __init__(self, units, activation=None, use_bias=True, use_wscale=False, lr_mul=1.0, kernel_initializer='glorot_uniform', bias_initializer='zeros', singular_vector_initializer=initializers.RandomNormal(0, 1), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, power_iter=1, trainable=True, **kwargs): super(SNDense, self).__init__( units=units, activation=activation, use_bias=use_bias, kernel_initializer=initializers.get(kernel_initializer), bias_initializer=initializers.get(bias_initializer), kernel_regularizer=regularizers.get(kernel_regularizer), bias_regularizer=regularizers.get(bias_regularizer), activity_regularizer=regularizers.get(activity_regularizer), kernel_constraint=constraints.get(kernel_constraint), bias_constraint=constraints.get(bias_constraint), **kwargs) self.use_wscale = use_wscale self.lr_mul = lr_mul self.singular_vector_initializer = singular_vector_initializer self.power_iter = power_iter self._trainable_var = None self.trainable = trainable
def resnet50(num_classes, batch_size=None, use_l2_regularizer=True, rescale_inputs=False): input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) if rescale_inputs: x = layers.Lambda( lambda x: x*255.0-backend.constant(imagenet_preprocessing.CHANNEL_MEANS, shape=[1,1,3], dtype=x.dtype, name='rescale')(img_input) ) else: x = img_input if backend.image_data_format()=='channels_first': x = layers.Lambda(lambda x:backend.permute_dimensions(x, (0,3,1,2)), name='tranpose')(x) bn_axis = 1 else: # channels_last bn_axis = 3 x = layers.ZeroPadding2D(padding=(3,3), name='conv1_pad')(x) # 在上下左右各填充3个尺寸 :230 x = layers.Conv2D(filters=64, kernel_size=(7,7), strides=(2,2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='conv1')(x) # :224 x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILOW, name='bn_conv1') x = layers.Activation('relu')(x) x = layers.MaxPool2D(pool_size=(3,3), strides=(2,2), padding='same')(x) # :224 x = conv_block(input_tensor=x, kernel_size=3, filters=[64,64,256], stage=2, block='a', strides=(1,1), use_l2_regualizer=use_l2_regularizer) # :224 x = identity_block(input_tensor=x, kernel_size=3, filters=[64,64,256], stage=2, block='b', use_l2_regularizer=use_l2_regularizer) # :224 x = identity_block(x, 3, [64,64,256], stage=2, block='c', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [128,128,512], stage=3, block='a', use_l2_regualizer=use_l2_regularizer) x = identity_block(x, 3, [128,128,256], stage=3, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128,128,256],stage=3, block='c', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128,128,256],stage=3, block='d', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, filters=[256,256,1024], stage=4, block='a', use_l2_regualizer=use_l2_regularizer) x = identity_block(x, 3, [256,256,1024], stage=4, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256,256,1024], stage=4, block='c', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256,256,1024], stage=4, block='d', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256,256,1024], stage=4, block='e', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256,256,1024], stage=4, block='f', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [512,512,2048], stage=5, block='a', use_l2_regualizer=use_l2_regularizer) x = identity_block(x, 3, [512,512,2048], stage=5, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [512,512,2048], stage=5, block='c', use_l2_regularizer=use_l2_regularizer) rm_axes = [1,2] if backend.image_data_format()=='channels_last' else [2,3] # 平均池化层 x = layers.Lambda(lambda x:backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense(units=num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')(x) x = layers.Activation('softmax', dtype='float32')(x) return models.Model(img_input, x, name='resnet50')
def resnet50(num_classes, batch_size=None, use_l2_regularizer=True, rescale_inputs=False, batch_norm_decay=0.9, batch_norm_epsilon=1e-5): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. batch_size: Size of the batches for each step. use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer. rescale_inputs: whether to rescale inputs from 0 to 1. batch_norm_decay: Moment of batch norm layers. batch_norm_epsilon: Epsilon of batch borm layers. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) if rescale_inputs: # Hub image modules expect inputs in the range [0, 1]. This rescales these # inputs to the range expected by the trained model. x = layers.Lambda( lambda x: x * 255.0 - backend.constant( imagenet_preprocessing.CHANNEL_MEANS, shape=[1, 1, 3], dtype=x.dtype), name='rescale')( img_input) else: x = img_input if backend.image_data_format() == 'channels_first': x = layers.Permute((3, 1, 2))(x) bn_axis = 1 else: # channels_last bn_axis = 3 block_config = dict( use_l2_regularizer=use_l2_regularizer, batch_norm_decay=batch_norm_decay, batch_norm_epsilon=batch_norm_epsilon) x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(x) x = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='conv1')( x) x = layers.BatchNormalization( axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv1')( x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = conv_block( x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), **block_config) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', **block_config) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', **block_config) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', **block_config) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', **block_config) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', **block_config) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', **block_config) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', **block_config) x = layers.GlobalAveragePooling2D()(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')( x) # A softmax that is followed by the model loss must be done cannot be done # in float16 due to numeric issues. So we pass dtype=float32. x = layers.Activation('softmax', dtype='float32')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def resnet50(num_classes=1000, batch_size=None, use_l2_regularizer=True, rescale_inputs=False, batch_norm_decay=0.9, batch_norm_epsilon=1e-5): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. batch_size: Size of the batches for each step. use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer. rescale_inputs: whether to rescale inputs from 0 to 1. batch_norm_decay: Moment of batch norm layers. batch_norm_epsilon: Epsilon of batch norm layers. Returns: A Keras model instance. """ # no surprise here input_shape = (224, 224, 3) # we use here functional layer API # and we use Input instead of InputLayer as recommended # what is Input? basically this is a part of functional API that # produces InputLayer under the hood # the only parameter we have to provide - input_shape img_input = layers.Input( shape=input_shape, # never seen before - batch size in the model itself batch_size=batch_size) if rescale_inputs: # Hub image modules expect inputs in the range [0, 1]. This rescales these # inputs to the range expected by the trained model. # why do we use those CHANNEL_MEANS? # well - its from ImageNet stat; # why do we need to rescale images in [0, 255]? why don't we use std? x = layers.Lambda( # so what's going on here? # 1) we create a constant: <tf.Tensor: shape=(1, 1, 3), dtype=float32, # numpy=array([[[123.68, 116.78, 103.94]]], dtype=float32)> # 2) we subtract this constant from each channel using broadcasting # so we per channel normalization as expected lambda x: x * 255.0 - backend.constant( CHANNEL_MEANS, shape=[1, 1, 3], dtype=x.dtype), name='rescale')(img_input) else: x = img_input if backend.image_data_format() == 'channels_first': # very strange permutation: [bs, 224, 3, 224] # why do we need this? x = layers.Permute((3, 1, 2))(x) bn_axis = 1 else: # channels_last bn_axis = 3 # why do we set all these params? aren't they default? # batch_norm_epsilon: we use 1e-5, default 1e-3 - so in fact we use different values # why do we use them? block_config = dict(use_l2_regularizer=use_l2_regularizer, batch_norm_decay=batch_norm_decay, batch_norm_epsilon=batch_norm_epsilon) x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(x) x = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=batch_norm_decay, epsilon=batch_norm_epsilon, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # so we may see here that we have (*3, *4, *6, *3): # - one conv block when skip connection includes conv layer; # - a few identity blocks; # they don't use any loops, just arrange all layers manually x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), **block_config) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', **block_config) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', **block_config) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', **block_config) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', **block_config) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', **block_config) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', **block_config) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', **block_config) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', **block_config) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', **block_config) x = layers.GlobalAveragePooling2D()(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')(x) # A softmax that is followed by the model loss must be done cannot be done # in float16 due to numeric issues. So we pass dtype=float32. x = layers.Activation('softmax', dtype='float32')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def ResNet50(num_classes): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. Returns: A Keras model instance. """ # Determine proper input shape if backend.image_data_format() == 'channels_first': input_shape = (3, 224, 224) bn_axis = 1 else: input_shape = (224, 224, 3) bn_axis = 3 img_input = layers.Input(shape=input_shape) x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input) x = layers.Conv2D(64, (7, 7), use_bias=False, strides=(2, 2), padding='valid', kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(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') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') 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 = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(num_classes, activation='softmax', kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='fc1000')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def resnet50(num_classes, batch_size=None, use_l2_regularizer=True, rescale_inputs=False): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. batch_size: Size of the batches for each step. use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer. rescale_inputs: whether to rescale inputs from 0 to 1. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) if rescale_inputs: # Hub image modules expect inputs in the range [0, 1]. This rescales these # inputs to the range expected by the trained model. x = layers.Lambda( lambda x: x * 255.0 - backend.constant(imagenet_preprocessing. CHANNEL_MEANS, shape=[1, 1, 3], dtype=x.dtype), name="rescale", )(img_input) else: x = img_input if backend.image_data_format() == "channels_first": x = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name="transpose")(x) bn_axis = 1 else: # channels_last bn_axis = 3 x = layers.ZeroPadding2D(padding=(3, 3), name="conv1_pad")(x) x = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding="valid", use_bias=False, kernel_initializer="he_normal", kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name="conv1", )(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name="bn_conv1")(x) x = layers.Activation("relu")(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x) x = conv_block( x, 3, [64, 64, 256], stage=2, block="a", strides=(1, 1), use_l2_regularizer=use_l2_regularizer, ) x = identity_block(x, 3, [64, 64, 256], stage=2, block="b", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [64, 64, 256], stage=2, block="c", use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [128, 128, 512], stage=3, block="a", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block="b", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block="c", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block="d", use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [256, 256, 1024], stage=4, block="a", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block="b", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block="c", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block="d", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block="e", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block="f", use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [512, 512, 2048], stage=5, block="a", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [512, 512, 2048], stage=5, block="b", use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [512, 512, 2048], stage=5, block="c", use_l2_regularizer=use_l2_regularizer) rm_axes = [1, 2 ] if backend.image_data_format() == "channels_last" else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name="reduce_mean")(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name="fc1000", )(x) # A softmax that is followed by the model loss must be done cannot be done # in float16 due to numeric issues. So we pass dtype=float32. x = layers.Activation("softmax", dtype="float32")(x) # Create model. return models.Model(img_input, x, name="resnet50")
def resnet18(num_classes, dtype='float32', batch_size=None): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. dtype: dtype to use float32 or float16 are most common. batch_size: Size of the batches for each step. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, dtype=dtype, batch_size=batch_size) if backend.image_data_format() == 'channels_first': x = layers.Lambda(lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(img_input) bn_axis = 1 else: # channels_last x = img_input bn_axis = 3 x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(x) x = layers.Conv2D(64, (7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = conv_block_2(x, 3, [64, 64], stage=2, block='a', strides=(1, 1)) x = identity_block_2(x, 3, [64, 64], stage=2, block='b') x = conv_block_2(x, 3, [128, 128], stage=3, block='a') x = identity_block_2(x, 3, [128, 128], stage=3, block='b') x = conv_block_2(x, 3, [256, 256], stage=4, block='a') x = identity_block_2(x, 3, [256, 256], stage=4, block='b') x = conv_block_2(x, 3, [512, 512], stage=5, block='a') x = identity_block_2(x, 3, [512, 512], stage=5, block='b') rm_axes = [1, 2] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name='fc1000')(x) # TODO(reedwm): Remove manual casts once mixed precision can be enabled with a # single line of code. x = backend.cast(x, 'float32') x = layers.Activation('softmax')(x) # Create model. return models.Model(img_input, x, name='resnet50')
def resnet50(num_classes, batch_size=None, use_l2_regularizer=True): """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. batch_size: Size of the batches for each step. use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape, batch_size=batch_size) if backend.image_data_format() == 'channels_first': x = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name='transpose')(img_input) bn_axis = 1 else: # channels_last x = img_input bn_axis = 3 x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(x) x = layers.Conv2D( 64, (7, 7), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='conv1')(x) x = layers.BatchNormalization(axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name='bn_conv1')(x) x = layers.Activation('relu')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f', use_l2_regularizer=use_l2_regularizer) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', use_l2_regularizer=use_l2_regularizer) x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', use_l2_regularizer=use_l2_regularizer) rm_axes = [1, 2 ] if backend.image_data_format() == 'channels_last' else [2, 3] x = layers.Lambda(lambda x: backend.mean(x, rm_axes), name='reduce_mean')(x) x = layers.Dense( num_classes, kernel_initializer=initializers.RandomNormal(stddev=0.01), kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer), bias_regularizer=_gen_l2_regularizer(use_l2_regularizer), name='fc1000')(x) # A softmax that is followed by the model loss must be done cannot be done # in float16 due to numeric issues. So we pass dtype=float32. x = layers.Activation('softmax', dtype='float32')(x) # Create model. return models.Model(img_input, x, name='resnet50')