CNNSupervisedTrainer_VGG16.py 22.3 KB
 Nicola Gatto committed Apr 08, 2019 1 2 3 4 5 6 ``````import mxnet as mx import logging import numpy as np import time import os import shutil `````` Christian Fuß committed Oct 08, 2019 7 ``````import pickle `````` Sebastian Nickels committed Oct 18, 2019 8 9 ``````import math import sys `````` Nicola Gatto committed Apr 08, 2019 10 11 ``````from mxnet import gluon, autograd, nd `````` Eyüp Harputlu committed Jun 05, 2019 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ``````class CrossEntropyLoss(gluon.loss.Loss): def __init__(self, axis=-1, sparse_label=True, weight=None, batch_axis=0, **kwargs): super(CrossEntropyLoss, self).__init__(weight, batch_axis, **kwargs) self._axis = axis self._sparse_label = sparse_label def hybrid_forward(self, F, pred, label, sample_weight=None): pred = F.log(pred) if self._sparse_label: loss = -F.pick(pred, label, axis=self._axis, keepdims=True) else: label = gluon.loss._reshape_like(F, label, pred) loss = -F.sum(pred * label, axis=self._axis, keepdims=True) loss = gluon.loss._apply_weighting(F, loss, self._weight, sample_weight) return F.mean(loss, axis=self._batch_axis, exclude=True) `````` Eyüp Harputlu committed Jun 24, 2019 28 29 30 31 32 33 34 35 36 ``````class LogCoshLoss(gluon.loss.Loss): def __init__(self, weight=None, batch_axis=0, **kwargs): super(LogCoshLoss, self).__init__(weight, batch_axis, **kwargs) def hybrid_forward(self, F, pred, label, sample_weight=None): loss = F.log(F.cosh(pred - label)) loss = gluon.loss._apply_weighting(F, loss, self._weight, sample_weight) return F.mean(loss, axis=self._batch_axis, exclude=True) `````` Sebastian Nickels committed Oct 30, 2019 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 ``````class SoftmaxCrossEntropyLossIgnoreIndices(gluon.loss.Loss): def __init__(self, axis=-1, ignore_indices=[], sparse_label=True, from_logits=False, weight=None, batch_axis=0, **kwargs): super(SoftmaxCrossEntropyLossIgnoreIndices, self).__init__(weight, batch_axis, **kwargs) self._axis = axis self._ignore_indices = ignore_indices self._sparse_label = sparse_label self._from_logits = from_logits def hybrid_forward(self, F, pred, label, sample_weight=None): log_softmax = F.log_softmax pick = F.pick if not self._from_logits: pred = log_softmax(pred, self._axis) if self._sparse_label: loss = -pick(pred, label, axis=self._axis, keepdims=True) else: label = _reshape_like(F, label, pred) loss = -(pred * label).sum(axis=self._axis, keepdims=True) # ignore some indices for loss, e.g. tokens in NLP applications for i in self._ignore_indices: `````` Christian Fuß committed Dec 18, 2019 57 `````` loss = loss * mx.nd.logical_not(mx.nd.equal(mx.nd.argmax(pred, axis=1), mx.nd.ones_like(mx.nd.argmax(pred, axis=1))*i) * mx.nd.equal(mx.nd.argmax(pred, axis=1), label)) `````` Sebastian Nickels committed Oct 30, 2019 58 59 `````` return loss.mean(axis=self._batch_axis, exclude=True) `````` Sebastian Nickels committed Oct 18, 2019 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 ``````@mx.metric.register class BLEU(mx.metric.EvalMetric): N = 4 def __init__(self, exclude=None, name='bleu', output_names=None, label_names=None): super(BLEU, self).__init__(name=name, output_names=output_names, label_names=label_names) self._exclude = exclude or [] self._match_counts = [0 for _ in range(self.N)] self._counts = [0 for _ in range(self.N)] self._size_ref = 0 self._size_hyp = 0 def update(self, labels, preds): labels, preds = mx.metric.check_label_shapes(labels, preds, True) new_labels = self._convert(labels) new_preds = self._convert(preds) for label, pred in zip(new_labels, new_preds): reference = [word for word in label if word not in self._exclude] hypothesis = [word for word in pred if word not in self._exclude] self._size_ref += len(reference) self._size_hyp += len(hypothesis) for n in range(self.N): reference_ngrams = self._get_ngrams(reference, n + 1) hypothesis_ngrams = self._get_ngrams(hypothesis, n + 1) match_count = 0 for ngram in hypothesis_ngrams: if ngram in reference_ngrams: reference_ngrams.remove(ngram) match_count += 1 self._match_counts[n] += match_count self._counts[n] += len(hypothesis_ngrams) def get(self): precisions = [sys.float_info.min for n in range(self.N)] i = 1 for n in range(self.N): match_counts = self._match_counts[n] counts = self._counts[n] if counts != 0: if match_counts == 0: i *= 2 match_counts = 1 / i `````` Christian Fuß committed Nov 12, 2019 117 118 `````` if (match_counts / counts) > 0: precisions[n] = match_counts / counts `````` Sebastian Nickels committed Oct 18, 2019 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 `````` bleu = self._get_brevity_penalty() * math.exp(sum(map(math.log, precisions)) / self.N) return (self.name, bleu) def calculate(self): precisions = [sys.float_info.min for n in range(self.N)] i = 1 for n in range(self.N): match_counts = self._match_counts[n] counts = self._counts[n] if counts != 0: if match_counts == 0: i *= 2 match_counts = 1 / i precisions[n] = match_counts / counts return self._get_brevity_penalty() * math.exp(sum(map(math.log, precisions)) / self.N) def _get_brevity_penalty(self): if self._size_hyp >= self._size_ref: return 1 else: return math.exp(1 - (self._size_ref / self._size_hyp)) @staticmethod def _get_ngrams(sentence, n): ngrams = [] if len(sentence) >= n: for i in range(len(sentence) - n + 1): ngrams.append(sentence[i:i+n]) return ngrams @staticmethod def _convert(nd_list): if len(nd_list) == 0: return [] new_list = [[] for _ in range(nd_list[0].shape[0])] for element in nd_list: for i in range(element.shape[0]): new_list[i].append(element[i].asscalar()) return new_list `````` Christian Fuß committed Oct 08, 2019 170 `````` `````` Sebastian Nickels committed Oct 30, 2019 171 172 `````` `````` Sebastian Nickels committed May 26, 2019 173 ``````class CNNSupervisedTrainer_VGG16: `````` Sebastian Nickels committed Jun 21, 2019 174 `````` def __init__(self, data_loader, net_constructor): `````` Nicola Gatto committed Apr 08, 2019 175 176 `````` self._data_loader = data_loader self._net_creator = net_constructor `````` Sebastian Nickels committed Jun 21, 2019 177 `````` self._networks = {} `````` Nicola Gatto committed Apr 08, 2019 178 179 180 181 `````` def train(self, batch_size=64, num_epoch=10, eval_metric='acc', `````` Sebastian Nickels committed Oct 18, 2019 182 `````` eval_metric_params={}, `````` Sebastian Nickels committed Dec 20, 2019 183 `````` eval_train=False, `````` Eyüp Harputlu committed Jun 05, 2019 184 185 `````` loss ='softmax_cross_entropy', loss_params={}, `````` Nicola Gatto committed Apr 08, 2019 186 187 188 189 `````` optimizer='adam', optimizer_params=(('learning_rate', 0.001),), load_checkpoint=True, checkpoint_period=5, `````` Sebastian Nickels committed Dec 20, 2019 190 191 `````` log_period=50, context='gpu', `````` Christian Fuß committed Nov 05, 2019 192 `````` save_attention_image=False, `````` 193 `````` use_teacher_forcing=False, `````` Julian Dierkes committed Jan 03, 2020 194 `````` normalize=True, `````` Sebastian Nickels committed Jan 10, 2020 195 196 `````` shuffle_data=False, clip_global_grad_norm=None, `````` Julian Dierkes committed Jan 03, 2020 197 `````` preprocessing = False): `````` Nicola Gatto committed Apr 08, 2019 198 199 200 201 202 203 204 `````` if context == 'gpu': mx_context = mx.gpu() elif context == 'cpu': mx_context = mx.cpu() else: logging.error("Context argument is '" + context + "'. Only 'cpu' and 'gpu are valid arguments'.") `````` Julian Dierkes committed Jan 03, 2020 205 206 `````` if preprocessing: preproc_lib = "CNNPreprocessor_VGG16_executor" `````` Sebastian Nickels committed Jan 10, 2020 207 `````` train_iter, test_iter, data_mean, data_std, train_images, test_images = self._data_loader.load_preprocessed_data(batch_size, preproc_lib, shuffle_data) `````` Julian Dierkes committed Jan 03, 2020 208 `````` else: `````` Sebastian Nickels committed Jan 10, 2020 209 `````` train_iter, test_iter, data_mean, data_std, train_images, test_images = self._data_loader.load_data(batch_size, shuffle_data) `````` Julian Dierkes committed Jan 03, 2020 210 `````` `````` Nicola Gatto committed Apr 08, 2019 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 `````` if 'weight_decay' in optimizer_params: optimizer_params['wd'] = optimizer_params['weight_decay'] del optimizer_params['weight_decay'] if 'learning_rate_decay' in optimizer_params: min_learning_rate = 1e-08 if 'learning_rate_minimum' in optimizer_params: min_learning_rate = optimizer_params['learning_rate_minimum'] del optimizer_params['learning_rate_minimum'] optimizer_params['lr_scheduler'] = mx.lr_scheduler.FactorScheduler( optimizer_params['step_size'], factor=optimizer_params['learning_rate_decay'], stop_factor_lr=min_learning_rate) del optimizer_params['step_size'] del optimizer_params['learning_rate_decay'] `````` Sebastian Nickels committed Jun 21, 2019 226 227 228 229 `````` if normalize: self._net_creator.construct(context=mx_context, data_mean=data_mean, data_std=data_std) else: self._net_creator.construct(context=mx_context) `````` Nicola Gatto committed Apr 08, 2019 230 231 232 `````` begin_epoch = 0 if load_checkpoint: `````` Sebastian Nickels committed Jan 10, 2020 233 `````` begin_epoch = self._net_creator.load(mx_context) `````` Nicola Gatto committed Apr 08, 2019 234 235 236 237 `````` else: if os.path.isdir(self._net_creator._model_dir_): shutil.rmtree(self._net_creator._model_dir_) `````` Sebastian Nickels committed Jun 21, 2019 238 `````` self._networks = self._net_creator.networks `````` Nicola Gatto committed Apr 08, 2019 239 240 241 242 243 244 245 `````` try: os.makedirs(self._net_creator._model_dir_) except OSError: if not os.path.isdir(self._net_creator._model_dir_): raise `````` Sebastian Nickels committed Oct 31, 2019 246 `````` trainers = [mx.gluon.Trainer(network.collect_params(), optimizer, optimizer_params) for network in self._networks.values() if len(network.collect_params().values()) != 0] `````` Nicola Gatto committed Apr 08, 2019 247 `````` `````` Eyüp Harputlu committed Jun 05, 2019 248 249 `````` margin = loss_params['margin'] if 'margin' in loss_params else 1.0 sparseLabel = loss_params['sparse_label'] if 'sparse_label' in loss_params else True `````` 250 `````` ignore_indices = [loss_params['ignore_indices']] if 'ignore_indices' in loss_params else [] `````` Eyüp Harputlu committed Jun 05, 2019 251 252 `````` if loss == 'softmax_cross_entropy': fromLogits = loss_params['from_logits'] if 'from_logits' in loss_params else False `````` 253 `````` loss_function = mx.gluon.loss.SoftmaxCrossEntropyLoss(from_logits=fromLogits, sparse_label=sparseLabel) `````` Christian Fuß committed Nov 26, 2019 254 `````` elif loss == 'softmax_cross_entropy_ignore_indices': `````` 255 `````` fromLogits = loss_params['from_logits'] if 'from_logits' in loss_params else False `````` Christian Fuß committed Nov 05, 2019 256 `````` loss_function = SoftmaxCrossEntropyLossIgnoreIndices(ignore_indices=ignore_indices, from_logits=fromLogits, sparse_label=sparseLabel) `````` Eyüp Harputlu committed Jun 05, 2019 257 `````` elif loss == 'sigmoid_binary_cross_entropy': `````` Nicola Gatto committed Apr 08, 2019 258 `````` loss_function = mx.gluon.loss.SigmoidBinaryCrossEntropyLoss() `````` Eyüp Harputlu committed Jun 05, 2019 259 260 261 `````` elif loss == 'cross_entropy': loss_function = CrossEntropyLoss(sparse_label=sparseLabel) elif loss == 'l2': `````` Nicola Gatto committed Apr 08, 2019 262 `````` loss_function = mx.gluon.loss.L2Loss() `````` Eyüp Harputlu committed Jun 05, 2019 263 `````` elif loss == 'l1': `````` Nicola Gatto committed Apr 08, 2019 264 `````` loss_function = mx.gluon.loss.L2Loss() `````` Eyüp Harputlu committed Jun 05, 2019 265 266 267 268 269 270 271 272 273 274 275 276 277 `````` elif loss == 'huber': rho = loss_params['rho'] if 'rho' in loss_params else 1 loss_function = mx.gluon.loss.HuberLoss(rho=rho) elif loss == 'hinge': loss_function = mx.gluon.loss.HingeLoss(margin=margin) elif loss == 'squared_hinge': loss_function = mx.gluon.loss.SquaredHingeLoss(margin=margin) elif loss == 'logistic': labelFormat = loss_params['label_format'] if 'label_format' in loss_params else 'signed' loss_function = mx.gluon.loss.LogisticLoss(label_format=labelFormat) elif loss == 'kullback_leibler': fromLogits = loss_params['from_logits'] if 'from_logits' in loss_params else True loss_function = mx.gluon.loss.KLDivLoss(from_logits=fromLogits) `````` Eyüp Harputlu committed Jun 24, 2019 278 279 `````` elif loss == 'log_cosh': loss_function = LogCoshLoss() `````` Eyüp Harputlu committed Jun 05, 2019 280 281 `````` else: logging.error("Invalid loss parameter.") `````` Nicola Gatto committed Apr 08, 2019 282 283 284 285 `````` tic = None for epoch in range(begin_epoch, begin_epoch + num_epoch): `````` Sebastian Nickels committed Jan 10, 2020 286 287 288 289 290 291 292 293 294 `````` if shuffle_data: if preprocessing: preproc_lib = "CNNPreprocessor_VGG16_executor" train_iter, test_iter, data_mean, data_std, train_images, test_images = self._data_loader.load_preprocessed_data(batch_size, preproc_lib, shuffle_data) else: train_iter, test_iter, data_mean, data_std, train_images, test_images = self._data_loader.load_data(batch_size, shuffle_data) global_loss_train = 0.0 train_batches = 0 `````` Sebastian Nickels committed Dec 20, 2019 295 296 `````` loss_total = 0 `````` Nicola Gatto committed Apr 08, 2019 297 298 `````` train_iter.reset() for batch_i, batch in enumerate(train_iter): `````` 299 300 `````` with autograd.record(): labels = [batch.label[i].as_in_context(mx_context) for i in range(1)] `````` Sebastian Nickels committed May 26, 2019 301 `````` `````` 302 303 `````` data_ = batch.data[0].as_in_context(mx_context) `````` Sebastian Nickels committed Dec 20, 2019 304 `````` predictions_ = mx.nd.zeros((batch_size, 1000,), ctx=mx_context) `````` Christian Fuß committed Oct 08, 2019 305 `````` `````` Sebastian Nickels committed Aug 12, 2019 306 `````` `````` 307 308 `````` nd.waitall() `````` Christian Fuß committed Sep 10, 2019 309 `````` lossList = [] `````` 310 `````` `````` Sebastian Nickels committed Aug 12, 2019 311 `````` predictions_ = self._networks[0](data_) `````` 312 313 `````` lossList.append(loss_function(predictions_, labels[0])) `````` Sebastian Nickels committed Jun 06, 2019 314 `````` `````` Christian Fuß committed Sep 10, 2019 315 316 317 `````` loss = 0 for element in lossList: loss = loss + element `````` Nicola Gatto committed Apr 08, 2019 318 319 `````` loss.backward() `````` Sebastian Nickels committed Jun 21, 2019 320 `````` `````` Sebastian Nickels committed Dec 20, 2019 321 322 `````` loss_total += loss.sum().asscalar() `````` Sebastian Nickels committed Jan 10, 2020 323 324 325 `````` global_loss_train += float(loss.mean().asscalar()) train_batches += 1 `````` Sebastian Nickels committed Jan 10, 2020 326 327 328 329 330 331 332 333 `````` if clip_global_grad_norm: grads = [] for network in self._networks.values(): grads.extend([param.grad(mx_context) for param in network.collect_params().values()]) gluon.utils.clip_global_norm(grads, clip_global_grad_norm) `````` Sebastian Nickels committed Jun 21, 2019 334 335 `````` for trainer in trainers: trainer.step(batch_size) `````` Nicola Gatto committed Apr 08, 2019 336 337 338 339 `````` if tic is None: tic = time.time() else: `````` Sebastian Nickels committed Dec 20, 2019 340 `````` if batch_i % log_period == 0: `````` Nicola Gatto committed Apr 08, 2019 341 `````` try: `````` Sebastian Nickels committed Dec 20, 2019 342 `````` speed = log_period * batch_size / (time.time() - tic) `````` Nicola Gatto committed Apr 08, 2019 343 344 345 `````` except ZeroDivisionError: speed = float("inf") `````` Sebastian Nickels committed Dec 20, 2019 346 347 348 349 `````` loss_avg = loss_total / (batch_size * log_period) loss_total = 0 logging.info("Epoch[%d] Batch[%d] Speed: %.2f samples/sec Loss: %.5f" % (epoch, batch_i, speed, loss_avg)) `````` Nicola Gatto committed Apr 08, 2019 350 351 352 `````` tic = time.time() `````` Sebastian Nickels committed Jan 10, 2020 353 354 355 `````` if train_batches > 0: global_loss_train /= train_batches `````` Nicola Gatto committed Apr 08, 2019 356 357 `````` tic = None `````` Sebastian Nickels committed Dec 20, 2019 358 359 360 361 362 `````` if eval_train: train_iter.reset() metric = mx.metric.create(eval_metric, **eval_metric_params) for batch_i, batch in enumerate(train_iter): `````` 363 `````` labels = [batch.label[i].as_in_context(mx_context) for i in range(1)] `````` Sebastian Nickels committed Jun 06, 2019 364 `````` `````` 365 `````` data_ = batch.data[0].as_in_context(mx_context) `````` Sebastian Nickels committed Jun 06, 2019 366 `````` `````` Sebastian Nickels committed Dec 20, 2019 367 `````` predictions_ = mx.nd.zeros((batch_size, 1000,), ctx=mx_context) `````` Sebastian Nickels committed Aug 12, 2019 368 `````` `````` Sebastian Nickels committed May 26, 2019 369 `````` `````` 370 371 `````` nd.waitall() `````` 372 `````` outputs = [] `````` Sebastian Nickels committed Jan 10, 2020 373 374 `````` lossList = [] attentionList = [] `````` Sebastian Nickels committed Aug 12, 2019 375 `````` predictions_ = self._networks[0](data_) `````` 376 `````` `````` Christian Fuß committed Sep 23, 2019 377 `````` outputs.append(predictions_) `````` Sebastian Nickels committed Jan 10, 2020 378 `````` lossList.append(loss_function(predictions_, labels[0])) `````` Sebastian Nickels committed May 26, 2019 379 `````` `````` Christian Fuß committed Nov 05, 2019 380 381 `````` if save_attention_image == "True": `````` Christian Fuß committed Dec 06, 2019 382 383 `````` import matplotlib matplotlib.use('Agg') `````` Christian Fuß committed Nov 05, 2019 384 385 386 387 388 389 390 `````` import matplotlib.pyplot as plt logging.getLogger('matplotlib').setLevel(logging.ERROR) if(os.path.isfile('src/test/resources/training_data/Show_attend_tell/dict.pkl')): with open('src/test/resources/training_data/Show_attend_tell/dict.pkl', 'rb') as f: dict = pickle.load(f) `````` Sebastian Nickels committed Dec 20, 2019 391 392 393 394 `````` plt.clf() fig = plt.figure(figsize=(15,15)) max_length = len(labels)-1 `````` 395 `````` ax = fig.add_subplot(max_length//3, max_length//4, 1) `````` Sebastian Nickels committed Dec 20, 2019 396 `````` ax.imshow(train_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` 397 `````` `````` Christian Fuß committed Nov 05, 2019 398 399 `````` for l in range(max_length): attention = attentionList[l] `````` Christian Fuß committed Nov 26, 2019 400 `````` attention = mx.nd.slice_axis(attention, axis=0, begin=0, end=1).squeeze() `````` Christian Fuß committed Nov 05, 2019 401 `````` attention_resized = np.resize(attention.asnumpy(), (8, 8)) `````` 402 `````` ax = fig.add_subplot(max_length//3, max_length//4, l+2) `````` Christian Fuß committed Nov 26, 2019 403 404 405 `````` if int(labels[l+1][0].asscalar()) > len(dict): ax.set_title("") elif dict[int(labels[l+1][0].asscalar())] == "": `````` 406 `````` ax.set_title(".") `````` Sebastian Nickels committed Dec 20, 2019 407 `````` img = ax.imshow(train_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` 408 409 410 411 `````` ax.imshow(attention_resized, cmap='gray', alpha=0.6, extent=img.get_extent()) break else: ax.set_title(dict[int(labels[l+1][0].asscalar())]) `````` Sebastian Nickels committed Dec 20, 2019 412 `````` img = ax.imshow(train_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` Christian Fuß committed Nov 26, 2019 413 `````` ax.imshow(attention_resized, cmap='gray', alpha=0.6, extent=img.get_extent()) `````` Christian Fuß committed Nov 05, 2019 414 415 416 417 `````` plt.tight_layout() target_dir = 'target/attention_images' if not os.path.exists(target_dir): `````` Christian Fuß committed Nov 26, 2019 418 `````` os.makedirs(target_dir) `````` Christian Fuß committed Nov 05, 2019 419 420 421 `````` plt.savefig(target_dir + '/attention_train.png') plt.close() `````` Sebastian Nickels committed Dec 20, 2019 422 423 424 425 426 427 `````` predictions = [] for output_name in outputs: if mx.nd.shape_array(mx.nd.squeeze(output_name)).size > 1: predictions.append(mx.nd.argmax(output_name, axis=1)) else: predictions.append(output_name) `````` Sebastian Nickels committed Jun 06, 2019 428 `````` `````` Sebastian Nickels committed Dec 20, 2019 429 430 431 432 `````` metric.update(preds=predictions, labels=labels) train_metric_score = metric.get()[1] else: train_metric_score = 0 `````` Nicola Gatto committed Apr 08, 2019 433 `````` `````` Sebastian Nickels committed Jan 10, 2020 434 435 436 `````` global_loss_test = 0.0 test_batches = 0 `````` Nicola Gatto committed Apr 08, 2019 437 `````` test_iter.reset() `````` Sebastian Nickels committed Oct 18, 2019 438 `````` metric = mx.metric.create(eval_metric, **eval_metric_params) `````` Nicola Gatto committed Apr 08, 2019 439 `````` for batch_i, batch in enumerate(test_iter): `````` Sebastian Nickels committed Jan 10, 2020 440 `````` if True: `````` 441 `````` labels = [batch.label[i].as_in_context(mx_context) for i in range(1)] `````` Sebastian Nickels committed Jun 06, 2019 442 `````` `````` 443 `````` data_ = batch.data[0].as_in_context(mx_context) `````` Sebastian Nickels committed Jun 06, 2019 444 `````` `````` Sebastian Nickels committed Dec 20, 2019 445 `````` predictions_ = mx.nd.zeros((batch_size, 1000,), ctx=mx_context) `````` Sebastian Nickels committed Aug 12, 2019 446 `````` `````` 447 `````` `````` 448 449 `````` nd.waitall() `````` 450 `````` outputs = [] `````` Sebastian Nickels committed Jan 10, 2020 451 452 `````` lossList = [] attentionList = [] `````` Sebastian Nickels committed Aug 12, 2019 453 `````` predictions_ = self._networks[0](data_) `````` 454 `````` `````` Christian Fuß committed Sep 23, 2019 455 `````` outputs.append(predictions_) `````` Sebastian Nickels committed Jan 10, 2020 456 `````` lossList.append(loss_function(predictions_, labels[0])) `````` 457 `````` `````` Christian Fuß committed Nov 05, 2019 458 459 `````` if save_attention_image == "True": `````` Sebastian Nickels committed Dec 20, 2019 460 461 462 463 464 465 466 467 468 469 `````` if not eval_train: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt logging.getLogger('matplotlib').setLevel(logging.ERROR) if(os.path.isfile('src/test/resources/training_data/Show_attend_tell/dict.pkl')): with open('src/test/resources/training_data/Show_attend_tell/dict.pkl', 'rb') as f: dict = pickle.load(f) `````` Christian Fuß committed Nov 05, 2019 470 `````` plt.clf() `````` 471 `````` fig = plt.figure(figsize=(15,15)) `````` Christian Fuß committed Nov 05, 2019 472 473 `````` max_length = len(labels)-1 `````` 474 `````` ax = fig.add_subplot(max_length//3, max_length//4, 1) `````` Sebastian Nickels committed Dec 20, 2019 475 `````` ax.imshow(test_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` 476 `````` `````` Christian Fuß committed Nov 05, 2019 477 478 `````` for l in range(max_length): attention = attentionList[l] `````` Christian Fuß committed Nov 26, 2019 479 `````` attention = mx.nd.slice_axis(attention, axis=0, begin=0, end=1).squeeze() `````` Christian Fuß committed Nov 05, 2019 480 `````` attention_resized = np.resize(attention.asnumpy(), (8, 8)) `````` 481 `````` ax = fig.add_subplot(max_length//3, max_length//4, l+2) `````` Christian Fuß committed Nov 26, 2019 482 483 484 `````` if int(mx.nd.slice_axis(outputs[l+1], axis=0, begin=0, end=1).squeeze().asscalar()) > len(dict): ax.set_title("") elif dict[int(mx.nd.slice_axis(outputs[l+1], axis=0, begin=0, end=1).squeeze().asscalar())] == "": `````` 485 `````` ax.set_title(".") `````` Sebastian Nickels committed Dec 20, 2019 486 `````` img = ax.imshow(test_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` 487 488 489 `````` ax.imshow(attention_resized, cmap='gray', alpha=0.6, extent=img.get_extent()) break else: `````` Christian Fuß committed Nov 26, 2019 490 `````` ax.set_title(dict[int(mx.nd.slice_axis(outputs[l+1], axis=0, begin=0, end=1).squeeze().asscalar())]) `````` Sebastian Nickels committed Dec 20, 2019 491 `````` img = ax.imshow(test_images[0+batch_size*(batch_i)].transpose(1,2,0)) `````` Christian Fuß committed Nov 26, 2019 492 `````` ax.imshow(attention_resized, cmap='gray', alpha=0.6, extent=img.get_extent()) `````` Christian Fuß committed Nov 05, 2019 493 494 `````` plt.tight_layout() `````` Sebastian Nickels committed Dec 20, 2019 495 496 497 `````` target_dir = 'target/attention_images' if not os.path.exists(target_dir): os.makedirs(target_dir) `````` Christian Fuß committed Nov 05, 2019 498 499 `````` plt.savefig(target_dir + '/attention_test.png') plt.close() `````` Sebastian Nickels committed Jan 10, 2020 500 501 502 503 504 505 `````` loss = 0 for element in lossList: loss = loss + element global_loss_test += float(loss.mean().asscalar()) test_batches += 1 `````` Christian Fuß committed Nov 05, 2019 506 `````` `````` Christian Fuß committed Sep 09, 2019 507 `````` predictions = [] `````` Christian Fuß committed Sep 23, 2019 508 `````` for output_name in outputs: `````` Sebastian Nickels committed Oct 30, 2019 509 `````` if mx.nd.shape_array(mx.nd.squeeze(output_name)).size > 1: `````` Christian Fuß committed Sep 09, 2019 510 511 512 513 `````` predictions.append(mx.nd.argmax(output_name, axis=1)) #ArgMax already applied else: predictions.append(output_name) `````` Sebastian Nickels committed May 26, 2019 514 `````` `````` Sebastian Nickels committed Jun 06, 2019 515 `````` metric.update(preds=predictions, labels=labels) `````` Nicola Gatto committed Apr 08, 2019 516 517 `````` test_metric_score = metric.get()[1] `````` Sebastian Nickels committed Jan 10, 2020 518 519 `````` if test_batches > 0: global_loss_test /= test_batches `````` Nicola Gatto committed Apr 08, 2019 520 `````` `````` Sebastian Nickels committed Jan 10, 2020 521 `````` logging.info("Epoch[%d] Train metric: %f, Test metric: %f, Train loss: %f, Test loss: %f" % (epoch, train_metric_score, test_metric_score, global_loss_train, global_loss_test)) `````` Christian Fuß committed Aug 28, 2019 522 `````` `````` Nicola Gatto committed Apr 08, 2019 523 `````` if (epoch - begin_epoch) % checkpoint_period == 0: `````` Sebastian Nickels committed Jun 21, 2019 524 525 `````` for i, network in self._networks.items(): network.save_parameters(self.parameter_path(i) + '-' + str(epoch).zfill(4) + '.params') `````` Nicola Gatto committed Apr 08, 2019 526 `````` `````` Sebastian Nickels committed Jun 21, 2019 527 528 529 `````` for i, network in self._networks.items(): network.save_parameters(self.parameter_path(i) + '-' + str(num_epoch + begin_epoch).zfill(4) + '.params') network.export(self.parameter_path(i) + '_newest', epoch=0) `````` Nicola Gatto committed Apr 08, 2019 530 `````` `````` Sebastian Nickels committed Jun 21, 2019 531 `````` def parameter_path(self, index): `````` Bernhard Rumpe committed Aug 23, 2019 532 `` return self._net_creator._model_dir_ + self._net_creator._model_prefix_ + '_' + str(index)``