def FasterRCNN_demo(): """ Demo code """ DATA_DIR = 'data/' demonet = 'res152_COCO' tf.reset_default_graph( ) # Needed to use different nets one after the other print(demonet) if 'VOC' in demonet: CLASSES = CLASSES_SET['VOC'] anchor_scales = [8, 16, 32] # It is needed for the right net architecture !! elif 'COCO' in demonet: CLASSES = CLASSES_SET['COCO'] anchor_scales = [4, 8, 16, 32] nbClasses = len(CLASSES) tfmodel = os.path.join(path_to_model, NETS_Pretrained[demonet]) #tfmodel = os.path.join(path_to_model,DATASETS[dataset][0],NETS[demonet][0]) print(tfmodel) # tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default', # NETS[demonet][0]) tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True # init session sess = tf.Session(config=tfconfig) # load network if 'vgg16' in demonet: net = vgg16() elif demonet == 'res50': raise NotImplementedError elif 'res101' in demonet: net = resnetv1(num_layers=101) elif 'res152' in demonet: net = resnetv1(num_layers=152) elif demonet == 'mobile': raise NotImplementedError else: raise NotImplementedError net.create_architecture("TEST", nbClasses, tag='default', anchor_scales=anchor_scales) saver = tf.train.Saver() saver.restore(sess, tfmodel) print('Loaded network {:s}'.format(tfmodel)) im_name = 'loulou.jpg' print('Demo for data/demo/{}'.format(im_name)) imfile = os.path.join(DATA_DIR, im_name) im = cv2.imread(imfile) scores, boxes = im_detect( sess, net, im) # Arguments: im (ndarray): a color image in BGR order # Only single-image batch implemented ! print('scores.shape', scores.shape) #print(scores) CONF_THRESH = 0.8 NMS_THRESH = 0.5 # non max suppression for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if (len(inds) > 0): print('CLASSES[cls_ind]', CLASSES[cls_ind]) vis_detections(im, cls, dets, thresh=CONF_THRESH) plt.show() sess.close()
def compute_FasterRCNN_feature_TransferLearning(): """ Compute the fc7 of the FasterRCNN """ path_to_img = 'data/' NETS_Pretrained = { 'res101_COCO': 'res101_faster_rcnn_iter_1190000.ckpt', 'res152_COCO': 'res152_faster_rcnn_iter_1190000.ckpt', 'vgg16_COCO': 'vgg16_faster_rcnn_iter_1190000.ckpt' } NETS_Pretrained = {'res152_COCO': 'res152_faster_rcnn_iter_1190000.ckpt'} for demonet in NETS_Pretrained.keys(): #demonet = 'res101_COCO' tf.reset_default_graph( ) # Needed to use different nets one after the other print(demonet) if 'VOC' in demonet: CLASSES = CLASSES_SET['VOC'] anchor_scales = [ 8, 16, 32 ] # It is needed for the right net architecture !! elif 'COCO' in demonet: CLASSES = CLASSES_SET['COCO'] anchor_scales = [ 4, 8, 16, 32 ] # we use 3 aspect ratios and 4 scales (adding 64**2) nbClasses = len(CLASSES) path_to_model = '/media/gonthier/HDD/models/tf-faster-rcnn/' tfmodel = os.path.join(path_to_model, NETS_Pretrained[demonet]) tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True # init session sess = tf.Session(config=tfconfig) # load network if 'vgg16' in demonet: net = vgg16() elif demonet == 'res50': raise NotImplementedError elif 'res101' in demonet: net = resnetv1(num_layers=101) elif 'res152' in demonet: net = resnetv1(num_layers=152) elif demonet == 'mobile': raise NotImplementedError else: raise NotImplementedError path_data = 'data/' listIm = ['loulou.jpg'] print('Start computing image region proposal') if demonet == 'vgg16_COCO': size_output = 4096 elif demonet == 'res101_COCO' or demonet == 'res152_COCO': size_output = 2048 # Use the output of fc7 net.create_architecture("TEST", nbClasses, tag='default', anchor_scales=anchor_scales, modeTL=True) saver = tf.train.Saver() saver.restore(sess, tfmodel) for i, name_img in enumerate(listIm): if i % 1000 == 0: print(i, name_img) complet_name = path_to_img + name_img im = cv2.imread(complet_name) cls_score, cls_prob, bbox_pred, rois, roi_scores, fc7, pool5 = TL_im_detect( sess, net, im) # Arguments: im (ndarray): a color image in BGR order print(fc7.shape)
def net_test(): 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' filename = tag + '/' + filename imdb = get_imdb(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() elif args.net == 'res50': net = resnetv1(num_layers=50) elif args.net == 'res101': net = resnetv1(num_layers=101) elif args.net == 'res152': net = resnetv1(num_layers=152) else: raise NotImplementedError # load model net.create_architecture("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 test_net_local(): """ Test des nets en local a la machine de Nicolas""" DATASET = 'pascal_voc' if DATASET == 'pascal_voc': TRAIN_IMDB = "voc_2007_trainval" TEST_IMDB = "voc_2007_test" ITERS = 70000 ANCHORS = [8, 16, 32] RATIOS = [0.5, 1, 2] elif DATASET == 'pascal_voc_0712': TRAIN_IMDB = "voc_2007_trainval+voc_2012_trainval" TEST_IMDB = "voc_2007_test" ITERS = 110000 ANCHORS = [8, 16, 32] RATIOS = [0.5, 1, 2] elif DATASET == 'coco': TRAIN_IMDB = "coco_2014_train+coco_2014_valminusminival" TEST_IMDB = "coco_2014_minival" ITERS = 490000 ANCHORS = [4, 8, 16, 32] RATIOS = [0.5, 1, 2] demonet = 'res101' model_path = '/media/gonthier/HDD/models/tf-faster-rcnn/' filename = model_path + demonet + '_faster_rcnn_iter_' + str( ITERS) + '.ckpt' imdb_name = TEST_IMDB imdb = get_imdb(imdb_name) comp_mode = True imdb.competition_mode(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 demonet == 'vgg16': net = vgg16() elif demonet == 'res50': net = resnetv1(num_layers=50) elif demonet == 'res101': net = resnetv1(num_layers=101) elif demonet == 'res152': net = resnetv1(num_layers=152) else: raise NotImplementedError # load model net.create_architecture("TEST", imdb.num_classes, tag='default', anchor_scales=ANCHORS, anchor_ratios=RATIOS) print(('Loading model check point from {:s}').format(filename)) saver = tf.train.Saver() saver.restore(sess, filename) print('Loaded.') max_per_image = 100 test_net(sess, net, imdb, filename, max_per_image=max_per_image) sess.close()
# tensorboard directory where the summaries are saved during training tb_dir = 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) print('{:d} validation roidb entries'.format(len(valroidb))) cfg.TRAIN.USE_FLIPPED = orgflip # load network if args.net == 'vgg16': net = vgg16() elif args.net == 'res50': net = resnetv1(num_layers=50) elif args.net == 'res101': net = resnetv1(num_layers=101) elif args.net == 'res152': net = resnetv1(num_layers=152) # elif args.net == 'mobile': # net = mobilenetv1() else: raise NotImplementedError trainWS_net(net, imdb, valroidb, output_dir, tb_dir, pretrained_model=args.weight,
def Compute_Faster_RCNN_features(demonet='res152_COCO', nms_thresh=0.7, database='IconArt_v1', verbose=True, k_regions=300, path_data='data', path_output='output', path_to_model='models'): """ @param : demonet : the backbone net used it can be 'vgg16_VOC07', 'vgg16_VOC12','vgg16_COCO','res101_VOC12','res101_COCO','res152_COCO' @param : nms_thresh : the nms threshold on the Region Proposal Network @param : database name of the dataset @param : k_regions : number of region per image @param : path_data path to the dataset @param : path_output path to the output model @param : path_to_model path to the pretarined model """ item_name, path_to_img, classes, ext, num_classes, str_val, df_label = get_database( database, default_path_imdb=path_data) N = 1 extL2 = '' savedstr = '_all' layer = 'fc7' tf.reset_default_graph( ) # Needed to use different nets one after the other if verbose: print(demonet) if 'VOC' in demonet: CLASSES = CLASSES_SET['VOC'] anchor_scales = [8, 16, 32] # It is needed for the right net architecture !! elif 'COCO' in demonet: CLASSES = CLASSES_SET['COCO'] anchor_scales = [ 4, 8, 16, 32 ] # we use 3 aspect ratios and 4 scales (adding 64**2) nbClassesDemoNet = len(CLASSES) pathlib.Path(path_to_model).mkdir(parents=True, exist_ok=True) tfmodel = os.path.join(path_to_model, NETS_Pretrained[demonet]) if not (os.path.exists(tfmodel)): print("You have to download the Faster RCNN pretrained, see README") return (0) tfconfig = tf.ConfigProto(allow_soft_placement=True) tfconfig.gpu_options.allow_growth = True # init session sess = tf.Session(config=tfconfig) # load network if 'vgg16' in demonet: net = vgg16() size_output = 4096 elif 'res101' in demonet: net = resnetv1(num_layers=101) size_output = 2048 elif 'res152' in demonet: net = resnetv1(num_layers=152) size_output = 2048 else: raise NotImplementedError net.create_architecture("TEST", nbClassesDemoNet, tag='default', anchor_scales=anchor_scales, modeTL=True, nms_thresh=nms_thresh) saver = tf.train.Saver() saver.restore(sess, tfmodel) features_resnet_dict = {} sets = ['trainval', 'test'] if k_regions == 300: k_per_bag_str = '' else: k_per_bag_str = '_k' + str(k_regions) dict_writers = {} for set_str in sets: name_pkl_all_features = os.path.join( path_output, 'FasterRCNN_' + demonet + '_' + database + '_N' + str(N) + extL2 + '_TLforMIL_nms_' + str(nms_thresh) + savedstr + k_per_bag_str + '_' + set_str + '.tfrecords') dict_writers[set_str] = tf.python_io.TFRecordWriter( name_pkl_all_features) Itera = 1000 for i, name_img in enumerate(df_label[item_name]): if i % Itera == 0: if verbose: print(i, name_img) if database in ['IconArt_v1', 'watercolor']: complet_name = path_to_img + name_img + '.jpg' name_sans_ext = name_img elif database == 'PeopleArt': complet_name = path_to_img + name_img name_sans_ext = os.path.splitext(name_img)[0] try: im = cv2.imread(complet_name) height = im.shape[0] width = im.shape[1] except AttributeError: print(complet_name, 'is missing') continue cls_score, cls_prob, bbox_pred, rois, roi_scores, fc7, pool5 = TL_im_detect( sess, net, im) # Arguments: im (ndarray): a color image in BGR order if k_regions == 300: num_regions = fc7.shape[0] num_features = fc7.shape[1] dim1_rois = rois.shape[1] classes_vectors = np.zeros((num_classes, 1)) rois_tmp = np.zeros((k_regions, 5)) roi_scores_tmp = np.zeros((k_regions, 1)) fc7_tmp = np.zeros((k_regions, size_output)) rois_tmp[0:rois.shape[0], 0:rois.shape[1]] = rois roi_scores_tmp[0:roi_scores.shape[0], 0:roi_scores.shape[1]] = roi_scores fc7_tmp[0:fc7.shape[0], 0:fc7.shape[1]] = fc7 rois = rois_tmp roi_scores = roi_scores_tmp fc7 = fc7_tmp else: # We will select only k_regions new_nms_thresh = 0.0 score_threshold = 0.1 minimal_surface = 36 * 36 num_regions = k_regions num_features = fc7.shape[1] dim1_rois = rois.shape[1] classes_vectors = np.zeros((num_classes, 1)) rois_reduce,roi_scores_reduce,fc7_reduce = reduce_to_k_regions(k_regions,rois, \ roi_scores, fc7,new_nms_thresh, \ score_threshold,minimal_surface) if (len(fc7_reduce) >= k_regions): rois = rois_reduce[0:k_regions, :] roi_scores = roi_scores_reduce[0:k_regions, ] fc7 = fc7_reduce[0:k_regions, :] else: number_repeat = k_regions // len(fc7_reduce) + 1 f_repeat = np.repeat(fc7_reduce, number_repeat, axis=0) roi_scores_repeat = np.repeat(roi_scores_reduce, number_repeat, axis=0) rois_reduce_repeat = np.repeat(rois_reduce, number_repeat, axis=0) rois = rois_reduce_repeat[0:k_regions, :] roi_scores = roi_scores_repeat[0:k_regions, ] fc7 = f_repeat[0:k_regions, :] if database in ['watercolor', 'PeopleArt']: for j in range(num_classes): value = int((int(df_label[classes[j]][i]) + 1.) / 2.) #print(value) classes_vectors[j] = value if database in ['IconArt_v1']: for j in range(num_classes): value = int(df_label[classes[j]][i]) classes_vectors[j] = value features = tf.train.Features( feature={ 'height': _int64_feature(height), 'width': _int64_feature(width), 'num_regions': _int64_feature(num_regions), 'num_features': _int64_feature(num_features), 'dim1_rois': _int64_feature(dim1_rois), 'rois': _floats_feature(rois), 'roi_scores': _floats_feature(roi_scores), 'fc7': _floats_feature(fc7), 'label': _floats_feature(classes_vectors), 'name_img': _bytes_feature(str.encode(name_sans_ext)) }) example = tf.train.Example(features=features) if database == 'PeopleArt': if (df_label.loc[df_label[item_name] == name_img]['set'] == 'train' ).any(): dict_writers['trainval'].write(example.SerializeToString()) elif (df_label.loc[df_label[item_name] == name_img]['set'] == 'val' ).any(): dict_writers['trainval'].write(example.SerializeToString()) elif (df_label.loc[df_label[item_name] == name_img]['set'] == 'test').any(): dict_writers['test'].write(example.SerializeToString()) if database in ['watercolor','IconArt_v1']\ or 'IconArt_v1' in database: if (df_label.loc[df_label[item_name] == name_img]['set'] == 'train' ).any(): dict_writers['trainval'].write(example.SerializeToString()) elif (df_label.loc[df_label[item_name] == name_img]['set'] == 'test').any(): dict_writers['test'].write(example.SerializeToString()) for set_str in sets: dict_writers[set_str].close()