Ejemplo n.º 1
0
    def get_scores(self, positive, negative):
        scores = []
        for array in [positive, negative]:
            A_imgs = []
            B_imgs = []
            info = []
            for (image_A_name, image_B_name, label) in tqdm(array.values):
                img_A = load_image(image_A_name,
                                   input_shape=self.input_shape,
                                   mode='train')
                img_B = load_image(image_B_name,
                                   input_shape=self.input_shape,
                                   mode='train')
                A_imgs.append(img_A)
                B_imgs.append(img_B)

                info.append((image_A_name, image_B_name, label))

            A_tensor = np.stack(A_imgs)
            B_tensor = np.stack(B_imgs)

            A_embedding = self.encoder.predict(A_tensor)
            B_embedding = self.encoder.predict(B_tensor)
            distances = cdist(A_embedding, B_embedding,
                              metric='euclidean').diagonal()

            scores.extend(zip(distances, info))

        positive_scores, negative_scores = scores[:len(positive)], scores[
            len(positive):]
        return positive_scores, negative_scores
Ejemplo n.º 2
0
    def show_visualizations(self, scores, number_of_samples=None):
        random.shuffle(scores)
        counter = 0
        for row in scores:
            score, (image_A_name, image_B_name, label) = row

            if number_of_samples is not None and counter >= number_of_samples:
                break

            img_A = load_image(image_A_name,
                               input_shape=self.input_shape,
                               mode='train')
            img_B = load_image(image_B_name,
                               input_shape=self.input_shape,
                               mode='train')

            heatmap_A = self.get_heatmap_encoder_learnt(img_A)
            heatmap_B = self.get_heatmap_encoder_learnt(img_B)

            plot_side_by_side(
                [heatmap_A * 255, img_A, img_B, heatmap_B * 255],
                titles=[
                    'heatmap B', image_A_name + "\nSCORE: {}".format(score),
                    image_B_name + "\nThe same? : {}".format(label),
                    'heatmap B'
                ])
            counter += 1
Ejemplo n.º 3
0
def gen_img_pair_data(scene, pair_num, id_img2depth):
    # for each scene, for each image, gen pair of images
    K = np.loadtxt(DATA_PATH + scene + '/intrinsics.txt')
    extrinsic_file = pd_util.preprocess_util.list_files(DATA_PATH + scene +
                                                        '/extrinsics/')
    extrinsic_file.sort()
    extrinsic = np.reshape(np.loadtxt(extrinsic_file[-1]), (-1, 3, 4))
    # keep the original
    id_img2depth = OrderedDict(sorted(id_img2depth.items(),
                                      key=lambda t: t[0]))
    image_names = id_img2depth.keys()

    for i in range(0, len(image_names) - 30, 10):
        pair_id = np.random.choice(range(10, 30), 10, replace=False)
        for j in pair_id:
            image_path1 = DATA_PATH + scene + '/image/' + image_names[
                i] + '.jpg'
            image_path2 = DATA_PATH + scene + '/image/' + image_names[
                i + j] + '.jpg'
            depth_path1 = DATA_PATH + scene + '/depth/' + id_img2depth[
                image_names[i]] + '.png'
            depth_path2 = DATA_PATH + scene + '/depth/' + id_img2depth[
                image_names[i + j]] + '.png'

            # try:
            image1 = np.array(uts.load_image(image_path1))
            image2 = np.array(uts.load_image(image_path2))
            depth1 = uts.read_depth(depth_path1)
            depth2 = uts.read_depth(depth_path2)
            # except:
            #     continue

            print "image1 name: {}, image2 name: {} \
                   depth1 name: {}, depth2 name: {}".format(
                image_names[i], image_names[i + j],
                id_img2depth[image_names[i]], id_img2depth[image_names[i + j]])

            flow, is_valid = get_opt_flow(depth1, depth2, K,
                                          extrinsic[i, :, :],
                                          extrinsic[i + j, :, :], True, image1,
                                          image2)
            is_valid = False
            uts.plot_images(
                OrderedDict([('image1', image1), ('image2', image2),
                             ('flowu', flow[:, :, 0]), ('flowv', flow[:, :,
                                                                      1])]))

            # print is_valid
            if is_valid:
                flow_file = FLOW_PATH + scene + '/flow/' + \
                            image_names[i] + '_' + image_names[i + j] + '.pkl'
                print 'saving ' + flow_file
                with open(flow_file, 'wb') as f:
                    pkl.dump(flow, f, -1)
Ejemplo n.º 4
0
def understand_blend():
    inputs_path = os.path.join(os.path.dirname(__file__), 'data', 'input')
    img1 = utils.load_image(os.path.join(inputs_path, 'figures.jpg'),
                            (500, 500))
    img2 = utils.load_image(os.path.join(inputs_path, 'cloud.jpg'), (500, 500))

    for alpha in np.arange(0, 1.2, 0.2):
        blend = img1 + alpha * (
            img2 - img1
        )  # This is how PIL's blend works simple linear interpolation
        plt.imshow(blend)
        plt.show()
Ejemplo n.º 5
0
def understand_blend():
    img1 = utils.load_image(os.path.join(INPUT_DATA_PATH, 'figures.jpg'),
                            (500, 500))
    img2 = utils.load_image(os.path.join(INPUT_DATA_PATH, 'cloud.jpg'),
                            (500, 500))

    for alpha in np.arange(0, 1.2, 0.2):
        blend = img1 + alpha * (
            img2 - img1
        )  # This is how PIL's blend works simple linear interpolation
        plt.imshow(blend)
        plt.show()
Ejemplo n.º 6
0
def stylized_frames_mask_combiner(relevant_directories, dump_frame_extension, other_style=None):
    # in dirs
    frames_dir = relevant_directories['frames_path']
    mask_frames_dir = relevant_directories['processed_masks_dump_path']
    stylized_frames_dir = relevant_directories['stylized_frames_path']

    # out dirs (we'll dump combined imagery here)
    dump_path = os.path.join(stylized_frames_dir, os.path.pardir)
    model_name_suffix = '_' + os.path.basename(os.path.split(other_style)[0]) if other_style is not None else ''
    dump_path_bkg_masked = os.path.join(dump_path, 'composed_background_masked' + model_name_suffix)
    dump_path_person_masked = os.path.join(dump_path, 'composed_person_masked' + model_name_suffix)
    os.makedirs(dump_path_bkg_masked, exist_ok=True)
    os.makedirs(dump_path_person_masked, exist_ok=True)

    # if other_stylized_frames_path exists overlay frames are differently styled frames and not original frames
    if other_style is not None:
        overlay_frames_dir = other_style
    else:
        overlay_frames_dir = frames_dir

    if len(os.listdir(dump_path_bkg_masked)) == 0 and len(os.listdir(dump_path_person_masked)) == 0:
        for cnt, (name1, name2, name3) in enumerate(zip(sorted(os.listdir(stylized_frames_dir)), sorted(os.listdir(mask_frames_dir)), sorted(os.listdir(overlay_frames_dir)))):
            s_img_path = os.path.join(stylized_frames_dir, name1)  # stylized original frame image
            m_img_path = os.path.join(mask_frames_dir, name2)  # mask image
            o_img_path = os.path.join(overlay_frames_dir, name3)  # overlay image

            # load input imagery
            s_img = utils.load_image(s_img_path)
            m_img = utils.load_image(m_img_path, target_shape=s_img.shape[:2])
            o_img = utils.load_image(o_img_path, target_shape=s_img.shape[:2])

            # prepare canvas imagery
            combined_img_background = s_img.copy()
            combined_img_person = s_img.copy()

            # create masks
            background_mask = m_img == 0.
            person_mask = m_img == 1.

            # apply masks
            combined_img_background[background_mask] = o_img[background_mask]
            combined_img_person[person_mask] = o_img[person_mask]

            # save combined imagery
            combined_img_background_path = os.path.join(dump_path_bkg_masked, str(cnt).zfill(FILE_NAME_NUM_DIGITS) + dump_frame_extension)
            combined_img_person_path = os.path.join(dump_path_person_masked, str(cnt).zfill(FILE_NAME_NUM_DIGITS) + dump_frame_extension)
            cv.imwrite(combined_img_background_path, (combined_img_background * 255).astype(np.uint8)[:, :, ::-1])
            cv.imwrite(combined_img_person_path, (combined_img_person * 255).astype(np.uint8)[:, :, ::-1])
    else:
        print('Skipping combining with masks, already done.')

    return {"dump_path_bkg_masked": dump_path_bkg_masked, "dump_path_person_masked": dump_path_person_masked}
Ejemplo n.º 7
0
    def __init__(self):
        super(Base, self).__init__()
        self.net = VGG().to(opt.device).eval()
        self.content_image = load_image(opt.content_image_path).to(opt.device)
        self.style_image = load_image(opt.style_image_path).to(opt.device)
        self.out_image = self.content_image.clone().requires_grad_(True)
        self.optimizer = optim.Adam([self.out_image], opt.learning_rate)

        ## Hyper Pramaters
        self.alpha = opt.alpha
        self.beta = opt.beta
        self.model = {'basemodel': self.net}
        self.visual_names = ['total_loss']
Ejemplo n.º 8
0
def getCk(in_dir, label_dir, csv_file):
    classes = get_classes(csv_file)
    input_images = sorted(os.listdir(in_dir))

    height, width, _ = utils.load_image(in_dir + "/" + input_images[0]).shape

    #height = 2
    #width = 2

    for k in range(len(classes)):
        cnt = np.zeros((height, width))
        class_representative = np.zeros((height, width, 3))
        for f in input_images:
            print("Reading " + f)
            image_path = in_dir + "/" + f
            label_path = label_dir + "/" + f
            image = cv2.imread(image_path, -1)
            label = cv2.imread(label_path, -1)
            #print(label)
            for h in range(height):
                for w in range(width):
                    if label[h, w, 2] == k:
                        class_representative[h, w] += image[h, w]
                        cnt[h, w] += 1

        for h in range(height):
            for w in range(width):
                if cnt[h, w] > 0:
                    class_representative[
                        h, w] = class_representative[h, w] / cnt[h, w]
        img = Image.fromarray(class_representative.astype(np.uint8))
        img.save("C" + str(k) + ".png")
        print("saved...")
Ejemplo n.º 9
0
def stylize(args):
    img_list=os.listdir(args.content_image)
    epoch_name=os.path.basename(args.model).split('.')[0]
    experiment_name=os.path.dirname(args.output_image)
    if not os.path.exists(experiment_name):
        os.system('mkdir {}'.format(experiment_name))
    if not os.path.exists(args.output_image):
        os.system('mkdir {}'.format(args.output_image))
    for img in img_list:
        if is_image_file(img):
            content_image = utils.load_image(os.path.join(args.content_image,img), scale=args.content_scale)
            content_image=content_image.convert('RGB')
            content_transform = transforms.Compose([
                transforms.ToTensor(),
                transforms.Lambda(lambda x: x.mul(255))
            ])
            content_image = content_transform(content_image)
            content_image = content_image.unsqueeze(0)
            if args.cuda:
                content_image = content_image.cuda()
            content_image = Variable(content_image, volatile=True)

            style_model = TransformerNet()
            style_model.load_state_dict(torch.load(args.model))
            if args.cuda:
                style_model.cuda()
            output = style_model(content_image)
            if args.cuda:
                output = output.cpu()
                content_image=content_image.cpu()
            output_data = output.data[0]
            content_image_data=content_image.data[0]
            output_data=torch.cat([content_image_data,output_data],2)
            output_name=os.path.join(args.output_image,epoch_name+"_result_"+img)
            utils.save_image(output_name, output_data)
Ejemplo n.º 10
0
Archivo: virus.py Proyecto: csr13/dtv
    def death_scene(self):
        """<Dr@m@tiK Sc3Ne>"""

        position = self.get_current_position()
        self.image = load_image("x_x.gif")
        self.rect = self.image.get_rect()
        self.rect[0], self.rect[1] = position
Ejemplo n.º 11
0
    def get_embeddings(self):
        all_unique_whales = pd.read_csv(
            os.path.join(DATA_DIR, 'validation_all.csv'))
        selected_whales_ids = all_unique_whales.groupby('Id')
        selected_whales_ids_with_most_occurances = list(
            selected_whales_ids.count().sort_values(
                'Image', ascending=False)[:15].index)

        selected_whales = all_unique_whales[all_unique_whales['Id'].isin(
            selected_whales_ids_with_most_occurances)]
        selected_whales = selected_whales[selected_whales['Id'] != 'new_whale']

        imgs = []
        classes = []
        classes_names = {}
        for image_name, class_name in selected_whales.values:
            img = load_image(image_name,
                             input_shape=self.input_shape,
                             mode='train')
            imgs.append(img)
            if class_name not in classes_names.keys():
                new_class = len(classes_names.keys()) + 1
                classes_names[class_name] = new_class
                classes.append(new_class)
            else:
                classes.append(classes_names[class_name])

            if len(classes_names.keys()) == 20:
                break

        tensor = np.stack(imgs)
        preprocessed_tensor = preprocess_input(tensor, self.model_name)
        embeddings = self.encoder.predict(preprocessed_tensor)
        return embeddings, np.array(classes), classes_names
Ejemplo n.º 12
0
    def __data_generator(self, batch_samples):
        # initialize images and labels tensors for faster processing
        X = np.empty((len(batch_samples), *self.img_crop_dims, 3))
        y = np.empty((len(batch_samples), self.n_classes))

        for i, sample in enumerate(batch_samples):
            # load and randomly augment image
            img_file = os.path.join(
                self.img_dir, '{}.{}'.format(sample['image_id'],
                                             self.img_format))

            img = utils.load_image(img_file, self.img_load_dims)
            if img is not None:
                img = utils.random_crop(img, self.img_crop_dims)
                img = utils.random_horizontal_flip(img)
                X[i, ] = img

            # normalize labels
            y[i, ] = utils.normalize_labels(sample['label'])

        # apply basenet specific preprocessing
        # input is 4D numpy array of RGB values within [0, 255]
        X = self.basenet_preprocess(X)

        return X, y
Ejemplo n.º 13
0
def deep_dream_video_ouroboros(config):
    """
    Feeds the output dreamed image back to the input and repeat

    Name etymology for nerds: https://en.wikipedia.org/wiki/Ouroboros

    """
    ts = time.time()
    assert any([config['input_name'].lower().endswith(img_ext) for img_ext in SUPPORTED_IMAGE_FORMATS]), \
        f'Expected an image, but got {config["input_name"]}. Supported image formats {SUPPORTED_IMAGE_FORMATS}.'

    utils.print_ouroboros_video_header(config)  # print some ouroboros-related metadata to the console

    img_path = utils.parse_input_file(config['input'])
    # load numpy, [0, 1] range, channel-last, RGB image
    # use_noise and consequently None value, will cause it to initialize the frame with uniform, [0, 1] range, noise
    frame = None if config['use_noise'] else utils.load_image(img_path, target_shape=config['img_width'])

    for frame_id in range(config['ouroboros_length']):
        print(f'Ouroboros iteration {frame_id+1}.')
        # Step 1: apply DeepDream and feed the last iteration's output to the input
        frame = deep_dream_static_image(config, frame)
        dump_path = utils.save_and_maybe_display_image(config, frame, name_modifier=frame_id)
        print(f'Saved ouroboros frame to: {os.path.relpath(dump_path)}\n')

        # Step 2: transform frame e.g. central zoom, spiral, etc.
        # Note: this part makes amplifies the psychodelic-like appearance
        frame = utils.transform_frame(config, frame)

    video_utils.create_video_from_intermediate_results(config)
    print(f'time elapsed = {time.time()-ts} seconds.')
 def __init__(self,
              image_path,
              mask_path,
              low_threshold,
              high_threshold,
              image_shape=(224, 224, 3),
              augment=True):
     self.image_path = image_path
     self.mask_path = mask_path
     self.low_threshold = np.log2(low_threshold)
     self.high_threshold = np.log2(high_threshold)
     self.image_shape = image_shape
     self.image = utils.load_image(self.image_path, image_shape)
     self.mask = utils.clean_mask(
         utils.load_image(self.mask_path, image_shape))
     self.augment = augment
Ejemplo n.º 15
0
def crop_and_save(img_paths, height, width, directories, with_label=False):
    with progressbar.ProgressBar(max_value=len(img_paths)) as bar:
        for index, img_path in enumerate(img_paths):
            img_path = str(img_path)

            img = utils.load_image(img_path)
            date_time = utils.get_date_from_metadata(img)
            image_save_directory = directories[0] + date_time

            if not os.path.exists(image_save_directory):
                os.makedirs(image_save_directory)
            image = np.asarray(img)

            # if with_label:
            # json_path =args.main_jsons+ name+".json"
            #     label_points = utils.list_from_dict(dict_from_json(json_path)[0]['Label']['cranberry'])
            # mask = utils.mask_from_list(label_points,3648,4864)
            # img_mask_pair = join_img_label(img,mask)
            #   # label_name = "/"+str(index) + "_" + date_time + "_label"
            #     label_save_direcotry = directories[1] + date_time+"_label"
            #     cropped,cropped_label = utils.crop_and_save_single(image,height,width,with_label=True)
            # else:
            cropped = utils.crop_and_save_single(image,
                                                 height,
                                                 width,
                                                 image_save_directory,
                                                 str(index) + "_" + date_time,
                                                 with_label=False)

            bar.update(1)
Ejemplo n.º 16
0
 def predict(self, file, is_base64=False):
     start = time.time()
     if is_base64:
         image = image_from_base64(file)
     else:
         image = load_image(file)
     cccd_result = self._predictcccd(image)
     if cccd_result is not None and 'id' in cccd_result and 'full_name' in cccd_result:
         return build_result(file, cccd_result, "CCCD", start)
     else:
         # print("check image is cmnd")
         cmnd_result = self.predictcmnd(image)
         if cmnd_result is not None and 'id' in cmnd_result:
             return build_result(file, cmnd_result, "CMND", start)
         else:
             # print("Not detect image")
             return {
                 # 'urlimg':"",
                 'idc': "",
                 'fullname': "",
                 'dob': "",
                 'sex': '',
                 # 'ethnicity': '',
                 'national': '',
                 'country': '',
                 'address': '',
                 'score': 0,
                 'typekyc': "None",
                 'totaltime': round(time.time() - start)
             }
Ejemplo n.º 17
0
 def compute_gram_style(self, size):
     style = utils.load_image(self.style_image_path, size=size)
     style = self.style_transform(style)
     style = style.repeat(self.opt.batch_size, 1, 1, 1).to(self.device)
     features_style = self.vgg.get_features(style, 'style')
     gram_style = [self.gram_matrix(y) for y in features_style['style']]
     return gram_style
Ejemplo n.º 18
0
def deep_dream_video(config):
    video_path = os.path.join(config['inputs_path'], config['input'])
    tmp_input_dir = os.path.join(config['out_videos_path'], 'tmp_input')
    tmp_output_dir = os.path.join(config['out_videos_path'], 'tmp_out')
    config['dump_dir'] = tmp_output_dir
    os.makedirs(tmp_input_dir, exist_ok=True)
    os.makedirs(tmp_output_dir, exist_ok=True)

    metadata = video_utils.dump_frames(video_path, tmp_input_dir)

    last_img = None
    for frame_id, frame_name in enumerate(sorted(os.listdir(tmp_input_dir))):
        print(f'Processing frame {frame_id}')
        frame_path = os.path.join(tmp_input_dir, frame_name)
        frame = utils.load_image(frame_path, target_shape=config['img_width'])
        if config['blend'] is not None and last_img is not None:
            # 1.0 - get only the current frame, 0.5 - combine with last dreamed frame and stabilize the video
            frame = utils.linear_blend(last_img, frame, config['blend'])

        dreamed_frame = deep_dream_static_image(config, frame)
        last_img = dreamed_frame
        utils.save_and_maybe_display_image(
            config,
            dreamed_frame,
            should_display=config['should_display'],
            name_modifier=frame_id)

    video_utils.create_video_from_intermediate_results(config, metadata)

    shutil.rmtree(tmp_input_dir)  # remove tmp files
    print(f'Deleted tmp frame dump directory {tmp_input_dir}.')
Ejemplo n.º 19
0
    def death_scene(self):
        """Dramatic scene."""

        position = self.get_current_position()
        self.image = load_image("x_x.gif")
        self.rect = self.image.get_rect()
        self.rect[0], self.rect[1] = position
        self.dead = True
Ejemplo n.º 20
0
 def load_image(self, image_name, mode):
     """
     Load image and convert to Numpy array with values between 0 and 1
     :param image_name: String with image name for ALL_DIR directory
     :param mode: String "train" or "test", depends on which folder take images from
     :return: Numpy array with values between 0 and 1 with shape self.train_conf.input_shape
     """
     return load_image(image_name, self.train_conf.input_shape, mode)
 def load_feature_style(self):
     image = load_image(os.path.join(self.style_dir, self.style_image_name), size=self.image_size)
     image = transforms.Compose([
         transforms.CenterCrop(min(image.size[0], image.size[1])),
         transforms.Resize(self.image_size),
         transforms.ToTensor(),
     ])(image)
     image = image.repeat(self.batch_size, 1, 1, 1)
     self.style_image = image.to(self.device)
Ejemplo n.º 22
0
def predict_folder(model, folder_path):
    image_list = utils.recursive_find_images(args.image,
                                             extensions=('.jpg', '.png',
                                                         '.jpeg'))
    results = []
    for image_path in image_list:
        image = utils.load_image(image_path)
        pred = predict_single_image(model, image_path)
        results.append((image, pred))
    return results
    def test_model(self):
        inputs = []
        images = []
        file_paths = list_files_in_directory('test_resources/*.jpg')
        for file_path in file_paths:
            image_array = load_image(file_path, False, target_size=(300, 300))
            images.append(imread(file_path))
            inputs.append(image_array)
        inputs = np.asarray(inputs, dtype='float32')
        inputs = preprocess_input(inputs)
        predictions = self.model.predict(inputs, batch_size=1, verbose=1)
        prior_box_manager = PriorBoxManager(self.prior_boxes,
                            box_scale_factors=[.1, .1, .2, .2])
        results = prior_box_manager.detection_out(predictions)
        for i, img in enumerate(images):
            # Parse the outputs.
            det_label = results[i][:, 0]
            det_conf = results[i][:, 1]
            det_xmin = results[i][:, 2]
            det_ymin = results[i][:, 3]
            det_xmax = results[i][:, 4]
            det_ymax = results[i][:, 5]

            # Get detections with confidence higher than 0.6.
            top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.8]

            top_conf = det_conf[top_indices]
            top_label_indices = det_label[top_indices].tolist()
            top_xmin = det_xmin[top_indices]
            top_ymin = det_ymin[top_indices]
            top_xmax = det_xmax[top_indices]
            top_ymax = det_ymax[top_indices]

            colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()

            plt.imshow(img / 255.)
            currentAxis = plt.gca()

            for i in range(top_conf.shape[0]):
                xmin = int(round(top_xmin[i] * img.shape[1]))
                ymin = int(round(top_ymin[i] * img.shape[0]))
                xmax = int(round(top_xmax[i] * img.shape[1]))
                ymax = int(round(top_ymax[i] * img.shape[0]))
                score = top_conf[i]
                label = int(top_label_indices[i])
                #label_name = voc_classes[label - 1]
                #label_name = self.class_names[label - 1]
                label_name = self.class_names[label]
                display_txt = '{:0.2f}, {}'.format(score, label_name)
                coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
                color = colors[label]
                currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
                currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
            plt.show()
Ejemplo n.º 24
0
def deep_dream_static_image(config, img):
    device = torch.device("cuda" if torch.cuda.is_available() else
                          "cpu")  # checking whether you have a GPU

    model = utils.fetch_and_prepare_model(config['model'],
                                          config['pretrained_weights'], device)
    try:
        layer_ids_to_use = [
            model.layer_names.index(layer_name)
            for layer_name in config['layers_to_use']
        ]
    except Exception as e:  # making sure you set the correct layer name for this specific model
        print('Invalid layer name!')
        print(
            f'Available layers for model {config["model"].name} are {model.layer_names}.'
        )
        exit(0)

    if img is None:  # load either image or start from pure noise image
        img_path = os.path.join(config['inputs_path'], config['input'])
        img = utils.load_image(img_path, target_shape=config['img_width']
                               )  # load numpy, [0, 1], channel-last, RGB image
        if config['use_noise']:
            shape = img.shape
            img = np.random.uniform(low=0.0, high=1.0,
                                    size=shape).astype(np.float32)

    img = utils.preprocess_numpy_img(img)
    base_shape = img.shape[:-1]  # save initial height and width

    # Note: simply rescaling the whole result (and not only details, see original implementation) gave me better results
    # Going from smaller to bigger resolution (from pyramid top to bottom)
    for pyramid_level in range(config['pyramid_size']):
        new_shape = utils.get_new_shape(config, base_shape, pyramid_level)
        img = cv.resize(img, (new_shape[1], new_shape[0]))
        input_tensor = utils.pytorch_input_adapter(img, device)

        for iteration in range(config['num_gradient_ascent_iterations']):
            h_shift, w_shift = np.random.randint(
                -config['spatial_shift_size'],
                config['spatial_shift_size'] + 1, 2)
            input_tensor = utils.random_circular_spatial_shift(
                input_tensor, h_shift, w_shift)

            gradient_ascent(config, model, input_tensor, layer_ids_to_use,
                            iteration)

            input_tensor = utils.random_circular_spatial_shift(
                input_tensor, h_shift, w_shift, should_undo=True)

        img = utils.pytorch_output_adapter(input_tensor)

    return utils.post_process_numpy_image(img)
Ejemplo n.º 25
0
 def save_evaluation_images(self, epoch, alpha=0, latest=False):
     image_names = os.listdir(self.opt.evaluation_images_path)
     for ind, image_path in enumerate(image_names):
         eval_image = utils.load_image(os.path.join(self.opt.evaluation_images_path, image_path), scale=2)
         eval_tensor = self.eval_transform(eval_image).to(self.device).expand(1, -1, -1, -1)
         output_tensor = self.recover_tensor(self.forward(eval_tensor, alpha_0=alpha).squeeze(dim=0)).clamp(min=0.0, max=1).cpu()
         if latest:
             save_name = 'latest_img_%d_alpha_%.3f.jpeg' % (ind, alpha)
         else:
             save_name = 'epoch_%d_img_%d_alpha_%.3f.jpeg' % (epoch, ind, alpha)
         save_path = os.path.join(self.opt.images_save_dir, save_name)
         utils.save_tensor_as_image(save_path, output_tensor)
 def calculate_MAP(self, model):
     for test_key in self.test_keys:
         image_path = self.path_prefix + test_key
         image_array = load_image(image_path, False, self.image_size)
         image_array = np.expand_dims(image_array, axis=0)
         image_array = preprocess_images(image_array)
         predicted_data = self.model.predict(image_array)
         predicted_data = np.squeeze(predicted_data)
         original_image_size = None
         predicted_boxes = self._decode_predictions(predicted_data,
                                                    original_image_size)
         ground_truth_box_data = self.ground_truth_data[test_key].copy()
Ejemplo n.º 27
0
def deep_dream_video_ouroboros(config):
    img_path = os.path.join(config['inputs_path'], config['input'])
    # load numpy, [0, 1], channel-last, RGB image, None will cause it to start from the uniform noise [0, 1] image
    frame = None if config['use_noise'] else utils.load_image(img_path, target_shape=config['img_width'])

    for frame_id in range(config['video_length']):
        print(f'Dream iteration {frame_id+1}.')
        frame = deep_dream_static_image(config, frame)
        utils.save_and_maybe_display_image(config, frame, should_display=config['should_display'], name_modifier=frame_id)
        frame = utils.transform_frame(config, frame)  # transform frame e.g. central zoom, spiral, etc.

    video_utils.create_video_from_intermediate_results(config)
Ejemplo n.º 28
0
def main(args):
    model, input_shape = utils.load_model_get_shape(args.model)
    print(f'Loaded model {args.model}')
    if args.dataset is not None:
        print('Running inference on validation dataset')
        results = predict_dataset(model, args.dataset)
        visualise_gt(results, args.save_images)
    elif os.path.isdir(args.image):
        results = predict_folder(model, args.image)
    else:
        image = utils.load_image(args.image)
        results = predict_single_image(model, image)
Ejemplo n.º 29
0
    def state(self, key, screen):
        """Keylogger"""

        self.shield = False
        self.rotate = False

        if key[K_SPACE]:
            if not self.shield_ok():
                self.shield = False
            elif self.shield_ok():
                self.shield = True
                self.rotate = True

        if key[K_DOWN]:
            if not self.rect.y > 369:
                self.rect = self.rect.move(0, self.speed)

        if key[K_UP]:
            if not self.rect.y < 82:
                self.rect = self.rect.move(0, -self.speed)

        if key[K_RIGHT]:
            self.image = load_image("unicorn.png")
            copy_pos = self.get_current_position()
            self.rect = self.image.get_rect()
            self.rect.x, self.rect.y = copy_pos

            if not self.rect.x > 680:
                self.rect = self.rect.move(self.speed, 0)

        if key[K_LEFT]:
            self.image = load_image("left_unicorn.png")
            copy_pos = self.get_current_position()
            self.rect = self.image.get_rect()
            self.rect.x, self.rect.y = copy_pos

            if not self.rect.x < 20:
                self.rect = self.rect.move(-self.speed, 0)
Ejemplo n.º 30
0
    def pull_image(self, index):
        """Returns the original image object at index in PIL form

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.

        Argument:
            index (int): index of img to show
        Return:
            PIL img
        """
        img_id = self.ids[index]
        img = load_image(self._imgpath / (img_id.stem + '.jpg'))
        return img
    def __data_generator(self, batch_samples):
        # initialize images and labels tensors for faster processing
        X = np.empty((len(batch_samples), *self.img_load_dims, 3))
        y = np.empty((len(batch_samples), self.n_classes))

        for i, sample in enumerate(batch_samples):
            # load and randomly augment image
            img_file = os.path.join(self.img_dir, '{}.{}'.format(sample['image_id'], self.img_format))
            img = utils.load_image(img_file, self.img_load_dims)
            if img is not None:
                X[i, ] = img

            # normalize labels
            if sample.get('label') is not None:
                y[i, ] = utils.normalize_labels(sample['label'])

        # apply basenet specific preprocessing
        # input is 4D numpy array of RGB values within [0, 255]
        X = self.basenet_preprocess(X)

        return X, y