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 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
Example #3
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
Example #4
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
def script_temp():

    path_to_val = "/disk2/imagenet/val"
    ext = "JPEG"

    out_dir = "/disk2/novemberExperiments/nn_imagenet_try"
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    in_file_pre = "list_of_ims_for_nn"
    in_file_pre = os.path.join(out_dir, in_file_pre)

    path_to_classify = ".."
    trainFlag = False
    # caffe_model='/home/maheenrashid/Downloads/caffe/caffe-rc2/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
    caffe_model = "/disk2/octoberExperiments/nn_performance_without_pascal/snapshot_iter_450000.caffemodel"
    caffe_deploy = "/disk2/octoberExperiments/nn_performance_without_pascal/deploy.prototxt"
    caffe_mean = "/disk2/octoberExperiments/nn_performance_without_pascal/mean.npy"
    gpu_no = 0
    layers = ["pool5", "fc6", "fc7"]
    out_file = "nn_non_trained"
    out_file = os.path.join(out_dir, out_file)

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

    synset_words = "../../data/ilsvrc12/synset_words.txt"
    val_gt_file = "../../data/ilsvrc12/val.txt"

    idx_chosen = pickle.load(open("/disk2/novemberExperiments/nn_imagenet/equal_mix_ids.p", "rb"))

    im_files_gt_classes = imagenet.selectTestSetByID(val_gt_file, idx_chosen, path_to_val=path_to_val)
    im_files = list(zip(*im_files_gt_classes)[0])
    gt_classes = list(zip(*im_files_gt_classes)[1])
    print len(im_files)
    print len(gt_classes)
    print len(set(gt_classes))
    per_file = len(im_files)

    # in_files,_=writeInputImageFiles(im_files,in_file_pre,per_file);
    in_files = [in_file_pre + "_" + str(0) + ".txt"]
    print in_files
    out_files = []
    for idx, in_file_curr in enumerate(in_files):
        out_file_curr = out_file + "_" + str(idx)
        out_files.append(
            caffe_wrapper.saveFeaturesOfLayers(
                in_file_curr,
                path_to_classify,
                gpu_no,
                layers,
                ext=ext,
                out_file=out_file_curr,
                meanFile=caffe_mean,
                deployFile=caffe_deploy,
                modelFile=caffe_model,
            )
        )

    print in_files
    print out_files

    file_list_all = []
    for in_file_curr in in_files:
        with open(in_file_curr, "rb") as f:
            file_list = f.readlines()
            file_list_all.extend([file_curr.strip("\n") for file_curr in file_list])
    print len(file_list_all)

    imagenet_idx_mapped, imagenet_ids_mapped, imagenet_labels_mapped = imagenet.getMappingInfo(
        file_list_all, synset_words, val_gt_file
    )

    print "about to combine"
    t = time.time()
    val_combined = combineDeepFeaturesFromFiles(out_files, layers)
    print time.time() - t

    for layer_curr in layers:
        print "about to nn for ", layer_curr
        t = time.time()
        indices, distances = nearest_neighbor.doCosineDistanceNN(val_combined[layer_curr], numberOfN=None)
        print time.time() - t
        #     break;
        # return

        print indices.shape
        print distances.shape

        print "writing to db"
        mani = Imagenet_Manipulator(db_path_out)
        mani.openSession()
        for idx in range(len(file_list_all)):
            if idx % 100 == 0:
                print layer_curr, idx, len(file_list_all)
            idx_out_file = idx / per_file
            out_file_layers = out_file + "_" + str(idx_out_file) + ".npz"

            mani.insert(
                idx,
                file_list_all[idx],
                layer_curr,
                out_file_layers,
                trainFlag,
                imagenet_idx_mapped[idx],
                imagenet_ids_mapped[idx],
                caffe_model,
                class_label_imagenet=imagenet_labels_mapped[idx],
                neighbor_index=indices[idx],
                neighbor_distance=distances[idx],
            )

        mani.closeSession()