summaryrefslogtreecommitdiff
path: root/ml/quick_label.py
blob: 05efbaebcee5217b516a3c3fc37421b1e6a982fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3

# © Copyright 2021-2022, Scott Gasch

"""A helper to facilitate quick manual labeling of ML training data."""

import logging
import os
import sys
import time
from abc import abstractmethod
from typing import Any, Dict, List, Optional, Set, Tuple

import argparse_utils
import config

logger = logging.getLogger(__name__)
parser = config.add_commandline_args(
    f"ML Quick Labeler ({__file__})",
    "Args related to quick labeling of ML training data",
)
parser.add_argument(
    "--ml_quick_label_skip_list_path",
    default="./qlabel_skip_list.txt",
    metavar="FILENAME",
    type=argparse_utils.valid_filename,
    help="Path to file in which to store already labeled data.",
)
parser.add_argument(
    "--ml_quick_label_use_skip_lists",
    default=True,
    action=argparse_utils.ActionNoYes,
    help='Should we use a skip list file to speed up execution?',
)
parser.add_argument(
    "--ml_quick_label_overwrite_labels",
    default=False,
    action=argparse_utils.ActionNoYes,
    help='Enable overwriting existing labels; default is to not relabel.',
)
parser.add_argument(
    '--ml_quick_label_skip_where_model_agrees',
    default=False,
    action=argparse_utils.ActionNoYes,
    help='Do not filter examples where the model disagrees with the current label.',
)
parser.add_argument(
    'ml_quick_label_delete_invalid_examples',
    default=False,
    action='store_true',
    help='If set we will delete invalid training examples.',
)


class QuickLabelHelper:
    '''To use this quick labeler your code must create a subclass of this
    class and implement the abstract methods below.  See comments for
    detailed semantics.'''

    @abstractmethod
    def get_candidate_files(self) -> List[str]:
        '''This must return a list of raw candidate files for labeling.'''
        pass

    @abstractmethod
    def get_features_for_file(self, filename: str) -> Optional[str]:
        '''Given a raw file, return its features file.'''
        pass

    @abstractmethod
    def render_example(self, filename: str, features: str, lines: List[str]) -> None:
        '''Render a raw file with its features for the user.'''
        pass

    @abstractmethod
    def unrender_example(self, filename: str, features: str, lines: List[str]) -> None:
        '''Unrender a raw file with its features (if necessary)...'''
        pass

    @abstractmethod
    def is_valid_example(self, filename: str, features: str, lines: List[str]) -> bool:
        '''Returns true iff the example is valid (all features are valid, there
        are the correct number of features, etc...'''
        pass

    @abstractmethod
    def ask_current_model_about_example(
        self,
        filename: str,
        features: str,
        lines: List[str],
    ) -> Any:
        '''Ask the current ML model about this example, if necessary.'''
        pass

    @abstractmethod
    def get_labelling_keystrokes(self) -> Dict[str, Any]:
        '''What keystrokes should be considered valid label actions and what
        label does each keystroke map into.  e.g. if you want to ask
        the user to hit 'y' for 'yes' and code that as 255 in your
        features or to hit 'n' for 'no' and code that as 0 in your
        features, return:

            { 'y': 255, 'n': 0 }

        '''
        pass

    @abstractmethod
    def get_everything_label(self) -> Any:
        '''If this returns something other than None it indicates that every
        example selected should be labeled with this result.  Caveat
        emptor, we will klobber all your files.

        '''
        pass

    @abstractmethod
    def get_label_feature(self) -> str:
        '''What feature denotes the example's label?  This is used to detect
        when examples already have a label and to assign labels to
        examples.'''
        pass


def _maybe_read_skip_list() -> Set[str]:
    '''Reads the skip list (files to just bypass) into memory if using.'''

    ret: Set[str] = set()
    if config.config['ml_quick_label_use_skip_lists']:
        quick_skip_file = config.config['ml_quick_label_skip_list_path']
        if os.path.exists(quick_skip_file):
            with open(quick_skip_file, 'r') as f:
                lines = f.readlines()
            for line in lines:
                line = line[:-1]
                line.strip()
                ret.add(line)
        logger.debug('Read %s and found %d entries.', quick_skip_file, len(ret))
    return ret


def _maybe_write_skip_list(skip_list) -> None:
    '''Writes the skip list (files to just bypass) to disk if using.'''

    if config.config['ml_quick_label_use_skip_lists']:
        quick_skip_file = config.config['ml_quick_label_skip_list_path']
        with open(quick_skip_file, 'w') as f:
            for filename in skip_list:
                filename = filename.strip()
                if len(filename) > 0:
                    f.write(f'{filename}\n')
        logger.debug('Updated %s', quick_skip_file)


def _filter_images(
    images: List[str], skip_list: Set[str], helper: QuickLabelHelper
) -> List[Tuple[str, str]]:
    filtered_images = []
    label_label = helper.get_label_feature()
    for image in images:
        if image in skip_list:
            logger.debug('Skipping %s because of the skip list', image)
            continue

        features = helper.get_features_for_file(image)
        if features is None or not os.path.exists(features):
            logger.warning('%s/%s: features doesn\'t exist, SKIPPING.', image, features)
            continue

        label = None
        filtered_lines = []
        with open(features, 'r') as rf:
            lines = rf.readlines()
        for line in lines:
            line = line[:-1]
            if line.startswith(label_label):
                label = ''.join(line.split(':')[1:])
                label = label.strip()
            else:
                filtered_lines.append(line)

        if not helper.is_valid_example(image, features, filtered_lines):
            logger.warning('%s/%s: Invalid example.', image, features)
            if config.config['ml_quick_label_delete_invalid_examples']:
                os.remove(image)
                os.remove(features)
            continue

        if label and not config.config['ml_quick_label_overwrite_labels']:
            logger.warning('%s/%s: already has label, SKIPPING.', image, features)
            continue

        if config.config['ml_quick_label_skip_where_model_agrees']:
            model_says = helper.ask_current_model_about_example(image, features, filtered_lines)
            if model_says and label:
                if model_says[0] == int(label):
                    continue
                print(f'{image}/{features}: The model disagrees with the current label.')
                print(f'    ...model says {model_says[0]} with probability {model_says[1]}.')
                print(f'    ...the example is currently labeled {label}')
        filtered_images.append((image, features))
    return filtered_images


def quick_label(helper: QuickLabelHelper) -> None:
    skip_list = _maybe_read_skip_list()

    # Ask helper for an initial set of files.
    images = helper.get_candidate_files()
    if len(images) == 0:
        logger.warning('No images files to operate on.')
        return

    # Filter out any that can't be converted to features or already have a
    # label (unless they used --ml_qukck_label_overwrite_labels).
    filtered_images = _filter_images(images, skip_list, helper)
    if len(filtered_images) == 0:
        logger.warning('No image files to operate on (post filter).')
        return

    # Allow the user to label the non-filtered images one by one.
    import input_utils

    cursor = 0
    label_label = helper.get_label_feature()
    while True:
        assert 0 <= cursor < len(filtered_images)

        image = filtered_images[cursor][0]
        assert os.path.exists(image)
        features = filtered_images[cursor][1]
        assert features and os.path.exists(features)

        filtered_lines = []
        label = None
        with open(features, 'r') as rf:
            lines = rf.readlines()
        for line in lines:
            line = line[:-1]
            if not line.startswith(label_label):
                filtered_lines.append(line)
            else:
                label = line

        # Render...
        helper.render_example(image, features, filtered_lines)

        # Prompt...
        print(
            f'{cursor} of {len(filtered_images)} {cursor/len(filtered_images)*100.0:.1f}%): {image}, {features}'
        )
        if label:
            print(f'    ...Already labelled: {label}')
        else:
            print('    ...Currently unlabeled')
        guess = helper.ask_current_model_about_example(image, features, filtered_lines)
        if guess:
            print(f'    ...Model says {guess}')
        print()

        # Did they want everything labelled the same?
        label_everything = helper.get_everything_label()
        if label_everything:
            filtered_lines.append(f"{label_label}: {label_everything}\n")
            with open(features, 'w') as f:
                f.writelines(line + '\n' for line in filtered_lines)
            if config.config['ml_quick_label_use_skip_lists']:
                skip_list.add(image)
            cursor += 1
            if cursor >= len(filtered_images):
                helper.unrender_example(image, features, filtered_lines)
                break

        # Otherwise ask about each example.
        else:
            labelling_keystrokes = helper.get_labelling_keystrokes()
            valid_keystrokes = ['<', '>', 'Q', '?']
            valid_keystrokes += labelling_keystrokes.keys()
            prompt = ','.join(valid_keystrokes)
            print(f'What should I do ({prompt})? ', end='')
            sys.stdout.flush()
            keystroke = input_utils.single_keystroke_response(valid_keystrokes)
            print()
            if keystroke == 'Q':
                logger.info('Ok, stopping for now.  Labeled examples are written to disk')
                helper.unrender_example(image, features, filtered_lines)
                break
            elif keystroke == '?':
                print(
                    '''
    >   =   Don't label, move to the next example.
    <   =   Don't label, move to the previous example.
    Q   =   Quit labeling now.
    ?   =   This message.
  else  =   These keystrokes assign a label to the example and persist it.'''
                )
                time.sleep(3.0)

            elif keystroke == '>':
                cursor += 1
                if cursor >= len(filtered_images):
                    print('Wrapping around...')
                    cursor = 0
            elif keystroke == '<':
                cursor -= 1
                if cursor < 0:
                    print('Wrapping around...')
                    cursor = len(filtered_images) - 1
            elif keystroke in labelling_keystrokes:
                label_value = labelling_keystrokes[keystroke]
                filtered_lines.append(f"{label_label}: {label_value}\n")
                with open(features, 'w') as f:
                    f.writelines(line + '\n' for line in filtered_lines)
                if config.config['ml_quick_label_use_skip_lists']:
                    skip_list.add(image)
                cursor += 1
                if cursor >= len(filtered_images):
                    print('Wrapping around...')
                    cursor = 0
            else:
                print(f'Unknown keystroke: {keystroke}')
        helper.unrender_example(image, features, filtered_lines)
    _maybe_write_skip_list(skip_list)