How to load image data using keys in keras?

I am working on a sequential model that accepts images as input. However, another thing is that the input images are actually defined by keys.
For example, a training sequence (you can assume that fi is the identifier of the video frame)

{ f1, f2, f3, ..., fn }

and corresponding image sequence

{ M[f1], M[f2], M[f3], ..., M[fn] }

where M is a map preserving the map {fi-> image}.
Suppose in the next installment my learning sequence becomes

{ f2, f3, ..., fn+1 }  

and the sequence of images becomes

{ M[f2], M[f3], M[f4], ..., M[fn+1] }

, , ( M [f2] M [fn] ). , , imagedataloader .
[ ]
2 , , (fi). , data_preprocess.
:

{f3,  f4,  f5,  f6,  f7}     1
{f4,  f5,  f6,  f7,  f8}     1
{f5,  f6,  f7,  f8,  f9}     1
...

:

{f1,  f2,  f3,  f4,  f5}     0
{f2,  f3,  f4,  f5,  f6}     0
{f10, f11, f12, f13, f14}    0
...

, , . , , .
[ II]
N :

|-data_root/
  |-Video 1/
  | |-frame_1_1.jpg
  | |-frame_1_2.jpg
  | ...
  |-Video 2/
  | |-frame_2_1.jpg
  | |-frame_2_2.jpg
  | ...
  ...
  ...
  |-Video N/
  | |-frame_N_1.jpg
  | |-frame_N_2.jpg
    ...

, / , , .
, ( ):

Sequence of scene i: frame_1,  frame_2,  frame_3,  ..., frame_n
Sub-sequence i_1:    frame_1,  frame_2,  frame_3,  ..., frame_10
Sub-sequence i_2:    frame_11, frame_12, frame_13, ..., frame_20
Sub-sequence i_3:    frame_21, frame_22, frame_23, ..., frame_30
...

Pi ( , ), :

   <Pair of sub-sequences>                 <Labels>
P1 {sub-sequence i_4, sub-sequence i_2},      1
P2 {sub-sequence i_3, sub-sequence i_5},      1
...                                       ...

(Ni) :

   <Pair of sub-sequences>                 <Labels>
N1 {sub-sequence i_1, sub-sequence j_6},      0
N2 {sub-sequence i_2, sub-sequence j_4},      0
...                                       ...

, / . . N2 P1 i_2. id (fi), / ( fi).
Keras?

+4
1

, , , ImageDataGenerator keras.preprocessing.image?

, flow_from_directory(directory_path). , filename :

my_generator = ImageDataGenerator(...)
my_generator.flow_from_directory(path_dir)
list_of_file_names = my_generator.filename

(= file_paths) .

, ?

EDIT:

dictionnary

map_images = {str(os.path.splitext(os.path.split(file_path)[1])[0]): file_path for file_path in list_of_file_names}

file_path ImageDataGenerator, , string, frame_id.

frame_id file_path, load_img() img_to_array() from keras.preprocessing.image

load_img() PIL:

def load_img(path, grayscale=False, target_size=None): """Loads an image into PIL format. # Arguments path: Path to image file grayscale: Boolean, whether to load the image as grayscale. target_size: Either 'None' (default to original size) or tuple of ints '(img_height, img_width)'. # Returns A PIL Image instance. # Raises ImportError: if PIL is not available. """

img_to_array() 3D numpy :

def img_to_array(img, dim_ordering='default'): """Converts a PIL Image instance to a Numpy array. # Arguments img: PIL Image instance. dim_ordering: Image data format. # Returns A 3D Numpy array. # Raises ValueError: if invalid 'img' or 'dim_ordering' is passed. """

, : 1 frame_id . img_load() img_to_array(). , !

2:

, , , :

# list of video_id of each frame
videos = my_generator.classes
# mapping of the frame_id to path_of_file and vid_id
map_images = {str(os.path.splitext(os.path.split(file_path)[1])[0]): (file_path, vid_id) for file_path,vid_id in zip(list_of_file_names,videos) }
+3

Source: https://habr.com/ru/post/1671384/


All Articles