Exemplo n.º 1
0
def write_textfile(p, r, a, tp, tn, fp, fn, threshold, model_base, dir=None):
    if dir is None:
        protoname = solverproto.replace('.prototxt', '')
        dir = 'multilabel_results-' + protoname + '_' + model_base.replace(
            '.caffemodel', '')
    Utils.ensure_dir(dir)
    fname = os.path.join(dir, model_base + 'results.txt')
    with open(fname, 'a') as f:
        f.write(model_base + ' threshold = ' + str(threshold) + '\n')
        f.write('solver:' + solverproto + '\n')
        f.write('model:' + caffemodel + '\n')
        f.write('categories: ' + str(constants.web_tool_categories) + '\n')
        f.write('precision\n')
        f.write(str(p) + '\n')
        f.write('recall\n')
        f.write(str(r) + '\n')
        f.write('accuracy\n')
        f.write(str(a) + '\n')
        f.write('true positives\n')
        f.write(str(tp) + '\n')
        f.write('true negatives\n')
        f.write(str(tn) + '\n')
        f.write('false positives\n')
        f.write(str(fp) + '\n')
        f.write('false negatives\n')
        f.write(str(fn) + '\n')
        f.close()
Exemplo n.º 2
0
def generate_bbfiles_from_dir_of_jsons(json_dir,
                                       imagefiles_dir_of_dirs,
                                       bb_dir,
                                       darknet=True,
                                       category_number=None,
                                       clobber=False):
    #    only_dirs = [d for d in json_dir if os.path.isdir(os.path.join(json_dir,d))]
    json_files = [
        f for f in os.listdir(json_dir)
        if os.path.isfile(os.path.join(json_dir, f)) and f[-5:] == '.json'
        and not 'retrieval' in f
    ]
    class_number = 0
    Utils.ensure_dir(bb_dir)
    for a_file in json_files:
        prefix = a_file[0:-5]
        imagefiles_dir = prefix
        print('json {} images {} class {}'.format(a_file, imagefiles_dir,
                                                  class_number))
        full_json_file = os.path.join(json_dir, a_file)
        full_images_dir = os.path.join(imagefiles_dir_of_dirs, prefix)
        generate_bbfiles_from_json(full_json_file,
                                   full_images_dir,
                                   bb_dir,
                                   darknet=True,
                                   class_number=class_number,
                                   clobber=False)
        class_number += 1
Exemplo n.º 3
0
def after_nn_processdir(indir,
                        outdir,
                        labels=constants.ultimate_21,
                        filter='.bmp'):
    os.listdir(indir)
    Utils.ensure_dir(outdir)
    masks = [f for f in os.listdir(indir) if filter in f]
    print('found {} files in {}'.format(len(masks), indir))

    label_dict = {labels[i]: i for i in range(len(labels))}
    print label_dict

    for f in masks:
        print f
        fullname = os.path.join(indir, f)
        mask = cv2.imread(fullname)  #have to worry abt 3chan masks?
        if len(mask.shape) == 3:
            mask = mask[:, :, 0]
        after_mask = pipeline.after_nn_conclusions(mask, label_dict)
        after_nn_conclusions_name = os.path.join(outdir,
                                                 f[:-4] + '_nnconclusions.bmp')
        cv2.imwrite(after_nn_conclusions_name, after_mask)
        imutils.show_mask_with_labels(after_nn_conclusions_name,
                                      labels,
                                      save_images=True)
Exemplo n.º 4
0
def download_image(prod, feature_name, category_id, max_images):
    downloaded_images = 0
    directory = os.path.join(category_id, feature_name)
    try:
        downloaded_images = len([name for name in os.listdir(directory) if os.path.isfile(name)])
    except:
        pass
    if downloaded_images < max_images:
            xlarge_url = prod['image']['sizes']['XLarge']['url']

            img_arr = Utils.get_cv2_img_array(xlarge_url)
            if img_arr is None:
                logging.warning("Could not download image at url: {0}".format(xlarge_url))
                return

            relevance = background_removal.image_is_relevant(img_arr)
            if relevance.is_relevant:
                logging.info("Image is relevant...")

                filename = "{0}_{1}.jpg".format(feature_name, prod["id"])
                filepath = os.path.join(directory, filename)
                Utils.ensure_dir(directory)
                logging.info("Attempting to save to {0}...".format(filepath))
                success = cv2.imwrite(filepath, img_arr)
                if not success:
                    logging.info("!!!!!COULD NOT SAVE IMAGE!!!!!")
                    return 0
                # downloaded_images += 1
                logging.info("Saved... Downloaded approx. {0} images in this category/feature combination"
                             .format(downloaded_images))
                return 1
            else:
                # TODO: Count number of irrelevant images (for statistics)
                return 0
Exemplo n.º 5
0
def dl_photos(photosfile, images_savedir, start_from=0):
    listings = get_product_photos(photosfile)
    max_items = 5000000
    Utils.ensure_dir(images_savedir)
    num_cores = multiprocessing.cpu_count()

    Parallel(n_jobs=num_cores)(delayed(get_file)(listings[i], i +
                                                 1, images_savedir)
                               for i in range(start_from, len(listings)))
def flickr_get_dates(tag,mintime=0,savedir=None,n_pages=9):
    time.sleep(21)

    time_inc = 3600*24*1  #1 day
    print('getting dates, min is '+str(mintime))
    if savedir is None:
        savedir = '/home/jeremy/image_dbs/flickr/'+tag+'/'
    Utils.ensure_dir(savedir)
    compressed_tag = tag.replace(' ','+')
    outfile = compressed_tag+'out.txt'
    print('outfile:'+outfile)
    maxtime = mintime+time_inc
    pages=0
    oldpages = -1
    while(pages<n_pages and  maxtime<time.time()):
        time.sleep(3)
        initial_query = '&tags='+tag+'&min_upload_date='+str(mintime)+'&max_upload_date='+str(maxtime)+'&per_page=500'
        initial_query = '&text='+compressed_tag+'&min_upload_date='+str(mintime)+'&max_upload_date='+str(maxtime)+'&per_page=500'
        print('trying dates '+str(mintime)+' and '+str(maxtime))
        cmd = 'curl -X GET "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=d8548143cce923734f4093b4b063bc4f&format=json'+initial_query+'" > ' + outfile
        print cmd
        res = subprocess.call(cmd,shell=True)

        with open(outfile,'r') as f:
            content=f.read()
            #the info is returned inside a function call like flickerApi({bla:bla...}) so I need to strip the beginning and end stuff
            stripped = content[14:-1]
            f.close()
        try:
            d = json.loads(stripped)
        except:
            print('get_dates json problem')
            print('problem josn:'+str(stripped))
            maxtime = maxtime + time_inc
            continue
    #    pprint(d)
        if not d:
            print('no file found')
        if not 'photos' in d:
            print('no photos field in result, continuing')
        phot = d['photos']
        if not 'photo' in phot:
            print('no photo field in result, continuing')
        if 'page' in phot:
            print('page '+str(phot['page']))
            page = phot['page']
        if 'pages' in phot:
            print('of total pages '+str(phot['pages']))
            pages = phot['pages']
            if pages==oldpages:
                time_inc = time_inc*1.4
        print('got '+str(pages)+' pages')
        maxtime = int(maxtime + time_inc)
        oldpages=pages
    maxtime = maxtime - time_inc
    print('returning maxtime:'+str(maxtime)+' pages:'+str(pages))
    return maxtime
def get_pd_results_and_save(url=None, filename=None):
    if url is not None:
        print('getting pd results for ' + url)
        image = Utils.get_cv2_img_array(url)
    elif filename is not None:
        print('getting pd results for ' + filename)
        image = cv2.imread(filename)
    if image is None:
        print('image came back none')
        return None
    try:
        seg_res = pd_falcon_client.pd(image)
    except:
        print('exception in pd_falcon_client.pd ' + str(sys.exc_info()[0]))
        return
#    print('segres:'+str(seg_res))
    if filename is not None:
        imgfilename = os.path.basename(
            filename
        )  #use the actual on-disk filename if thats what we started with, otherwise use the name generated by pd
        #this will et saved to /home/jeremy/pd_output/filename
    else:
        try:
            imgfilename = seg_res['filename'] + '.jpg'
        except:
            print('some error on imgfile name')
            imgfilename = str(int(time.time())) + '.jpg'
    print('filename ' + imgfilename)
    if not 'mask' in seg_res:
        print('pd did not return mask')
        print seg_res
        return
    mask = seg_res['mask']
    label_dict = seg_res['label_dict']
    print('labels:' + str(label_dict))
    conversion_utils.count_values(mask)
    pose = seg_res['pose']
    mask_np = np.array(mask, dtype=np.uint8)
    print('masksize ' + str(mask_np.shape))
    pose_np = np.array(pose, dtype=np.uint8)
    print('posesize ' + str(pose_np.shape))
    #    print('returned url '+seg_res['url'])
    converted_mask = convert_and_save_results(mask_np, label_dict, pose_np,
                                              imgfilename, image, url)
    dir = constants.pd_output_savedir
    Utils.ensure_dir(dir)
    full_name = os.path.join(dir, imgfilename)
    #            full_name = filename
    bmp_name = full_name.strip('.jpg') + (
        '.bmp')  #this bmp should get generated by convert_and_save_results
    #    imutils.show_mask_with_labels(bmp_name,constants.fashionista_categories_augmented_zero_based,original_image = full_name,save_images=False)
    return mask, label_dict, pose_np, converted_mask
Exemplo n.º 8
0
def convert_labels_dir(
        indir,
        outdir,
        jpgdir=None,
        converter=constants.
    fashionista_aug_zerobased_to_pixlevel_categories_v2,
        suffix_in='.png',
        suffix_out='_pixlevelv2.bmp',
        for_webtool=False,
        inlabels=constants.fashionista_categories_augmented_zero_based,
        outlabels=constants.pixlevel_categories_v2,
        save_legends=True):
    '''
    convert e..g from paperdoll to ultimate21 or pixlevel_categories_v2 .
    Optionally only convert R channel for use with webtool. Don't forget to convert back to all chans after done w webtool
    :param dir:
    :param converter:
    :param input_suffix:
    :param for_webtool:
    :return:
    '''
    Utils.ensure_dir(outdir)
    files = [
        os.path.join(indir, f) for f in os.listdir(indir) if suffix_in in f
    ]
    print('STARTING CONVERT - converting ' + str(len(files)) + ' files in ' +
          indir)
    for f in files:
        print('')
        newname = os.path.join(outdir, os.path.basename(f))
        newname = newname.replace(suffix_in, suffix_out)
        print('converting {} to {} '.format(f, newname))
        converted_arr = convert_labels(f,
                                       converter=converter,
                                       for_webtool=for_webtool,
                                       inlabels=inlabels,
                                       outlabels=outlabels)
        cv2.imwrite(newname, converted_arr)
        #raw_input('ret to cont')
        if save_legends:
            if jpgdir is None:
                jpgdir = indir
            orig_imagename = os.path.basename(f).replace(suffix_in, '.jpg')
            orig_imagename = os.path.join(jpgdir, orig_imagename)
            print('saving legend using {} '.format(orig_imagename))

            imutils.show_mask_with_labels(converted_arr,
                                          outlabels,
                                          original_image=orig_imagename,
                                          save_images=True)
Exemplo n.º 9
0
def close_html(model_base, dir=None):
    if dir is None:
        protoname = solverproto.replace('.prototxt', '')
        dir = 'multilabel_results-' + protoname + '_' + model_base.replace(
            '.caffemodel', '')
    Utils.ensure_dir(dir)
    htmlname = os.path.join(dir, model_base + 'results.html')
    with open(htmlname, 'a') as g:
        g.write('</table><br>')
        plotfilename = 'multilabel_results' + model_base + '.png'

        g.write('<a href=\"' + plotfilename + '\">plot<img src = \"' +
                plotfilename + '\" style=\"width:300px\"></a>')
        g.write('</html>')
Exemplo n.º 10
0
def write_textfile(caffemodel,
                   solverproto,
                   threshold,
                   model_base,
                   dir=None,
                   classes=None):
    if dir is None:
        protoname = solverproto.replace('.prototxt', '')
        dir = 'multilabel_results-' + protoname + '_' + model_base.replace(
            '.caffemodel', '')
    Utils.ensure_dir(dir)
    fname = os.path.join(dir, model_base + 'results.txt')
    with open(fname, 'a') as f:
        f.write(model_base + ' threshold = ' + str(threshold) + '\n')
        f.write('solver:' + solverproto + '\n')
        f.write('model:' + caffemodel + '\n')
        f.write('categories: ' + str(classes) + '\n')
        f.close()
Exemplo n.º 11
0
def replace_labels_dir(dir_of_masks,
                       index_conversion,
                       overwrite=False,
                       outdir=None,
                       filter=None):
    '''
    convert masks in dir from tamara_berg_improved to ultimate_21 labels
    #21 cats for direct replacement of VOC systems
    #lose the necklace,
    #combine tights and leggings
    :param dir_of_masks:
    :return:
    '''
    if filter:
        masks = [
            os.path.join(dir_of_masks, f) for f in os.listdir(dir_of_masks)
            if filter in f
        ]
    else:
        masks = [
            os.path.join(dir_of_masks, f) for f in os.listdir(dir_of_masks)
        ]
    for f in masks:
        mask_arr = replace_labels(f, index_conversion)
        if mask_arr is None:
            logging.warning('got no file')
            continue
        if len(mask_arr.shape) == 3:
            logging.warning('multichannel mask, taking chan 0')
            mask_arr = mask_arr[:, :, 0]
        if overwrite:
            outname = f
        else:
            indir = os.path.dirname(f)
            parentdir = os.path.abspath(os.path.join(indir, os.pardir))
            if outdir == None:
                outdir = os.path.join(parentdir, '_relabelled')
                Utils.ensure_dir(outdir)
            inname = os.path.basename(f)
            outname = os.path.join(outdir, f)
        print('in {} out {}'.format(f, outname))
        cv2.imwrite(mask_arr)
Exemplo n.º 12
0
def move_the_rest(mask_filename, k):
    if not os.path.exists(mask_filename):
        print(mask_filename + ' does not exist')
        return
    indir = os.path.dirname(mask_filename)
    parentdir = os.path.abspath(os.path.join(indir, os.pardir))
    curdir = os.path.split(indir)[1]  #the parent of current dir
    print('in {} parent {} cur {}'.format(indir, parentdir, curdir))
    if k == ord('d'):
        newdir = curdir + '_removed'
        dest_dir = os.path.join(parentdir, newdir)
        Utils.ensure_dir(dest_dir)
        print('REMOVING moving {} to {}'.format(mask_filename, dest_dir))
        shutil.move(mask_filename, dest_dir)

    elif k == ord('c'):
        newdir = curdir + '_needwork'
        dest_dir = os.path.join(parentdir, newdir)
        Utils.ensure_dir(dest_dir)
        print('CLOSE so moving {} to {}'.format(mask_filename, dest_dir))
        shutil.move(mask_filename, dest_dir)

    elif k == ord('k'):
        newdir = curdir + '_kept'
        dest_dir = os.path.join(parentdir, newdir)
        Utils.ensure_dir(dest_dir)
        print('KEEPING moving {} to {}'.format(mask_filename, dest_dir))
        shutil.move(mask_filename, dest_dir)

    else:
        print('doing nothing')
Exemplo n.º 13
0
def open_html(modelname,
              dir=None,
              solverproto='',
              caffemodel='',
              classlabels=constants.web_tool_categories,
              name=None):
    model_base = os.path.basename(modelname)
    if dir is not None:
        Utils.ensure_dir(dir)
        htmlname = os.path.join(dir, model_base + 'results.html')
    else:
        htmlname = os.path.join(model_base, 'results.html')
    if name is not None:
        htmlname = name
    with open(htmlname, 'a') as g:
        g.write('<!DOCTYPE html>')
        g.write('<html>')
        g.write('<head>')
        g.write('<title>')
        dt = datetime.datetime.today()
        g.write(model_base + ' ' + dt.isoformat())
        g.write('</title>')
        g.write('solver:' + solverproto + '\n' + '<br>')
        g.write('model:' + caffemodel + '\n' + '<br>')
        g.write('</head>')
        #        g.write('categories: '+str(constants.web_tool_categories)+'<br>'+'\n')
        g.write('<table style=\"width:100%\">\n')
        g.write('<tr>\n')
        g.write('<th>')
        g.write('metric')
        g.write('</th>\n')
        g.write('<th>')
        g.write('fw avg.')
        g.write('</th>\n')
        if classlabels:
            for i in range(len(classlabels)):
                g.write('<th>')
                g.write(classlabels[i])
                g.write('</th>\n')
        g.write('</tr>\n')
Exemplo n.º 14
0
def precision_accuracy_recall(caffemodel,
                              solverproto,
                              outlayer='label',
                              n_tests=100):
    #TODO dont use solver to get inferences , no need for solver for that

    caffe.set_mode_gpu()
    caffe.set_device(1)

    workdir = './'
    snapshot = 'snapshot'
    #    caffemodel = '/home/jeremy/caffenets/multilabel/vgg_ilsvrc_16_multilabel_2/snapshot/train_iter_240000.caffemodel'
    #    caffemodel = '/home/jeremy/caffenets/multilabel/vgg_ilsvrc_16_multilabel_2/snapshot/train_iter_340000.caffemodel'
    model_base = caffemodel.split('/')[-1]
    p_all = []
    r_all = []
    a_all = []
    n_all = []
    #    for t in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.85,0.9,0.92,0.95,0.98]:
    thresh = [0.1, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95]
    #    thresh = [0.1,0.5,0.95]
    protoname = solverproto.replace('.prototxt', '')
    netname = get_netname(solverproto)
    if netname:
        dir = 'multilabel_results-' + netname + '_' + model_base.replace(
            '.caffemodel', '')
        dir = dir.replace('"', '')  #remove quotes
        dir = dir.replace(' ', '')  #remove spaces
        dir = dir.replace('\n', '')  #remove newline
        dir = dir.replace('\r', '')  #remove return
    else:
        dir = 'multilabel_results-' + protoname + '_' + model_base.replace(
            '.caffemodel', '')
        dir = dir.replace('"', '')  #remove quotes
        dir = dir.replace(' ', '')  #remove spaces
        dir = dir.replace('\n', '')  #remove newline
        dir = dir.replace('\r', '')  #remove return

    print('dir to save stuff in : ' + str(dir))
    Utils.ensure_dir(dir)
    #    open_html(model_base,dir=dir)
    positives = True
    for t in thresh:
        p, r, a, tp, tn, fp, fn = check_accuracy(solverproto,
                                                 caffemodel,
                                                 threshold=t,
                                                 num_batches=n_tests,
                                                 outlayer=outlayer)
        p_all.append(p)
        r_all.append(r)
        a_all.append(a)
        n_occurences = [tp[i] + fn[i] for i in range(len(tp))]
        n_all.append(n_occurences)
        write_textfile(p, r, a, tp, tn, fp, fn, t, model_base, dir=dir)
        write_html(p,
                   r,
                   a,
                   n_occurences,
                   t,
                   model_base,
                   positives=positives,
                   dir=dir,
                   tp=tp,
                   tn=tn,
                   fp=fp,
                   fn=fn)
        positives = False
    close_html(model_base, dir=dir)

    p_all_np = np.transpose(np.array(p_all))
    r_all_np = np.transpose(np.array(r_all))
    a_all_np = np.transpose(np.array(a_all))
    labels = constants.web_tool_categories
    plabels = [label + 'precision' for label in labels]
    rlabels = [label + 'recall' for label in labels]
    alabels = [label + 'accuracy' for label in labels]

    important_indices = [3, 5, 7, 10, 11, 13, 17]
    #cardigan  dress footwear jeans pants skirt top
    #['bag', 'belt', 'blazer','cardigan','coat','dress', 'eyewear', 'footwear', 'hat','jacket',
    #                  'jeans','pants','shorts', 'skirt','stocking','suit','sweater','top','scarf','womens_swimwear_bikini',
    #                 'womens_swimwear_nonbikini']

    p_important = [p_all_np[i] for i in important_indices]
    r_important = [r_all_np[i] for i in important_indices]
    a_important = [a_all_np[i] for i in important_indices]
    labels_important = [labels[i] for i in important_indices]
    for i in range(len(important_indices)):
        print(constants.web_tool_categories[i] + ' p:' + str(p_important[i]) +
              ' r:' + str(r_important[i]) + ' a:' + str(a_important[i]))
    thresh_all_np = np.array(thresh)
    print('shape:' + str(p_all_np.shape))
    print('len:' + str(len(p_important)))

    markers = [
        '^', '<', 'v', '^', '8', 'o', '.', 'x', '|', '+', 0, '4', 3, 4, 'H',
        '3', 'p', 'h', '*', 7, '', 5, ',', '2', 1, 6, 's', 'd', '1', '_', 2,
        ' ', 'D'
    ]
    markers = [
        '.', 'x', '|', '^', '+', '<', 0, 'v', '4', 3, '^', '8', 4, 'o', 'H',
        '3', 'p', '*', 'h', 7, '', 5, ',', '2', 1, 6, 's', 'd', '1', '_', 2,
        ' ', 'D'
    ]
    markers_important = ['^', '<', 'v', '^', '8', 'o', 'H', '3', 'p', '*', 'h']

    for i in range(len(p_important)):
        plt.subplot(311)
        print('plotting {} vs {}'.format(p_all_np[i, :], thresh_all_np))
        plt.plot(thresh_all_np,
                 p_important[i],
                 label=labels_important[i],
                 linestyle='None',
                 marker=markers_important[i])
        plt.subplot(312)  #
        plt.plot(thresh_all_np,
                 r_important[i],
                 label=labels_important[i],
                 linestyle='None',
                 marker=markers_important[i])
        plt.subplot(313)
        plt.plot(thresh_all_np,
                 a_important[i],
                 label=labels_important[i],
                 linestyle='None',
                 marker=markers_important[i])
#        plt.plot(thresh_all_np,a_all_np[i],label=labels_important[i],linestyle='None',marker=markers_important[i])
#        plt.plot(thresh_all_np,p_all_np[i,:],label=labels[i],marker=markers[i])
#        plt.subplot(312)   #
#        plt.plot(thresh_all_np,r_all_np[i,:],label=labels[i],linestyle='None',marker=markers[i])
#        plt.subplot(313)
#        plt.plot(thresh_all_np,a_all_np[i,:],label=labels[i],linestyle='None',marker=markers[i])
    plt.subplot(311)
    plt.title('results ' + model_base)
    plt.xlabel('threshold')
    plt.ylabel('precision')
    plt.grid(True)
    plt.ylim((0, 1))
    plt.subplot(312)  #
    plt.xlabel('threshold')
    plt.ylabel('recall')
    plt.grid(True)
    plt.ylim((0, 1))
    plt.subplot(313)
    plt.xlabel('threshold')
    plt.ylabel('accuracy')
    plt.ylim((0, 1))
    plt.grid(True)

    plt.legend(loc='center left', bbox_to_anchor=(1, 0.1))
    plt.show()  #

    figname = os.path.join(dir, model_base + '.png')
    print('saving figure:' + str(figname))
    plt.savefig(figname, bbox_inches='tight')
    #
    summary_html(dir)
def combine_pixlevel_and_multilabel_using_graylevel(
        pixel_graylevels,
        multilabel,
        multilabel_threshold=0.7,
        median_factor=1.0,
        multilabel_to_ultimate21_conversion=constants.
    binary_classifier_categories_to_ultimate_21,
        multilabel_labels=constants.binary_classifier_categories,
        face=None,
        output_layer='pixlevel_sigmoid_output',
        required_image_size=(224, 224),
        do_graylevel_zeroing=True):
    print(
        'combining multilabel w. neurodoll using nfc watch out, required imsize:'
        + str(required_image_size))
    thedir = './images'
    Utils.ensure_dir(thedir)
    if isinstance(url_or_np_array, basestring):
        image = url_to_image(url_or_np_array)
        orig_filename = os.path.join(thedir,
                                     url_or_np_array.split('/')[-1]).replace(
                                         '.jpg', '')
    elif type(url_or_np_array) == np.ndarray:
        hash = hashlib.sha1()
        hash.update(str(time.time()))
        name_base = 'orig' + hash.hexdigest()[:10]
        orig_filename = os.path.join(thedir, name_base)
        image = url_or_np_array
    if image is None:
        logging.debug('got None in grabcut_using_neurodoll_output')
    print('writing orig to ' + orig_filename + '.jpg')
    cv2.imwrite(orig_filename + '.jpg', image)
    multilabel = get_multilabel_output(url_or_np_array)
    #    multilabel = get_multilabel_output_using_nfc(url_or_np_array)
    #take only labels above a threshold on the multilabel result
    #possible other way to do this: multiply the neurodoll mask by the multilabel result and threshold that product
    if multilabel is None:
        logging.debug('None result from multilabel')
        return None
    thresholded_multilabel = [ml > multilabel_threshold
                              for ml in multilabel]  #
    logging.info('orig label:' + str(multilabel) + ' len:' +
                 str(len(multilabel)))
    #    print('incoming label:'+str(multilabel))
    #    logging.info('thresholded label:'+str(thresholded_multilabel))
    for i in range(len(thresholded_multilabel)):
        if thresholded_multilabel[i]:
            logging.info(multilabel_labels[i] + ' is over threshold')
#    print('multilabel to u21 conversion:'+str(multilabel_to_ultimate21_conversion))
#    print('multilabel labels:'+str(multilabel_labels))

#todo - this may be wrong later if we start taking both nd and multilabel into acct. Maybe ml thinks theres nothing there but nd thinks there is...
    if np.equal(thresholded_multilabel,
                0).all():  #all labels 0 - nothing found
        logging.debug('no items found')
        return  #

    pixlevel_categorical_output = graylevel_nd_output.argmax(
        axis=2)  #the returned mask is HxWxC so take max along C
    pixlevel_categorical_output = threshold_pixlevel(
        pixlevel_categorical_output)  #threshold out the small areas
    print('before graylevel zeroing:')
    count_values(pixlevel_categorical_output, labels=constants.ultimate_21)

    if do_graylevel_zeroing:
        graylevel_nd_output = zero_graylevels_not_in_ml(graylevel_nd_output,
                                                        multilabel,
                                                        threshold=0.7)

    pixlevel_categorical_output = graylevel_nd_output.argmax(
        axis=2)  #the returned mask is HxWxC so take max along C
    pixlevel_categorical_output = threshold_pixlevel(
        pixlevel_categorical_output)  #threshold out the small areas
    print('after graylevel zeroing:')
    count_values(pixlevel_categorical_output, labels=constants.ultimate_21)
    foreground = np.array((pixlevel_categorical_output > 0) * 1)
    background = np.array((pixlevel_categorical_output == 0) * 1)
    #    item_masks =  nfc.pd(image, get_all_graylevels=True)
    logging.debug('shape of pixlevel categorical output:' +
                  str(pixlevel_categorical_output.shape))
    logging.debug('n_fg {} n_bg {} tot {} w*h {}'.format(
        np.sum(foreground), np.sum(background),
        np.sum(foreground) + np.sum(background),
        pixlevel_categorical_output.shape[0] *
        pixlevel_categorical_output.shape[1]))

    first_time_thru = True  #hack to dtermine image size coming back from neurodoll

    #   final_mask = np.zeros([224,224])
    final_mask = np.zeros(pixlevel_categorical_output.shape[:])
    print('final_mask shape ' + str(final_mask.shape))

    if face:
        y_split = face[1] + 3 * face[3]
    else:
        # BETTER TO SEND A FACE
        y_split = np.round(0.4 * final_mask.shape[0])
    print('y split {} face {}'.format(y_split, face))

    #the grabcut results dont seem too hot so i am moving to a 'nadav style' from-nd-and-ml-to-results system
    #namely : for top , decide if its a top or dress or jacket
    # for bottom, decide if dress/pants/skirt
    #decide on one bottom
    #   for i in range(len(thresholded_multilabel)):
    #       if multilabel_labels[i] in ['dress', 'jeans','shorts','pants','skirt','suit','overalls'] #missing from list is various swimwear which arent getting returned from nd now anyway

    #############################################################################################
    #Make some conclusions nadav style.
    #Currently the decisions are based only on ml results without taking into acct the nd results.
    #In future possibly inorporate nd as well, first do a head-to-head test of nd vs ml
    #############################################################################################
    #1. take winning upper cover,  donate losers to winner
    #2. take winning upper under, donate losers to winner
    #3. take winning lower cover, donate losers to winner.
    #4. take winning lower under, donate losers to winner
    #5. decide on whole body item (dress, suit, overall) vs. non-whole body (two part e.g. skirt+top) items.
    #6. if wholebody beats two-part - donate all non-whole-body pixels to whole body (except upper-cover (jacket/blazer etc)  and lower under-stockings)
    #?  if no upper cover and no upper under and no whole-body: take max of all those and donate losers to winner

    #upper_cover: jacket, coat, blazer etc
    #upper under: shirt, top, blouse etc
    #lower cover: skirt, pants, shorts
    #lower under: tights, leggings

    whole_body_indexlist = [
        multilabel_labels.index(s) for s in ['dress', 'suit', 'overalls']
    ]  #swimsuits could be added here
    upper_cover_indexlist = [
        multilabel_labels.index(s)
        for s in ['cardigan', 'coat', 'jacket', 'sweater', 'sweatshirt']
    ]
    upper_under_indexlist = [multilabel_labels.index(s) for s in ['top']]
    lower_cover_indexlist = [
        multilabel_labels.index(s)
        for s in ['jeans', 'pants', 'shorts', 'skirt']
    ]
    lower_under_indexlist = [multilabel_labels.index(s) for s in ['stocking']]

    final_mask = np.copy(pixlevel_categorical_output)
    logging.info('size of final mask ' + str(final_mask.shape))

    print('wholebody indices:' + str(whole_body_indexlist))
    for i in whole_body_indexlist:
        print multilabel_labels[i]
    whole_body_ml_values = np.array(
        [multilabel[i] for i in whole_body_indexlist])
    print('wholebody ml_values:' + str(whole_body_ml_values))
    whole_body_winner = whole_body_ml_values.argmax()
    whole_body_winner_value = whole_body_ml_values[whole_body_winner]
    whole_body_winner_index = whole_body_indexlist[whole_body_winner]
    print('winning index:' + str(whole_body_winner) + ' mlindex:' +
          str(whole_body_winner_index) + ' value:' +
          str(whole_body_winner_value))

    print('uppercover indices:' + str(upper_cover_indexlist))
    for i in upper_cover_indexlist:
        print multilabel_labels[i]
    upper_cover_ml_values = np.array(
        [multilabel[i] for i in upper_cover_indexlist])
    print('upper_cover ml_values:' + str(upper_cover_ml_values))
    upper_cover_winner = upper_cover_ml_values.argmax()
    upper_cover_winner_value = upper_cover_ml_values[upper_cover_winner]
    upper_cover_winner_index = upper_cover_indexlist[upper_cover_winner]
    print('winning upper_cover:' + str(upper_cover_winner) + ' mlindex:' +
          str(upper_cover_winner_index) + ' value:' +
          str(upper_cover_winner_value))

    print('upperunder indices:' + str(upper_under_indexlist))
    for i in upper_under_indexlist:
        print multilabel_labels[i]
    upper_under_ml_values = np.array(
        [multilabel[i] for i in upper_under_indexlist])
    print('upper_under ml_values:' + str(upper_under_ml_values))
    upper_under_winner = upper_under_ml_values.argmax()
    upper_under_winner_value = upper_under_ml_values[upper_under_winner]
    upper_under_winner_index = upper_under_indexlist[upper_under_winner]
    print('winning upper_under:' + str(upper_under_winner) + ' mlindex:' +
          str(upper_under_winner_index) + ' value:' +
          str(upper_under_winner_value))

    print('lowercover indices:' + str(lower_cover_indexlist))
    for i in lower_cover_indexlist:
        print multilabel_labels[i]
    lower_cover_ml_values = np.array(
        [multilabel[i] for i in lower_cover_indexlist])
    print('lower_cover ml_values:' + str(lower_cover_ml_values))
    lower_cover_winner = lower_cover_ml_values.argmax()
    lower_cover_winner_value = lower_cover_ml_values[lower_cover_winner]
    lower_cover_winner_index = lower_cover_indexlist[lower_cover_winner]
    print('winning lower_cover:' + str(lower_cover_winner) + ' mlindex:' +
          str(lower_cover_winner_index) + ' value:' +
          str(lower_cover_winner_value))

    print('lowerunder indices:' + str(lower_under_indexlist))
    for i in lower_under_indexlist:
        print multilabel_labels[i]
    lower_under_ml_values = np.array(
        [multilabel[i] for i in lower_under_indexlist])
    print('lower_under ml_values:' + str(lower_under_ml_values))
    lower_under_winner = lower_under_ml_values.argmax()
    lower_under_winner_value = lower_under_ml_values[lower_under_winner]
    lower_under_winner_index = lower_under_indexlist[lower_under_winner]
    print('winning lower_under:' + str(lower_under_winner) + ' mlindex:' +
          str(lower_under_winner_index) + ' value:' +
          str(lower_under_winner_value))

    #for use later, decide on a winner between upper cover and upper under
    if upper_under_winner_value > upper_cover_winner_value:
        upper_winner_value = upper_under_winner_value
        upper_winner_index = upper_under_winner_index
    else:
        upper_winner_value = upper_cover_winner_value
        upper_winner_index = upper_cover_winner_index
    #for use later, decide on a winner between lower cover and lower under
    if lower_under_winner_value > lower_cover_winner_value:
        lower_winner_value = lower_under_winner_value
        lower_winner_index = lower_under_winner_index
    else:
        lower_winner_value = lower_cover_winner_value
        lower_winner_index = lower_cover_winner_index
    upper_winner_nd_index = multilabel_to_ultimate21_conversion[
        upper_winner_index]
    lower_winner_nd_index = multilabel_to_ultimate21_conversion[
        lower_winner_index]
    print('upper winner {} nd {} val {} lower winner {} nd {} val {}'.format(
        upper_winner_index, upper_winner_nd_index, upper_winner_value,
        lower_winner_index, lower_winner_nd_index, lower_winner_value))
    #1. take max upper cover , donate losers to winner
    #this actually might not be always right, e.g. jacket+ sweater
    #todo  - #1 - 4 can be put into a function since they are nearly identical
    neurodoll_upper_cover_index = multilabel_to_ultimate21_conversion[
        upper_cover_winner_index]
    if neurodoll_upper_cover_index is None:
        logging.warning('nd upper cover index {}  has no conversion '.format(
            upper_cover_winner_index))
    else:
        n = len(final_mask[final_mask == neurodoll_upper_cover_index])
        logging.debug('donating to upper cover winner, initial n :' + str(n) +
                      ' for ndindex ' + str(neurodoll_upper_cover_index) +
                      ' ml index ' + str(upper_cover_winner_index) +
                      ', checking mls ' + str(upper_cover_indexlist))
        for i in upper_cover_indexlist:  #whole_body donated to upper_under
            nd_index = multilabel_to_ultimate21_conversion[i]
            if nd_index is None:
                logging.warning('ml index {} has no conversion '.format(i))
                continue
            x = final_mask[final_mask == nd_index]
            final_mask[final_mask == nd_index] = neurodoll_upper_cover_index
            n = len(final_mask[final_mask == neurodoll_upper_cover_index])
            logging.info(
                'upper cover ndindex {} {} donated to upper cover winner nd {} , now {} pixels, lenx {} '
                .format(nd_index, constants.ultimate_21[nd_index],
                        neurodoll_upper_cover_index, n, len(x)))

#2. take max upper under, donate losers to winner
    neurodoll_upper_under_index = multilabel_to_ultimate21_conversion[
        upper_under_winner_index]
    if neurodoll_upper_under_index is None:
        logging.warning('nd upper cover index {}  has no conversion '.format(
            upper_under_winner_index))
    else:
        n = len(final_mask[final_mask == neurodoll_upper_under_index])
        logging.debug('donating to upper under winner, initial n :' + str(n) +
                      ' for ndindex ' + str(neurodoll_upper_under_index) +
                      ' ml index ' + str(upper_under_winner_index) +
                      ', checking mls ' + str(upper_under_indexlist))
        for i in upper_under_indexlist:  #upper under losers donated to upper under winner
            nd_index = multilabel_to_ultimate21_conversion[i]
            print('nd index {} ml index {}'.format(nd_index, i))
            if nd_index is None:
                logging.warning('ml index {} has no conversion '.format(i))
                continue
            final_mask[final_mask == nd_index] = neurodoll_upper_under_index
            n = len(final_mask[final_mask == neurodoll_upper_under_index])
            logging.info(
                'upper under ndindex {} {} donated to upper under winner nd {}, now {} pixels'
                .format(nd_index, constants.ultimate_21[nd_index],
                        neurodoll_upper_under_index, n))

#3. take max lower cover, donate losers to winner.
    neurodoll_lower_cover_index = multilabel_to_ultimate21_conversion[
        lower_cover_winner_index]
    if neurodoll_lower_cover_index is None:
        logging.warning('nd lower cover index {}  has no conversion '.format(
            lower_cover_winner_index))
    else:
        n = len(final_mask[final_mask == neurodoll_lower_cover_index])
        logging.debug('donating to lower cover winner, initial n :' + str(n) +
                      ' for ndindex ' + str(neurodoll_lower_cover_index) +
                      ' ml index ' + str(lower_cover_winner_index) +
                      ', checking mls ' + str(lower_cover_indexlist))
        for i in lower_cover_indexlist:  #lower cover losers donated to lower cover winner
            nd_index = multilabel_to_ultimate21_conversion[i]
            if nd_index is None:
                logging.warning('ml index {} has no conversion '.format(i))
                continue
            final_mask[final_mask == nd_index] = neurodoll_lower_cover_index
            n = len(final_mask[final_mask == neurodoll_lower_cover_index])
            logging.info(
                'lower cover ndindex {} {} donated to lower cover winner nd {}, now {} pixels'
                .format(nd_index, constants.ultimate_21[nd_index],
                        neurodoll_lower_cover_index, n))

#4. take max lower under, donate losers to winner.
    neurodoll_lower_under_index = multilabel_to_ultimate21_conversion[
        lower_under_winner_index]
    if neurodoll_lower_under_index is None:
        logging.warning('nd lower under index {}  has no conversion '.format(
            lower_under_winner_index))
    else:
        n = len(final_mask[final_mask == neurodoll_lower_under_index])
        logging.debug('donating to lower under winner, initial n :' + str(n) +
                      ' for ndindex ' + str(neurodoll_lower_under_index) +
                      ' ml index ' + str(lower_under_winner_index) +
                      ', checking mls ' + str(lower_under_indexlist))
        for i in lower_under_indexlist:  #lower under losers donated to lower under winner
            nd_index = multilabel_to_ultimate21_conversion[i]
            if nd_index is None:
                logging.warning('ml index {} has no conversion '.format(i))
                continue
            final_mask[final_mask == nd_index] = neurodoll_lower_under_index
            n = len(final_mask[final_mask == neurodoll_lower_under_index])
            logging.info(
                'lower under ndindex {} {} donated to lower under winner nd {}, now {} pixels'
                .format(nd_index, constants.ultimate_21[nd_index],
                        neurodoll_lower_under_index, n))

    logging.debug('after step 4, pixelcounts look like:')
    count_values(final_mask, labels=constants.ultimate_21)

    #########################
    # 5. WHOLEBODY VS TWO-PART
    # decide on whole body item (dress, suit, overall) vs. non-whole body items.
    # case 1 wholebody>upper_under>lower_cover
    # case 2 upper_under>wholebody>lower_cover
    # case 3 lower_cover>wholebody>upper-under
    # case 4 lower_cover,upper_under > wholebody
    #consider reducing this to nadav's method:
    #    whole_sum = np.sum([item.values()[0] for item in mask_sizes['whole_body']])
    #    partly_sum = np.sum([item.values()[0] for item in mask_sizes['upper_under']]) +\
    #                 np.sum([item.values()[0] for item in mask_sizes['lower_cover']])
    #                if whole_sum > partly_sum:
    #    donate partly to whole
    # its a little different tho since in multilabel you cant compare directly two items to one , e.g. if part1 = 0.6, part2 = 0.6, and whole=0.99, you
    # should prob go with whole even tho part1+part2>whole
    #########################
    neurodoll_wholebody_index = multilabel_to_ultimate21_conversion[
        whole_body_winner_index]
    if neurodoll_wholebody_index is None:
        logging.warning(
            'nd wholebody index {} ml index {} has no conversion '.format(
                neurodoll_wholebody_index, whole_body_winner_index))

#first case - wholebody > upper_under > lowercover
#donate all non-whole-body pixels to whole body (except upper-cover (jacket/blazer etc)  and lower under-stockings)
    elif (whole_body_winner_value > upper_under_winner_value) and (
            whole_body_winner_value > lower_cover_winner_value
    ) and whole_body_winner_value > multilabel_threshold:
        logging.info(
            'case 1. one part {} wins over upper cover {} and lower cover {}'.
            format(whole_body_winner_value, upper_cover_winner_value,
                   lower_cover_winner_value))
        n = len(final_mask[final_mask == neurodoll_wholebody_index])
        logging.info('n in final mask from wholebody alone:' + str(n))
        for i in upper_cover_indexlist:
            #jackets etc can occur with dress/overall so dont donate these
            pass
        for i in upper_under_indexlist:  #donate upper_under to whole_body
            #todo fix the case of suit (which can have upper_under)
            #ideally, do this for dress - suit and overalls can have upper_under
            nd_index = multilabel_to_ultimate21_conversion[i]
            if nd_index is None:
                logging.debug(
                    'upper cover nd index for {} has no conversion '.format(i))
                continue
            #add upper cover item to wholebody mask
            final_mask[final_mask == nd_index] = neurodoll_wholebody_index
            logging.info('adding upperunder nd index {} '.format(nd_index))
            n = final_mask[final_mask == neurodoll_wholebody_index]
            logging.info('n in final mask from wholebody:' + str(n))
        for i in lower_cover_indexlist:  #donate lower_cover to whole_body
            nd_index = multilabel_to_ultimate21_conversion[i]
            final_mask[final_mask == nd_index] = neurodoll_wholebody_index
            logging.info('adding lowercover nd index {} '.format(nd_index))
            n = final_mask[final_mask == neurodoll_wholebody_index]
            logging.info('n in final mask from wholebody alone:' + str(n))
        for i in lower_under_indexlist:
            #not doing this for stockings which is currently the only lower under
            pass
        logging.debug('after case one pixel values look like')
        count_values(final_mask, labels=constants.ultimate_21)

# second case - upper_under > wholebody > lowercover
# here its not clear who to sack - the wholebody or the upper_under
# so i arbitrarily decided to sack th whole_body in favor of the upper_under since upper_under is higher
# EXCEPT if the wholebody is overalls , in which case keep overalls, upper_under and upper_cover, donate  lower_cover/under to overalls
# otherwise  if wholebody is e.g. dress then add dress to upper_under and lower_cover

    elif (whole_body_winner_value < upper_under_winner_value) and (
            whole_body_winner_value > lower_cover_winner_value) and (
                whole_body_winner_value > multilabel_threshold):
        logging.info(
            'case 2. one part {} < upper under {} but > lower cover {}'.format(
                whole_body_winner_value, upper_under_winner_value,
                lower_cover_winner_value))
        #if overalls, donate loewr_cover and lower_under to overalls
        if whole_body_winner_index == multilabel_labels.index('overalls'):
            neurodoll_whole_body_index = multilabel_to_ultimate21_conversion[
                whole_body_winner_index]
            n = len(final_mask[final_mask == neurodoll_wholebody_index])
            logging.info('n in final mask from wholebody (overall) alone:' +
                         str(n))
            for i in upper_cover_indexlist:
                pass  #upper cover ok with overalls
            for i in upper_under_indexlist:
                pass  #upper under ok with overalls
            for i in lower_cover_indexlist:  #lower cover donated to overalls
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning('ml index {} has no conversion '.format(i))
                    continue
                final_mask[final_mask == nd_index] = neurodoll_wholebody_index
                logging.info(
                    'uppercover nd index {} donated to overalls'.format(
                        nd_index))
                n = len(final_mask[final_mask == neurodoll_wholebody_index])
                logging.info('n in final mask from wholebody alone:' + str(n))
            for i in lower_under_indexlist:  #lower under donated to overalls - this can conceivably go wrong e.g. with short overalls and stockings
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning('ml index {} has no conversion '.format(i))
                    continue
                final_mask[final_mask == nd_index] = neurodoll_wholebody_index
                logging.info(
                    'uppercover nd index {} donated to overalls'.format(
                        nd_index))
                n = len(final_mask[final_mask == neurodoll_wholebody_index])
                logging.info('n in final mask from wholebody alone:' + str(n))
#not overalls, so donate  whole_body to upper_under - maybe not to  lower_under . Not clear what to do actually.
        else:  #not overalls
            if upper_winner_nd_index is None:
                logging.warning(
                    'nd wholebody index {} ml index {} has no conversion '.
                    format(upper_winner_nd_index, upper_winner_index))
            else:
                n1 = len(final_mask[final_mask == upper_winner_nd_index])
                n2 = len(final_mask[final_mask == lower_winner_nd_index])
                logging.info(
                    'n in final mask from wholebody before donation to upper winner {} px,nd {} (lower {} px,nd {}'
                    .format(n1, upper_winner_nd_index, n2,
                            lower_winner_nd_index))
                #todo - actually only wholebody pixels in the upper half of the image should be donated
                for i in whole_body_indexlist:  #whole_body donated to upper_under
                    nd_index = multilabel_to_ultimate21_conversion[i]
                    if nd_index is None:
                        logging.warning(
                            'ml index {} has no conversion (4upper)'.format(i))
                        continue  #donate upper pixels to upper_winner
                    logging.debug(
                        '3. donating nd {} in top of wholebody to upper_under and bottom to lower_under'
                        .format(nd_index))
                    logging.debug(
                        'first adding from upper part of nd {}  to nd {}, ysplit {}'
                        .format(nd_index, upper_winner_nd_index, y_split))
                    for y in range(0, final_mask.shape[0]):
                        if y <= y_split:
                            for x in range(0, final_mask.shape[1]):
                                if final_mask[y][x] == nd_index:
                                    final_mask[y][x] = upper_winner_nd_index
                n1 = len(final_mask[final_mask == upper_winner_nd_index])
                n2 = len(final_mask[final_mask == lower_winner_nd_index])
                logging.info(
                    'n in final mask from wholebody after donation to upper winner {} px,nd {} (lower {} px,nd {}'
                    .format(n1, upper_winner_nd_index, n2,
                            lower_winner_nd_index))

    # donate whole-body pixels to lower winner
            if lower_winner_nd_index is None:
                logging.warning(
                    'nd wholebody index {} ml index {} has no conversion '.
                    format(upper_winner_nd_index, upper_winner_index))
            else:
                n1 = len(final_mask[final_mask == upper_winner_nd_index])
                n2 = len(final_mask[final_mask == lower_winner_nd_index])
                logging.info(
                    'n in final mask from wholebody after donation to upper winner {} px,nd {} , lower winner {} px, nd {} '
                    .format(n1, upper_winner_nd_index, n2,
                            lower_winner_nd_index))
                #todo - actually only wholebody pixels in the upper half of the image should be donated
                for i in whole_body_indexlist:  #whole_body donated to upper_under
                    nd_index = multilabel_to_ultimate21_conversion[i]
                    if nd_index is None:
                        logging.warning(
                            'ml index {} has no conversion (4lower)'.format(i))
                        continue
            #donate upper pixels to upper_winner
                    logging.debug(
                        '3. donating nd {} in botto of wholebody to upper_under and bottom to lower_under'
                        .format(nd_index))
                    logging.debug(
                        'second, adding from lower part of nd {}  to nd {}, ysplit {}'
                        .format(nd_index, lower_winner_nd_index, y_split))
                    for y in range(0, final_mask.shape[0]):
                        if y > y_split:
                            for x in range(0, final_mask.shape[1]):
                                if final_mask[y][x] == nd_index:
                                    final_mask[y][x] = lower_winner_nd_index
            #donate upper pixels to lower_winner
                n1 = len(final_mask[final_mask == upper_winner_nd_index])
                n2 = len(final_mask[final_mask == lower_winner_nd_index])
                logging.info(
                    'n in final mask from wholebody donation to upper {} and lower {}:'
                    .format(n1, n2))

        logging.debug('after case two pixel values look like')
        count_values(final_mask, labels=constants.ultimate_21)

# third case - lowercover > wholebody > upper_under
# here its not clear who to sack - the lowercover or the wholebody
# so i arbitrarily decided to sack the whole_body in favor of the lowercover since lowercover is higher
# donate lower part of wholebody to lowerwinner and upper part to upper winner
# this can be combined with second case I guess as there is nothing different - whole body gets added to lower/upper winners

    elif (whole_body_winner_value < lower_cover_winner_value) and (
            whole_body_winner_value > upper_under_winner_value
    ) and whole_body_winner_value > multilabel_threshold:
        logging.info(
            'case 3. one part {} > upper under {} and < lower cover {}'.format(
                whole_body_winner_value, upper_under_winner_value,
                lower_cover_winner_value))
        if upper_winner_nd_index is None:
            logging.warning(
                'nd wholebody index {} ml index {} has no conversion '.format(
                    upper_winner_nd_index, upper_winner_index))
        else:
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody before donation to upper winner {} px,nd {} (lower {} px,nd {}'
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))
            #todo - actually only wholebody pixels in the upper half of the image should be donated
            for i in whole_body_indexlist:  #whole_body donated to upper_under
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning(
                        'ml index {} has no conversion (4upper)'.format(i))
                    continue  #donate upper pixels to upper_winner
                logging.debug(
                    '3. donating nd {} in top of wholebody to upper_under and bottom to lower_under'
                    .format(nd_index))
                logging.debug(
                    'first adding from upper part of nd {}  to nd {}, ysplit {}'
                    .format(nd_index, upper_winner_nd_index, y_split))
                for y in range(0, final_mask.shape[0]):
                    if y <= y_split:
                        for x in range(0, final_mask.shape[1]):
                            if final_mask[y][x] == nd_index:
                                final_mask[y][x] = upper_winner_nd_index
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody after donation to upper winner {} px,nd {} (lower {} px,nd {}'
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))

# donate whole-body pixels to lower winner
        if lower_winner_nd_index is None:
            logging.warning(
                'nd wholebody index {} ml index {} has no conversion '.format(
                    upper_winner_nd_index, upper_winner_index))
        else:
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody after donation to upper winner {} px,nd {} , lower winner {} px, nd {} '
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))
            #todo - actually only wholebody pixels in the upper half of the image should be donated
            for i in whole_body_indexlist:  #whole_body donated to upper_under
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning(
                        'ml index {} has no conversion (4lower)'.format(i))
                    continue
        #donate upper pixels to upper_winner
                logging.debug(
                    '3. donating nd {} in botto of wholebody to upper_under and bottom to lower_under'
                    .format(nd_index))
                logging.debug(
                    'second, adding from lower part of nd {}  to nd {}, ysplit {}'
                    .format(nd_index, lower_winner_nd_index, y_split))
                for y in range(0, final_mask.shape[0]):
                    if y > y_split:
                        for x in range(0, final_mask.shape[1]):
                            if final_mask[y][x] == nd_index:
                                final_mask[y][x] = lower_winner_nd_index
        #donate upper pixels to lower_winner
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody donation to upper {} and lower {}:'
                .format(n1, n2))

# fourth case - lowercover , upper_under > wholebody
# sack wholebody in favor of upper and lower
# donate top of wholebody to greater of upper cover/upper under (yes this is arbitrary and possibly wrong)
# donate bottom pixels of wholebody to greater of lower cover/lower under (again somewhat arbitrary)
# this also could get combined with #2,3 I suppose
# neurodoll_upper_cover_index = multilabel_to_ultimate21_conversion[upper_cover_winner_index] #
        logging.debug('after case three pixel values look like')
        count_values(final_mask, labels=constants.ultimate_21)

    elif (whole_body_winner_value < lower_cover_winner_value) and (
            whole_body_winner_value < upper_under_winner_value):
        logging.info(
            'case 4.one part {} < upper under {} and < lower cover {}'.format(
                whole_body_winner_value, upper_under_winner_value,
                lower_cover_winner_value))
        neurodoll_lower_cover_index = multilabel_to_ultimate21_conversion[
            lower_cover_winner_index]
        # donate whole-body pixels to upper winner
        if upper_winner_nd_index is None:
            logging.warning(
                'nd wholebody index {} ml index {} has no conversion '.format(
                    upper_winner_nd_index, upper_winner_index))
        else:
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody before donation to upper winner {} px,nd {} (lower {} px,nd {}'
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))
            #todo - actually only wholebody pixels in the upper half of the image should be donated
            for i in whole_body_indexlist:  #whole_body donated to upper_under
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning(
                        'ml index {} has no conversion (4upper)'.format(i))
                    continue  #donate upper pixels to upper_winner
                logging.debug(
                    '4. donating nd {} in top of wholebody to upper_under and bottom to lower_under'
                    .format(nd_index))
                logging.debug(
                    'first adding from upper part of nd {}  to nd {}, ysplit {}'
                    .format(nd_index, upper_winner_nd_index, y_split))
                for y in range(0, final_mask.shape[0]):
                    if y <= y_split:
                        for x in range(0, final_mask.shape[1]):
                            if final_mask[y][x] == nd_index:
                                final_mask[y][x] = upper_winner_nd_index
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody after donation to upper winner {} px,nd {} (lower {} px,nd {}'
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))

# donate whole-body pixels to lower winner
        if lower_winner_nd_index is None:
            logging.warning(
                'nd wholebody index {} ml index {} has no conversion '.format(
                    upper_winner_nd_index, upper_winner_index))
        else:
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody after donation to upper winner {} px,nd {} , lower winner {} px, nd {} '
                .format(n1, upper_winner_nd_index, n2, lower_winner_nd_index))
            #todo - actually only wholebody pixels in the upper half of the image should be donated
            for i in whole_body_indexlist:  #whole_body donated to upper_under
                nd_index = multilabel_to_ultimate21_conversion[i]
                if nd_index is None:
                    logging.warning(
                        'ml index {} has no conversion (4lower)'.format(i))
                    continue
        #donate upper pixels to upper_winner
                logging.debug(
                    '4. donating nd {} in botto of wholebody to upper_under and bottom to lower_under'
                    .format(nd_index))
                logging.debug(
                    'second, adding from lower part of nd {}  to nd {}, ysplit {}'
                    .format(nd_index, lower_winner_nd_index, y_split))
                for y in range(0, final_mask.shape[0]):
                    if y > y_split:
                        for x in range(0, final_mask.shape[1]):
                            if final_mask[y][x] == nd_index:
                                final_mask[y][x] = lower_winner_nd_index
        #donate upper pixels to lower_winner
            n1 = len(final_mask[final_mask == upper_winner_nd_index])
            n2 = len(final_mask[final_mask == lower_winner_nd_index])
            logging.info(
                'n in final mask from wholebody donation to upper {} and lower {}:'
                .format(n1, n2))

        logging.debug('after case four pixel values look like')
        count_values(final_mask, labels=constants.ultimate_21)

    foreground = np.array(
        (pixlevel_categorical_output > 0) * 1)  #*1 turns T/F into 1/0
    final_mask = final_mask * foreground  # only keep stuff that was part of original fg - this is already  true
    # unless we start adding pixvalues that didn't 'win'

    #7. if no lower cover and no whole-body was decided upon above: take max of lowercover items , donate losers to winner
    #8. take at most one lower under, donate losers to winner

    if (0):
        for i in range(len(thresholded_multilabel)):
            if thresholded_multilabel[i]:
                neurodoll_index = multilabel_to_ultimate21_conversion[i]
                if neurodoll_index is None:
                    print('no mapping from index {} (label {}) to neurodoll'.
                          format(i, multilabel_labels[i]))
                    continue
                nd_pixels = len(pixlevel_categorical_output[
                    pixlevel_categorical_output == neurodoll_index])
                print(
                    'index {} webtoollabel {} newindex {} neurodoll_label {} was above threshold {} (ml value {}) nd_pixels {}'
                    .format(i, multilabel_labels[i], neurodoll_index,
                            constants.ultimate_21[neurodoll_index],
                            multilabel_threshold, multilabel[i], nd_pixels))
                gray_layer = graylevel_nd_output[:, :, neurodoll_index]
                print('gray layer size:' + str(gray_layer.shape))
                #            item_mask = grabcut_using_neurodoll_output(url_or_np_array,neurodoll_index,median_factor=median_factor)
                if nd_pixels > 0:  #possibly put a threshold here, too few pixels and forget about it
                    item_mask = grabcut_using_neurodoll_graylevel(
                        url_or_np_array,
                        gray_layer,
                        median_factor=median_factor)
                    #the grabcut results dont seem too hot so i am moving to a 'nadav style' from-nd-and-ml-to-results system
                    #namely : for top , decide if its a top or dress or jacket
                    # for bottom, decide if dress/pants/skirt
                    pass
                else:
                    print('no pixels in mask, skipping')
                if item_mask is None:
                    continue
                item_mask = np.multiply(item_mask, neurodoll_index)
                if first_time_thru:
                    final_mask = np.zeros_like(item_mask)
                    first_time_thru = False
                unique_to_new_mask = np.logical_and(
                    item_mask != 0, final_mask == 0
                )  #dealing with same pixel claimed by two masks. if two masks include same pixel take first, don't add the pixel vals together
                unique_to_new_mask = np.multiply(unique_to_new_mask,
                                                 neurodoll_index)
                final_mask = final_mask + unique_to_new_mask
    #            cv2.imshow('mask '+str(i),item_mask)
    #            cv2.waitKey(0)
    timestamp = int(10 * time.time())

    #write file (for debugging)
    name = orig_filename + '_combinedoutput.png'

    print('combined png name:' + name + ' orig filename ' + orig_filename)
    cv2.imwrite(name, final_mask)
    nice_output = imutils.show_mask_with_labels(name,
                                                constants.ultimate_21,
                                                save_images=True,
                                                original_image=orig_filename +
                                                '.jpg',
                                                visual_output=False)
    #    nice_output = imutils.show_mask_with_labels(name,constants.ultimate_21,save_images=True)

    #save graymask, this should be identical to nd except no threshold on low amt of pixels
    graymask_filename = orig_filename + '_origmask.png'
    print('original mask file:' + graymask_filename)
    cv2.imwrite(graymask_filename, pixlevel_categorical_output)
    nice_output = imutils.show_mask_with_labels(graymask_filename,
                                                constants.ultimate_21,
                                                save_images=True,
                                                original_image=orig_filename +
                                                '.jpg',
                                                visual_output=False)
    count_values(final_mask, labels=constants.ultimate_21)

    return final_mask
Exemplo n.º 16
0
        subprocess.call(copycmd, shell=True)

#generate report filename
if type == 'pixlevel':
    outname = os.path.join(
        outdir, outdir[2:] + '_netoutput.txt'
    )  #TODO fix the shell script to not look for this, then it wont be needed
if type == 'multilabel':
    outname = os.path.join(outdir, outdir[2:] + '_mlresults.html')
if type == 'single_label':
    outdir = outdir + '_' + cat
    outname = os.path.join(outdir, outdir[2:] + '_' + cat + '_slresults.txt')
loss_outputname = os.path.join(outdir, outdir[2:] + '_loss.txt')
print('outname:{}\n lossname {}\n outdir {}\n'.format(outname, loss_outputname,
                                                      outdir))
Utils.ensure_dir(outdir)
time.sleep(0.1)
Utils.ensure_file(loss_outputname)

#copycmd = 'cp -r '+outdir + ' ' + host_dirname
scpcmd = 'rsync -avz ' + outdir + ' [email protected]:/var/www/results/' + type + '/'

i = 0
losses = []
iters = []
loss_avg = [0] * n_iter
accuracy_avg = [0] * n_iter
tot_iters = 0

#instead of taking steps its also possible to do
#solver.solve()
Exemplo n.º 17
0
def get_hydra_output(url_or_image_arr,
                     out_dir='./',
                     orig_size=(256, 256),
                     crop_size=(224, 224),
                     mean=(104.0, 116.7, 122.7),
                     gpu=1,
                     save_data=True,
                     save_path='/data/jeremy/caffenets/hydra/production/saves',
                     detection_thresholds=constants.hydra_tg_thresholds,
                     url=None):
    '''
    start net, get a bunch of results. DONE: resize to e.g. 250x250 (whatever was done in training) and crop to dims
    :param url_or_image_arr_list:#
    :param prototxt:
    :param caffemodel:
    :param out_dir:
    :param dims:
    :param output_layers:
    :param mean:
    :return:
    '''
    detection_thresholds = None
    start_time = time.time()
    caffe.set_mode_gpu()
    caffe.set_device(gpu)
    #    print('params:'+str(hydra_net.params))
    out_layers = hydra_net.outputs
    out_layers = put_in_numeric_not_alphabetic_order(out_layers)
    #    print('out layers: '+str(out_layers))
    j = 0
    output_names = constants.hydra_tg_heads

    # load image, resize, crop, subtract mean, and make dims C x H x W for Caffe
    im = Utils.get_cv2_img_array(url_or_image_arr)
    if im is None:
        logging.warning('could not get image ' + str(url_or_image_arr))
        return None
    if isinstance(url_or_image_arr, basestring):
        print('get_hydra_output working on:' + url_or_image_arr)
    print('img  size:' + str(im.shape))
    im = imutils.resize_keep_aspect(im, output_size=orig_size)
    im = imutils.center_crop(im, crop_size)

    in_ = np.array(im, dtype=np.float32)
    if len(in_.shape) != 3:
        print('got 1-chan image, skipping')
        return
    elif in_.shape[2] != 3:
        print('got n-chan image, skipping - shape:' + str(in_.shape))
        return
    in_ -= mean
    in_ = in_.transpose((2, 0, 1))  #W,H,C -> C,W,H
    hydra_net.blobs['data'].reshape(1, *in_.shape)
    hydra_net.blobs['data'].data[...] = in_
    # run net and take argmax for prediction
    hydra_net.forward()
    out = {}
    i = 0
    for output_layer in out_layers:
        one_out = hydra_net.blobs[output_layer].data[
            0]  #not sure why the data is nested [1xN] matrix and not a flat [N] vector
        second_neuron = copy.copy(
            one_out[1]
        )  #the copy is required - if you dont do it then out gets over-written with each new one_out
        second_neuron = round(float(second_neuron), 3)
        #      print('type:'+str(type(second_neuron)))
        name = output_names[i]
        if detection_thresholds is None:
            out[name] = second_neuron
            print('{} for category {} {}'.format(second_neuron, i, name))
        elif second_neuron > detection_thresholds[i]:
            out[name] = second_neuron
            print('{} is past threshold {} for category {} {}'.format(
                second_neuron, detection_thresholds[i], i, name))
        logging.debug('output for {} {} is {}'.format(output_layer, name,
                                                      second_neuron))
        #        print('final till now:'+str(all_outs)+' '+str(all_outs2))
        i = i + 1
    logging.debug('all output:' + str(out))
    logging.debug('elapsed time:' + str(time.time() - start_time))

    if save_data:
        if isinstance(url_or_image_arr, basestring):
            filename = url_or_image_arr.replace('https://', '').replace(
                'http://', '').replace('/', '_')
            url = url_or_image_arr
        else:
            n_chars = 6
            filename = ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(n_chars)) + '.jpg'
            if url is None:
                url = 'not_from_url'
        Utils.ensure_dir(save_path)
        imgname = os.path.join(save_path, filename)
        if imgname[:-4] != '.jpg':
            imgname = imgname + '.jpg'
        cv2.imwrite(imgname, im)
        #    out['imgname']=filename
        out['url'] = url
        textfile = os.path.join(save_path, 'output.txt')
        with open(textfile, 'a') as fp:
            json.dump(out, fp, indent=4)
            fp.close()
        print('wrote image to {} and output text to {}'.format(
            imgname, textfile))

    return out
Exemplo n.º 18
0
def single_label_acc(caffemodel,
                     testproto,
                     net=None,
                     label_layer='label',
                     estimate_layer='loss',
                     n_tests=100,
                     gpu=0,
                     classlabels=constants.web_tool_categories_v2,
                     save_dir=None):
    #TODO dont use solver to get inferences , no need for solver for that
    #DONE
    print('checking accuracy of net {} using proto {}'.format(
        caffemodel, testproto))
    n_classes = len(classlabels)
    print('nclasses {} labels {}'.format(n_classes, classlabels))
    if net is None:
        caffe.set_mode_gpu()
        caffe.set_device(gpu)
        #        net = caffe.Net(testproto,caffemodel, caffe.TEST)  #apparently this is how test is chosen instead of train if you use a proto that contains both test and train
        if caffemodel == '':
            caffemodel = None  #hack to keep things working, ideally change refs to caffemodel s.t. None is ok
            net = caffe.Net(
                testproto, caffe.TEST
            )  #apparently this is how test is chosen instead of train if you use a proto that contains both test and train
        else:
            net = caffe.Net(
                testproto, caffe.TEST, weights=caffemodel
            )  #apparently this is how test is chosen instead of train if you use a proto that contains both test and train
        #Net('train_val.prototxt', 1, weights='')
    if caffemodel is not '' and caffemodel is not None:
        model_base = caffemodel.split('/')[-1]
    else:
        model_base = 'scratch'
    protoname = testproto.replace('.prototxt', '')
    netname = multilabel_accuracy.get_netname(testproto)
    if netname:
        name = 'single_label_' + netname + '_' + model_base.replace(
            '.caffemodel', '')
    else:
        name = 'single_label_' + protoname + '_' + model_base.replace(
            '.caffemodel', '')
    name = name.replace('"', '')  #remove quotes
    name = name.replace(' ', '')  #remove spaces
    name = name.replace('\n', '')  #remove newline
    name = name.replace('\r', '')  #remove return
    htmlname = name + '.html'
    if save_dir is not None:
        Utils.ensure_dir(save_dir)
        htmlname = os.path.join(save_dir, htmlname)
    print('htmlname : ' + str(htmlname))
    #    Utils.ensure_dir(dir)

    confmat = check_accuracy(net,
                             n_classes,
                             n_tests=n_tests,
                             label_layer=label_layer,
                             estimate_layer=estimate_layer)
    open_html(htmlname,
              testproto,
              caffemodel,
              netname=netname,
              classlabels=classlabels)  #
    write_confmat_to_html(htmlname, confmat, classlabels=classlabels)
    acc_list = []
    for i in range(n_classes):
        p, r, a = precision_recall_accuracy(confmat, i)
        acc_list.append(a)
        write_pra_to_html(htmlname, p, r, a, i, classlabels[i])
    close_html(htmlname)
    #    accs = np.array(acc_list)
    #    mean_acc = np.mean(accs)
    acc = overall_acc_from_confmat(confmat)
    return acc
Exemplo n.º 19
0
def local_yolo(img_arr,
               url='',
               classes=constants.hls_yolo_categories,
               save_results=True):
    #                item = {'object':label,'bbox':[xmin,ymin,xmax,ymax],'confidence':'>'+str(thresh)}
    print('started local pyyolo detect, thresh=' + str(thresh) + ' hier ' +
          str(hier_thresh))
    save_path = '/data/jeremy/pyyolo/results/'
    if img_arr is None:
        print('got None img array!!')
        return
    img_original = copy.copy(img_arr)
    if len(img_arr.shape) == 2:  #got 1-chan(gray) image
        print('got gray img')
        img_arr_bgr = np.zeros([img_arr.shape[0], img_arr.shape[1], 3])
        img_arr_bgr[:, :] = img_arr  #copy bw image into all channels
        print('sizes: {} {}'.format(img_arr_bgr, img_arr))
        img_arr = img_arr_bgr
    print('img arr size {}'.format(img_arr.shape))
    max_image_size = 1200  #1280 fails
    resized = False
    if img_arr.shape[0] > max_image_size or img_arr.shape[
            1] > max_image_size:  #maybe this is causing prob at http://a.abcnews.com/images/US/150815_yt_phillypd_33x16_1600.jpg
        maxside = max(img_arr.shape)
        reduction_factor = maxside / 1000.0  #force maxside to 1000
        original_size = img_arr.shape
        resized = True
        img_arr = cv2.resize(img_arr,
                             (int(img_arr.shape[1] / reduction_factor),
                              int(img_arr.shape[0] / reduction_factor)))
        print('reduced size to {}'.format(img_arr.shape))
        #generate randonm filename
    hash = hashlib.sha1()
    hash.update(str(time.time()))
    if save_results:
        timestr = time.strftime("%Y%m%d.%H%M%S")
        img_filename = timestr + hash.hexdigest()[:5] + '.jpg'
        Utils.ensure_dir(save_path)
        img_path = os.path.join(save_path, img_filename)
        print('detect_yolo_pyyolo saving file ' + str(img_path))
        try:
            cv2.imwrite(img_path, img_arr)
        except:
            print('some trouble saving image,' + str(sys.exc_info()[0]))
    relevant_items = []

    ###############
    #action is here
    ###############
    print('getting results from get_pyyolo_results')
    yolo_results = get_local_pyyolo_results(img_arr)
    print('got results from get_pyyolo_results')

    if resized:
        img_arr = cv2.resize(img_arr, (original_size[1], original_size[0]))
        print('de-resizing image')
    for item in yolo_results:
        print(item)
        if resized:
            print('de-resizing bbs')
            item['bbox'][0] = int(item['bbox'][0] * reduction_factor)
            item['bbox'][1] = int(item['bbox'][1] * reduction_factor)
            item['bbox'][2] = int(item['bbox'][2] * reduction_factor)
            item['bbox'][3] = int(item['bbox'][3] * reduction_factor)
            print('fixed size back to original {}'.format(item))

        xmin = item['bbox'][0]
        ymin = item['bbox'][1]
        xmax = item['bbox'][2]
        ymax = item['bbox'][3]
        assert xmin < xmax, 'xmin not < xmax!!!'
        assert ymin < ymax, 'xmin not < xmax!!!'

        relevant_items.append(item)
        imutils.bb_with_text(img_arr,
                             [xmin, ymin, (xmax - xmin),
                              (ymax - ymin)], item['object'])

    if save_results:
        marked_imgname = img_path.replace('.jpg', '_bb_yolos.jpg')
        json_name = img_path.replace('.jpg', '.json')
        print('pyyolo bb image being writtten to ' + str(marked_imgname))
        print('pyyolo bb data being writtten to ' + str(json_name))
        try:
            with open(json_name, 'w') as fp:
                json.dump(yolo_results, fp, indent=4)
            r = cv2.imwrite(marked_imgname, img_arr)
            #skip saving bb image to conserve space
            print('imgwrite result ' + str(r))
        except:
            print('some trouble saving bb image or data:' +
                  str(sys.exc_info()[0]))

    output_like_api = {'annotations': relevant_items}
    print('detect yolo returning:' + str(output_like_api))

    return output_like_api
Exemplo n.º 20
0
def write_html(p,
               r,
               a,
               n,
               threshold,
               modelname,
               positives=False,
               dir=None,
               name=None,
               classlabels=None):
    model_base = os.path.basename(modelname)

    if dir is not None:
        Utils.ensure_dir(dir)
        htmlname = os.path.join(dir, model_base + 'results.html')
    else:
        htmlname = os.path.join(model_base, 'results.html')
    if name is not None:
        htmlname = name

    open_html(model_base, dir=dir, classlabels=classlabels)

    with open(htmlname, 'a') as g:
        fwavp = 0
        fwavr = 0
        fwava = 0
        n_p = 0
        n_r = 0
        n_a = 0
        fwavn = 0
        n_sum = 0
        #calculate frequency-weighted averages
        for i in range(len(p)):
            if not np.isnan(p[i]):
                fwavp = fwavp + p[i] * n[i]
                n_p = n_p + n[i]
            if not np.isnan(r[i]):
                fwavr = fwavr + r[i] * n[i]
                n_r = n_r + n[i]
            if not np.isnan(a[i]):
                fwava = fwava + a[i] * n[i]
                n_a = n_a + n[i]
            n_sum = n_sum + n[i]
        print(
            'n sum {} fwavp {} fwavr {} fwava {} before division np {} nr {} na {} '
            .format(n_sum, fwavp, fwavr, fwava, n_p, n_r, n_a))
        fwavp = fwavp / float(n_p)
        fwavr = fwavr / float(n_r)
        fwava = fwava / float(n_a)
        fwavn = n_sum / float(len(p))

        print('frequency weighted averages p {} r {} acc {} n {}'.format(
            fwavp, fwavr, fwava, fwavn))
        g.write('frequency weighted averages p {} r {} acc {} n {}'.format(
            round(fwavp, 3), round(fwavr, 3), round(fwava, 3), round(fwavn,
                                                                     3)))
        #write line with n_positives
        if (positives):
            g.write('<tr>\n')
            g.write('<td>')
            g.write('n_positives')
            g.write('</td>\n')
            g.write('<td>')
            g.write(str(round(fwavn, 3)))
            g.write('</td>\n')
            for i in range(len(p)):
                g.write('<td>')
                g.write(str(int(n[i])))
                g.write('</td>\n')
            g.write('</tr>\n<br>\n')

    #write line with threshold
#       g.write('<table style=\"width:100%\">\n')
        g.write('<b>')
        g.write('<tr>\n')
        g.write('<td>')
        g.write('threshold\n')
        g.write('</td>')
        g.write('<td>')
        g.write('')
        g.write('</td>\n')
        for i in range(len(p)):
            g.write('<td>')
            g.write(str(round(threshold, 3)))
            g.write('</td>\n')
        g.write('</tr>\n')
        g.write('</b>')

        #write row with precision
        g.write('<tr>\n')
        g.write('<td>')
        g.write('precision')
        g.write('</td>\n')
        g.write('<td>')
        g.write(str(round(fwavp, 3)))
        g.write('</td>\n')
        for i in range(len(p)):
            g.write('<td>')
            g.write(str(round(p[i], 3)))
            g.write('</td>\n')
        g.write('</tr>\n')

        #write row with recall
        g.write('<tr>\n')
        g.write('<td>')
        g.write('recall')
        g.write('</td>\n')
        g.write('<td>')
        g.write(str(round(fwavr, 3)))
        g.write('</td>\n')
        for i in range(len(p)):
            g.write('<td>')
            g.write(str(round(r[i], 3)))
            g.write('</td>\n')
        g.write('</tr>\n')

        #write row with accuracy
        g.write('<tr>\n')
        g.write('<td>')
        g.write('accuracy')
        g.write('</td>\n')
        g.write('<td>')
        g.write(str(round(fwava, 3)))
        g.write('</td>\n')
        for i in range(len(p)):
            g.write('<td>')
            g.write(str(round(a[i], 3)))
            g.write('</td>\n')
        g.write('</tr>\n<br>\n')

        g.write('<tr><td><br/></td></tr>')  #blank row
Exemplo n.º 21
0
def selectsiya(dir):
    alone_dir = os.path.join(dir,'alone')
    Utils.ensure_dir(alone_dir)
    delete_dir = os.path.join(dir,'delete_these')
    Utils.ensure_dir(delete_dir)
    good_images_dir = os.path.join(dir,'good_images')
    Utils.ensure_dir(good_images_dir)
    files = [f for f in os.listdir(dir) if 'jpg' in f]
 #   print('files:'+str(files))
    n = 0
    i = 0
    start_time=time.time()
    while i < len(files):
        f = files[i]
        print('i='+str(i))
        count_curdir = len([g for g in os.listdir(dir) if os.path.isfile(os.path.join(dir, g))])
        count_alonedir = len([g for g in os.listdir(alone_dir) if os.path.isfile(os.path.join(alone_dir, g))])
        count_deletedir = len([g for g in os.listdir(delete_dir) if os.path.isfile(os.path.join(delete_dir, g))])
        count_goodimagesdir = len([g for g in os.listdir(good_images_dir) if os.path.isfile(os.path.join(good_images_dir, g))])
        print(str(n)+' done of '+ str(count_curdir)+' files, '+str(count_goodimagesdir)+' good images, '+str(count_alonedir)+' alone, '+str(count_deletedir)+' deleted, tpi='+str((time.time()-start_time)/(i+1)))
        fullfile = os.path.join(dir,f)
        print('file:'+str(fullfile))
        try:
            img_arr = cv2.imread(fullfile)
        except:
            print('something bad happened trying to imread')
            continue
        try:
            h,w = img_arr.shape[0:2]
            img_arr = cv2.resize(img_arr,dsize=(w*2,h*2))
            cv2.imshow('candidate',img_arr)
        except:
            print('something bad happened trying to imshow')
            destname = os.path.join(delete_dir, f)
            print('source:'+fullfile+' dest:'+destname)
            os.rename(fullfile,destname)
        print('(d)elete (a)lone (b)ack (space)nothing (q)uit ')
        c = cv2.waitKey(0)
        if c == ord('b'):
            print('go back')
            prev_dir = os.path.join(dir, files[i-1])
            os.rename(destname, prev_dir)
            print('moved image to main dir')
            i=i-1
            continue
        elif c == ord('d'):
            print('delete')
            destname = os.path.join(delete_dir, f)
            print('source:'+fullfile+' dest:'+destname)
            os.rename(fullfile,destname)
        elif c == ord('a'):
            print('alone')
            destname = os.path.join(alone_dir, f)
            print('source:'+fullfile+' dest:'+destname)
            os.rename(fullfile,destname)
        elif c == ord(' '):
            print('good image')
            destname = os.path.join(good_images_dir, f)
            print('source:' + fullfile + ' dest:' + destname)
            os.rename(fullfile, destname)
        elif c == ord('q'):
            print('quit')
            cv2.destroyAllWindows()
            return
        else:
            print('nothing')
        n = n + 1
        i = i + 1
Exemplo n.º 22
0
def get_pixlevel_confmat_using_falcon(images_and_labels_file,
                                      labels=constants.ultimate_21,
                                      save_dir='./nd_output'):
    with open(images_and_labels_file, 'r') as fp:
        lines = fp.readlines()
    n_cl = len(labels)
    hist = np.zeros((n_cl, n_cl))
    loss = 0
    print('n channels: ' + str(n_cl))
    start_time = time.time()
    for line in lines:
        imagefile = line.split()[0]
        gtfile = line.split()[1]
        img_arr = cv2.imread(imagefile)
        if img_arr is None:
            logging.warning('could not get image data from ' + imagefile)
            continue
        gt_data = cv2.imread(gtfile)
        if gt_data is None:
            logging.warning('could not get gt data from ' + gtfile)
            continue
        if len(gt_data.shape) == 3:
            logging.warning('got 3 chan image for mask, taking chan 0 ' +
                            gtfile)
            gt_data = gt_data[:, :, 0]

        dic = nfc.pd(img_arr)
        if not dic['success']:
            logging.debug('nfc pd not a success')
            continue
        net_data = dic['mask']
        print('sizes of gt {} net output {}'.format(gt_data.shape,
                                                    net_data.shape))

        hist += jrinfer.fast_hist(gt_data.flatten(), net_data.flatten(), n_cl)

        if save_dir:
            Utils.ensure_dir(save_dir)
            gt_name = os.path.basename(imagefile)[:-4] + '_gt_legend.jpg'
            gt_name = os.path.join(save_dir, gt_name)
            ndout_name = os.path.basename(imagefile)[:-4] + '_ndout_legend.jpg'
            ndout_name = os.path.join(save_dir, ndout_name)
            imutils.show_mask_with_labels(gt_data,
                                          labels,
                                          original_image=imagefile,
                                          save_images=True,
                                          visual_output=False,
                                          savename=gt_name)
            imutils.show_mask_with_labels(
                net_data,
                labels,
                original_image=imagefile,
                save_images=True,
                visual_output=False,
                savename=ndout_name)  # compute the loss as well

    results_dict = jrinfer.results_from_hist(hist,
                                             save_file=os.path.join(
                                                 save_dir, 'output.html'))
    print results_dict
    elapsed_time = time.time() - start_time
    print('elapsed time: ' + str(elapsed_time) + ' tpi:' +
          str(float(elapsed_time) / len(lines)))
    return hist
Exemplo n.º 23
0
def do_pixlevel_accuracy(caffemodel,
                         n_tests,
                         layer,
                         classes=constants.ultimate_21,
                         testproto=None,
                         solverproto=None,
                         iter=0,
                         savepics=True,
                         gpu=0,
                         output_layer='output'):
    #to do accuracy we prob dont need to load solver
    caffemodel_base = os.path.basename(caffemodel)
    dir = 'pixlevel_results-' + caffemodel_base.replace('.caffemodel', '')
    Utils.ensure_dir(dir)
    if savepics:
        picsdir = os.path.join(dir, caffemodel_base + '_output')
        Utils.ensure_dir(picsdir)
    else:
        picsdir = False
    htmlname = os.path.join(dir, dir + '.html')
    detailed_outputname = htmlname[:-5] + '.txt'
    print('saving net of {} {} to dir {} html {} and file {}'.format(
        caffemodel, solverproto, picsdir, htmlname, detailed_outputname))

    n_images = range(n_tests)
    if gpu is not None:
        caffe.set_device(gpu)
    else:
        caffe.set_mode_cpu()

    if (solverproto is not None):  #do this the old way with sgdsolver
        solver = caffe.SGDSolver(solverproto)
        solver.net.copy_from(caffemodel)
        print('using net defined by {} and {} '.format(solverproto,
                                                       caffemodel))
        answer_dict = jrinfer.seg_tests(solver,
                                        n_images,
                                        output_layer=output_layer,
                                        gt_layer='label',
                                        outfilename=detailed_outputname)

    elif (testproto is not None):  #try using net without sgdsolver
        net = caffe.Net(testproto, caffemodel, caffe.TEST)
        answer_dict = jrinfer.do_seg_tests(net,
                                           iter,
                                           picsdir,
                                           n_images,
                                           output_layer=output_layer,
                                           gt_layer='label',
                                           outfilename=detailed_outputname)

#   in_ = np.array(im, dtype=np.float32)
#   net.blobs['data'].reshape(1, *in_.shape)
#   net.blobs['data'].data[...] = in_
#   # run net and take argmax for prediction
#   net.forward()
#   out = net.blobs['seg-score'].data[0].argmax(axis=0)

    open_html(htmlname, caffemodel, solverproto, classes, answer_dict)
    write_html(htmlname, answer_dict)
    close_html(htmlname)
Exemplo n.º 24
0
def dosolve(weights,
            solverproto,
            testproto,
            type='single_label',
            steps_per_iter=1,
            n_iter=200,
            n_loops=200,
            n_tests=1000,
            cat=None,
            classlabels=None,
            baremetal_hostname='brainiK80a',
            solverstate=None):

    if classlabels is None:
        classlabels = ['not_' + cat, cat]
    caffe.set_device(int(sys.argv[1]))
    caffe.set_mode_gpu()
    solver = caffe.get_solver(solverproto)
    if weights is not None:
        solver.net.copy_from(weights)
    if solverstate is not None:
        solver.restore(
            solverstate)  #see https://github.com/BVLC/caffe/issues/3651
        #No need to use solver.net.copy_from(). .caffemodel contains the weights. .solverstate contains the momentum vector. Both are needed to restart training. If you restart training without momentum, the loss will spike up and it will take ~50k iterations to recover. At test time you only need .caffemodel.
    training_net = solver.net
    solver.test_nets[0].share_with(
        solver.net)  #share train weight updates with testnet
    test_net = solver.test_nets[0]  # more than one testnet is supported

    net_name = multilabel_accuracy.get_netname(testproto)

    docker_hostname = socket.gethostname()

    datestamp = datetime.datetime.strftime(datetime.datetime.now(),
                                           'time%H.%M_%d-%m-%Y')
    prefix = baremetal_hostname + '_' + net_name + '_' + docker_hostname + '_' + datestamp

    #get netname, copy train/test to outdir
    tt = caffe_utils.get_traintest_from_proto(solverproto)
    print('netname {} train/test {}'.format(net_name, tt))

    #detailed_jsonfile = detailed_outputname[:-4]+'.json'
    if weights:
        weights_base = os.path.basename(weights)
    else:
        weights_base = '_noweights_'
    threshold = 0.5
    if net_name:
        outdir = type + '_' + prefix + '_' + weights_base.replace(
            '.caffemodel', '')
    else:
        outdir = type + '_' + prefix + '_' + testproto + '_' + weights_base.replace(
            '.caffemodel', '')
    outdir = outdir.replace('"', '')  #remove quotes
    outdir = outdir.replace(' ', '')  #remove spaces
    outdir = outdir.replace('\n', '')  #remove newline
    outdir = outdir.replace('\r', '')  #remove return
    outdir = './' + outdir

    #generate report filename, outdir to save everything (loss, html etc)
    if type == 'pixlevel':
        outname = os.path.join(
            outdir, outdir[2:] + '_netoutput.txt'
        )  #TODO fix the shell script to not look for this, then it wont be needed
    if type == 'multilabel':
        outname = os.path.join(outdir, outdir[2:] + '_mlresults.html')
    if type == 'single_label':
        outdir = outdir + '_' + cat
        outname = os.path.join(outdir,
                               outdir[2:] + '_' + cat + '_slresults.txt')
    loss_outputname = os.path.join(outdir, outdir[2:] + '_loss.txt')
    print('outname:{}\n lossname {}\n outdir {}\n'.format(
        outname, loss_outputname, outdir))
    Utils.ensure_dir(outdir)
    time.sleep(0.1)
    Utils.ensure_file(loss_outputname)

    #copy training and test files to outdir
    if tt is not None:
        if len(tt) == 1:  #copy single traintest file to dir of info
            copycmd = 'cp ' + tt[0] + ' ' + outdir
            subprocess.call(copycmd, shell=True)
        else:  #copy separate train and test files to dir of info
            copycmd = 'cp ' + tt[0] + ' ' + outdir
            subprocess.call(copycmd, shell=True)
            copycmd = 'cp ' + tt[1] + ' ' + outdir
            subprocess.call(copycmd, shell=True)
    #cpoy solverproto to results dir
    if solverproto is not None:
        copycmd = 'cp ' + solverproto + ' ' + outdir
        subprocess.call(copycmd, shell=True)
    #copy test proto to results dir
    if testproto is not None:
        copycmd = 'cp ' + testproto + ' ' + outdir
        subprocess.call(copycmd, shell=True)
    #copy this file too
    copycmd = 'cp solve.py ' + outdir
    subprocess.call(copycmd, shell=True)

    #copycmd = 'cp -r '+outdir + ' ' + host_dirname
    scpcmd = 'rsync -avz ' + outdir + ' [email protected]:/var/www/results/' + type + '/'

    i = 0
    losses = []
    iters = []
    loss_avg = np.zeros(n_iter)
    accuracy_avg = np.zeros(n_iter)
    tot_iters = 0

    #instead of taking steps its also possible to do
    #solver.solve()

    if type == 'multilabel':
        multilabel_accuracy.open_html(
            weights,
            dir=outdir,
            solverproto=solverproto,
            caffemodel=weights,
            classlabels=constants.web_tool_categories_v2,
            name=outname)

    for _ in range(n_loops):
        for i in range(n_iter):
            solver.step(steps_per_iter)
            #        loss = solver.net.blobs['score'].data
            loss = solver.net.blobs['loss'].data
            loss_avg[i] = loss
            losses.append(loss)
            tot_iters = tot_iters + steps_per_iter * n_iter
            if type == 'single_label':
                accuracy = solver.net.blobs['accuracy'].data
                accuracy_avg[i] = accuracy
                print('iter ' + str(i * steps_per_iter) + ' loss:' +
                      str(loss) + ' acc:' + str(accuracy))
            else:
                print('iter ' + str(i * steps_per_iter) + ' loss:' + str(loss))

        averaged_loss = np.mean(loss_avg)
        if type == 'single_label':
            averaged_acc = np.mean(accuracy_avg)
            s = 'avg loss over last {} steps is {}, acc:{}'.format(
                n_iter * steps_per_iter, averaged_loss, averaged_acc)
            print(s)
            s2 = '{}\t{}\t{}\n'.format(tot_iters, averaged_loss, averaged_acc)
        else:
            s = 'avg loss over last {} steps is {}'.format(
                n_iter * steps_per_iter, averaged_loss)
            print(s)
            s2 = '{}\t{}\n'.format(tot_iters, averaged_loss)
        #for test net:
    #    solver.test_nets[0].forward()  # test net (there can be more than one)
        with open(loss_outputname, 'a+') as f:
            f.write(str(int(time.time())) + '\t' + s2)
            f.close()
    #    progress_plot.lossplot(loss_outputname)  this hits tkinter problem
        if type == 'multilabel':
            precision, recall, accuracy, tp, tn, fp, fn = multilabel_accuracy.check_acc(
                test_net,
                num_samples=n_tests,
                threshold=0.5,
                gt_layer='labels',
                estimate_layer='prob')
            print('solve.py: p {} r {} a {} tp {} tn {} fp {} fn {}'.format(
                precision, recall, accuracy, tp, tn, fp, fn))
            n_occurences = [tp[i] + fn[i] for i in range(len(tp))]
            multilabel_accuracy.write_html(precision,
                                           recall,
                                           accuracy,
                                           n_occurences,
                                           threshold,
                                           weights,
                                           positives=True,
                                           dir=outdir,
                                           name=outname)
        elif type == 'pixlevel':
            # number of tests for pixlevel
            val = range(0, n_tests)  #
            jrinfer.seg_tests(solver,
                              val,
                              output_layer='mypixlevel_output',
                              gt_layer='label',
                              outfilename=outname,
                              save_dir=outdir)

        elif type == 'single_label':
            acc = single_label_accuracy.single_label_acc(
                weights,
                testproto,
                net=test_net,
                label_layer='label',
                estimate_layer='fc4_0',
                n_tests=n_tests,
                classlabels=classlabels,
                save_dir=outdir)
            #       test_net = solver.test_nets[0] # more than one testnet is supported
            #        testloss = test_net.blobs['loss'].data
            try:
                testloss = test_net.blobs['loss'].data
            except:
                print('n o testloss available')
                testloss = 0
            with open(loss_outputname, 'a+') as f:
                f.write('test\t' + str(int(time.time())) + '\t' +
                        str(tot_iters) + '\t' + str(testloss) + '\t' +
                        str(acc) + '\n')
                f.close()
    #
    #   subprocess.call(copycmd,shell=True)
        subprocess.call(scpcmd, shell=True)
Exemplo n.º 25
0
    #    imgfiles = [f.split('.')[0] for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]

    labelfiles = [f for f in os.listdir(labelpath) if 'png' in f]
    imgfiles = [f for f in os.listdir(imgpath) if 'jpg' in f]

    print('nfiles:' + str(len(labelfiles)))
    #    fashionista_ordered_categories = constants.fashionista_categories
    #in case it changes in future - as of 2/16 this list goes a little something like this:
    #fashionista_categories = ['null','tights','shorts','blazer','t-shirt','bag','shoes','coat','skirt','purse',
    # 'boots',  'blouse','jacket','bra','dress','pants','sweater','shirt','jeans','leggings','scarf','hat',
    #              'top','cardigan','accessories','vest','sunglasses','belt','socks','glasses','intimate',
    #            'stockings','necklace','cape','jumper','sweatshirt','suit','bracelet','heels','wedges','ring',
    #          'flats','tie','romper','sandals','earrings','gloves','sneakers','clogs','watch','pumps','wallet',
    #        'bodysuit','loafers','hair','skin']
    #    colorbars(fashionista_ordered_categories)
    Utils.ensure_dir(savedir)
    for file in imgfiles:
        full_img = os.path.join(imgpath, file)
        full_lbl = os.path.join(labelpath, file.replace('.jpg', '.png'))
        if not os.path.isfile(full_img):
            print('img {} is not there '.format(full_img))
            continue
        if not os.path.isfile(full_lbl):
            print('img {} is not there '.format(full_lbl))
            continue
        save_clothing_parts(full_lbl, full_img, savedir=savedir)

#      raw_input('enter')
#        show_pd_results(fullpath)
#        raw_input('enter for next')
Exemplo n.º 26
0
    def detect_yolo_shell(self, img_arr, url='',classes=constants.hls_yolo_categories,save_results=True):
        #RETURN dict like: ({'object':class_name,'bbox':bbox,'confidence':round(float(score),3)})
 #  relevant_bboxes.append({'object':class_name,'bbox':bbox,'confidence':round(float(score),3)})
        print('started defense_falcon_rcnn.detect_yolo')
        hash = hashlib.sha1()
        hash.update(str(time.time()))
      #  img_filename = 'incoming.jpg'
        yolo_path = '/data/jeremy/darknet_python/darknet'
#        cfg_path = '/data/jeremy/darknet_python/cfg/yolo-voc_544.cfg'
        cfg_path = '/data/jeremy/darknet_python/cfg/yolo-voc_608.cfg'
#        weights_path = '/data/jeremy/darknet_python/yolo-voc_544_95000.weights'
        weights_path = '/data/jeremy/darknet_python/yolo-voc_608_46000.weights'
        save_path = './results/'
        detections_path = 'detections.txt'  #these are getting stored in local dir it seems
        img_filename = hash.hexdigest()[:10]+'.jpg'
        Utils.ensure_dir(save_path)
        img_path = os.path.join(save_path,img_filename)
        print('save file '+str(img_path))
        cv2.imwrite(img_path,img_arr)
        try:
            os.remove(detections_path)  #this is a temp file to hold current detection - erase then write
        except:
            print('file {} doesnt exist'.format(detections_path))
        cmd = yolo_path+' detect '+cfg_path+' '+weights_path+' '+img_path
        subprocess.call(cmd, shell=True)  #blocking call
        time.sleep(0.1) #wait for file to get written
        relevant_bboxes = []
        if not os.path.exists(detections_path):
            return []
        with open(detections_path,'r') as fp:
            lines = fp.readlines()
            fp.close()
        saved_detections = img_path.replace('.jpg','.txt')
        with open(saved_detections,'w') as fp2: #copy into another file w unique name so we can delete original
            fp2.writelines(lines)
            fp2.close()

        for line in lines:
            label_index,confidence,xmin,ymin,xmax,ymax = line.split()
            label_index=int(label_index)
            label=classes[label_index]
            confidence=float(confidence)
            xmin=int(xmin)
            xmax=int(xmax)
            ymin=int(ymin)
            ymax=int(ymax)
            item = {'object':label,'bbox':[xmin,ymin,xmax,ymax],'confidence':round(float(confidence),3)}
            if label == 'person':
                cropped_image = img_arr[ymin:ymax, xmin:xmax]
                # print('crop:{} {}'.format(item["bbox"],cropped_image.shape))
                # get hydra results
                try:
                    hydra_output = self.get_hydra_output(cropped_image)
                    if hydra_output:
                        item['details'] = hydra_output
                except:
                    print "Hydra failed " + traceback.format_exc()
            relevant_bboxes.append(item)
            if save_results:
                imutils.bb_with_text(img_arr,[xmin,ymin,(xmax-xmin),(ymax-ymin)],label)
        if save_results:
            marked_imgname = img_path.replace('.jpg','_bbs.jpg')
            print('bbs writtten to '+str(marked_imgname))
            cv2.imwrite(marked_imgname,img_arr)
        self.write_log(url,relevant_bboxes)
        print relevant_bboxes
        return relevant_bboxes
def convert_and_save_results(mask,
                             label_names,
                             pose,
                             filename,
                             img,
                             url,
                             forwebtool=True):
    '''
    This saves the mask using the labelling fashionista_categories_augmented_zero_based
    :param mask:
    :param label_names:
    :param pose:
    :param filename:
    :param img:
    :param url:
    :return:
     '''
    fashionista_ordered_categories = constants.fashionista_categories_augmented_zero_based  #constants.fashionista_categories
    h, w = mask.shape[0:2]
    new_mask = np.ones(
        (h, w, 3), dtype=np.uint8
    ) * 255  # anything left with 255 wasn't dealt with in the following conversion code
    print('new mask size:' + str(new_mask.shape))
    success = True  #assume innocence until proven guilty
    print('attempting convert and save, shapes:' + str(mask.shape) + ' new:' +
          str(new_mask.shape))
    for label in label_names:  # need these in order
        if label in fashionista_ordered_categories:
            fashionista_index = fashionista_ordered_categories.index(
                label) + 0  # number by  0=null, 55=skin  , not 1=null,56=skin
            pd_index = label_names[label]
            pixlevel_v2_index = constants.fashionista_aug_zerobased_to_pixlevel_categories_v2[
                fashionista_index]
            pixlevel_index = constants.fashionista_aug_zerobased_to_pixlevel_categories_v4_for_web[
                fashionista_index]
            if pixlevel_index is None:
                pixlevel_index = 0  #map unused categories (used in fashionista but not pixlevel v2)  to background


#            new_mask[mask==pd_index] = fashionista_index
#       print('old index '+str(pd_index)+' for '+str(label)+': gets new index:'+str(fashionista_index)+':' + fashionista_ordered_categories[fashionista_index]+ ' and newer index '+str(pixlevel_v2_index)+':'+constants.pixlevel_categories_v2[pixlevel_v2_index])
            new_mask[mask == pd_index] = pixlevel_index
        else:
            print('label ' + str(label) + ' not found in regular cats')
            success = False
    if 255 in new_mask:
        print('didnt fully convert mask')
        success = False
    if success:
        try:  #write orig file
            #            conversion_utils.count_values(new_mask,labels=constants.pixlevel_categories_v2)
            conversion_utils.count_values(
                new_mask, labels=constants.pixlevel_categories_v4_for_web)
            dir = constants.pd_output_savedir
            Utils.ensure_dir(dir)
            full_name = os.path.join(dir, filename)
            #            full_name = filename
            print('writing output img to ' + str(full_name))
            cv2.imwrite(full_name, img)
        except:
            print('fail in try 1, ' + sys.exc_info()[0])
        try:  #write rgb mask
            #            bmp_name = full_name.replace('.jpg','_pixv2.png')
            bmp_name = full_name.replace('.jpg', '_pixv4.png')
            print('writing mask bmp to ' + str(bmp_name))
            cv2.imwrite(bmp_name, new_mask)
            imutils.show_mask_with_labels(
                new_mask,
                labels=constants.pixlevel_categories_v4,
                original_image=full_name,
                save_images=True)
            if socket.gethostname() != 'extremeli-evolution-1':
                command_string = 'scp ' + bmp_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd/'
                subprocess.call(command_string, shell=True)
                command_string = 'scp ' + full_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd/'
                subprocess.call(command_string, shell=True)

        except:
            print('fail in try 2, ' + str(sys.exc_info()[0]))
        try:  #write webtool mask
            if forwebtool:
                webtool_mask = copy.copy(new_mask)
                webtool_mask[:, :,
                             0] = 0  #zero out the B,G for webtool - leave only R
                webtool_mask[:, :,
                             1] = 0  #zero out the B,G for webtool - leave only R
                #                bmp_name=full_name.replace('.jpg','_pixv2_webtool.png')
                bmp_name = full_name.replace('.jpg', '_pixv4_webtool.png')
                print('writing mask bmp to ' + str(bmp_name))
                cv2.imwrite(bmp_name, webtool_mask)
                command_string = 'scp ' + bmp_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd'
                subprocess.call(command_string, shell=True)
        except:
            print('fail in try 3, ' + str(sys.exc_info()[0]))
        try:
            pose_name = full_name.strip('.jpg') + '.pose'
            with open(pose_name, "w+") as outfile:
                print('succesful open, attempting to write pose')
                poselist = pose[0].tolist()
                #                json.dump([1,2,3], outfile, indent=4)
                json.dump(poselist, outfile, indent=4)
            if url is not None:
                url_name = full_name.strip('.jpg') + '.url'
                print('writing url to ' + str(url_name))
                with open(url_name, "w+") as outfile2:
                    print('succesful open, attempting to write:' + str(url))
                    outfile2.write(url)
        except:
            print('fail in convert_and_save_results dude, bummer')
            print(str(sys.exc_info()[0]))
        return
    else:
        print(
            'didnt fully convert mask, or unkown label in convert_and_save_results'
        )
        success = False
        return
Exemplo n.º 28
0
def flickr_dl(tag,avoid_these_terms=None,n_pages = 20000,start_page=1,savedir=None):
    '''
    https://www.flickr.com/services/api/flickr.photos.search.html  tags (Optional)
                A comma-delimited list of tags. Photos with one or more of the tags listed will be returned. You can exclude results that match a term by prepending it with a - character.
    :param tag:
    :param avoid_these_terms:
    :param n_pages:
    :param start_page:
    :param savedir:
    :return:
    '''
    results_per_page = 500
    max_pages_returned = int(float(4900)/results_per_page)
    compressed_tag = tag.replace(' ','+')
    if savedir is None:
        savedir = '/home/jeremy/image_dbs/flickr/'+compressed_tag+'/'
    Utils.ensure_dir(savedir)
    outfile = compressed_tag+'out.txt'
    n_dl = 0
    mintime = 1262307661  #jan 2010
    n_dates = n_pages/max_pages_returned
    for dateloop in range(n_dates):
        time.sleep(1)
#
        maxtime = flickr_get_dates(tag,mintime,savedir=savedir,n_pages=max_pages_returned)
        print('mintime '+str(mintime)+' maxtime:'+str(maxtime))

        for i in range(start_page,start_page+n_pages):
            query = '&tags='+tag+'&page='+str(i)+'&min_upload_date='+str(mintime)+'&max_upload_date='+str(maxtime)+'&per_page='+str(results_per_page)
            query = '&text='+compressed_tag+'&page='+str(i)+'&min_upload_date='+str(mintime)+'&max_upload_date='+str(maxtime)+'&per_page='+str(results_per_page)
            print query
            #kyle key 6351acc69daa0868c61319df617780c0   secret b7a74cf16401856b
            cmd = 'curl -X GET "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=d8548143cce923734f4093b4b063bc4f&format=json'+query+'" > ' + outfile
            print cmd
            res = subprocess.call(cmd,shell=True)
            print('res:'+str(type(res))+':'+str(res))
            with open(outfile,'r') as f:
                content=f.read()
                #the info is returned inside a function call like flickerApi({bla:bla...}) so I need to strip the beginning and end stuff
                stripped = content[14:-1]
                f.close()
            try:
                d = json.loads(stripped)
            except:
                print('json problem '+str(stripped))
                continue
            pprint(d)
            if not d:
                print('no file found')
                continue
            if not 'photos' in d:
                print('no photos field in result, continuing')
                continue
            phot = d['photos']
            if not 'photo' in phot:
                print('no photo field in result, continuing')
                continue
            if 'page' in phot:
                print('page '+str(phot['page']))
                page = phot['page']
            if 'pages' in phot:
                print('of total pages '+str(phot['pages']))
                pages = phot['pages']
            if page and pages and page>pages:
                print('beyond last page')
                return
            imgs = phot['photo']
            l = len(imgs)
    #        print imgs
            print(str(l)+' images found')
            skip_this = False

            for j in range(l):
 #               time.sleep(0.05)
                nth_img = imgs[j]
                if avoid_these_terms:
                    #go thru the entire dict and check if terms to avoid is in there somewhere
                    for k,v in nth_img.iteritems():
                        for item in avoid_these_terms:
    #                        print('item:'+item+' k,v:'+str(k)+':'+str(v)+' type:'+str(type(v)))
                            if v and (type(v) is str or type(v) is unicode) and item in v.lower():
                                skip_this = True
                                print('SKIPPING due to :'+str(k)+':'+str(v))
                                break
                        if skip_this:
                            break
             #       raw_input('ret to cont')
      #          print nth_img
                if skip_this:
                    continue
                if not 'farm' in nth_img:
                    print('no farm found, continuing')
                    continue
                farm = nth_img['farm']
                if not 'id' in nth_img:
                    print('no id found, continuing')
                    continue
                id = nth_img['id']
                if not 'server' in nth_img:
                    print('no server found, continuing')
                    continue
                server = nth_img['server']
                if not 'secret' in nth_img:
                    print('no secret found, continuing')
                    continue #
                secret = nth_img['secret']
                url = 'https://farm'+str(farm)+'.staticflickr.com/'+str(server)+'/'+str(id)+'_'+str(secret)+'.jpg'
      #          print url
                savename=str(id)+'.'+str(server)+'.'+str(farm)+'.jpg'
                savename = compressed_tag + savename
                savename = os.path.join(savedir,savename)
                Utils.ensure_dir(savedir)
                if os.path.exists(savename):
                    print(savename+' exists!!')
                    continue
#                    savename=savename[:-4]+'.b.jpg'
    #                raw_intput('check the flies')
                save_img_at_url(url,savename=savename)
                n_dl = n_dl + 1
            n_files = len(os.listdir(savedir))
            print('n dl:'+str(n_dl)+' n_files:'+str(n_files)+' '+savename)
        mintime = maxtime
Exemplo n.º 29
0
def getty_dl(searchphrase,avoid_these_terms=None,n_pages = 20000,savedir=None):
    if savedir is None:
        savedir = '/home/jeremy/image_dbs/getty/'+searchphrase+'/'
    Utils.ensure_dir(savedir)
    #do a first curl to set the page size
#    cmd = 'curl -X GET -H "Api-Key: r6zm5n78dguspxkg2ss4xvje"  https://api.gettyimages.com/v3/search/images?page_size=100000 > resout1.txt'
#    res = subprocess.call(cmd,shell=True)
    #next curl with the right phrase, all subsequent ones with ?page= to get next results from same query
    outfile = searchphrase+'out.txt'
    for i in range(n_pages):
        query = '?phrase='+searchphrase+'&page='+str(i+1)
        print query
        cmd = 'curl -X GET -H "Api-Key: r6zm5n78dguspxkg2ss4xvje"  "https://api.gettyimages.com/v3/search/images'+query+ '" > ' + outfile
        print cmd
        res = subprocess.call(cmd,shell=True)
        with open(outfile,'r') as f:
            d = json.load(f)
            f.close()
            pprint(d)
        if not d:
            print('no file found')
            continue
        if not 'images' in d:
            print('no images field in result, continuing')
            continue
        imgs = d['images']
        l = len(imgs)
#        print imgs
        print l
        skip_this = False
        for j in range(l):
            time.sleep(0.05)
            nth_img = imgs[j]
            if avoid_these_terms:
                skip_this = False
                #go thru the entire dict and check if terms to avoid is in there somewhere
                for k,v in nth_img.iteritems():
                    for item in avoid_these_terms:
#                        print('item:'+item+' k,v:'+str(k)+':'+str(v)+' type:'+str(type(v)))
                        if v and (type(v) is str or type(v) is unicode) and item in v.lower():
                            skip_this = True
                            print('SKIPPING due to :'+str(k)+':'+str(v))
                            break
                    if skip_this:
                        break
         #       raw_input('ret to cont')
  #          print nth_img
            if skip_this:
                continue
            if not 'display_sizes' in nth_img:
                print('no display sizes field found, continuing')
                continue
            ds = nth_img['display_sizes']
#            print ds
            first = ds[0]
 #           print first
            uri = first['uri']
#                print uri
            clean_url = uri.split('?')[0]
#                print(clean_url)
            savename=clean_url.split('?')[0]
            savename=savename.split('/')[-1]
            savename = searchphrase + savename
            savename = os.path.join(savedir,savename)
            Utils.ensure_dir(savedir)
#            print(savename)
            save_img_at_url(uri,savename=savename)
Exemplo n.º 30
0
    def detect_yolo_pyyolo(self, img_arr, url='',classes=constants.hls_yolo_categories,save_results=True):
#                item = {'object':label,'bbox':[xmin,ymin,xmax,ymax],'confidence':'>'+str(thresh)}
        print('started pyyolo detect, thresh='+str(thresh)+' hier '+str(hier_thresh))
        save_path = '/data/jeremy/pyyolo/results/'
        if img_arr is None:
            print('got None img array!!')
            None
        if len(img_arr.shape) == 2: #got 1-chan(gray) image
            print('got gray img')
            img_arr_bgr=np.zeros([img_arr.shape[0],img_arr.shape[1],3])
            img_arr_bgr=img_arr
            print('sizes: {} {}'.format(img_arr_bgr,img_arr))
            img_arr = img_arr_bgr
        print('img arr size {}'.format(img_arr.shape))
        max_image_size=1200 #1280 fails
        resized=False
        if img_arr.shape[0]>max_image_size or img_arr.shape[1]>max_image_size:  #maybe this is causing prob at http://a.abcnews.com/images/US/150815_yt_phillypd_33x16_1600.jpg
            maxside=max(img_arr.shape)
            reduction_factor = maxside/1000.0 #force maxside to 1000
            original_size=img_arr.shape
            resized=True
            img_arr = cv2.resize(img_arr,(int(img_arr.shape[1]/reduction_factor),int(img_arr.shape[0]/reduction_factor)))
            print('reduced size to {}'.format(img_arr.shape))
            #generate randonm filename
        hash = hashlib.sha1()
        hash.update(str(time.time()))
        if save_results:
            timestr = time.strftime("%Y%m%d.%H%M%S")
            img_filename = timestr+hash.hexdigest()[:5]+'.jpg'
            Utils.ensure_dir(save_path)
            img_path = os.path.join(save_path,img_filename)
            print('detect_yolo_pyyolo saving file '+str(img_path))
            try:
                cv2.imwrite(img_path,img_arr)
            except:
                print('some trouble saving image,'+str(sys.exc_info()[0]))
        relevant_items = []
        print('getting results from get_pyyolo_results')
        yolo_results = self.get_pyyolo_results(img_arr)
        print('got results from get_pyyolo_results')

        for item in yolo_results:
            print(item)
            if resized:
                img_arr = cv2.resize(img_arr,(original_size[1],original_size[0]))
                item['bbox'][0]=int(item['bbox'][0]*reduction_factor)
                item['bbox'][1]=int(item['bbox'][1]*reduction_factor)
                item['bbox'][2]=int(item['bbox'][2]*reduction_factor)
                item['bbox'][3]=int(item['bbox'][3]*reduction_factor)
                print('fixed size back to original {}'.format(item))

            xmin=item['bbox'][0]
            ymin=item['bbox'][1]
            xmax=item['bbox'][2]
            ymax=item['bbox'][3]
            assert xmin<xmax,'xmin not < xmax!!!'
            assert ymin<ymax,'xmin not < xmax!!!'
##### TAKING OUT RELEVANT ITEMS ON ROYS SUGGESTION
            use_hydra=False
            if use_hydra:
                if item['object'] == 'person':
                    cropped_image = img_arr[ymin:ymax, xmin:xmax]
                    # print('crop:{} {}'.format(item["bbox"],cropped_image.shape))
                    # get hydra results
                    try:
                        hydra_output = self.get_hydra_output(cropped_image)
                        if hydra_output:
                            item['details'] = hydra_output
                    except:
                        print "Hydra call from pyyolo defense falcon failed " + traceback.format_exc()
            relevant_items.append(item)
            if save_results:
                imutils.bb_with_text(img_arr,[xmin,ymin,(xmax-xmin),(ymax-ymin)],item['object'])
        if save_results:
            marked_imgname = img_path.replace('.jpg','_bb_yolos.jpg')
            print('pyyolo bbs being writtten to '+str(marked_imgname))
            try:
                pass  #no real need to save bb file as no extra info there
              #  r=cv2.imwrite(marked_imgname,img_arr)
              #skip saving bb image to conserve space
              #  print('write result '+str(r))
            except:
                print('some trouble saving bb image,'+str(sys.exc_info()[0]))
            txtname=img_path.replace('.jpg','.txt')
            self.write_log(url,relevant_items,filename=txtname)

        print('detect yolo returning:'+str(relevant_items))
        return relevant_items