geowatch.tasks.sam.predict module

CommandLine

# To Execute
export DVC_EXPT_DPATH=$(geowatch_dvc --tags='phase2_expt' --hardware=auto)
sdvc request -r aws $DVC_EXPT_DPATH/models/sam/sam_vit_h_4b8939.pth
xdoctest geowatch.tasks.sam.predict __doc__:0

Example

>>> # xdoctest: +REQUIRES(env:DVC_EXPT_DPATH)
>>> import geowatch
>>> import kwcoco
>>> import ubelt as ub
>>> # Define where to write the output
>>> output_dpath = ub.Path.appdir('/mnt/ssd2/data/test/geowatch/geowatch/tasks/sam').ensuredir()
>>> output_kwcoco_fpath = output_dpath / 'demo_sam.kwcoco.zip'
>>> # Define the input
>>> dset = kwcoco.CocoDataset.demo('vidshapes', num_frames=4, num_videos=1)
>>> dset.reroot(absolute=True)
>>> dset.fpath = output_dpath / 'sam_input.kwcoco.zip'
>>> dset.dump()
>>> input_kwcoco_fpath = dset.fpath
>>> # The main external data this test needs is the SAM weights
>>> dvc_dpath = geowatch.find_dvc_dpath(tags='phase2_expt')
>>> weights_fpath = dvc_dpath / "models/sam/sam_vit_h_4b8939.pth"
>>> # Setup the arguments to SAM predict
>>> from geowatch.tasks.sam import predict as sam_predict
>>> kwargs = {
>>>     'input_kwcoco': input_kwcoco_fpath,
>>>     'output_kwcoco': output_kwcoco_fpath,
>>>     'fixed_resolution': None,
>>>     'weights_fpath': weights_fpath,
>>>     'channels': 'r|g|b',
>>> }
>>> cmdline = 0
>>> sam_predict.main(cmdline=cmdline, **kwargs)
>>> if 1:
>>>     ub.cmd(f'geowatch visualize {output_kwcoco_fpath} --stack=only --channels "r|g|b,sam.0:3,sam.3:6,sam.6:9"', verbose=3)
class geowatch.tasks.sam.predict.SAMConfig(*args, **kwargs)[source]

Bases: DataConfig

Compute SAM encoder features

Usage:

DVC_EXPT_DPATH=$(geowatch_dvc –tags=’phase2_expt’ –hardware=auto) python -m geowatch.tasks.sam.predict

–input_kwcoco <input-kwcoco> –output_kwcoco <output-kwcoco> –fixed_resolution=None –channels=”red|green|blue” –weights_fpath “$DVC_EXPT_DPATH/models/sam/sam_vit_h_4b8939.pth” –window_overlap=0.33333 –data_workers=”2” –io_workers 0 –assets_dname=”sam_feats”

Valid options: []

Parameters:
  • *args – positional arguments for this data config

  • **kwargs – keyword arguments for this data config

default = {'assets_dname': 'sam', 'batch_size': 1, 'channels': <Value(['red|green|blue', 'pan'])>, 'data_workers': 2, 'device': <Value(0)>, 'fixed_resolution': '10GSD', 'input_kwcoco': <Value(None)>, 'io_workers': 0, 'output_kwcoco': <Value(None)>, 'select_images': None, 'track_emissions': True, 'weights_fpath': <Value(None)>, 'window_dims': (1024, 1024), 'window_overlap': 0.33333}
geowatch.tasks.sam.predict.rgb_from_kwcoco_frame(frame, channel_priority)[source]
class geowatch.tasks.sam.predict.SAMWrapperDataset(subdset, channel_priority)[source]

Bases: Dataset

class geowatch.tasks.sam.predict.DenseFeaturePredictor(config)[source]

Bases: object

Base class for computing per-image dense features

short_code = NotImplemented
chan_code = NotImplemented
proc_name = NotImplemented
WrapperDsetClass = NotImplemented
setup()[source]
run()[source]
predict_batch(batch)[source]
class geowatch.tasks.sam.predict.SAMFeaturePredictor(config)[source]

Bases: DenseFeaturePredictor

short_code = 'sam'
proc_name = 'geowatch.tasks.sam.predict'
chan_code = 'sam.0:256'
WrapperDsetClass

alias of SAMWrapperDataset

predict_batch(batch)[source]
geowatch.tasks.sam.predict.main(cmdline=1, **kwargs)[source]

CommandLine

DVC_DATA_DPATH=$(geowatch_dvc --tags='phase2_data' --hardware=auto)
DVC_EXPT_DPATH=$(geowatch_dvc --tags='phase2_expt' --hardware=auto)

CommandLine

TEST_SAM=1 xdoctest -m geowatch.tasks.sam.predict main

Example

>>> # xdoctest: +REQUIRES(env:TEST_SAM)
>>> from geowatch.tasks.sam.predict import *  # NOQA
>>> import geowatch
>>> dvc_expt_dpath = geowatch.find_dvc_dpath(tags='phase2_expt', hardware='auto')
>>> dvc_data_dpath = geowatch.find_dvc_dpath(tags='phase2_data', hardware='auto')
>>> weights_fpath = dvc_expt_dpath / 'models/sam/sam_vit_h_4b8939.pth'
>>> input_kwcoco = dvc_data_dpath / 'Drop6-MeanYear10GSD-V2/imgonly-KR_R001.kwcoco.zip'
>>> kwargs = dict(
>>>     weights_fpath=weights_fpath,
>>>     input_kwcoco=input_kwcoco,
>>>     output_kwcoco=(dvc_data_dpath / 'Drop6-MeanYear10GSD-V2/_test.kwcoco.zip')
>>> )
>>> cmdline = 0
>>> main(cmdline, **kwargs)

Example

>>> # xdoctest: +SKIP
>>> cmdline = 0
>>> kwargs = dict()
>>> main(cmdline=cmdline, **kwargs)