def model_fn(features, labels, mode, params): x = tf.reshape(features, [-1, 125, 161, 2], name='stricCnn5') x_norm = tf.layers.batch_normalization( x, training=mode == tf.estimator.ModeKeys.TRAIN, name='x_norm') x = tf.reshape(x_norm[:, :, :, 0], [-1, 125, 161, 1], name='reshape_spec') conv = x conv = stric_block(conv, 32, mode, 'conv_1') conv = stric_block(conv, 64, mode, 'conv_2') conv = stric_block(conv, 128, mode, 'conv_3') conv = stric_block(conv, 256, mode, 'conv_4') conv = stric_block(conv, 512, mode, 'conv_5', only_same=True) incep = inception_block(conv, t1x1=128, t3x3=128, t5x5=128, tmp=128, name='incep6') incep = inception_block(incep, t1x1=128, t3x3=128, t5x5=128, tmp=128, name='incep7') incep = inception_block(incep, t1x1=128, t3x3=128, t5x5=128, tmp=128, name='incep8') flat = flatten(incep) dropout1 = tf.layers.dropout(flat, rate=params['dropout_rate'], training=mode == tf.estimator.ModeKeys.TRAIN, name='dropout1') dense1 = tf.layers.dense(dropout1, units=2048, activation=tf.nn.relu, name='dense1') dropout2 = tf.layers.dropout(dense1, rate=params['dropout_rate'], training=mode == tf.estimator.ModeKeys.TRAIN, name='dropout2') dense2 = tf.layers.dense(dropout2, units=2048, activation=tf.nn.relu, name='dense2') dropout3 = tf.layers.dropout(dense2, rate=params['dropout_rate'], training=mode == tf.estimator.ModeKeys.TRAIN, name='dropout3') logits = tf.layers.dense(dropout3, units=params['num_classes'], name='logits') predictions = { 'classes': tf.argmax(logits, axis=1, name='prediction_classes'), 'probabilities': tf.nn.softmax(logits, name='prediction_softmax') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec( mode=mode, predictions={'predictions': predictions['probabilities']}) tf.summary.image( 'confusion_matrix', conf_mat(labels, predictions['classes'], params['num_classes'])) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=params['num_classes'], name='onehot_labels') loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) tf.summary.scalar('loss', loss) optimizer = tf.train.GradientDescentOptimizer( learning_rate=params['learning_rate']) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) eval_metric_ops = { 'accuracy': tf.metrics.accuracy(labels=labels, predictions=predictions['classes']) } tf.summary.scalar('accuracy', eval_metric_ops['accuracy'][1]) return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op, eval_metric_ops=eval_metric_ops)
def model_fn(features, labels, mode, params): x = tf.reshape(features, [-1, 125, 161, 2], name='cnn4') x_norm = tf.layers.batch_normalization( x, training=mode == tf.estimator.ModeKeys.TRAIN, name='x_norm') x = tf.reshape(x_norm[:, :, :, 0], [-1, 125, 161, 1], name='reshape_spec') if params['verbose_summary']: tf.summary.image('input', x) conv1 = tf.layers.conv2d(x, filters=16, kernel_size=5, activation=tf.nn.relu, name='conv1') pool1 = tf.layers.max_pooling2d(conv1, pool_size=[2, 2], strides=2, name='pool1') if params['verbose_summary']: log_conv_kernel('conv1') tf.summary.image('pool1', pool1[:, :, :, 0:1]) conv2 = tf.layers.conv2d(pool1, filters=32, kernel_size=5, activation=tf.nn.relu, name='conv2') pool2 = tf.layers.max_pooling2d(conv2, pool_size=[2, 2], strides=2, name='pool2') if params['verbose_summary']: log_conv_kernel('conv2') tf.summary.image('pool2', pool2[:, :, :, 0:1]) conv3 = tf.layers.conv2d(pool2, filters=64, kernel_size=5, activation=tf.nn.relu, name='conv3') pool3 = tf.layers.max_pooling2d(conv3, pool_size=[2, 2], strides=2, name='pool3') if params['verbose_summary']: log_conv_kernel('conv3') tf.summary.image('pool3', pool3[:, :, :, 0:1]) dim = pool3.get_shape()[1:] dim = int(dim[0] * dim[1] * dim[2]) flat = tf.reshape(pool3, [-1, dim], name='flat') dropout4 = tf.layers.dropout(flat, rate=params['dropout_rate'], training=mode == ModeKeys.TRAIN, name='dropout4') dense4 = tf.layers.dense(dropout4, units=128, activation=tf.nn.relu, name='dense4') logits = tf.layers.dense(dense4, units=params['num_classes'], name='logits') predictions = { 'classes': tf.argmax(logits, axis=1, name='prediction_classes'), 'probabilities': tf.nn.softmax(logits, name='prediction_probabilities'), } if mode == ModeKeys.PREDICT: return EstimatorSpec( mode=mode, predictions={'predictions': predictions['probabilities']}) tf.summary.image( 'confusion_matrix', conf_mat(labels, predictions['classes'], params['num_classes'])) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=params['num_classes'], name='onehot_labels') loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) tf.summary.scalar('loss', loss) optimizer = tf.train.GradientDescentOptimizer( learning_rate=params['learning_rate']) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) eval_metric_ops = { 'accuracy': tf.metrics.accuracy(labels=labels, predictions=predictions['classes']) } tf.summary.scalar('accuracy', eval_metric_ops['accuracy'][1]) return EstimatorSpec(mode=mode, loss=loss, train_op=train_op, eval_metric_ops=eval_metric_ops)
def model_fn(features, labels, mode, params): x = tf.reshape(features, [-1, 125, 161, 2], name='incep902') x_norm = tf.layers.batch_normalization(x, training=mode == tf.estimator.ModeKeys.TRAIN, name='x_norm') x = tf.reshape(x_norm[:, :, :, 0], [-1, 125, 161, 1], name='reshape_spec') conv1 = tf.layers.conv2d(x, filters=16, kernel_size=3, padding='same', activation=tf.nn.relu, name='conv1') conv1b = tf.layers.conv2d(conv1, filters=16, kernel_size=3, activation=tf.nn.relu, name='conv1b') pool1 = tf.layers.max_pooling2d(conv1b, pool_size=[2, 2], strides=2, name='pool1') if params['verbose_summary']: log_conv_kernel('conv1') log_conv_kernel('conv1b') tf.summary.image('pool1', pool1[:, :, :, 0:1]) incep2 = inception_block(pool1, t1x1=8, t3x3=8, t5x5=8, tmp=8, name='incep2') conv3 = tf.layers.conv2d(incep2, filters=32, kernel_size=3, padding='same', activation=tf.nn.relu, name='conv3') conv3b = tf.layers.conv2d(conv3, filters=32, kernel_size=3, activation=tf.nn.relu, name='conv3b') pool3 = tf.layers.max_pooling2d(conv3b, pool_size=[2, 2], strides=2, name='pool3') if params['verbose_summary']: log_conv_kernel('conv3') log_conv_kernel('conv3b') tf.summary.image('pool3', pool3[:, :, :, 0:1]) conv5 = tf.layers.conv2d(pool3, filters=64, kernel_size=3, padding='same', activation=tf.nn.relu, name='conv5') conv5b = tf.layers.conv2d(conv5, filters=64, kernel_size=3, activation=tf.nn.relu, name='conv5b') pool5 = tf.layers.max_pooling2d(conv5b, pool_size=[2, 2], strides=2, name='pool5') if params['verbose_summary']: log_conv_kernel('conv5') log_conv_kernel('conv5b') tf.summary.image('pool5', pool5[:, :, :, 0:1]) incep6 = inception_block(pool5, t1x1=32, t3x3=32, t5x5=32, tmp=32, name='incep6') conv7 = tf.layers.conv2d(incep6, filters=128, kernel_size=3, padding='same', activation=tf.nn.relu, name='conv7') conv7b = tf.layers.conv2d(conv7, filters=128, kernel_size=3, activation=tf.nn.relu, name='conv7b') pool7 = tf.layers.max_pooling2d(conv7b, pool_size=[2, 2], strides=2, name='pool7') if params['verbose_summary']: log_conv_kernel('conv7') log_conv_kernel('conv7b') tf.summary.image('pool7', pool7[:, :, :, 0:1]) conv8 = tf.layers.conv2d(pool7, filters=256, kernel_size=3, padding='same', activation=tf.nn.relu, name='conv8') conv8b = tf.layers.conv2d(conv8, filters=256, kernel_size=3, activation=tf.nn.relu, name='conv8b') pool8 = tf.layers.max_pooling2d(conv8b, pool_size=[2, 2], strides=2, name='pool8') if params['verbose_summary']: log_conv_kernel('conv8') log_conv_kernel('conv8b') tf.summary.image('pool8', pool8[:, :, :, 0:1]) conv = tf.layers.conv2d(pool8, filters=256, kernel_size=[1, 3], activation=tf.nn.relu, name='conv1x1') conv = tf.layers.conv2d(conv, filters=params['num_classes'], kernel_size=1, activation=None, name='conv_logits') logits = tf.reshape(conv, [-1, 12], name='flatten') predictions = { 'classes': tf.argmax(logits, axis=1, name='prediction_classes'), 'probabilities': tf.nn.softmax(logits, name='prediction_softmax') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions={'predictions': predictions['probabilities']}) tf.summary.image('confusion_matrix', conf_mat(labels, predictions['classes'], params['num_classes'])) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=params['num_classes'], name='onehot_labels') loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) tf.summary.scalar('loss', loss) optimizer = tf.train.GradientDescentOptimizer(learning_rate=params['learning_rate']) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) eval_metric_ops = { 'accuracy': tf.metrics.accuracy(labels=labels, predictions=predictions['classes']) } tf.summary.scalar('accuracy', eval_metric_ops['accuracy'][1]) return tf.estimator.EstimatorSpec( mode=mode, loss=loss, train_op=train_op, eval_metric_ops=eval_metric_ops )