def inference(images,training,name,trainable = True): ops.init_scope_vars() with tf.variable_scope(name) as scope: # Preform some image preprocessing net = images net = ops.delist(net) net = tf.cast(net,tf.float32) net = ops.im_norm(net) # If we receive image channels in a format that shouldn't be normalized, # that goes here. with tf.variable_scope('Process_Network'): # Run two dense reduction modules to reduce the number of parameters in # the network and for low parameter feature extraction. net = ops.dense_reduction(net,training,filters = 4 , kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Block_1') net = ops.dense_reduction(net,training,filters = 8, kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Block_2') net = ops.dense_reduction(net,training,filters = 12, kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Block_3') # Run the network over some resnet modules, including reduction # modules in order to further reduce the parameters and have a powerful, # proven network architecture. net = resnetA(net) net = resnetA(net) net = ops.dense_reduction(net,training,filters = 12, kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Redux_4') net = resnetA(net) net = resnetA(net) net = ops.dense_reduction(net,training,filters = 12, kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Redux_5') net = resnetB(net) net = resnetB(net) net = ops.dense_reduction(net,training,filters = 12, kernel = 3, stride = 2, activation=tf.nn.leaky_relu,trainable=trainable, name = 'Dense_Redux_6') net = resnetC(net) net = resnetC(net) # net = ops.dense_reduction(net,training,filters = 56, kernel = 3, stride = 2, # activation=tf.nn.leaky_relu,trainable=trainable, # name = 'Dense_Block_7') with tf.variable_scope('Plant_Neurons') as scope: p_net = util.squish_to_batch(net) _b,neurons = net.get_shape().as_list() p_log = tf.layers.dense(p_net,neurons,name = 'Plant_Neurons') p_log = tf.layers.dense(p_log,FLAGS.num_plant_classes,name = 'Plant_Decider') p_log = tf.squeeze(p_log) with tf.variable_scope('Disease_Neurons') as scope: # A specific disease ResnetC module net = resnetC(net) net = util.squish_to_batch(net) _b,neurons = net.get_shape().as_list() # Construct a number of final layers for diseases equal to the number of plants. d_log = [] chan = tf.layers.dense(net,neurons, name = 'Disease_Neurons') for x in range(FLAGS.num_plant_classes): d_n = tf.layers.dense(chan,FLAGS.num_disease_classes,name = '%s_Decider'%plants[x]) d_n = tf.squeeze(d_n) d_log.append(d_n) with tf.variable_scope('DieaseFormatting') as scope: d_log = tf.stack(d_log) return p_log,d_log
def inference(images, training, name, trainable=True): ops.init_scope_vars() with tf.variable_scope(name) as scope: # Preform some image preprocessing net = images net = ops.delist(net) net = tf.cast(net, tf.float32) net = ops.batch_norm(net, training, trainable) # net = tf.fft2d(net) # If we receive image channels in a format that shouldn't be normalized, # that goes here. with tf.variable_scope('Process_Network'): # Run two dense reduction modules to reduce the number of parameters in # the network and for low parameter feature extraction. net = ops.dense_reduction(net, training, filters=4, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_1') net = ops.dense_reduction(net, training, filters=8, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_2') net = ops.dense_reduction(net, training, filters=4, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_3') net = ops.dense_reduction(net, training, filters=8, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_4') # Run the network over some resnet modules, including reduction # modules in order to further reduce the parameters and have a powerful, # proven network architecture. net = ops.inception_block_a(net, training, trainable, name='inception_block_a_1') net = ops.dense_reduction(net, training, filters=16, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_5') net = ops.inception_block_a(net, training, trainable, name='inception_block_a_2') net = ops.inception_block_a(net, training, trainable, name='inception_block_a_3') net = ops.dense_reduction(net, training, filters=24, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_6') net = ops.inception_block_b(net, training, trainable, name='inception_block_b_1') net = ops.inception_block_b(net, training, trainable, name='inception_block_b_2') # Commenting out for proof of concept # net = ops.inception_block_c(net,training,trainable,name='inception_block_c_1') # net = ops.inception_block_c(net,training,trainable,name='inception_block_c_1') # We're leaving the frequency domain in order to preform fully connected # inferences. Fully connected inferences do not work with imaginary # numbers. The error would always have an i/j term that will not be able # to generate a correct gradient for. # net = tf.ifft2d(net) # Theoretically, the network will be 8x8x128, for 8192 neurons in the first # fully connected network. net = util.squish_to_batch(net) net = tf.layers.dropout(net, .40) _b, neurons = net.get_shape().as_list() input(neurons) with tf.variable_scope('Disease_Neurons') as scope: # Construct a number of final layers for diseases equal to the number of # plants. chan = tf.layers.dense(net, neurons, name='Disease_Neurons') d_log = tf.layers.dense(chan, FLAGS.num_disease_classes, name='Disease_Decider') return d_log
def inference(global_step, images, p_lab, d_lab, training, name, trainable=True): with tf.variable_scope(name) as scope: # Preform some image preprocessing net = images net = ops.delist(net) net = tf.cast(net, tf.float32) net = ops.batch_norm(net, training, trainable) # net = tf.fft2d(net) # If we receive image channels in a format that shouldn't be normalized, # that goes here. with tf.variable_scope('Process_Network'): # Run two dense reduction modules to reduce the number of parameters in # the network and for low parameter feature extraction. net = ops.dense_reduction(net, training, filters=4, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_1') net = ops.dense_reduction(net, training, filters=8, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_2') net = ops.dense_reduction(net, training, filters=4, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_3') net = ops.dense_reduction(net, training, filters=8, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_4') # Run the network over some resnet modules, including reduction # modules in order to further reduce the parameters and have a powerful, # proven network architecture. net = ops.inception_block_a(net, training, trainable, name='inception_block_a_1') net = ops.dense_reduction(net, training, filters=16, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_5') net = ops.inception_block_a(net, training, trainable, name='inception_block_a_2') net = ops.inception_block_a(net, training, trainable, name='inception_block_a_3') net = ops.dense_reduction(net, training, filters=24, kernel=3, stride=2, activation=tf.nn.leaky_relu, trainable=trainable, name='Dense_Block_6') net = ops.inception_block_b(net, training, trainable, name='inception_block_b_1') net = ops.inception_block_b(net, training, trainable, name='inception_block_b_2') # Commenting out for proof of concept # net = ops.inception_block_c(net,training,trainable,name='inception_block_c_1') # net = ops.inception_block_c(net,training,trainable,name='inception_block_c_1') # We're leaving the frequency domain in order to preform fully connected # inferences. Fully connected inferences do not work with imaginary # numbers. The error would always have an i/j term that will not be able # to generate a correct gradient for. # net = tf.ifft2d(net) with tf.variable_scope('Plant_Neurons') as scope: # Theoretically, the network will be 8x8x128, for 8192 neurons in the first # fully connected network. net = util.squish_to_batch(net) _b, neurons = net.get_shape().as_list() # Fully connected network with number of neurons equal to the number of # parameters in the network at this point net = tf.layers.dense(net, neurons) # Fully connected layer to extract the plant classification # NOTE: This plant classification will be used to extract the proper # disease classification matrix p_log = tf.layers.dense(net, FLAGS.num_plant_classes) with tf.variable_scope('Disease_Neurons') as scope: # Construct a number of final layers for diseases equal to the number of # plants. d_net = [] for x in range(FLAGS.num_plant_classes): chan = tf.layers.dense(net, FLAGS.num_disease_classes, name='Disease_%d' % x) d_net.append(chan) d_net = tf.stack(d_net) # If we're training, we want to not use the plant network output, rather the # plant label. This ensures that the disease layers train properly. # NOTE: The disease loss function only trains based on this final layer. # IE: The disease gradient does not flow through the whole network, # using the plant network as its preprocessing. index = d_lab #if training else tf.argmax(p_log) index = tf.cast(index, tf.int32) size = [1, 1, FLAGS.num_disease_classes] # Extract the disease logit per example in batch d_log = [] for x in range(FLAGS.batch_size): start = [index[x], x, 0] val = tf.slice(d_net, start, size) d_log.append(val) d_log = tf.stack(d_log) d_log = tf.reshape(d_log, [FLAGS.batch_size, FLAGS.num_disease_classes]) # Get the losses and metrics with tf.variable_scope('Metrics') as scope: p_loss = ops.xentropy_loss(labels=p_lab, logits=p_log, name="Plant_Metrics") d_loss = ops.xentropy_loss(labels=d_lab, logits=d_log, name="Disease_Metrics") p_log = tf.argmax(p_log, -1) d_log = tf.argmax(d_log, -1) p_acc = ops.accuracy(p_lab, p_log, name='Plant_Accuracy') d_acc = ops.accuracy(d_lab, d_log, name='Disease_Accuracy') metrics = (p_loss, d_loss, p_acc, d_acc) p_vars = [ var for var in tf.trainable_variables() if 'Process_Network' in var.name or 'Plant_Neurons' in var.name ] d_vars = [ var for var in tf.trainable_variables() if 'Disease_Neurons' in var.name ] with tf.variable_scope('Trainer') as scope: train = tf.assign_add(global_step, 1, name='Global_Step') if training: p_train = trainer(global_step, p_loss, p_vars) d_train = trainer(global_step, d_loss, d_vars) train = (train, p_train, d_train) return p_log, d_log, train, metrics