Esempio n. 1
0
def evalidate(flag, args):
    with tf.Graph().as_default():
        queue_loader = data_loader(False,
                                   batch_size=args.bsize,
                                   num_epochs=args.ep,
                                   dataset_dir=args.dataset_dir)
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_101(queue_loader.images,
                                                         num_classes=764,
                                                         is_training=flag)

        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            "accuracy":
            slim.metrics.accuracy(np.argmax(logits, 1), queue_loader.labels)
        })

        for name, value in names_to_values.items():
            op_name = 'eval_{}'.format(name)
            op = tf.summary.scalar(op_name, value)
            tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)
            slim.evaluation.evaluate_once(
                master='',
                checkpoint_path=args.modelpath,
                logdir=args.evallog,
                num_evals=queue_loader.num_batches,
                eval_op=list(names_to_updates.values()),
                variables_to_restore=slim.get_variables_to_restore())
Esempio n. 2
0
def forward(datas_train,is_training):
    if FLAGS.typenets == 'vggnet16':
        net = vggnet(datas_train)
        net = slim.fully_connected(net,num_classes,activation_fn=None,scope='fc1')
        #the last layer
        outputs = slim.softmax(net, scope='predictions')
        #outputs = tf.nn.softmax(net,namescope='output')
    elif FLAGS.typenets == 'resnet50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)):
            net,_ = resnet_v2.resnet_v2_50(datas_train,num_classes=num_classes)
            net = slim.flatten(net,scope='flat2')
            outputs = slim.softmax(net, scope='predictions')
    elif FLAGS.typenets == 'resnet101':  
        with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)):
            net,_ = resnet_v2.resnet_v2_101(datas_train,num_classes=num_classes)
            net = slim.flatten(net,scope='flat2')
            outputs = slim.softmax(net, scope='predictions')
    elif FLAGS.typenets == 'resnet152':  
        with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)):
            net,_ = resnet_v2.resnet_v2_152(datas_train,num_classes=num_classes)
            net = slim.flatten(net,scope='flat2')
            outputs = slim.softmax(net, scope='predictions')
    elif FLAGS.typenets == 'resnet200':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)):
            net,_ = resnet_v2.resnet_v2_200(datas_train,num_classes=num_classes)
            net = slim.flatten(net,scope='flat2')
            outputs = slim.softmax(net, scope='predictions')
    elif FLAGS.typenets == 'simple':
        outputs = forward_simple(datas_train,is_training)
    return outputs
def build_frontend(inputs,
                   frontend,
                   is_training=True,
                   pretrained_dir="models"):
    if frontend == 'ResNet50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            frontend_scope = 'resnet_v2_50'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_50'),
                ignore_missing_vars=True)
    elif frontend == 'ResNet101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            frontend_scope = 'resnet_v2_101'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_101'),
                ignore_missing_vars=True)
    elif frontend == 'ResNet152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            frontend_scope = 'resnet_v2_152'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_152'),
                ignore_missing_vars=True)
    elif frontend == 'MobileNetV2':
        with slim.arg_scope(mobilenet_v2.training_scope()):
            logits, end_points = mobilenet_v2.mobilenet(
                inputs,
                is_training=is_training,
                scope='mobilenet_v2',
                base_only=True)
            frontend_scope = 'mobilenet_v2'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir,
                                        'mobilenet_v2_1.4_224.ckpt'),
                var_list=slim.get_model_variables('mobilenet_v2'),
                ignore_missing_vars=True)
    elif frontend == 'InceptionV4':
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            logits, end_points = inception_v4.inception_v4(
                inputs, is_training=is_training, scope='inception_v4')
            frontend_scope = 'inception_v4'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'inception_v4.ckpt'),
                var_list=slim.get_model_variables('inception_v4'),
                ignore_missing_vars=True)
    else:
        raise ValueError(
            "Unsupported fronetnd model '%s'. This function only supports ResNet50, ResNet101, ResNet152, and MobileNetV2"
            % (frontend))

    return logits, end_points, frontend_scope, init_fn
Esempio n. 4
0
def build_deeplabv3_plus(inputs, num_classes, preset_model='DeepLabV3+-Res50', weight_decay=1e-5, is_training=True, pretrained_dir="models"):
    """
    Builds the DeepLabV3 model.

    Arguments:
      inputs: The input tensor=
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction
      num_classes: Number of classes

    Returns:
      DeepLabV3 model
    """

    if preset_model == 'DeepLabV3_plus-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope='resnet_v2_50'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'DeepLabV3_plus-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope='resnet_v2_101'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'DeepLabV3_plus-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope='resnet_v2_152'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model))


    label_size = tf.shape(inputs)[1:3]

    encoder_features = end_points['pool2']

    net = AtrousSpatialPyramidPoolingModule(end_points['pool4'])
    net = slim.conv2d(net, 256, [1, 1], scope="conv_1x1_output", activation_fn=None)
    decoder_features = Upsampling(net, label_size / 4)

    encoder_features = slim.conv2d(encoder_features, 48, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=None)

    net = tf.concat((encoder_features, decoder_features), axis=3)

    net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None)
    net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None)

    net = Upsampling(net, label_size)

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
Esempio n. 5
0
 def _build(self):
     reuse = True if self.built else None
     with slim.arg_scope(resnet_v2.resnet_arg_scope()):
         logits, end_points = resnet_v2.resnet_v2_101(
             self.input,
             num_classes=self.num_classes,
             is_training=False,
             reuse=reuse)
         self.built = True
     self.end_points = end_points
     self.logits = logits
     if not self.ckpt_loaded:
         saver = tf.train.Saver()
         saver.restore(self.sess, ckpt_dir + 'resnet_v2_101.ckpt')
         self.ckpt_loaded = True
Esempio n. 6
0
def encoder_resnet101(input_x=None, n_classes=20, is_training=True):
    with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=1e-5)):
        logits, end_points = resnet_v2.resnet_v2_101(input_x,
                                                     is_training=is_training,
                                                     scope='resnet_v2_101')
        x = tf.keras.layers.GlobalAveragePooling2D()(logits)
        x = tf.keras.layers.Dense(n_classes,
                                  activation=None,
                                  name='predictions_no_softmax')(x)
        '''
        same on tensorflow pure
        # Global average pooling
        x = tf.reduce_mean(x6_, [1,2])
        # Last layer 
        x = tf.layers.dense(x, n_classes)
        '''
        return x
Esempio n. 7
0
def build_pspnet(inputs,
                 label_size,
                 num_classes,
                 preset_model='PSPNet-Res50',
                 pooling_type="MAX",
                 weight_decay=1e-5,
                 upscaling_method="conv",
                 is_training=True,
                 pretrained_dir="models"):
    """
    Builds the PSPNet model. 

    Arguments:
      inputs: The input tensor
      label_size: Size of the final label tensor. We need to know this for proper upscaling 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes
      pooling_type: Max or Average pooling

    Returns:
      PSPNet model
    """

    if preset_model == 'PSPNet-Res50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope = 'resnet_v2_50'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'PSPNet-Res101':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope = 'resnet_v2_101'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'PSPNet-Res152':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope = 'resnet_v2_152'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError(
            "Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152"
            % (preset_model))

    feature_map_shape = [int(x / 8.0) for x in label_size]
    print(feature_map_shape)
    psp = PyramidPoolingModule(end_points['pool3'],
                               feature_map_shape=feature_map_shape,
                               pooling_type=pooling_type)

    net = slim.conv2d(psp, 512, [3, 3], activation_fn=None)
    net = slim.batch_norm(net, fused=True)
    net = tf.nn.relu(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, label_size)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
Esempio n. 8
0
def build_gcn(inputs, num_classes, preset_model='GCN-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"):
    """
    Builds the GCN model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      GCN model
    """

    inputs = mean_image_subtraction(inputs)

    if preset_model == 'GCN-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope = 'resnet_v2_50'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'GCN-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope = 'resnet_v2_101'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'GCN-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope = 'resnet_v2_152'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
    	raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model))

    


    res = [end_points['pool5'], end_points['pool4'],
         end_points['pool3'], end_points['pool2']]

    down_5 = GlobalConvBlock(res[0], n_filters=21, size=3)
    down_5 = BoundaryRefinementBlock(down_5, n_filters=21, kernel_size=[3, 3])
    down_5 = ConvUpscaleBlock(down_5, n_filters=21, kernel_size=[3, 3], scale=2)

    down_4 = GlobalConvBlock(res[1], n_filters=21, size=3)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = tf.add(down_4, down_5)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = ConvUpscaleBlock(down_4, n_filters=21, kernel_size=[3, 3], scale=2)

    down_3 = GlobalConvBlock(res[2], n_filters=21, size=3)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = tf.add(down_3, down_4)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = ConvUpscaleBlock(down_3, n_filters=21, kernel_size=[3, 3], scale=2)

    down_2 = GlobalConvBlock(res[3], n_filters=21, size=3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = tf.add(down_2, down_3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = ConvUpscaleBlock(down_2, n_filters=21, kernel_size=[3, 3], scale=2)

    net = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    net = ConvUpscaleBlock(net, n_filters=21, kernel_size=[3, 3], scale=2)
    net = BoundaryRefinementBlock(net, n_filters=21, kernel_size=[3, 3])

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
Esempio n. 9
0
def test_resnet(device, num_classes, num_layers, dataset, normalization, checkpoint):
    """
    Computes accuracy for the test dataset
    Inputs: device - gpu device
            num_classes - number of output classes 
            num_layers - number of layers selected for ResNet 
            dataset - dataset selected, options are val and test
            normalization - normalization used options are standard z-score normalization or unet normalization
            checkpoint - file where graph model weights are stored
    Output: None
    """
    print dataset
    os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # use nvidia-smi to see available options '0' means first gpu
    config = GleasonConfig() # loads pathology configuration 

    if dataset == 'val':  
        print "Using val..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/val.tfrecords')
    elif dataset == 'test':
        print "Using test..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/test.tfrecords')
    else:
        config.test_fn = None

    config.test_checkpoint = checkpoint
    print "Loading checkpoint: {0}".format(checkpoint)

    if int(num_classes) == 2:
        print "Converting to Output Shape to Binary..."
        config.output_shape = 2
    elif int(num_classes) == 4:
        config.output_shape = 4
    else:
        raise Exception('Invalid number of classes!')

    batch_size = 128
    # loading test data
    test_meta = np.load(tfrecord2metafilename(config.test_fn))
    print 'Using {0} tfrecords: {1} | {2} images'.format(dataset, config.test_fn, len(test_meta['labels']))
    test_filename_queue = tf.train.string_input_producer([config.test_fn] , num_epochs=1) # 1 epoch, passing through the
                                                                                            # the dataset once

    test_img, test_t_l, test_f_p  = read_and_decode(filename_queue = test_filename_queue,
                                           img_dims = config.input_image_size,
                                           model_dims = config.model_image_size,
                                           size_of_batch = batch_size,
                                           augmentations_dic = config.val_augmentations_dic,
                                           num_of_threads = 4,
                                           shuffle = False)
    if int(num_classes) == 2:
        print "Converting labels to Binary..."
        test_t_l = tf.clip_by_value(test_t_l, 0, 1)

    if num_layers == "50":
        print "Loading Resnet 50..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_50(inputs = test_img, 
                                                       num_classes = config.output_shape,
                                                       is_training = False) 
    elif num_layers == "101":
        print "Loading Resnet 101..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_101(inputs = test_img, 
                                                           num_classes = config.output_shape,
                                                           is_training = False)
    else:
        raise Expection("Wrong number of layers! allowed numbers are 50 and 101.")
        
    target_prob = tf.nn.softmax(test_target_logits)
    prob_and_label_files = [target_prob,  test_t_l, test_f_p]
    restorer = tf.train.Saver()
    print "Variables stored in checkpoint:"
    print_tensors_in_checkpoint_file(file_name=config.test_checkpoint, tensor_name='', all_tensors='')
    
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
      
        sess.run(tf.group(tf.global_variables_initializer(),tf.local_variables_initializer()))

        restorer.restore(sess, config.test_checkpoint)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        all_predictions_target = []
        all_predictions_t_n = []
        all_labels = []
        all_files = []

        batch_num = 1
        try:
            print "Total number of batch iterations needed: {0}".format(int(math.ceil(len(test_meta['labels'])/batch_size)))
            while not coord.should_stop():
               
                np_prob_and_label_files = sess.run(prob_and_label_files)

                target_probs = np_prob_and_label_files[0]
                labels = np_prob_and_label_files[1] 
                files = np_prob_and_label_files[2]
                
                all_labels += list(labels) 
                all_files += list(files)
                all_predictions_target += list(np.argmax(target_probs, axis=1)) 
                print "evaluating current batch number: {0}".format(batch_num)
                batch_num +=1
         
        except tf.errors.OutOfRangeError:
            print "{0} accuracy: {1:.2f}".format(dataset, (metrics.accuracy_score(all_labels, all_predictions_target)*100))
            if int(num_classes) == 2:
                print "{0} precision: {1:.2f}".format(dataset, (metrics.precision_score(all_labels, all_predictions_target)*100))
                print "{0} recall: {1:.2f}".format(dataset, (metrics.recall_score(all_labels, all_predictions_target)*100))
            print 
        finally:
            coord.request_stop()  
        coord.join(threads) 
Esempio n. 10
0
 def encoder(self, x_ph, reuse=False, trainable=True):
     with tf.variable_scope(self.name):
         x_reshape = tf.tile(x_ph, [1,1,1,3])
         with slim.arg_scope(resnet_v2.resnet_arg_scope()):
             logits, _ = resnet_v2.resnet_v2_101(x_reshape, 1001, is_training=True)
     return logits
def build_refinenet(inputs, num_classes, preset_model='RefineNet-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"):
    """
    Builds the RefineNet model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      RefineNet model
    """

    if preset_model == 'RefineNet-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'RefineNet-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'RefineNet-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
    	raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model))

    


    high = [end_points['pool5'], end_points['pool4'],
         end_points['pool3'], end_points['pool2']]

    low = [None, None, None, None]

    # Get the feature maps to the proper size with bottleneck
    high[0]=slim.conv2d(high[0], 512, 1)
    high[1]=slim.conv2d(high[1], 256, 1)
    high[2]=slim.conv2d(high[2], 256, 1)
    high[3]=slim.conv2d(high[3], 256, 1)

    # RefineNet
    low[0]=RefineBlock(high_inputs=high[0],low_inputs=None) # Only input ResNet 1/32
    low[1]=RefineBlock(high[1],low[0]) # High input = ResNet 1/16, Low input = Previous 1/16
    low[2]=RefineBlock(high[2],low[1]) # High input = ResNet 1/8, Low input = Previous 1/8
    low[3]=RefineBlock(high[3],low[2]) # High input = ResNet 1/4, Low input = Previous 1/4

    # g[3]=Upsampling(g[3],scale=4)

    net = low[3]

    net = ResidualConvUnit(net)
    net = ResidualConvUnit(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, scale=4)

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
Esempio n. 12
0
def build_icnet(inputs, label_size, num_classes, preset_model='ICNet-Res50', pooling_type = "MAX",
    weight_decay=1e-5, is_training=True, pretrained_dir="models"):
    """
    Builds the ICNet model. 

    Arguments:
      inputs: The input tensor
      label_size: Size of the final label tensor. We need to know this for proper upscaling 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes
      pooling_type: Max or Average pooling

    Returns:
      ICNet model
    """

    inputs_4 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*4,  tf.shape(inputs)[2]*4])   
    inputs_2 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*2,  tf.shape(inputs)[2]*2])
    inputs_1 = inputs

    if preset_model == 'ICNet-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_50(inputs_4, is_training=is_training, scope='resnet_v2_50')
            logits_16, end_points_16 = resnet_v2.resnet_v2_50(inputs_2, is_training=is_training, scope='resnet_v2_50')
            logits_8, end_points_8 = resnet_v2.resnet_v2_50(inputs_1, is_training=is_training, scope='resnet_v2_50')
            resnet_scope='resnet_v2_50'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'ICNet-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_101(inputs_4, is_training=is_training, scope='resnet_v2_101')
            logits_16, end_points_16 = resnet_v2.resnet_v2_101(inputs_2, is_training=is_training, scope='resnet_v2_101')
            logits_8, end_points_8 = resnet_v2.resnet_v2_101(inputs_1, is_training=is_training, scope='resnet_v2_101')
            resnet_scope='resnet_v2_101'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'ICNet-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_152(inputs_4, is_training=is_training, scope='resnet_v2_152')
            logits_16, end_points_16 = resnet_v2.resnet_v2_152(inputs_2, is_training=is_training, scope='resnet_v2_152')
            logits_8, end_points_8 = resnet_v2.resnet_v2_152(inputs_1, is_training=is_training, scope='resnet_v2_152')
            resnet_scope='resnet_v2_152'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model))



    feature_map_shape = [int(x / 32.0) for x in label_size]
    block_32 = PyramidPoolingModule(end_points_32['pool3'], feature_map_shape=feature_map_shape, pooling_type=pooling_type)

    out_16, block_16 = CFFBlock(psp_32, end_points_16['pool3'])
    out_8, block_8 = CFFBlock(block_16, end_points_8['pool3'])
    out_4 = Upsampling_by_scale(out_8, scale=2)
    out_4 = slim.conv2d(out_4, num_classes, [1, 1], activation_fn=None) 

    out_full = Upsampling_by_scale(out_4, scale=2)
    
    out_full = slim.conv2d(out_full, num_classes, [1, 1], activation_fn=None, scope='logits')

    net = tf.concat([out_16, out_8, out_4, out_final])

    return net, init_fn
Esempio n. 13
0
def build_deeplabv3(inputs,
                    num_classes,
                    preset_model='DeepLabV3-Res50',
                    upscaling_method="bilinear",
                    weight_decay=1e-5,
                    is_training=True,
                    pretrained_dir="models"):
    """
    Builds the DeepLabV3 model. 

    Arguments:
      inputs: The input tensor= 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      DeepLabV3 model
    """

    inputs = mean_image_subtraction(inputs)

    if preset_model == 'DeepLabV3-Res50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope = 'resnet_v2_50'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'DeepLabV3-Res101':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope = 'resnet_v2_101'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'DeepLabV3-Res152':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope = 'resnet_v2_152'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError(
            "Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152"
            % (preset_model))

    label_size = tf.shape(inputs)[1:3]

    net = AtrousSpatialPyramidPoolingModule(end_points['pool5'])

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, label_size)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
Esempio n. 14
0
def train(flag, args):
    tf.reset_default_graph()

    queue_loader = data_loader(flag,
                               batch_size=args.bsize,
                               num_epochs=args.ep,
                               dataset_dir=args.dataset_dir)

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits, end_points = resnet_v2.resnet_v2_101(queue_loader.images,
                                                     764,
                                                     is_training=flag)

        g_step = tf.Variable(0, trainable=False, dtype=tf.int64, name='g_step')
        # g_step = tf.train.create_global_step()
        decay_steps = int(math.floor(43971 / args.bsize * 2.0))
        lr = tf.train.exponential_decay(args.lr,
                                        global_step=g_step,
                                        decay_steps=decay_steps,
                                        decay_rate=0.94,
                                        staircase=True)

        # loss_AuxLogits = tf.losses.sparse_softmax_cross_entropy( labels=queue_loader.labels, logits=end_points['AuxLogits'], weights=0.4)
        total_loss = tf.losses.sparse_softmax_cross_entropy(
            labels=queue_loader.labels, logits=logits, weights=1.0)

        # total_loss = tf.losses.get_total_loss(add_regularization_losses=False)

        #         train_op = tf.train.RMSPropOptimizer(lr).minimize(total_loss)

        labels = queue_loader.labels
        correct = tf.equal(tf.argmax(logits, 1), labels)

        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

        # tf.summary.scalar('AuxLogits loss', loss_AuxLogits)
        tf.summary.scalar('accuracy', accuracy)
        tf.summary.scalar('total loss', total_loss)
        tf.summary.scalar('learnning_rate', lr)
        merged_op = tf.summary.merge_all()
        # for var in slim.get_model_variables():
        #     if var.op.name.startswith("Variabl"):
        #         print(var)

        with tf.variable_scope('Adam_vars'):
            # optimizer = tf.train.AdamOptimizer(lr).minimize(loss_op,global_step=global_step)
            optimizer = tf.train.AdamOptimizer(lr)
            grads_vars_tuple_list = optimizer.compute_gradients(total_loss)
            grads, vars = zip(*grads_vars_tuple_list)
            new_grads, _ = tf.clip_by_global_norm(grads, 5)
            new_grads_vars_tuple_list = zip(new_grads, vars)
            train_op = optimizer.apply_gradients(new_grads_vars_tuple_list,
                                                 global_step=g_step)
            # optimizer = tf.train.AdamOptimizer(lr)
            # grads_and_vars = optimizer.compute_gradients(loss_op)
            # for i,(grad,var) in enumerate(grads_and_vars):
            #     if grad is not None:
            #         grads_and_vars[i] = (tf.clip_by_norm(grad,5),var)
            # train_op = optimizer.apply_gradients(grads_and_vars,global_step=global_step)

        if len(os.listdir(args.modelpath)) > 0:
            var_to_restore = slim.get_model_variables()
            ckpt_path = tf.train.latest_checkpoint(args.modelpath)
            print('###########################ckpt_path', ckpt_path)

        else:
            # var_to_restore = slim.get_variables_to_restore(exclude=['InceptionV3/AuxLogits/Conv2d_1b_1x1',
            #                                                 'InceptionV3/Logits/Conv2d_1b_1x1/Conv2d_1c_1x1'])
            var_to_restore = slim.get_variables_to_restore(
                exclude=['resnet_v2_101/logits', 'Adam_vars', 'g_step'])
            # 'InceptionV4/AuxLogits/Conv2d_1b_1x1/BatchNorm/beta/Adam',
            # 'InceptionV4/AuxLogits/Conv2d_1b_1x1/weights/Adam'
            # Aux_logit = slim.get_variables_to_restore(include=['InceptionV4/AuxLogits/Aux_logits'])
            # Logit = slim.get_variables_to_restore(include=['InceptionV4/Logits/Logits'])
            # adam = slim.get_variables_to_restore(include=['Adam_vars'])
            ckpt_path = tf.train.latest_checkpoint(args.dataset_dir2)
            print('###########################ckpt_path', ckpt_path)
        # Aux_logit_init = tf.variables_initializer(Aux_logit)
        # Logit_init = tf.variables_initializer(Logit)
        # adam_init = tf.variables_initializer(adam)
        init_func = slim.assign_from_checkpoint_fn(ckpt_path, var_to_restore)

        saver = tf.train.Saver(max_to_keep=1)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        writer = tf.summary.FileWriter(args.trainlog, sess.graph)
        sess.run(
            tf.group(tf.global_variables_initializer(),
                     tf.local_variables_initializer()))
        # sess.run(Aux_logit_init)
        # sess.run(Logit_init)
        # sess.run(adam_init)
        init_func(sess)
        print(
            '###################################### restore checkpoint sucessful#######################'
        )

        print('Start training')
        print('batch size: %d, epoch: %d, initial learning rate: %.3f' %
              (args.bsize, args.ep, args.lr))
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        try:
            ep = 1
            correct_all = 0
            start = datetime.now()
            while not coord.should_stop():
                # logit, loss, correct_list, _, summary,acc,g_s= sess.run([
                #     logits,loss_op,correct, train_op, merged_op,accuracy,global_step])
                logit, loss, correct_list, _, summary, acc, g_s, l = sess.run([
                    logits, total_loss, correct, train_op, merged_op, accuracy,
                    g_step, lr
                ])

                writer.add_summary(summary, g_s)

                # print('argmax logits',np.argmax(logits,1),sess.run(queue_loader.labels))
                # print('correct.sum :  ',correct.sum())
                correct_all += correct_list.sum()
                # step_accuracy = correct_list.sum()*100.0/args.bsize

                if g_s % 10 == 0:
                    end_time = datetime.now()
                    print(
                        'epoch: %2d, globle_step: %3d,accuracy : %.2f%%,loss: %.3f, cost time : %s sec'
                        % (ep, g_s, acc * 100.0, loss, end_time - start))
                    start = datetime.now()
                if g_s != 0 and g_s % queue_loader.num_batches == 0:
                    print('EPOCH %2d is end, ACCURACY: %.2f%%.' %
                          (ep, correct_all * 100.0 /
                           (queue_loader.num_batches * args.bsize)))

                    saver.save(sess,
                               os.path.join(args.modelpath, 'model.ckpt'),
                               global_step=g_s)
                    ep += 1
                    correct_all = 0

        except tf.errors.OutOfRangeError:
            print('\nDone training, epoch limit: %d reached.' % (ep))
        finally:
            coord.request_stop()

        coord.join(threads)
        sess.close()
        print('Done')
def build_refinenet(inputs,
                    num_classes,
                    preset_model='RefineNet-Res101',
                    weight_decay=1e-5,
                    is_training=True,
                    upscaling_method="bilinear",
                    pretrained_dir="models"):
    """
    Builds the RefineNet model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      RefineNet model
    """

    inputs = mean_image_subtraction(inputs)

    if preset_model == 'RefineNet-Res50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'RefineNet-Res101':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'RefineNet-Res152':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError(
            "Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152"
            % (preset_model))

    f = [
        end_points['pool5'], end_points['pool4'], end_points['pool3'],
        end_points['pool2']
    ]

    g = [None, None, None, None]
    h = [None, None, None, None]

    for i in range(4):
        h[i] = slim.conv2d(f[i], 256, 1)

    g[0] = RefineBlock(high_inputs=None, low_inputs=h[0])
    g[1] = RefineBlock(g[0], h[1])
    g[2] = RefineBlock(g[1], h[2])
    g[3] = RefineBlock(g[2], h[3])

    # g[3]=Upsampling(g[3],scale=4)

    net = g[3]

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, scale=4)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
Esempio n. 16
0
    def _resnet_v2(self, is_training):
        with slim.arg_scope(resnet_arg_scope()):
            pool, _ = resnet_v2_101(self._image, is_training=is_training)

        return pool