Пример #1
0
 def get_screen(self):
     if (self.env_type == "Unity"):
         return img_to_tensor(self.env.get_screen())
     elif (self.env_type == "Gridworld"):
         return img_to_tensor(np.expand_dims(self.env.renderEnv(), axis=3))
         #return self.env.renderEnv()
     else:
         # Gym
         return self.gym_screen_processing.get_screen()
Пример #2
0
    def __getitem__(self, idx):
        if self.beast:
            img = self.imgs[idx]
        else:
            sample_data = self.data.iloc[idx]
            img_dir = self.prefix + sample_data['Image Index']
            img = cv2.imread(img_dir, 0)  # grayscale
            img = cv2.resize(img, (320, 320))

        augmented_img = self.augment(img.copy())

        img, augmented_img = img_to_tensor(img), img_to_tensor(augmented_img)
        return img, augmented_img
Пример #3
0
 def _augment_imgs(self, x):
     augmented_imgs = []
     for _ in range(self.n_predictions):
         augmented_img = img_to_tensor(self.augmentations(x)).float().cuda()
         augmented_img.unsqueeze_(0)
         augmented_imgs.append(augmented_img)
     augmented_imgs = torch.cat(augmented_imgs)
     return augmented_imgs
Пример #4
0
def preprocess(img_name, quality=None):
    """Reads an image, applies JPEG compression if specified, and convert it to a torch tensor."""
    img = plt.imread(img_name)
    Y, X, C = img.shape
    img = img[:Y-Y%2, :X-X%2, :3]
    if quality is not None:
        img = jpeg_compress(img, quality)
    img = img_to_tensor(img).cuda().type(torch.float)
    return img
Пример #5
0
 def __getitem__(self, idx):
     if self.beast:
         img = self.imgs[idx].copy()
     else:
         sample_data = self.data.iloc[idx]
         img_dir = self.prefix + sample_data.Path
         img = cv2.imread(img_dir, 0)  # grayscale
     img = cv2.resize(img, (320, 320))
     # apply augmentations if not in UDA mode
     if not self.uda and self.augmentations:
         img = self.augment(img)
     if not self.raw:
         img = img_to_tensor(img)
     label = self.labels[idx]
     return img, label
Пример #6
0
import utils as ut
import random
import sys

if __name__ == '__main__':
    '''Command line args:
        argv[1]: partial directory to image(fruit_name/img.jpg)
        argv[2]: label of image
        argv[3]: size of square used for occlusion
    '''
    assert (len(sys.argv) == 4)
    img_path, label, occl_size = '/scidata/fruits-360/Test/' + str(
        sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])
    device = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
    model = ut.load_model(device)
    img = ut.img_to_tensor(img_path)
    '''For each pixel in the input image making occlusion with
        square centered in that pixel filled with zeros.
        For such new image making forward pass and saving 
        softmax score in corresponding place in heatmap.
    '''
    _, _, h, w = img.shape
    heatmap = torch.zeros(h, w)
    r = occl_size // 2

    for y in range(h):
        for x in range(w):
            h_start = max(0, y - r)
            h_end = min(h, y + r)
            w_start = max(0, x - r)
            w_end = min(w, x + r)
Пример #7
0
 image_name = args.input
 block_size = args.block_size
 quality = args.jpeg
 model = args.model
 out = args.out
 confidences = {}
 net = FullNet().cuda()
 net.load_state_dict(torch.load(model))
 img = plt.imread(image_name)
 Y_o, X_o, C = img.shape
 img = img[:Y_o - Y_o % 2, :X_o - X_o % 2, :3]
 if img.max() > 1:
     img /= 255
 if quality is not None:
     img = jpeg_compress(img, quality)
 img_t = img_to_tensor(img).cuda().type(torch.float)
 res = np.exp(net(img_t, block_size).detach().cpu().numpy())
 res[:, 1] = res[([1, 0, 3, 2], 1)]
 res[:, 2] = res[([2, 3, 0, 1], 2)]
 res[:, 3] = res[([3, 2, 1, 0], 3)]
 res = np.mean(res, axis=1)
 best_grid = np.argmax(np.mean(res, axis=(1, 2)))
 authentic = np.argmax(res, axis=(0)) == best_grid
 confidence = 1 - np.max(res, axis=0)
 confidence[confidence < 0] = 0
 confidence[confidence > 1] = 1
 confidence[authentic] = 1
 if out is not None:
     error_map = 1 - confidence  # highest values (white) correspond to suspected forgeries
     # Resample the output to match the original image size
     error_map = np.repeat(np.repeat(error_map, block_size, axis=0),
Пример #8
0
action_shape = 1
rollouts = RolloutStorage(num_steps=args.num_steps,
                          num_processes=1,
                          obs_shape=obs_shape)
step = 0
episode = 0
ppo_update = 0
total_reward = 0
done = True

while True:  # nb episodes
    if done:
        env_info = env.reset(train_mode=train_mode,
                             config=config)[default_brain]
    obs = env_info.observations[0]
    obs = img_to_tensor(obs)
    while True:  # nb of steps
        action, action_log_prob, value = agent.act(obs)
        #action_cpu = action.data.numpy()
        action_cuda = action.data.cpu().numpy()
        #print(action_cuda)

        env_info = env.step(action_cuda)[default_brain]
        done = env_info.local_done[0]
        reward = torch.cuda.FloatTensor([env_info.rewards[0]])
        total_reward += env_info.rewards[0]

        mask = 0 if env_info.local_done[0] else 1
        mask = torch.cuda.FloatTensor([mask])
        rollouts.insert(step, obs.data, action.data, action_log_prob.data,
                        value.data, reward, mask)
Пример #9
0
 def get_screen(self):
     if (self.env_type == "Unity"):
         return img_to_tensor(self.env.get_screen())
     else:
         # Gym
         return self.gym_screen_processing.get_screen()