예제 #1
0
    def __init__(self, args):

        self.imdb_name = args.imdb_name
        self.net_name = args.net_name
        self.tag = args.tag
        self.iters = args.iters

        # Config
        cfg_file = osp.join(mrcn_dir,
                            'experiments/cfgs/%s.yml' % self.net_name)
        cfg_list = [
            'ANCHOR_SCALES', [4, 8, 16, 32], 'ANCHOR_RATIOS', [0.5, 1, 2]
        ]
        if cfg_file is not None: cfg_from_file(cfg_file)
        if cfg_list is not None: cfg_from_list(cfg_list)
        print('Using config:')
        pprint.pprint(cfg)

        # load imdb
        self.imdb = get_imdb(get_imdb_name(self.imdb_name)['TEST_IMDB'])

        # Load network
        self.net = self.load_net()

        self._scale = None
    def __init__(self, rpn_model_path, rcnn_model_path, cfg_file, classes=None, max_image_size=None, anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2), max_object_per_image=15, conf_thresh=0.3, nms_thresh=0.5, iou_thresh=0.5):
        self.rpn_model_path = rpn_model_path
        self.rcnn_model_path = rcnn_model_path
        self.cfg_file = cfg_file
        if classes is None:
            classes = ['background', 'object']
        self._classes = classes
        self.num_classes = len(self._classes)
        self._max_image_size = max_image_size
        self.num_images = 1
        self.conf_thresh = conf_thresh
        self.nms_thresh = nms_thresh
        self.iou_thresh = iou_thresh
        self.max_object_per_image = max_object_per_image
        self._feat_stride = [16, ]
        self._feat_compress = [1. / 16., ]
                
        # load network configuration
        cfg_from_file(self.cfg_file)
        # pprint.pprint(cfg)
        self._anchor_scales = cfg.ANCHOR_SCALES
        self._num_scales = len(self._anchor_scales)
        self._anchor_ratios = cfg.ANCHOR_RATIOS
        self._num_ratios = len(self._anchor_ratios)
        self._num_anchors = self._num_scales * self._num_ratios

        with tf.gfile.FastGFile(self.rpn_model_path , 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            with tf.Graph().as_default() as graph:
                tf.import_graph_def(graph_def, name="rpn")
                self._images = graph.get_tensor_by_name('rpn/Placeholder:0')
                self._rpn_cls_prob = graph.get_tensor_by_name('rpn/MobilenetV1_2/rpn_cls_prob/transpose_1:0')
                self._rpn_bbox_pred = graph.get_tensor_by_name('rpn/MobilenetV1_2/rpn_bbox_pred/BiasAdd:0')
                self._rpn_feature_map = graph.get_tensor_by_name('rpn/MobilenetV1_1/Conv2d_11_pointwise/Relu6:0')
                self.rpn_graph = graph

        with tf.gfile.FastGFile(self.rcnn_model_path , 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            with tf.Graph().as_default() as graph:
                tf.import_graph_def(graph_def, name="rcnn")
                self._rcnn_feature_map = graph.get_tensor_by_name('rcnn/Placeholder_3:0')
                self._rois = graph.get_tensor_by_name('rcnn/Placeholder_4:0')
                self._cls_prob = graph.get_tensor_by_name('rcnn/MobilenetV1_2/cls_prob:0')
                self._bbox_pred = graph.get_tensor_by_name('rcnn/add:0')
                self.rcnn_graph = graph

        # set config
        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth=True
        # tfconfig=tf.ConfigProto(log_device_placement=False,allow_soft_placement=True)
        self.rpn_sess = tf.Session(graph=self.rpn_graph, config=tfconfig) 
        self.rcnn_sess = tf.Session(graph=self.rcnn_graph, config=tfconfig)
예제 #3
0
def main(args):

    opt = vars(args)

    # initialize
    opt['dataset_splitBy'] = opt['dataset'] + '_' + opt['splitBy']
    checkpoint_dir = osp.join(opt['checkpoint_path'], opt['dataset_splitBy'])
    if not osp.isdir(checkpoint_dir): os.makedirs(checkpoint_dir)

    # set random seed
    torch.manual_seed(opt['seed'])
    random.seed(opt['seed'])

    # set up loader
    data_json = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.json')
    data_h5 = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.h5')

    loader = GtMRCNLoader(data_json, data_h5)

    # set up model
    opt['vocab_size'] = loader.vocab_size
    opt['C4_feat_dim'] = 1024
    net = resnetv1(opt, batch_size=opt['batch_size'],
                   num_layers=101)  # determine batch size in opt.py

    # output directory where the models are saved
    output_dir = osp.join(opt['dataset_splitBy'],
                          'output_{}'.format(opt['output_postfix']))
    print('Output will be saved to `{:s}`'.format(output_dir))

    # tensorboard directory where the summaries are saved during training
    tb_dir = osp.join(opt['dataset_splitBy'],
                      'tb_{}'.format(opt['output_postfix']))
    print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))

    # also add the validation set, but with no flipping images
    orgflip = cfg.TRAIN.USE_FLIPPED
    cfg.TRAIN.USE_FLIPPED = False
    cfg.TRAIN.USE_FLIPPED = orgflip

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    #train_net(net, imdb, roidb, valroidb, output_dir, tb_dir,
    train_net(
        net,
        loader,
        output_dir,
        tb_dir,
        pretrained_model=
        'pyutils/mask-faster-rcnn/output/res101/coco_2014_train_minus_refer_valtest+coco_2014_valminusminival/notime/res101_mask_rcnn_iter_1250000.pth',
        max_iters=args.max_iters)
def export(mode,
           net_name,
           model_path,
           config_file,
           num_classes=2,
           feature_channel=128,
           export_path='./',
           model_name='faster_rcnn.pbtxt'):
    if not os.path.isfile(model_path + '.meta'):
        raise IOError(
            ('{:s} not found.\nDid you download the proper networks from '
             'our server and place them properly?').format(model_path +
                                                           '.meta'))

    # load network configuration
    cfg_from_file(config_file)
    # pprint.pprint(cfg)

    # load network
    if net_name == 'vgg16':
        net = vgg16(batch_size=1)
    elif net_name == 'res50':
        net = resnetv1(batch_size=1, num_layers=50)
    elif net_name == 'res101':
        net = resnetv1(batch_size=1, num_layers=101)
    elif net_name == 'res152':
        net = resnetv1(batch_size=1, num_layers=152)
    elif net_name == 'mobile':
        net = mobilenetv1(batch_size=1)
    else:
        raise NotImplementedError

    # set config
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    # tfconfig=tf.ConfigProto(log_device_placement=False,allow_soft_placement=True)
    # init session
    sess = tf.Session(config=tfconfig)
    with sess.as_default(), tf.device('/cpu:0'):
        net.create_architecture(sess,
                                mode,
                                num_classes,
                                tag='default',
                                anchor_scales=cfg.ANCHOR_SCALES,
                                anchor_ratios=cfg.ANCHOR_RATIOS,
                                feature_channel=feature_channel)

        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        print('restored from ckpt: {}'.format(model_path))
        tf.train.write_graph(sess.graph_def, export_path, model_name)
        print("exported graph to: {}{}".format(export_path, model_name))
예제 #5
0
  def __init__(self, args):

    self.imdb_name = args.imdb_name
    self.net_name = args.net_name
    self.tag = args.tag
    self.iters = args.iters

    # Config
    cfg_file = osp.join(mrcn_dir, 'experiments/cfgs/%s.yml' % self.net_name)
    cfg_list = ['ANCHOR_SCALES', [4,8,16,32], 'ANCHOR_RATIOS', [0.5,1,2]]
    if cfg_file is not None: cfg_from_file(cfg_file)
    if cfg_list is not None: cfg_from_list(cfg_list)
    print('Using config:')
    pprint.pprint(cfg)

    # Load network
    self.num_classes = 81  # hard code this
    self.net = self.load_net()
예제 #6
0
def my_main(imdb_name, network, cfg_file, set_cfgs, tag, max_iters, im_names,
            score_thresh, clip_bbox):

    # Clip bboxes after bbox reg to image boundary
    cfg_from_list(['TEST.BBOX_CLIP', str(clip_bbox)])

    # Already set everything here, so the path can be determined correctly
    if cfg_file:
        cfg_from_file(cfg_file)
    if set_cfgs:
        cfg_from_list(set_cfgs)

    model_dir = osp.abspath(
        osp.join(cfg.ROOT_DIR, 'output', 'frcnn', cfg.EXP_DIR, imdb_name, tag))
    model = osp.join(
        model_dir,
        cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(max_iters) + '.pth')
    output_dir = osp.join(model_dir, 'demo')
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    args = {
        'imdb_name': imdb_name,
        'net': network,
        'cfg_file': None,
        'set_cfgs': None,
        'tag': tag,
        'output_dir': output_dir,
        'model': model,
        'im_names': im_names,
        'score_thresh': score_thresh
    }

    print('Called with args:')
    print(args)

    frcnn_demo(args)
예제 #7
0
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    # if has model, get the name from it
    # if does not, then just use the initialization weights
    if args.model:
        filename = os.path.splitext(os.path.basename(args.model))[0]
    else:
        filename = os.path.splitext(os.path.basename(args.weight))[0]

    tag = args.tag
    tag = tag if tag else 'default'
예제 #8
0
    jfn = '/om/user/mcusi/nnInit/pytorch-faster-rcnn/data/' + dataname + '/special_demos/' + image_name + '_' + exptname + '_c' + str(
        CONF_THRESH) + '_n' + str(NMS_THRESH) + '.json'
    jfn = '/om2/user/mcusi/bayesianASA/sounds/' + image_name + '.json'
    with open(jfn, 'w') as outfile:
        outfile.write(json.dumps(elements))


if __name__ == '__main__':

    folder = ''
    dataname = 'bASAGP1'  #os.environ.get('dataname','bASA')
    exptname = 'vgg16_ar4_anch6_mult4'  #os.environ.get('exptname','vgg16_ar4')
    iteration = 200000

    cfg_from_file('experiments/cfgs/vgg16.yml'
                  )  # Add usual config options for network type
    cfg_from_file(
        'output/vgg16/' + dataname + '_train/default/' + folder + exptname +
        '.yml')  # add config options for that particular trained network
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    # model path
    saved_model = 'output/vgg16/' + dataname + '_train/default/' + folder + exptname + '_iter_' + str(
        iteration) + '.pth'

    # load network
    net = vgg16()
    net.create_architecture(3,
                            tag='default',
                            anchor_scales=cfg.ANCHOR_SCALES,
                            anchor_ratios=cfg.ANCHOR_RATIOS)
예제 #9
0
def evaluate(args):

    opt = vars(args)

    # make other options
    opt['dataset_splitBy'] = opt['dataset'] + '_' + opt['splitBy']

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    # set up loader
    data_json = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.json')
    data_h5 = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.h5')
    loader = GtMRCNLoader(data_json, data_h5)

    # set up model
    opt['vocab_size'] = loader.vocab_size
    opt['C4_feat_dim'] = 1024
    net = resnetv1(opt, batch_size=1, num_layers=101)

    net.create_architecture(81,
                            tag='default',
                            anchor_scales=cfg.ANCHOR_SCALES,
                            anchor_ratios=cfg.ANCHOR_RATIOS)

    sfile = osp.join(opt['dataset_splitBy'],
                     'output_{}'.format(opt['output_postfix']),
                     'res101_mask_rcnn_iter_{}.pth'.format(opt['model_iter']))
    print('Restoring model snapshots from {:s}'.format(sfile))
    saved_state_dict = torch.load(str(sfile))
    count_1 = 0
    new_params = net.state_dict().copy()
    for name, param in new_params.items():
        #print(name, param.size(), saved_state_dict[name].size())
        if name in saved_state_dict and param.size(
        ) == saved_state_dict[name].size():
            new_params[name].copy_(saved_state_dict[name])
            #print('---- copy ----')
        else:
            print(name, '----')
            count_1 += 1
    print('size not match:', count_1)
    net.load_state_dict(new_params)

    net.eval()
    net.cuda()

    split = opt['split']

    crit = None
    acc, eval_seg_iou_list, seg_correct, seg_total, cum_I, cum_U, num_sent = eval_split(
        loader, net, crit, split, opt)
    print('Comprehension on %s\'s %s (%s sents) is %.2f%%' % \
          (opt['dataset_splitBy'], split, num_sent, acc*100.))

    # write to results.txt
    f = open('experiments/det_results.txt', 'a')
    f.write('[%s][%s], id[%s]\'s acc is %.2f%%\n' % \
            (opt['dataset_splitBy'], opt['split'], opt['id'], acc*100.0))

    # print
    print('Segmentation results on [%s][%s]' % (opt['dataset_splitBy'], split))
    results_str = ''
    for n_eval_iou in range(len(eval_seg_iou_list)):
        results_str += '    precision@%s = %.2f\n' % \
          (str(eval_seg_iou_list[n_eval_iou]), seg_correct[n_eval_iou]*100./seg_total)
    results_str += '    overall IoU = %.2f\n' % (cum_I * 100. / cum_U)
    print(results_str)

    # save results
    #save_dir = osp.join('cache/results', opt['dataset_splitBy'], 'masks')
    #if not osp.isdir(save_dir):
    #  os.makedirs(save_dir)

    #results['iou'] = cum_I*1./cum_U
    #assert 'rle' in results['predictions'][0]
    #with open(osp.join(save_dir, args.id+'_'+split+'.json'), 'w') as f:
    #  json.dump(results, f)

    # write to results.txt
    f = open('experiments/mask_results.txt', 'a')
    f.write('[%s][%s]\'s iou is:\n%s' % \
            (opt['dataset_splitBy'], split, results_str))
from model.config import cfg, cfg_from_file, cfg_from_list
from datasets.factory import get_imdb
import argparse
import pprint
import time, os, sys

from nets.vgg16 import vgg16
from nets.resnet_v1 import resnetv1
from nets.mobilenet_v1 import mobilenetv1

import torch

if __name__ == '__main__':
    imdb_name = 'Lip_320_train'
    cfg_from_file(
        '/media/rgh/rgh-data/PycharmProjects/cvpr2018/experiments/cfgs/vgg16.yml'
    )
    tag = 'default/rgh'
    model = '/media/rgh/rgh-data/PycharmProjects/cvpr2018/output/vgg16/Lip_320_train/default/' \
                       + 'vgg16_faster_rcnn_iter_70000.pth'
    cfg.ANCHOR_SCALES = [4, 8, 16, 32]
    cfg.ANCHOR_RATIOS = [0.5, 1, 2]
    #cfg.HAS_PARSING_LABEL = False
    cfg.POOLING_MODE = 'crop'
    cfg.FC6_IN_CHANNEL = 512
    cfg.TEST.CLEAN_PRE_RESULT = True
    print('Using config:')
    pprint.pprint(cfg)
    imdb = get_imdb(imdb_name)
    net = vgg16()
    net.create_architecture(imdb.num_classes,
예제 #11
0
def main(args):
  opt = vars(args)
  
  # initialize
  opt['dataset_splitBy'] = opt['dataset'] + '_' + opt['splitBy']
  checkpoint_dir = osp.join(opt['checkpoint_path'], opt['dataset_splitBy'])
  if not osp.isdir(checkpoint_dir): os.makedirs(checkpoint_dir)

  # set random seed
  torch.manual_seed(opt['seed'])
  random.seed(opt['seed'])
  
  # set up loader
  data_json = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.json')
  data_h5 = osp.join('cache/prepro', opt['dataset_splitBy'], 'data.h5')
  
  loader = CycleLoader(data_json, data_h5) ####
  
  # set up model
  opt['vocab_size']= loader.vocab_size
  #opt['fc7_dim']   = loader.fc7_dim
  #opt['pool5_dim'] = loader.pool5_dim
  #opt['num_atts']  = loader.num_atts
  #model = JointMatching(opt)
  opt['C4_feat_dim'] = 1024
  opt['use_att'] = utils.if_use_att(opt['caption_model'])
  opt['seq_length'] = loader.label_length
  
  
  #### can change to restore opt from info.pkl
  #infos = {}
  #histories = {}
  if opt['start_from']is not None:
    # open old infos and check if models are compatible
    with open(os.path.join(opt['dataset_splitBy'], opt['start_from'], 'infos-best.pkl')) as f:
      infos = cPickle.load(f)
      saved_model_opt = infos['opt']
      need_be_same = ['caption_model', 'rnn_type', 'rnn_size', 'num_layers']
      for checkme in need_be_same:
        assert vars(saved_model_opt)[checkme] == opt[checkme], "Command line argument and saved model disagree on '%s'" % checkme

    #if os.path.isfile(os.path.join(opt['dataset_splitBy'], opt['start_from'], 'histories.pkl')):
    #  with open(os.path.join(opt['dataset_splitBy'], opt['start_from'], 'histories.pkl')) as f:
    #    histories = cPickle.load(f)
  
  
  
  net = resnetv1(opt, batch_size=opt['batch_size'], num_layers=101) #### determine batch size in opt.py
  
  # output directory where the models are saved
  output_dir = osp.join(opt['dataset_splitBy'], 'output_{}'.format(opt['output_postfix']))
  print('Output will be saved to `{:s}`'.format(output_dir))

  # tensorboard directory where the summaries are saved during training
  tb_dir = osp.join(opt['dataset_splitBy'], 'tb_{}'.format(opt['output_postfix']))
  print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))

  # also add the validation set, but with no flipping images
  orgflip = cfg.TRAIN.USE_FLIPPED
  cfg.TRAIN.USE_FLIPPED = False
  #_, valroidb = combined_roidb('coco_2014_minival')
  #_, valroidb = combined_roidb('refcoco_test')
  #print('{:d} validation roidb entries'.format(len(valroidb)))
  cfg.TRAIN.USE_FLIPPED = orgflip
  
  if args.cfg_file is not None:
    cfg_from_file(args.cfg_file)
  if args.set_cfgs is not None:
    cfg_from_list(args.set_cfgs)
  
  
  #train_net(net, imdb, roidb, valroidb, output_dir, tb_dir,
  train_net(net, loader, output_dir, tb_dir,
            pretrained_model='pyutils/mask-faster-rcnn/output/res101/coco_2014_train_minus_refer_valtest+coco_2014_valminusminival/notime/res101_mask_rcnn_iter_1250000.pth',
            max_iters=args.max_iters)
예제 #12
0
    processed_ims.append(im)

  # Create a blob to hold the input images
  blob = im_list_to_blob(processed_ims)

  return blob, np.array(im_scale_factors)


if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    # if has model, get the name from it
    # if does not, then just use the initialization weights
    if args.model:
        filename = os.path.splitext(os.path.basename(args.model))[0]
    else:
        filename = os.path.splitext(os.path.basename(args.weight))[0]

    tag = args.tag
    tag = tag if tag else 'default'
예제 #13
0
    def __init__(self,
                 net_name,
                 model_path,
                 cfg_file,
                 num_classes=2,
                 max_object_per_image=15,
                 conf_thresh=0.3,
                 nms_thresh=0.5,
                 iou_thresh=0.5):
        self.net_name = net_name
        # self.sess = sess
        self.model_path = model_path
        self.cfg_file = cfg_file
        self.num_images = 1
        self.num_classes = num_classes
        self.conf_thresh = conf_thresh
        self.nms_thresh = nms_thresh
        self.iou_thresh = iou_thresh
        self.max_object_per_image = max_object_per_image

        # set config
        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True
        # tfconfig=tf.ConfigProto(log_device_placement=False,allow_soft_placement=True)
        # init session
        self.sess = tf.Session(config=tfconfig)

        if not os.path.isfile(self.model_path + '.meta'):
            raise IOError((
                '{:s} not found.\nDid you download the proper networks from '
                'our server and place them properly?').format(self.model_path +
                                                              '.meta'))

        # load network configuration
        cfg_from_file(self.cfg_file)
        # pprint.pprint(cfg)

        # load network
        if self.net_name == 'vgg16':
            self.net = vgg16(batch_size=1)
        elif self.net_name == 'res50':
            self.net = resnetv1(batch_size=1, num_layers=50)
        elif self.net_name == 'res101':
            self.net = resnetv1(batch_size=1, num_layers=101)
        elif self.net_name == 'res152':
            self.net = resnetv1(batch_size=1, num_layers=152)
        elif self.net_name == 'mobile':
            self.net = mobilenetv1(batch_size=1)
        else:
            raise NotImplementedError

        with self.sess.as_default():
            self.net.create_architecture(self.sess,
                                         "TEST",
                                         self.num_classes,
                                         tag='default',
                                         anchor_scales=cfg.ANCHOR_SCALES,
                                         anchor_ratios=cfg.ANCHOR_RATIOS)

            saver = tf.train.Saver()
            saver.restore(self.sess, self.model_path)
예제 #14
0
def testing(imdbval_name, classes, cfg_file, model, weights, tag, net,
            max_per_image):

    __sets = {}

    for split in ['train', 'val', 'trainval', 'test']:
        name = imdbval_name.split('_')[0] + '_{}'.format(split)
        __sets[name] = (lambda split=split: dataset(split, classes,
                                                    name.split('_')[0]))

    if cfg_file is not None:
        cfg_from_file(cfg_file)

    print('Using config:')
    pprint.pprint(cfg)

    # if has model, get the name from it
    # if does not, then just use the inialization weights
    if model:
        filename = os.path.splitext(os.path.basename(model))[0]
    else:
        filename = os.path.splitext(os.path.basename(weights))[0]

    tag = tag if tag else 'default'
    filename = tag + '/' + filename
    imdb = get_imdb(imdbval_name, __sets)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True

    # init session
    sess = tf.Session(config=tfconfig)
    # load network
    if net == 'vgg16':
        net = vgg16(batch_size=1)
    elif net == 'res50':
        net = resnetv1(batch_size=1, num_layers=50)
    elif net == 'res101':
        net = resnetv1(batch_size=1, num_layers=101)
    elif net == 'res152':
        net = resnetv1(batch_size=1, num_layers=152)
    else:
        raise NotImplementedError

    # load model
    net.create_architecture(sess,
                            "TEST",
                            imdb.num_classes,
                            tag='default',
                            anchor_scales=cfg.ANCHOR_SCALES,
                            anchor_ratios=cfg.ANCHOR_RATIOS)

    if model:
        print(('Loading model check point from {:s}').format(model))
        saver = tf.train.Saver()
        saver.restore(sess, model)
        print('Loaded.')
    else:
        print(('Loading initial weights from {:s}').format(weights))
        sess.run(tf.global_variables_initializer())
        print('Loaded.')

    test_net(sess, net, imdb, filename, max_per_image=max_per_image)

    sess.close()
예제 #15
0
def infer_faster_rcnn(net, classes, cfg_file, model, imgfolder, im_names,
                      outfolder, crop, save_inferences):

    if cfg_file is not None:
        cfg_from_file(cfg_file)

    print('Using config:')
    pprint.pprint(cfg)

    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    # model path
    classes = ('__background__', ) + tuple(classes.strip().split(','))
    anchors = cfg.ANCHOR_SCALES
    anchors = map(int, anchors)

    # Add bboxes.txt to storage all the bboxes and scores in a .txt file:
    bboxes_list = []

    if not os.path.isfile(model + '.meta'):
        raise IOError(
            ('{:s} not found.\nDid you download the proper networks from '
             'our server and place them properly? If you want something '
             'simple and handy, try ./tools/demo_depre.py first.'
             ).format(model + '.meta'))

    # set config
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True

    # init session
    sess = tf.Session(config=tfconfig)
    # load network
    if net == 'vgg16':
        net = vgg16(batch_size=1)
    elif net == 'res50':
        net = resnetv1(batch_size=1, num_layers=50)
    elif net == 'res101':
        net = resnetv1(batch_size=1, num_layers=101)
    elif net == 'res152':
        net = resnetv1(batch_size=1, num_layers=152)
    else:
        raise NotImplementedError
    net.create_architecture(sess,
                            "TEST",
                            len(classes),
                            tag='default',
                            anchor_scales=anchors)
    saver = tf.train.Saver()
    saver.restore(sess, model)

    print('Loaded network {:s}'.format(model))

    for im_name in im_names:
        print('Processing {} from {}'.format(im_name, imgfolder))
        bboxes_list = process_image(sess, net, imgfolder, im_name, classes,
                                    bboxes_list, outfolder, crop,
                                    save_inferences)
    print('')
    print('Saving {}/bboxes.txt'.format(outfolder))
    print('')
    with open(os.path.join(outfolder, 'bboxes.txt'), 'w') as bboxes_file:
        for i in range(len(bboxes_list)):
            bboxes_file.write(bboxes_list[i])
예제 #16
0
    cfg_file = '/mnt/data/tf-faster-rcnn-master/experiments/cfgs/vgg16.yml'
    set_cfgs = [
        'ANCHOR_SCALES', '[8,16,32]', 'ANCHOR_RATIOS', '[0.5,1,2]',
        'TRAIN.STEPSIZE', '[40000]'
    ]
    imdb_name = 'voc_2007_trainval'
    imdbval_name = 'voc_2007_test'
    max_iters = 20000
    net = 'vgg16'
    tag = None
    max_per_image = 100
    weight = '/mnt/data//tf-faster-rcnn-master/data/imagenet_weights/vgg16.ckpt'

    if cfg_file is not None:
        cfg_from_file(cfg_file)
    if set_cfgs is not None:
        cfg_from_list(set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    np.random.seed(cfg.RNG_SEED)

    # train set
    imdb, roidb = combined_roidb(imdb_name)
    print('{:d} roidb entries'.format(len(roidb)))

    # output directory where the models are saved
    output_dir = get_output_dir(imdb, tag)
    print('Output will be saved to `{:s}`'.format(output_dir))
    def launch_test(self, conf, hash_model):
        '''
        '''
        args = {}
        args['cfg_file'] = conf.frcnn_cfg
        args['weight'] = conf.starting_weights
        args['model'] = hash_model
        args['imdb_name'] = conf.valid_set
        args['comp_mode'] = False
        args['tag'] = conf.frcnn_tag
        args['net'] = conf.frcnn_net
        args['set_cfgs'] = None
        args['max_per_image'] = 5

        print('Called with args:')
        print(args)

        if args['cfg_file'] is not None:
            cfg_from_file(argsargs['cfg_file'])
        if args['set_cfgs'] is not None:
            cfg_from_list(args['set_cfgs'])

        print('Using config:')
        pprint.pprint(cfg)

        # if has model, get the name from it
        # if does not, then just use the inialization weights
        if args['model']:
            filename = os.path.splitext(os.path.basename(args['model']))[0]
        else:
            filename = os.path.splitext(os.path.basename(args['weight']))[0]

        tag = args['tag']
        tag = tag if tag else 'default'
        filename = tag + '/' + filename

        # TODO This is really bad but it works, I'm sincerely sorry
        conf_copy = copy.deepcopy(conf)
        conf_copy.train_set = conf_copy.valid_set
        imdb = get_imdb(args['imdb_name'], conf_copy)
        print(args['imdb_name'])
        imdb.competition_mode(args['comp_mode'])

        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True

        # init session
        sess = tf.Session(config=tfconfig)
        # load network
        if args['net'] == 'vgg16':
            net = vgg16(batch_size=1)
        elif args['net'] == 'res50':
            net = resnetv1(batch_size=1, num_layers=50)
        elif args['net'] == 'res101':
            net = resnetv1(batch_size=1, num_layers=101)
        elif args['net'] == 'res152':
            net = resnetv1(batch_size=1, num_layers=152)
        elif args['net'] == 'mobile':
            net = mobilenetv1(batch_size=1)
        else:
            raise NotImplementedError

        # load model
        net.create_architecture(sess,
                                "TEST",
                                imdb.num_classes,
                                tag='default',
                                anchor_scales=cfg.ANCHOR_SCALES,
                                anchor_ratios=cfg.ANCHOR_RATIOS)

        if args['model']:
            print(
                ('Loading model check point from {:s}').format(args['model']))
            saver = tf.train.Saver()
            saver.restore(sess, args['model'])
            print('Loaded.')
        else:
            print(('Loading initial weights from {:s}').format(args['weight']))
            sess.run(tf.global_variables_initializer())
            print('Loaded.')

        test_net(sess,
                 net,
                 imdb,
                 filename,
                 max_per_image=args['max_per_image'])

        sess.close()
    def launch_train(self, conf):
        '''
        
        '''
        args = {}
        args['cfg_file'] = conf.frcnn_cfg
        args['weight'] = conf.starting_weights
        args['imdb_name'] = conf.train_set
        args['imdbval_name'] = conf.valid_set
        args['max_iters'] = conf.iters
        args['tag'] = conf.frcnn_tag
        args['net'] = conf.frcnn_net
        args['set_cfgs'] = None

        print('Called with args:')
        print(args)

        if args['cfg_file'] is not None:
            cfg_from_file(args['cfg_file'])
        if args['set_cfgs'] is not None:
            cfg_from_list(args['set_cfgs'])

        print('Using config:')
        pprint.pprint(cfg)

        np.random.seed(cfg.RNG_SEED)

        # train set
        imdb, roidb = combined_roidb(args['imdb_name'], conf)
        print('{:d} roidb entries'.format(len(roidb)))

        # output directory where the models are saved
        output_dir = conf.backup_folder  #get_output_dir(imdb, args.tag)
        print('Output will be saved to `{:s}`'.format(output_dir))

        # tensorboard directory where the summaries are saved during training
        tb_dir = conf.backup_folder  # get_output_tb_dir(imdb, args.tag)
        print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))

        # also add the validation set, but with no flipping images
        orgflip = cfg.TRAIN.USE_FLIPPED
        cfg.TRAIN.USE_FLIPPED = False
        _, valroidb = combined_roidb(args['imdbval_name'], conf)
        print('{:d} validation roidb entries'.format(len(valroidb)))
        cfg.TRAIN.USE_FLIPPED = orgflip
        if args['net'] == 'vgg16':
            net = vgg16(batch_size=cfg.TRAIN.IMS_PER_BATCH)
        elif args['net'] == 'res50':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=50)
        elif args['net'] == 'res101':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=101)

        # load network
        elif args['net'] == 'res152':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=152)
        elif args['net'] == 'mobile':
            net = mobilenetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH)
        else:
            raise NotImplementedError

        train_net(net,
                  imdb,
                  roidb,
                  valroidb,
                  output_dir,
                  tb_dir,
                  pretrained_model=args['weight'],
                  max_iters=args['max_iters'])
예제 #19
0
    def __init__(self):
        self.root = Tk()
        self.widthpixels = 1500
        self.heightpixels = 800
        self.root.wm_title("Cell Counter Application")
        self.C = [0, 0, 0, 0, 0]
        self.prev_C = [0, 0, 0, 0, 0]
        self.root.geometry('{}x{}'.format(self.widthpixels, self.heightpixels))
        self.button_frame = Frame(self.root)
        self.button_frame.pack(side=LEFT, fill=Y)
        self.img_frame = Frame(self.root)
        self.img_frame.pack(side=RIGHT)

        labelframe1 = LabelFrame(self.button_frame, text='start panel')
        labelframe1.pack(fill="both", expand="yes")
        self.start_button = Button(labelframe1,
                                   text='start',
                                   height=2,
                                   width=10,
                                   command=self.start_win)
        self.start_button.grid(row=0, column=0, pady=10)
        self.zoom_button = Button(
            labelframe1,
            text='Focus',
            height=2,
            width=10,
            command=lambda index=7: self.auto_zoom(index))
        self.zoom_button.grid(row=0, column=1)
        self.zoom_button = Button(
            labelframe1,
            text='Fast Focus',
            height=2,
            width=10,
            command=lambda index=3: self.auto_zoom(index))
        self.zoom_button.grid(row=0, column=2)

        labelframe2 = LabelFrame(self.button_frame, text='move panel')
        labelframe2.pack(fill="both", expand="yes")
        step_label = Label(labelframe2, text='step size', height=1, width=10)
        step_label.grid(row=0, column=0, pady=10)
        self.step_text = Text(labelframe2, height=1, width=5)
        self.step_text.grid(row=0, column=1, pady=10)
        dum = Label(labelframe2, text='  ', height=3, width=10)
        dum.grid(row=1, column=0, pady=10)
        dum = Label(labelframe2, text='  ', height=3, width=3)
        dum.grid(row=1, column=1)
        img_up = Image.open("../pics/up.jpeg")
        photo_up = ImageTk.PhotoImage(img_up)
        self.up_button = Button(labelframe2,
                                image=photo_up,
                                height=50,
                                width=50,
                                command=self.move_up)
        self.up_button.grid(row=1, column=2)
        dum = Label(labelframe2, text='  ', height=3, width=3)
        dum.grid(row=1, column=3)

        dum = Label(labelframe2, text='  ', height=3, width=10)
        dum.grid(row=3, column=0)
        dum = Label(labelframe2, text='  ', height=3, width=3)
        dum.grid(row=3, column=1)
        img_down = Image.open("../pics/down.jpeg")
        photo_down = ImageTk.PhotoImage(img_down)
        self.down_button = Button(labelframe2,
                                  image=photo_down,
                                  height=50,
                                  width=50,
                                  command=self.move_down)
        self.down_button.grid(row=3, column=2)
        dum = Label(labelframe2, text='  ', height=3, width=3)
        dum.grid(row=3, column=3)

        dum = Label(labelframe2, text='  ', height=3, width=10)
        dum.grid(row=2, column=0)
        img_left = Image.open("../pics/left.jpeg")
        photo_left = ImageTk.PhotoImage(img_left)
        self.left_button = Button(labelframe2,
                                  image=photo_left,
                                  height=50,
                                  width=50,
                                  command=self.move_left)
        self.left_button.grid(row=2, column=1)
        dum = Label(labelframe2, text='  ', height=3, width=3)
        dum.grid(row=2, column=2)
        img_right = Image.open("../pics/right.jpeg")
        photo_right = ImageTk.PhotoImage(img_right)
        self.right_button = Button(labelframe2,
                                   image=photo_right,
                                   height=50,
                                   width=50,
                                   command=self.move_right)
        self.right_button.grid(row=2, column=3)

        labelframe3 = LabelFrame(self.button_frame, text='count panel')
        labelframe3.pack(fill="both", expand="yes")
        self.count_button = Button(labelframe3,
                                   text='Count',
                                   height=2,
                                   width=10,
                                   command=self.count)
        self.count_button.grid(row=0, column=0, pady=10)
        self.undo_count_button = Button(labelframe3,
                                        text='Undo Count',
                                        height=2,
                                        width=10,
                                        command=self.undo_count)
        self.undo_count_button.grid(row=0, column=1)
        self.edit_count_button = Button(labelframe3,
                                        text='Edit manually',
                                        height=2,
                                        width=10,
                                        command=self.edit_manually)
        self.edit_count_button.grid(row=0, column=2)

        temp = ttk.Separator(labelframe3, orient=HORIZONTAL)
        temp.grid(row=1, column=0, pady=15, sticky="ew")
        temp = tk.Label(labelframe3, text='Results', height=3, width=10)
        temp.grid(row=1, column=1, pady=10)
        temp = ttk.Separator(labelframe3, orient=HORIZONTAL)
        temp.grid(row=1, column=2, pady=10, sticky="ew")
        self.C_label = []
        i = 0
        self.count1_label = tk.Button(
            labelframe3,
            text='Neutrophil',
            height=2,
            width=10,
            command=lambda index=i: self.single_classifier(index))
        self.count1_label.grid(row=2, column=0)
        self.C_label.append(
            Entry(labelframe3,
                  width=5,
                  font="Helvetica 20 bold",
                  highlightcolor='white'))
        self.C_label[0].insert(0, '0')
        self.C_label[0].grid(row=2, column=1)
        self.C_label[0].config(state='disabled')

        i = 1
        self.count2_label = tk.Button(
            labelframe3,
            text='Lymph',
            height=2,
            width=10,
            command=lambda index=i: self.single_classifier(index))
        self.count2_label.grid(row=3, column=0)
        self.C_label.append(
            Entry(labelframe3,
                  width=5,
                  font="Helvetica 20 bold",
                  highlightcolor='white'))
        self.C_label[1].insert(0, '0')
        self.C_label[1].grid(row=3, column=1)
        self.C_label[1].config(state='disabled')

        i = 2
        self.count3_label = tk.Button(
            labelframe3,
            text='Monocyte',
            height=2,
            width=10,
            command=lambda index=i: self.single_classifier(index))
        self.count3_label.grid(row=4, column=0)
        self.C_label.append(
            Entry(labelframe3,
                  width=5,
                  font="Helvetica 20 bold",
                  highlightcolor='white'))
        self.C_label[2].insert(0, '0')
        self.C_label[2].grid(row=4, column=1)
        self.C_label[2].config(state='disabled')

        i = 3
        self.count4_label = tk.Button(
            labelframe3,
            text='Eosinophil',
            height=2,
            width=10,
            command=lambda index=i: self.single_classifier(index))
        self.count4_label.grid(row=5, column=0)
        self.C_label.append(
            Entry(labelframe3,
                  width=5,
                  font="Helvetica 20 bold",
                  highlightcolor='white'))
        self.C_label[3].insert(0, '0')
        self.C_label[3].grid(row=5, column=1)
        self.C_label[3].config(state='disabled')

        count5_label = tk.Button(labelframe3,
                                 text='Basophil',
                                 height=2,
                                 width=10)
        count5_label.grid(row=6, column=0)
        self.C_label.append(
            Entry(labelframe3,
                  width=5,
                  font="Helvetica 20 bold",
                  highlightcolor='white'))
        self.C_label[4].insert(0, '0')
        self.C_label[4].grid(row=6, column=1)
        self.C_label[4].config(state='disabled')

        self.apply = Button(labelframe3,
                            text='Apply Changes',
                            height=2,
                            width=10,
                            command=self.apply_changes)
        self.apply.grid(row=7, column=1)
        self.apply.config(state='disabled')
        tf.reset_default_graph()
        self.rects = []
        self.other_imgs = []
        CLASSES = ('__background__', '1')
        os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
        self.tfmodel = "../models/vgg16_faster_rcnn_iter_50000.ckpt"
        if not os.path.isfile(self.tfmodel + '.meta'):
            raise IOError(
                ('{:s} not found.\nDid you download the proper networks from '
                 'our server and place them properly?').format(self.tfmodel +
                                                               '.meta'))
        self.cfg_file = '../experiments/cfgs/vgg16.yml'
        cfg_from_file(self.cfg_file)
        self.tfconfig = tf.ConfigProto(allow_soft_placement=True)
        self.tfconfig.gpu_options.allow_growth = True
        self.sess = tf.Session(config=self.tfconfig)
        self.net = vgg16()
        self.net.create_architecture("TEST",
                                     2,
                                     tag='default',
                                     anchor_scales=[2],
                                     anchor_ratios=[1])
        self.saver = tf.train.Saver()
        self.saver.restore(self.sess, self.tfmodel)
        self.CONF_THRESH = 0.9
        self.NMS_THRESH = 0.3
        print('Loaded network {:s}'.format(self.tfmodel))
        name = '../models/global_model.pt'
        name2 = '../models/global_model_parameters.pt'
        self.model = torch.load(name,
                                map_location=lambda storage, loc: storage)
        self.model.load_state_dict(
            torch.load(name2, map_location=lambda storage, loc: storage))
        self.preprocess = transforms.Compose([
            # transformer.Pad_resize_conditional(320),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])