def preprocess_imdb(train_tokenized, test_tokenized, train_data, test_data, vocab): """Preprocess the IMDB data set for sentiment analysis.""" def encode_samples(tokenized_samples, vocab): features = [] for sample in tokenized_samples: feature = [] for token in sample: if token in vocab.token_to_idx: feature.append(vocab.token_to_idx[token]) else: feature.append(0) features.append(feature) return features def pad_samples(features, maxlen=500, PAD=0): padded_features = [] for feature in features: if len(feature) > maxlen: padded_feature = feature[:maxlen] else: padded_feature = feature while len(padded_feature) < maxlen: padded_feature.append(PAD) padded_features.append(padded_feature) return padded_features train_features = encode_samples(train_tokenized, vocab) test_features = encode_samples(test_tokenized, vocab) train_features = nd.array(pad_samples(train_features, 500, 0)) test_features = nd.array(pad_samples(test_features, 500, 0)) train_labels = nd.array([score for _, score in train_data]) test_labels = nd.array([score for _, score in test_data]) return train_features, test_features, train_labels, test_labels
def __init__(self, is_train, crop_size, voc_dir, colormap2label): self.rgb_mean = nd.array([0.485, 0.456, 0.406]) self.rgb_std = nd.array([0.229, 0.224, 0.225]) self.crop_size = crop_size data, labels = read_voc_images(root=voc_dir, is_train=is_train) self.data = [self.normalize_image(im) for im in self.filter(data)] self.labels = self.filter(labels) self.colormap2label = colormap2label print('read ' + str(len(self.data)) + ' examples')
def plotscore(w, d): xgrid = np.arange(-3, 3, 0.02) ygrid = np.arange(-3, 3, 0.02) xx, yy = np.meshgrid(xgrid, ygrid) zz = nd.zeros(shape=(xgrid.size, ygrid.size, 2)) zz[:, :, 0] = nd.array(xx) zz[:, :, 1] = nd.array(yy) vv = nd.dot(zz, w) + b CS = plt.contour(xgrid, ygrid, vv.asnumpy()) plt.clabel(CS, inline=1, fontsize=10)
def predict(net, data, label): data = nd.array(data) label = nd.array(label) hidden = net.begin_state(func=mx.nd.zeros,batch_size = data.shape[0],ctx=mx.cpu()) dd = nd.array(data.reshape((data.shape[0],5,11)).swapaxes(0,1)) output,hidden = net(dd,hidden) output = output.reshape((5,data.shape[0],1)) output = nd.sum(output,axis=0)/5 l = nd.argmax(output, axis=1) res = nd.mean(l==label) return res.asscalar()
def preprocess_imdb(data, vocab): """Preprocess the IMDB data set for sentiment analysis.""" max_l = 500 def pad(x): return x[:max_l] if len(x) > max_l else x + [0] * (max_l - len(x)) tokenized_data = get_tokenized_imdb(data) features = nd.array([pad(vocab.to_indices(x)) for x in tokenized_data]) labels = nd.array([score for _, score in data]) return features, labels
def reset(self): """Resets the iterator to the beginning of the data.""" self.curr_idx = 0 random.shuffle(self.idx) for i in range(len(self.data)): data, labels = self.data[i], self.labels[i] p = np.random.permutation(len(data)) self.data[i], self.labels[i] = data[p], labels[p] self.nddata = [] self.ndlabel = [] for buck,label_buck in zip(self.data, self.labels): self.nddata.append(nd.array(buck, dtype=self.dtype)) self.ndlabel.append(nd.array(label_buck, dtype=self.dtype))
def __iter__(self): data = self.dataset[:] X = data[0] y = nd.array(data[1]) n = X.shape[0] if self.shuffle: idx = np.arange(n) np.random.shuffle(idx) X = nd.array(X.asnumpy()[idx]) y = nd.array(y.asnumpy()[idx]) for i in range(n // self.batch_size): yield (X[i * self.batch_size:(i + 1) * self.batch_size], y[i * self.batch_size:(i + 1) * self.batch_size])
def forward(self, words1, words2, words3): # pylint: disable=arguments-differ """Implement forward computation. Parameters ---------- words1 : NDArray Word indices. words2 : NDArray Word indices. words3 : NDArray Word indices. Returns ------- predicted_indices : NDArray Predicted indices of shape (batch_size, k) """ pred_idxs = self.analogy(words1, words2, words3) if self.exclude_question_words: orig_context = pred_idxs.context pred_idxs = pred_idxs.asnumpy().tolist() pred_idxs = [[ idx for idx in row if idx != w1 and idx != w2 and idx != w3 ] for row, w1, w2, w3 in zip(pred_idxs, words1, words2, words3)] pred_idxs = [p[:self.k] for p in pred_idxs] pred_idxs = nd.array(pred_idxs, ctx=orig_context) return pred_idxs
def run(self, inputs, **kwargs): """Run model inference and return the result Parameters ---------- inputs : numpy array input to run a layer on Returns ------- params : numpy array result obtained after running the inference on mxnet """ # create module, passing cpu context if self.device == 'CPU': ctx = mx.cpu() else: raise NotImplementedError("ONNX tests are run only for CPU context.") # run inference net_inputs = [nd.array(input_data, ctx=ctx) for input_data in inputs] net_outputs = self.net(*net_inputs) results = [] results.extend([o for o in net_outputs.asnumpy()]) result = np.array(results) return [result]
def build_array(lines, vocab, max_len, is_source): lines = [vocab[line] for line in lines] if not is_source: lines = [[vocab.bos] + line + [vocab.eos] for line in lines] array = nd.array([pad(line, max_len, vocab.pad) for line in lines]) valid_len = (array != vocab.pad).sum(axis=1) return array, valid_len
def data_iter_random(corpus_indices, batch_size, num_steps, ctx=None): """Sample mini-batches in a random order from sequential data.""" num_examples = (len(corpus_indices) - 1) // num_steps epoch_size = num_examples // batch_size example_indices = list(range(num_examples)) random.shuffle(example_indices) def _data(pos): return corpus_indices[pos : pos+num_steps] for i in range(epoch_size): i = i * batch_size batch_indices = example_indices[i : i+batch_size] X = nd.array( [_data(j * num_steps) for j in batch_indices], ctx=ctx) Y = nd.array( [_data(j * num_steps + 1) for j in batch_indices], ctx=ctx) yield X, Y
def data_iter(batch_size, num_examples, X, y): """walk around dataset""" idx = list(range(num_examples)) random.shuffle(idx) for i in range(0, num_examples, batch_size): j = nd.array(idx[i: min(i + batch_size, num_examples)]) yield X.take(j), y.take(j)
def data_iter(batch_size, num_examples, features, labels): """遍历数据集。""" indices = list(range(num_examples)) random.shuffle(indices) for i in range(0, num_examples, batch_size): j = nd.array(indices[i: min(i + batch_size, num_examples)]) yield features.take(j), labels.take(j)
def __setitem__(self, tokens, new_embedding): """Updates embedding vectors for tokens. If self.allow_extend is True, vectors for previously unknown tokens can be introduced. Parameters ---------- tokens : hashable object or a list or tuple of hashable objects A token or a list of tokens whose embedding vector are to be updated. new_embedding : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embedding of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D. """ if self.allow_extend and self._idx_to_vec is None: # Initialize self._idx_to_vec assert C.UNK_IDX == 0 self._idx_to_vec = self._init_unknown_vec(shape=(1, new_embedding.shape[-1])) tokens = self._check_vector_update(tokens, new_embedding) if self.allow_extend: # Add new / previously unknown tokens for token in filter(lambda t: t not in self._token_to_idx, tokens): idx = len(self._token_to_idx) self._token_to_idx[token] = idx self._idx_to_token.append(token) num_extended = len(self._token_to_idx) - self.idx_to_vec.shape[0] if num_extended == 1: warnings.warn( 'When adding new tokens via TokenEmbedding.__setitem__ ' 'the internal embedding matrix needs to be reallocated. ' 'Users are therefore encouraged to batch their updates ' '(i.e. add multiple new tokens at a time).') # Extend shape of idx_to_vec idx_to_vec = nd.zeros(shape=(len(self._token_to_idx), self.idx_to_vec.shape[1])) idx_to_vec[:self.idx_to_vec.shape[0]] = self._idx_to_vec self._idx_to_vec = idx_to_vec indices = [] for token in tokens: if token in self._token_to_idx: indices.append(self._token_to_idx[token]) else: if self.unknown_token: raise KeyError(('Token "{}" is unknown. To update the embedding vector for an' ' unknown token, please explicitly include "{}" as the ' '`unknown_token` in `tokens`. This is to avoid unintended ' 'updates.').format(token, self._idx_to_token[C.UNK_IDX])) else: raise KeyError(('Token "{}" is unknown. Updating the embedding vector for an ' 'unknown token is not allowed because `unknown_token` is not ' 'specified.').format(token)) self._idx_to_vec[nd.array(indices)] = new_embedding
def data_iter(batch_size, features, labels): """Iterate through a data set.""" num_examples = len(features) indices = list(range(num_examples)) random.shuffle(indices) for i in range(0, num_examples, batch_size): j = nd.array(indices[i: min(i + batch_size, num_examples)]) yield features.take(j), labels.take(j)
def evals(net, adata ,alabel, batch_size): hidden = net.begin_state(func=mx.nd.zeros,batch_size = batch_size,ctx=mx.cpu()) dataLoader = DataLoader(adata, alabel) tl = 0 for data, label in dataLoader.dataIter(batch_size): label = nd.array(label) #label = nd.ones(shape=(5,batch_size)) * label #label = label.reshape((-1,)) dd = nd.array(data.reshape((batch_size,5,11)).swapaxes(0,1)) #hidden = detach(hidden) output,hidden = net(dd, hidden) output = output.reshape((5,batch_size,1)) output = nd.sum(output,axis=0)/5 lv = loss(output, label) tl += nd.sum(lv).asscalar() return tl / len(adata)
def try_gpu(): """If GPU is available, return mx.gpu(0); else return mx.cpu().""" try: ctx = mx.gpu() _ = nd.array([0], ctx=ctx) except mx.base.MXNetError: ctx = mx.cpu() return ctx
def try_gpu(): """If GPU is available, return mx.gpu(0); else return mx.cpu()""" try: ctx = mx.gpu() _ = nd.array([0], ctx=ctx) except: ctx = mx.cpu() return ctx
def grad_clipping(params, theta, ctx): """Clip the gradient.""" norm = nd.array([0], ctx) for param in params: norm += (param.grad ** 2).sum() norm = norm.sqrt().asscalar() if norm > theta: for param in params: param.grad[:] *= theta / norm
def grad_clipping(params, theta, ctx): """Gradient clipping.""" if theta is not None: norm = nd.array([0.0], ctx) for p in params: norm += nd.sum(p.grad ** 2) norm = nd.sqrt(norm).asscalar() if norm > theta: for p in params: p.grad[:] *= theta / norm
def grad_clipping(params, clipping_norm, ctx): """Gradient clipping.""" if clipping_norm is not None: norm = nd.array([0.0], ctx) for p in params: norm += nd.sum(p.grad ** 2) norm = nd.sqrt(norm).asscalar() if norm > clipping_norm: for p in params: p.grad[:] *= clipping_norm / norm
def load_data_imdb(batch_size, max_len=500): """Download an IMDB dataset, return the vocabulary and iterators.""" data_dir = '../data' url = 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz' fname = gutils.download(url, data_dir) with tarfile.open(fname, 'r') as f: f.extractall(data_dir) def read_imdb(folder='train'): data, labels = [], [] for label in ['pos', 'neg']: folder_name = os.path.join(data_dir, 'aclImdb', folder, label) for file in os.listdir(folder_name): with open(os.path.join(folder_name, file), 'rb') as f: review = f.read().decode('utf-8').replace('\n', '') data.append(review) labels.append(1 if label == 'pos' else 0) return data, labels train_data, test_data = read_imdb('train'), read_imdb('test') def tokenize(sentences): return [line.split(' ') for line in sentences] train_tokens = tokenize(train_data[0]) test_tokens = tokenize(test_data[0]) vocab = Vocab([tk for line in train_tokens for tk in line], min_freq=5) def pad(x): return x[:max_len] if len(x) > max_len else x + [vocab.unk] * (max_len - len(x)) train_features = nd.array([pad(vocab[line]) for line in train_tokens]) test_features = nd.array([pad(vocab[line]) for line in test_tokens]) train_set = gdata.ArrayDataset(train_features, train_data[1]) test_set = gdata.ArrayDataset(test_features, test_data[1]) train_iter = gdata.DataLoader(train_set, batch_size, shuffle=True) test_iter = gdata.DataLoader(test_set, batch_size) return vocab, train_iter, test_iter
def translate_ch7(model, src_sentence, src_vocab, tgt_vocab, max_len, ctx): """Translate based on an encoder-decoder model with greedy search.""" src_tokens = src_vocab[src_sentence.lower().split(' ')] src_len = len(src_tokens) if src_len < max_len: src_tokens += [src_vocab.pad] * (max_len - src_len) enc_X = nd.array(src_tokens, ctx=ctx) enc_valid_length = nd.array([src_len], ctx=ctx) enc_outputs = model.encoder(enc_X.expand_dims(axis=0), enc_valid_length) dec_state = model.decoder.init_state(enc_outputs, enc_valid_length) dec_X = nd.array([tgt_vocab.bos], ctx=ctx).expand_dims(axis=0) predict_tokens = [] for _ in range(max_len): Y, dec_state = model.decoder(dec_X, dec_state) dec_X = Y.argmax(axis=2) py = dec_X.squeeze(axis=0).astype('int32').asscalar() if py == tgt_vocab.eos: break predict_tokens.append(py) return ' '.join(tgt_vocab.to_tokens(predict_tokens))
def saturation(src, low, high, p=0.5): """Saturation distortion.""" if np.random.uniform(0, 1) > p: alpha = np.random.uniform(low, high) gray = src * nd.array([[[0.299, 0.587, 0.114]]], ctx=src.context) gray = mx.nd.sum(gray, axis=2, keepdims=True) gray *= (1.0 - alpha) src *= alpha src += gray return src return src
def transform_fn(net, data, input_content_type, output_content_type): """ Transform a request using the Gluon model. Called once per request. :param net: The Gluon model. :param data: The request payload. :param input_content_type: The request content type. :param output_content_type: The (desired) response content type. :return: response payload and content type. """ ctx = mx.cpu() parsed = json.loads(data) trained_net, customer_index, product_index = net users = pd.DataFrame({'customer_id': parsed['customer_id']}).merge(customer_index, how='left')['user'].values items = pd.DataFrame({'product_id': parsed['product_id']}).merge(product_index, how='left')['item'].values predictions = trained_net(nd.array(users).as_in_context(ctx), nd.array(items).as_in_context(ctx)) response_body = json.dumps(predictions.asnumpy().tolist()) return response_body, output_content_type
def data_iter_random(corpus_indices, batch_size, num_steps, ctx=None): """Sample mini-batches in a random order from sequential data.""" # Subtract 1 because label indices are corresponding input indices + 1. num_examples = (len(corpus_indices) - 1) // num_steps epoch_size = num_examples // batch_size # Randomize samples. example_indices = list(range(num_examples)) random.shuffle(example_indices) def _data(pos): return corpus_indices[pos: pos + num_steps] for i in range(epoch_size): # Read batch_size random samples each time. i = i * batch_size batch_indices = example_indices[i: i + batch_size] data = nd.array( [_data(j * num_steps) for j in batch_indices], ctx=ctx) label = nd.array( [_data(j * num_steps + 1) for j in batch_indices], ctx=ctx) yield data, label
def train_and_predict_rnn(rnn, is_random_iter, num_epochs, num_steps, num_hiddens, lr, clipping_theta, batch_size, vocab_size, pred_period, pred_len, prefixes, get_params, get_inputs, ctx, corpus_indices, idx_to_char, char_to_idx, is_lstm=False): """Train an RNN model and predict the next item in the sequence.""" if is_random_iter: data_iter = data_iter_random else: data_iter = data_iter_consecutive params = get_params() loss = gloss.SoftmaxCrossEntropyLoss() for epoch in range(1, num_epochs + 1): if not is_random_iter: state_h = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx) if is_lstm: state_c = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx) train_l_sum = nd.array([0], ctx=ctx) train_l_cnt = 0 for X, Y in data_iter(corpus_indices, batch_size, num_steps, ctx): if is_random_iter: state_h = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx) if is_lstm: state_c = nd.zeros(shape=(batch_size, num_hiddens), ctx=ctx) else: state_h = state_h.detach() if is_lstm: state_c = state_c.detach() with autograd.record(): if is_lstm: outputs, state_h, state_c = rnn( get_inputs(X, vocab_size), state_h, state_c, *params) else: outputs, state_h = rnn( get_inputs(X, vocab_size), state_h, *params) y = Y.T.reshape((-1,)) outputs = nd.concat(*outputs, dim=0) l = loss(outputs, y) l.backward() grad_clipping(params, clipping_theta, ctx) sgd(params, lr, 1) train_l_sum = train_l_sum + l.sum() train_l_cnt += l.size if epoch % pred_period == 0: print("\nepoch %d, perplexity %f" % (epoch, (train_l_sum / train_l_cnt).exp().asscalar())) for prefix in prefixes: print(' - ', predict_rnn( rnn, prefix, pred_len, params, num_hiddens, vocab_size, ctx, idx_to_char, char_to_idx, get_inputs, is_lstm))
def resize_contain(src, size, fill=0): """Resize the image to fit in the given area while keeping aspect ratio. If both the height and the width in `size` are larger than the height and the width of input image, the image is placed on the center with an appropriate padding to match `size`. Otherwise, the input image is scaled to fit in a canvas whose size is `size` while preserving aspect ratio. Parameters ---------- src : mxnet.nd.NDArray The original image with HWC format. size : tuple Tuple of length 2 as (width, height). fill : int or float or array-like The value(s) for padded borders. If `fill` is numerical type, RGB channels will be padded with single value. Otherwise `fill` must have same length as image channels, which resulted in padding with per-channel values. Returns ------- mxnet.nd.NDArray Augmented image. tuple Tuple of (offset_x, offset_y, scaled_x, scaled_y) """ h, w, c = src.shape ow, oh = size scale_h = oh / h scale_w = ow / w scale = min(min(scale_h, scale_w), 1) scaled_x = int(w * scale) scaled_y = int(h * scale) if scale < 1: src = mx.image.imresize(src, scaled_x, scaled_y) off_y = (oh - scaled_y) // 2 if scaled_y < oh else 0 off_x = (ow - scaled_x) // 2 if scaled_x < ow else 0 # make canvas if isinstance(fill, numeric_types): dst = nd.full(shape=(oh, ow, c), val=fill, dtype=src.dtype) else: fill = nd.array(fill, ctx=src.context) if not c == fill.size: raise ValueError("Channel and fill size mismatch, {} vs {}".format(c, fill.size)) dst = nd.repeat(fill, repeats=oh * ow).reshape((oh, ow, c)) dst[off_y:off_y+scaled_y, off_x:off_x+scaled_x, :] = src return dst, (off_x, off_y, scaled_x, scaled_y)
def try_all_gpus(): """Return all available GPUs, or [mx.gpu()] if there is no GPU""" ctx_list = [] try: for i in range(16): ctx = mx.gpu(i) _ = nd.array([0], ctx=ctx) ctx_list.append(ctx) except: pass if not ctx_list: ctx_list = [mx.cpu()] return ctx_list
def data_iter_consecutive(corpus_indices, batch_size, num_steps, ctx=None): """Sample mini-batches in a consecutive order from sequential data.""" corpus_indices = nd.array(corpus_indices, ctx=ctx) data_len = len(corpus_indices) batch_len = data_len // batch_size indices = corpus_indices[0 : batch_size*batch_len].reshape(( batch_size, batch_len)) epoch_size = (batch_len - 1) // num_steps for i in range(epoch_size): i = i * num_steps X = indices[:, i : i+num_steps] Y = indices[:, i+1 : i+num_steps+1] yield X, Y
for url in urls) result = loop.run_until_complete(asyncio.gather(*download_tasks)) except Exception as e: print(e) urls = [] records = [] continue nd_img_list = [] succeed_ids = [] docs = [] for i, (f_ret, rec) in enumerate(zip(result, records)): try: pil_img = Image.open(f_ret[0]) nd_img_list.append( test_transform(nd.array(np.asarray(pil_img)))) new_rec = {} new_rec['_id'] = rec['_id'] new_rec['_int_id'] = rec['int_id'] new_rec.update(rec['_source']) docs.append(new_rec) except Exception as e: print(urls[i]) print(e) #nd_img_list = [test_transform(nd.array(np.asarray(Image.open(f_ret[0])))) for f_ret in result ] if len(nd_img_list) != len(records) or len(nd_img_list) < 2: if len(nd_img_list) < 2: print(urls[0]) print("caution,failed to download all pictures") print(result[0][1][0], result[0][1][1])
def predict(self, test_data, log_folder, params_folder, folder_holds_rst_pics, path_to_save_file, batch_size, gpu_id): """ Predict result with single GPU. :param test_data: Data for predicting. Instance of tuple. - orig_images_id: - orig_images_shape: - norm_images: - norm_centermap: :param params_folder: :param batch_size: :param gpu_id: :return: """ logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(), logging.FileHandler(log_folder + 'test_' + self._name + '_batch_' + str(batch_size))]) # 0.result predicted_result = {'images_id': [], 'keypoints': []} orig_keypoints = [] # 1.check params file params_file = self.utils_params_file(batch_size, 'check', params_folder)[2] if params_file is None: logging.info("No params files detected. Please train first.") return # 2.load params logging.info("Loading params file from detected file '%s'..." % params_file) self.collect_params().load(params_folder + params_file, ctx=mx.gpu(gpu_id)) # 3.predict batch_index = 0 while True: # (1) get data orig_images_id_batch, orig_images_batch, orig_images_shape_batch, orig_keypoints_batch, norm_images_batch, norm_centermap_batch, _, _ = \ test_data.get_batch_data(if_data_aug=False, loss_mode='softmax', batch_size=batch_size, batch_index=batch_index) if orig_images_id_batch is None and orig_images_batch is None and orig_images_shape_batch is None and \ norm_images_batch is None and norm_centermap_batch is None: break # (2) predict predicted_beliefMaps = self.forward(input_images=nd.array(norm_images_batch, ctx=mx.gpu(gpu_id)), center_maps=nd.array(norm_centermap_batch, ctx=mx.gpu(gpu_id)))[-1] #predicted_beliefMaps = None #for pred_BMs in _predicted_beliefMaps: # predicted_beliefMaps = pred_BMs if predicted_beliefMaps is None else (predicted_beliefMaps + pred_BMs) # (3) transfer predicted belief maps into original key points predicted_orig_keypoints_batch = self.transform_beliefMaps_into_origKeypoints( predicted_beliefMaps=predicted_beliefMaps.asnumpy(), orig_images_shape=orig_images_shape_batch) # (4) save for orig_image, pred_orig_keypoint, orig_images_id in zip(orig_images_batch, predicted_orig_keypoints_batch, orig_images_id_batch): # 1> predicted result predicted_result['images_id'].append(orig_images_id) predicted_result['keypoints'].append(pred_orig_keypoint) # 2> images with predicted result utils.show_image_and_keypoints(image=orig_image, keypoints=pred_orig_keypoint, image_name=os.path.splitext(orig_images_id.split('/')[-1])[0], save_folder=folder_holds_rst_pics) # (5) if original key points is not None, calculate NE if orig_keypoints_batch is not None: # 1> save original key points for orig_kp_batch in orig_keypoints_batch: orig_keypoints.append(orig_kp_batch) # 2> calculate NE NE = self.calculate_error(valid_keys=utils.keypoints_order[test_data.category], category=test_data.category, predicted_keypoints=predicted_orig_keypoints_batch, orig_keypoints=orig_keypoints_batch) logging.info("Batch[%d] predicting completed. NE: %f%%." % (batch_index, NE*100)) else: logging.info("Batch[%d] predicting completed." % batch_index) batch_index += 1 # 4.finish predicted_file = open(path_to_save_file, 'wb') pickle.dump(predicted_result, predicted_file) predicted_file.close() # 5.calculate total NE if len(orig_keypoints) != 0: NE = self.calculate_error(valid_keys=utils.keypoints_order[test_data.category], category=test_data.category, predicted_keypoints=predicted_result['keypoints'], orig_keypoints=orig_keypoints) logging.info("Predicting completed. NE: %.2f%%." % (NE*100)) else: logging.info("Predicting completed.")
# -*- coding:utf-8 -*- import mxnet as mx import matplotlib.pyplot as plt from mxnet import nd from yolo_v2 import yolo2_feature_spliter, yolo2_target, transform_size, transform_center import numpy as np from mxnet import gluon l1_loss = gluon.loss.L1Loss() RGB_MEAN = nd.array([123, 117, 104]) RGB_STD = nd.array([58.395, 57.12, 57.375]) #anchor_scales = [[3.2,5.6],[4,4],[5.6,3.2]] anchor_scales = [ [ 4.0, 4.8 ], #每一行表示一个预先选定的bbox形状,第一列为width,第二列为height。【【这里的数值不是在全图上的数值,而是根据网络输出feature map的大小(256/16=16)定的】】 [4.8, 4.8], [5.6, 4.0], [4.8, 8.0], [6.4, 6.4] ] class LossRecorder(mx.metric.EvalMetric): def __init__(self, name): super(LossRecorder, self).__init__(name) def update(self, labels, preds=0): for loss in labels:
import mxnet as mx from mxnet import nd, autograd x = nd.array([[1, 2], [3, 4]]) print(x) # [[1. 2.] # [3. 4.]] # <NDArray 2x2 @cpu(0)> # compute gradient ################################################################################ x.attach_grad() ## define the function f(x) = 2x² def f(x): return 2 * x**2 with autograd.record(): y = f(x) print(y) # [[ 2. 8.] # [18. 32.]] # <NDArray 2x2 @cpu(0)> ## backward propagation of y y.backward()
def train(): print('Start to train') logger = add_logger(args.log_dir, args.prefix, remove_previous_log=args.start_epoch == 0) check_ckpt(args.ckpt_dir, args.prefix) model3d = sio.loadmat(args.model3d) shape_pc = nd.array(model3d['shapePC']).transpose().as_in_context(ctx) exp_pc = nd.array(model3d['expPC']).transpose().as_in_context(ctx) trainset = SupervisedDataset(args.train_list) valset = SupervisedDataset(args.val_list) train_loader = gluon.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True) val_loader = gluon.data.DataLoader(valset, batch_size=args.batch_size, shuffle=False) inference = E2FAR(freeze=args.freeze) # initialize initialize_inference(inference, args.pretrained, args.start_epoch) lr_scheduler = multi_factor_scheduler(args.lr_steps, len(trainset), args.batch_size, args.lr_factor, args.start_epoch) trainer = gluon.Trainer(inference.collect_params(), optimizer='adam', optimizer_params={ 'learning_rate': args.lr, 'wd': args.wd, 'lr_scheduler': lr_scheduler }) criterion_shape = ProjectionL2Loss(shape_pc) criterion_exp = ProjectionL2Loss(exp_pc) metric_shape, metric_exp = mx.metric.Loss('shape-loss'), mx.metric.Loss( 'exp-loss') for cur_epoch in range(args.start_epoch + 1, args.epochs + 1): metric_shape.reset() metric_exp.reset() for i, batch in enumerate(train_loader): data = batch[0].as_in_context(ctx) gt_shape = batch[1].as_in_context(ctx) gt_exp = batch[2].as_in_context(ctx) with autograd.record(): preds_shape, preds_exp = inference(data) loss_shape = criterion_shape(preds_shape, gt_shape) loss_exp = criterion_exp(preds_exp, gt_exp) loss = loss_shape + 5 * loss_exp loss.backward() trainer.step(data.shape[0]) metric_shape.update(None, preds=loss_shape) metric_exp.update(None, preds=loss_exp) if i % args.log_interval == 0 and i > 0: logger.info( 'Epoch [%d] Batch [%d]: shape loss=%f, exp loss=%f, total loss=%f' % (cur_epoch, i, metric_shape.get()[1], metric_exp.get()[1], 0.001 * metric_shape.get()[1] + metric_exp.get()[1])) logger.info('Epoch [%d]: train-shape-loss=%f' % (cur_epoch, metric_shape.get()[1])) logger.info('Epoch [%d]: train-exp-loss=%f' % (cur_epoch, metric_exp.get()[1])) inference.save_params( os.path.join(args.ckpt_dir, args.prefix, '%s-%d.params' % (args.prefix, cur_epoch))) metric_shape.reset() metric_exp.reset() for i, batch in enumerate(val_loader): data = batch[0].as_in_context(ctx) gt_shape = batch[1].as_in_context(ctx) gt_exp = batch[2].as_in_context(ctx) preds_shape, preds_exp = inference(data) loss_shape = criterion_shape(preds_shape, gt_shape) loss_exp = criterion_exp(preds_exp, gt_exp) metric_shape.update(None, preds=loss_shape) metric_exp.update(None, preds=loss_exp) logger.info('Epoch [%d]: val-shape-loss=%f' % (cur_epoch, metric_shape.get()[1])) logger.info('Epoch [%d]: val-exp-loss=%f' % (cur_epoch, metric_exp.get()[1])) print('Done')
def weighted_train(net, lossfunc, trainer, Xtrain, ytrain, Xval, yval, ctx, dfeat, epoch=1, batch_size=64, weightfunc=None, weightvec=None, data_ctx=mx.cpu(), cnn_flag=False): ''' :param net: Forward pass model with NDarray parameters attached. :param lossfunc: Loss function used :param trainer: A declared optimizer. :param Xtrain: Training data features :param ytrain: Training data labels :param Xval: Validation data features :param yval: Validation data labels :param epoch: The number of data passes to run in training :param weightfunc: An optional lambda function that takes in a vector of X and y and outputs a vector of weights to assign to those examples. :param weightvec: A weight vector in numpy array that has the same number of rows as ytrain. * Note that this is only active if weightfunc is none. :return: The function does not return anything. Trained parameters will remain in net. ''' # declare data iterators if weightvec is not None: assert (Xtrain.shape[0] == len(weightvec) ), "weightvec dimension does not match that of Xtrain!" train_data = mx.io.NDArrayIter([ nd.array(Xtrain, ctx=data_ctx), nd.array(weightvec, ctx=data_ctx) ], nd.array(ytrain, ctx=data_ctx), batch_size, shuffle=True) else: train_data = mx.io.NDArrayIter(nd.array(Xtrain, ctx=data_ctx), nd.array(ytrain, ctx=data_ctx), batch_size, shuffle=True) val_data = mx.io.NDArrayIter(nd.array(Xval, ctx=data_ctx), nd.array(yval, ctx=data_ctx), batch_size, shuffle=False) smoothing_constant = .01 moving_loss = 0 #params = net.collect_params() #for param in params.values(): # ctx = param.list_ctx()[0] # break # assuming all parameters are on the same context for e in range(epoch): train_data.reset() for i, batch in enumerate(train_data): if cnn_flag: data = batch.data[0].as_in_context(ctx) else: data = batch.data[0].as_in_context(ctx).reshape((-1, dfeat)) label = batch.label[0].as_in_context(ctx) if weightfunc is not None: wt_batch = weightfunc(data, label).reshape((-1, )) elif weightvec is not None: wt_batch = batch.data[1].as_in_context(ctx).reshape((-1, )) else: wt_batch = nd.ones_like( label) # output an ndarray of importance weight with autograd.record(): output = net(data) loss = lossfunc(output, label) loss = loss * wt_batch loss.backward() trainer.step(data.shape[0]) curr_loss = nd.mean(loss).asscalar() moving_loss = (curr_loss if ((i == 0) and (e == 0)) else (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss) val_data.reset() train_data.reset() val_accuracy = evaluate_accuracy(val_data, net, ctx, dfeat, cnn_flag=cnn_flag) train_accuracy = evaluate_accuracy(train_data, net, ctx, dfeat, cnn_flag=cnn_flag) print("Epoch %s. Loss: %s, Train_acc %s, Validation_acc %s" % (e, moving_loss, train_accuracy, val_accuracy))
from mxnet import nd x = nd.arange(12) # 改 X = x.reshape((3, 4)) Y = nd.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) print(X) print(Y) #左闭右开 print(X[1:3]) X[1,2] = 9 print(X) X[1:2, :] = 12 print(X)
def batchify(self, data_filepaths: List[str], ctx: mx.context.Context): data = [self.data_encoder.load_datapoint(i) for i in data_filepaths] # Get the size of each graph batch_sizes = nd.array([len(dp.node_names) for dp in data], dtype='int32', ctx=ctx) combined_node_types = tuple( itertools.chain(*[dp.node_types for dp in data])) node_types = tuple_of_tuples_to_padded_array(combined_node_types, ctx) combined_node_names = tuple( itertools.chain(*[dp.node_names for dp in data])) node_names = [] for name in combined_node_names: if name == self.data_encoder.internal_node_flag: node_names.append( self.data_encoder.name_to_1_hot( '', embedding_size=self.data_encoder. max_name_encoding_length, mark_as_internal=True)) elif name == self.data_encoder.fill_in_flag: node_names.append( self.data_encoder.name_to_1_hot( '', embedding_size=self.data_encoder. max_name_encoding_length, mark_as_special=True)) else: node_names.append( self.data_encoder.name_to_1_hot( name, embedding_size=self.data_encoder. max_name_encoding_length)) node_names = nd.array(np.stack(node_names), dtype='float32', ctx=ctx) # Combine all the adjacency matrices into one big, disconnected graph edges = OrderedDict() for edge_type in self.data_encoder.all_edge_types: adj_mat = sp.sparse.block_diag( [dp.edges[edge_type] for dp in data]).tocsr() adj_mat = nd.sparse.csr_matrix( (adj_mat.data, adj_mat.indices, adj_mat.indptr), shape=adj_mat.shape, dtype='float32', ctx=ctx) edges[edge_type] = adj_mat # 1-hot whether a variable should have been indicated or not length = 0 labels = [] # Relabel the labels to match the indices in the batchified graph for dp in data: labels += [i + length for i in dp.label] length += len(dp.node_types) labels = nd.array(labels, dtype='int32', ctx=ctx) one_hot_labels = nd.zeros(length, dtype='float32', ctx=ctx) one_hot_labels[labels] = 1 data = self.InputClass(edges, node_types, node_names, batch_sizes, ctx) return Batch(data, one_hot_labels)
def compute_model(self): self.clear_all() self.flows = [[],[],[],[]] device = mx.cpu() if not hasattr(self, 'net'): self.net = unet.Net2D() self.net.hybridize() self.net.initialize(ctx = device) self.current_model = self.ModelChoose.currentText() print(self.current_model) self.net.load_parameters(os.path.join(self.model_dir, self.current_model)) elif self.ModelChoose.currentText() != self.current_model: self.net = unet.Net2D() self.net.hybridize() self.net.initialize(ctx = device) self.current_model = self.ModelChoose.currentText() print(self.current_model) self.net.load_parameters(os.path.join(self.model_dir, self.current_model)) for z in range(len(self.stack)): image = self.stack[z].astype(np.float32) # use grayscale image image = self.chanchoose(image) # rescale image try: x = utils.normalize99(image) Ly,Lx = x.shape[-2:] imgi, pads = transforms.pad_image_CS0(x) while imgi.ndim<4: imgi = np.expand_dims(imgi, 0) X = nd.array(imgi, ctx=device) # run network y = self.net(X)[0] print(X.shape) except: # add zero channel if image.shape[0]==1: image = np.concatenate((image, np.zeros_like(image)), axis=0) else: image = image[:1] x = utils.normalize99(image) Ly,Lx = x.shape[-2:] imgi, pads = transforms.pad_image_CS0(x) while imgi.ndim<4: imgi = np.expand_dims(imgi, 0) X = nd.array(imgi, ctx=device) # run network y = self.net(X)[0] print(X.shape) y = y.detach().asnumpy() # undo padding y = y[:, :, pads[-2][0]:y.shape[-2]-pads[-2][-1], pads[-1][0]:y.shape[-1]-pads[-1][-1]] print(y.shape) # compute dynamics from flows incell = y[0,2] > .0 yout = utils.run_dynamics(-y[:,:2] * incell) masks = utils.get_mask(yout[0])[0] y[0,0] /= np.abs(y[0,0]).max() y[0,1] /= np.abs(y[0,1]).max() self.flows[0].append((y[0,0] * 127 + 127).astype(np.uint8)) self.flows[1].append((y[0,1] * 127 + 127).astype(np.uint8)) if 0: self.flows[2].append((y[0,1] * 127 + 127).astype(np.uint8)) else: self.flows[2].append(np.zeros((Ly,Lx), np.uint8)) self.flows[-1].append(np.clip(y[0,-1] * 127 + 127, 0, 255).astype(np.uint8)) yc = np.array([0,Ly-1,0,Ly-1]) xc = np.array([0,0,Lx-1,Lx-1]) print('%d cells found with unet'%(len(np.unique(masks)[1:]))) for n in np.unique(masks)[1:]: col_rand = np.random.randint(1000) color = self.colormap[col_rand,:3] mn = (masks==n).astype(np.uint8) nc0 = self.ncells if mn.sum() > 5: pix = self.find_outline(mn=mn) if pix is not None: pix = np.concatenate((z*np.ones_like(pix[:,:1]), pix, np.ones_like(pix[:,:1])), axis=-1) median = self.add_mask(points=pix, color=color, mask=mn.nonzero()) if median is not None: self.cellcolors.append(color) self.ncells+=1 for z,m in enumerate(median): self.medians[z].append(m) if self.ncells>0: self.ClearButton.setEnabled(True) print(self.ncells)
from mxnet import autograd,gluon,init,nd from mxnet.gluon import loss as gloss , nn,data as gdata import mxnet as mx import numpy as np import random import time dirTrain='D:\\image\\txt\\2l\\' ctx=mx.gpu() f=np.loadtxt(dirTrain+"image_train_features.txt",delimiter=' ') l=np.loadtxt(dirTrain+"image_train_labels.txt",delimiter=' ') features=nd.array(f).copyto(ctx) labels=nd.array(l).copyto(ctx) labels_test=nd.zeros(labels.shape,ctx) data_num=len(f) batch_size=10000 dataset=gdata.ArrayDataset(features,labels) data_iter = gdata.DataLoader(dataset,batch_size,shuffle=True) net = nn.Sequential() net.add(nn.Dense(128,activation='relu'), nn.BatchNorm(),
def get_backbones(config_filename, adj_filename, ctx): config = configparser.ConfigParser() config.read(config_filename) K = int(config['Training']['K']) num_of_weeks = int(config['Training']['num_of_weeks']) num_of_days = int(config['Training']['num_of_days']) num_of_hours = int(config['Training']['num_of_hours']) num_of_vertices = int(config['Data']['num_of_vertices']) adj_mx = get_adjacency_matrix(adj_filename, num_of_vertices) L_tilde = scaled_Laplacian(adj_mx) cheb_polynomials = [nd.array(i, ctx = ctx) for i in cheb_polynomial(L_tilde, K)] backbones1 = [ { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_weeks, "cheb_polynomials": cheb_polynomials }, { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1, "cheb_polynomials": cheb_polynomials } ] backbones2 = [ { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_days, "cheb_polynomials": cheb_polynomials }, { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1, "cheb_polynomials": cheb_polynomials } ] backbones3 = [ { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": num_of_hours, "cheb_polynomials": cheb_polynomials }, { "K": K, "num_of_chev_filters": 64, "num_of_time_filters": 64, "time_conv_strides": 1, "cheb_polynomials": cheb_polynomials } ] all_backbones = [ backbones1, backbones2, backbones3 ] return all_backbones
frame_id_list = range(0, 64, 2) video_data = vr.get_batch(frame_id_list).asnumpy() clip_input = [video_data[vid, :, :, :] for vid, _ in enumerate(frame_id_list)] print([clip.shape for clip in clip_input]) print(len(clip_input)) transform_fn = video.VideoGroupValTransform(size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) clip_input = transform_fn(clip_input) clip_input = np.stack(clip_input, axis=0) clip_input = clip_input.reshape((-1, ) + (32, 3, 224, 224)) clip_input = np.transpose(clip_input, (0, 2, 1, 3, 4)) print('Video data is downloaded and preprocessed.') print(clip_input.shape) model_name = 'i3d_resnet50_v1_hmdb51' net = get_model(model_name, pretrained=True) print('%s model is successfully loaded.' % model_name) pred = net(nd.array(clip_input)) classes = net.classes topK = 5 ind = nd.topk(pred, k=topK)[0].astype('int') print('The input video clip is classified to be') for i in range(topK): print('\t[%s], with probability %.3f.' % (classes[ind[i].asscalar()], nd.softmax(pred)[0][ind[i]].asscalar()))
def train_eval(opt): mx.random.seed(123) np.random.seed(123) os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3' gpus = [] if opt.gpus is None or opt.gpus is '' else [ int(gpu) for gpu in opt.gpus.split(',')] num_gpus = len(gpus) batch_size = opt.batch_per_device * max(1, num_gpus) context = [mx.gpu(i) for i in gpus][0] if num_gpus > 0 else [mx.cpu()] steps = [int(step) for step in opt.lr_scheduler_steps.split(',')] vis_env = opt.dataset + opt.output vis = Visulizer(env=vis_env) vis.log(opt) net = R2Plus2D_MT(num_scenes=19,num_actions=44, model_depth=opt.model_depth, final_temporal_kernel=opt.n_frame // 8) # labels set 63 # train_loader,val_loader = get_meitu_dataloader(data_dir=opt.meitu_dir, # device_id=opt.decoder_gpu, # batch_size=batch_size, # n_frame=opt.n_frame, # crop_size=opt.crop_size, # scale_h=opt.scale_h, # scale_w=opt.scale_w, # num_workers=opt.num_workers) # use multi gpus to load data train_loader, val_loader,sample_weight = get_meitu_multi_task_dataloader(data_dir=opt.meitu_dir, device_id=opt.decoder_gpu, batch_size=batch_size, num_workers=opt.num_workers, n_frame=opt.n_frame, crop_size=opt.crop_size, scale_h=opt.scale_h, scale_w=opt.scale_w, cache_size=opt.cache_size) action_loss = gloss.SoftmaxCrossEntropyLoss() #scene_loss = LsepLoss() # [type(data) for i,enumerate(train_loader) if i<2] # step when 66,in data/nvvl_meitu.py # create new find_nvv_error.py ,copy train_nvvl_r3d.py one by one test, # find error loss_dict = {'bce': gloss.SigmoidBinaryCrossEntropyLoss, 'warp_nn': WarpLoss, 'warp_fn': WARP_funcLoss, 'lsep_nn': LsepLoss, 'lsep_fn': LSEP_funcLoss} scene_loss = loss_dict[opt.loss_type]() # if opt.loss_type == 'lsep_nnh': # loss_criterion = LsepLossHy(batch_size=batch_size // num_gpus, num_class=opt.num_class) # loss_criterion.hybridize() # elif opt.loss_type == 'bce': # loss_criterion = gloss.SigmoidBinaryCrossEntropyLoss() # loss_criterion.hybridize() # else: # # net.initialize(mx.init.Xavier(), # ctx=context) # net parameter initialize in several cards net.initialize(mx.init.Xavier(), ctx=context) if not opt.pretrained is None: if opt.pretrained.endswith('.pkl'): net.load_from_caffe2_pickle(opt.pretrained) elif opt.pretrained.endswith('.params'): try: print("load pretrained params ", opt.pretrained) net.load_from_sym_params(opt.pretrained, ctx=context) except Exception as e: print("load as sym params failed,reload as gluon params") net.load_params(opt.pretrained, ctx=context) # load params to net context #net.hybridize() trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': opt.lr, 'momentum': 0.9, 'wd': opt.wd}, kvstore=opt.kvstore) # the trainer lr_steps = lr_schedualer.MultiFactorScheduler(steps, opt.lr_schedualer_factor) lr_steps.base_lr = opt.lr best_eval = 0.0 for epoch in range(opt.num_epoch): tic = time() scene_pre_loss, scene_cumulative_loss = 0.0,0.0 action_pre_loss,action_cumulative_loss = 0.0, 0.0 trainer.set_learning_rate(lr_steps(epoch)) vis.log('Epoch %d learning rate %f' % (epoch, trainer.learning_rate)) for i, (data, scene_label,action_label) in enumerate(train_loader): # single card not split with autograd.record(): data = data.as_in_context(context) scene_label = scene_label.as_in_context(context) action_label = action_label.as_in_context(context) pred_scene,pred_action = net(data) loss_scene = scene_loss(pred_scene,scene_label) loss_action = action_loss(pred_action,action_label) loss = loss_scene + opt.action_rate*loss_action.mean() scene_cumulative_loss += nd.mean(loss_scene).asscalar() action_cumulative_loss +=nd.mean(loss_action).asscalar() loss.backward() trainer.step(data.shape[0]) if (i + 1) % opt.log_interval == 0: vis.log('[Epoch %d,Iter %d ] scene loss= %f' % (epoch, i + 1, scene_cumulative_loss - scene_pre_loss)) vis.plot('scene_loss', scene_cumulative_loss - scene_pre_loss) scene_pre_loss = scene_cumulative_loss vis.log('[Epoch %d,Iter %d ] action loss= %f' % (epoch, i + 1, action_cumulative_loss - action_pre_loss )) vis.plot("action_loss", action_cumulative_loss - action_pre_loss) action_pre_loss = action_cumulative_loss if opt.debug: if (i + 1) // (opt.log_interval) == 3: break vis.log('[Epoch %d] scene loss=%f,action loss=%f' % (epoch, scene_cumulative_loss,action_cumulative_loss)) vis.log('[Epoch %d] time used: %f' % (epoch, time() - tic)) vis.log('[Epoch %d] saving net') save_path = './{0}/{1}_test-val{2}.params'.format(opt.output, str(opt.dataset + 'multi'), str(epoch)) vis.log("save path %s" % (save_path)) net.save_parameters(save_path) label_inter =1e-4 label_union =1e-4 acc = nd.array([0], ctx=mx.cpu()) val_iter =0 for i,(data,scene_label,action_label) in enumerate(val_loader): data = data.as_in_context(context) action_label = action_label.as_in_context(context) scene_pred,action_pred = net(data) scene_order = scene_pred.argsort()[:,::-1] scene_order_np = scene_order.asnumpy() scene_label_np = scene_label.asnumpy() for scene_pred_v,scene_label_v in zip(scene_order_np,scene_label_np): label_set = set([index for index,value in enumerate(scene_label_v) if value>0.1]) pred_top1 = set([scene_pred_v[0]]) label_inter += len(pred_top1.intersection(label_set)) label_union += len(pred_top1.union(label_set)) action_pred = action_pred.argmax(axis=1) acc += (action_pred == action_label.astype('float32')).mean().asscalar() val_iter +=1 if (i + 1) % (opt.log_interval) == 0: vis.log("[Epoch %d,Iter %d],action_acc= %f"%(epoch,i,acc.asscalar()/val_iter)) vis.log("[Epoch %d,Iter %d],scene_top1=%f"%(epoch,i,label_inter/label_union)) if opt.debug: if (i + 1) // (opt.log_interval) == 2: break vis.log("""---------------------------------------- ----XXXX------finished------------------ ----------------------------------------""")
def predict_sentiment(net, vocab, sentence): """Predict the sentiment of a given sentence.""" sentence = nd.array([vocab.token_to_idx[token] for token in sentence], ctx=try_gpu()) label = nd.argmax(net(nd.reshape(sentence, shape=(1, -1))), axis=1) return 'positive' if label.asscalar() == 1 else 'negative'
# ================================================================== # # 1. Basic autograd example 1 (Line 25 to 43) # 2. Basic autograd example 2 (Line 50 to 89) # 3. Loading data from numpy (Line 96 to 103) # 4. Input pipline (Line 110 to 134) # 5. Input pipline for custom dataset (Line 141 to 161) # 6. Pretrained model (Line 168 to 180) # 7. Save and load model (Line 187 to 190) # ================================================================== # # 1. Basic autograd example 1 # # ================================================================== # # Create ndarray. x = nd.array([1]) x.attach_grad() w = nd.array([2]) w.attach_grad() b = nd.array([3]) b.attach_grad() # Build a computational graph. with autograd.record(): y = w * x + b # y = 2 * x + 3 # Compute gradients. y.backward() # Print out the gradients. print(x.grad) # x.grad = 2
from mxnet import autograd, nd from mxnet.gluon import nn def corr2d(X, K): h, w = K.shape Y = nd.zeros(X.shape[0] - h + 1, X.shape[1] - w + 1) for i in range(Y.shae[0]): for j in range(Y.shapq[1]): Y[i, j] = (X[i:i + h][j:j + w] * K).sum() return Y x = nd.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) K = nd.array([[1, 2], [3, 4]]) corr2d(x, K)
W4 = nd.random.normal(scale=scale, shape=(128, 10)) b4 = nd.zeros(shape=10) params = [W1, b1, W2, b2, W3, b3, W4, b4] # 定义交叉熵损失函数 loss = gluon.loss.SoftmaxCrossEntropyLoss() # 测试:将模型参数复制到gpu(0)上 new_params = get_params(params, mx.gpu(0)) print('b1 weight:', new_params[1]) print('b1 grad:', new_params[1].grad) # 测试:allreduce data = [nd.ones((1, 2), ctx=mx.gpu(i)) * (i + 1) for i in range(2)] print('before allreduce:', data) allreduce(data) print('after allreduce:', data) # 测试:将6个数据样本平均分给2块显卡的显存 batch = nd.array(24).reshape((6, 4)) ctx = [mx.gpu(0), mx.gpu(1)] splitted = split_and_load(batch, ctx) print('input:', batch) print('load into ', ctx) print('output:', splitted) # 在单GPU上训练 train(num_gpus=1, batch_size=256, lr=0.2) # 在两块GPU上训练 train(num_gpus=2, batch_size=256, lr=0.2)
width, height = 416, 416 # resize image to 416x416 after all data augmentation train_transform = presets.yolo.YOLO3DefaultTrainTransform(width, height) val_transform = presets.yolo.YOLO3DefaultValTransform(width, height) ############################################################################## utils.random.seed(123) # fix seed in this tutorial ############################################################################## # apply transforms to train image train_image2, train_label2 = train_transform(train_image, train_label) print('tensor shape:', train_image2.shape) ############################################################################## # Images in tensor are distorted because they no longer sit in (0, 255) range. # Let's convert them back so we can see them clearly. train_image2 = train_image2.transpose((1, 2, 0)) * nd.array( (0.229, 0.224, 0.225)) + nd.array((0.485, 0.456, 0.406)) train_image2 = (train_image2 * 255).clip(0, 255) ax = viz.plot_bbox(train_image2.asnumpy(), train_label2[:, :4], labels=train_label2[:, 4:5], class_names=train_dataset.classes) plt.show() ############################################################################## # Transforms used in training include random color distortion, random expand/crop, random flipping, # resizing and fixed color normalization. # In comparison, validation only involves resizing and color normalization. ########################################################## # Data Loader # -----------
def train(net, train_data, val_data, eval_metric, ctx, args): """Training pipeline""" net.collect_params().reset_ctx(ctx) if args.no_wd: for k, v in net.collect_params('.*beta|.*gamma|.*bias').items(): v.wd_mult = 0.0 if args.label_smooth: net._target_generator._label_smooth = True if args.lr_decay_period > 0: lr_decay_epoch = list(range(args.lr_decay_period, args.epochs, args.lr_decay_period)) else: lr_decay_epoch = [int(i) for i in args.lr_decay_epoch.split(',')] lr_decay_epoch = [e - args.warmup_epochs for e in lr_decay_epoch] num_batches = args.num_samples // args.batch_size lr_scheduler = LRSequential([ LRScheduler('linear', base_lr=0, target_lr=args.lr, nepochs=args.warmup_epochs, iters_per_epoch=num_batches), LRScheduler(args.lr_mode, base_lr=args.lr, nepochs=args.epochs - args.warmup_epochs, iters_per_epoch=num_batches, step_epoch=lr_decay_epoch, step_factor=args.lr_decay, power=2), ]) trainer = gluon.Trainer( net.collect_params(), 'sgd', {'wd': args.wd, 'momentum': args.momentum, 'lr_scheduler': lr_scheduler}, kvstore='local') # targets sigmoid_ce = gluon.loss.SigmoidBinaryCrossEntropyLoss(from_sigmoid=False) l1_loss = gluon.loss.L1Loss() # metrics obj_metrics = mx.metric.Loss('ObjLoss') center_metrics = mx.metric.Loss('BoxCenterLoss') scale_metrics = mx.metric.Loss('BoxScaleLoss') cls_metrics = mx.metric.Loss('ClassLoss') # set up logger logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.INFO) log_file_path = args.save_prefix + '_train.log' log_dir = os.path.dirname(log_file_path) if log_dir and not os.path.exists(log_dir): os.makedirs(log_dir) fh = logging.FileHandler(log_file_path) logger.addHandler(fh) logger.info(args) logger.info('Start training from [Epoch {}]'.format(args.start_epoch)) best_map = [0] for epoch in range(args.start_epoch, args.epochs): if args.mixup: # TODO(zhreshold): more elegant way to control mixup during runtime try: train_data._dataset.set_mixup(np.random.beta, 1.5, 1.5) except AttributeError: train_data._dataset._data.set_mixup(np.random.beta, 1.5, 1.5) if epoch >= args.epochs - args.no_mixup_epochs: try: train_data._dataset.set_mixup(None) except AttributeError: train_data._dataset._data.set_mixup(None) tic = time.time() btic = time.time() mx.nd.waitall() # net.hybridize() for i, batch in enumerate(train_data): batch_size = batch[0].shape[0] data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0) # objectness, center_targets, scale_targets, weights, class_targets fixed_targets = [gluon.utils.split_and_load(batch[it], ctx_list=ctx, batch_axis=0) for it in range(1, 7)] gt_boxes = gluon.utils.split_and_load(batch[7], ctx_list=ctx, batch_axis=0) sum_losses = [] obj_losses = [] center_losses = [] scale_losses = [] cls_losses = [] with autograd.record(): for ix, x in enumerate(data): # obj_loss, center_loss, scale_loss, cls_loss = net(x, gt_boxes[ix], *[ft[ix] for ft in fixed_targets]) loss = net(x, gt_boxes[ix], *[ft[ix] for ft in fixed_targets]) obj_loss, center_loss, scale_loss, cls_loss = nd.array([0]), nd.array([0]), nd.array([0]), nd.array([0]) # sum_losses.append(obj_loss + center_loss + scale_loss + cls_loss) sum_losses.append(loss) obj_losses.append(loss) center_losses.append(center_loss) scale_losses.append(scale_loss) cls_losses.append(cls_loss) autograd.backward(sum_losses) trainer.step(batch_size) obj_metrics.update(0, obj_losses) center_metrics.update(0, center_losses) scale_metrics.update(0, scale_losses) cls_metrics.update(0, cls_losses) if args.log_interval and not (i + 1) % args.log_interval: name1, loss1 = obj_metrics.get() name2, loss2 = center_metrics.get() name3, loss3 = scale_metrics.get() name4, loss4 = cls_metrics.get() logger.info('[Epoch {}][Batch {}], LR: {:.2E}, Speed: {:.3f} samples/sec, {}={:.3f}, {}={:.3f}, {}={:.3f}, {}={:.3f}'.format( epoch, i, trainer.learning_rate, batch_size/(time.time()-btic), name1, loss1, name2, loss2, name3, loss3, name4, loss4)) btic = time.time() name1, loss1 = obj_metrics.get() name2, loss2 = center_metrics.get() name3, loss3 = scale_metrics.get() name4, loss4 = cls_metrics.get() logger.info('[Epoch {}] Training cost: {:.3f}, {}={:.3f}, {}={:.3f}, {}={:.3f}, {}={:.3f}'.format( epoch, (time.time()-tic), name1, loss1, name2, loss2, name3, loss3, name4, loss4)) if not (epoch + 1) % args.val_interval: # consider reduce the frequency of validation to save time map_name, mean_ap = validate(net, val_data, ctx, eval_metric) val_msg = '\n'.join(['{}={}'.format(k, v) for k, v in zip(map_name, mean_ap)]) logger.info('[Epoch {}] Validation: \n{}'.format(epoch, val_msg)) current_map = float(mean_ap[-1]) else: current_map = 0. save_params(net, best_map, current_map, epoch, args.save_interval, args.save_prefix)
import d2lzh as d2l from mxnet import autograd, nd from mxnet.gluon import loss as gloss import math, time, numpy as np # 读取数据 # corpus_indices 1w字的idx # char_to_idx 字符转idx # idx_to_char idx转字符 # vocab_size不同字的个数 (corpus_indices, char_to_idx, idx_to_char, vocab_size) = d2l.load_data_jay_lyrics() # one-hot向量 print(nd.one_hot(nd.array([1, 2]), vocab_size)) # one-hot一行只有一个1,哪个位置是1呢?1,2位置 def to_onehot(X, size): return [nd.one_hot(x, size) for x in X.T] # X中列是feature,行是sample # Test X = nd.arange(10).reshape((2, 5)) # 2:batch_size 5:num_step inputs = to_onehot(X, vocab_size) # 转成num_steps个形状为(batch_size,vocab_size) np.set_printoptions(edgeitems=6) # 显示个数设置,默认显示3个 print(len(inputs), inputs[0]) # 5个长度, 2*1027 ################################################# TODO 初始化模型参数 ##################################################### num_inputs, num_hiddens, num_outputs = vocab_size, 256, vocab_size ctx = d2l.try_gpu() print('use ', ctx)
if __name__ == '__main__': # 获取数据集 train_data = pd.read_csv("../data/kaggle_house_pred_train.csv") test_data = pd.read_csv("../data/kaggle_house_pred_test.csv") all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:, 1:])) # 预处理数据 numeric_features = all_features.dtypes[ all_features.dtypes != 'object'].index all_features[numeric_features] = all_features[numeric_features].apply( lambda x: (x - x.mean()) / (x.std())) all_features[numeric_features] = all_features[numeric_features].fillna(0) all_features = pd.get_dummies(all_features, dummy_na=True) n_train = train_data.shape[0] train_features = nd.array(all_features[:n_train].values) test_features = nd.array(all_features[n_train:].values) train_labels = nd.array(train_data.SalePrice.values).reshape((-1, 1)) # 损失函数选择 loss = gluon.loss.L2Loss() # 模型选择 k = 5 num_epochs = 100 learning_rate = 5 weight_decay = 0 batch_size = 64 train_l, valid_l = k_fold(k, train_features, train_labels, num_epochs, learning_rate, weight_decay, batch_size) print("%d-fold validation: avg train rmse %f, avg valid rmse %f" %
def load_data(self, batch_size, shuffle=False): train_h5, test_h5 = self.load_h5_files() train_data = {} data_mean = {} data_std = {} train_images = {} for input_name in self._input_names_: train_data["input" + str(self._input_names_.index( input_name))] = train_h5[input_name] train_dataset = train_h5[input_name] train_dataset_shape = train_data[ "input" + str(self._input_names_.index(input_name))].shape # slice_size limits the memory consumption, by only loading slices of size <500MB into memory slice_size = min(train_dataset_shape[0] - 1, int(500e6 / (train_h5[input_name][0].size * \ train_h5[input_name][0].itemsize))) num_slices = max(1, int(train_h5[input_name].shape[0] / slice_size)) mean = np.zeros(train_dataset_shape[1:]) std = np.zeros(train_dataset_shape[1:]) for i in range(int(train_dataset_shape[0] / slice_size)): mean += train_dataset[i * slice_size:(i + 1) * slice_size].mean(axis=0) / num_slices std += train_dataset[i * slice_size:(i + 1) * slice_size].std(axis=0) / num_slices if slice_size > train_dataset_shape[0] - 1: mean += train_dataset[num_slices * slice_size:].mean( axis=0) / (slice_size - num_slices % slice_size) std += train_dataset[num_slices * slice_size:].std( axis=0) / (slice_size - num_slices % slice_size) std += 1e-5 data_mean[input_name + '_'] = nd.array(mean) data_std[input_name + '_'] = nd.array(std) if 'images' in train_h5: train_images = train_h5['images'] train_label = {} index = 0 for output_name in self._output_names_: train_label[index] = train_h5[output_name] index += 1 train_iter = mx.io.NDArrayIter(data=train_data, label=train_label, batch_size=batch_size, shuffle=shuffle) test_iter = None if test_h5 != None: test_data = {} test_images = {} for input_name in self._input_names_: test_data["input" + str(self._input_names_.index( input_name))] = test_h5[input_name] if 'images' in test_h5: test_images = test_h5['images'] test_label = {} index = 0 for output_name in self._output_names_: test_label[index] = test_h5[output_name] index += 1 test_iter = mx.io.NDArrayIter(data=test_data, label=test_label, batch_size=batch_size) return train_iter, test_iter, data_mean, data_std, train_images, test_images
def iteration_LIGOStrain(T, fs, LIGOdata, step, batch_size=8): ''' Input Strain. ''' GPStime, strain_H1, chan_dict_H1, strain_L1, chan_dict_L1 = LIGOdata assert strain_H1.size == strain_L1.size == GPStime.size lentime = int(GPStime.size / fs) # 4096 length of strain time bolnotnan = ~(np.isnan(strain_H1) + np.isnan(strain_L1)) isvalid = lambda a, b: not False in bolnotnan[a:b] Slice = [(i * int(step * fs), (i * int(step * fs) + fs * T)) for i in tqdm(range(lentime * fs), desc='Generate slicing windows ({}/{})'.format( strain_H1[bolnotnan].size / fs, lentime)) if isvalid(i * int(step * fs), (i * int(step * fs) + fs * T)) if (i * int(step * fs) + fs * T) <= lentime * fs] # Slice above may cost 1m11s # Check the validation of Slice assert [0 for i, j in Slice if False in bolnotnan[i:j]] == [] logger.debug('Num of valid slice/steps: {}', len(Slice)) H1_block = nd.array( np.concatenate([strain_H1[i:j].reshape(1, -1) for (i, j) in Slice])) L1_block = nd.array( np.concatenate([strain_L1[i:j].reshape(1, -1) for (i, j) in Slice])) logger.debug('H1_block shape: {}; L1_block shape: {}', H1_block.shape, L1_block.shape) # (Steps, T*fs) H1_psd_block = nd.concatenate([ EquapEvent(fs, i) for i in tqdm(H1_block.expand_dims(1), desc='Calc. PSD for H1_block') ]) L1_psd_block = nd.concatenate([ EquapEvent(fs, i) for i in tqdm(L1_block.expand_dims(1), desc='Calc. PSD for L1_block') ]) logger.debug('H1_psd_block shape: {}; L1_psd_block shape: {}', H1_psd_block.shape, L1_psd_block.shape) # (Steps, 2, 1, T*fs) # psd_block here may cost 26s O1_psd_block = nd.concat(H1_psd_block.expand_dims(2), L1_psd_block.expand_dims(2), dim=2) logger.debug('O1_psd_block shape: {}', O1_psd_block.shape) # # (Steps, 2, 2, 1, T*fs) slice2df = lambda strain, i, j: pd.DataFrame(strain).iloc[int(i / fs):int( j / fs)] df2dict = lambda df: {col: df[col].values for col in df.columns} chan_dict_H1 = [ df2dict(slice2df(chan_dict_H1, i, j)) for (i, j) in tqdm(Slice, desc='Slicing chan_dict_H1') ] chan_dict_L1 = [ df2dict(slice2df(chan_dict_L1, i, j)) for (i, j) in tqdm(Slice, desc='Slicing chan_dict_L1') ] chan_dict_block = np.concatenate(([chan_dict_H1], [chan_dict_L1]), axis=0).T logger.debug('chan_dict_block shape: {}', chan_dict_block.shape) # O1_psd_block (steps, 2, 2, 1, T*fs) nd.array cpu # GPStime_block (steps, ) list # chan_list_block (steps, 2) np.array([dict], [dict]) GPStime_block = [GPStime[i + T * fs // 2] for (i, _) in Slice] dataset = gluon.data.ArrayDataset(O1_psd_block, GPStime_block) iterator = gdata.DataLoader(dataset, batch_size=batch_size, shuffle=False, last_batch='keep', num_workers=0) return dataset, iterator, chan_dict_block
def get_data_ch7(): """Get the data set used in Chapter 7.""" data = np.genfromtxt('../data/airfoil_self_noise.dat', delimiter='\t') data = (data - data.mean(axis=0)) / data.std(axis=0) return nd.array(data[:, :-2]), nd.array(data[:,-1])
def predict(features, results, tokenizer, max_answer_length=64, null_score_diff_threshold=0.0, n_best_size=10, version_2=False): """Get prediction results. Parameters ---------- features : list of SQuADFeature List of squad features for the example. results : list of data.qa.PredResult List of model predictions for span start and span end. tokenizer: callable Tokenizer function. max_answer_length: int, default 64 Maximum length of the answer tokens. null_score_diff_threshold: float, default 0.0 If null_score - best_non_null is greater than the threshold predict null. n_best_size: int, default 10 The total number of n-best predictions. version_2: bool, default False If true, the SQuAD examples contain some that do not have an answer. Returns ------- prediction: str The final prediction. nbest : list of (str, float) n-best predictions with their probabilities. """ _PrelimPrediction = namedtuple('PrelimPrediction', [ 'feature_index', 'start_index', 'end_index', 'pred_start', 'pred_end' ]) _NbestPrediction = namedtuple('NbestPrediction', ['text', 'pred_start', 'pred_end']) prelim_predictions = [] score_diff = None score_null = 1000000 # large and positive min_null_feature_index = 0 # the paragraph slice with min mull score null_pred_start = 0 # the start logit at the slice with min null score null_pred_end = 0 # the end logit at the slice with min null score for features_id, (result, feature) in enumerate(zip(results, features)): start_indexes = _get_best_indexes(result.start, n_best_size) end_indexes = _get_best_indexes(result.end, n_best_size) if version_2: feature_null_score = result.start[0] + \ result.end[0] if feature_null_score < score_null: score_null = feature_null_score min_null_feature_index = features_id null_pred_start = result.start[0] null_pred_end = result.end[0] for start_index in start_indexes: for end_index in end_indexes: # We could hypothetically create invalid predictions, e.g., predict # that the start of the span is in the question. We throw out all # invalid predictions. if start_index >= len(feature.tokens): continue if end_index >= len(feature.tokens): continue if start_index not in feature.token_to_orig_map: continue if end_index not in feature.token_to_orig_map: continue if not feature.token_is_max_context.get(start_index, False): continue if end_index < start_index: continue length = end_index - start_index + 1 if length > max_answer_length: continue prelim_predictions.append( _PrelimPrediction(feature_index=features_id, start_index=start_index, end_index=end_index, pred_start=result.start[start_index], pred_end=result.end[end_index])) if version_2: prelim_predictions.append( _PrelimPrediction(feature_index=min_null_feature_index, start_index=0, end_index=0, pred_start=null_pred_start, pred_end=null_pred_end)) prelim_predictions = sorted(prelim_predictions, key=lambda x: (x.pred_start + x.pred_end), reverse=True) seen_predictions = {} nbest = [] for pred in prelim_predictions: if len(nbest) >= n_best_size: break feature = features[pred.feature_index] if pred.start_index > 0: # this is a non-null prediction tok_tokens = feature.tokens[pred.start_index:(pred.end_index + 1)] orig_doc_start = feature.token_to_orig_map[pred.start_index] orig_doc_end = feature.token_to_orig_map[pred.end_index] orig_tokens = feature.doc_tokens[orig_doc_start:(orig_doc_end + 1)] tok_text = ' '.join(tok_tokens) # De-tokenize WordPieces that have been split off. tok_text = tok_text.replace(' ##', '') tok_text = tok_text.replace('##', '') # Clean whitespace tok_text = tok_text.strip() tok_text = ' '.join(tok_text.split()) orig_text = ' '.join(orig_tokens) final_text = get_final_text(tok_text, orig_text, tokenizer) if final_text in seen_predictions: continue seen_predictions[final_text] = True else: final_text = '' seen_predictions[final_text] = True nbest.append( _NbestPrediction(text=final_text, pred_start=pred.pred_start, pred_end=pred.pred_end)) # if we didn't inlude the empty option in the n-best, inlcude it if version_2: if '' not in seen_predictions: nbest.append( _NbestPrediction(text='', pred_start=null_pred_start, pred_end=null_pred_end)) # In very rare edge cases we could have no valid predictions. So we # just create a nonce prediction in this case to avoid failure. if not nbest: nbest.append( _NbestPrediction(text='empty', pred_start=0.0, pred_end=0.0)) assert len(nbest) >= 1 total_scores = [] best_non_null_entry = None for entry in nbest: total_scores.append(entry.pred_start + entry.pred_end) if not best_non_null_entry: if entry.text: best_non_null_entry = entry probs = nd.softmax(nd.array(total_scores)).asnumpy() nbest_json = [] for (i, entry) in enumerate(nbest): nbest_json.append((entry.text, float(probs[i]))) if not version_2: prediction = nbest_json[0][0] else: # predict '' iff the null score - the score of best non-null > threshold score_diff = score_null - best_non_null_entry.pred_start - \ best_non_null_entry.pred_end if score_diff > null_score_diff_threshold: prediction = '' else: prediction = best_non_null_entry.text prediction = nbest_json[0][0] return prediction, nbest_json
def load_preprocessed_data(self, batch_size, preproc_lib, shuffle=False): train_h5, test_h5 = self.load_h5_files() wrapper = importlib.import_module(preproc_lib) instance = getattr(wrapper, preproc_lib)() instance.init() lib_head, _sep, tail = preproc_lib.rpartition('_') inp = getattr(wrapper, lib_head + "_input")() train_data = {} train_label = {} data_mean = {} data_std = {} train_images = {} shape_output = self.preprocess_data(instance, inp, 0, train_h5) train_len = len(train_h5[self._input_names_[0]]) for input_name in self._input_names_: if type(getattr(shape_output, input_name + "_out")) == np.ndarray: cur_shape = (train_len, ) + getattr(shape_output, input_name + "_out").shape else: cur_shape = (train_len, 1) train_data["input" + str(self._input_names_.index( input_name))] = mx.nd.zeros(cur_shape) for output_name in self._output_names_: if type(getattr(shape_output, output_name + "_out")) == nd.array: cur_shape = (train_len, ) + getattr(shape_output, output_name + "_out").shape else: cur_shape = (train_len, 1) train_label[output_name] = mx.nd.zeros(cur_shape) for i in range(train_len): output = self.preprocess_data(instance, inp, i, train_h5) for input_name in self._input_names_: train_data[ "input" + str(self._input_names_.index(input_name))][i] = getattr( output, input_name + "_out") for output_name in self._output_names_: train_label[output_name][i] = getattr(shape_output, output_name + "_out") for input_name in self._input_names_: data_mean[input_name + '_'] = nd.array( train_data["input" + str(self._input_names_.index(input_name))][:].mean( axis=0)) data_std[input_name + '_'] = nd.array( train_data["input" + str(self._input_names_.index(input_name))] [:].asnumpy().std(axis=0) + 1e-5) if 'images' in train_h5: train_images = train_h5['images'] train_iter = mx.io.NDArrayIter(data=train_data, label=train_label, batch_size=batch_size, shuffle=shuffle) test_data = {} test_label = {} shape_output = self.preprocess_data(instance, inp, 0, test_h5) test_len = len(test_h5[self._input_names_[0]]) for input_name in self._input_names_: if type(getattr(shape_output, input_name + "_out")) == np.ndarray: cur_shape = (test_len, ) + getattr(shape_output, input_name + "_out").shape else: cur_shape = (test_len, 1) test_data["input" + str(self._input_names_.index(input_name))] = mx.nd.zeros( cur_shape) for output_name in self._output_names_: if type(getattr(shape_output, output_name + "_out")) == nd.array: cur_shape = (test_len, ) + getattr(shape_output, output_name + "_out").shape else: cur_shape = (test_len, 1) test_label[output_name] = mx.nd.zeros(cur_shape) for i in range(test_len): output = self.preprocess_data(instance, inp, i, test_h5) for input_name in self._input_names_: test_data[ "input" + str(self._input_names_.index(input_name))][i] = getattr( output, input_name + "_out") for output_name in self._output_names_: test_label[output_name][i] = getattr(shape_output, output_name + "_out") test_images = {} if 'images' in test_h5: test_images = test_h5['images'] test_iter = mx.io.NDArrayIter(data=test_data, label=test_label, batch_size=batch_size) return train_iter, test_iter, data_mean, data_std, train_images, test_images
kernel_dice = 1. - (2 * kernel_intersection) / kernel_union kernel_dice_loss = F.mean(kernel_dice, axis=1) self.C_loss = C_dice_loss self.kernel_loss = kernel_dice_loss loss = self.lam * C_dice_loss + (1. - self.lam) * kernel_dice_loss return loss if __name__ == '__main__': import numpy as np from mxnet import autograd np.random.seed(29999) # loss = DiceLoss_with_OHEM(lam=0.7, debug=True, num_kernels=7) loss = DiceLoss(lam=0.7, num_kernels=7) for i in range(1): score_gt = F.array(np.random.uniform(0, 1, size=(7, 128, 128))) x = F.array(np.random.uniform(0, 1, size=(7, 6, 128, 128))) x.attach_grad() x_pred = F.array(np.random.uniform(0, 1, size=(7, 7, 128, 128))) mask = F.ones(shape=(7, 128, 128)) with autograd.record(): tmp_loss = loss.forward(score_gt, x, x_pred, mask) # tmp_loss.backward() print tmp_loss, loss.C_loss, loss.kernel_loss, loss.pixel_acc
from mxnet import autograd, gluon, nd from mxnet.gluon import data as gdata, loss as gloss, nn def pool2d(X, pool_size, mode = 'max'): Y = nd.zeros((X.shape[0] - pool_size + 1, X.shape[1] - pool_size + 1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): if mode == 'max': Y[i, j] = X[i:i+pool_size, j: j+pool_size].max() elif mode == 'avg': Y[i, j] = X[i:i + pool_size, j: j + pool_size].mean() return Y if __name__ == "__main__": X = nd.array([ [0, 1, 2], [3, 4, 5], [6, 7, 8]]) pool_size = 2 print("X:", X, " shape:", X.shape) Y = nd.zeros(shape=(X.shape[0] - pool_size + 1, X.shape[1] - pool_size + 1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): Y[i, j] = X[i: i + pool_size, j:j+pool_size].max() print("Max pooling:", Y, " shape:", Y.shape) Y = nd.zeros(shape=(X.shape[0] - pool_size + 1, X.shape[1] - pool_size + 1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): Y[i, j] = X[i: i + pool_size, j:j + pool_size].mean() print("Average pooling:", Y, " shape:", Y.shape) Y = pool2d(X, pool_size, 'max') print("Max pooling:", Y, " shape:", Y.shape) Y = pool2d(X, pool_size, 'avg')
# Third-party imports from mxnet import nd import pytest # First-party imports from gluonts.kernels import RBFKernel test_cases = [ # This tests the simple case where the amplitude and length scale parameters are constant # and the time series lengths are equal. The number of features and batch size are both 1. ( nd.array([4, 2, 8]).expand_dims(1), nd.array([0, 2, 4]).expand_dims(1), nd.array([3]), nd.array([2]), nd.array([[16, 4, 0], [4, 0, 4], [64, 36, 16]]), ), # This tests the case where the amplitude and length scale parameters are constant # and the time series lengths are equal. The batch size is fixed to 1. # The number of features is now 2. ( nd.array([[0, 1], [2, 3], [3, 0], [4, 2]]), nd.array([[3, 2], [0, 2], [1, 1], [2, 0]]), nd.array([3]), nd.array([2]), nd.array([[10, 1, 1, 5], [2, 5, 5, 9], [4, 13, 5, 1], [1, 16, 10, 8]]), ), # This tests the case where the amplitude and length scale parameters are constant # The number of features is 2 and the batch size is 1. # The time series lengths are unequal.
def label2image(pred, ctx=mx.cpu()): colormap = nd.array(VOC_COLORMAP, ctx=ctx, dtype="uint8") X = pred.astype("int32") return colormap[X, :]