def testInvalidStrides(self): height, width = 7, 9 images = random_ops.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp(ValueError, 'strides'): pooling_layers.max_pooling2d(images, 3, strides=(1, 2, 3)) with self.assertRaisesRegexp(ValueError, 'strides'): pooling_layers.max_pooling2d(images, 3, strides=None)
def testInvalidPoolSize(self): height, width = 7, 9 images = random_ops.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp(ValueError, 'pool_size'): pooling_layers.max_pooling2d(images, (1, 2, 3), strides=2) with self.assertRaisesRegexp(ValueError, 'pool_size'): pooling_layers.max_pooling2d(images, None, strides=2)
def testInvalidPoolSize(self): height, width = 7, 9 images = random_ops.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp(ValueError, 'pool_size'): pooling_layers.max_pooling2d(images, (1, 2, 3), strides=2) with self.assertRaisesRegexp(ValueError, 'pool_size'): pooling_layers.max_pooling2d(images, None, strides=2)
def testInvalidDataFormat(self): height, width = 7, 9 images = random_ops.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp(ValueError, 'data_format'): pooling_layers.max_pooling2d(images, 3, strides=2, data_format='invalid')
def testInvalidStrides(self): height, width = 7, 9 images = random_ops.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp(ValueError, 'strides'): pooling_layers.max_pooling2d(images, 3, strides=(1, 2, 3)) with self.assertRaisesRegexp(ValueError, 'strides'): pooling_layers.max_pooling2d(images, 3, strides=None)
def cnn_2d(images, is_training): """ Build the model for 2D-CNN. Inputs: -- images: Images placeholder -- is_training: bool placeholder, training or not Output: -- Logits: Return the output of the model """ # Build the CNN model l_conv1 = conv2d(images, CONV1_FILTERS, KERNEL_SIZE1, strides=STRIDE_CONV1, activation=relu, name='Conv1') l_maxpool1 = max_pooling2d(l_conv1, POOL_SIZE1, POOL_SIZE1, padding='same', name='Maxpool1') l_conv2 = conv2d(l_maxpool1, CONV2_FILTERS, KERNEL_SIZE2, strides=STRIDE_CONV2, activation=relu, name='Conv2') l_maxpool2 = max_pooling2d(l_conv2, POOL_SIZE2, POOL_SIZE2, padding='same', name='Maxpool2') l_flatten = flatten(l_maxpool2, scope='Flatten') l_fc1 = dense(l_flatten, FC1, activation=relu, name='Fc1') l_drop = dropout(l_fc1, DROP_RATE, training=is_training, name='Dropout') l_fc2 = dense(l_drop, FC2, activation=relu, name='Fc2') logits = dense(l_fc2, NUM_CLASSES, name='Output') return logits
def _make_conv_layers(self): self.image_input = tf.placeholder( dtype=tf.float32, shape=[None, self.HEIGHT, self.WIDTH, 3], name="image_input") layer = self.image_input for p, pooling_config in enumerate(self.config.poolings, 1): for c, conv_config in enumerate(pooling_config.convolutions, 1): layer = conv2d( inputs=layer, filters=conv_config.n_filters, kernel_size=conv_config.kernel_size, activation=relu, padding="SAME", name=f"conv_{p}_{c}", ) layer = max_pooling2d(inputs=layer, pool_size=2, strides=pooling_config.strides, name=f"pool_{p}") self.last_pool_layer = layer
def mpool(self, k_height, k_width, d_height=2, d_width=2, mode='VALID', input_layer=None, num_channels_in=None): """Construct a max pooling layer.""" if input_layer is None: input_layer = self.top_layer else: self.top_size = num_channels_in name = 'mpool' + str(self.counts['mpool']) self.counts['mpool'] += 1 pool = pooling_layers.max_pooling2d( input_layer, [k_height, k_width], [d_height, d_width], padding=mode, data_format=self.channel_pos, name=name) self.top_layer = pool return pool
def testInvalidDataFormat(self): height, width = 7, 9 images = tf.random_uniform((5, height, width, 3), seed=1) with self.assertRaisesRegexp( ValueError, 'data_format'): pooling_layers.max_pooling2d(images, 3, strides=2, data_format='invalid')
def _maxpool(self, bottom, stride): return max_pooling2d(bottom, [stride,stride], [stride,stride])
def MNIST_model(features, labels, mode): #input features input_layer = tf.reshape(features["x"], [-1, 28, 28, 1]) if mode == tf.estimator.ModeKeys.EVAL: #input_data = tf.constant(1.0) - input_layer input_data = input_layer else: input_data = input_layer #convolution 1 conv1 = conv2d(inputs=input_data, filters=32, kernel_size=[5, 5], padding="same", activation=relu, use_bias=True) #max pooling 1 pool1 = max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) #convolution 2 conv2 = conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=relu, use_bias=True) #max pooling 2 pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) #Fully connected pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) dense_out = dense(inputs=pool2_flat, units=hidden_size, activation=relu) dropout_out = dropout(inputs=dense_out, rate=drop_rate, training=mode == tf.estimator.ModeKeys.TRAIN) #Generate a [28 * 28, 10] matrix as context #initialize w_a_1 = weight_variable(name="w_a_1", shape=[hidden_size, 28 * 28 * 10]) b_a_1 = bias_variable(name="b_a_1", shape=[28 * 28 * 10]) context = tf.add(tf.matmul(dropout_out, w_a_1), b_a_1) context_matrix = tf.reshape(context, [-1, 28 * 28, 10]) #dot product layer input_data_flat = tf.reshape(input_data, [-1, 28 * 28, 1]) input_data_tiled = tf.tile(input=input_data_flat, multiples=[1, 1, 10]) weighted_context = tf.multiply(input_data_tiled, context_matrix) #Generate softmax result logits = tf.reduce_sum(weighted_context, axis=[1]) #a dictionary of prediction operators predictions = { "logits": tf.multiply(logits, tf.constant(1.0), name="logit_out"), #class prediction "classes": tf.argmax(input=logits, axis=1), #probability prediction "probabilities": tf.nn.softmax(logits, name="softmax_tensor") } #prediction mode if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) #regularization l1_regularizer = tf.contrib.layers.l1_regularizer(scale=reg_scale) regularization_cost = tf.contrib.layers.apply_regularization( l1_regularizer, [context_matrix]) #Calculate Loss (for both TRAIN and EVAL modes) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10) error_cost = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) #total lost loss = regularization_cost + error_cost #train mode if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.AdamOptimizer(learning_rate=1e-4) train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) #evaluation metrics (for EVAL mode) eval_metric_ops = { "accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"]) } return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def MNIST_model(features, labels, mode): #input features input_layer = tf.reshape(features["x"], [-1, 28, 28, 1]) if mode == tf.estimator.ModeKeys.EVAL: input_data = tf.constant(1.0) - input_layer else: input_data = input_layer #convolution 1 conv1 = conv2d(inputs=input_data, filters=32, kernel_size=[5, 5], padding="same", activation=relu, use_bias=True) #max pooling 1 pool1 = max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) #convolution 2 conv2 = conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=relu, use_bias=True) #max pooling 2 pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) #Fully connected pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) dense_out = dense(inputs=pool2_flat, units=hidden_size, activation=relu) dropout_out = dropout(inputs=dense_out, rate=drop_rate, training=mode == tf.estimator.ModeKeys.TRAIN) logits = dense(inputs=dropout_out, units=10) #a dictionary of prediction operators predictions = { "logits": tf.multiply(logits, tf.constant(1.0), name="logit_out"), #class prediction "classes": tf.argmax(input=logits, axis=1), #probability prediction "probabilities": tf.nn.softmax(logits, name="softmax_tensor") } #prediction mode if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) #Calculate Loss (for both TRAIN and EVAL modes) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10) loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) #train mode if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.AdamOptimizer(learning_rate=1e-3) train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) #evaluation metrics (for EVAL mode) eval_metric_ops = { "accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"]) } return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def _maxpool(self, bottom): return max_pooling2d(bottom, [2, 2], [2, 2])
def _create_model(self): self.input_image = tf.placeholder(tf.float32, shape=(None, None, None, self.img_input_channels), name='input_image_placeholder') self.gt_image = tf.placeholder(tf.int32, shape=(None, None, None, self.num_classes), name='gt_image_placeholder') self.gt_contours = tf.placeholder(tf.int32, shape=(None, None, None, self.num_classes), name='gt_contours_placeholder') self.dropout_prob = tf.placeholder(dtype=tf.float32, shape=None, name='dropout_prob_placeholder') self.lr = tf.placeholder(dtype=tf.float32, shape=None, name='learning_rate_placeholder') scale_nc = self.hps.get('scale_nc') with tf.variable_scope("encoder"): with tf.variable_scope("block_1"): conv1 = self._add_common_layers(conv2d(self.input_image, filters=32*scale_nc, kernel_size=3, padding='same')) conv2 = self._add_common_layers(conv2d(conv1, filters=32*scale_nc, kernel_size=3, padding='same')) with tf.variable_scope("block_2"): mp2 = max_pooling2d(conv2, pool_size=2, strides=2, padding='same') bn1 = self._add_common_layers(self._bottleneck(mp2, size=64*scale_nc)) bn2 = self._add_common_layers(self._bottleneck(bn1, size=64*scale_nc)) with tf.variable_scope("block_3"): mp3 = max_pooling2d(bn2, pool_size=2, strides=2, padding='same') bn3 = self._add_common_layers(self._bottleneck(mp3, size=128*scale_nc)) bn4 = self._add_common_layers(self._bottleneck(bn3, size=128*scale_nc)) with tf.variable_scope("block_4"): mp4 = max_pooling2d(bn4, pool_size=2, strides=2, padding='same') bn5 = self._add_common_layers(self._bottleneck(mp4, size=256*scale_nc)) bn6 = self._add_common_layers(self._bottleneck(bn5, size=256*scale_nc)) d1 = dropout(bn6, rate=self.dropout_prob) with tf.variable_scope("block_5"): mp5 = max_pooling2d(d1, pool_size=2, strides=2, padding='same') bn7 = self._add_common_layers(self._bottleneck(mp5, size=256*scale_nc)) bn8 = self._add_common_layers(self._bottleneck(bn7, size=256*scale_nc)) d2 = dropout(bn8, rate=self.dropout_prob) with tf.variable_scope("block_6"): mp6 = max_pooling2d(d2, pool_size=2, strides=2, padding='same') bn9 = self._add_common_layers(self._bottleneck(mp6, size=256*scale_nc)) bn10 = self._add_common_layers(self._bottleneck(bn9, size=256*scale_nc)) d3 = dropout(bn10, rate=self.dropout_prob) self.img_descriptor = tf.reduce_mean(d3, axis=(1, 2)) with tf.variable_scope("decoder_seg"): deconvs = [] deconvs.append(conv2d(conv2, filters=self.num_classes, kernel_size=3, padding='same')) deconvs.append(self._upsample(bn2, k=1)) deconvs.append(self._upsample(bn4, k=2)) deconvs.append(self._upsample(d1, k=3)) deconvs.append(self._upsample(d2, k=4)) deconvs.append(self._upsample(d3, k=5)) concat = tf.concat(deconvs, axis=3) conv3 = conv2d(concat, filters=self.num_classes, kernel_size=3, padding='same') ac1 = self._add_common_layers(conv3) conv4 = conv2d(ac1, filters=self.num_classes, kernel_size=1, padding='same') ac2 = self._add_common_layers(conv4) self.preds_seg = softmax(ac2) with tf.variable_scope("decoder_cont"): deconvs = [] deconvs.append(conv2d(conv2, filters=self.num_classes, kernel_size=3, padding='same')) deconvs.append(self._upsample(bn2, k=1)) deconvs.append(self._upsample(bn4, k=2)) deconvs.append(self._upsample(d1, k=3)) deconvs.append(self._upsample(d2, k=4)) deconvs.append(self._upsample(d3, k=5)) concat = tf.concat(deconvs, axis=3) conv3 = conv2d(concat, filters=self.num_classes, kernel_size=3, padding='same') ac1 = self._add_common_layers(conv3) conv4 = conv2d(ac1, filters=self.num_classes, kernel_size=1, padding='same') ac2 = self._add_common_layers(conv4) self.preds_cont = softmax(ac2) cond1 = tf.greater_equal(self.preds_seg, self.threshold) cond2 = tf.less(self.preds_cont, self.threshold) conditions = tf.logical_and(cond1, cond2) self.preds = tf.where(conditions, tf.ones_like(conditions), tf.zeros_like(conditions)) self._add_train_op() self.summaries = tf.summary.merge_all()