def script_visualizePascalNeighborsFromOtherClass(params):
    db_path_out = params.db_path_out
    class_id_pascal = params.class_id_pascal
    limit = params.limit
    layer = params.layer
    trainFlag = params.trainFlag
    rel_path = params.rel_path
    out_file_html = params.out_file_html
    top_n = params.top_n
    height_width = params.height_width

    mani = Imagenet_Manipulator(db_path_out)
    mani.openSession()
    vals = mani.select(
        (Imagenet.idx, Imagenet.img_path, Imagenet.class_label_imagenet, Imagenet.neighbor_index),
        (Imagenet.class_id_pascal == class_id_pascal, Imagenet.layer == layer, Imagenet.trainedClass == trainFlag),
        distinct=True,
        limit=limit,
    )
    print len(vals)

    idx_non_pascal = mani.select(
        (Imagenet.idx,),
        (Imagenet.class_id_pascal == None, Imagenet.layer == layer, Imagenet.trainedClass == trainFlag),
        distinct=True,
    )

    img_paths_html = []
    captions_html = []
    for val_curr in vals:
        html_row = []
        captions_row = []

        img_path = val_curr[1]
        imagenet_label = val_curr[2]
        nearest_neighbor = val_curr[3]
        remove_bool = np.in1d(nearest_neighbor, idx_non_pascal, invert=True)
        nearest_neighbor = np.delete(nearest_neighbor, np.where(remove_bool))

        html_row.append(img_path.replace(rel_path[0], rel_path[1]))
        captions_row.append("test image " + imagenet_label)

        for idx_curr in range(top_n):
            idx_nn = nearest_neighbor[idx_curr]
            (img_path_nn, class_id_imagenet, class_label_imagenet) = mani.select(
                (Imagenet.img_path, Imagenet.class_id_imagenet, Imagenet.class_label_imagenet),
                (Imagenet.idx == idx_nn, Imagenet.layer == layer, Imagenet.trainedClass == trainFlag),
                distinct=True,
            )[0]
            html_row.append(img_path_nn.replace(rel_path[0], rel_path[1]))
            captions_row.append(class_id_imagenet + " " + class_label_imagenet)

        img_paths_html.append(html_row)
        captions_html.append(captions_row)
        # raw_input();

    mani.closeSession()
    visualize.writeHTML(out_file_html, img_paths_html, captions_html, height_width[0], height_width[1])
def getNNRankComparisonInfo(params):
    db_path = params.db_path
    class_id = params.class_id
    layer = params.layer
    pascalFlag = params.pascalFlag

    mani = Imagenet_Manipulator(db_path)
    mani.openSession()

    trainFlag = True

    if pascalFlag:
        criterion = (Imagenet.class_id_pascal == class_id, Imagenet.layer == layer, Imagenet.trainedClass == trainFlag)
    else:
        criterion = (
            Imagenet.class_id_imagenet == class_id,
            Imagenet.layer == layer,
            Imagenet.trainedClass == trainFlag,
        )

    toSelect = (Imagenet.img_path, Imagenet.neighbor_index)
    rows = mani.select(toSelect, criterion, distinct=True)
    rows_trained = sorted(rows, key=lambda x: x[0])

    trainFlag = False

    if pascalFlag:
        criterion = (Imagenet.class_id_pascal == class_id, Imagenet.layer == layer, Imagenet.trainedClass == trainFlag)
    else:
        criterion = (
            Imagenet.class_id_imagenet == class_id,
            Imagenet.layer == layer,
            Imagenet.trainedClass == trainFlag,
        )

    toSelect = (Imagenet.img_path, Imagenet.neighbor_index)
    rows = mani.select(toSelect, criterion, distinct=True)
    rows_no_trained = sorted(rows, key=lambda x: x[0])

    img_paths_trained = [row_trained[0] for row_trained in rows_trained]
    img_paths_no_trained = [row_no_trained[0] for row_no_trained in rows_no_trained]
    assert img_paths_trained == img_paths_no_trained
    trainFlag = True
    img_paths_train, nn_rank_train = getImgPathsAndRanksSorted(
        rows_trained, db_path, class_id, trainFlag, layer, pascalFlag
    )
    trainFlag = False
    img_paths_no_train, nn_rank_no_train = getImgPathsAndRanksSorted(
        rows_no_trained, db_path, class_id, trainFlag, layer, pascalFlag
    )
    mani.closeSession()

    output = {}
    output["img_paths_test"] = img_paths_trained
    output["img_paths_nn_train"] = img_paths_train
    output["img_paths_nn_no_train"] = img_paths_no_train
    output["nn_rank_train"] = nn_rank_train
    output["nn_rank_no_train"] = nn_rank_no_train
    return output
def getImgInfoSameClass(db_path, class_id, trainFlag, layer, pascalFlag):
    mani = Imagenet_Manipulator(db_path)
    mani.openSession()

    if pascalFlag:
        criterion = (Imagenet.class_id_pascal == class_id, Imagenet.trainedClass == trainFlag, Imagenet.layer == layer)
    else:
        criterion = (
            Imagenet.class_id_imagenet == class_id,
            Imagenet.trainedClass == trainFlag,
            Imagenet.layer == layer,
        )
    toSelect = (Imagenet.idx, Imagenet.img_path)
    rows_same_class = mani.select(toSelect, criterion, distinct=True)

    mani.closeSession()
    return rows_same_class
示例#4
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")
示例#5
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')
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()