def compare_stride(image_id, crop_metric, which_layers=VGG_LAYERS):

    with open('high-act-sample', 'rb') as readfile:
        h_act = pickle.load(readfile)
    with open('low-act-sample', 'rb') as readfile:
        l_act = pickle.load(readfile)

    maxdiff_folder = settings.maxdiff_folder_name(crop_metric)
    hconfcrop = np.load(PATH_TO_DATA + maxdiff_folder +
                        settings.get_ind_name(image_id) + '_' +
                        str(crop_metric) +
                        '_maxdiff_highconf_diffcorrectness.npy')
    lconfcrop = np.load(PATH_TO_DATA + maxdiff_folder +
                        settings.get_ind_name(image_id) + '_' +
                        str(crop_metric) +
                        '_maxdiff_lowconf_diffcorrectness.npy')

    direction = None
    if np.array_equal(lconfcrop[:, 0, :], hconfcrop[:, 1, :]):
        direction = 'left'
    elif np.array_equal(lconfcrop[:, 1, :], hconfcrop[:, 0, :]):
        direction = 'right'
    elif np.array_equal(lconfcrop[0, :, :], hconfcrop[1, :, :]):
        direction = 'up'
    elif np.array_equal(lconfcrop[1, :, :], hconfcrop[0, :, :]):
        direction = 'down'
    if direction is None:
        print('Finding direction failed.')
        return

    hlayers = dict(zip(VGG_LAYERS, [np.squeeze(mat) for mat in h_act]))
    llayers = dict(zip(VGG_LAYERS, [np.squeeze(mat) for mat in l_act]))
    hlayers = {k: v for k, v in hlayers.items() if k in which_layers}
    llayers = {k: v for k, v in llayers.items() if k in which_layers}

    differences = []

    for layer in which_layers:

        hlayer = hlayers[layer]
        llayer = llayers[layer]
        if direction == 'left':
            hcommon = hlayer[:, 1:, :]
            lcommon = llayer[:, :-1, :]
        elif direction == 'right':
            hcommon = hlayer[:, :-1, :]
            lcommon = llayer[:, 1:, :]
        elif direction == 'up':
            hcommon = hlayer[1:, :, :]
            lcommon = llayer[:-1, :, :]
        else:
            hcommon = hlayer[:-1, :, :]
            lcommon = llayer[1:, :, :]

        D = np.linalg.norm(hcommon - lcommon, axis=(0, 1))
        D2 = np.linalg.norm(hcommon - lcommon, axis=0)
        D3 = hcommon - lcommon

        np.save(str(image_id) + '_' + layer + '_high', hlayer)
        np.save(str(image_id) + '_' + layer + '_low', llayer)
Esempio n. 2
0
def random():

    # Start VGG
    sess = tf.Session()
    imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg = vgg16(imgs, COMMAND_PATH + 'vgg16_weights.npz', sess)

    # Get random crop list
    folder = COMMAND_PATH + DATA_PATH + settings.folder_name(DATATYPE)
    rand_crops = os.listdir(folder)

    # Get true value
    image_id = int(sys.argv[2])
    image_tag = settings.get_ind_name(image_id)
    labels_file = open(COMMAND_PATH + 'caffe_ilsvrc12/' + settings.DATASET + '-labels.json')
    true_labels = json.load(labels_file)
    true_value = true_labels[image_tag]

    # Get old classifications if they exist
    results_folder = COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/'
    old_results_name = settings.get_random_crops_results_name(image_id)
    if old_results_name in os.listdir(results_folder):
        with open(results_folder + old_results_name, 'r') as old_results_file:
            old_crop_classifications = json.load(old_results_file)
    else:
        old_crop_classifications = {}

    # Classify crops and store results in dictionary
    with open(COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/' + image_tag + '_' + 'dim_ids.json',
              'r') as dim_ids_file:
        dim_ids = json.load(dim_ids_file)

    crop_classifications = {}
    beginning = 'ILSVRC2012_val_00000000_crop'
    end = '.JPEG'
    for rand_crop in rand_crops:
        crop_id = rand_crop[len(beginning):-1 * len(end)]  # from the end of 'crop' to beginning of '.JPEG'. TODO fix; super abusive
        img1 = imread(folder + rand_crop, mode='RGB')
        img1 = imresize(img1, (224, 224))
        prob = sess.run(vgg.probs, feed_dict={vgg.imgs: [img1]})[0]
        max_prob = np.amax(prob)
        pred = (np.argsort(prob))[-1]
        print 'PRED:', pred
        print 'PROB:', max_prob
        dims = dim_ids[crop_id]
        if pred == true_value and max_prob > settings.CROP_CONFIDENCE_THRESHOLD:
            crop_classifications[str(dims)] = True
        else:
            crop_classifications[str(dims)] = False

    # Update old classifications with new ones and save everything
    old_crop_classifications.update(crop_classifications)
    with open(results_folder + image_tag + '_crop_results.json', 'w') as results_file:
        json.dump(old_crop_classifications, results_file)
        print old_crop_classifications

    shutil.rmtree(folder)
def get_crop_size(smalldataset_id, crop_metric):
    imagenetval_id = settings.convert_id_small_to_imagenetval(smalldataset_id)
    image_tag = settings.get_ind_name(imagenetval_id)
    image_filename = PATH_TO_DATA + settings.folder_name('img') + image_tag + '.JPEG'
    im = Image.open(image_filename)
    width, height = im.size
    # print('IMAGE SHAPE:', width, height)
    crop_type = 'proportional' if crop_metric <= 1. else 'constant'
    crop_size = c_m_p.get_crop_size(height, crop_metric, crop_type) if height <= width else c_m_p.get_crop_size(width, crop_metric, crop_type)
    return crop_size
Esempio n. 4
0
def save_crops(coords,
               crop_metric,
               model_name,
               image_scale,
               axis,
               compare_corr,
               num_samples=50):

    folder = PATH_TO_OUTPUT_DATA + settings.maxdiff_folder_name(
        axis, crop_metric, model_name, image_scale,
        'diff' if compare_corr else 'any')
    if not os.path.exists(folder):
        os.makedirs(folder)

    # top_ids = sorted(coords, reverse=True, key=lambda x: coords[x][2])[num_samples:num_samples * 2]

    # print(top_ids)

    for smalldataset_id in coords:

        print('CURRENTLY SAVING:', smalldataset_id)

        im_filename = PATH_TO_DATA + settings.folder_name(
            'img') + settings.get_ind_name(
                settings.convert_id_small_to_imagenetval(
                    smalldataset_id)) + '.JPEG'
        im = Image.open(im_filename)
        hcell, lcell = coords[smalldataset_id][:2]
        hfn, lfn = (PATH_TO_OUTPUT_DATA + settings.maxdiff_file_name(
            smalldataset_id, axis, crop_metric, model_name, image_scale,
            'diff' if compare_corr else 'any', conf)
                    for conf in ['high', 'low'])

        if axis == 'scale':
            high_size = get_crop_size(smalldataset_id, crop_metric)
            low_size = high_size - 2
            hcrop = im.crop((hcell[0], hcell[1], hcell[0] + high_size,
                             hcell[1] + high_size))
            lcrop = im.crop(
                (lcell[0], lcell[1], lcell[0] + low_size, lcell[1] + low_size))

        elif axis == 'shift':
            size = get_crop_size(smalldataset_id, crop_metric)
            hcrop = im.crop(
                (hcell[0], hcell[1], hcell[0] + size, hcell[1] + size))
            lcrop = im.crop(
                (lcell[0], lcell[1], lcell[0] + size, lcell[1] + size))

        print(hfn)
        print(lfn)

        hcrop.save(hfn, 'JPEG')
        lcrop.save(lfn, 'JPEG')

    print('SAVES COMPLETE')
Esempio n. 5
0
def highlight_best_crop(img_id):

    img_name = settings.get_ind_name(img_id)
    img_folder = folder_name('img')
    results_folder = COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/'

    with open(results_folder + 'best-crops.json', 'r') as crops_file:
        crops = json.load(crops_file)

    if str(img_id) in crops:
        draw_boundinbox(img_name, img_folder, [tuple(crops[str(img_id)][1])])
    else:
        print "This image hasn't been randomly cropped yet."
Esempio n. 6
0
def save_human_crops(coords,
                     crop_metric,
                     model_name,
                     image_scale,
                     compare_corr,
                     num_samples=50):

    PATH_TO_DATA = '/om/user/sanjanas/min-img-data/human-image-comparison/'
    PATH_TO_OUTPUT_DATA = PATH_TO_DATA
    axis = 'shift'

    folder = PATH_TO_OUTPUT_DATA + settings.maxdiff_folder_name(
        axis, crop_metric, model_name, image_scale,
        'diff' if compare_corr else 'any')
    if not os.path.exists(folder):
        os.makedirs(folder)

    # top_ids = sorted(coords, reverse=True, key=lambda x: coords[x][2])[num_samples:num_samples * 2]

    for image_id in range(50002, 50009):

        if image_id in coords:

            im_filename = PATH_TO_DATA + 'human-images/' + settings.get_ind_name(
                image_id) + '.png'
            im = Image.open(im_filename)
            hcell, lcell = coords[image_id][:2]
            hfn, lfn = (PATH_TO_OUTPUT_DATA + settings.maxdiff_file_name(
                image_id, axis, crop_metric, model_name, image_scale,
                'diff' if compare_corr else 'any', conf)
                        for conf in ['high', 'low'])

            width, height = im.size
            dimension = height if height <= width else width
            size = int(((crop_metric * dimension) // 2) * 2 + 1)
            hcrop = im.crop(
                (hcell[0], hcell[1], hcell[0] + size, hcell[1] + size))
            lcrop = im.crop(
                (lcell[0], lcell[1], lcell[0] + size, lcell[1] + size))

            hcrop.save(hfn, 'PNG')
            lcrop.save(lfn, 'PNG')
Esempio n. 7
0
def highlight_random_crops(img_id, visualization='black'):

    img_name = settings.get_ind_name(img_id)
    img_folder = folder_name('img')
    results_folder = COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/'

    results_name = settings.get_random_crops_results_name(img_id)
    if results_name in os.listdir(results_folder):
        with open(results_folder + results_name, 'r') as results_file:
            results = json.load(results_file)

        all_dims = []
        for dims in results:
            if results[dims]:  # if it was classified correctly
                all_dims.append(
                    json.loads(dims))  # They were stringified, so load them

        draw_boundinbox(img_name, img_folder, all_dims)

    else:
        print "This image hasn't been randomly cropped yet."
def minimal_image_distribution(num_imgs, crop_metric, model_name, image_scale, strictness):

    resize_dim = 150
    minimal_image_aggregation = np.zeros((resize_dim, resize_dim))

    # img_ids = random.sample(range(100), num_imgs)     # for testing on my machine: only a subset of the maps. TODO remove for full job
    img_ids = range(3)
    for smalldataset_id in img_ids:

        # get bbx dimensions
        imagenetval_id = settings.convert_id_small_to_imagenetval(smalldataset_id)
        image_tag = settings.get_ind_name(imagenetval_id)
        with open(BBX_FILE, 'r') as bbx_file:
            all_bbxs = json.load(bbx_file)
            crop_dims = [bbx[0] for bbx in all_bbxs[image_tag]]     # get all x1, y1, x2, y2 crops

        minimal_map_f = PATH_TO_DATA + settings.map_filename(settings.TOP5_MAPTYPE, crop_metric, model_name, image_scale, smalldataset_id)
        minimal_map_f = minimal_map_f + '_' + ('l' if strictness == 'loose' else '') + 'map'
        minimal_map = np.load(minimal_map_f + '.npy')

        image_filename = PATH_TO_DATA + settings.folder_name('img') + image_tag + '.JPEG'
        try:                                    # for testing on my machine: if the image is not on my machine, move on. TODO remove for full job
            im = Image.open(image_filename)
        except OSError:
            continue
        width, height = im.size
        crop_type = 'proportional' if crop_metric <= 1. else 'constant'
        crop_size = c_m_p.get_crop_size(height, crop_metric, crop_type) if height <= width else c_m_p.get_crop_size(width, crop_metric, crop_type)
        for x1, y1, x2, y2 in crop_dims:
            minmap_sub = minimal_map[y1:y2 - crop_size + 1, x1:x2 - crop_size + 1]
            minmap_sub = imresize(minmap_sub, (resize_dim, resize_dim))
            minimal_image_aggregation += minmap_sub

    vis = (minimal_image_aggregation - np.min(minimal_image_aggregation))
    vis /= np.max(vis)
    vis *= 255.
    Image.fromarray(vis).show()

    return minimal_image_aggregation
Esempio n. 9
0
def backprop_crops(image_id, crop_metric, model_name, image_scale):
    '''
    note: even with the graph inefficiency, it makes more sense for this function to be one image at a time because the indices are different for each one. This would only change if I figured out a programmatic way to find the right indices. 
    '''

    # Get indices for this map
    with open(PATH_TO_DATA + settings.BACKPROP_INDICES_FILENAME,
              'r') as indfile:
        all_inds = json.load(indfile)
        indices = all_inds[str(
            (crop_metric, model_name, image_scale, image_id))]

    with tf.Session() as sess:

        # Set up CNN
        model = settings.MODELS[model_name]
        batch_size = len(indices)
        imgs = tf.placeholder(tf.float32,
                              [batch_size, model.im_size, model.im_size, 3])
        network = model(imgs, sess)

        # Create backprop objects
        true_labels = json.load(
            open('caffe_ilsvrc12/' + settings.DATASET + '-labels.json'))
        true_label = true_labels[settings.get_ind_name(image_id)]
        y = tf.constant([true_label for __ in range(batch_size)])
        err = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=y, logits=network.logits)
        dy_dx = tf.gradients(err, network.imgs)

        # Scale image, get all crops and run backprop
        image = Image.open(PATH_TO_DATA + settings.folder_name('img') +
                           settings.get_ind_name(image_id) + '.JPEG')
        width, height = image.size
        image = imresize(image,
                         (int(width * image_scale), int(height * image_scale)))
        crop_type = 'proportional' if crop_metric <= 1. else 'constant'
        crop_size = get_crop_size(
            height, crop_metric,
            crop_type) if height <= width else get_crop_size(
                width, crop_metric, crop_type)
        all_crops = []
        for x1, y1 in indices:
            crop = image[y1:y1 + crop_size, x1:x1 + crop_size, :]
            # crop = image.crop((x1, y1, x1 + crop_size, y1 + crop_size))
            all_crops.append(imresize(crop, (model.im_size, model.im_size)))
            # all_crops.append(imresize(image, (model.im_size, model.im_size)))	# TODO remove after making sure this code is working

        backprops = sess.run(
            dy_dx,
            feed_dict={network.imgs: model.preprocess(np.array(all_crops))})[0]

    # Make backprop results visualizable
    backprops = backprops - np.min(backprops, axis=(1, 2), keepdims=True)
    backprops = backprops / np.max(backprops, axis=(1, 2), keepdims=True)
    backprops = backprops * 255.
    backprops = backprops.astype(np.uint8)

    # savez results
    folder = PATH_TO_DATA + settings.map_folder_name(
        settings.BACKPROP_MAPTYPE, crop_metric, model_name, image_scale)
    filename = PATH_TO_DATA + settings.map_filename(
        settings.BACKPROP_MAPTYPE, crop_metric, model_name, image_scale,
        image_id)
    if not os.path.exists(folder):
        os.makedirs(folder)
    np.savez(filename,
             **{str(indices[i]): backprops[i]
                for i in range(len(indices))})
Esempio n. 10
0
def get_maxdiff_size_crops(start_id,
                           end_id,
                           crop_metric,
                           model_name,
                           image_scale,
                           compare_corr=True):

    # For each image id in range(start_id, end_id + 1), finds the crop of size crop_metric and the crop ~2 pixels smaller that are maximally different in confidence. If compare_corr is True, it necessarily finds crops where the smaller one is classified incorrectly and the larger one is classified correctly. This uses the top5 maps for the two scales.

    crop_type = 'proportional' if crop_metric <= 1. else 'constant'
    folders = [
        PATH_TO_DATA + settings.maxdiff_folder_name(
            'size', crop_metric, model_name, image_scale,
            'diff' if compare_corr else 'any', conf)
        for conf in ['high', 'low']
    ]
    for folder in folders:
        if not os.path.exists(folder):
            os.makedirs(folder)

    map_folder = PATH_TO_DATA + settings.maxdiff_folder_name(
        'size', crop_metric, model_name, image_scale, 'map')
    if not os.path.exists(map_folder):
        os.makedirs(map_folder)

    # with tf.Session() as sess:	# TODO use for testing
    # model = settings.MODELS[model_name]
    # imgs = tf.placeholder(tf.float32, [None, model.im_size, model.im_size, 3])
    # network = model(imgs, sess)

    true_labels = json.load(
        open('caffe_ilsvrc12/' + settings.DATASET + '-labels.json'))

    for image_id in range(start_id, end_id + 1):
        image_tag = settings.get_ind_name(image_id)
        image_filename = PATH_TO_DATA + settings.folder_name(
            'img') + image_tag + '.JPEG'
        true_class = true_labels[image_tag]
        im = Image.open(image_filename)
        if im.mode != 'RGB':
            im = im.convert('RGB')
        width, height = im.size
        im = im.resize((int(width * image_scale), int(height * image_scale)))
        width, height = im.size
        im = np.asarray(im)

        # Get the small crop_metric, crop sizes for the large and small crop_metrics
        size_dim = height if height <= width else width
        large_size = get_crop_size(
            size_dim, crop_metric,
            crop_type) if height <= width else get_crop_size(
                width, crop_metric, crop_type)
        small_metric = 0.194 if crop_metric == 0.2 else 0.394  # TODO change to be a calculation and command
        small_size = get_crop_size(size_dim, small_metric, crop_type)
        metrics = [crop_metric, small_metric]

        # Get the correctness maps (top5, may become a choice between top5 and top1 in the future), and if the call requires diff correctness, check that that's possible
        corr_fns = [
            PATH_TO_DATA +
            settings.map_filename(settings.TOP5_MAPTYPE, metric, model_name,
                                  image_scale, image_id) + '.npy'
            for metric in metrics
        ]  # TODO change to allow choice of top1 or top5
        lcor, scor = cor_maps = [np.load(corr_fn) for corr_fn in corr_fns]
        if compare_corr:
            for cor_map in cor_maps:
                if not cor_map.any():
                    print('%s has no correctly classified crops.' % image_tag)
                    continue
                elif cor_map.all():
                    print('%s has only correctly classified crops.' %
                          image_tag)
                    continue

        # Get confidence maps
        con_fns = [
            PATH_TO_DATA +
            settings.map_filename(settings.CONFIDENCE_MAPTYPE, metric,
                                  model_name, image_scale, image_id) + '.npy'
            for metric in metrics
        ]
        lcon, scon = [np.load(con_fn) for con_fn in con_fns]

        # Calculate difference matrices
        lrows, lcols = lcon.shape
        offset = large_size - small_size  # get the metric that bottom and right are off by

        tl_sub = scon[:lrows, :
                      lcols]  # for top left, get the top left small crops that correspond to big crops - same shape. It's all of them because the large crops are all adjacent, even if the difference in pixels is >1.
        tr_sub = scon[:lrows, offset:lcols +
                      offset]  # for top right, everything that is 'offset' cols over
        bl_sub = scon[offset:lrows + offset, :lcols]
        br_sub = scon[offset:lrows + offset, offset:lcols + offset]
        ctoffset = int(offset / 2)
        ct_sub = scon[ctoffset:lrows + ctoffset, ctoffset:lcols + ctoffset]

        diffs = {
            'tl': lcon -
            tl_sub,  # use subtraction because we are looking for increase in conf from increase in size
            'tr': lcon - tr_sub,
            'bl': lcon - bl_sub,
            'br': lcon - br_sub,
            'ct': lcon - ct_sub
        }

        # Make map of the largest size change in confidence across all directions of shrinking
        change_map = np.maximum.reduce(list(diffs.values()))
        np.save(map_folder + str(image_id), change_map)

        # Find maxdiff pair by searching for maximally different pairs until one with different correctness is found (if diffcor. Else, this will terminate after one loop as the first pair found will be the maximally different one and therefore the right one for anycor.)
        while True:

            maxes = {
                corner: np.unravel_index(np.argmax(diffs[corner]),
                                         diffs[corner].shape)
                for corner in diffs
            }  # map each corner diff to its argmax (index of maximum confidence diff)
            max_dir = max(
                [corner for corner in maxes],
                key=lambda corner: diffs[corner][tuple(maxes[corner])]
            )  # get the corner id of the diff whose max change is the highest out of the four max changes
            # getting the indices of the maximal confidence increase. Indices are based on the size of the large crop size map. The first index is the index for the large crop, and the second is for the small crop.
            corner_max = maxes[max_dir]
            if max_dir == 'tl':
                lcell, scell = tuple(corner_max), tuple(corner_max)
            elif max_dir == 'tr':
                lcell, scell = tuple(corner_max), (corner_max[0],
                                                   corner_max[1] + offset)
            elif max_dir == 'bl':
                lcell, scell = tuple(corner_max), (corner_max[0] + offset,
                                                   corner_max[1])
            elif max_dir == 'br':
                lcell, scell = tuple(corner_max), (corner_max[0] + offset,
                                                   corner_max[1] + offset)
            else:
                lcell, scell = tuple(corner_max), (corner_max[0] + ctoffset,
                                                   corner_max[1] + ctoffset)

            diff_corr = lcor[lcell] != scor[scell]
            if diff_corr or not compare_corr:
                sy, sx = scell
                ly, lx = lcell
                lcropped = im[lcell[0]:lcell[0] + large_size,
                              lcell[1]:lcell[1] + large_size]
                scropped = im[scell[0]:scell[0] + small_size,
                              scell[1]:scell[1] + small_size]
                # lcropped = imresize(lcropped, (network.im_size, network.im_size))
                # scropped = imresize(scropped, (network.im_size, network.im_size))
                # result = sess.run(network.probs, feed_dict={network.imgs: np.array([lcropped, scropped])})	# run and see if it works later. Without this, the sess isn't actually being used - this is for internal test.
                break
            else:  # if that location wasn't diffcorr, set the diff's entry to -2.
                diffs[max_dir][lcell] = -2.

        lfolder, sfolder = folders
        np.save(lfolder + str(image_id), lcropped)
        np.save(sfolder + str(image_id), scropped)
Esempio n. 11
0
def best_crops():

    # Start VGG
    sess = tf.Session()
    imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg = vgg16(imgs, COMMAND_PATH + 'vgg16_weights.npz', sess)

    # Get best crop records - CHANGED TO just start a new best crop record
    # results_folder = COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/'
    # if 'best-crops.json' in os.listdir(results_folder):
    #     with open(COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/best-crops.json', 'r') as best_crop_file:
    #         best_crop_record = json.load(best_crop_file)
    # else:
    #     best_crop_record = {}
    best_crop_record = {}

    start_id = int(sys.argv[2])
    end_id = int(sys.argv[3])
    num_crops = int(sys.argv[4])
    for image_id in range(start_id, end_id + 1):

        # Get random crop list
        folder = folder = COMMAND_PATH + DATA_PATH + settings.folder_name(DATATYPE + '%08d' % image_id)
        rand_crops = os.listdir(folder)

        # Get true value
        image_tag = settings.get_ind_name(image_id)
        labels_file = open(COMMAND_PATH + 'caffe_ilsvrc12/' + settings.DATASET + '-labels.json')
        true_labels = json.load(labels_file)
        true_value = true_labels[image_tag]

        # Classify crops and get the best crop
        dim_id_path = COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/' + image_tag + '_dim_ids.json'
        with open(dim_id_path,
                  'r') as dim_ids_file:
            dim_ids = json.load(dim_ids_file)

        best_prob = 0
        best_dims = None
        beginning = 'ILSVRC2012_val_00000000_crop'
        end = '.JPEG'
        for rand_crop in rand_crops:
            crop_id = rand_crop[len(beginning):-1 * len(end)]  # from the end of 'crop' to beginning of '.JPEG'. TODO fix; super abusive
            img1 = imread(folder + rand_crop, mode='RGB')
            img1 = imresize(img1, (224, 224))
            prob = sess.run(vgg.probs, feed_dict={vgg.imgs: [img1]})[0]

            spec_prob = prob[true_value]    # get the probability of this image being the true value

            if spec_prob > best_prob:   # if it's better, save its dimensions and check whether it correctly classified the image
                best_prob = spec_prob
                best_dims = dim_ids[crop_id]

                pred = (np.argsort(prob))[-5:]   # get the actual top 5 predictions

                if true_value in pred:
                    correct = True
                else:
                    correct = False

        print 'IMAGE ID:', image_id
        print type(best_crop_record)
        print len(best_crop_record)

        # if str(image_id) not in best_crop_record or float(best_crop_record[str(image_id)][0]) < best_prob:    # if it doesn't exist yet or this new probability is better than the old one
        #     best_crop_record[image_id] = (str(best_prob), best_dims, correct)     # update the best crop to this
        best_crop_record[image_id] = (str(best_prob), best_dims, correct)   # just add it - no longer updating an existing one

        os.remove(dim_id_path)  # Get rid of the dimensions file
        shutil.rmtree(folder)   # Get rid of the crops

    # Update test records with an entry on number of images and performance on those images

    with open(COMMAND_PATH + DATA_PATH + settings.IMAGENET_SET + '_randind_results/best-crops.json', 'w') as best_crop_file:
        json.dump(best_crop_record, best_crop_file)

    success = sum(best_crop_record[image_id][2] for image_id in best_crop_record)
    total = len(best_crop_record)

    top5_accuracy = float(success)/float(total)

    fieldnames = settings.DATA_FIELDNAMES
    with open(COMMAND_PATH + DATA_PATH + 'test_statistics.csv', 'rU') as stats_file:
        stats_reader = csv.DictReader(stats_file, fieldnames=fieldnames)
        # TODO make this code less disgusting
        testID = 0
        for line in stats_reader:
            testID = line['testID']
        testID = int(testID) + 1

    with open(COMMAND_PATH + DATA_PATH + 'test_statistics.csv', 'a') as stats_file:
        entry = {'testID': testID,
                 'imagenet_set': settings.IMAGENET_SET,
                 'dataset': settings.DATASET,
                 'datatype': DATATYPE,
                 'num_images': total,
                 # 'top1_accuracy': None,
                 'top5_accuracy': top5_accuracy,
                 'aggregation': None,
                 'num_crops': num_crops}
        stats_writer = csv.DictWriter(stats_file, fieldnames=fieldnames)
        stats_writer.writerow(entry)
Esempio n. 12
0
def print_tf(thing, printed_things):
    with tf.Session() as sess:
        thing = tf.Print(thing, printed_things)
        thing.eval()


if __name__ == '__main__':

    image_size = inception_v4.default_image_size
    #   with tf.Graph().as_default():

    ids = [1, 13, 26, 29]
    images = []
    for i in ids:
        image = imread(PATH_TO_DATA + 'ILSVRC2012_img_val/' +
                       settings.get_ind_name(i) + '.JPEG',
                       mode='RGB')
        image = imresize(image, (image_size, image_size))
        images.append(image)

    imgs = tf.placeholder(tf.float32, [None, image_size, image_size, 3])

    #   images = np.array([image, image2])
    images = np.array(images)
    #   sess = tf.Session()
    with tf.Session() as sess:
        network = inception(imgs, sess)
        processed_images = inception.preprocess(images)
        probabilities = np.array(
            sess.run(network.probs, feed_dict={network.imgs:
                                               processed_images}))
Esempio n. 13
0
import sys
import tensorflow as tf

import settings


# DATA PARAMS
PATH_TO_DATA = '../../poggio-urop-data/'
CONFIDENCE_MAP_FOLDER = 'ILSVRC2012_confidence_maps/'

# TEST PARAMS
CROP_TYPE = 'proportional'          # choose between 'proportional' and 'constant'
CONSTANT = 81                       # pixel-length of square crop (must be odd)
PROPORTION = 0.2                    # proportion of height that will be length of square crop
IMAGE_ID = int(sys.argv[1])
IMAGE_FILENAME = PATH_TO_DATA + settings.folder_name('img') + settings.get_ind_name(IMAGE_ID) + '.JPEG'      # filename of the image including path to poggio-urop-data

image_tag = settings.get_ind_name(IMAGE_ID)
labels_file = open('caffe_ilsvrc12/' + settings.DATASET + '-labels.json')
true_labels = json.load(labels_file)
TRUE_CLASS = true_labels[image_tag]


# START VGG
class vgg16:
    def __init__(self, imgs, weights=None, sess=None):
        self.imgs = imgs
        self.convlayers()
        self.fc_layers()
        self.probs = tf.nn.softmax(self.fc3l)
        if weights is not None and sess is not None: