コード例 #1
0
def autocolor(img):
    img = img / 255
    #if len(img.shape) == 3:
    #  img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    classifier = autocolorize.load_default_classifier()
    rgb = autocolorize.colorize(img, classifier=classifier)
    return rgb
コード例 #2
0
    def get_occlusion_window(self, img, img_type):
        """

        :param net: an objct of neural net
        :param img: a numpy image of dimension (ht, wt)
        :return:
        """

        substrate = np.copy(img)
        ht, wt = img.shape

        # get response for unaltered image

        print "for unaltered image"
        res_org = self._get_fc7_activations(substrate, img_type=img_type)
        iteration = 1
        for win_sizeX, win_sizeY in self.scales:
            for x in range(0, ht, self.strideX):
                x_st, x_en = x, min(x + win_sizeX, ht - 1)
                if (x_en - x_st < win_sizeX / 2.0):
                    continue
                for y in range(0, wt, self.strideY):
                    y_st, y_en = y, min(y + win_sizeY, wt - 1)
                    if (y_en - y_st < win_sizeY / 2.0):
                        continue

                    print "Iteration: ", iteration
                    # print "Running for scale: ", win_sizeX, " ", win_sizeY
                    print "x_st: ", x_st, "  x_en: ", x_en, "  ht: ", x_en - x_st
                    print "y_st: ", y_st, "  y_en: ", y_en, "  wt: ", y_en - y_st

                    iteration += 1

                    cache = np.copy(substrate[x_st:x_en, y_st:y_en])
                    substrate[x_st:x_en, y_st:y_en] = 0
                    response = self._get_fc7_activations(substrate,
                                                         img_type=img_type)
                    substrate[x_st:x_en, y_st:y_en] = cache
                    self.neural_response.append(
                        ((x_st, y_st, x_en, y_en), (win_sizeX, win_sizeY),
                         response))

        best_pos, best_win, best_delta = self._get_max_L2_distance(
            res_org, self.neural_response)
        print best_pos, best_win, best_delta

        if best_pos != None and best_win != None:
            x_st, y_st, x_en, y_en = best_pos
            substrate[x_st:x_en, y_st:y_en] = 0
            rgb, _ = ac.colorize(img, classifier=self.net, return_info=True)
            rgb[x_st:x_en, y_st:y_en, :] = 0
            return best_pos, best_win, best_delta, rgb
        else:
            print "Nothing best found"
            return [None, None, None, None]
コード例 #3
0
    def _get_fc7_activations(self, img, img_type):
        try:
            rgb, _ = ac.colorize(img, classifier=self.net, return_info=True)
            activations = self.net.blobs['fc7'].data
            # print activations.shape
            activations = activations.reshape(activations.shape[1],
                                              activations.shape[2], -1)
            max_act = []
            for neuron in self.map_imgtype_neuron[img_type]:
                act_n = activations[neuron, :, :]
                act_n = np.squeeze(act_n)
                act_n = self._get_metric_max(act_n)
                max_act.append(act_n)

            # print max_act
            return np.asarray(max_act)
        except:
            print
            "category not found"
コード例 #4
0
    def _get_fc7_activations(self, img, img_type):
        try:
            rgb, _ = ac.colorize(img, classifier=self.net, return_info=True)
            activations = self.net.blobs['fc7'].data
            activations = activations.reshape(activations.shape[1],
                                              activations.shape[2], -1)
            responsible_neuron_act = []

            for neuron in self.map_imgtype_neuron[img_type]:
                act_n = activations[neuron, :, :]

                # Uncomment: If you want to only use max/sum of the returned values
                # responsible_neuron_act = self._get_metric_max(responsible_neuron_act)

                responsible_neuron_act.append(act_n)

            return np.asarray(responsible_neuron_act)
        except:
            print "category not found"
            sys.exit(-1)
コード例 #5
0
def cnn_noise_remover(from_folders, to_folder, hard_hue, plotstuff=False):

    classifier = autocolorize.load_default_classifier(input_size=448)

    count = 0
    for from_folder in from_folders:

        match_object = re.match(r'.*_(.*_.*_.*/)', from_folder)
        noise_type = match_object.group(1)

        number_of_images = len(os.listdir(from_folder))
        for filename in os.listdir(from_folder):

            bgr = cv2.imread(from_folder + filename).astype(float) / 255
            if bgr is None:
                raise Exception("Image {} wasn't read".format(from_folder + filename))

            rgb = autocolorize.colorize(bgr[:, :, ::-1], hard_hue=hard_hue, classifier=classifier)

            if plotstuff:
                plt.subplot(211)
                plt.imshow(bgr[:, :, ::-1]), plt.title('Input')
                plt.subplot(212)
                plt.imshow(rgb), plt.title('Colorized')
                plt.show()

            if not cv2.imwrite(to_folder + noise_type + filename,
                               rgb[:, :, ::-1] * 255, [cv2.IMWRITE_PNG_COMPRESSION, 0]):
                print "cnn_noise_remover: Did not write {} to file".format(filename)

            count += 1
            if count % 10 == 0:
                if hard_hue:
                    print "My method: {} / {} images done".format(count, number_of_images)
                else:
                    print "Larsson method: {} / {} images done".format(count, number_of_images)
コード例 #6
0
def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('input', nargs='*', type=str, help='Input images')
    parser.add_argument('-o', '--output', type=str,
                        help='Output image or directory')
    parser.add_argument('-V', '--version', action='store_true')
    parser.add_argument('-v', '--verbose', action='store_true')
    parser.add_argument('-w', '--weights', type=str, help='Weights file')
    parser.add_argument('-g', '--gpu', type=int),
    parser.add_argument('-d', '--device', choices=['cpu', 'gpu'],
                        default='gpu')
    parser.add_argument('-p', '--param', type=str)
    parser.add_argument('-s', '--size', type=int, default=512,
                        help='Processing size of input')
    parser.add_argument('--install', action='store_true',
                        help='Download weights file')
    args = parser.parse_args()

    if args.version:
        print('autocolorize', autocolorize.__version__)
        return

    input_size = args.size

    # Not all input sizes will produce even sizes for all layers, so we have
    # to restrict the choices to multiples of 32
    if input_size % 32 != 0:
        s = 'Invalid processing size (--size {}): Must be a multiple of 32.'
        print(s.format(input_size), file=sys.stderr)
        sys.exit(1)

    if args.install:
        download.weights_filename_with_download(args.weights)
        return

    import caffe

    if args.device == 'gpu':
        caffe.set_mode_gpu()
    elif args.device == 'cpu':
        caffe.set_mode_cpu()

    if args.gpu is not None:
        if args.device == 'cpu':
            raise ValueError('Cannot specify GPU when using CPU mode')
        caffe.set_device(args.gpu)

    classifier = autocolorize.load_default_classifier(input_size=input_size,
                                                      weights=args.weights)

    img_list = args.input
    output_fn0 = None
    if args.output is None:
        output_dir = 'colorization-output'
    elif os.path.splitext(os.path.basename(args.output))[1]:
        output_fn0 = args.output
        output_dir = None
        if len(img_list) > 1:
            raise ValueError("Cannot output to a single file if "
                             "multiple files are input")
    else:
        output_dir = args.output

    for img_i, img_fn in enumerate(img_list):
        name = os.path.splitext(os.path.basename(img_fn))[0]
        if output_fn0:
            output_fn = output_fn0
        else:
            if not os.path.isdir(output_dir):
                os.mkdir(output_dir)

            output_fn = os.path.join(output_dir, name + '.png')

        raw_img = image.load(img_fn)
        rgb, info = autocolorize.colorize(raw_img, classifier=classifier,
                                          param=args.param, return_info=True)

        image.save(output_fn, rgb)

        print('Colorized: {} -> {}'.format(img_fn, output_fn))

        if args.verbose:
            print('Min side:', info['min_side'])
            print('Max side:', info['max_side'])
            print('Input shape', info['input_shape'])
            print('Scaled shape', info['scaled_shape'])
            print('Padded shape', info['padded_shape'])
            print('Output shape:', info['output_shape'])
コード例 #7
0
def main():
    IMG_SIZE = 576
    args = parse_args()
    print(args)

    # configure logging
    if args.debug:
        configure_logging(level=logging.DEBUG)
    else:
        configure_logging()

    # load the network and get the images
    net = ac.load_default_classifier(input_size=IMG_SIZE)
    logging.info('Loaded network')
    # image_dict=dd.get_images();
    fr = FileReader()
    # run on an image here to get the height and width
    act_dict = dict()
    batch_size = 100
    n_hid_units = 4096
    wid = IMG_SIZE / 32  # 5 pooling layers before fc7 with kernel size 2
    hgh = IMG_SIZE / 32
    # initialize_dict(act_dict,batch_size,n_hid_units,wid,hgh)

    # best dict
    best_dict = dict()
    if (not args.load_best_cat_dict):
        logging.info('Initializing best dictionary from scratch')
        n_top = 50
        initialize_best_act_dict(best_dict, n_hid_units, n_top)
    else:
        logging.info('Loading previously saved cat dict file')
        with open(config.BEST_CAT_DICT_FILE, 'rb') as f:
            best_dict = p.load(f)
        print(best_dict.keys())
        print(best_dict[ACT])
        print(best_dict[IMG])

    cat = fr.getCategories()
    logging.info(cat)
    limit = 1
    for ct in cat[3:]:
        logging.info('Category: ' + ct + " Size = " +
                     str(fr.get_category_size(ct)))
        l = 0
        while fr.has_more(ct) and l < limit:
            imgs, idx = fr.getNextFiles(ct, batch_size)
            initialize_dict(act_dict, len(idx), n_hid_units, wid, hgh)
            j = 0
            for img in imgs:
                logging.info("Run for img " + str(idx[j]) + " shape = ")
                logging.info(img.shape)
                rgb, info = ac.colorize(img, classifier=net, return_info=True)
                activation = get_fc7_activations(net)
                # print("Append to dict")
                append_to_dict(act_dict, activation, j)
                # print("Save")
                if args.save_img:
                    plt.subplot(1, 2, 1)
                    plt.imshow(np.stack((img, img, img), -1))
                    plt.subplot(1, 2, 2)
                    plt.imshow(rgb)
                    plt.savefig(config.SAVE_IMG_DIR + str(idx[j]) + '.png')
                    # ac.image.save(config.SAVE_IMG_DIR+str(idx[j])+'.jpg',img)
                    # ac.image.save(config.SAVE_IMG_DIR+'color_'+str(idx[j])+'.jpg',rgb)
                # print("Saved")
                j += 1
            logging.info('Batch done, update best')
            update_best_dict(best_dict, act_dict, idx, wid)
            l += 1
        logging.info('Move to next category')
        with open(config.BEST_CAT_DICT_FILE, 'wb') as f:
            p.dump(best_dict, f)

    logging.info('All images done')

    with open('best_dict.p', 'wb') as f:
        p.dump(best_dict, f)