예제 #1
0
파일: tests.py 프로젝트: maheenRashid/caffe
def testSubsetFiles(meta_val_file,meta_synset_words_file,sub_val_file,sub_synset_words_file,excluded_ids_all_file,removePath):

    meta_list_files_val,meta_idx_val=zip(*imagenet.readLabelsFile(meta_val_file));
    meta_val_idx_mapped=[int(idx_curr) for idx_curr in meta_idx_val];
    
    meta_val_idx_mapped=np.array(meta_val_idx_mapped);

    t=time.time();
    meta_ids,meta_labels=zip(*imagenet.readLabelsFile(meta_synset_words_file));
    meta_ids=np.array(meta_ids);
    meta_val_ids_mapped=np.empty((len(meta_list_files_val),),dtype='object');
    
    for idx_curr in np.unique(meta_val_idx_mapped):
        idx_rel=meta_val_idx_mapped==idx_curr;
        meta_val_ids_mapped[idx_rel]=meta_ids[idx_curr];
    meta_val_ids_mapped=list(meta_val_ids_mapped);
    print time.time()-t

    sub_list_files_val,sub_idx_val=zip(*imagenet.readLabelsFile(sub_val_file));
    sub_val_idx_mapped=[int(idx_curr) for idx_curr in sub_idx_val];

    excluded_ids_all=imagenet.readSynsetsFile(excluded_ids_all_file);
    #all excluded files are excluded
    check_exclude=testFilesExcludedProperly(sub_list_files_val,meta_list_files_val,meta_val_ids_mapped,excluded_ids_all)
    #all included files are included
    check_include=testFilesIncludedProperly(sub_list_files_val,meta_list_files_val,meta_val_ids_mapped,excluded_ids_all)
    #all indices labeling is consistent
    check_labeling=testFilesLabeledProperly(sub_val_idx_mapped,sub_list_files_val,meta_val_idx_mapped,meta_list_files_val)
    return check_exclude,check_include,check_labeling;
예제 #2
0
def script_pascalClasses_get():
    path_to_file = "../../data/ilsvrc12/synset_words.txt"
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])

    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = "/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt"
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])
    pascal_labels = list(zip(*pascal_ids)[1])
    pascal_labels = [id_curr.strip(" ") for id_curr in pascal_labels]
    pascal_labels[pascal_labels.index("dining_table")] = "diningtable"
    pascal_labels[pascal_labels.index("tv/monitor")] = "tvmonitor"

    pascal3d_ids = [
        "boat",
        "train",
        "bicycle",
        "chair",
        "motorbike",
        "aeroplane",
        "sofa",
        "diningtable",
        "bottle",
        "tvmonitor",
        "bus",
        "car",
    ]
    idx_mapping = []
    for id_curr in pascal3d_ids:
        idx_mapping.append(pascal_labels.index(id_curr))

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids, pascal_just_ids, False)
    return to_exclude
예제 #3
0
def script_writeHTMLS():
    path_to_file = '../../data/ilsvrc12/synset_words.txt'
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])
    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])

    val_gt_file = '../../data/ilsvrc12/val.txt'
    list_of_ids_im = [id for id_list in to_exclude for id in id_list]
    mapping_file = '../../data/ilsvrc12/synsets.txt'

    list_of_ids = imagenet.getImagenetIdToTrainingIdMapping(
        mapping_file, list_of_ids_im)
    print len(list_of_ids)

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids,
                                                   pascal_just_ids,
                                                   keepMapping=True)
    list_of_ids_pascal = []

    for id_no in range(len(to_exclude)):
        list_of_ids_pascal = list_of_ids_pascal + [id_no] * len(
            to_exclude[id_no])

    file_text_labels = '../../data/ilsvrc12/synset_words.txt'
    text_labels = np.loadtxt(file_text_labels, str, delimiter='\t')

    trained_file = '/disk2/octoberExperiments/nn_performance_without_pascal/trained/20151023153522'
    notrained_file = '/disk2/octoberExperiments/nn_performance_without_pascal/notrained/20151026132705'

    file_pres = [trained_file, notrained_file]
    layers = ['pool5', 'fc6', 'fc7']
    file_names = [
        file_pre + '_' + layer for file_pre in file_pres for layer in layers
    ]

    for file_name in file_names:
        [img_paths, gt_labels, indices,
         _] = pickle.load(open(file_name + '.p', 'rb'))
        gt_labels_pascal = [
            list_of_ids_pascal[list_of_ids.index(gt_label)]
            for gt_label in gt_labels
        ]

        file_name_l = file_name + '_' + layer + '_pascal'
예제 #4
0
def preprocessImagesFromImagenet(imagenet_ids_to_test,
                                 synset_words,
                                 val_gt_file,
                                 path_to_images_imagenet='',
                                 all_files_info=None,
                                 start_idx=1):
    # class_ids=[];
    ids, labels = zip(*imagenet.readLabelsFile(synset_words))
    list_of_idx = [ids.index(id_curr) for id_curr in imagenet_ids_to_test]
    test_set = imagenet.selectTestSetByID(val_gt_file,
                                          list_of_idx,
                                          path_to_val=path_to_images_imagenet)

    if all_files_info is None:
        all_files_info = []

    for img_path, imagenet_idx in test_set:
        dict_curr = {}
        dict_curr['img_path'] = img_path
        id_curr = ids[imagenet_idx]
        dict_curr['class_idx'] = imagenet_ids_to_test.index(
            id_curr) + start_idx
        dict_curr['class_id'] = id_curr
        dict_curr['azimuth'] = None
        all_files_info.append(dict_curr)

    return all_files_info
def main():

	synset_words='/disk2/octoberExperiments/nn_performance_without_pascal/synset_words.txt'
	pascal_classes=['person','bird','cat','cow','dog','horse','sheep','aeroplane','bicycle','boat','bus','car','motorbike','train','bottle','chair','dining_table','potted_plant','sofa','tv/monitor','none']
	out_dir='/disk2/novemberExperiments/network_no_pascal'
	out_file_to_exclude_text=os.path.join(out_dir,'to_exclude.txt');

	print len(pascal_classes);
	ids_labels=imagenet.readLabelsFile(synset_words);
	print len(ids_labels);
	out_file=os.path.join(out_dir,'mappingImagenetToPascal.p');
	mappingDict=pickle.load(open(out_file,'rb'));
	writeExcludedIdsFile(mappingDict,out_file_to_exclude_text)

	
	# print sum(vals=='none'),len(vals)

	return
	out_file=os.path.join(out_dir,'mappingImagenetToPascal_temp.p');
	out_file_text=os.path.join(out_dir,'excluded_classes_info_temp.txt');

	imagenet_labels=[x[0]+'\n'+x[1] for x in ids_labels];
	sample_images=[os.path.join(out_dir,'sample_images/'+x[0]+'.JPEG') for x in ids_labels]

	imagenet_labels=imagenet_labels[:10];
	sample_images=sample_images[:10];

	window=Window(pascal_classes,imagenet_labels,sample_images,out_file=out_file);

	mappingDict=pickle.load(open(out_file,'rb'));
	writeExcludedClassInfoFile(mappingDict,out_file_text)
	print out_file_text
예제 #6
0
def script_setUpPascalExcludedTextFiles():
    path_to_file = '../../data/ilsvrc12/synset_words.txt'
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])
    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids,
                                                   pascal_just_ids)

    im_list_file = '../../data/ilsvrc12/val.txt'
    mapping_file = '../../data/ilsvrc12/synsets.txt'

    ims_to_keep, class_ids_to_keep, classes_to_keep = imagenet.removeImagesFromListByClass(
        im_list_file, mapping_file, to_exclude)

    new_file_val = '/disk2/octoberExperiments/nn_performance_without_pascal/val.txt'
    classes_uni_val = writeNewDataClassFile(new_file_val,
                                            zip(ims_to_keep, classes_to_keep))

    im_list_file = '../../data/ilsvrc12/train.txt'
    ims_to_keep, class_ids_to_keep, classes_to_keep = imagenet.removeImagesFromListByClass(
        im_list_file, mapping_file, to_exclude)

    new_file_val = '/disk2/octoberExperiments/nn_performance_without_pascal/train.txt'
    classes_uni_train = imagenet.writeNewDataClassFile(
        new_file_val, zip(ims_to_keep, classes_to_keep))

    assert (str(classes_uni_val) == str(classes_uni_train))

    class_file = '/disk2/octoberExperiments/nn_performance_without_pascal/synsets.txt'

    with open(class_file, 'wb') as f:
        for class_id in classes_uni_train:
            f.write(class_id + '\n')

    with open(new_file_val, 'rb') as f:
        content = f.read()

    #sanity check
    for id_to_exclude in to_exclude:
        if id_to_exclude in content:
            print 'FOUND ERROR', id_to_exclude
예제 #7
0
def testSubsetFiles(meta_val_file, meta_synset_words_file, sub_val_file,
                    sub_synset_words_file, excluded_ids_all_file, removePath):

    meta_list_files_val, meta_idx_val = zip(
        *imagenet.readLabelsFile(meta_val_file))
    meta_val_idx_mapped = [int(idx_curr) for idx_curr in meta_idx_val]

    meta_val_idx_mapped = np.array(meta_val_idx_mapped)

    t = time.time()
    meta_ids, meta_labels = zip(
        *imagenet.readLabelsFile(meta_synset_words_file))
    meta_ids = np.array(meta_ids)
    meta_val_ids_mapped = np.empty((len(meta_list_files_val), ),
                                   dtype='object')

    for idx_curr in np.unique(meta_val_idx_mapped):
        idx_rel = meta_val_idx_mapped == idx_curr
        meta_val_ids_mapped[idx_rel] = meta_ids[idx_curr]
    meta_val_ids_mapped = list(meta_val_ids_mapped)
    print time.time() - t

    sub_list_files_val, sub_idx_val = zip(
        *imagenet.readLabelsFile(sub_val_file))
    sub_val_idx_mapped = [int(idx_curr) for idx_curr in sub_idx_val]

    excluded_ids_all = imagenet.readSynsetsFile(excluded_ids_all_file)
    #all excluded files are excluded
    check_exclude = testFilesExcludedProperly(sub_list_files_val,
                                              meta_list_files_val,
                                              meta_val_ids_mapped,
                                              excluded_ids_all)
    #all included files are included
    check_include = testFilesIncludedProperly(sub_list_files_val,
                                              meta_list_files_val,
                                              meta_val_ids_mapped,
                                              excluded_ids_all)
    #all indices labeling is consistent
    check_labeling = testFilesLabeledProperly(sub_val_idx_mapped,
                                              sub_list_files_val,
                                              meta_val_idx_mapped,
                                              meta_list_files_val)
    return check_exclude, check_include, check_labeling
예제 #8
0
def script_printExcludedInfoFile():
    path_to_file = '../../data/ilsvrc12/synset_words.txt'
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])

    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids,
                                                   pascal_just_ids,
                                                   keepMapping=True)

    f_info = '/disk2/octoberExperiments/nn_performance_without_pascal/excluded_classes_info.txt'
    with open(f_info, 'wb') as f:
        for idx, to_exclude_curr in enumerate(to_exclude):
            f.write(str(pascal_ids[idx]) + '\n')
            for to_exclude_id in to_exclude_curr:
                f.write(to_exclude_id + ' ' +
                        val_just_labels[val_just_ids.index(to_exclude_id)] +
                        '\n')
            f.write('___' + '\n')
예제 #9
0
def script_setUpFilesForTrainingOnSubset(params):
    parent_synset_words_file = params.parent_synset_words_file
    parent_val_file = params.parent_val_file
    parent_train_file = params.parent_train_file
    parent_synset_file = params.parent_synset_file
    new_val_file = params.new_val_file
    new_train_file = params.new_train_file
    new_synset_file = params.new_synset_file
    new_synset_words_file = params.new_synset_words_file
    to_exclude_text_file = params.to_exclude_text_file

    val_ids = imagenet.readLabelsFile(parent_synset_words_file)
    val_just_ids = list(zip(*val_ids)[0])
    val_just_labels = list(zip(*val_ids)[1])

    with open(to_exclude_text_file, "rb") as f:
        to_exclude = f.readlines()
        to_exclude = [to_exclude_curr.strip("\n") for to_exclude_curr in to_exclude]

    ims_to_keep, class_ids_to_keep, classes_to_keep = imagenet.removeImagesFromListByClass(
        parent_val_file, parent_synset_file, to_exclude
    )
    classes_uni_val = imagenet.writeNewDataClassFile(new_val_file, zip(ims_to_keep, classes_to_keep))

    ims_to_keep, class_ids_to_keep, classes_to_keep = imagenet.removeImagesFromListByClass(
        parent_train_file, parent_synset_file, to_exclude
    )
    classes_uni_train = imagenet.writeNewDataClassFile(new_train_file, zip(ims_to_keep, classes_to_keep))

    assert str(classes_uni_val) == str(classes_uni_train)

    with open(new_synset_file, "wb") as f:
        for class_id in classes_uni_train:
            f.write(class_id + "\n")

    with open(new_synset_words_file, "wb") as f:
        for class_id in classes_uni_train:
            f.write(class_id + " " + val_just_labels[val_just_ids.index(class_id)] + "\n")

    with open(new_synset_file, "rb") as f:
        content = f.read()

    # sanity check
    for id_to_exclude in to_exclude:
        if id_to_exclude in content:
            print "FOUND ERROR", id_to_exclude
예제 #10
0
def preprocessImagesFromImagenet(imagenet_ids_to_test,synset_words,val_gt_file,path_to_images_imagenet='',all_files_info=None,start_idx=1):
    # class_ids=[];
    ids,labels=zip(*imagenet.readLabelsFile(synset_words));
    list_of_idx=[ids.index(id_curr) for id_curr in imagenet_ids_to_test];
    test_set=imagenet.selectTestSetByID(val_gt_file,list_of_idx,path_to_val=path_to_images_imagenet)
    
    if all_files_info is None:
        all_files_info=[];

    for img_path,imagenet_idx in test_set:
        dict_curr={}
        dict_curr['img_path']=img_path;
        id_curr=ids[imagenet_idx];
        dict_curr['class_idx']=imagenet_ids_to_test.index(id_curr)+start_idx;
        dict_curr['class_id']=id_curr;
        dict_curr['azimuth']=None;
        all_files_info.append(dict_curr);

    return all_files_info
예제 #11
0
def script_runNNOnPascalExcludedInTraining():
    path_to_file = '../../data/ilsvrc12/synset_words.txt'
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])
    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids,
                                                   pascal_just_ids,
                                                   keepMapping=True)

    val_gt_file = '../../data/ilsvrc12/val.txt'
    list_of_ids_im = [id for id_list in to_exclude for id in id_list]
    mapping_file = '../../data/ilsvrc12/synsets.txt'
    print len(list_of_ids_im)

    list_of_ids, _ = imagenet.getImagenetIdToTrainingIdMapping(
        mapping_file, list_of_ids_im)
    print len(list_of_ids)
    # print list_of_ids[0]
    list_of_ids_pascal = []

    for id_no in range(len(to_exclude)):
        list_of_ids_pascal = list_of_ids_pascal + [id_no] * len(
            to_exclude[id_no])

    path_to_val = '/disk2/imagenet/val'
    test_set = imagenet.selectTestSetByID(val_gt_file, list_of_ids,
                                          path_to_val)

    # out_dir='/disk2/octoberExperiments/nn_performance_without_pascal/notrained'
    out_dir = '/disk2/novemberExperiments/nn_imagenet_top5/trained'
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    layers = ['pool5', 'fc6', 'fc7']
    gpu_no = 1
    path_to_classify = '..'
    numberOfN = 5
    relativePaths = ['/disk2', '../../../..']

    # deployFile='/disk2/octoberExperiments/nn_performance_without_pascal/deploy.prototxt'
    # meanFile='/disk2/octoberExperiments/nn_performance_without_pascal/mean.npy'
    # modelFile='/disk2/octoberExperiments/nn_performance_without_pascal/snapshot_iter_450000.caffemodel'

    modelFile = '/home/maheenrashid/Downloads/caffe/caffe-rc2/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
    deployFile = '/home/maheenrashid/Downloads/caffe/caffe-rc2/models/bvlc_reference_caffenet/deploy.prototxt'
    meanFile = '/home/maheenrashid/Downloads/caffe/caffe-rc2/python/caffe/imagenet/ilsvrc_2012_mean.npy'

    # modelFile='/disk2/novemberExperiments/network_no_pascal/snapshots/snapshot_iter_450000.caffemodel';
    # deployFile='/disk2/novemberExperiments/network_no_pascal/deploy.prototxt';
    # meanFile='/disk2/novemberExperiments/network_no_pascal/mean.npy';

    # out_file=script_nearestNeigbourExperiment.runClassificationTestSet(test_set,out_dir,path_to_classify,gpu_no,layers,deployFile=deployFile,meanFile=meanFile,modelFile=modelFile)

    # return
    # file_name='/disk2/octoberExperiments/nn_performance_without_pascal/notrained/20151026132705'
    # file_name='/disk2/novemberExperiments/nn_imagenet_top5/notrained/20151130193757';
    file_name = '/disk2/novemberExperiments/nn_imagenet_top5/trained/20151130230243'
    file_text_labels = '../../data/ilsvrc12/synset_words.txt'

    text_labels = np.loadtxt(file_text_labels, str, delimiter='\t')

    vals = np.load(file_name + '.npz')

    test_set = sorted(test_set, key=lambda x: x[0])
    test_set = zip(*test_set)
    img_paths = list(test_set[0])
    gt_labels = list(test_set[1])
    gt_labels_pascal = [
        list_of_ids_pascal[list_of_ids.index(gt_label)]
        for gt_label in gt_labels
    ]

    for layer in layers:
        print layer
        file_name_l = file_name + '_' + layer
        indices = script_nearestNeigbourExperiment.doNN(img_paths,
                                                        gt_labels,
                                                        vals[layer],
                                                        numberOfN=numberOfN,
                                                        distance='cosine',
                                                        algo='brute')
        conf_matrix = 0
        pickle.dump([img_paths, gt_labels, indices, conf_matrix],
                    open(file_name_l + '.p', 'wb'))

    file_text_labels_pascal = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    text_labels_pascal = np.loadtxt(file_text_labels_pascal,
                                    str,
                                    delimiter='\t')

    for layer in layers:
        print layer
        file_name_l = file_name + '_' + layer
        [img_paths, gt_labels, indices,
         _] = pickle.load(open(file_name_l + '.p', 'rb'))
        img_paths_curr = [
            x.replace(relativePaths[0], relativePaths[1]) for x in img_paths
        ]
        im_paths, captions = script_nearestNeigbourExperiment.createImageAndCaptionGrid(
            img_paths_curr, gt_labels, indices, text_labels)
        script_nearestNeigbourExperiment.writeHTML(file_name_l + '.html',
                                                   im_paths, captions)
        no_correct, _ = script_nearestNeigbourExperiment.getNumberOfCorrectNNMatches(
            indices, gt_labels)
        print no_correct
        with open(file_name_l + '.txt', 'wb') as f:
            for no_correct_curr in no_correct:
                f.write(str(no_correct_curr) + ' ')

        file_name_l = file_name + '_' + layer + '_pascal'
        im_paths, captions = script_nearestNeigbourExperiment.createImageAndCaptionGrid(
            img_paths_curr, gt_labels_pascal, indices, text_labels_pascal)
        script_nearestNeigbourExperiment.writeHTML(file_name_l + '.html',
                                                   im_paths, captions)
        no_correct, _ = script_nearestNeigbourExperiment.getNumberOfCorrectNNMatches(
            indices, gt_labels_pascal)
        with open(file_name_l + '.txt', 'wb') as f:
            for no_correct_curr in no_correct:
                f.write(str(no_correct_curr) + ' ')

        print no_correct
예제 #12
0
def script_runNNOnPascalIncludedInTraining():
    path_to_file = '../../data/ilsvrc12/synset_words.txt'
    val_ids = imagenet.readLabelsFile(path_to_file)
    val_just_ids = list(zip(*val_ids)[0])
    val_just_labels = list(zip(*val_ids)[1])

    pascal_ids_file = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    pascal_ids = imagenet.readLabelsFile(pascal_ids_file)
    pascal_just_ids = list(zip(*pascal_ids)[0])

    to_exclude = imagenet.removeClassesWithOverlap(val_just_ids,
                                                   pascal_just_ids,
                                                   keepMapping=True)

    val_gt_file = '../../data/ilsvrc12/val.txt'
    list_of_ids_im = [id for id_list in to_exclude for id in id_list]
    mapping_file = '../../data/ilsvrc12/synsets.txt'

    list_of_ids = imagenet.getImagenetIdToTrainingIdMapping(
        mapping_file, list_of_ids_im)
    print len(list_of_ids)

    list_of_ids_pascal = []

    for id_no in range(len(to_exclude)):
        list_of_ids_pascal = list_of_ids_pascal + [id_no] * len(
            to_exclude[id_no])

    path_to_val = '/disk2/imagenet/val'
    test_set = imagenet.selectTestSetByID(val_gt_file, list_of_ids,
                                          path_to_val)

    out_dir = '/disk2/octoberExperiments/nn_performance_without_pascal/trained'
    layers = ['pool5', 'fc6', 'fc7']
    gpu_no = 0
    path_to_classify = '..'
    numberOfN = 5
    relativePaths = ['/disk2', '../../../..']
    # out_file=script_nearestNeigbourExperiment.runClassificationTestSet(test_set,out_dir,path_to_classify,gpu_no,layers)

    file_name = '/disk2/octoberExperiments/nn_performance_without_pascal/trained/20151023153522'

    file_text_labels = '../../data/ilsvrc12/synset_words.txt'

    text_labels = np.loadtxt(file_text_labels, str, delimiter='\t')

    vals = np.load(file_name + '.npz')

    test_set = sorted(test_set, key=lambda x: x[0])
    test_set = zip(*test_set)
    img_paths = list(test_set[0])
    gt_labels = list(test_set[1])
    gt_labels_pascal = [
        list_of_ids_pascal[list_of_ids.index(gt_label)]
        for gt_label in gt_labels
    ]

    # for layer in layers:
    #     file_name_l=file_name+'_'+layer;
    #     indices,conf_matrix=doNN(img_paths,gt_labels,vals[layer],numberOfN=numberOfN,distance='cosine',algo='brute')
    #     pickle.dump([img_paths,gt_labels,indices,conf_matrix],open(file_name_l+'.p','wb'));

    file_text_labels_pascal = '/disk2/octoberExperiments/nn_performance_without_pascal/pascal_classes.txt'
    text_labels_pascal = np.loadtxt(file_text_labels_pascal,
                                    str,
                                    delimiter='\t')

    for layer in layers:
        print layer
        file_name_l = file_name + '_' + layer
        [img_paths, gt_labels, indices,
         _] = pickle.load(open(file_name_l + '.p', 'rb'))
        img_paths_curr = [
            x.replace(relativePaths[0], relativePaths[1]) for x in img_paths
        ]
        im_paths, captions = script_nearestNeigbourExperiment.createImageAndCaptionGrid(
            img_paths_curr, gt_labels, indices, text_labels)
        script_nearestNeigbourExperiment.writeHTML(file_name_l + '.html',
                                                   im_paths, captions)
        no_correct, _ = script_nearestNeigbourExperiment.getNumberOfCorrectNNMatches(
            indices, gt_labels)
        print no_correct
        with open(file_name_l + '.txt', 'wb') as f:
            for no_correct_curr in no_correct:
                f.write(str(no_correct_curr) + ' ')

        file_name_l = file_name + '_' + layer + '_pascal'
        im_paths, captions = script_nearestNeigbourExperiment.createImageAndCaptionGrid(
            img_paths_curr, gt_labels_pascal, indices, text_labels_pascal)
        script_nearestNeigbourExperiment.writeHTML(file_name_l + '.html',
                                                   im_paths, captions)
        no_correct, _ = script_nearestNeigbourExperiment.getNumberOfCorrectNNMatches(
            indices, gt_labels_pascal)
        with open(file_name_l + '.txt', 'wb') as f:
            for no_correct_curr in no_correct:
                f.write(str(no_correct_curr) + ' ')

        print no_correct
예제 #13
0
def main():

    # in_file='/disk2/temp/small_set.txt';
    path_to_val = "/disk2/imagenet/val"

    # val_gt_file='../../data/ilsvrc12/val.txt'
    # synset_words='../../data/ilsvrc12/synset_words.txt'
    # deploy_file='../../models/bvlc_reference_caffenet/deploy.prototxt';
    # model_file='../../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
    # mean_file='../../python/caffe/imagenet/ilsvrc_2012_mean.npy';
    # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/val_performance_top5_trained.p';

    val_gt_file = "/disk2/octoberExperiments/nn_performance_without_pascal/val.txt"
    synset_words = "/disk2/octoberExperiments/nn_performance_without_pascal/synset_words.txt"
    deploy_file = "/disk2/octoberExperiments/nn_performance_without_pascal/deploy.prototxt"
    model_file = "/disk2/octoberExperiments/nn_performance_without_pascal/snapshot_iter_450000.caffemodel"
    mean_file = "/disk2/octoberExperiments/nn_performance_without_pascal/mean.npy"
    # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/new_val_performance_top5_no_trained.p';

    val_gt = imagenet.readLabelsFile(val_gt_file)
    im_files = list(zip(*val_gt)[0])
    im_files = [os.path.join(path_to_val, im_file) for im_file in im_files]
    im_files = im_files[:200]
    imagenet_idx_mapped = list(zip(*val_gt)[1])
    imagenet_idx_mapped = [int(x) for x in imagenet_idx_mapped]

    batch_size = 1000
    top_n = 5
    gpu_no = 1
    getClasses = True
    net, transformer = setUpNet(model_file, deploy_file, mean_file, gpu_no)
    out_file = "temp"
    in_file_text = "temp.txt"

    with open(in_file_text, "wb") as f:
        for im_file in im_files:
            f.write(im_file + "\n")

    layers = ["fc7"]

    saveLayers(net, transformer, in_file_text, layers, out_file)
    vals = np.load(out_file + ".npz")
    print vals.keys()
    for k in vals.keys():
        print k, vals[k].shape

    # error_bin,pred_classes=getTopNError(net,transformer,im_files,imagenet_idx_mapped,batch_size,top_n,printDebug=True,pred_classes=True)
    # print sum(error_bin),len(error_bin)
    # print pred_classes[:10]

    # pickle.dump([error_bin,pred_classes],open(out_file_topk,'wb'));

    return

    db_path = "sqlite://///disk2/novemberExperiments/nn_imagenet/nn_imagenet.db"

    all_pascal_id = [
        "boat",
        "train",
        "bicycle",
        "chair",
        "motorbike",
        "aeroplane",
        "sofa",
        "diningtable",
        "bottle",
        "tvmonitor",
        "bus",
        "car",
    ]

    batch_size = 1000
    top_n = 1
    gpu_no = 1
    getClasses = True

    ids_labels = imagenet.readLabelsFile(synset_words)
    labels = list(zip(*ids_labels)[1])
    imagenet_ids = list(zip(*ids_labels)[0])

    net, transformer = setUpNet(model_file, deploy_file, mean_file, gpu_no)

    for pascal_id in ["car"]:
        # all_pascal_id:
        print pascal_id
        out_file_topk = "/disk2/novemberExperiments/nn_imagenet/" + pascal_id + "_pred_no_train.p"
        out_file_topk_txt = "/disk2/novemberExperiments/nn_imagenet/" + pascal_id + "_pred_no_train.txt"

        mani = Imagenet_Manipulator(db_path)
        mani.openSession()
        criterion = (Imagenet.class_id_pascal == pascal_id,)
        vals = mani.select((Imagenet.img_path,), criterion, distinct=True)

        mani.closeSession()
        im_files = [val[0] for val in vals]
        imagenet_idx_mapped = [772] * len(im_files)
        print len(im_files)

        # return
        # error_bin=pickle.load(open(out_file_topk,'rb'));
        # print sum(error_bin),len(error_bin),sum(error_bin)/float(len(error_bin))

        # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/val_performance_top5_trained.p';
        # error_bin=pickle.load(open(out_file_topk,'rb'));
        # print sum(error_bin),len(error_bin),sum(error_bin)/float(len(error_bin))

        # return

        # val_gt=imagenet.readLabelsFile(val_gt_file);
        # im_files=list(zip(*val_gt)[0]);
        # im_files=[os.path.join(path_to_val,im_file) for im_file in im_files]
        # imagenet_idx_mapped=list(zip(*val_gt)[1])
        # imagenet_idx_mapped=[int(x) for x in imagenet_idx_mapped];

        # print idx_mapped_simple[:10],len(idx_mapped_simple);
        # print 'getting mapping'

        # imagenet_idx_mapped,imagenet_ids_mapped,imagenet_labels_mapped=imagenet.getMappingInfo(im_files,synset_words,val_gt_file)
        # print imagenet_idx_mapped[:10],len(imagenet_idx_mapped),type(imagenet_idx_mapped),type(imagenet_idx_mapped[0]),type(idx_mapped_simple),type(idx_mapped_simple[0])

        # for idx_idx,idx in enumerate(idx_mapped_simple):
        #   print idx,imagenet_idx_mapped[idx_idx]
        #   assert idx==imagenet_idx_mapped[idx_idx]
        # return

        error_bin, pred_classes = getTopNError(
            net, transformer, im_files, imagenet_idx_mapped, batch_size, top_n, printDebug=True, pred_classes=True
        )
        print sum(error_bin), len(error_bin)
        print pred_classes[:10]

        pickle.dump([error_bin, pred_classes], open(out_file_topk, "wb"))

        pred_classes = pred_classes.ravel()
        tuples = []
        for class_curr in np.unique(pred_classes):
            # print imagenet_ids[class_curr],labels[class_curr];
            tuples.append((class_curr, labels[class_curr], sum(pred_classes == class_curr) / float(len(pred_classes))))

        tuples = sorted(tuples, key=lambda x: x[2])[::-1]
        with open(out_file_topk_txt, "wb") as f:
            for tuple_curr in tuples:
                print imagenet_ids[tuple_curr[0]], tuple_curr
                f.write(str(tuple_curr[0]) + "\t" + str(tuple_curr[1]) + "\t" + str(round(tuple_curr[2] * 100, 2)))
                f.write("\n")
예제 #14
0
def main():

    # in_file='/disk2/temp/small_set.txt';
    path_to_val = '/disk2/imagenet/val'

    # val_gt_file='../../data/ilsvrc12/val.txt'
    # synset_words='../../data/ilsvrc12/synset_words.txt'
    # deploy_file='../../models/bvlc_reference_caffenet/deploy.prototxt';
    # model_file='../../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
    # mean_file='../../python/caffe/imagenet/ilsvrc_2012_mean.npy';
    # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/val_performance_top5_trained.p';

    val_gt_file = '/disk2/octoberExperiments/nn_performance_without_pascal/val.txt'
    synset_words = '/disk2/octoberExperiments/nn_performance_without_pascal/synset_words.txt'
    deploy_file = '/disk2/octoberExperiments/nn_performance_without_pascal/deploy.prototxt'
    model_file = '/disk2/octoberExperiments/nn_performance_without_pascal/snapshot_iter_450000.caffemodel'
    mean_file = '/disk2/octoberExperiments/nn_performance_without_pascal/mean.npy'
    # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/new_val_performance_top5_no_trained.p';

    val_gt = imagenet.readLabelsFile(val_gt_file)
    im_files = list(zip(*val_gt)[0])
    im_files = [os.path.join(path_to_val, im_file) for im_file in im_files]
    im_files = im_files[:200]
    imagenet_idx_mapped = list(zip(*val_gt)[1])
    imagenet_idx_mapped = [int(x) for x in imagenet_idx_mapped]

    batch_size = 1000
    top_n = 5
    gpu_no = 1
    getClasses = True
    net, transformer = setUpNet(model_file, deploy_file, mean_file, gpu_no)
    out_file = 'temp'
    in_file_text = 'temp.txt'

    with open(in_file_text, 'wb') as f:
        for im_file in im_files:
            f.write(im_file + '\n')

    layers = ['fc7']

    saveLayers(net, transformer, in_file_text, layers, out_file)
    vals = np.load(out_file + '.npz')
    print vals.keys()
    for k in vals.keys():
        print k, vals[k].shape

    # error_bin,pred_classes=getTopNError(net,transformer,im_files,imagenet_idx_mapped,batch_size,top_n,printDebug=True,pred_classes=True)
    # print sum(error_bin),len(error_bin)
    # print pred_classes[:10]

    # pickle.dump([error_bin,pred_classes],open(out_file_topk,'wb'));

    return

    db_path = 'sqlite://///disk2/novemberExperiments/nn_imagenet/nn_imagenet.db'

    all_pascal_id = [
        'boat', 'train', 'bicycle', 'chair', 'motorbike', 'aeroplane', 'sofa',
        'diningtable', 'bottle', 'tvmonitor', 'bus', 'car'
    ]

    batch_size = 1000
    top_n = 1
    gpu_no = 1
    getClasses = True

    ids_labels = imagenet.readLabelsFile(synset_words)
    labels = list(zip(*ids_labels)[1])
    imagenet_ids = list(zip(*ids_labels)[0])

    net, transformer = setUpNet(model_file, deploy_file, mean_file, gpu_no)

    for pascal_id in ['car']:
        # all_pascal_id:
        print pascal_id
        out_file_topk = '/disk2/novemberExperiments/nn_imagenet/' + pascal_id + '_pred_no_train.p'
        out_file_topk_txt = '/disk2/novemberExperiments/nn_imagenet/' + pascal_id + '_pred_no_train.txt'

        mani = Imagenet_Manipulator(db_path)
        mani.openSession()
        criterion = (Imagenet.class_id_pascal == pascal_id, )
        vals = mani.select((Imagenet.img_path, ), criterion, distinct=True)

        mani.closeSession()
        im_files = [val[0] for val in vals]
        imagenet_idx_mapped = [772] * len(im_files)
        print len(im_files)

        # return
        # error_bin=pickle.load(open(out_file_topk,'rb'));
        # print sum(error_bin),len(error_bin),sum(error_bin)/float(len(error_bin))

        # out_file_topk='/disk2/octoberExperiments/nn_performance_without_pascal/val_performance_top5_trained.p';
        # error_bin=pickle.load(open(out_file_topk,'rb'));
        # print sum(error_bin),len(error_bin),sum(error_bin)/float(len(error_bin))

        # return

        # val_gt=imagenet.readLabelsFile(val_gt_file);
        # im_files=list(zip(*val_gt)[0]);
        # im_files=[os.path.join(path_to_val,im_file) for im_file in im_files]
        # imagenet_idx_mapped=list(zip(*val_gt)[1])
        # imagenet_idx_mapped=[int(x) for x in imagenet_idx_mapped];

        # print idx_mapped_simple[:10],len(idx_mapped_simple);
        # print 'getting mapping'

        # imagenet_idx_mapped,imagenet_ids_mapped,imagenet_labels_mapped=imagenet.getMappingInfo(im_files,synset_words,val_gt_file)
        # print imagenet_idx_mapped[:10],len(imagenet_idx_mapped),type(imagenet_idx_mapped),type(imagenet_idx_mapped[0]),type(idx_mapped_simple),type(idx_mapped_simple[0])

        # for idx_idx,idx in enumerate(idx_mapped_simple):
        #   print idx,imagenet_idx_mapped[idx_idx]
        #   assert idx==imagenet_idx_mapped[idx_idx]
        # return

        error_bin, pred_classes = getTopNError(net,
                                               transformer,
                                               im_files,
                                               imagenet_idx_mapped,
                                               batch_size,
                                               top_n,
                                               printDebug=True,
                                               pred_classes=True)
        print sum(error_bin), len(error_bin)
        print pred_classes[:10]

        pickle.dump([error_bin, pred_classes], open(out_file_topk, 'wb'))

        pred_classes = pred_classes.ravel()
        tuples = []
        for class_curr in np.unique(pred_classes):
            # print imagenet_ids[class_curr],labels[class_curr];
            tuples.append(
                (class_curr, labels[class_curr],
                 sum(pred_classes == class_curr) / float(len(pred_classes))))

        tuples = sorted(tuples, key=lambda x: x[2])[::-1]
        with open(out_file_topk_txt, 'wb') as f:
            for tuple_curr in tuples:
                print imagenet_ids[tuple_curr[0]], tuple_curr
                f.write(
                    str(tuple_curr[0]) + '\t' + str(tuple_curr[1]) + '\t' +
                    str(round(tuple_curr[2] * 100, 2)))
                f.write('\n')