def setup_class(cls): """Setup method.""" cls.optimizer = Momentum(Tensor(0.12)) cls.loss_fn = SoftmaxCrossEntropyWithLogits() cls.net = ResNet() cls.run_context = dict() cls.run_context['train_network'] = cls.net cls.run_context['loss_fn'] = cls.loss_fn cls.run_context['net_outputs'] = Tensor(np.array([0.03])) cls.run_context['optimizer'] = cls.optimizer cls.run_context['train_dataset'] = MindDataset(dataset_size=32) cls.run_context['epoch_num'] = 10 cls.run_context['cur_step_num'] = 320 cls.run_context['parallel_mode'] = "stand_alone" cls.run_context['device_number'] = 2 cls.run_context['batch_num'] = 32 cls.summary_record = SummaryRecord(SUMMARY_DIR) callback = [ ModelCheckpoint(directory=SUMMARY_DIR), SummaryStep(cls.summary_record), TrainLineage(cls.summary_record) ] cls.run_context['list_callback'] = _ListCallback(callback) cls.user_defined_info = {"info": "info1", "version": "v1"}
def test_get_file_path(self): """Test get_file_path method.""" model_ckpt = ModelCheckpoint(prefix='', directory='/path/to') summary_step = SummaryStep( MagicMock(full_file_name='/path/to/summary.log')) list_callback = [model_ckpt, summary_step] ckpt_file_path, _ = AnalyzeObject.get_file_path(list_callback) self.assertEqual(ckpt_file_path, '/path/to/test_model.ckpt')
def test_train_callback(test_with_simu): """ test_train_callback """ dataset = get_dataset() model = get_model() fn = CallbackTest() summary_recode = SummaryStep(fn, 2) if test_with_simu: return model.train(2, dataset, callbacks=summary_recode)
def test_graph_summary_callback2(): dataset = get_dataset() net = Net() loss = nn.SoftmaxCrossEntropyWithLogits() optim = Momentum(net.trainable_params(), 0.1, 0.9) context.set_context(mode=context.GRAPH_MODE) model = Model(net, loss_fn=loss, optimizer=optim, metrics=None) with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_GRAPH", network=net) as test_writer: summary_cb = SummaryStep(test_writer, 1) model.train(2, dataset, callbacks=summary_cb)
def train_lenet(): context.set_context(mode=context.GRAPH_MODE, save_graphs=True, device_target="CPU") dataset_sink_mode = False # download mnist dataset download_dataset() # learning rate setting lr = 0.01 momentum = 0.9 epoch_size = 1 mnist_path = "../MNIST_Data" # define the loss function net_loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean") repeat_size = epoch_size # create the network network = LeNet5() # define the optimizer net_opt = nn.Momentum(network.trainable_params(), lr, momentum) config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10) # save the network model and parameters for subsequence fine-tuning ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck) # group layers into an object with training and evaluation features model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) summary_writer = SummaryRecord(log_dir="../../summary", network=network) summary_callback = SummaryStep(summary_writer, flush_step=10) # Init TrainLineage to record the training information train_callback = TrainLineage(summary_writer) train_net( model, epoch_size, mnist_path, repeat_size, ckpoint_cb, dataset_sink_mode, callbacks=[summary_callback, train_callback], ) test_net(network, model, mnist_path) summary_writer.close()
def test_image_summary_train(): """ test_image_summary_train """ dataset = get_dataset() log.debug("begin test_image_summary_sample") # step 0: create the thread with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_IMAGE") as test_writer: # step 1: create the test data for summary # step 2: create the Event model = get_model() fn = ImageSummaryCallback(test_writer) summary_recode = SummaryStep(fn, 1) model.train(2, dataset, callbacks=summary_recode) # step 3: send the event to mq # step 4: accept the event and write the file log.debug("finished test_image_summary_sample")
def test_validate(): with SummaryRecord(SUMMARY_DIR) as sr: with pytest.raises(ValueError): SummaryStep(sr, 0) with pytest.raises(ValueError): SummaryStep(sr, -1) with pytest.raises(ValueError): SummaryStep(sr, 1.2) with pytest.raises(ValueError): SummaryStep(sr, True) with pytest.raises(ValueError): SummaryStep(sr, "str") sr.record(1) with pytest.raises(ValueError): sr.record(False) with pytest.raises(ValueError): sr.record(2.0) with pytest.raises(ValueError): sr.record((1, 3)) with pytest.raises(ValueError): sr.record([2, 3]) with pytest.raises(ValueError): sr.record("str") with pytest.raises(ValueError): sr.record(sr) SummaryStep(sr, 1) with pytest.raises(ValueError): SummaryStep(sr, 1.2) with pytest.raises(ValueError): SummaryStep(sr, False) with pytest.raises(ValueError): SummaryStep(sr, "str") with pytest.raises(ValueError): SummaryStep(sr, (1, 2)) with pytest.raises(ValueError): SummaryStep(sr, [3, 4]) with pytest.raises(ValueError): SummaryStep(sr, sr)