Пример #1
0
def main(args):

    pdb.set_trace()

    lr_boundaries = [int(x) for x in args.lr_steps.split(',')]
    lr_values = [float(x) for x in args.lr_values.split(',')]


    smallest_ratio = [int(x) for x in args.smallest_ratio.split(',')]
    ANCHORS_MAP, NUM_ANCHORS = ssd_common.get_anchors(ANCHORS_STRIDE,ANCHORS_ASPECT_RATIOS,MIN_SIZE_RATIO, MAX_SIZE_RATIO, INPUT_DIM, smallest_ratio)

    resize_method = args.resize_method
    #mscoco = dataset_feeder.MscocoFeeder(args,ssd_augmenter_vgg.augment, resize_method)
    #mscoco = dataset_feeder.MscocoFeeder(args,ssd_augmenter_vgg.augment)

    dtst_list=[]
    for ii,dtst_name in enumerate(args.dataset_meta):
        mscoco1 = dataset_feeder.MscocoFeeder(args, dtst_name, exclude_img_list_file=args.exclude_img_list[ii], category_map_file=args.category_map_file)
        dtst_list.append(mscoco1)


    nsamples = sum([v.num_samples for v in dtst_list])
    nclasses = len(dtst_list[0].cat_names)+ 1  
    batch_size_per_gpu = args.batch_size
    batch_size_total = args.num_gpus*batch_size_per_gpu
    pretrained_feature_path = args.pretrained_feature_path 
    exm_per_epoch = nsamples
    modelDir = args.model_dir

    tfobj = preprocess.TFdata(args, nsamples, ssd_augmenter_vgg.augment)

    last_model, last_step = check_for_existing_model(modelDir)
    nu_epoch0 = exm_per_epoch*1.0/(batch_size_per_gpu*args.num_gpus)


    #get_sample = mscoco.get_samples_fn()
    graph = tf.Graph()
    with graph.as_default(), tf.device('/cpu:0'):

        step=tf.train.get_or_create_global_step()
        nu_epoch = tf.cast(nu_epoch0, tf.int32)
        #nu_epoch = tf.cast(exm_per_epoch*1.0/(batch_size_per_gpu*args.num_gpus), tf.int32) # num_update_per_epoch
        init_lr = args.learning_rate

        boundaries = lr_boundaries
        values = lr_values
        learning_rate, optimizer = get_lr_optimizer(step, boundaries, values)



        dataset = tfobj.create_tfdataset(dtst_list, batch_size_total, args.num_epochs)
        iterator = dataset.make_one_shot_iterator()
        image_id, images, classes, boxes, scale, translation, filename = iterator.get_next()
        gt_classes, gt_boxes, gt_mask = ssd_common.encode_gt(classes,boxes, ANCHORS_MAP, batch_size_total )
        
        split_images=tf.split(images, args.num_gpus)
        split_classes = tf.split(gt_classes, args.num_gpus)
        split_boxes = tf.split(gt_boxes, args.num_gpus)
        split_mask = tf.split(gt_mask, args.num_gpus)


        pred_classes_array= []
        pred_boxes_array= []
        tower_grads = []
        class_loss_array = []
        box_loss_array = []
        l2_loss_array = []
        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(args.num_gpus):
                with tf.name_scope('tower_%d' % (i)), tf.device('/gpu:%d' % i):
                    pred_classes, pred_boxes  = ssd300.net(split_images[i], nclasses, NUM_ANCHORS, True, 'my_vgg3')

                    loss, class_loss, bboxes_loss, loss_l2, nmatches, nnegs = losses.compute_loss(pred_classes, pred_boxes, split_classes[i], split_boxes[i], split_mask[i], huber_loss_delta=args.huber_loss_delta, error_normalize_factor=args.error_normalize_factor, weight_decay=args.weight_decay)


                    grads = optimizer.compute_gradients(loss) #, var_list=self.train_vars)

                    tower_grads.append(grads)

                    pred_classes_array.append(pred_classes)
                    pred_boxes_array.append(pred_boxes)
                    class_loss_array.append(class_loss)
                    box_loss_array.append(bboxes_loss)
                    l2_loss_array.append(loss_l2)
                    
                    tf.get_variable_scope().reuse_variables()
        
        
        avg_grads = average_gradients(tower_grads)
        minimize_op = optimizer.apply_gradients(avg_grads, global_step=step)

        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        train_op = tf.group(minimize_op, update_ops)


        pred_classes_array = tf.concat(values=pred_classes_array,axis=0)
        pred_boxes_array = tf.concat(values=pred_boxes_array,axis=0)
        
        
        global_init=tf.global_variables_initializer() # initializes all variables in the graph

        var_load_fn = get_var_load_fn(last_model, pretrained_feature_path)
        
        

    #pdb.set_trace()
    with tf.Session(graph=graph) as sess:
        print('Start counting time ..')
        
        sess.run(global_init)
        var_load_fn(sess)

        start_epoch = 0
        if last_model is not None:
            print('last step = ', last_step)
            trn_iter = sess.run(tf.assign(step,last_step+1))
            last_epoch = int(last_step/nu_epoch0)
            start_epoch = last_epoch
        elif pretrained_feature_path is not None:
            trn_iter=0

        #classes , boxes, split_classes, split_boxes = sess.run([classes,boxes,split_classes,split_boxes])
        nbatches = int(nsamples/batch_size_total)
        #trn_iter = 0
        start_time = time.time() 
        for ep in range(start_epoch, args.num_epochs):
            for bb in range(nbatches):
                
                if (trn_iter%250) ==0:
                    closs,bloss,lloss,lr,_ = sess.run([class_loss_array,box_loss_array,l2_loss_array, learning_rate,train_op])
                    elapsed_time = time.time() - start_time
                    #print('time needed = '+time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))

                    print('iter= ', trn_iter, 'lr= ',lr,'cls= ',np.amax(closs), 'bls= ', np.amax(bloss), 'l2= ',np.amax(lloss), 'time = '+time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
                    start_time = time.time()
                else:
                    npos,nneg,_ = sess.run([nmatches, nnegs, train_op])

                trn_iter+=1

                if (trn_iter % 5000) == 0:
                    saver0 = tf.train.Saver()
                    modelName = 'model-'+str(trn_iter).zfill(6)
                    modelPath= os.path.join(modelDir,modelName)#'/trn_dir/models/model_tmp/model-00000'
                    saver0.save(sess, modelPath)
                    saver0.export_meta_graph(modelPath+'.meta')
Пример #2
0
def main(args):

    #pdb.set_trace()
    mscoco = dataset_feeder.MscocoFeeder(args, args.dataset_meta[0])
    nsamples = len(mscoco.samples)
    nclasses = len(mscoco.cat_names) + 1  #categories + background

    batch_size_per_gpu = args.batch_size
    batch_size = args.num_gpus * batch_size_per_gpu
    pretrained_model_path = args.pretrained_model_path
    smallest_ratio = [int(x) for x in args.smallest_ratio.split(',')]
    tfobj = preprocess.TFdata(args, nsamples, ssd_augmenter_mb.augment)

    if args.minival_ids_file is not None:
        minival_ids = np.loadtxt(args.minival_ids_file, dtype=int).tolist()

    ANCHORS_MAP, NUM_ANCHORS = ssd_common.get_anchors(
        ANCHORS_STRIDE, ANCHORS_ASPECT_RATIOS, MIN_SIZE_RATIO, MAX_SIZE_RATIO,
        INPUT_DIM, smallest_ratio)
    #get_sample = mscoco.get_samples_fn()
    graph = tf.Graph()
    with graph.as_default(), tf.device('/cpu:0'):

        dataset = tfobj.create_tfdataset([mscoco], batch_size, args.num_epochs)
        iterator = dataset.make_one_shot_iterator()
        image_id, images, classes, boxes, scale, translation, filename = iterator.get_next(
        )

        split_images = tf.split(images, args.num_gpus)
        split_classes = tf.split(classes, args.num_gpus)
        split_boxes = tf.split(boxes, args.num_gpus)

        pred_classes_array = []
        pred_boxes_array = []

        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(args.num_gpus):
                with tf.name_scope('tower_%d' % (i)), tf.device('/gpu:%d' % i):
                    pred_classes, pred_boxes = ssd300_mb.net(
                        split_images[i], nclasses, NUM_ANCHORS, False, 'my_mb')
                    pred_classes_array.append(pred_classes)
                    pred_boxes_array.append(pred_boxes)
                    tf.get_variable_scope().reuse_variables()

        pred_classes_array = tf.concat(values=pred_classes_array, axis=0)
        pred_boxes_array = tf.concat(values=pred_boxes_array, axis=0)

        topk_scores, topk_labels, topk_bboxes, _ = ssd300_mb.detect(
            pred_classes_array, pred_boxes_array, ANCHORS_MAP, batch_size,
            nclasses, args.confidence_threshold)

        global_init = tf.global_variables_initializer(
        )  # initializes all variables in the graph

        variables_to_restore = tf.contrib.framework.get_variables_to_restore()
        #variables_to_restore = [v for v  in tf.trainable_variables()]
        #other_variables = [v for v in tf.global_variables() if (v not in variables_to_restore and 'batch_normalization' in v.name and 'moving' in v.name)]
        #variables_to_restore.extend(other_variables)
        var_load_fn = tf.contrib.framework.assign_from_checkpoint_fn(
            pretrained_model_path, variables_to_restore)

    #pdb.set_trace()
    with tf.Session(graph=graph) as sess:
        print('Start counting time ..')
        sess.run(global_init)
        var_load_fn(sess)
        #classes , boxes, split_classes, split_boxes = sess.run([classes,boxes,split_classes,split_boxes])
        nbatches = int(nsamples / batch_size)

        all_detections = []
        all_image_ids = []
        all_inf_times = []
        for bb in range(nbatches):

            start_time = time.time()
            image_ids, imgnames, scores, classes, boxes = sess.run(
                [image_id, filename, topk_scores, topk_labels, topk_bboxes])
            elapsed_time = time.time() - start_time
            all_inf_times.append(elapsed_time)

            num_images = len(imgnames)
            for i in range(num_images):
                file_name = imgnames[i][0].decode('ascii')
                if (args.minival_ids_file
                        is not None) and (image_ids[i][0] not in minival_ids):
                    continue

                num_detections = len(classes[i])

                input_image = np.array(Image.open(file_name))
                h, w = input_image.shape[:2]

                # COCO evaluation is based on per detection
                for d in range(num_detections):
                    box = boxes[i][d]
                    box = box * [float(w), float(h), float(w), float(h)]
                    box[0] = round(np.clip(box[0], 0, w), 1)
                    box[1] = round(np.clip(box[1], 0, h), 1)
                    box[2] = round(np.clip(box[2], 0, w), 1)
                    box[3] = round(np.clip(box[3], 0, h), 1)
                    box[2] = round(box[2] - box[0], 1)
                    box[3] = round(box[3] - box[1], 1)
                    result = {
                        "image_id":
                        int(image_ids[i][0]),
                        "category_id":
                        int(mscoco.class_id_to_category_id[classes[i][d]]),
                        "bbox":
                        box.tolist(),
                        "score":
                        float(scores[i][d])
                    }
                    all_detections.append(result)

                all_image_ids.append(image_ids[i][0])

        elapsed_time = np.sum(all_inf_times)
        print('elapsed time = ' +
              time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))

        print('batch = ', bb)
        print('Finished prediction ... ')

        if args.output_file is not None:
            pdb.set_trace()
            fid = open(args.output_file, 'wt')
            json.dump(all_detections, fid)
            fid.close()

        elif len(all_detections) > 0:
            annotation_file = os.path.join(
                args.dataset_dir, "annotations",
                "instances_" + args.dataset_meta[0] +
                ".json")  #"instances_" + DATASET_META + ".json")
            coco = COCO(annotation_file)

            coco_results = coco.loadRes(all_detections)

            cocoEval = COCOeval(coco, coco_results, "bbox")
            cocoEval.params.imgIds = all_image_ids
            cocoEval.evaluate()
            cocoEval.accumulate()
            cocoEval.summarize()
Пример #3
0
def main(args):

    pdb.set_trace()
    mscoco = dataset_feeder.MscocoFeeder(args,args.dataset_meta[0],category_map_file=args.category_map_file)
    nsamples = len(mscoco.samples)
    nclasses = len(mscoco.cat_names)+ 1 #categories + background
    batch_size = args.num_gpus*args.batch_size
    pretrained_model_path = args.pretrained_model_path 

    tfobj = preprocess.TFdata(args, nsamples, ssd_augmenter_mb.preprocess_for_apply)

    smallest_ratio = [int(x) for x in args.smallest_ratio.split(',')]
    ANCHORS_MAP, NUM_ANCHORS = ssd_common.get_anchors(ANCHORS_STRIDE,ANCHORS_ASPECT_RATIOS,MIN_SIZE_RATIO, MAX_SIZE_RATIO, INPUT_DIM,smallest_ratio)
    #get_sample = mscoco.get_samples_fn()
    graph = tf.Graph()
    with graph.as_default(), tf.device('/cpu:0'):

        input_names = tf.placeholder(dtype=tf.string, name='input_names')
        #images = tfobj.parse_fn(0, input_names, [0], tf.zeros([1,4]))

        preprocess_fn  = lambda x: tfobj.parse_fn_apply(x)
        images = tf.map_fn(preprocess_fn, input_names, dtype=tf.float32)
        
        
        split_images=tf.split(images, args.num_gpus)
        
        pred_classes_array= []
        pred_boxes_array= []

        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(args.num_gpus):
                with tf.name_scope('tower_%d' % (i)), tf.device('/gpu:%d' % i):
                    pred_classes, pred_boxes  = ssd300_mb.net(split_images[i], nclasses, NUM_ANCHORS, False, 'my_mb')
                    pred_classes_array.append(pred_classes)
                    pred_boxes_array.append(pred_boxes)
                    tf.get_variable_scope().reuse_variables()


        pred_classes_array = tf.concat(values=pred_classes_array,axis=0)
        pred_boxes_array = tf.concat(values=pred_boxes_array,axis=0)

        topk_scores, topk_labels, topk_bboxes, _ = ssd300_mb.detect(pred_classes_array, pred_boxes_array, ANCHORS_MAP, batch_size, nclasses, args.confidence_threshold)

        

        variables_to_restore = tf.contrib.framework.get_variables_to_restore()
        #variables_to_restore = [v for v  in tf.trainable_variables()]
        #other_variables = [v for v in tf.global_variables() if (v not in variables_to_restore and 'batch_normalization' in v.name and 'moving' in v.name)]
        #variables_to_restore.extend(other_variables)
        var_load_fn = tf.contrib.framework.assign_from_checkpoint_fn(pretrained_model_path,  variables_to_restore)
         
        global_init=tf.global_variables_initializer() # initializes all variables in the graph
     
    pdb.set_trace()
    inputfolder = '/home/testing_framework/check_testing_website/motion_1frame_max'
    subfolders = os.listdir(inputfolder)
    all_det = []
    with tf.Session(graph=graph) as sess:
        print('Start counting time ..')
        sess.run(global_init)
        var_load_fn(sess)
        for subf in subfolders:
            img_filenames = glob.glob(os.path.join(inputfolder,subf,'*.jpg'))
            for input_name in img_filenames:
                scores,classes,boxes = sess.run([topk_scores, topk_labels, topk_bboxes], feed_dict={input_names:[input_name]})
                dets = [] 
                for ii in range(len(scores[0])):
                    if scores[0][ii] < args.confidence_threshold:
                        continue
                    det1 = {}
                    cat_id = mscoco.class_id_to_category_id[classes[0][ii]]
                    cat_name = mscoco.catid_to_name[cat_id]
                    det1['score'] = float(scores[0][ii])
                    detected_box = np.clip(boxes[0][ii],0,1)
                    det1['box'] = detected_box.tolist()
                    det1['name'] = cat_name
                    if cat_name=='cat' or cat_name=='dog':
                        det1['name']='pet'
                    dets.append(det1)
          
                all_det.append({'Id':subf, 'Image':os.path.basename(input_name), 'Detections':dets})
                print(dets)

        pdb.set_trace()
        output_name = args.output
        with open(output_name,'wt') as fid:
            json.dump(all_det, fid)
Пример #4
0
def main(args):

    #pdb.set_trace()
    mscoco = dataset_feeder.MscocoFeeder(args,args.dataset_meta[0])
    nsamples = len(mscoco.samples)
    nclasses = len(mscoco.cat_names)+ 1 #categories + background
    batch_size = args.num_gpus*args.batch_size
    pretrained_model_path = args.pretrained_model_path 
    tfobj = preprocess.TFdata(args, nsamples, ssd_augmenter_vgg.augment)

    smallest_ratio = [int(x) for x in args.smallest_ratio.split(',')]
    ANCHORS_MAP, NUM_ANCHORS = ssd_common.get_anchors(ANCHORS_STRIDE,ANCHORS_ASPECT_RATIOS,MIN_SIZE_RATIO, MAX_SIZE_RATIO, INPUT_DIM,smallest_ratio)
    #get_sample = mscoco.get_samples_fn()
    graph = tf.Graph()
    with graph.as_default(), tf.device('/cpu:0'):

        dataset = tfobj.create_tfdataset([mscoco], batch_size, args.num_epochs)
        iterator = dataset.make_one_shot_iterator()
        image_id, images, classes, boxes, scale, translation, filename = iterator.get_next()

        split_images=tf.split(images, args.num_gpus)
        split_classes = tf.split(classes, args.num_gpus)
        split_boxes = tf.split(boxes, args.num_gpus)

        pred_classes_array= []
        pred_boxes_array= []

        with tf.variable_scope(tf.get_variable_scope()):
            for i in range(args.num_gpus):
                with tf.name_scope('tower_%d' % (i)), tf.device('/gpu:%d' % i):
                    pred_classes, pred_boxes  = ssd300.net(split_images[i], nclasses, NUM_ANCHORS, False, 'my_vgg3')
                    pred_classes_array.append(pred_classes)
                    pred_boxes_array.append(pred_boxes)
                    tf.get_variable_scope().reuse_variables()


        pred_classes_array = tf.concat(values=pred_classes_array,axis=0)
        pred_boxes_array = tf.concat(values=pred_boxes_array,axis=0)

        topk_scores, topk_labels, topk_bboxes, _ = ssd300.detect(pred_classes_array, pred_boxes_array, ANCHORS_MAP, batch_size, nclasses, args.confidence_threshold)

        global_init=tf.global_variables_initializer() # initializes all variables in the graph

        #variables_to_restore = tf.contrib.framework.get_variables_to_restore(exclude=['resnet_model/dense','global_step'])
        variables_to_restore = [v for v  in tf.trainable_variables()]
        #other_variables = [v for v in tf.global_variables() if (v not in variables_to_restore and 'batch_normalization' in v.name and 'moving' in v.name)]
        #variables_to_restore.extend(other_variables)
        var_load_fn = tf.contrib.framework.assign_from_checkpoint_fn(pretrained_model_path,  variables_to_restore)


    #pdb.set_trace()
    with tf.Session(graph=graph) as sess:
        print('Start counting time ..')
        sess.run(global_init)
        var_load_fn(sess)
        #classes , boxes, split_classes, split_boxes = sess.run([classes,boxes,split_classes,split_boxes])
        nbatches = int(nsamples/batch_size)
        all_det = {}
        for bb in range(nbatches):
            #print('batch = ',bb) 
            imgname,scores,classes,boxes = sess.run([filename,topk_scores, topk_labels, topk_bboxes]) 
            for ii,iname in enumerate(imgname):
                iname = iname[0].decode('ascii')
                cat_id = [mscoco.class_id_to_category_id[v] for v in classes[ii].tolist()]
                cat_name = [mscoco.catid_to_name[v] for v in cat_id]
                all_det[iname] = {'class_name':cat_name, 'score':scores[ii].tolist(), 'box':boxes[ii].tolist()}

        output_name = 'detections_'+args.dataset_meta[0]+'.json'
        with open(output_name,'wt') as fid:
            json.dump(all_det, fid)