def combining_imageWith_fixations(filtered_experiment_class, 
                        dir_images,
                        memory,
                        search_type):
    """Create Tripplets  [Imagename (String),
    EyeMovements (DataFrame), Imagepath (path)]
    
    
    Argument:
        filtered_experiment_class: (DataFrame) Eye Movements
        
        dir_images: (Path)      Image Directory
        
        memory: (String)        memory or search
        
        search_type: (String)  (String)
                               'target_not_present'     
                               'expectedlocation'        
                               'randomlocation'  
        
    Returns:
        Tripplets (Lists)
    
    """
    path =0
    image_paths = get_img_paths(dir_images)
    
    list_of_masks = []
        
    for i, image in enumerate(np.unique(filtered_experiment_class["imageid"]) ):  
        for image_path in image_paths:
            if memory ==1:
                #in case of memory
                if int(re.findall(r'\d+',
                                  os.path.basename(image_path))[0]) ==  image:
                    path = image_path
            elif memory==0:
                
                if search_type == "target_not_present":
                    match = "L_"
                elif search_type == 'expectedlocation':
                    match = "E_"
                else:
                    match = "U_"
                                   
                #in case of search
                if  int(re.findall(r'\d+', 
           os.path.basename(image_path))[0]) == image and match in image_path:
                    path = image_path

        dataframe_temp =\
        filtered_experiment_class[filtered_experiment_class["imageid"] == image]
        
        if path==0:
            raise ValueError("wrong image path !")
        
        list_of_masks.append([str(i+1)+".png",
                             path,
                             dataframe_temp])
    return list_of_masks
Exemplo n.º 2
0
def load_images_and_masks(main_dir, height, width, br=None):
    """Load images and masks into numpy arrays.

  Arguments:
    main_dir: directory containing images and masks in the subdirectories
    images and masks

    height: target image height for resizing

    width: target image width for resizing

    br: number of images and masks to load

  Returns:
    Images and masks as a tuple of two numpy arrays.
  """
    all_image_paths = get_img_paths(main_dir + "/images/")
    all_mask_paths = get_img_paths(main_dir + "/masks/")

    image_ids = [os.path.basename(i) for i in all_image_paths]
    mask_ids = [os.path.basename(i) for i in all_mask_paths]

    if br:
        image_ids = image_ids[:br]
        mask_ids = mask_ids[:br]

    image_list = []
    mask_list = []
    for n, (image_id, mask_id) in tqdm(enumerate(zip(image_ids, mask_ids)),
                                       total=len(image_ids)):
        try:
            image = read_image(main_dir + '/images/' + image_id, height, width)
            mask = read_mask(main_dir + '/masks/' + mask_id, height, width)
        except Exception as e:
            print('Skipped image and mask:', n, image_id, mask_id, e)
            continue
        image_list.append(np.float16(image / 255))
        mask_list.append(np.float16(mask / 255))

    X = np.stack(image_list)
    Y = np.stack(mask_list)
    return X, Y
Exemplo n.º 3
0
def load_images_and_masks(path, down_size=2, br=100000):
    """Load the images and preprocess for the training and prediction
  
  input: path to the images and mask in two seperate directorys
  
  returns:  images, masks
  """
    all_image_paths = get_img_paths(path + "/images/")
    train_ids = [os.path.basename(i) for i in all_image_paths]

    X_images = np.zeros((len(train_ids), int(
        IMG_HEIGHT / down_size), int(IMG_WIDTH / down_size), 3),
                        dtype=np.float)
    Y_images = np.zeros((len(train_ids), int(
        IMG_HEIGHT / down_size), int(IMG_WIDTH / down_size), 1),
                        dtype=np.float)

    i = 0
    for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):
        image = np.zeros((IMG_HEIGHT, IMG_WIDTH, 3), dtype=np.float)

        try:
            img = imread(path + "/images/" + id_)

            if len(img.shape) == 3:
                image = img[:, :, :3]
            else:
                image[:, :, 0] = img[:, :]
                image[:, :, 1] = img[:, :]
                image[:, :, 2] = img[:, :]

            image = resize(
                image,
                (int(IMG_HEIGHT / down_size), int(IMG_WIDTH / down_size)),
                mode='constant',
                preserve_range=True)

            mask = imread(path + "/masks/" + id_)
            mask = np.expand_dims(resize(
                mask,
                (int(IMG_HEIGHT / down_size), int(IMG_WIDTH / down_size)),
                mode='constant',
                preserve_range=True),
                                  axis=-1)

            X_images[i] = image / 255
            Y_images[i] = mask / 255
            i += 1
            if i == br: break

        except:
            print("ERROR")

    return X_images, Y_images
Exemplo n.º 4
0
def load_data(path, down_size=16):
    """
    """
    #loading data
    img_height = 768
    img_width = 1024
    img_channels = 3

    # Get train and test IDs
    train_ids = [os.path.basename(i) for i in get_img_paths(path + "images")]

    # Get and resize train images and masks
    X_train = np.zeros((len(train_ids), int(
        img_height / down_size), int(img_width / down_size), img_channels),
                       dtype=np.float)
    Y_train = np.zeros((len(train_ids), int(
        img_height / down_size), int(img_width / down_size), 1),
                       dtype=np.float)
    i = 0

    sys.stdout.flush()
    for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):

        try:
            img = imread(path + "images" + "/" + id_)[:, :, :img_channels]
        except:
            return X_train, Y_train

        try:
            mask = imread(path + "masks/" + id_)
        except:
            return X_train, Y_train

        img = resize(img,
                     (int(img_height / down_size), int(img_width / down_size)),
                     mode='constant',
                     preserve_range=True)
        mask = np.expand_dims(resize(
            mask, (int(img_height / down_size), int(img_width / down_size)),
            mode='constant',
            preserve_range=True),
                              axis=-1)

        #X_train[n] = img / 255
        # Y_train[n] = mask / 255

        X_train[n] = img / 255
        Y_train[n] = mask / 255

    return X_train, Y_train
def get_image_objects_memory(filtered_search_data, dir_images):
    """creating objects each pictures with different experiment types


  input: filtered_data:      Dataframe of the Experients,
         dir_images:         Directory of the images


  return: objects of each image with eyemovements and pictures

  """
    #get the image paths
    image_paths = get_img_paths(dir_images)

    original_paths = [
        i for i in image_paths if "original" in i and "._O" not in i
    ]
    memory_paths = [i for i in original_paths if "memory" in i]

    list_of_exp = classify_data(filtered_memory_data)

    list_of_masks = []

    for u in range(len(list_of_exp)):

        for i in range(len(np.unique(list_of_exp[u][0]["imageid"]))):
            if list_of_exp[u][3] == "memory":

                for z, image_path in enumerate(memory_paths):

                    if list_of_exp[u][2] in image_path:

                        if os.path.basename(image_path) == str(i + 1) + ".png":
                            path = image_path

            dataframe_temp = list_of_exp[u][0][list_of_exp[u][0]["imageid"] ==
                                               i + 1]

            if dataframe_temp.shape[0]:
                if list_of_exp[u][3] == "memory":
                    list_of_masks.append([
                        list_of_exp[u][0][list_of_exp[u][0]["imageid"] == i +
                                          1], list_of_exp[u][1],
                        list_of_exp[u][2], list_of_exp[u][3], path,
                        dataframe_temp.shape[0]
                    ])

    return list_of_masks
Exemplo n.º 6
0
def rotate_allimages(path, degree):
  """Rotate every images in the given directory, with the given degree
  
  input: directory path, degree to rotate
  """
  image_paths = get_img_paths(path)  
  
  # iterate through the names of contents of the folder
  for image_path in image_paths:
    # create the full input path and read the file
    image_to_rotate = ndimage.imread(image_path)
    # rotate the image
    rotated = ndimage.rotate(image_to_rotate, degree) 
    # create full output path, 'example.jpg' 
    # becomes 'rotate_example.jpg', save the file to disk
    #fullpath = os.path.join(outPath, 'rotated_'+image_id)
    misc.imsave(image_path, rotated)
Exemplo n.º 7
0
def imageWith_fixations(filtered_experiment_class, dir_images, memory,
                        search_type):
    """This function combinates the experiment DataFrame with Image

    Argument:
        DataFrame: filtered Experiment class
        Directory: Image Directory
    Returns:
        List of connected imagepaths with fixation DataFrames

    """
    image_paths = get_img_paths(dir_images)

    list_of_masks = []

    for i, image in enumerate(np.unique(filtered_experiment_class["imageid"])):
        for image_path in image_paths:
            if memory == 1:
                #in case of memory
                if int(re.findall(r'\d+',
                                  os.path.basename(image_path))[0]) == image:
                    path = image_path
            elif memory == 0:

                if search_type == "target_not_present":
                    match = "L_"
                elif search_type == 'expectedlocation':
                    match = "E_"
                else:
                    match = "U_"

                #in case of search
                if int(re.findall(r'\d+', os.path.basename(image_path))
                       [0]) == image and match in image_path:
                    path = image_path

        dataframe_temp = filtered_experiment_class[
            filtered_experiment_class["imageid"] == image]

        list_of_masks.append([str(i + 1) + ".png", path, dataframe_temp])
    return list_of_masks
Exemplo n.º 8
0
def get_image_objects(filtered_data,dir_images):
  """creating objects each pictures with different experiment types
    
    
  input: filtered_data:      Dataframe of the Experients, 
         dir_images:         Directory of the images
         
         
  return: objects of each image with eyemovements and pictures

  """
  #get the image paths
  image_paths = get_img_paths(dir_images)
  
  original_paths = [i for i in image_paths if "original" in i and "._O" not in i]
  memory_paths = [i for i in original_paths if "memory" in i]
  search_paths = [i for i in original_paths if "search" in i]
  list_of_exp = seperate(filtered_data)

  list_of_masks = []
  
  for u in range(len(list_of_exp)):
  
    for i in range(len(np.unique(list_of_exp[u][0]["imageid"]) )):   
      if list_of_exp[u][3] == "memory":    
    
        for z, image_path in enumerate(memory_paths):

          if  list_of_exp[u][2] in image_path:
        
            if os.path.basename(image_path) ==  str(i+1)+".png":
              path = image_path
                
      elif list_of_exp[u][3] == "search":     
        if list_of_exp[u][4] == 'target_not_present':
          match = "L_"
        else:
          if list_of_exp[u][5] == 'expectedlocation':
            match = "E_"
          else:
            match = "U_"
        
        path_0 = [f for f in search_paths if (i+1) == int(re.findall(r'\d+', 
          os.path.basename(f))[0]) and match in f and list_of_exp[u][2] in f]
  
        try:
          path =path_0[0]
        except:
          pass        
      
      dataframe_temp = list_of_exp[u][0][list_of_exp[u][0]["imageid"] == i+1]
      
      if dataframe_temp.shape[0]: 
        if list_of_exp[u][3] == "memory":
          list_of_masks.append(
                [list_of_exp[u][0][list_of_exp[u][0]["imageid"] == i+1],
                list_of_exp[u][1],
                list_of_exp[u][2],
                list_of_exp[u][3],
                path,
                dataframe_temp.shape[0]])
        elif list_of_exp[u][3] == "search":
  
          list_of_masks.append(
                [list_of_exp[u][0][(list_of_exp[u][0]["imageid"] == i+1) ],
                list_of_exp[u][1],
                list_of_exp[u][2],
                list_of_exp[u][3],
                list_of_exp[u][4],
                list_of_exp[u][5],
                path,
                dataframe_temp.shape[0]]) 
  
  return list_of_masks
Exemplo n.º 9
0
                            'mean_pred' : mean_pred,
                            'max_pred': max_pred,
                            'min_pred' : min_pred,
                            'mean_true': mean_true,
                            'max_true': max_true,
                            'min_true':min_true})
else:
  print('Using normal model.')
  model = vgg16_enc_dec_model((img_height, img_width, img_channels),
                              freeze_vgg = False,
                              output_choice = 'sigmoid',
                              **{'reg':None,'dropout':0.1})
  model.load_weights(weights_path)

if tr_or_val == 'tr':
  image_paths = get_img_paths(path_train+"/images")
  mask_paths = get_img_paths(path_train+"/masks")
else:
  image_paths = get_img_paths(path_test+"/images")
  mask_paths = get_img_paths(path_test+"/masks")

#TODO: If there is a problem with NaN values, one can try this:
#for n,layer in enumerate(model.layers):
#  weights = layer.get_weights()
#  new_weights = [np.nan_to_num(w) for w in weights]
#  layer.set_weights(new_weights)

#%%
if nb_random_shows > len(image_paths):
  print('Showing all images since number of random shows exceed set lengths.')
  for n, (path,mask) in enumerate(zip(image_paths, mask_paths)):