added always_flag=False

This commit is contained in:
aliabd 2019-07-25 23:43:58 -07:00
parent 17fa68a3bc
commit ccd88a4d41
15 changed files with 32 additions and 21 deletions

View File

@ -122,9 +122,9 @@ class Sketchpad(AbstractInput):
""" """
Default rebuild method to decode a base64 image Default rebuild method to decode a base64 image
""" """
inp = msg['data']['input']
im = preprocessing_utils.decode_base64_to_image(inp) im = preprocessing_utils.decode_base64_to_image(msg)
timestamp = time.time()*1000 timestamp = int(time.time()*1000)
filename = f'input_{timestamp}.png' filename = f'input_{timestamp}.png'
im.save(f'{dir}/{filename}', 'PNG') im.save(f'{dir}/{filename}', 'PNG')
return filename return filename
@ -197,11 +197,10 @@ class Textbox(AbstractInput):
""" """
Default rebuild method for text saves it .txt file Default rebuild method for text saves it .txt file
""" """
inp = msg['data']['input']
timestamp = time.time()*1000 timestamp = time.time()*1000
filename = f'input_{timestamp}.png' filename = f'input_{timestamp}.png'
with open(f'{dir}/{filename}.txt','w') as f: with open(f'{dir}/{filename}.txt','w') as f:
f.write(inp) f.write(msg)
return filename return filename
def get_sample_inputs(self): def get_sample_inputs(self):
@ -251,8 +250,7 @@ class ImageUpload(AbstractInput):
""" """
Default rebuild method to decode a base64 image Default rebuild method to decode a base64 image
""" """
inp = msg['data']['input'] im = preprocessing_utils.decode_base64_to_image(msg)
im = preprocessing_utils.decode_base64_to_image(inp)
timestamp = time.time()*1000 timestamp = time.time()*1000
filename = f'input_{timestamp}.png' filename = f'input_{timestamp}.png'
im.save(f'{dir}/{filename}', 'PNG') im.save(f'{dir}/{filename}', 'PNG')
@ -283,8 +281,7 @@ class CSV(AbstractInput):
""" """
Default rebuild method for csv Default rebuild method for csv
""" """
inp = msg['data']['inp'] return json.loads(msg)
return json.loads(inp)
class Microphone(AbstractInput): class Microphone(AbstractInput):
@ -304,8 +301,7 @@ class Microphone(AbstractInput):
""" """
Default rebuild method for csv Default rebuild method for csv
""" """
inp = msg['data']['inp'] return json.loads(msg)
return json.loads(inp)
# Automatically adds all subclasses of AbstractInput into a dictionary (keyed by class name) for easy referencing. # Automatically adds all subclasses of AbstractInput into a dictionary (keyed by class name) for easy referencing.

View File

@ -47,6 +47,7 @@ class Interface:
postprocessing_fns=None, postprocessing_fns=None,
verbose=True, verbose=True,
saliency=None, saliency=None,
always_flag=False
): ):
""" """
:param inputs: a string or `AbstractInput` representing the input interface. :param inputs: a string or `AbstractInput` representing the input interface.
@ -99,6 +100,7 @@ class Interface:
self.simple_server = None self.simple_server = None
self.hash = random.getrandbits(32) self.hash = random.getrandbits(32)
self.saliency = saliency self.saliency = saliency
self.always_flag = always_flag
@staticmethod @staticmethod
def _infer_model_type(model): def _infer_model_type(model):

View File

@ -194,6 +194,16 @@ def serve_files_in_background(interface, port, directory_to_serve=None):
import numpy as np import numpy as np
saliency = interface.saliency(interface, interface.model_obj, raw_input, processed_input, prediction, processed_output) saliency = interface.saliency(interface, interface.model_obj, raw_input, processed_input, prediction, processed_output)
output['saliency'] = saliency.tolist() output['saliency'] = saliency.tolist()
if interface.always_flag:
msg = json.loads(data_string)
flag_dir = os.path.join(FLAGGING_DIRECTORY, str(interface.hash))
os.makedirs(flag_dir, exist_ok=True)
output_flag = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg['data']),
'output': interface.output_interface.rebuild_flagged(flag_dir, processed_output),
}
with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f:
f.write(json.dumps(output_flag))
f.write("\n")
# Prepare return json dictionary. # Prepare return json dictionary.
self.wfile.write(json.dumps(output).encode()) self.wfile.write(json.dumps(output).encode())
@ -202,10 +212,10 @@ def serve_files_in_background(interface, port, directory_to_serve=None):
self._set_headers() self._set_headers()
data_string = self.rfile.read(int(self.headers["Content-Length"])) data_string = self.rfile.read(int(self.headers["Content-Length"]))
msg = json.loads(data_string) msg = json.loads(data_string)
flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY) flag_dir = os.path.join(FLAGGING_DIRECTORY, str(interface.hash))
os.makedirs(flag_dir, exist_ok=True) os.makedirs(FLAGGING_DIRECTORY, exist_ok=True)
output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg), output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg['data']['input_data']),
'output': interface.output_interface.rebuild_flagged(flag_dir, msg), 'output': interface.output_interface.rebuild_flagged(flag_dir, msg['data']['output_data']),
'message': msg['data']['message']} 'message': msg['data']['message']}
with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f: with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f:
f.write(json.dumps(output)) f.write(json.dumps(output))

View File

@ -123,8 +123,7 @@ class Label(AbstractOutput):
""" """
Default rebuild method for label Default rebuild method for label
""" """
out = msg['data']['output'] return json.loads(msg)
return json.loads(out)
class Textbox(AbstractOutput): class Textbox(AbstractOutput):
@ -141,8 +140,7 @@ class Textbox(AbstractOutput):
""" """
Default rebuild method for label Default rebuild method for label
""" """
out = msg['data']['output'] return json.loads(msg)
return json.loads(out)
class Image(AbstractOutput): class Image(AbstractOutput):
@ -159,8 +157,7 @@ class Image(AbstractOutput):
""" """
Default rebuild method to decode a base64 image Default rebuild method to decode a base64 image
""" """
out = msg['data']['output'] im = preprocessing_utils.decode_base64_to_image(msg)
im = preprocessing_utils.decode_base64_to_image(out)
timestamp = datetime.datetime.now() timestamp = datetime.datetime.now()
filename = f'output_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png' filename = f'output_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
im.save(f'{dir}/{filename}', 'PNG') im.save(f'{dir}/{filename}', 'PNG')

View File

@ -0,0 +1 @@
{"input": "input_1564123002454.png", "output": {"label": 0, "confidences": [{"label": 0, "confidence": 0.6294623613357544}, {"label": 9, "confidence": 0.20205073058605194}, {"label": 8, "confidence": 0.1176086813211441}]}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -0,0 +1,2 @@
{"input": "input_1564122648517.png", "output": {"label": 7, "confidences": [{"label": 7, "confidence": 0.9945800304412842}, {"label": 2, "confidence": 0.004080282989889383}, {"label": 0, "confidence": 0.0005652490654028952}]}}
{"input": "input_1564122787604.png", "output": {"label": 6, "confidences": [{"label": 6, "confidence": 0.18893034756183624}, {"label": 0, "confidence": 0.1815452128648758}, {"label": 5, "confidence": 0.12178582698106766}]}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 B

3
static/flagged/data.txt Normal file
View File

@ -0,0 +1,3 @@
{"input": "input_1564121741675.png", "output": {"label": 3, "confidences": [{"label": 3, "confidence": 0.49897417426109314}, {"label": 2, "confidence": 0.49327075481414795}, {"label": 9, "confidence": 0.0031273311469703913}]}, "message": ""}
{"input": "input_1564122304245.png", "output": {"label": 1, "confidences": [{"label": 1, "confidence": 0.9119029641151428}, {"label": 0, "confidence": 0.04364873468875885}, {"label": 6, "confidence": 0.014955705031752586}]}}
{"input": "input_1564122365828.png", "output": {"label": 1, "confidences": [{"label": 1, "confidence": 0.8408133387565613}, {"label": 4, "confidence": 0.07017231732606888}, {"label": 2, "confidence": 0.032813962548971176}]}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB