def testMaxPooling3DGradient(self): def forward(a): r = max_pooling3d(a, pool_size=pool_size, strides=strides, padding='SAME') return r input_sizes = [1, 3, 2, 4, 1] pool_size = (2, 2, 1) strides = (1, 1, 1) total_size = np.prod(input_sizes) x = np.arange(1, total_size + 1, dtype=np.float32) aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) da = backprop.gradients_function(forward)(aa) if not context.executing_eagerly(): tf_aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) tf_max = max_pooling3d(tf_aa, pool_size=pool_size, strides=strides, padding='SAME') tf_da = gradients.gradients(tf_max, [tf_aa]) self.assertAllEqual(da[0], tf_da[0].eval())
def testMaxPooling3DGradient(self): def forward(a): r = max_pooling3d(a, pool_size=pool_size, strides=strides, padding='SAME') return r input_sizes = [1, 3, 2, 4, 1] pool_size = (2, 2, 1) strides = (1, 1, 1) total_size = np.prod(input_sizes) x = np.arange(1, total_size + 1, dtype=np.float32) aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) da = backprop.gradients_function(forward)(aa) if not context.executing_eagerly(): tf_aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) tf_max = max_pooling3d( tf_aa, pool_size=pool_size, strides=strides, padding='SAME') tf_da = gradients.gradients(tf_max, [tf_aa]) self.assertAllEqual(da[0], tf_da[0].eval())
def cnn_3d(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 """ # Size of images & labels height = int(images.shape[1]) width = int(images.shape[2]) depth = int(images.shape[3]) images = tf.reshape(images, [-1, height, width, depth, 1]) # Build the model with tf.name_scope('CONV1'): l_conv1 = conv3d(images, filters=4, kernel_size=[3, 3, 10], strides=[1, 1, 5], activation=relu, kernel_regularizer=l2_regularizer(REG_lambda)) # l_conv1 = rrelu(l_conv1, is_training) l_maxpool1 = max_pooling3d(l_conv1, pool_size=[3, 3, 3], strides=[1, 1, 1], padding='same', name='Maxpool1') with tf.name_scope('CONV2'): l_conv2 = conv3d( l_maxpool1, filters=16, kernel_size=[3, 3, 10], strides=[1, 1, 2], # activation=relu, kernel_regularizer=l2_regularizer(REG_lambda)) l_conv2 = rrelu(l_conv2, is_training) l_maxpool2 = max_pooling3d(l_conv2, pool_size=[3, 3, 3], strides=[1, 1, 1], padding='same', name='Maxpool2') l_flatten = flatten(l_maxpool2, scope='Flatten') with tf.name_scope('FC1'): l_fc1 = dense( l_flatten, 200, kernel_regularizer=l2_regularizer(REG_lambda), # activation=relu ) l_fc1 = rrelu(l_fc1, is_training) l_drop1 = dropout(l_fc1, Utils.drop, training=is_training, name='Dropout1') with tf.name_scope('FC2'): l_fc2 = dense( l_drop1, 200, kernel_regularizer=l2_regularizer(REG_lambda), # activation=relu ) l_fc2 = rrelu(l_fc2, is_training) l_drop2 = dropout(l_fc2, Utils.drop, training=is_training, name='Dropout2') logits = dense(l_drop2, NUM_CLASSES, name='Output') return logits
def forward(a): r = max_pooling3d(a, pool_size=pool_size, strides=strides, padding='SAME') return r