ClementPinard / FlowNetPytorch

Pytorch implementation of FlowNet by Dosovitskiy et al.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run your model on a folder containing multiple images?

AshviniKSharma opened this issue · comments

Hello, Thank you for your work.
Currently, it is working fine on two images but I want to run it on a folder.

shouldn't be too hard to adapt it to get it to work on img_n img_n+1

you just need to change how you construct the img_pairs list
Originnaly it's :

    for ext in args.img_exts:
        test_files = data_dir.files('*1.{}'.format(ext))
        for file in test_files:
            img_pair = file.parent / (file.namebase[:-1] + '2.{}'.format(ext))
            if img_pair.isfile():
                img_pairs.append([file, img_pair])

and now it should be something like

    for ext in args.img_exts:
        test_files = sorted(data_dir.files('*.{}'.format(ext)))
        for file1, file2 in zip(test_files[:-1], test_files[1:]):
            img_pairs.append([file1, file2])

Thanks, I got it working, but the scale of output is very low it's 128x96 for images of size 512x384, can't we produce the same scale output? I read your upscaling output comment on the previous issue but where I have to add that interpolation line I am not able to figure out, can you help?

At the end, you are dealing with images and not tnesors, so you can simply use classic image resize functions.
See e.g. https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.misc.imresize.html

shouldn't be too hard to adapt it to get it to work on img_n img_n+1

you just need to change how you construct the img_pairs list
Originnaly it's :

    for ext in args.img_exts:
        test_files = data_dir.files('*1.{}'.format(ext))
        for file in test_files:
            img_pair = file.parent / (file.namebase[:-1] + '2.{}'.format(ext))
            if img_pair.isfile():
                img_pairs.append([file, img_pair])

and now it should be something like

    for ext in args.img_exts:
        test_files = sorted(data_dir.files('*.{}'.format(ext)))
        for file1, file2 in zip(test_files[:-1], test_files[1:]):
            img_pairs.append([file1, file2])

hi i don't understand this code: test_files = data_dir.files('*1.{}'.format(ext))
i mean when i write some code like print(test_files) and it shows that this list is empty like this: test_files=[ ], there is nothing in this list( i did not change any code of yours while i just write some code like print('a'))

You are talking about the code for images pairs It is asked for these files to be named e.g. "something1.jpg" and "something2.jpg"
If in your folder none of your files is named like this, you won't any img pair to feed the network.

Alternatively, you can use the second snippet to get optical flow from a sequence of images : it will take every imge file in the folder [Img1, Img2, ..] and compute optical flow between each consecutive images : [Img1, Img2] , [Img2, Img3] ...

Of course, this won't work if you give frames that are not from the same sequence

You are talking about the code for images pairs It is asked for these files to be named e.g. "something1.jpg" and "something2.jpg"
If in your folder none of your files is named like this, you won't any img pair to feed the network.

Alternatively, you can use the second snippet to get optical flow from a sequence of images : it will take every imge file in the folder [Img1, Img2, ..] and compute optical flow between each consecutive images : [Img1, Img2] , [Img2, Img3] ...

Of course, this won't work if you give frames that are not from the same sequence

hi i got it work. i'm new to python and there is another problem i want to ask.
currently i want to concatnate the optical flow with images from davis2017 but i don't know how to get it work.
here is the code of reading images from davis2017:

# Load sequences
    self.sequences = [Sequence(self._phase, s.name, lmdb_env=lmdb_env_seq) for s in self._db_sequences]
    self._db_sequences = db_read_sequences(args.year,self._phase)

    # Load annotations
    self.annotations = [Annotation(self._phase,s.name,self._single_object, lmdb_env=lmdb_env_annot)
        for s in self._db_sequences]

    # Load sequences
    self.sequence_clips = []
    
    self._db_sequences = db_read_sequences(args.year,self._phase)
    for seq, s in zip(self.sequences, self._db_sequences):

        if self.use_prev_mask == False:

            images = seq.files

            starting_frame_idx = 0
            starting_frame = int(osp.splitext(osp.basename(images[starting_frame_idx]))[0])
            self.sequence_clips.append(SequenceClip_simple(seq, starting_frame))
            num_frames = self.sequence_clips[-1]._numframes
            num_clips = int(num_frames / self._length_clip)

            for idx in range(num_clips - 1):
                starting_frame_idx += self._length_clip
                starting_frame = int(osp.splitext(osp.basename(images[starting_frame_idx]))[0])
                self.sequence_clips.append(SequenceClip_simple(seq, starting_frame))

        else:

            annot_seq_dir = osp.join(cfg.PATH.ANNOTATIONS, s.name)
            annotations = glob.glob(osp.join(annot_seq_dir,'*.png'))
            annotations.sort()
            #We only consider the first frame annotated to start the inference mode with such a frame
            starting_frame = int(osp.splitext(osp.basename(annotations[0]))[0])
            self.sequence_clips.append(SequenceClip(self._phase, s.name, starting_frame, lmdb_env=lmdb_env_seq))

 
    # Load annotations
    self.annotation_clips = []
    self._db_sequences = db_read_sequences(args.year,self._phase)
    for annot, s in zip(self.annotations, self._db_sequences):

        images = annot.files

        starting_frame_idx = 0
        starting_frame = int(osp.splitext(osp.basename(images[starting_frame_idx]))[0])
        self.annotation_clips.append(AnnotationClip_simple(annot, starting_frame))
        num_frames = self.annotation_clips[-1]._numframes
        num_clips = int(num_frames / self._length_clip)

        for idx in range(num_clips - 1):
            starting_frame_idx += self._length_clip
            starting_frame = int(osp.splitext(osp.basename(images[starting_frame_idx]))[0])
            self.annotation_clips.append(AnnotationClip_simple(annot, starting_frame))

    self._keys = dict(zip([s for s in self.sequences],
      range(len(self.sequences))))
      
    self._keys_clips = dict(zip([s.name+str(s.starting_frame) for s in self.sequence_clips],
      range(len(self.sequence_clips))))  
      
    try:
      self.color_palette = np.array(Image.open(
        self.annotations[0].files[0]).getpalette()).reshape(-1,3)
    except Exception as e:
      self.color_palette = np.array([[0,255,0]])

actually the code that i'm using is from imatge-upc/rvos in the github