def log(self, key, v): value = deepcopy(v) if isinstance(value, np.ndarray) and len(value.shape) == 0: value = float(value) elif isinstance(value, np.ndarray) and len(value.shape) == 1: value = File.as_image(np.array([value])) elif isinstance(value, np.ndarray) and len(value.shape) <= 3: value = File.as_image(value) elif isinstance(value, np.ndarray): return self.run[key].log(value)
def test_log_many_values(self): exp = init(mode="debug", flush_period=0.5) exp["some/num/val"].log([5, 10, 15]) exp["some/str/val"].log(["some text", "other"]) exp["some/img/val"].log([ FileVal.as_image(PIL.Image.new("RGB", (60, 30), color="red")), FileVal.as_image(PIL.Image.new("RGB", (20, 90), color="red")), ]) self.assertEqual(exp["some"]["num"]["val"].fetch_last(), 15) self.assertEqual(exp["some"]["str"]["val"].fetch_last(), "other") self.assertIsInstance(exp.get_structure()["some"]["img"]["val"], FileSeries)
def test_log_value_errors(self): exp = init(mode="debug", flush_period=0.5) img = FileVal.as_image(PIL.Image.new("RGB", (60, 30), color="red")) with self.assertRaises(ValueError): exp["x"].log([]) with self.assertRaises(ValueError): exp["x"].log([5, "str"]) with self.assertRaises(ValueError): exp["x"].log([5, 10], step=10) exp["some/num/val"].log([5], step=1) exp["some/num/val"].log([]) with self.assertRaises(ValueError): exp["some/num/val"].log("str") with self.assertRaises(TypeError): exp["some/num/val"].log(img) exp["some/str/val"].log(["str"], step=1) exp["some/str/val"].log([]) exp["some/img/val"].log([img], step=1) exp["some/img/val"].log([]) with self.assertRaises(TypeError): exp["some/img/val"].log(5) with self.assertRaises(FileNotFound): exp["some/img/val"].log("path") self.assertEqual(exp["some"]["num"]["val"].fetch_last(), 5) self.assertEqual(exp["some"]["str"]["val"].fetch_last(), "str") self.assertIsInstance(exp.get_structure()["some"]["img"]["val"], FileSeries)
def handle_files_and_images(self): # image im_frame = Image.open(self.img_path) g_img = File.as_image(im_frame) self.exp[ARTIFACT_ATTRIBUTE_SPACE]["assigned image"] = g_img self.exp.wait() with self.with_check_if_file_appears("assigned image.png"): self.exp[ARTIFACT_ATTRIBUTE_SPACE]["assigned image"].download() with self.with_check_if_file_appears("custom_dest.png"): self.exp[ARTIFACT_ATTRIBUTE_SPACE]["assigned image"].download( "custom_dest.png") self.exp[ARTIFACT_ATTRIBUTE_SPACE]["logged image"].log(g_img) with open(self.img_path, mode="r") as f: # self.exp[ARTIFACT_ATTRIBUTE_SPACE]['assigned image stream'] = f self.exp[ARTIFACT_ATTRIBUTE_SPACE]["logged image stream"].log(f) # artifact text_file = neptune.types.File(self.text_file_path) self.exp[ARTIFACT_ATTRIBUTE_SPACE]["assigned file"] = text_file # self.exp[ARTIFACT_ATTRIBUTE_SPACE]['logged file'].log(text_file) # wrong type with open(self.text_file_path, mode="r") as f: self.exp[ARTIFACT_ATTRIBUTE_SPACE]["assigned file stream"] = f self.exp[ARTIFACT_ATTRIBUTE_SPACE]["logged file stream"].log(f)
def test_assign_content(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) # when attr.assign([file], wait=wait) # then op_processor.enqueue_operation.assert_has_calls([ call(ClearImageLog(path), False), call( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), None, None), None, self._now(), ) ], ), wait, ), ])
def test_log_content(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) # when attr.log( file, step=3, timestamp=self._now(), wait=wait, name="nazwa", description="opis", ) # then op_processor.enqueue_operation.assert_called_once_with( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), "nazwa", "opis"), 3, self._now(), ) ], ), wait, )
def validation_step(self, batch, batch_idx): if batch_idx == 0: # Plot example model_output = self(batch) fig = plot_example(batch, model_output) self.logger.experiment['validation/plot'].log(File.as_image(fig)) return self._training_or_validation_step(batch, is_train_step=False)
def test_log(self): exp = init(mode="debug", flush_period=0.5) exp["some/num/val"].log(5) exp["some/str/val"].log("some text") exp["some/img/val"].log( FileVal.as_image(PIL.Image.new("RGB", (60, 30), color="red"))) exp["some/img/val"].log(PIL.Image.new("RGB", (60, 30), color="red")) self.assertEqual(exp["some"]["num"]["val"].fetch_last(), 5) self.assertEqual(exp["some"]["str"]["val"].fetch_last(), "some text") self.assertIsInstance(exp.get_structure()["some"]["img"]["val"], FileSeries)
def test_as_image(self): # given image_array = numpy.random.rand(10, 10) * 255 expected_image = Image.fromarray(image_array.astype(numpy.uint8)) # when file = File.as_image(expected_image) # then self.assertEqual(file.extension, "png") self.assertEqual(file.content, _get_pil_image_data(expected_image))
def validation_step(self, batch, batch_idx): if batch_idx == 0: # Plot example model_output = self(batch) fig = plot_example(batch, model_output, history_len=params['history_len'], forecast_len=params['forecast_len'], nwp_channels=params['nwp_channels']) self.logger.experiment['validation/plot'].log(File.as_image(fig)) return self._training_or_validation_step(batch, is_train_step=False)
def plot_images_on_weblogger(dataset_name, stats, images, labels, more, log_text, weblogger=2): plot_images = images[0:np.max((4, len(images)))] if labels is None: labels = np.repeat('', len(plot_images)) labels = labels[0:np.max((4, len(labels)))] add_text = [''] * len(labels) if isinstance(more, dict) and 'image_name' in list(more.keys()): add_text = more['image_name'] metric_str = 'Debug/{} example images: [{}]'.format(log_text, dataset_name) if isinstance(weblogger, neptune.run.Run): [weblogger[metric_str].log (File.as_image(convert_normalized_tensor_to_plottable_array(im, stats['mean'], stats['std'], text=f'{lb}' + os.path.splitext(n)[0])/255)) for im, lb, n in zip(plot_images, labels, add_text)]
def log_series(self): # floats self.exp[LOG_ATTRIBUTE_SPACE]["m1"].log(1) self.exp[LOG_ATTRIBUTE_SPACE]["m1"].log(2) self.exp[LOG_ATTRIBUTE_SPACE]["m1"].log(3) self.exp[LOG_ATTRIBUTE_SPACE]["m1"].log(2) self.exp[LOG_ATTRIBUTE_SPACE]["nested"]["m1"].log(1) # texts self.exp[LOG_ATTRIBUTE_SPACE]["m2"].log("a") self.exp[LOG_ATTRIBUTE_SPACE]["m2"].log("b") self.exp[LOG_ATTRIBUTE_SPACE]["m2"].log("c") # images im_frame = Image.open(self.img_path) g_img = File.as_image(im_frame) self.exp[LOG_ATTRIBUTE_SPACE]["g_img"].log(g_img)
def test_assign_series(self): exp = init(mode="debug", flush_period=0.5) exp["some/num/val"].assign(FloatSeriesVal([1, 2, 0, 10])) exp["some/str/val"].assign(StringSeriesVal(["text1", "text2"]), wait=True) exp["some/img/val"].assign( FileSeriesVal([ FileVal.as_image(PIL.Image.new("RGB", (10, 15), color="red")) ])) self.assertEqual(exp["some"]["num"]["val"].fetch_last(), 10) self.assertEqual(exp["some"]["str"]["val"].fetch_last(), "text2") self.assertIsInstance(exp.get_structure()["some"]["img"]["val"], FileSeries) exp["some/num/val"].assign(FloatSeriesVal([122, 543, 2, 5])) exp["some/str/val"].assign(StringSeriesVal( ["other 1", "other 2", "other 3"]), wait=True) self.assertEqual(exp["some"]["num"]["val"].fetch_last(), 5) self.assertEqual(exp["some"]["str"]["val"].fetch_last(), "other 3")
def test_log_path(self): # given wait = self._random_wait() path = self._random_path() op_processor = MagicMock() exp = self._create_run(processor=op_processor) attr = FileSeries(exp, path) file = File.as_image(numpy.random.rand(10, 10) * 255) with create_file(file.content, binary_mode=True) as tmp_filename: saved_file = File(tmp_filename) # when attr.log( saved_file, step=3, timestamp=self._now(), wait=wait, description="something", ) # then op_processor.enqueue_operation.assert_called_once_with( LogImages( path, [ LogImages.ValueType( ImageValue(base64_encode(file.content), None, "something"), 3, self._now(), ) ], ), wait, )