def test_word_embedding_similarity_evaluation_models(similarity_function): try: from scipy import stats except ImportError: raise ImportError('This testcase requires scipy.') dataset = nlp.data.WordSim353() counter = nlp.data.utils.Counter(w for wpair in dataset for w in wpair[:2]) vocab = nlp.vocab.Vocab(counter) vocab.set_embedding( nlp.embedding.create('fasttext', source='wiki.simple', embedding_root='tests/data/embedding')) data = [[vocab[d[0]], vocab[d[1]], d[2]] for d in dataset] words1, words2, scores = zip(*data) evaluator = nlp.embedding.evaluation.WordEmbeddingSimilarity( vocab.embedding.idx_to_vec, similarity_function=similarity_function) evaluator.initialize() words1, words2 = nd.array(words1), nd.array(words2) pred_similarity = evaluator(words1, words2) sr = stats.spearmanr(pred_similarity.asnumpy(), np.array(scores)) assert np.isclose(0.6076485693769645, sr.correlation)
def main(ctx): calcEngine = CALC() tmp = np.asarray( [k for k in range(6)] ) matA = nd.array( np.reshape( tmp ,(2,3) ) ).as_in_context( ctx ) tmp = np.asarray( [k*10 for k in range(6)] ) matB = nd.array( np.reshape( tmp, (2,3) ) ).as_in_context( ctx ) num = 1000 if 1: t0 = time.time() for k in range(num): matD = calcEngine.calc_sum(matA, matB) t1 = time.time() print 'dll: time cost {}ms'.format( float(t1 - t0)*1000/num) print matD if 1: t0 = time.time() for k in range(num): matC = calc_sum(matA, matB) t1 = time.time() print 'py: time cost {}ms'.format( float(t1 - t0)*1000/num) print matC
def _preprocess(self, data): input_shape = self.signature['inputs'][0]['data_shape'] height, width = input_shape[2:] img_arr = image.read(data[0]) img_arr = image.resize(img_arr, width, height) img_arr = image.color_normalize(img_arr, nd.array([127.5]), nd.array([127.5])) img_arr = image.transform_shape(img_arr) return [img_arr]
def next(self): if self._fetcher.iter_next(): tic = time.time() data_batch = self._fetcher.get() print 'Waited for {} seconds'.format(time.time() - tic) else: raise StopIteration return DataBatch(data=[array(data_batch[0])], label=[array(data_batch[1])])
def train(input_variable, target_variable, encoder, decoder, teacher_forcing_ratio, encoder_optimizer, decoder_optimizer, criterion, max_length, ctx): with autograd.record(): loss = F.zeros((1,), ctx=ctx) encoder_hidden = encoder.initHidden(ctx) input_length = input_variable.shape[0] target_length = target_variable.shape[0] encoder_outputs, encoder_hidden = encoder( input_variable.expand_dims(0), encoder_hidden) if input_length < max_length: encoder_outputs = F.concat(encoder_outputs.flatten(), F.zeros((max_length - input_length, encoder.hidden_size), ctx=ctx), dim=0) else: encoder_outputs = encoder_outputs.flatten() decoder_input = F.array([SOS_token], ctx=ctx) decoder_hidden = encoder_hidden use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False if use_teacher_forcing: # Teacher forcing: Feed the target as the next input for di in range(target_length): decoder_output, decoder_hidden, decoder_attention = decoder( decoder_input, decoder_hidden, encoder_outputs) loss = F.add(loss, criterion(decoder_output, target_variable[di])) print criterion(decoder_output, target_variable[di]) decoder_input = target_variable[di] # Teacher forcing else: # Without teacher forcing: use its own predictions as the next input for di in range(target_length): decoder_output, decoder_hidden, decoder_attention = decoder( decoder_input, decoder_hidden, encoder_outputs) topi = decoder_output.argmax(axis=1) decoder_input = F.array([topi.asscalar()], ctx=ctx) loss = F.add(loss, criterion(decoder_output, target_variable[di])) if topi.asscalar() == EOS_token: break loss.backward() encoder_optimizer.step(1) decoder_optimizer.step(1) return loss.asscalar()/target_length
def _score_sentence(self, feats, tags): # Gives the score of a provided tag sequence score = nd.array([0]) tags = nd.concat(nd.array([self.tag2idx[START_TAG]]), *tags, dim=0) for i, feat in enumerate(feats): score = score + \ self.transitions[to_scalar(tags[i+1]), to_scalar(tags[i])] + feat[to_scalar(tags[i+1])] score = score + self.transitions[self.tag2idx[STOP_TAG], to_scalar(tags[int(tags.shape[0]-1)])] return score
def forward(self, x): if self.scale_factor == 0: warnings.warn("Scale factor cannot be 0.") return x if isinstance(x, np.ndarray): return nd.array(x/self.scale_factor) return x / self.scale_factor
def calculate_avg_q(samples, qnet): total_q = 0.0 for i in range(len(samples)): state = nd.array(samples[i:i + 1], ctx=qnet.ctx) / float(255.0) total_q += qnet.forward(is_train=False, data=state)[0].asnumpy().max(axis=1).sum() avg_q_score = total_q / float(len(samples)) return avg_q_score
def test_out_grads(): x = nd.ones((3, 5)) dx = nd.zeros_like(x) mark_variables([x], [dx]) da = None db = nd.array([1,2,3,4,5]) dc = nd.array([5,4,3,2,1]) with train_section(): a, b, c = nd.split(x, axis=0, num_outputs=3, squeeze_axis=True) backward([a, b, c], [da, db, dc]) assert (dx.asnumpy() == np.array( [[1,1,1,1,1], [1,2,3,4,5], [5,4,3,2,1]])).all()
def SGD(sym, data_inputs, X, Y, X_test, Y_test, total_iter_num, lr=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, dev=mx.gpu()): if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgd', learning_rate=lr, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) start = time.time() for i in range(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) if (i + 1) % 500 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) sample_test_acc(exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=100) start = time.time() return exe, params, params_grad
def data_generator(batch_size): index = list(range(config.training_size)) random.shuffle(index) for i in range(0, config.training_size, batch_size): j = nd.array(index[i:min(i + batch_size, config.training_size)]) yield nd.take(X, j), nd.take(y, j)
def data_iter(): # 产生一个随机索引 idx = list(range(num_examples)) random.shuffle(idx)##打乱 for i in range(0, num_examples, batch_size):##0 10 20 ... j = nd.array(idx[i:min(i+batch_size,num_examples)])##随机抽取10个样例 yield nd.take(X, j), nd.take(y, j)##样例和标签 我们通过python的yield来构造一个迭代器。
def data_iter(): # generate random indices idx = list(range(num_examples)) random.shuffle(idx) # randomly sort for i in range(0, num_examples, batch_size): #1000 examples and fetch 10 each time j = nd.array(idx[i: min(i+batch_size, num_examples)]) yield nd.take(X, j), nd.take(y,j) # ?
def _forward_alg(self, feats): # Do the forward algorithm to compute the partition function alphas = [[-10000.] * self.tagset_size] alphas[0][self.tag2idx[START_TAG]] = 0. alphas = nd.array(alphas) # Iterate through the sentence for feat in feats: alphas_t = [] # The forward variables at this timestep for next_tag in range(self.tagset_size): # broadcast the emission score: it is the same regardless of # the previous tag emit_score = feat[next_tag].reshape((1, -1)) # the ith entry of trans_score is the score of transitioning to # next_tag from i trans_score = self.transitions[next_tag].reshape((1, -1)) # The ith entry of next_tag_var is the value for the # edge (i -> next_tag) before we do log-sum-exp next_tag_var = alphas + trans_score + emit_score # The forward variable for this tag is log-sum-exp of all the # scores. alphas_t.append(log_sum_exp(next_tag_var)) alphas = nd.concat(*alphas_t, dim=0).reshape((1, -1)) terminal_var = alphas + self.transitions[self.tag2idx[STOP_TAG]] alpha = log_sum_exp(terminal_var) return alpha
def test_to_tensor(): # 3D Input data_in = np.random.uniform(0, 255, (300, 300, 3)).astype(dtype=np.uint8) out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8')) assert_almost_equal(out_nd.asnumpy(), np.transpose( data_in.astype(dtype=np.float32) / 255.0, (2, 0, 1))) # 4D Input data_in = np.random.uniform(0, 255, (5, 300, 300, 3)).astype(dtype=np.uint8) out_nd = transforms.ToTensor()(nd.array(data_in, dtype='uint8')) assert_almost_equal(out_nd.asnumpy(), np.transpose( data_in.astype(dtype=np.float32) / 255.0, (0, 3, 1, 2))) # Invalid Input invalid_data_in = nd.random.uniform(0, 255, (5, 5, 300, 300, 3)).astype(dtype=np.uint8) transformer = transforms.ToTensor() assertRaises(MXNetError, transformer, invalid_data_in)
def get_image(self,X): B,C,H,W = self.shape X = np.reshape(X,(28,28)) X = X[:,:,np.newaxis] X = np.tile(X,(1,1,3)) if H > X.shape[0] or W > X.shape[1]: raise RuntimeError if H < X.shape[0] or W < X.shape[1]: if self.fortrain: X, _ = mx.image.random_crop(nd.array(X),(H,W)) else: X,_ = mx.image.center_crop(nd.array(X),(H,W)) X = np.transpose(X.asnumpy(),(2,0,1)) else: #print "data augment is off" X = np.transpose(X,(2,0,1)) return X
def SGLD(sym, X, Y, X_test, Y_test, total_iter_num, data_inputs=None, learning_rate=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, thin_interval=100, burn_in_iter_num=1000, task='classification', dev=mx.gpu()): if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgld', learning_rate=learning_rate, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) sample_pool = [] start = time.time() for i in xrange(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) print k, nd.norm(params_grad[k]).asnumpy() if i < burn_in_iter_num: continue else: if 0 == (i - burn_in_iter_num) % thin_interval: if optimizer.lr_scheduler is not None: lr = optimizer.lr_scheduler(optimizer.num_update) else: lr = learning_rate sample_pool.append([lr, copy_param(exe)]) if (i + 1) % 100000 == 0: end = time.time() if task == 'classification': print "Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start) test_correct, test_total, test_acc = \ sample_test_acc(exe, sample_pool=sample_pool, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) print "Test %d/%d=%f" % (test_correct, test_total, test_acc) else: print "Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", print sample_test_regression(exe=exe, sample_pool=sample_pool, X=X_test, Y=Y_test, minibatch_size=minibatch_size, save_path='regression_SGLD.txt') start = time.time() return exe, sample_pool
def forward(self, x): if isinstance(x, np.ndarray): x = nd.array(x) if self._max_len > x.size: pad = nd.ones((self._max_len - x.size,)) * self._fill_value x = nd.concat(x, pad, dim=0) elif self._max_len < x.size: x = x[:self._max_len] return x
def grad_clipping(params, theta, ctx): if theta is not None: norm = nd.array([0.0], ctx) for p in params: norm += nd.sum(p.grad * p.grad) norm = nd.sqrt(norm).asscalar() if norm > theta: for p in params: p.grad[:] *= theta / norm
def test_normalize(): data_in = np.random.uniform(0, 255, (300, 300, 3)).astype(dtype=np.uint8) data_in = transforms.ToTensor()(nd.array(data_in, dtype='uint8')) out_nd = transforms.Normalize(mean=(0, 1, 2), std=(3, 2, 1))(data_in) data_expected = data_in.asnumpy() data_expected[:][:][0] = data_expected[:][:][0] / 3.0 data_expected[:][:][1] = (data_expected[:][:][1] - 1.0) / 2.0 data_expected[:][:][2] = data_expected[:][:][2] - 2.0 assert_almost_equal(data_expected, out_nd.asnumpy())
def test_word_embedding_analogy_evaluation_models(analogy_function): dataset = nlp.data.GoogleAnalogyTestSet() dataset = [d for i, d in enumerate(dataset) if i < 10] embedding = nlp.embedding.create('fasttext', source='wiki.simple', embedding_root='tests/data/embedding') counter = nlp.data.utils.Counter(embedding.idx_to_token) vocab = nlp.vocab.Vocab(counter) vocab.set_embedding(embedding) dataset_coded = [[vocab[d[0]], vocab[d[1]], vocab[d[2]], vocab[d[3]]] for d in dataset] dataset_coded_nd = nd.array(dataset_coded) for k in [1, 3]: for exclude_question_words in [True, False]: evaluator = nlp.embedding.evaluation.WordEmbeddingAnalogy( idx_to_vec=vocab.embedding.idx_to_vec, analogy_function=analogy_function, k=k, exclude_question_words=exclude_question_words) evaluator.initialize() words1 = dataset_coded_nd[:, 0] words2 = dataset_coded_nd[:, 1] words3 = dataset_coded_nd[:, 2] pred_idxs = evaluator(words1, words2, words3) # If we don't exclude inputs most predictions should be wrong words4 = dataset_coded_nd[:, 3] accuracy = nd.mean(pred_idxs[:, 0] == nd.array(words4)) accuracy = accuracy.asscalar() if not exclude_question_words: assert accuracy <= 0.1 # Instead the model would predict W3 most of the time accuracy_w3 = nd.mean(pred_idxs[:, 0] == nd.array(words3)) assert accuracy_w3.asscalar() >= 0.89 else: # The wiki.simple vectors don't perform too good assert accuracy >= 0.29 # Assert output shape assert pred_idxs.shape[1] == k
def _csv_labelled_dataset(self, root, skip_rows=0): with open(self._train_csv, "r") as traincsv: for line in islice(csv.reader(traincsv), skip_rows, None): filename = os.path.join(root, line[0]) label = line[1].strip() if label not in self.synsets: self.synsets.append(label) if self._format not in filename: filename = filename+self._format self.items.append((filename, nd.array([self.synsets.index(label)]).reshape((1,))))
def data_iter_random(corpus_indices, batch_size, num_steps, ctx=None): num_examples = len(corpus_indices) epoch_size = (num_examples - num_steps) // batch_size example_indices = list(range(num_examples - num_steps)) 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] dat = [_data(j) for j in batch_indices] data = nd.array( dat, ctx=ctx) label = nd.array( [_data(j + 1) for j in batch_indices], ctx=ctx ) yield data,label
def forward(self, x): if isinstance(x, np.ndarray): y = x elif isinstance(x, nd.NDArray): y = x.asnumpy() else: warnings.warn("MFCC - allowed datatypes mx.nd.NDArray and numpy.ndarray") return x audio_tmp = np.mean(librosa.feature.mfcc(y=y, sr=self._sampling_rate, n_mfcc=self._num_fcc).T, axis=0) return nd.array(audio_tmp)
def get_next(self): """in zoo, each DataBatch has pic of one time step of sample of #batch_size """ assert(self.cursor < self.num_data), "DataIter needs reset." if self.cursor + self.batch_size <= self.num_data: data = [ array(d[1][self.cursor:self.cursor+self.batch_size]) for d in self.data] label = [ array(l[1][self.cursor:self.cursor+self.batch_size]) for l in self.label] else: pad = self.batch_size - self.num_data + self.cursor data = [array(np.concatenate( (d[1][self.cursor:], d[1][:pad]), axis=0)) for d in self.data] label = [array(np.concatenate( (l[1][self.cursor:], l[1][:pad]), axis=0)) for l in self.label] batch = DataBatch(data = data, label = label, pad= self.getpad(), index = None) return batch
def gradient_clipping(parameters, threshold, ctx): if threshold is not None: norm = nd.array([0.0], ctx) for parameter in parameters: norm += nd.sum(parameter.grad ** 2) norm = nd.sqrt(norm).asscalar() if norm > threshold: for parameter in parameters: parameter.grad[:] *= (threshold / norm)
def reset(self): """Resets the iterator to the beginning of the data.""" self.curr_idx = 0 #shuffle data in each bucket random.shuffle(self.idx) for i, buck in enumerate(self.sentences): self.indices[i], self.sentences[i], self.characters[i], self.label[i] = shuffle(self.indices[i], self.sentences[i], self.characters[i], self.label[i]) self.ndindex = [] self.ndsent = [] self.ndchar = [] self.ndlabel = [] #for each bucket of data for i, buck in enumerate(self.sentences): #append the lists with an array self.ndindex.append(ndarray.array(self.indices[i], dtype=self.dtype)) self.ndsent.append(ndarray.array(self.sentences[i], dtype=self.dtype)) self.ndchar.append(ndarray.array(self.characters[i], dtype=self.dtype)) self.ndlabel.append(ndarray.array(self.label[i], dtype=self.dtype))
def next(self): """Returns the next batch of data.""" #print('next') batch_size = self.batch_size batch_data = nd.empty((batch_size,)+self.data_shape) batch_label = nd.empty((batch_size,)+self.label_shape) i = 0 #self.cutoff = random.randint(800,1280) try: while i < batch_size: #print('N', i) data, label = self.next_sample() data = nd.array(data) data = nd.transpose(data, axes=(2, 0, 1)) label = nd.array(label) #print(data.shape, label.shape) batch_data[i][:] = data batch_label[i][:] = label i += 1 except StopIteration: if not i: raise StopIteration return mx.io.DataBatch([batch_data], [batch_label], batch_size - i)
def _list_images(self, root): self._image_list = [] self._label_list = [] for dir in os.listdir(root): for filename in os.listdir(root + "/" + dir): name, ext = os.path.splitext(filename) if ext.lower() not in self._exts: continue else: labels = dir.split("_") num_of_labels = [] for i in range(len(labels)): num_of_labels.append(nd.array([self._label_dict[i][labels[i]]])) self._image_list.append(root + "/" + dir + "/" + filename) self._label_list.append(num_of_labels)
def dumpR(data_set, mx_model, batch_size, name='', data_extra = None, label_shape = None): print('dump verification embedding..') data_list = data_set[0] issame_list = data_set[1] model = mx_model embeddings_list = [] if data_extra is not None: _data_extra = nd.array(data_extra) time_consumed = 0.0 if label_shape is None: _label = nd.ones( (batch_size,) ) else: _label = nd.ones( label_shape ) for i in xrange( len(data_list) ): data = data_list[i] embeddings = None ba = 0 while ba<data.shape[0]: bb = min(ba+batch_size, data.shape[0]) count = bb-ba _data = nd.slice_axis(data, axis=0, begin=bb-batch_size, end=bb) #print(_data.shape, _label.shape) time0 = datetime.datetime.now() if data_extra is None: db = mx.io.DataBatch(data=(_data,), label=(_label,)) else: db = mx.io.DataBatch(data=(_data,_data_extra), label=(_label,)) model.forward(db, is_train=False) net_out = model.get_outputs() _embeddings = net_out[0].asnumpy() time_now = datetime.datetime.now() diff = time_now - time0 time_consumed+=diff.total_seconds() if embeddings is None: embeddings = np.zeros( (data.shape[0], _embeddings.shape[1]) ) embeddings[ba:bb,:] = _embeddings[(batch_size-count):,:] ba = bb embeddings_list.append(embeddings) embeddings = embeddings_list[0] + embeddings_list[1] embeddings = sklearn.preprocessing.normalize(embeddings) actual_issame = np.asarray(issame_list) outname = os.path.join('temp.bin') with open(outname, 'wb') as f: pickle.dump((embeddings, issame_list), f, protocol=pickle.HIGHEST_PROTOCOL)
# print all_X.head() #对数值序列,用列的均值填充nan all_X = all_X.fillna(all_X.mean()) # print all_X.head() num_train = train.shape[0] x_train = all_X[:num_train].as_matrix() x_test = all_X[num_train:].as_matrix() # y_train = train.SalePrice # y_train = (y_train - y_train.mean()) / y_train.std() y_train = train.SalePrice.as_matrix() #转换为NDarray数据 X_train = nd.array(x_train) X_test = nd.array(x_test) Y_train = nd.array(y_train) Y_train.reshape((num_train, 1)) square_loss = gluon.loss.L2Loss() def get_srme_log(net, X_train, Y_train): num_train = X_train.shape[0] clipped_preds = nd.clip(net(X_train), 1, float('inf')) #对net(X)结果进行截断[1,正无穷] return np.sqrt(2 * nd.sum( square_loss(nd.log(clipped_preds), nd.log(Y_train))).asscalar() / num_train)
def prepare_sequence(seq, word2Idx): return nd.array([word2Idx[w] for w in seq])
import numpy as np # def f(a): # b = a * 2 # while nd.norm(b).asscalar() < 1000: # b = b * 2 # if nd.sum(b).asscalar() > 0: # c = b # else: # c = 100 * b # return c # # a = nd.random_normal(shape=3) # a.attach_grad() # with ag.record(): # c = f(a) # c.backward() # # print(a.grad == c/a) x = nd.array([1]) x.attach_grad() with ag.record(): #y = x * x * 2 + x + 1 y = nd.exp(x) y.backward() print(x.grad)
def unique(input): # TODO: fallback to numpy is unfortunate tmp = input.asnumpy() tmp = np.unique(tmp) return nd.array(tmp, ctx=input.context, dtype=input.dtype)
# -*- coding: UTF-8 -*- import numpy as np import mxnet.ndarray as nd import mxnet.autograd as ag x = nd.array([[1, 2], [3, 4]]) x.attach_grad() with ag.record(): y = x * 2 z = y * x z.backward() print(x.grad) def f(a): b = a * 2 while nd.norm(b).asscalar() < 1000: b = b * 2 if nd.sum(b).asscalar() > 0: c = b else: c = 100 * b return c a = nd.random_normal(shape=3)
#填充缺失数据 all_X = all_X.fillna(all_X.mean()) #转换数据格式 num_train = train.shape[0] X_train = all_X[:num_train].as_matrix() X_test = all_X[:num_train].as_matrix() y_train = train.SalePrice.as_matrix() #导入NDArray格式数据 from mxnet import ndarray as nd from mxnet import autograd from mxnet import gluon X_train = nd.array(X_train) y_train = nd.array(y_train) y_train.reshape((num_train, 1)) X_test = nd.array(X_test) #定义损失函数 square_loss = gluon.loss.L2Loss() #定义测量结果用函数 def get_rmse_log(net, X_train, y_train): num_train = X_train.shape[0] clipped_preds = nd.clip(net(X_train), 1, float('inf')) #约束预测值的大小 return np.sqrt(2 * nd.sum(square_loss(nd.log(clipped_preds), nd.log(y_train))).asscalar() /num_train)
def arange(start, stop, dtype=np.int64): if start >= stop: return nd.array([], dtype=dtype) else: return nd.arange(start, stop, dtype=dtype)
def detect(self, img, threshold=0.05, scales=[1.0]): proposals_list = [] scores_list = [] for im_scale in scales: if im_scale != 1.0: im = cv2.resize(img, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) else: im = img im = im.astype(np.float32) #self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) im_info = [im.shape[0], im.shape[1], im_scale] im_tensor = np.zeros((1, 3, im.shape[0], im.shape[1])) for i in range(3): im_tensor[0, i, :, :] = im[:, :, 2 - i] - self.pixel_means[2 - i] data = nd.array(im_tensor) db = mx.io.DataBatch(data=(data, ), provide_data=[('data', data.shape)]) self.model.forward(db, is_train=False) net_out = self.model.get_outputs() pre_nms_topN = self._rpn_pre_nms_top_n #post_nms_topN = self._rpn_post_nms_top_n #min_size_dict = self._rpn_min_size_fpn for s in self._feat_stride_fpn: if len(scales) > 1 and s == 32 and im_scale == scales[-1]: continue _key = 'stride%s' % s stride = int(s) idx = 0 if s == 16: idx = 2 elif s == 8: idx = 4 print('getting', im_scale, stride, idx, len(net_out), data.shape, file=sys.stderr) scores = net_out[idx].asnumpy() #print(scores.shape) idx += 1 #print('scores',stride, scores.shape, file=sys.stderr) scores = scores[:, self._num_anchors['stride%s' % s]:, :, :] bbox_deltas = net_out[idx].asnumpy() #if DEBUG: # print 'im_size: ({}, {})'.format(im_info[0], im_info[1]) # print 'scale: {}'.format(im_info[2]) _height, _width = int(im_info[0] / stride), int(im_info[1] / stride) height, width = bbox_deltas.shape[2], bbox_deltas.shape[3] A = self._num_anchors['stride%s' % s] K = height * width anchors = anchors_plane( height, width, stride, self._anchors_fpn['stride%s' % s].astype(np.float32)) #print((height, width), (_height, _width), anchors.shape, bbox_deltas.shape, scores.shape, file=sys.stderr) anchors = anchors.reshape((K * A, 4)) #print('pre', bbox_deltas.shape, height, width) bbox_deltas = self._clip_pad(bbox_deltas, (height, width)) #print('after', bbox_deltas.shape, height, width) bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)).reshape( (-1, 4)) scores = self._clip_pad(scores, (height, width)) scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1)) #print(anchors.shape, bbox_deltas.shape, A, K, file=sys.stderr) proposals = self._bbox_pred(anchors, bbox_deltas) #proposals = anchors proposals = clip_boxes(proposals, im_info[:2]) #keep = self._filter_boxes(proposals, min_size_dict['stride%s'%s] * im_info[2]) #proposals = proposals[keep, :] #scores = scores[keep] #print('333', proposals.shape) scores_ravel = scores.ravel() order = scores_ravel.argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] proposals /= im_scale proposals_list.append(proposals) scores_list.append(scores) proposals = np.vstack(proposals_list) scores = np.vstack(scores_list) scores_ravel = scores.ravel() order = scores_ravel.argsort()[::-1] #if config.TEST.SCORE_THRESH>0.0: # _count = np.sum(scores_ravel>config.TEST.SCORE_THRESH) # order = order[:_count] #if pre_nms_topN > 0: # order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] det = np.hstack((proposals, scores)).astype(np.float32) #if np.shape(det)[0] == 0: # print("Something wrong with the input image(resolution is too low?), generate fake proposals for it.") # proposals = np.array([[1.0, 1.0, 2.0, 2.0]]*post_nms_topN, dtype=np.float32) # scores = np.array([[0.9]]*post_nms_topN, dtype=np.float32) # det = np.array([[1.0, 1.0, 2.0, 2.0, 0.9]]*post_nms_topN, dtype=np.float32) if self.nms_threshold < 1.0: keep = self.nms(det) det = det[keep, :] if threshold > 0.0: keep = np.where(det[:, 4] >= threshold)[0] det = det[keep, :] return det
def predict_value(self, inputs): assert mx.autograd.is_training() is False _, value = self.actorcritic(nd.array(inputs, ctx=CTX)) value = value.asnumpy() return value
def data_iter(): 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 nd.take(X, j), nd.take(y, j)
def model_data(): mnist = mx.test_utils.get_mnist() train_data = nd.array(mnist["train_data"].reshape(-1, 784)) train_label = nd.array(mnist["train_label"]) test_data = nd.array(mnist["test_data"].reshape(-1, 784)) return train_data, train_label, test_data
def predict(self, x): return self.forward(nd.array([x], self.ctx, dtype=int))[0].asnumpy()
def K_means_Algorithm(epoch=100, point_numbers=2000, centroid_numbers=5, ctx=mx.gpu(0)): dataset = [] centroid = [] # data generation for i in range(point_numbers): if random.random() > 0.5: dataset.append([ np.random.normal(loc=0, scale=0.9), np.random.normal(loc=0, scale=0.9) ]) else: dataset.append([ np.random.normal(loc=3, scale=0.5), np.random.normal(loc=0, scale=0.9) ]) df = pd.DataFrame({ "x": [d[0] for d in dataset], "y": [d[1] for d in dataset] }) sns.lmplot("x", "y", data=df, fit_reg=False, size=10) plt.savefig("K means Algorithm init using mxnet.png") # 1-step random.shuffle(dataset) for i in range(centroid_numbers): centroid.append(random.choice(dataset)) # using mxnet dataset = nd.array(dataset, ctx=ctx) centroid = nd.array(centroid, ctx=ctx) # data assignment , updating new center values for i in tqdm(range(epoch)): # 2-step diff = nd.subtract(nd.expand_dims(dataset, axis=0), nd.expand_dims(centroid, axis=1)) sqr = nd.square(diff) distance = nd.sum(sqr, axis=2) clustering = nd.argmin(distance, axis=0) # 3-step ''' Because mxnet's nd.where did not return the location. I wrote the np.where function. ''' for j in range(centroid_numbers): centroid[j][:] = nd.mean(nd.take( dataset, nd.array(np.reshape( np.where(np.equal(clustering.asnumpy(), j)), (-1, )), ctx=ctx), axis=0), axis=0) print("epoch : {}".format(i + 1)) for i in range(centroid_numbers): print("{}_center : Final center_value : {}".format( i + 1, centroid.asnumpy()[i])) #4 show result data = {"x": [], "y": [], "cluster": []} for i in range(len(clustering)): data["x"].append(dataset[i][0].asscalar()) data["y"].append(dataset[i][1].asscalar()) data["cluster"].append(clustering[i].asscalar()) df = pd.DataFrame(data) sns.lmplot("x", "y", data=df, fit_reg=False, size=10, hue="cluster") plt.savefig("K means Algorithm completed using mxnet.png") plt.show()
def test(data_set, mx_model, batch_size, nfolds=10, data_extra = None, label_shape = None): ''' test() takes a validation set data_set and the MXNet model mx_model as input and computes accuracies using evaluate() on the set using nfolds cross validation ''' print('testing verification..') data_list = data_set[0] issame_list = data_set[1] model = mx_model embeddings_list = [] if data_extra is not None: _data_extra = nd.array(data_extra) time_consumed = 0.0 if label_shape is None: _label = nd.ones( (batch_size,) ) else: _label = nd.ones( label_shape ) for i in range( len(data_list) ): data = data_list[i] embeddings = None ba = 0 while ba<data.shape[0]: bb = min(ba+batch_size, data.shape[0]) count = bb-ba _data = nd.slice_axis(data, axis=0, begin=bb-batch_size, end=bb) time0 = datetime.datetime.now() if data_extra is None: db = mx.io.DataBatch(data=(_data,), label=(_label,)) else: db = mx.io.DataBatch(data=(_data,_data_extra), label=(_label,)) model.forward(db, is_train=False) net_out = model.get_outputs() _embeddings = net_out[0].asnumpy() time_now = datetime.datetime.now() diff = time_now - time0 time_consumed+=diff.total_seconds() if embeddings is None: embeddings = np.zeros( (data.shape[0], _embeddings.shape[1]) ) embeddings[ba:bb,:] = _embeddings[(batch_size-count):,:] ba = bb embeddings_list.append(embeddings) _xnorm = 0.0 _xnorm_cnt = 0 for embed in embeddings_list: for i in range(embed.shape[0]): _em = embed[i] _norm=np.linalg.norm(_em) _xnorm+=_norm _xnorm_cnt+=1 _xnorm /= _xnorm_cnt embeddings = embeddings_list[0].copy() embeddings = sklearn.preprocessing.normalize(embeddings) acc1 = 0.0 std1 = 0.0 embeddings = embeddings_list[0] + embeddings_list[1] embeddings = sklearn.preprocessing.normalize(embeddings) print(embeddings.shape) print('infer time', time_consumed) _, _, accuracy = evaluate(embeddings, issame_list, nrof_folds=nfolds) acc2, std2 = np.mean(accuracy), np.std(accuracy) return acc1, std1, acc2, std2, _xnorm, embeddings_list
for word in sentence: if word not in word2idx: word2idx[word] = len(word2idx) tag2idx = {"B": 0, "I": 1, "O": 2, START_TAG: 3, STOP_TAG: 4} model = BiLSTM_CRF(len(word2idx), tag2idx, EMBEDDING_DIM, HIDDEN_DIM) model.initialize(mx.init.Xavier(magnitude=2.24), ctx=mx.cpu()) optimizer = gluon.Trainer(model.collect_params(), 'sgd', { 'learning_rate': 0.01, 'wd': 1e-4 }) # Check predictions before training precheck_sent = prepare_sequence(training_data[0][0], word2idx) precheck_tags = nd.array([tag2idx[t] for t in training_data[0][1]]) print(model(precheck_sent)) # Make sure prepare_sequence from earlier in the LSTM section is loaded for epoch in range( 300): # again, normally you would NOT do 300 epochs, it is toy data neg_log_likelihood_acc = 0. iter = 0 for i, (sentence, tags) in enumerate(training_data): # Step 1. Get our inputs ready for the network, that is, # turn them into Variables of word indices. # Remember to use autograd to record the calculation. with ag.record(): sentence_in = prepare_sequence(sentence, word2idx) targets = nd.array([tag2idx[t] for t in tags])
def test(data_set, mx_model, batch_size, nfolds=10, data_extra=None, label_shape=None): print('testing verification..') data_list = data_set[0] issame_list = data_set[1] model = mx_model embeddings_list = [] if data_extra is not None: _data_extra = nd.array(data_extra) time_consumed = 0.0 if label_shape is None: _label = nd.ones((batch_size, )) else: _label = nd.ones(label_shape) for i in range(len(data_list)): data = data_list[i] embeddings = None ba = 0 while ba < data.shape[0]: bb = min(ba + batch_size, data.shape[0]) count = bb - ba _data = nd.slice_axis(data, axis=0, begin=bb - batch_size, end=bb) time0 = datetime.datetime.now() if data_extra is None: db = mx.io.DataBatch(data=(_data, ), label=(_label, )) else: db = mx.io.DataBatch(data=(_data, _data_extra), label=(_label, )) model.forward(db, is_train=False) net_out = model.get_outputs() #_arg, _aux = model.get_params() #__arg = {} #for k,v in _arg.iteritems(): # __arg[k] = v.as_in_context(_ctx) #_arg = __arg #_arg["data"] = _data.as_in_context(_ctx) #_arg["softmax_label"] = _label.as_in_context(_ctx) #for k,v in _arg.iteritems(): # print(k,v.context) #exe = sym.bind(_ctx, _arg ,args_grad=None, grad_req="null", aux_states=_aux) #exe.forward(is_train=False) #net_out = exe.outputs _embeddings = net_out[0].asnumpy() time_now = datetime.datetime.now() diff = time_now - time0 time_consumed += diff.total_seconds() if embeddings is None: embeddings = np.zeros((data.shape[0], _embeddings.shape[1])) embeddings[ba:bb, :] = _embeddings[(batch_size - count):, :] ba = bb embeddings_list.append(embeddings) _xnorm = 0.0 _xnorm_cnt = 0 for embed in embeddings_list: for i in range(embed.shape[0]): _em = embed[i] _norm = np.linalg.norm(_em) _xnorm += _norm _xnorm_cnt += 1 _xnorm /= _xnorm_cnt embeddings = embeddings_list[0].copy() embeddings = sklearn.preprocessing.normalize(embeddings) acc1 = 0.0 std1 = 0.0 #_, _, accuracy, val, val_std, far = evaluate(embeddings, issame_list, nrof_folds=10) #acc1, std1 = np.mean(accuracy), np.std(accuracy) #print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) #embeddings = np.concatenate(embeddings_list, axis=1) embeddings = embeddings_list[0] + embeddings_list[1] embeddings = sklearn.preprocessing.normalize(embeddings) print(embeddings.shape) print('infer time', time_consumed) _, _, accuracy, val, val_std, far = evaluate(embeddings, issame_list, nrof_folds=nfolds) acc2, std2 = np.mean(accuracy), np.std(accuracy) return acc1, std1, acc2, std2, _xnorm, embeddings_list
#l = np.array(os.listdir(os.path.join(PATH, 'testing-images')))#[:1000] #np.random.shuffle(l) #l = list(l) #l = ['Pepsi-slogans-over-years.jpg'] cur_model = model.model() data = mx.sym.Variable(name='data')#shape=(1, 3, img.shape[0], img.shape[1])) out = cur_model.get_symbol(data=data) net = mx.gluon.SymbolBlock(outputs=out, inputs=data) ctx = mx.gpu(0) net.collect_params().initialize(mx.init.Normal(0.01), mx.gpu(0)) net.load_params(os.path.join('simple-0019.params'), ctx=mx.gpu(0), allow_missing=True, ignore_extra=True) ans_hist = [] #for k0,k in enumerate(l): parser = argparse.ArgumentParser(description='Detector') parser.add_argument('path', help='Path to your RGB image') args = parser.parse_args() img = cv2.cvtColor(cv2.imread(os.path.join(args.path)), cv2.COLOR_BGR2RGB).astype(float) / 255. #cv2.imshow('img', img) #cv2.waitKey() ans = net(nd.array(np.expand_dims(np.transpose(img, [2,0,1]), 0), ctx=mx.gpu(0))) anchors = ans.asnumpy()[0,:3,:,:] coords = ans.asnumpy()[0,3:,:,:] show_boxes(img, np.concatenate([anchors, coords], 0), thresh=8., name=args.path)
def batchify(self, data): """ Collate data into batch. Use shared memory for stacking. :param data: a list of array, with layout of 'NTC'. :return either x and x's unpadded lengths, or x, x's unpadded lengths, y and y's unpadded lengths if labels are not supplied. """ # input layout is NTC if len(self._labels) < 1: keys, inputs, labels = [item[0] for item in data ], [item[1] for item in data], None else: keys, inputs, labels = [item[0] for item in data], [item[1] for item in data], \ [item[2] for item in data] if len(data) > 1: max_data_len = max([seq.shape[0] for seq in inputs]) max_labels_len = 0 if not labels else max( [len(seq) for seq in labels]) else: max_data_len = inputs[0].shape[0] max_labels_len = 0 if not labels else len(labels[0]) x_lens = [item.shape[0] for item in inputs] if len(self._labels) < 1: y_lens = None else: y_lens = [len(item) + 2 for item in labels] # 2 for BOS, EOS # labels = [None for i in range(len(inputs))] for i, seq in enumerate(inputs): if self._reverse.lower() == 'true': seq = np.flip(seq, axis=0) pad_len = max_data_len - seq.shape[0] if self._random_pad: head_pad = int(random() * pad_len) inputs[i] = np.pad(seq, ((head_pad, pad_len - head_pad), (0, 0)), 'constant', constant_values=0) else: inputs[i] = np.pad(seq, ((0, pad_len), (0, 0)), 'constant', constant_values=0) if labels is not None: labels[i].insert(0, BOS) labels[i].append(EOS) while len(labels[i]) < max_labels_len + 2: # 2 for BOS, EOS labels[i].append(PAD) labels[i] = np.array(self._dict.words_to_ids(labels[i])) inputs = np.asarray(inputs, dtype=np.float32) if labels is not None: labels = np.asarray(labels, dtype=np.int32) if self._layout == 'TNC': inputs = inputs.transpose((1, 0, 2)) if labels is not None: labels = labels.transpose((1, 0)) elif self._layout == 'NTC': pass else: raise Exception("unknown layout") return (keys, nd.array(inputs, dtype=inputs.dtype, ctx=context.Context('cpu_shared', 0)), nd.array(x_lens, ctx=context.Context('cpu_shared', 0))) \ if labels is None else (keys, nd.array(inputs, dtype=inputs.dtype, ctx=context.Context('cpu_shared', 0)), nd.array(x_lens, ctx=context.Context('cpu_shared', 0)), nd.array(labels, dtype=labels.dtype, ctx=context.Context('cpu_shared', 0)), nd.array(y_lens, ctx=context.Context('cpu_shared', 0)))
def test_token_embedding_from_file(tmpdir, allow_extend): embed_root = str(tmpdir) embed_name = 'my_embed' elem_delim = '\t' pretrain_file = 'my_pretrain_file.txt' from_file = functools.partial(nlp.embedding.TokenEmbedding.from_file, allow_extend=allow_extend) _mk_my_pretrain_file(os.path.join(embed_root, embed_name), elem_delim, pretrain_file) pretrain_file_path = os.path.join(embed_root, embed_name, pretrain_file) my_embed = from_file(pretrain_file_path, elem_delim) assert 'a' in my_embed assert my_embed.unknown_token == '<unk>' assert my_embed.unknown_token in my_embed first_vec = my_embed.idx_to_vec[0] assert_almost_equal(first_vec.asnumpy(), np.array([0, 0, 0, 0, 0])) # Test properties assert my_embed.token_to_idx == {'<unk>': 0, 'a': 1, 'b': 2} assert my_embed.idx_to_token == ['<unk>', 'a', 'b'] assert_almost_equal( my_embed.idx_to_vec.asnumpy(), np.array([[0, 0, 0, 0, 0], [0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1]])) # Test __getitem__. unk_vec = my_embed['A'] assert_almost_equal(unk_vec.asnumpy(), np.array([0, 0, 0, 0, 0])) a_vec = my_embed['a'] assert_almost_equal(a_vec.asnumpy(), np.array([0.1, 0.2, 0.3, 0.4, 0.5])) my_embed = from_file(pretrain_file_path, elem_delim) # Test __setitem__. my_embed['a'] = nd.array([1, 2, 3, 4, 5]) assert_almost_equal(my_embed['a'].asnumpy(), np.array([1, 2, 3, 4, 5])) if allow_extend: my_embed['unknown$$$'] = nd.array([0, 0, 0, 0, 0]) assert_almost_equal(my_embed['unknown$$$'].asnumpy(), np.array([0, 0, 0, 0, 0])) else: with pytest.raises(KeyError): my_embed['unknown$$$'] = nd.array([0, 0, 0, 0, 0]) with pytest.raises(AssertionError): my_embed['<unk>'] = nd.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) with pytest.raises(AssertionError): my_embed['<unk>'] = nd.array([0]) unk_vecs = my_embed['<unk$unk@unk>', '<unk$unk@unk>'] assert_almost_equal(unk_vecs.asnumpy(), np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])) # Test loaded unknown vectors. pretrain_file2 = 'my_pretrain_file2.txt' _mk_my_pretrain_file3(os.path.join(embed_root, embed_name), elem_delim, pretrain_file2) pretrain_file_path = os.path.join(embed_root, embed_name, pretrain_file2) my_embed2 = from_file(pretrain_file_path, elem_delim, init_unknown_vec=nd.ones, unknown_token='<unk>') unk_vec2 = my_embed2['<unk>'] assert_almost_equal(unk_vec2.asnumpy(), np.array([1, 1, 1, 1, 1])) unk_vec2 = my_embed2['<unk$unk@unk>'] assert_almost_equal(unk_vec2.asnumpy(), np.array([1, 1, 1, 1, 1])) my_embed3 = from_file(pretrain_file_path, elem_delim, init_unknown_vec=nd.ones, unknown_token='<unk1>') unk_vec3 = my_embed3['<unk1>'] assert_almost_equal(unk_vec3.asnumpy(), np.array([1.1, 1.2, 1.3, 1.4, 1.5])) unk_vec3 = my_embed3['<unk$unk@unk>'] assert_almost_equal(unk_vec3.asnumpy(), np.array([1.1, 1.2, 1.3, 1.4, 1.5])) # Test error handling. invalid_pretrain_file = 'invalid_pretrain_file.txt' _mk_my_invalid_pretrain_file(os.path.join(embed_root, embed_name), elem_delim, invalid_pretrain_file) pretrain_file_path = os.path.join(embed_root, embed_name, invalid_pretrain_file) with pytest.raises(AssertionError): from_file(pretrain_file_path, elem_delim) invalid_pretrain_file2 = 'invalid_pretrain_file2.txt' _mk_my_invalid_pretrain_file2(os.path.join(embed_root, embed_name), elem_delim, invalid_pretrain_file2) pretrain_file_path = os.path.join(embed_root, embed_name, invalid_pretrain_file2) with pytest.raises(AssertionError): from_file(pretrain_file_path, elem_delim)
def test_vocab_set_embedding_with_one_custom_embedding(tmpdir, allow_extend): embed_root = str(tmpdir) embed_name = 'my_embed' elem_delim = '\t' pretrain_file = 'my_pretrain_file1.txt' from_file = functools.partial(nlp.embedding.TokenEmbedding.from_file, allow_extend=allow_extend) _mk_my_pretrain_file(os.path.join(embed_root, embed_name), elem_delim, pretrain_file) pretrain_file_path = os.path.join(embed_root, embed_name, pretrain_file) counter = nlp.data.utils.Counter( ['a', 'b', 'b', 'c', 'c', 'c', 'some_word$']) v1 = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token='<unk>', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=['<pad>']) v1_no_unk = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token=None, padding_token=None, bos_token=None, eos_token=None, reserved_tokens=['<pad>']) e1 = from_file(pretrain_file_path, elem_delim, init_unknown_vec=nd.ones) assert v1.embedding is None assert v1_no_unk.embedding is None v1.set_embedding(e1) v1_no_unk.set_embedding(e1) assert v1.embedding is not None assert v1_no_unk.embedding is not None # Test properties assert v1.embedding.token_to_idx == { '<unk>': 0, '<pad>': 1, 'c': 2, 'b': 3, 'a': 4, 'some_word$': 5 } assert v1.embedding.idx_to_token == [ '<unk>', '<pad>', 'c', 'b', 'a', 'some_word$' ] assert v1_no_unk.embedding.token_to_idx == { '<pad>': 0, 'c': 1, 'b': 2, 'a': 3, 'some_word$': 4 } assert v1_no_unk.embedding.idx_to_token == [ '<pad>', 'c', 'b', 'a', 'some_word$' ] assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0.6, 0.7, 0.8, 0.9, 1], [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1, 1, 1, 1]])) assert_almost_equal( v1_no_unk.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0.6, 0.7, 0.8, 0.9, 1], [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1, 1, 1, 1]])) assert_almost_equal(v1.embedding['c'].asnumpy(), np.array([1, 1, 1, 1, 1])) assert_almost_equal(v1_no_unk.embedding['c'].asnumpy(), np.array([1, 1, 1, 1, 1])) assert_almost_equal(v1.embedding[['c']].asnumpy(), np.array([[1, 1, 1, 1, 1]])) assert_almost_equal(v1_no_unk.embedding[['c']].asnumpy(), np.array([[1, 1, 1, 1, 1]])) assert_almost_equal(v1.embedding[['a', 'not_exist']].asnumpy(), np.array([[0.1, 0.2, 0.3, 0.4, 0.5], [1, 1, 1, 1, 1]])) with pytest.raises(KeyError): v1_no_unk.embedding['a', 'not_exist'] assert_almost_equal( v1.embedding[['a', 'b']].asnumpy(), np.array([[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1]])) assert_almost_equal( v1_no_unk.embedding[['a', 'b']].asnumpy(), np.array([[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1]])) assert_almost_equal(v1.embedding[['A', 'b']].asnumpy(), np.array([[1, 1, 1, 1, 1], [0.6, 0.7, 0.8, 0.9, 1]])) with pytest.raises(KeyError): v1_no_unk.embedding['A', 'b'] v1.embedding['a'] = nd.array([2, 2, 2, 2, 2]) v1.embedding['b'] = nd.array([3, 3, 3, 3, 3]) v1_no_unk.embedding['a'] = nd.array([2, 2, 2, 2, 2]) v1_no_unk.embedding['b'] = nd.array([3, 3, 3, 3, 3]) assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [3, 3, 3, 3, 3], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]])) assert_almost_equal( v1_no_unk.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [3, 3, 3, 3, 3], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]])) v1.embedding['<unk>'] = nd.array([0, 0, 0, 0, 0]) assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [3, 3, 3, 3, 3], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]])) with pytest.raises(KeyError): # The TokenEmbedding assigned to a vocab is never extendable v1_no_unk.embedding['<unk>'] = nd.array([0, 0, 0, 0, 0]) v1.embedding['<unk>'] = nd.array([10, 10, 10, 10, 10]) assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[10, 10, 10, 10, 10], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [3, 3, 3, 3, 3], [2, 2, 2, 2, 2], [1, 1, 1, 1, 1]])) v1.set_embedding(None) assert v1.embedding is None v1_no_unk.set_embedding(None) assert v1_no_unk.embedding is None
def accuracy(y_hat, y): return (y_hat == y.astype('float32')).mean().asscalar() if __name__ == '__main__': from load_data import split_train_data, train_features, train_labels, test_features, test_id, evaliate import numpy as np from decision_tree import sklearn_tree tfs, tls, vfs, vls = split_train_data() i = 0 mean_acc = 0 for (features, labels, vfeatures, vlabels) in zip(tfs, tls, vfs, vls): print('net : ', i, ' train') features = nd.array(features) labels = nd.array(np.array(labels)) vfeatures = nd.array(np.array(vfeatures)) vlabels = nd.array(np.array(vlabels)) net = train(features, labels, './params/nn.params') net = inter_output(net, 12) features = net(features) clf = sklearn_tree(features.asnumpy(), labels.asnumpy()) vfeatures = net(vfeatures) predict = clf.predict(vfeatures.asnumpy()) print(net) accuracy = evaliate(predict, vlabels.asnumpy()) print('accracy :', accuracy) # predict = net(vfeatures)
def test_vocab_set_embedding_with_two_custom_embeddings(tmpdir, allow_extend): embed_root = str(tmpdir) embed_name = 'my_embed' elem_delim = '\t' pretrain_file1 = 'my_pretrain_file1.txt' pretrain_file2 = 'my_pretrain_file2.txt' from_file = functools.partial(nlp.embedding.TokenEmbedding.from_file, allow_extend=allow_extend) _mk_my_pretrain_file(os.path.join(embed_root, embed_name), elem_delim, pretrain_file1) _mk_my_pretrain_file2(os.path.join(embed_root, embed_name), elem_delim, pretrain_file2) pretrain_file_path1 = os.path.join(embed_root, embed_name, pretrain_file1) pretrain_file_path2 = os.path.join(embed_root, embed_name, pretrain_file2) my_embed1 = from_file(pretrain_file_path1, elem_delim, init_unknown_vec=nd.ones) my_embed2 = from_file(pretrain_file_path2, elem_delim) counter = nlp.data.utils.Counter( ['a', 'b', 'b', 'c', 'c', 'c', 'some_word$']) v1 = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token='<unk>', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=None) v1.set_embedding(my_embed1, my_embed2) assert v1.embedding is not None assert v1.embedding.token_to_idx == { '<unk>': 0, 'c': 1, 'b': 2, 'a': 3, 'some_word$': 4 } assert v1.embedding.idx_to_token == ['<unk>', 'c', 'b', 'a', 'some_word$'] with pytest.raises(AssertionError): v1.set_embedding(my_embed1, None, my_embed2) assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0.06, 0.07, 0.08, 0.09, 0.1], [0.6, 0.7, 0.8, 0.9, 1, 0, 0, 0, 0, 0], [0.1, 0.2, 0.3, 0.4, 0.5, 0.01, 0.02, 0.03, 0.04, 0.05], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]])) assert_almost_equal(v1.embedding['c'].asnumpy(), np.array([1, 1, 1, 1, 1, 0.06, 0.07, 0.08, 0.09, 0.1])) assert_almost_equal( v1.embedding[['b', 'not_exist']].asnumpy(), np.array([[0.6, 0.7, 0.8, 0.9, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]])) v1.embedding['a'] = nd.array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) v1.embedding['b'] = nd.array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3]) assert_almost_equal( v1.embedding.idx_to_vec.asnumpy(), np.array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0.06, 0.07, 0.08, 0.09, 0.1], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0]])) # Test loaded unknown tokens pretrain_file3 = 'my_pretrain_file3.txt' pretrain_file4 = 'my_pretrain_file4.txt' _mk_my_pretrain_file3(os.path.join(embed_root, embed_name), elem_delim, pretrain_file3) _mk_my_pretrain_file4(os.path.join(embed_root, embed_name), elem_delim, pretrain_file4) pretrain_file_path3 = os.path.join(embed_root, embed_name, pretrain_file3) pretrain_file_path4 = os.path.join(embed_root, embed_name, pretrain_file4) my_embed3 = from_file(pretrain_file_path3, elem_delim, init_unknown_vec=nd.ones, unknown_token='<unk1>') my_embed4 = from_file(pretrain_file_path4, elem_delim, unknown_token='<unk2>') v2 = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token='<unk>', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=None) v2.set_embedding(my_embed3, my_embed4) assert v2.embedding.token_to_idx == { '<unk>': 0, 'c': 1, 'b': 2, 'a': 3, 'some_word$': 4 } assert v2.embedding.idx_to_token == ['<unk>', 'c', 'b', 'a', 'some_word$'] assert_almost_equal( v2.embedding.idx_to_vec.asnumpy(), np.array([[1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15], [1.1, 1.2, 1.3, 1.4, 1.5, 0.06, 0.07, 0.08, 0.09, 0.1], [0.6, 0.7, 0.8, 0.9, 1, 0.11, 0.12, 0.13, 0.14, 0.15], [0.1, 0.2, 0.3, 0.4, 0.5, 0.01, 0.02, 0.03, 0.04, 0.05], [1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15]])) v3 = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token='<unk1>', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=None) v3.set_embedding(my_embed3, my_embed4) assert v3.embedding.token_to_idx == { '<unk1>': 0, 'c': 1, 'b': 2, 'a': 3, 'some_word$': 4 } assert v3.embedding.idx_to_token == ['<unk1>', 'c', 'b', 'a', 'some_word$'] assert_almost_equal( v3.embedding.idx_to_vec.asnumpy(), np.array([[1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15], [1.1, 1.2, 1.3, 1.4, 1.5, 0.06, 0.07, 0.08, 0.09, 0.1], [0.6, 0.7, 0.8, 0.9, 1, 0.11, 0.12, 0.13, 0.14, 0.15], [0.1, 0.2, 0.3, 0.4, 0.5, 0.01, 0.02, 0.03, 0.04, 0.05], [1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15]])) v4 = nlp.Vocab(counter, max_size=None, min_freq=1, unknown_token='<unk2>', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=None) v4.set_embedding(my_embed3, my_embed4) assert v4.embedding.token_to_idx == { '<unk2>': 0, 'c': 1, 'b': 2, 'a': 3, 'some_word$': 4 } assert v4.embedding.idx_to_token == ['<unk2>', 'c', 'b', 'a', 'some_word$'] assert_almost_equal( v4.embedding.idx_to_vec.asnumpy(), np.array([[1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15], [1.1, 1.2, 1.3, 1.4, 1.5, 0.06, 0.07, 0.08, 0.09, 0.1], [0.6, 0.7, 0.8, 0.9, 1, 0.11, 0.12, 0.13, 0.14, 0.15], [0.1, 0.2, 0.3, 0.4, 0.5, 0.01, 0.02, 0.03, 0.04, 0.05], [1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15]])) counter2 = nlp.data.utils.Counter(['b', 'b', 'c', 'c', 'c', 'some_word$']) v5 = nlp.Vocab(counter2, max_size=None, min_freq=1, unknown_token='a', padding_token=None, bos_token=None, eos_token=None, reserved_tokens=None) v5.set_embedding(my_embed3, my_embed4) assert v5.embedding.token_to_idx == { 'a': 0, 'c': 1, 'b': 2, 'some_word$': 3 } assert v5.embedding.idx_to_token == ['a', 'c', 'b', 'some_word$'] assert_almost_equal( v5.embedding.idx_to_vec.asnumpy(), np.array([[1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15], [1.1, 1.2, 1.3, 1.4, 1.5, 0.06, 0.07, 0.08, 0.09, 0.1], [0.6, 0.7, 0.8, 0.9, 1, 0.11, 0.12, 0.13, 0.14, 0.15], [1.1, 1.2, 1.3, 1.4, 1.5, 0.11, 0.12, 0.13, 0.14, 0.15]]))
def test_badcase(data_set, mx_model, batch_size, name='', data_extra=None, label_shape=None): print('testing verification badcase..') data_list = data_set[0] issame_list = data_set[1] model = mx_model embeddings_list = [] if data_extra is not None: _data_extra = nd.array(data_extra) time_consumed = 0.0 if label_shape is None: _label = nd.ones((batch_size, )) else: _label = nd.ones(label_shape) for i in range(len(data_list)): data = data_list[i] embeddings = None ba = 0 while ba < data.shape[0]: bb = min(ba + batch_size, data.shape[0]) count = bb - ba _data = nd.slice_axis(data, axis=0, begin=bb - batch_size, end=bb) #print(_data.shape, _label.shape) time0 = datetime.datetime.now() if data_extra is None: db = mx.io.DataBatch(data=(_data, ), label=(_label, )) else: db = mx.io.DataBatch(data=(_data, _data_extra), label=(_label, )) model.forward(db, is_train=False) net_out = model.get_outputs() _embeddings = net_out[0].asnumpy() time_now = datetime.datetime.now() diff = time_now - time0 time_consumed += diff.total_seconds() if embeddings is None: embeddings = np.zeros((data.shape[0], _embeddings.shape[1])) embeddings[ba:bb, :] = _embeddings[(batch_size - count):, :] ba = bb embeddings_list.append(embeddings) embeddings = embeddings_list[0] + embeddings_list[1] embeddings = sklearn.preprocessing.normalize(embeddings) thresholds = np.arange(0, 4, 0.01) actual_issame = np.asarray(issame_list) nrof_folds = 10 embeddings1 = embeddings[0::2] embeddings2 = embeddings[1::2] assert (embeddings1.shape[0] == embeddings2.shape[0]) assert (embeddings1.shape[1] == embeddings2.shape[1]) nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) nrof_thresholds = len(thresholds) k_fold = LFold(n_splits=nrof_folds, shuffle=False) tprs = np.zeros((nrof_folds, nrof_thresholds)) fprs = np.zeros((nrof_folds, nrof_thresholds)) accuracy = np.zeros((nrof_folds)) indices = np.arange(nrof_pairs) diff = np.subtract(embeddings1, embeddings2) dist = np.sum(np.square(diff), 1) data = data_list[0] pouts = [] nouts = [] for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): # Find the best threshold for the fold acc_train = np.zeros((nrof_thresholds)) #print(train_set) #print(train_set.__class__) for threshold_idx, threshold in enumerate(thresholds): p2 = dist[train_set] p3 = actual_issame[train_set] _, _, acc_train[threshold_idx] = calculate_accuracy( threshold, p2, p3) best_threshold_index = np.argmax(acc_train) for threshold_idx, threshold in enumerate(thresholds): tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = calculate_accuracy( threshold, dist[test_set], actual_issame[test_set]) _, _, accuracy[fold_idx] = calculate_accuracy( thresholds[best_threshold_index], dist[test_set], actual_issame[test_set]) best_threshold = thresholds[best_threshold_index] for iid in test_set: ida = iid * 2 idb = ida + 1 asame = actual_issame[iid] _dist = dist[iid] violate = _dist - best_threshold if not asame: violate *= -1.0 if violate > 0.0: imga = data[ida].asnumpy().transpose( (1, 2, 0))[..., ::-1] #to bgr imgb = data[idb].asnumpy().transpose((1, 2, 0))[..., ::-1] #print(imga.shape, imgb.shape, violate, asame, _dist) if asame: pouts.append((imga, imgb, _dist, best_threshold, ida)) else: nouts.append((imga, imgb, _dist, best_threshold, ida)) tpr = np.mean(tprs, 0) fpr = np.mean(fprs, 0) acc = np.mean(accuracy) pouts = sorted(pouts, key=lambda x: x[2], reverse=True) nouts = sorted(nouts, key=lambda x: x[2], reverse=False) print(len(pouts), len(nouts)) print('acc', acc) gap = 10 image_shape = (112, 224, 3) out_dir = "./badcases" if not os.path.exists(out_dir): os.makedirs(out_dir) if len(nouts) > 0: threshold = nouts[0][3] else: threshold = pouts[-1][3] for item in [(pouts, 'positive(false_negative).png'), (nouts, 'negative(false_positive).png')]: cols = 4 rows = 8000 outs = item[0] if len(outs) == 0: continue #if len(outs)==9: # cols = 3 # rows = 3 _rows = int(math.ceil(len(outs) / cols)) rows = min(rows, _rows) hack = {} if name.startswith('cfp') and item[1].startswith('pos'): hack = { 0: 'manual/238_13.jpg.jpg', 6: 'manual/088_14.jpg.jpg', 10: 'manual/470_14.jpg.jpg', 25: 'manual/238_13.jpg.jpg', 28: 'manual/143_11.jpg.jpg' } filename = item[1] if len(name) > 0: filename = name + "_" + filename filename = os.path.join(out_dir, filename) img = np.zeros((image_shape[0] * rows + 20, image_shape[1] * cols + (cols - 1) * gap, 3), dtype=np.uint8) img[:, :, :] = 255 text_color = (0, 0, 153) text_color = (255, 178, 102) text_color = (153, 255, 51) for outi, out in enumerate(outs): row = outi // cols col = outi % cols if row == rows: break imga = out[0].copy() imgb = out[1].copy() if outi in hack: idx = out[4] print('noise idx', idx) aa = hack[outi] imgb = cv2.imread(aa) #if aa==1: # imgb = cv2.transpose(imgb) # imgb = cv2.flip(imgb, 1) #elif aa==3: # imgb = cv2.transpose(imgb) # imgb = cv2.flip(imgb, 0) #else: # for ii in range(2): # imgb = cv2.transpose(imgb) # imgb = cv2.flip(imgb, 1) dist = out[2] _img = np.concatenate((imga, imgb), axis=1) k = "%.3f" % dist #print(k) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(_img, k, (80, image_shape[0] // 2 + 7), font, 0.6, text_color, 2) #_filename = filename+"_%d.png"%outi #cv2.imwrite(_filename, _img) img[row * image_shape[0]:(row + 1) * image_shape[0], (col * image_shape[1] + gap * col):((col + 1) * image_shape[1] + gap * col), :] = _img #threshold = outs[0][3] font = cv2.FONT_HERSHEY_SIMPLEX k = "threshold: %.3f" % threshold cv2.putText(img, k, (img.shape[1] // 2 - 70, img.shape[0] - 5), font, 0.6, text_color, 2) cv2.imwrite(filename, img)
def detect(self, img, threshold=0.5, scales=[1.0], do_flip=False): #print('in_detect', threshold, scales, do_flip, do_nms) proposals_list = [] scores_list = [] landmarks_list = [] timea = datetime.datetime.now() flips = [0] if do_flip: flips = [0, 1] for im_scale in scales: for flip in flips: if im_scale!=1.0: im = cv2.resize(img, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) else: im = img.copy() if flip: im = im[:,::-1,:] if self.nocrop: if im.shape[0]%32==0: h = im.shape[0] else: h = (im.shape[0]//32+1)*32 if im.shape[1]%32==0: w = im.shape[1] else: w = (im.shape[1]//32+1)*32 _im = np.zeros( (h, w, 3), dtype=np.float32 ) _im[0:im.shape[0], 0:im.shape[1], :] = im im = _im else: im = im.astype(np.float32) if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('X1 uses', diff.total_seconds(), 'seconds') #self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) #im_info = [im.shape[0], im.shape[1], im_scale] im_info = [im.shape[0], im.shape[1]] im_tensor = np.zeros((1, 3, im.shape[0], im.shape[1])) for i in range(3): im_tensor[0, i, :, :] = (im[:, :, 2 - i]/self.pixel_scale - self.pixel_means[2 - i])/self.pixel_stds[2-i] if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('X2 uses', diff.total_seconds(), 'seconds') data = nd.array(im_tensor) db = mx.io.DataBatch(data=(data,), provide_data=[('data', data.shape)]) if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('X3 uses', diff.total_seconds(), 'seconds') self.model.forward(db, is_train=False) net_out = self.model.get_outputs() #post_nms_topN = self._rpn_post_nms_top_n #min_size_dict = self._rpn_min_size_fpn for _idx,s in enumerate(self._feat_stride_fpn): #if len(scales)>1 and s==32 and im_scale==scales[-1]: # continue _key = 'stride%s'%s stride = int(s) #if self.vote and stride==4 and len(scales)>2 and (im_scale==scales[0]): # continue if self.use_landmarks: idx = _idx*3 else: idx = _idx*2 #print('getting', im_scale, stride, idx, len(net_out), data.shape, file=sys.stderr) scores = net_out[idx].asnumpy() if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('A uses', diff.total_seconds(), 'seconds') #print(scores.shape) #print('scores',stride, scores.shape, file=sys.stderr) scores = scores[:, self._num_anchors['stride%s'%s]:, :, :] idx+=1 bbox_deltas = net_out[idx].asnumpy() #if DEBUG: # print 'im_size: ({}, {})'.format(im_info[0], im_info[1]) # print 'scale: {}'.format(im_info[2]) #_height, _width = int(im_info[0] / stride), int(im_info[1] / stride) height, width = bbox_deltas.shape[2], bbox_deltas.shape[3] A = self._num_anchors['stride%s'%s] K = height * width anchors_fpn = self._anchors_fpn['stride%s'%s] anchors = anchors_plane(height, width, stride, anchors_fpn) #print((height, width), (_height, _width), anchors.shape, bbox_deltas.shape, scores.shape, file=sys.stderr) anchors = anchors.reshape((K * A, 4)) #print('num_anchors', self._num_anchors['stride%s'%s], file=sys.stderr) #print('HW', (height, width), file=sys.stderr) #print('anchors_fpn', anchors_fpn.shape, file=sys.stderr) #print('anchors', anchors.shape, file=sys.stderr) #print('bbox_deltas', bbox_deltas.shape, file=sys.stderr) #print('scores', scores.shape, file=sys.stderr) scores = self._clip_pad(scores, (height, width)) scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1)) #print('pre', bbox_deltas.shape, height, width) bbox_deltas = self._clip_pad(bbox_deltas, (height, width)) #print('after', bbox_deltas.shape, height, width) bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)) bbox_pred_len = bbox_deltas.shape[3]//A #print(bbox_deltas.shape) bbox_deltas = bbox_deltas.reshape((-1, bbox_pred_len)) #print(anchors.shape, bbox_deltas.shape, A, K, file=sys.stderr) proposals = self.bbox_pred(anchors, bbox_deltas) proposals = clip_boxes(proposals, im_info[:2]) #if self.vote: # if im_scale>1.0: # keep = self._filter_boxes2(proposals, 160*im_scale, -1) # else: # keep = self._filter_boxes2(proposals, -1, 100*im_scale) # if stride==4: # keep = self._filter_boxes2(proposals, 12*im_scale, -1) # proposals = proposals[keep, :] # scores = scores[keep] #keep = self._filter_boxes(proposals, min_size_dict['stride%s'%s] * im_info[2]) #proposals = proposals[keep, :] #scores = scores[keep] #print('333', proposals.shape) scores_ravel = scores.ravel() #print('__shapes', proposals.shape, scores_ravel.shape) #print('max score', np.max(scores_ravel)) order = np.where(scores_ravel>=threshold)[0] #_scores = scores_ravel[order] #_order = _scores.argsort()[::-1] #order = order[_order] proposals = proposals[order, :] scores = scores[order] if stride==4 and self.decay4<1.0: scores *= self.decay4 if flip: oldx1 = proposals[:, 0].copy() oldx2 = proposals[:, 2].copy() proposals[:, 0] = im.shape[1] - oldx2 - 1 proposals[:, 2] = im.shape[1] - oldx1 - 1 proposals[:,0:4] /= im_scale proposals_list.append(proposals) scores_list.append(scores) if not self.vote and self.use_landmarks: idx+=1 landmark_deltas = net_out[idx].asnumpy() landmark_deltas = self._clip_pad(landmark_deltas, (height, width)) landmark_pred_len = landmark_deltas.shape[1]//A landmark_deltas = landmark_deltas.transpose((0, 2, 3, 1)).reshape((-1, 5, landmark_pred_len//5)) #print(landmark_deltas.shape, landmark_deltas) landmarks = self.landmark_pred(anchors, landmark_deltas) landmarks = landmarks[order, :] if flip: landmarks[:,:,0] = im.shape[1] - landmarks[:,:,0] - 1 #for a in range(5): # oldx1 = landmarks[:, a].copy() # landmarks[:,a] = im.shape[1] - oldx1 - 1 order = [1,0,2,4,3] flandmarks = landmarks.copy() for idx, a in enumerate(order): flandmarks[:,idx,:] = landmarks[:,a,:] #flandmarks[:, idx*2] = landmarks[:,a*2] #flandmarks[:, idx*2+1] = landmarks[:,a*2+1] landmarks = flandmarks landmarks[:,:,0:2] /= im_scale #landmarks /= im_scale #landmarks = landmarks.reshape( (-1, landmark_pred_len) ) landmarks_list.append(landmarks) #proposals = np.hstack((proposals, landmarks)) if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('B uses', diff.total_seconds(), 'seconds') proposals = np.vstack(proposals_list) landmarks = None if proposals.shape[0]==0: if self.use_landmarks: landmarks = np.zeros( (0,5,2) ) return np.zeros( (0,5) ), landmarks scores = np.vstack(scores_list) #print('shapes', proposals.shape, scores.shape) scores_ravel = scores.ravel() order = scores_ravel.argsort()[::-1] #if config.TEST.SCORE_THRESH>0.0: # _count = np.sum(scores_ravel>config.TEST.SCORE_THRESH) # order = order[:_count] proposals = proposals[order, :] scores = scores[order] if not self.vote and self.use_landmarks: landmarks = np.vstack(landmarks_list) landmarks = landmarks[order].astype(np.float32, copy=False) pre_det = np.hstack((proposals[:,0:4], scores)).astype(np.float32, copy=False) if not self.vote: keep = self.nms(pre_det) det = np.hstack( (pre_det, proposals[:,4:]) ) det = det[keep, :] if self.use_landmarks: landmarks = landmarks[keep] else: det = np.hstack( (pre_det, proposals[:,4:]) ) det = self.bbox_vote(det) #if self.use_landmarks: # det = np.hstack((det, landmarks)) if self.debug: timeb = datetime.datetime.now() diff = timeb - timea print('C uses', diff.total_seconds(), 'seconds') return det, landmarks
def detect(self, img, threshold=0.5, scale=1.0): proposals_list = [] scores_list = [] landmarks_list = [] if scale==1.0: im = img else: im = cv2.resize(img, None, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR) im_info = [im.shape[0], im.shape[1]] im_tensor = np.zeros((1, 3, im.shape[0], im.shape[1])) for i in range(3): im_tensor[0, i, :, :] = im[:, :, 2 - i] data = nd.array(im_tensor) db = mx.io.DataBatch(data=(data,), provide_data=[('data', data.shape)]) self.model.forward(db, is_train=False) net_out = self.model.get_outputs() for _idx,s in enumerate(self._feat_stride_fpn): _key = 'stride%s'%s stride = int(s) if self.use_landmarks: idx = _idx*3 else: idx = _idx*2 scores = net_out[idx].asnumpy() scores = scores[:, self._num_anchors['stride%s'%s]:, :, :] idx+=1 bbox_deltas = net_out[idx].asnumpy() height, width = bbox_deltas.shape[2], bbox_deltas.shape[3] A = self._num_anchors['stride%s'%s] K = height * width key = (height, width, stride) if key in self.anchor_plane_cache: anchors = self.anchor_plane_cache[key] else: anchors_fpn = self._anchors_fpn['stride%s'%s] anchors = anchors_plane(height, width, stride, anchors_fpn) anchors = anchors.reshape((K * A, 4)) if len(self.anchor_plane_cache)<100: self.anchor_plane_cache[key] = anchors scores = clip_pad(scores, (height, width)) scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1)) bbox_deltas = clip_pad(bbox_deltas, (height, width)) bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)) bbox_pred_len = bbox_deltas.shape[3]//A bbox_deltas = bbox_deltas.reshape((-1, bbox_pred_len)) proposals = bbox_pred(anchors, bbox_deltas) #proposals = clip_boxes(proposals, im_info[:2]) scores_ravel = scores.ravel() order = np.where(scores_ravel>=threshold)[0] proposals = proposals[order, :] scores = scores[order] proposals[:,0:4] /= scale proposals_list.append(proposals) scores_list.append(scores) if self.use_landmarks: idx+=1 landmark_deltas = net_out[idx].asnumpy() landmark_deltas = clip_pad(landmark_deltas, (height, width)) landmark_pred_len = landmark_deltas.shape[1]//A landmark_deltas = landmark_deltas.transpose((0, 2, 3, 1)).reshape((-1, 5, landmark_pred_len//5)) landmark_deltas *= self.landmark_std #print(landmark_deltas.shape, landmark_deltas) landmarks = landmark_pred(anchors, landmark_deltas) landmarks = landmarks[order, :] landmarks[:,:,0:2] /= scale landmarks_list.append(landmarks) proposals = np.vstack(proposals_list) landmarks = None if proposals.shape[0]==0: if self.use_landmarks: landmarks = np.zeros( (0,5,2) ) return np.zeros( (0,5) ), landmarks scores = np.vstack(scores_list) scores_ravel = scores.ravel() order = scores_ravel.argsort()[::-1] proposals = proposals[order, :] scores = scores[order] if self.use_landmarks: landmarks = np.vstack(landmarks_list) landmarks = landmarks[order].astype(np.float32, copy=False) pre_det = np.hstack((proposals[:,0:4], scores)).astype(np.float32, copy=False) keep = self.nms(pre_det) det = np.hstack( (pre_det, proposals[:,4:]) ) det = det[keep, :] if self.use_landmarks: landmarks = landmarks[keep] return det, landmarks
def main(): parser = argparse.ArgumentParser( description='Script to test the trained network on a game.') parser.add_argument('-r', '--rom', required=False, type=str, default=os.path.join('roms', 'breakout.bin'), help='Path of the ROM File.') parser.add_argument('-v', '--visualization', required=False, type=int, default=0, help='Visualize the runs.') parser.add_argument('--lr', required=False, type=float, default=0.01, help='Learning rate of the AdaGrad optimizer') parser.add_argument('--eps', required=False, type=float, default=0.01, help='Eps of the AdaGrad optimizer') parser.add_argument('--clip-gradient', required=False, type=float, default=None, help='Clip threshold of the AdaGrad optimizer') parser.add_argument('--double-q', required=False, type=bool, default=False, help='Use Double DQN') parser.add_argument('--wd', required=False, type=float, default=0.0, help='Weight of the L2 Regularizer') parser.add_argument( '-c', '--ctx', required=False, type=str, default='gpu', help='Running Context. E.g `-c gpu` or `-c gpu1` or `-c cpu`') parser.add_argument('-d', '--dir-path', required=False, type=str, default='', help='Saving directory of model files.') parser.add_argument( '--start-eps', required=False, type=float, default=1.0, help='Eps of the epsilon-greedy policy at the beginning') parser.add_argument('--replay-start-size', required=False, type=int, default=50000, help='The step that the training starts') parser.add_argument( '--kvstore-update-period', required=False, type=int, default=1, help='The period that the worker updates the parameters from the sever' ) parser.add_argument( '--kv-type', required=False, type=str, default=None, help= 'type of kvstore, default will not use kvstore, could also be dist_async' ) parser.add_argument('--optimizer', required=False, type=str, default="adagrad", help='type of optimizer') args = parser.parse_args() if args.dir_path == '': rom_name = os.path.splitext(os.path.basename(args.rom))[0] args.dir_path = 'dqn-%s-lr%g' % (rom_name, args.lr) replay_start_size = args.replay_start_size max_start_nullops = 30 replay_memory_size = 1000000 history_length = 4 rows = 84 cols = 84 ctx = parse_ctx(args.ctx) q_ctx = mx.Context(*ctx[0]) game = AtariGame(rom_path=args.rom, resize_mode='scale', replay_start_size=replay_start_size, resized_rows=rows, resized_cols=cols, max_null_op=max_start_nullops, replay_memory_size=replay_memory_size, display_screen=args.visualization, history_length=history_length) ##RUN NATURE freeze_interval = 10000 epoch_num = 100 steps_per_epoch = 250000 update_interval = 4 discount = 0.99 eps_start = args.start_eps eps_min = 0.1 eps_decay = (eps_start - eps_min) / 1000000 eps_curr = eps_start freeze_interval /= update_interval minibatch_size = 32 action_num = len(game.action_set) data_shapes = { 'data': (minibatch_size, history_length) + (rows, cols), 'dqn_action': (minibatch_size, ), 'dqn_reward': (minibatch_size, ) } dqn_sym = dqn_sym_nature(action_num) qnet = Base(data_shapes=data_shapes, sym_gen=dqn_sym, name='QNet', initializer=DQNInitializer(factor_type="in"), ctx=q_ctx) target_qnet = qnet.copy(name="TargetQNet", ctx=q_ctx) use_easgd = False optimizer = mx.optimizer.create(name=args.optimizer, learning_rate=args.lr, eps=args.eps, clip_gradient=args.clip_gradient, rescale_grad=1.0, wd=args.wd) updater = mx.optimizer.get_updater(optimizer) qnet.print_stat() target_qnet.print_stat() # Begin Playing Game training_steps = 0 total_steps = 0 for epoch in range(epoch_num): # Run Epoch steps_left = steps_per_epoch episode = 0 epoch_reward = 0 start = time.time() game.start() while steps_left > 0: # Running New Episode episode += 1 episode_loss = 0.0 episode_q_value = 0.0 episode_update_step = 0 episode_action_step = 0 time_episode_start = time.time() game.begin_episode(steps_left) while not game.episode_terminate: # 1. We need to choose a new action based on the current game status if game.state_enabled and game.replay_memory.sample_enabled: do_exploration = (npy_rng.rand() < eps_curr) eps_curr = max(eps_curr - eps_decay, eps_min) if do_exploration: action = npy_rng.randint(action_num) else: # TODO Here we can in fact play multiple gaming instances simultaneously and make actions for each # We can simply stack the current_state() of gaming instances and give prediction for all of them # We need to wait after calling calc_score(.), which makes the program slow # TODO Profiling the speed of this part! current_state = game.current_state() state = nd.array( current_state.reshape((1, ) + current_state.shape), ctx=q_ctx) / float(255.0) qval_npy = qnet.forward(is_train=False, data=state)[0].asnumpy() action = numpy.argmax(qval_npy) episode_q_value += qval_npy[0, action] episode_action_step += 1 else: action = npy_rng.randint(action_num) # 2. Play the game for a single mega-step (Inside the game, the action may be repeated for several times) game.play(action) total_steps += 1 # 3. Update our Q network if we can start sampling from the replay memory # Also, we update every `update_interval` if total_steps % update_interval == 0 and game.replay_memory.sample_enabled: # 3.1 Draw sample from the replay_memory training_steps += 1 episode_update_step += 1 states, actions, rewards, next_states, terminate_flags \ = game.replay_memory.sample(batch_size=minibatch_size) states = nd.array(states, ctx=q_ctx) / float(255.0) next_states = nd.array(next_states, ctx=q_ctx) / float(255.0) actions = nd.array(actions, ctx=q_ctx) rewards = nd.array(rewards, ctx=q_ctx) terminate_flags = nd.array(terminate_flags, ctx=q_ctx) # 3.2 Use the target network to compute the scores and # get the corresponding target rewards if not args.double_q: target_qval = target_qnet.forward(is_train=False, data=next_states)[0] target_rewards = rewards + nd.choose_element_0index(target_qval, nd.argmax_channel(target_qval))\ * (1.0 - terminate_flags) * discount else: target_qval = target_qnet.forward(is_train=False, data=next_states)[0] qval = qnet.forward(is_train=False, data=next_states)[0] target_rewards = rewards + nd.choose_element_0index(target_qval, nd.argmax_channel(qval))\ * (1.0 - terminate_flags) * discount outputs = qnet.forward(is_train=True, data=states, dqn_action=actions, dqn_reward=target_rewards) qnet.backward() qnet.update(updater=updater) # 3.3 Calculate Loss diff = nd.abs( nd.choose_element_0index(outputs[0], actions) - target_rewards) quadratic_part = nd.clip(diff, -1, 1) loss = 0.5 * nd.sum(nd.square(quadratic_part)).asnumpy()[0] +\ nd.sum(diff - quadratic_part).asnumpy()[0] episode_loss += loss # 3.3 Update the target network every freeze_interval if training_steps % freeze_interval == 0: qnet.copy_params_to(target_qnet) steps_left -= game.episode_step time_episode_end = time.time() # Update the statistics epoch_reward += game.episode_reward info_str = "Epoch:%d, Episode:%d, Steps Left:%d/%d, Reward:%f, fps:%f, Exploration:%f" \ % (epoch, episode, steps_left, steps_per_epoch, game.episode_reward, game.episode_step / (time_episode_end - time_episode_start), eps_curr) if episode_update_step > 0: info_str += ", Avg Loss:%f/%d" % ( episode_loss / episode_update_step, episode_update_step) if episode_action_step > 0: info_str += ", Avg Q Value:%f/%d" % ( episode_q_value / episode_action_step, episode_action_step) if episode % 100 == 0: logging.info(info_str) end = time.time() fps = steps_per_epoch / (end - start) qnet.save_params(dir_path=args.dir_path, epoch=epoch) logging.info("Epoch:%d, FPS:%f, Avg Reward: %f/%d" % (epoch, fps, epoch_reward / float(episode), episode))
def nonzero_1d(input): # TODO: fallback to numpy is unfortunate tmp = input.asnumpy() tmp = np.nonzero(tmp)[0] r = nd.array(tmp, ctx=input.context, dtype=tmp.dtype) return r
def pre_fftfilt(b, shape, nfft=None): (numsamples, numsamplepoints) = shape inputNoise = nd.random.randn(numsamples, numsamplepoints, ctx=mx.cpu()) b, x = b.reshape((-1, 1)), inputNoise.T m = x.shape[0] if m == 1: x = x.reshape((-1, 1)) # turn row into a column nx = x.shape[0] if min(b.shape) > 1: assert b.shape[1] == x.shape[ 1] and x.shape[1] <= 1, "signal:fftfilt:InvalidDimensions" else: b = b.reshape((-1, 1)) # make input a column nb = b.shape[0] if nfft == None: # figure out which nfft and L to use if (nb >= nx) or (nb > 2**20): # take a single FFT in this case nfft = int(2**round(np.log(nb + nx - 1) / np.log(2))) L = nx else: fftflops = nd.array([ 18, 59, 138, 303, 660, 1441, 3150, 6875, 14952, 32373, 69762, 149647, 319644, 680105, 1441974, 3047619, 6422736, 13500637, 28311786, 59244791, 59244791 * 2.09 ]) n = 2**nd.arange(1, 22, 1) validset_first = nd.argmax(n > nb - 1, axis=0).asscalar() n = nd.slice(n, begin=[ int(validset_first), ], end=(None, )) fftflops = nd.slice(fftflops, begin=[ int(validset_first), ], end=(None, )) # minimize (number of blocks) * (number of flops per fft) L = n - (nb - 1) temp = nd.ceil(nx / L) * fftflops dum, ind = nd.min(temp), nd.argmin(temp, axis=0) nfft = int(n[int(ind.asscalar())].asscalar()) L = int(L[int(ind.asscalar())].asscalar()) else: # nfft is given # Cast to enforce precision rules pass raise 'nfft is given?' ''' nfft = signal.internal.sigcasttofloat(nfft,'double','fftfilt','N','allownumeric'); if nfft < nb nfft = nb; end nfft = 2.^(ceil(log(nfft)/log(2))); % force this to a power of 2 for speed L = nfft - nb + 1; ''' # Check the input data type. Single precision is not supported. ''' try chkinputdatatype(b,x,nfft); catch ME throwAsCaller(ME); end''' return (b, m, nx, nb, L, nfft)
def getPolicy(self, inputs): assert mx.autograd.is_training() is False prob, _ = self.actorcritic(nd.array(inputs, ctx=CTX)) prob = prob.asnumpy() return prob
def get_dev(self, u, t, beta, user_meanday): mean_day = user_meanday[u] deviation = t - mean_day ans = 0 if deviation == 0 else deviation / math.fabs(deviation) * \ math.fabs(deviation) ** beta return nd.array([ans])