def testSupports_KerasLayer(self): self.assertTrue(self.quantize_registry.supports(l.Dense(10))) self.assertTrue(self.quantize_registry.supports(l.Conv2D(10, (2, 2))))
model.add(layers.Dense(120, activation='relu')) model.add(layers.Dense(84, activation='relu')) model.add(layers.Dense(16, activation='relu')) model.add(layers.Dense(4, activation='softmax')) model.summary() """ model = Sequential([ layers.Conv2D(32, 5, activation='relu', input_shape=(32, 32, 1)), layers.MaxPooling2D(2), layers.Conv2D(16, 5, activation='relu'), layers.MaxPooling2D(2), layers.Flatten(), layers.Dense(120, activation='relu'), layers.Dense(84, activation='relu'), layers.Dense(16, activation='relu'), layers.Dense(4, activation='softmax') ]) model.summary() # model parameter info, ceil > 올림 epoch_step = np.ceil(len(train_files) / batch_size) val_step = np.ceil(len(val_files) / batch_size) test_step = np.ceil(len(test_files) / batch_size) epoch_step = int(epoch_step) val_step = int(val_step) test_step = int(test_step)
model.add( layers.LSTM(1000, dropout=0.1, recurrent_dropout=0.1, return_sequences=True)) model.add( layers.LSTM(1000, dropout=0.1, recurrent_dropout=0.1, return_sequences=True)) model.add( layers.LSTM(1000, dropout=0.1, recurrent_dropout=0.1, return_sequences=True)) model.add(layers.LSTM(1000, dropout=0.1, recurrent_dropout=0.1)) model.add(layers.Dense(5, activation="softmax")) model.summary() model.load_weights(model_file) model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=0.0001, decay=0.0001), metrics=['accuracy']) print(model.model.metrics_names) score = model.evaluate_generator(test_gen, steps=3000) raw_labels.close() raw_features.close() print(score)
def __init__(self, units, use_bias=True, use_bn=False, dropout=0, activations=None, kernel_initializers='glorot_uniform', bias_initializers='zeros', kernel_regularizers=tf.keras.regularizers.l2(1e-5), bias_regularizers=None, **kwargs): """ :param units: An iterable of hidden layers' neural units' number, its length is the depth of the DNN. :param use_bias: Iterable/Boolean. If this is not iterable, every layer of the DNN will have the same param, the same below. :param activations: Iterable/String/TF activation class :param kernel_initializers: Iterable/String/TF initializer class :param bias_initializers: Iterable/String/TF initializer class :param kernel_regularizers: Iterable/String/TF regularizer class :param bias_regularizers: Iterable/String/TF regularizer class """ super(DNN, self).__init__(**kwargs) self.units = units self.use_bias = use_bias self.use_bn = use_bn self.dropout = dropout self.activations = activations self.kernel_initializers = kernel_initializers self.bias_initializers = bias_initializers self.kernel_regularizers = kernel_regularizers self.bias_regularizers = bias_regularizers if not isinstance(self.use_bias, Iterable): self.use_bias = [self.use_bias] * len(self.units) if not isinstance(self.use_bn, Iterable): self.use_bn = [self.use_bn] * len(self.units) if not isinstance(self.dropout, Iterable): self.dropout = [self.dropout] * len(self.units) if not isinstance(self.activations, Iterable): self.activations = [self.activations] * len(self.units) if isinstance(self.kernel_initializers, str) or not isinstance(self.kernel_initializers, Iterable): self.kernel_initializers = [self.kernel_initializers] * len(self.units) if isinstance(self.bias_initializers, str) or not isinstance(self.bias_initializers, Iterable): self.bias_initializers = [self.bias_initializers] * len(self.units) if isinstance(self.kernel_regularizers, str) or not isinstance(self.kernel_regularizers, Iterable): self.kernel_regularizers = [self.kernel_regularizers] * len(self.units) if isinstance(self.bias_regularizers, str) or not isinstance(self.bias_regularizers, Iterable): self.bias_regularizers = [self.bias_regularizers] * len(self.units) self.mlp = tf.keras.Sequential() for i in range(len(self.units)): self.mlp.add(layers.Dense( units=self.units[i], activation=self.activations[i], use_bias=self.use_bias[i], kernel_initializer=self.kernel_initializers[i], bias_initializer=self.bias_initializers[i], kernel_regularizer=self.kernel_regularizers[i], bias_regularizer=self.bias_regularizers[i] )) if self.dropout[i] > 0: self.mlp.add(layers.Dropout(self.dropout[i])) if self.use_bn[i]: self.mlp.add(layers.BatchNormalization())
def MobileNetV2(input_shape=None, alpha=1.0): if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [96, 128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=True) row_axis = 0 if backend.image_data_format() == 'channels_last' else 1 rows = input_shape[row_axis] img_input = layers.Input(shape=input_shape) channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 first_block_filters = _make_divisible(32 * alpha, 8) x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(img_input, 3), name='Conv1_pad')(img_input) x = layers.Conv2D(first_block_filters, kernel_size=3, strides=(2, 2), padding='valid', use_bias=False, name='Conv1')(x) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x) x = layers.ReLU(6., name='Conv1_relu')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, expansion=6, block_id=6) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=7) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=8) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=9) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=10) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=11) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=12) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, expansion=6, block_id=13) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=14) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=15) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, expansion=6, block_id=16) # no alpha applied to last conv as stated in the paper: # if the width multiplier is greater than 1 we # increase the number of output channels if alpha > 1.0: last_block_filters = _make_divisible(1280 * alpha, 8) else: last_block_filters = 1280 x = layers.Conv2D(last_block_filters, kernel_size=1, use_bias=False, name='Conv_1')(x) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x) x = layers.ReLU(6., name='out_relu')(x) x = layers.GlobalAveragePooling2D()(x) imagenet_utils.validate_activation('softmax', None) x = layers.Dense(NUM_CLASSES, activation='softmax', name='predictions')(x) # Create model. model = training.Model(img_input, x, name='mobilenetv2_%0.2f_%s' % (alpha, rows)) return model
def build(self, input_shape): self.layer_a = layers.Dense(self.num_hidden, activation='relu') activation = 'sigmoid' if self.num_classes == 1 else 'softmax' self.layer_b = layers.Dense(self.num_classes, activation=activation)
batch(model_parameters.batch_size) dataset = generate_samples(num_samples=500000) def validation_dataset(): return tf.random.normal( [model_parameters.batch_size, model_parameters.latent_size]) validation_dataset = validation_dataset() generator = sequential.SequentialModel(layers=[ keras.Input(shape=[model_parameters.latent_size]), layers.Dense(units=15), layers.ELU(), layers.Dense(units=10), layers.ELU(), layers.Dense(units=5), layers.ELU(), layers.Dense(units=2, activation='linear'), ]) discriminator = sequential.SequentialModel([ keras.Input(shape=[2]), layers.Dense(units=25, activation='relu'), layers.Dense(units=15, activation='relu'), layers.Dense(units=10, activation='relu'), layers.Dense(units=2, activation='sigmoid'), ])
def _model_fn(): x = layers.Input(shape=(1, ), name='input') y = layers.Dense(1, name='dense')(x) model = training.Model(x, y) return model
def EfficientNet( width_coefficient, depth_coefficient, default_size, dropout_rate=0.2, drop_connect_rate=0.2, depth_divisor=8, activation='swish', blocks_args='default', model_name='efficientnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax', ): """Instantiates the EfficientNet architecture using given scaling coefficients. 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: width_coefficient: float, scaling coefficient for network width. depth_coefficient: float, scaling coefficient for network depth. default_size: integer, default input image size. dropout_rate: float, dropout rate before final classifier layer. drop_connect_rate: float, dropout rate at skip connections. depth_divisor: integer, a unit of network width. activation: activation function. blocks_args: list of dicts, parameters to construct block modules. model_name: string, model name. 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. 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. classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Returns: A `keras.Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. ValueError: if `classifier_activation` is not `softmax` or `None` when using a pretrained top layer. """ if blocks_args == 'default': blocks_args = DEFAULT_BLOCKS_ARGS if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) 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 def round_filters(filters, divisor=depth_divisor): """Round number of filters based on depth multiplier.""" filters *= width_coefficient new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor) # Make sure that round down does not go down by more than 10%. if new_filters < 0.9 * filters: new_filters += divisor return int(new_filters) def round_repeats(repeats): """Round number of repeats based on depth multiplier.""" return int(math.ceil(depth_coefficient * repeats)) # Build stem x = img_input x = layers.Rescaling(1. / 255.)(x) x = layers.Normalization(axis=bn_axis)(x) x = layers.ZeroPadding2D( padding=imagenet_utils.correct_pad(x, 3), name='stem_conv_pad')(x) x = layers.Conv2D( round_filters(32), 3, strides=2, padding='valid', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='stem_conv')(x) x = layers.BatchNormalization(axis=bn_axis, name='stem_bn')(x) x = layers.Activation(activation, name='stem_activation')(x) # Build blocks blocks_args = copy.deepcopy(blocks_args) b = 0 blocks = float(sum(args['repeats'] for args in blocks_args)) for (i, args) in enumerate(blocks_args): assert args['repeats'] > 0 # Update block input and output filters based on depth multiplier. args['filters_in'] = round_filters(args['filters_in']) args['filters_out'] = round_filters(args['filters_out']) for j in range(round_repeats(args.pop('repeats'))): # The first block needs to take care of stride and filter size increase. if j > 0: args['strides'] = 1 args['filters_in'] = args['filters_out'] x = block( x, activation, drop_connect_rate * b / blocks, name='block{}{}_'.format(i + 1, chr(j + 97)), **args) b += 1 # Build top x = layers.Conv2D( round_filters(1280), 1, padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='top_conv')(x) x = layers.BatchNormalization(axis=bn_axis, name='top_bn')(x) x = layers.Activation(activation, name='top_activation')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) if dropout_rate > 0: x = layers.Dropout(dropout_rate, name='top_dropout')(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Dense( classes, activation=classifier_activation, kernel_initializer=DENSE_KERNEL_INITIALIZER, name='predictions')(x) else: 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 # Create model. model = training.Model(inputs, x, name=model_name) # Load weights. if weights == 'imagenet': if include_top: file_suffix = '.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][0] else: file_suffix = '_notop.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][1] file_name = model_name + file_suffix weights_path = data_utils.get_file( file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def __init__(self, num_classes=10, dtype="float32", batch_size=None): super(CustomModel, self).__init__(name="resnet50") if backend.image_data_format() == "channels_first": self._lambda = layers.Lambda( lambda x: backend.permute_dimensions(x, (0, 3, 1, 2)), name="transpose", ) bn_axis = 1 data_format = "channels_first" else: bn_axis = 3 data_format = "channels_last" self._padding = layers.ZeroPadding2D( padding=(3, 3), data_format=data_format, name="zero_pad" ) self._conv2d_1 = 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", ) self._bn_1 = layers.BatchNormalization( axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, name="bn_conv1", ) self._activation_1 = layers.Activation("relu") self._maxpooling2d = layers.MaxPooling2D( (3, 3), strides=(2, 2), padding="same" ) self._conv_block_1 = ConvBlock( 3, [64, 64, 256], stage=2, block="a", strides=(1, 1) ) self._identity_block_1 = IdentityBlock( 3, [64, 64, 256], stage=2, block="b" ) self._identity_block_2 = IdentityBlock( 3, [64, 64, 256], stage=2, block="c" ) self._conv_block_2 = ConvBlock(3, [128, 128, 512], stage=3, block="a") self._identity_block_3 = IdentityBlock( 3, [128, 128, 512], stage=3, block="b" ) self._identity_block_4 = IdentityBlock( 3, [128, 128, 512], stage=3, block="c" ) self._identity_block_5 = IdentityBlock( 3, [128, 128, 512], stage=3, block="d" ) self._conv_block_3 = ConvBlock(3, [256, 256, 1024], stage=4, block="a") self._identity_block_6 = IdentityBlock( 3, [256, 256, 1024], stage=4, block="b" ) self._identity_block_7 = IdentityBlock( 3, [256, 256, 1024], stage=4, block="c" ) self._identity_block_8 = IdentityBlock( 3, [256, 256, 1024], stage=4, block="d" ) self._identity_block_9 = IdentityBlock( 3, [256, 256, 1024], stage=4, block="e" ) self._identity_block_10 = IdentityBlock( 3, [256, 256, 1024], stage=4, block="f" ) self._conv_block_4 = ConvBlock(3, [512, 512, 2048], stage=5, block="a") self._identity_block_11 = IdentityBlock( 3, [512, 512, 2048], stage=5, block="b" ) self._identity_block_12 = IdentityBlock( 3, [512, 512, 2048], stage=5, block="c" ) rm_axes = ( [1, 2] if backend.image_data_format() == "channels_last" else [2, 3] ) self._lamba_2 = layers.Lambda( lambda x: backend.mean(x, rm_axes), name="reduce_mean" ) self._dense = layers.Dense( num_classes, kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY), bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY), name="fc1000", ) self._activation_2 = layers.Activation("softmax")
def _testDynamicDecodeRNN(self, time_major, has_attention, with_alignment_history=False): encoder_sequence_length = np.array([3, 2, 3, 1, 1]) decoder_sequence_length = np.array([2, 0, 1, 2, 3]) batch_size = 5 decoder_max_time = 4 input_depth = 7 cell_depth = 9 attention_depth = 6 vocab_size = 20 end_token = vocab_size - 1 start_token = 0 embedding_dim = 50 max_out = max(decoder_sequence_length) output_layer = layers.Dense(vocab_size, use_bias=True, activation=None) beam_width = 3 with self.cached_session(): batch_size_tensor = constant_op.constant(batch_size) embedding = np.random.randn(vocab_size, embedding_dim).astype(np.float32) cell = rnn_cell.LSTMCell(cell_depth) initial_state = cell.zero_state(batch_size, dtypes.float32) coverage_penalty_weight = 0.0 if has_attention: coverage_penalty_weight = 0.2 inputs = array_ops.placeholder_with_default( np.random.randn(batch_size, decoder_max_time, input_depth).astype(np.float32), shape=(None, None, input_depth)) tiled_inputs = beam_search_decoder.tile_batch( inputs, multiplier=beam_width) tiled_sequence_length = beam_search_decoder.tile_batch( encoder_sequence_length, multiplier=beam_width) attention_mechanism = attention_wrapper.BahdanauAttention( num_units=attention_depth, memory=tiled_inputs, memory_sequence_length=tiled_sequence_length) initial_state = beam_search_decoder.tile_batch( initial_state, multiplier=beam_width) cell = attention_wrapper.AttentionWrapper( cell=cell, attention_mechanism=attention_mechanism, attention_layer_size=attention_depth, alignment_history=with_alignment_history) cell_state = cell.zero_state(dtype=dtypes.float32, batch_size=batch_size_tensor * beam_width) if has_attention: cell_state = cell_state.clone(cell_state=initial_state) bsd = beam_search_decoder.BeamSearchDecoderV2( cell=cell, beam_width=beam_width, output_layer=output_layer, length_penalty_weight=0.0, coverage_penalty_weight=coverage_penalty_weight, output_time_major=time_major, maximum_iterations=max_out) final_outputs, final_state, final_sequence_lengths = bsd( embedding, start_tokens=array_ops.fill([batch_size_tensor], start_token), end_token=end_token, initial_state=cell_state) def _t(shape): if time_major: return (shape[1], shape[0]) + shape[2:] return shape self.assertIsInstance( final_outputs, beam_search_decoder.FinalBeamSearchDecoderOutput) self.assertIsInstance(final_state, beam_search_decoder.BeamSearchDecoderState) beam_search_decoder_output = final_outputs.beam_search_decoder_output expected_seq_length = 3 if context.executing_eagerly() else None self.assertEqual( _t((batch_size, expected_seq_length, beam_width)), tuple(beam_search_decoder_output.scores.get_shape().as_list())) self.assertEqual( _t((batch_size, expected_seq_length, beam_width)), tuple(final_outputs.predicted_ids.get_shape().as_list())) self.evaluate(variables.global_variables_initializer()) eval_results = self.evaluate({ 'final_outputs': final_outputs, 'final_sequence_lengths': final_sequence_lengths }) max_sequence_length = np.max( eval_results['final_sequence_lengths']) # A smoke test self.assertEqual( _t((batch_size, max_sequence_length, beam_width)), eval_results['final_outputs'].beam_search_decoder_output. scores.shape) self.assertEqual( _t((batch_size, max_sequence_length, beam_width)), eval_results['final_outputs'].beam_search_decoder_output. predicted_ids.shape)
def resnet50(num_classes): # TODO(tfboyd): add training argument, just lik resnet56. """Instantiates the ResNet50 architecture. Args: num_classes: `int` number of classes for image classification. Returns: A Keras model instance. """ input_shape = (224, 224, 3) img_input = layers.Input(shape=input_shape) 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.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x) x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') 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_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 get_quantize_inputs_test_model(input_shapes): # (1) (2) (3) (4) (5) # | | | | |-----\ # (conv1) (MP) (MP) (MP) (MP) | # | | | | | | | # | | (+) | | | # | |--\ | | | | # | | \ | | | | # | (conv2) | (conv3) | | | # | | | | \ / | # | (AvP) \ | (cat) | # | | \ | | | # (conv4) (linear) \ | (conv6) | # | | (cat) | | # | | | (+)------/ # | | (conv5) | # (AvP) | | | # | | (AvP) | # \ | / | # \---(cat)---------------/ # | # (dense) inputs = [] for i, input_shape in enumerate(input_shapes): inputs.append( tf.keras.Input(shape=input_shape[1:], name='input_{}'.format(i + 1))) # pylint: disable=unbalanced-tuple-unpacking input_1, input_2, input_3, input_4, input_5 = inputs conv1 = layers.Conv2D(filters=8, kernel_size=3) conv2 = layers.Conv2D(filters=8, kernel_size=3) conv3 = layers.Conv2D(filters=8, kernel_size=3) conv4 = layers.Conv2D(filters=16, kernel_size=3) conv5 = layers.Conv2D(filters=3, kernel_size=1) conv6 = layers.Conv2D(filters=3, kernel_size=2) dense = layers.Dense(8) x_1 = layers.Rescaling(1. / 255.)(input_1) x_1 = conv1(x_1) x_1 = conv4(x_1) x_1 = layers.GlobalAveragePooling2D()(x_1) x_1 = layers.Flatten()(x_1) x_2_br = layers.MaxPool2D(pool_size=2)(input_2) x_2 = conv2(x_2_br) x_2 = layers.GlobalAveragePooling2D()(x_2) x_2 = layers.Flatten()(x_2) x_2 = dense(x_2) x_3 = layers.MaxPool2D(pool_size=2)(input_3) x_3 = x_3 + x_3 x_3 = conv3(x_3) x_3 = layers.Flatten()(x_3) x_2_br = layers.Flatten()(x_2_br) x_3 = tf.concat([x_2_br, x_3], -1) x_3 = conv5(tf.expand_dims(tf.expand_dims(x_3, -1), -1)) x_3 = layers.GlobalAveragePooling2D()(x_3) x_3 = layers.Flatten()(x_3) x_4 = layers.MaxPool2D(pool_size=2)(input_4) x_5 = layers.MaxPool2D(pool_size=2)(input_5) x_45 = tf.concat([x_4, x_5], -1) x_45 = conv6(x_45) x_45 = layers.Flatten()(x_45) in_5_flat = layers.Flatten()(input_5) # pylint: disable=E1120 x_45 = tf.pad(x_45, [[0, 0], [0, in_5_flat.shape[1] - x_45.shape[1]]]) x_45 += in_5_flat x = tf.concat([x_1, x_2, x_3, x_45], -1) outputs = layers.Dense(10)(x) return tf.keras.Model(inputs=inputs, outputs=outputs)
def ResNet(stack_fn, use_bias, model_name='resnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, deep_stem=False, stem_width=None, classes=1000, classifier_activation='softmax', **kwargs): """Instantiates the ResNeSt, ResNeXt, and Wide ResNet 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`. Caution: Be sure to properly pre-process your inputs to the application. Please see `applications.resnet.preprocess_input` for an example. Arguments: stack_fn: a function that returns output tensor for the stacked residual blocks. 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. 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. 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. classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. **kwargs: For backwards compatibility only. Returns: A `keras.Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. ValueError: if `classifier_activation` is not `softmax` or `None` when using a pretrained top layer. """ if 'layers' in kwargs: global layers layers = kwargs.pop('layers') if kwargs: raise ValueError('Unknown argument(s): %s' % (kwargs, )) if not (weights in {'imagenet', 'ssl', 'swsl', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), `ssl` ' '(semi-supervised), `swsl` ' '(semi-weakly supervised), ' 'or the path to the weights file to be loaded.') if (weights == 'imagenet' or weights == 'ssl' or weights == 'swsl') and include_top and classes != 1000: raise ValueError('If using `weights` as `"imagenet"`, ' 'or `weights` as `"ssl"`, ' 'or `weights` as `"swsl"`, ' 'with `include_top` ' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) 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 if deep_stem: # Deep stem based off of ResNet-D x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv1_0_pad')(img_input) x = layers.Conv2D(stem_width, 3, strides=2, use_bias=use_bias, name='conv1_0_conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_0_bn')(x) x = layers.Activation('relu', name='conv1_0_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv1_1_pad')(x) x = layers.Conv2D(stem_width, 3, strides=1, use_bias=use_bias, name='conv1_1_conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_1_bn')(x) x = layers.Activation('relu', name='conv1_1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv1_2_pad')(x) x = layers.Conv2D(stem_width * 2, 3, strides=1, use_bias=use_bias, name='conv1_2_conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_2_bn')(x) x = layers.Activation('relu', name='conv1_2_relu')(x) else: x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Dense(classes, activation=classifier_activation, name='predictions')(x) else: 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 # Create model. model = training.Model(inputs, x, name=model_name) # Load weights. global BASE_WEIGHTS_PATH if 'resnest' in model_name: BASE_WEIGHTS_PATH += 'v0.2.0/' elif 'resnet' in model_name and 'wide' not in model_name: BASE_WEIGHTS_PATH += 'v0.3.0/' else: BASE_WEIGHTS_PATH += 'v0.1.0/' if (weights == 'imagenet') and (model_name in IMAGENET_WEIGHTS_HASHES): if include_top: file_name = model_name + '_imagenet_top.h5' file_hash = IMAGENET_WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_imagenet_notop.h5' file_hash = IMAGENET_WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif (weights == 'ssl') and (model_name in SSL_WEIGHTS_HASHES): if include_top: file_name = model_name + '_ssl_top.h5' file_hash = IMAGENET_WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_ssl_notop.h5' file_hash = IMAGENET_WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif (weights == 'swsl') and (model_name in SWSL_WEIGHTS_HASHES): if include_top: file_name = model_name + '_swsl_top.h5' file_hash = SWSL_WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_swsl_notop.h5' file_hash = SWSL_WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def resnet(num_blocks, 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. """ input_shape = (32, 32, 3) img_input = layers.Input(shape=input_shape) 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='he_normal', kernel_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
model.add(layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPooling2D(3, 3)) model.add(layers.Conv2D(16, (3, 3), activation='relu')) model.add(layers.MaxPooling2D(2, 2)) model.add(layers.Conv2D(16, (2, 2), activation='relu')) # summary for convolution section print("CONVOLUTION SUMMARY") model.summary() """ The output shape of each layer so far has been 3D. You can flatten it by adding a flatten layer, which will convert the model to a 1D vector. Then, dense layers are addded in which the number of parameters is the product of the input dimensions and output dimensions -- every possible connection between nodes exists and is differentiable """ model.add(layers.Flatten()) model.add(layers.Dense(32, activation='relu')) # the final layer should have dimension of 10, since there are 10 possible classifcations model.add(layers.Dense(10, activation='softmax')) print("COMPLETE SUMMARY") model.summary() #%% model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) train_pics = train_pics.reshape((10000, 28, 28, 1)) model.fit(train_pics, train_labels, epochs=3) #%% # evalutate against test data test_pics = test_pics.reshape((1000, 28, 28, 1))
def get_small_functional_mlp(num_hidden, num_classes, input_dim): inputs = layers.Input(shape=(input_dim, )) outputs = layers.Dense(num_hidden, activation='relu')(inputs) activation = 'sigmoid' if num_classes == 1 else 'softmax' outputs = layers.Dense(num_classes, activation=activation)(outputs) return models.Model(inputs, outputs)
def MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000, **kwargs): """Instantiates the MobileNetV2 architecture. Reference paper: - [MobileNetV2: Inverted Residuals and Linear Bottlenecks] (https://arxiv.org/abs/1801.04381) (CVPR 2018) Optionally loads weights pre-trained on ImageNet. Arguments: input_shape: Optional shape tuple, to be specified if you would like to use a model with an input image resolution that is not (224, 224, 3). It should have exactly 3 inputs channels (224, 224, 3). You can also omit this option if you would like to infer input_shape from an input_tensor. If you choose to include both input_tensor and input_shape then input_shape will be used if they match, if the shapes do not match then we will throw an error. E.g. `(160, 160, 3)` would be one valid value. alpha: Float between 0 and 1. controls the width of the network. This is known as the width multiplier in the MobileNetV2 paper, but the name is kept for consistency with `applications.MobileNetV1` model in Keras. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. include_top: Boolean, whether to include the fully-connected layer at the top of the network. Defaults to `True`. weights: String, 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. pooling: String, 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: Integer, optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. **kwargs: For backwards compatibility only. Returns: A `keras.Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape or invalid alpha, rows when weights='imagenet' """ if 'layers' in kwargs: global layers layers = kwargs.pop('layers') if kwargs: raise ValueError('Unknown argument(s): %s' % (kwargs, )) if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top` ' 'as true, `classes` should be 1000') # Determine proper input shape and default size. # If both input_shape and input_tensor are used, they should match if input_shape is not None and input_tensor is not None: try: is_input_t_tensor = backend.is_keras_tensor(input_tensor) except ValueError: try: is_input_t_tensor = backend.is_keras_tensor( layer_utils.get_source_inputs(input_tensor)) except ValueError: raise ValueError('input_tensor: ', input_tensor, 'is not type input_tensor') if is_input_t_tensor: if backend.image_data_format == 'channels_first': if backend.int_shape(input_tensor)[1] != input_shape[1]: raise ValueError( 'input_shape: ', input_shape, 'and input_tensor: ', input_tensor, 'do not meet the same shape requirements') else: if backend.int_shape(input_tensor)[2] != input_shape[1]: raise ValueError( 'input_shape: ', input_shape, 'and input_tensor: ', input_tensor, 'do not meet the same shape requirements') else: raise ValueError('input_tensor specified: ', input_tensor, 'is not a keras tensor') # If input_shape is None, infer shape from input_tensor if input_shape is None and input_tensor is not None: try: backend.is_keras_tensor(input_tensor) except ValueError: raise ValueError('input_tensor: ', input_tensor, 'is type: ', type(input_tensor), 'which is not a valid type') if input_shape is None and not backend.is_keras_tensor(input_tensor): default_size = 224 elif input_shape is None and backend.is_keras_tensor(input_tensor): if backend.image_data_format() == 'channels_first': rows = backend.int_shape(input_tensor)[2] cols = backend.int_shape(input_tensor)[3] else: rows = backend.int_shape(input_tensor)[1] cols = backend.int_shape(input_tensor)[2] if rows == cols and rows in [96, 128, 160, 192, 224]: default_size = rows else: default_size = 224 # If input_shape is None and no input_tensor elif input_shape is None: default_size = 224 # If input_shape is not None, assume default size else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [96, 128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if backend.image_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 weights == 'imagenet': if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]: raise ValueError('If imagenet weights are being loaded, ' 'alpha can be one of `0.35`, `0.50`, `0.75`, ' '`1.0`, `1.3` or `1.4` only.') if rows != cols or rows not in [96, 128, 160, 192, 224]: rows = 224 logging.warning('`input_shape` is undefined or non-square, ' 'or `rows` is not in [96, 128, 160, 192, 224].' ' Weights for input shape (224, 224) will be' ' loaded as the default.') 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 channel_axis = 1 if backend.image_data_format() == 'channels_first' else -1 first_block_filters = _make_divisible(32 * alpha, 8) x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(img_input, 3), name='Conv1_pad')(img_input) x = layers.Conv2D(first_block_filters, kernel_size=3, strides=(2, 2), padding='valid', use_bias=False, name='Conv1')(x) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x) x = layers.ReLU(6., name='Conv1_relu')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, expansion=6, block_id=6) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=7) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=8) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=9) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=10) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=11) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=12) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, expansion=6, block_id=13) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=14) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=15) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, expansion=6, block_id=16) # no alpha applied to last conv as stated in the paper: # if the width multiplier is greater than 1 we # increase the number of output channels if alpha > 1.0: last_block_filters = _make_divisible(1280 * alpha, 8) else: last_block_filters = 1280 x = layers.Conv2D(last_block_filters, kernel_size=1, use_bias=False, name='Conv_1')(x) x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x) x = layers.ReLU(6., name='out_relu')(x) if include_top: x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(classes, activation='softmax', use_bias=True, name='Logits')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(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 # Create model. model = training.Model(inputs, x, name='mobilenetv2_%0.2f_%s' % (alpha, rows)) # Load weights. if weights == 'imagenet': if include_top: model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) + '_' + str(rows) + '.h5') weight_path = BASE_WEIGHT_PATH + model_name weights_path = data_utils.get_file(model_name, weight_path, cache_subdir='models') else: model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) + '_' + str(rows) + '_no_top' + '.h5') weight_path = BASE_WEIGHT_PATH + model_name weights_path = data_utils.get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
import tensorflow as tf from tensorflow.python.keras.models import Sequential from tensorflow.python.keras import layers grayScale = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [12, 24, 34, 4, 52, 63, 74, 85, 978]] middlePixel = [[1, 2, 3], [23, 3, 12]] testingGray = grayScale testingMiddlePixel = middlePixel print("making model...") model = Sequential([ layers.Dense(10, input_dim=9, activation=tf.nn.relu), layers.Dense(3, activation=tf.nn.sigmoid) ]) print("model made") print("compiling model...") model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) print("Fitting") model.fit( grayScale, middlePixel, epochs=5 ) #grayscale = training b&w window , middelPixel = rgb value of middle pixel print("evaluating...") accuracy = model.evaluate(testingGray, testingMiddlePixel) print(accuracy) #use pixel = model.predict(window) and multiply each value by 255 to find pixel rgb value
def test_repr_and_string(self): kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec( shape=(1, 2, 3), dtype=dtypes.float32)) expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(1, 2, 3), " "dtype=tf.float32, name=None))") expected_repr = "<KerasTensor: shape=(1, 2, 3) dtype=float32>" self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec( shape=(2, ), dtype=dtypes.int32), inferred_value=[2, 3]) expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(2,), " "dtype=tf.int32, name=None), inferred_value=[2, 3])") expected_repr = ( "<KerasTensor: shape=(2,) dtype=int32 inferred_value=[2, 3]>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = keras_tensor.KerasTensor(type_spec=sparse_tensor.SparseTensorSpec( shape=(1, 2, 3), dtype=dtypes.float32)) expected_str = ("KerasTensor(type_spec=SparseTensorSpec(" "TensorShape([1, 2, 3]), tf.float32))") expected_repr = ("<KerasTensor: type_spec=SparseTensorSpec(" "TensorShape([1, 2, 3]), tf.float32)>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) with testing_utils.use_keras_tensors_scope(True): inp = layers.Input(shape=(3, 5)) kt = layers.Dense(10)(inp) expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(None, 3, 10), " "dtype=tf.float32, name=None), name='dense/BiasAdd:0', " "description=\"Symbolic value 0 from symbolic call 0 " "of layer 'dense'\")") expected_repr = ( "<KerasTensor: shape=(None, 3, 10) dtype=float32 (Symbolic value 0 " "from symbolic call 0 of layer 'dense')>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = array_ops.reshape(kt, shape=(3, 5, 2)) expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(3, 5, 2), dtype=tf.float32, " "name=None), name='tf.reshape/Reshape:0', description=\"Symbolic " "value 0 from symbolic call 0 of layer 'tf.reshape'\")") expected_repr = ( "<KerasTensor: shape=(3, 5, 2) dtype=float32 (Symbolic " "value 0 from symbolic call 0 of layer 'tf.reshape')>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kts = array_ops.unstack(kt) for i in range(3): expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(5, 2), dtype=tf.float32, " "name=None), name='tf.unstack/unstack:%s', description=\"Symbolic " "value %s from symbolic call 0 of layer 'tf.unstack'\")" ) % (i, i) expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 " "(Symbolic value %s from symbolic call 0 " "of layer 'tf.unstack')>") % i self.assertEqual(expected_str, str(kts[i])) self.assertEqual(expected_repr, repr(kts[i]))
def test_repr_and_string(self): kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec( shape=(1, 2, 3), dtype=dtypes.float32)) expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(1, 2, 3), " "dtype=tf.float32, name=None))") expected_repr = "<KerasTensor: shape=(1, 2, 3) dtype=float32>" self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = keras_tensor.KerasTensor(type_spec=tensor_spec.TensorSpec( shape=(2, ), dtype=dtypes.int32), inferred_value=[2, 3]) expected_str = ("KerasTensor(type_spec=TensorSpec(shape=(2,), " "dtype=tf.int32, name=None), inferred_value=[2, 3])") expected_repr = ( "<KerasTensor: shape=(2,) dtype=int32 inferred_value=[2, 3]>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = keras_tensor.KerasTensor(type_spec=sparse_tensor.SparseTensorSpec( shape=(1, 2, 3), dtype=dtypes.float32)) expected_str = ("KerasTensor(type_spec=SparseTensorSpec(" "TensorShape([1, 2, 3]), tf.float32))") expected_repr = ("<KerasTensor: type_spec=SparseTensorSpec(" "TensorShape([1, 2, 3]), tf.float32)>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) inp = layers.Input(shape=(3, 5)) kt = layers.Dense(10)(inp) expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(None, 3, 10), " "dtype=tf.float32, name=None), name='dense/BiasAdd:0', " "description=\"created by layer 'dense'\")") expected_repr = ( "<KerasTensor: shape=(None, 3, 10) dtype=float32 (created " "by layer 'dense')>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kt = array_ops.reshape(kt, shape=(3, 5, 2)) expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(3, 5, 2), dtype=tf.float32, " "name=None), name='tf.reshape/Reshape:0', description=\"created " "by layer 'tf.reshape'\")") expected_repr = ( "<KerasTensor: shape=(3, 5, 2) dtype=float32 (created " "by layer 'tf.reshape')>") self.assertEqual(expected_str, str(kt)) self.assertEqual(expected_repr, repr(kt)) kts = array_ops.unstack(kt) for i in range(3): expected_str = ( "KerasTensor(type_spec=TensorSpec(shape=(5, 2), dtype=tf.float32, " "name=None), name='tf.unstack/unstack:%s', description=\"created " "by layer 'tf.unstack'\")" % (i, )) expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 " "(created by layer 'tf.unstack')>") self.assertEqual(expected_str, str(kts[i])) self.assertEqual(expected_repr, repr(kts[i]))
from tensorflow.python.keras.utils import plot_model model = Sequential() model.add( layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(32, 32, 1), strides=(1, 1))) model.add(layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2))) model.add( layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu', strides=(1, 1))) model.add(layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(units=120, activation='relu')) model.add(layers.Dense(units=84, activation='relu')) model.add(layers.Dense(units=10, activation='softmax')) plot_model(model, to_file=f'{__file__.split(".py")[0]}.png', show_layer_names=True, show_shapes=True)
def __init__(self): super(vgg16_skip, self).__init__() kernel_size = 3 activation_fn = tf.nn.relu self.conv1_1 = layers.Conv2D(64, kernel_size, activation=activation_fn, padding='SAME') self.conv1_2 = layers.Conv2D(64, kernel_size, activation=activation_fn, padding='SAME') self.pool_1 = layers.MaxPooling2D(strides=(2, 2), padding='SAME') self.conv2_1 = layers.Conv2D(128, kernel_size, activation=activation_fn, padding='SAME') self.conv2_2 = layers.Conv2D(128, kernel_size, activation=activation_fn, padding='SAME') self.pool_2 = layers.MaxPooling2D(strides=(2, 2), padding='SAME') self.conv3_1 = layers.Conv2D(256, kernel_size, activation=activation_fn, padding='SAME') self.conv3_2 = layers.Conv2D(256, kernel_size, activation=activation_fn, padding='SAME') self.conv3_3 = layers.Conv2D(256, kernel_size, activation=activation_fn, padding='SAME') self.pool_3 = layers.MaxPooling2D(strides=(2, 2), padding='SAME') self.conv4_1 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.conv4_2 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.conv4_3 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.pool_4 = layers.MaxPooling2D(strides=(2, 2), padding='SAME') self.conv5_1 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.conv5_2 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.conv5_3 = layers.Conv2D(512, kernel_size, activation=activation_fn, padding='SAME') self.pool_5 = layers.MaxPooling2D(strides=(2, 2), padding='SAME') self.x_flat = layers.Flatten() self.skip_flat = layers.Flatten() self.fc1 = layers.Dense(4096) self.fc2 = layers.Dense(4096) self.fc3 = layers.Dense(10, activation=tf.nn.softmax)
class histSequenceVal(histSequence): def __init__(self, x, y, batch_size): self.x, self.y = x, y self.batch_size = batch_size batch_size = 1000 sequenceGenerator = histSequence(X_train, y_train, batch_size) validationSeqGen = histSequenceVal(X_val, y_val, batch_size) print(validationSeqGen.__getitem__(0)) # Defining the ML model model = Sequential() model.add(layers.InputLayer(input_shape=(50, ))) model.add(layers.Dense(256, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dropout(0.2)) model.add(layers.Dense(128, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(64, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(32, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(16, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.Dense(3, activation='softmax')) tensorboard = TensorBoard(log_dir="logs/{}".format(time())) model.compile(optimizer="rmsprop",
def test_stateful_metrics(self): with self.cached_session(): np.random.seed(1334) class BinaryTruePositives(layers.Layer): """Stateful Metric to count the total true positives over all batches. Assumes predictions and targets of shape `(samples, 1)`. Arguments: threshold: Float, lower limit on prediction value that counts as a positive class prediction. name: String, name for the metric. """ def __init__(self, name='true_positives', **kwargs): super(BinaryTruePositives, self).__init__(name=name, **kwargs) self.true_positives = K.variable(value=0, dtype='int32') self.stateful = True def reset_states(self): K.set_value(self.true_positives, 0) def __call__(self, y_true, y_pred): """Computes the number of true positives in a batch. Args: y_true: Tensor, batch_wise labels y_pred: Tensor, batch_wise predictions Returns: The total number of true positives seen this epoch at the completion of the batch. """ y_true = math_ops.cast(y_true, 'int32') y_pred = math_ops.cast(math_ops.round(y_pred), 'int32') correct_preds = math_ops.cast( math_ops.equal(y_pred, y_true), 'int32') true_pos = math_ops.cast( math_ops.reduce_sum(correct_preds * y_true), 'int32') current_true_pos = self.true_positives * 1 self.add_update(state_ops.assign_add( self.true_positives, true_pos), inputs=[y_true, y_pred]) return current_true_pos + true_pos metric_fn = BinaryTruePositives() config = metrics.serialize(metric_fn) metric_fn = metrics.deserialize( config, custom_objects={'BinaryTruePositives': BinaryTruePositives}) # Test on simple model inputs = layers.Input(shape=(2, )) outputs = layers.Dense(1, activation='sigmoid')(inputs) model = Model(inputs, outputs) model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['acc', metric_fn]) # Test fit, evaluate samples = 100 x = np.random.random((samples, 2)) y = np.random.randint(2, size=(samples, 1)) val_samples = 10 val_x = np.random.random((val_samples, 2)) val_y = np.random.randint(2, size=(val_samples, 1)) history = model.fit(x, y, epochs=1, batch_size=10, validation_data=(val_x, val_y)) outs = model.evaluate(x, y, batch_size=10) preds = model.predict(x) def ref_true_pos(y_true, y_pred): return np.sum(np.logical_and(y_pred > 0.5, y_true == 1)) # Test correctness (e.g. updates should have been run) self.assertAllClose(outs[2], ref_true_pos(y, preds), atol=1e-5) # Test correctness of the validation metric computation val_preds = model.predict(val_x) val_outs = model.evaluate(val_x, val_y, batch_size=10) self.assertAllClose(val_outs[2], ref_true_pos(val_y, val_preds), atol=1e-5) self.assertAllClose(val_outs[2], history.history['val_true_positives'][-1], atol=1e-5) # Test with generators gen = [(np.array([x0]), np.array([y0])) for x0, y0 in zip(x, y)] val_gen = [(np.array([x0]), np.array([y0])) for x0, y0 in zip(val_x, val_y)] history = model.fit_generator(iter(gen), epochs=1, steps_per_epoch=samples, validation_data=iter(val_gen), validation_steps=val_samples) outs = model.evaluate_generator(iter(gen), steps=samples) preds = model.predict_generator(iter(gen), steps=samples) # Test correctness of the metric results self.assertAllClose(outs[2], ref_true_pos(y, preds), atol=1e-5) # Test correctness of the validation metric computation val_preds = model.predict_generator(iter(val_gen), steps=val_samples) val_outs = model.evaluate_generator(iter(val_gen), steps=val_samples) self.assertAllClose(val_outs[2], ref_true_pos(val_y, val_preds), atol=1e-5) self.assertAllClose(val_outs[2], history.history['val_true_positives'][-1], atol=1e-5)
import os base_dir = "D:\\参赛文件\\cats_and_dogs_small" train_dir = os.path.join(base_dir, 'train') validation_dir = os.path.join(base_dir, 'validation') test_dir = os.path.join(base_dir, 'test') #使用数据增强的特征提取 from tensorflow.python.keras.applications import VGG16 data_path = "F:\\5-model data\\vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5" conv_base = VGG16(weights=data_path, include_top=False, input_shape=(150, 150, 3)) from tensorflow.python.keras import layers, models model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) print("model.trainable_weights=", len(model.trainable_weights)) # conv_base.trainable = False #冻结conv_base层网络并将最后一个卷积层解冻 set_trainable = False for layer in conv_base.layers: if layer.name == 'block5_conv1': set_trainable = True if set_trainable: layer.trainable = True else: layer.trainable = False print("After freeaed model.trainable_weights=", len(model.trainable_weights)) from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
def supervised_learning(load): # Loading dataset data = [] g = gzip.open("Software.json.gz", 'r') print("Loading dataset ...") for l in g: data.append(json.loads(l)) N = 100000 print("The dataset used has ", len(data), "entries! Of this dataset", N, "entries are used to train the model.") reviews = [] ratings = [] print("Text preprocessing ...") for d in data[:N]: if d.get('reviewText') is None: continue # remove all unwanted chars review = re.compile(r'[^a-z0-9\s]').sub( r'', re.compile(r'[\W]').sub(r' ', d.get('reviewText').lower())) reviews.append(review) rating = float(d.get('overall')) ratings.append(rating) # vectorized the input texts max_features = 200000 tokenizer = Tokenizer(num_words=max_features) tokenizer.fit_on_texts(reviews) reviews = tokenizer.texts_to_sequences(reviews) # calculating the maximal review length & pad all inputs to the same length for the neural network max_length = max(len(train_r) for train_r in reviews) reviews = tf.keras.preprocessing.sequence.pad_sequences(reviews, maxlen=max_length) # split the data into training set, test set and validation set print("Splitting dataset ...") train_reviews, test_reviews, train_ratings, test_ratings = train_test_split( np.array(reviews), np.array(ratings), test_size=0.1) train_reviews, validation_reviews, train_ratings, validation_ratings = train_test_split( train_reviews, train_ratings, test_size=0.2) # Create the neural network. Input size was calculated above input = layers.Input(shape=(max_length, )) x = layers.Embedding(max_features, 64)(input) # three times, use a convolutional layer, normalization and max pooling layer x = layers.Conv1D(64, 5, activation='relu')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool1D(3)(x) x = layers.Conv1D(64, 5, activation='relu')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool1D(5)(x) x = layers.Conv1D(64, 5, activation='relu')(x) x = layers.GlobalMaxPool1D()(x) x = layers.Flatten()(x) # two normally connected layers to condense the output to a single number x = layers.Dense(64, activation='relu')(x) output = layers.Dense(1, activation=mapping_to_target_range)(x) model = models.Model(inputs=input, outputs=output) # Adam (a stochastic gradient descent variant) as optimization function opt = tf.keras.optimizers.Adam(learning_rate=0.001) # compiling the model. As error the MSE is specified since the output and target are floats model.compile(optimizer=opt, loss='mean_squared_error') # loading model weights if wanted if load: print("\nLoading previous model weights:\n") model.load_weights('weights/supervisedLearning') # training the model print("Training Model:\n") model.fit(train_reviews, train_ratings, batch_size=64, epochs=3, validation_data=(validation_reviews, validation_ratings)) # calculating the predictions on the test set test_pred = model.predict(test_reviews) # printing the classification report print(classification_report(test_ratings, np.round(test_pred))) # testing the model with 3 examples (positive, negative, neutral review) print("\nTesting model: ") x = "I really like this book. It is one of the best I have read." x = re.compile(r'[^a-z\s]').sub(r'', re.compile(r'[\W]').sub(r' ', x.lower())) x = tokenizer.texts_to_sequences(np.array([x])) x = tf.keras.preprocessing.sequence.pad_sequences(x, maxlen=max_length) result = model.predict(x) print( "'I really like this book. It is one of the best I have read.' got the rating: ", round(result[0][0])) x = "I really hate this book. It is one of the worst I have read." x = re.compile(r'[^a-z\s]').sub(r'', re.compile(r'[\W]').sub(r' ', x.lower())) x = tokenizer.texts_to_sequences(np.array([x])) x = tf.keras.preprocessing.sequence.pad_sequences(x, maxlen=max_length) result = model.predict(x) print( "'I really hate this book. It is one of the worst I have read.' got the rating: ", round(result[0][0])) x = "This book is ok. It is very average." x = re.compile(r'[^a-z\s]').sub(r'', re.compile(r'[\W]').sub(r' ', x.lower())) x = tokenizer.texts_to_sequences(np.array([x])) x = tf.keras.preprocessing.sequence.pad_sequences(x, maxlen=max_length) result = model.predict(x) print("'This book is ok. It is very average.' got the rating: ", round(result[0][0])) # saving the model weights print("\n\nSaving model weights ...") model.save_weights('weights/supervisedLearning')
def LeNet(include_top=True, input_tensor=None, input_shape=None, pooling=None, classes=1000): """ Instantiates the LeNet-5 architecture. Args: include_top: whether to include the 3 fully-connected layers 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 input channels, and width and height should be no smaller than 32. 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 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. """ if classes == 1000: raise ValueError('If use dataset is `imagenet`, please use it' 'otherwise please use classifier images classes.') 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 x = layers.Conv2D(6, (5, 5), activation=tf.nn.relu, padding='same', input_shape=(32, 32, 1), name='conv1')(img_input) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='max_pool1')(x) x = layers.Conv2D(16, (3, 3), activation=tf.nn.relu, padding='same', name='conv2')(x) x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='max_pool2')(x) if include_top: # Classification block x = layers.Flatten(name='flatten')(x) x = layers.Dense(120, activation=tf.nn.relu, name='fc1')(x) x = layers.Dense(84, activation=tf.nn.relu, name='fc2')(x) x = layers.Dense(classes, activation=tf.nn.softmax, name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = models.Model(inputs, x, name='LeNet-5') return model
def next_frame_and_done_cnn(final_activation, input_shape=(8, 8, 7), output_size=1, cnn_l2=0, cnn_channels=64, cnn_n_layers=2, cnn_final_pool_size=(2, 2), cnn_batch_norm=False, cnn_kernel_size=(3, 3), cnn_strides=(1, 1), cnn_dilation=(1, 1), global_average_pooling=False, fc_n_hidden=128, fc_n_layers=1, fc_dropout=0, fc_dropout_input=False, fc_l2=0., image_output=False): """ Simple convolutional architecture. Args: global_average_pooling: if use GAP after convolutions, note that fully connected layers still will be applied (if fc_n_layers > 0). Setting True enables to infer network on different input shapes. """ if global_average_pooling: # Remove height and width to enable inference on different image shapes. input_shape = (None, None, input_shape[2]) input = layers.Input(shape=input_shape) x = cnn_body_v0_2(input, l2=cnn_l2, channels=cnn_channels, n_layers=cnn_n_layers, final_pool_size=cnn_final_pool_size, batch_norm=cnn_batch_norm, strides=cnn_strides, kernel_size=cnn_kernel_size, dilation=cnn_dilation) x_img = x if global_average_pooling: x = layers.GlobalAveragePooling2D()(x) x = flatten_and_mlp_v0_1(x, n_hidden=fc_n_hidden, n_layers=fc_n_layers, dropout=fc_dropout, dropout_input=fc_dropout_input, l2=fc_l2) # apply final transformations # x_img -> next_frame (logits map) # x -> if_done (logit) x_and_name = [ (x_img, Target.NEXT_FRAME.value), (x, "if_done"), ] pred = [ layers.Dense(output_size[name], activation=final_activation[name], name=name)(activation) for activation, name in x_and_name ] print("TODO remove") model = keras.models.Model(inputs=input, outputs=pred) return model
def _simple_dense_layer(self): layer = l.Dense(2) layer.build(input_shape=(3, )) return layer