def get_flownet_feature(concat_inputs, trainable=True): with tf.variable_scope('FlowNetS'): with slim.arg_scope([slim.conv2d, slim.conv2d_transpose], # Only backprop this network if trainable trainable=trainable, # He (aka MSRA) weight initialization weights_initializer=slim.variance_scaling_initializer(), activation_fn=LeakyReLU, # We will do our own padding to match the original Caffe code padding='VALID'): weights_regularizer = slim.l2_regularizer(LONG_SCHEDULE['weight_decay']) with slim.arg_scope([slim.conv2d], weights_regularizer=weights_regularizer): with slim.arg_scope([slim.conv2d], stride=2): conv_1 = slim.conv2d(pad(concat_inputs, 3), 64, 7, scope='conv1') conv_2 = slim.conv2d(pad(conv_1, 2), 128, 5, scope='conv2') conv_3 = slim.conv2d(pad(conv_2, 2), 256, 5, scope='conv3') conv3_1 = slim.conv2d(pad(conv_3), 256, 3, scope='conv3_1') with slim.arg_scope([slim.conv2d], num_outputs=512, kernel_size=3): conv4 = slim.conv2d(pad(conv3_1), stride=2, scope='conv4') conv4_1 = slim.conv2d(pad(conv4), scope='conv4_1') conv5 = slim.conv2d(pad(conv4_1), stride=2, scope='conv5') conv5_1 = slim.conv2d(pad(conv5), scope='conv5_1') conv6 = slim.conv2d(pad(conv5_1), 1024, 3, stride=2, scope='conv6') conv6_1 = slim.conv2d(pad(conv6), 1024, 3, scope='conv6_1') shape = conv6_1.shape.as_list() conv6_1 = tf.reshape(conv6_1, [-1, shape[-3]*shape[-2]*shape[-1]]) return conv6_1
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ # Copy matches array, to avoid overwriting it orig_matches = matches.copy() matches = matches.copy() N = matches.shape[0] print(N) n_samples = int(N * 0.2) p_matched1 = pad(keypoints1[matches[:, 0]]) p_matched2 = pad(keypoints2[matches[:, 1]]) matched1 = keypoints1[matches[:, 0]] matched2 = keypoints2[matches[:, 1]] max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for i in range(n_iters): rand_indices = np.random.randint(0, N, (n_samples, )) rand_matched1 = matched1[rand_indices] rand_matched2 = matched2[rand_indices] H_est = fit_affine_matrix(rand_matched1, rand_matched2) inliers = np.sqrt( ((p_matched2.dot(H_est) - p_matched1)**2).sum(axis=-1)) < threshold curr_n_inliers = np.count_nonzero(inliers) if curr_n_inliers > n_inliers: n_inliers = curr_n_inliers max_inliers = inliers H = fit_affine_matrix(matched1[max_inliers], matched2[max_inliers]) ### END YOUR CODE print(H) return H, orig_matches[max_inliers]
def fit_affine_matrix(p1, p2): """ Fit affine matrix such that p2 * H = p1 Hint: You can use np.linalg.lstsq function to solve the problem. Args: p1: an array of shape (M, P) p2: an array of shape (M, P) Return: H: a matrix of shape (P * P) that transform p2 to p1. """ assert (p1.shape[0] == p2.shape[0]),\ 'Different number of points in p1 and p2' p1 = pad(p1) p2 = pad(p2) ##################################### # START YOUR CODE HERE # ##################################### H = np.linalg.lstsq(p2, p1, rcond=None)[0] ###################################### # END OF YOUR CODE # ###################################### # Sometimes numerical issues cause least-squares to produce the last # column which is not exactly [0, 0, 1] H[:, 2] = np.array([0, 0, 1]) return H
def fit_affine_matrix(p1, p2): """ Fit affine matrix such that p2 * H = p1 Hint: You can use np.linalg.lstsq function to solve the problem. Args: p1: an array of shape (M, P) p2: an array of shape (M, P) Return: H: a matrix of shape (P * P) that transform p2 to p1. """ assert (p1.shape[0] == p2.shape[0]),\ 'Different number of points in p1 and p2' p1 = pad(p1) p2 = pad(p2) ### YOUR CODE HERE H, _, _, _ = np.linalg.lstsq(p2, p1) ### END YOUR CODE H[:, 2] = np.array([0, 0, 1]) return H
def make_dnn_feats(fpath, noise_path, snr, P, maxlen=1339): speech = read(fpath) noise = read(noise_path) noise = pad_noise(speech, noise) blend = generate_noisy(speech, noise, snr) mel_clean = librosa.feature.melspectrogram(y=speech, sr=16000, n_fft=512, hop_length=256, n_mels=64) mel_noisy = librosa.feature.melspectrogram(y=blend, sr=16000, n_fft=512, hop_length=256, n_mels=64) feats = pad(window(mel_noisy, P), maxlen).T target = np.log(mel_clean) mask = pad(np.ones((target.shape[0], target.shape[1])), maxlen).T target = pad(target, maxlen).T return { 'x': torch.tensor(feats), 't': torch.tensor(target), 'mask': torch.tensor(mask) }
def fit_affine_matrix(p1, p2): """ Fit affine matrix such that p2 * H = p1 Hint: You can use np.linalg.lstsq function to solve the problem. Args: p1: an array of shape (M, P) p2: an array of shape (M, P) Return: H: a matrix of shape (P, P) that transform p2 to p1. """ assert (p1.shape[0] == p2.shape[0]),\ 'Different number of points in p1 and p2' p1 = pad(p1) p2 = pad(p2) ### YOUR CODE HERE #np.linalg.lstsq参考以下文档:https://numpy.org/devdocs/reference/generated/numpy.linalg.lstsq.html#numpy.linalg.lstsq H, r, rank, s = np.linalg.lstsq(p2, p1, rcond=None) pass ### END YOUR CODE # Sometimes numerical issues cause least-squares to produce the last # column which is not exactly [0, 0, 1] H[:, 2] = np.array([0, 0, 1]) return H
def _process(agent_skill, data): # TODO: this could be more efficient sub_skill_names = [None] + [ sub_skill.skill_name for sub_skill in agent_skill.sub_skills ] iput = np.asarray([ utils.pad(step.arg, agent_skill.skill_model.arg_in_len) + [step.cnt] + utils.one_hot(sub_skill_names.index(step.ret_name), agent_skill.skill_model.num_sub) + utils.pad(step.ret_val, agent_skill.skill_model.ret_in_len) for step in data ]) sub = np.asarray([sub_skill_names.index(step.sub_name) for step in data]) arg = np.asarray([ utils.pad(step.sub_arg, agent_skill.skill_model.arg_out_len) for step in data ]) return DictTree( len=len(data), iput=iput, oput=DictTree( sub=sub, arg=arg, ), )
def train(self, X, y, y_real, output_dir): X_split = [normalize(t.split()) for t in X] split_arrays = train_test_split(X_split, y, y_real, test_size=self.settings['test_size'], stratify=y_real) X_train, X_test, y_train, y_test, y_real_train, y_real_test = split_arrays text_field = data.Field() text_field.build_vocab(X_train, max_size=10000) # pad X_train_pad = [pad(s, self.settings['max_seq_length']) for s in tqdm(X_train, desc='pad')] X_test_pad = [pad(s, self.settings['max_seq_length']) for s in tqdm(X_test, desc='pad')] # to index X_train_index = [to_indexes(text_field.vocab, s) for s in tqdm(X_train_pad, desc='to index')] X_test_index = [to_indexes(text_field.vocab, s) for s in tqdm(X_test_pad, desc='to index')] train_dataset = self.to_dataset(X_train_index, y_train, y_real_train) val_dataset = self.to_dataset(X_test_index, y_test, y_real_test) model = self.model(text_field) model.to(device()) self.full_train(model, train_dataset, val_dataset, output_dir) torch.save(text_field, os.path.join(output_dir, self.vocab_name)) return model, text_field.vocab
def predict_turn(self, turn, ontology, args, threshold=0.5): model, tokenizer = self.bert, self.tokenizer batch_size = args.batch_size was_training = model.training self.eval() preds = [] examples = turn_to_examples(turn, ontology, tokenizer) for i in range(0, len(examples), batch_size): batch = examples[i:i + batch_size] slots, values, input_ids, token_type_ids, _ = list(zip(*batch)) # Padding and Convert to Torch Tensors input_ids, input_masks = pad(input_ids, args.device) token_type_ids = pad(token_type_ids, args.device)[0] # Forward Pass logits = model(input_ids, token_type_ids, input_masks) probs = torch.softmax(logits, dim=-1)[:, 1].cpu().data.numpy() # Update preds for j in range(len(batch)): if probs[j] >= threshold: preds.append((slots[j], values[j])) if was_training: self.train() return preds
def fit_affine_matrix(p1, p2): """ Fit affine matrix such that p2 * H = p1. First, pad the descriptor vectors with a 1 using pad() to convert to homogeneous coordinates, then return the least squares fit affine matrix in homogeneous coordinates. Hint: You can use np.linalg.lstsq function to solve the problem. Explicitly specify np.linalg.lstsq's new default parameter rcond=None to suppress deprecation warnings, and match the autograder. Args: p1: an array of shape (M, P) holding descriptors of size P about M keypoints p2: an array of shape (M, P) holding descriptors of size P about M keypoints Return: H: a matrix of shape (P+1, P+1) that transforms p2 to p1 in homogeneous coordinates """ assert (p1.shape[0] == p2.shape[0]),\ 'Different number of points in p1 and p2' p1 = pad(p1) p2 = pad(p2) ### YOUR CODE HERE pass ### END YOUR CODE # Sometimes numerical issues cause least-squares to produce the last # column which is not exactly [0, 0, 1] H[:, 2] = np.array([0, 0, 1]) return H
def _build_frange_part(start, stop, stride, zfill=0): """ Private method: builds a proper and padded :class:`fileseq.framerange.FrameRange` string. :type start: int :param start: first frame :type stop: int :param stop: last frame :type stride: int :param stride: increment :type zfill: int :param zfill: width for zero padding :rtype: str """ if stop is None: return '' pad_start = pad(start, zfill) pad_stop = pad(stop, zfill) if stride is None or start == stop: return '{0}'.format(pad_start) elif abs(stride) == 1: return '{0}-{1}'.format(pad_start, pad_stop) else: return '{0}-{1}x{2}'.format(pad_start, pad_stop, stride)
def cal_dev_loss(model, dev_data, batch_size, sent_vocab, tag_vocab, device): """ Calculate loss on the development data Args: model: the model being trained dev_data: development data batch_size: batch size sent_vocab: sentence vocab tag_vocab: tag vocab device: torch.device on which the model is trained Returns: the average loss on the dev data """ is_training = model.training model.eval() loss, n_sentences = 0, 0 with torch.no_grad(): for sentences, tags in utils.batch_iter(dev_data, batch_size, shuffle=False): sentences, sent_lengths = utils.pad(sentences, sent_vocab[sent_vocab.PAD], device) tags, _ = utils.pad(tags, tag_vocab[sent_vocab.PAD], device) batch_loss = model(sentences, tags, sent_lengths) # shape: (b,) loss += batch_loss.sum().item() n_sentences += len(sentences) model.train(is_training) return loss / n_sentences
def fit_affine_matrix(p1, p2): """ Fit affine matrix such that p2 * H = p1 Hint: You can use np.linalg.lstsq function to solve the problem. Args: p1: an array of shape (M, P) p2: an array of shape (M, P) Return: H: a matrix of shape (P * P) that transform p2 to p1. """ assert (p1.shape[0] == p2.shape[0]),\ 'Different number of points in p1 and p2' p1 = pad(p1) p2 = pad(p2) result = linalg.lstsq(p2, p1) #求解从p1变换到p2的最优变换矩阵 H = result[0] #变换矩阵X是一个3×3的矩阵 # Sometimes numerical issues cause least-squares to produce the last # column which is not exactly [0, 0, 1] H[:, 2] = np.array([0, 0, 1]) return H
def visualize(self, lookahead): source_energy = utils.pad(self.source_energy, lookahead) assigned_energy = utils.pad(self.assigned_energy, lookahead) # available_energy = source_energy - assigned_energy planned_energy = utils.pad(self.planned_energy, lookahead) consumed_energy = assigned_energy + planned_energy fig, ax = plt.subplots() ax.set_xlabel('ticks') ax.set_ylabel('energy') ax.set_ylim((-0.6, 1.25)) ax.plot(source_energy, color='green', label='available') ax.plot(assigned_energy, color='red', label='assigned') ax.plot(planned_energy, color='blue', label='planned') ax.plot(consumed_energy, color='black', label='consumed') for job in self.running_jobs: offset = 0 duration = len(job.profile) rect = patches.Rectangle((offset, -0.2-0.1*job.request_id), duration, 0.1, linewidth=1, edgecolor='red', facecolor='pink') ax.add_patch(rect) for request in self.waiting_requests: offset = self.plan[request.request_id] duration = len(request.profile) rect = patches.Rectangle((offset, -0.2-0.1*request.request_id), duration, 0.1, linewidth=1, edgecolor='blue', facecolor='lightblue') ax.add_patch(rect) ax.legend(loc='upper right') return fig
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ # Copy matches array, to avoid overwriting it orig_matches = matches.copy() matches = matches.copy() N = matches.shape[0] n_samples = int(N * 0.2) matched1 = keypoints1[matches[:, 0]] matched2 = keypoints2[matches[:, 1]] padded1 = pad(matched1) padded2 = pad(matched2) #max_inliers = np.zeros(N) max_inliers = [] n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for i in range(n_iters): m = [p for p in range(N)] np.random.shuffle(m) m = m[:n_samples] H = fit_affine_matrix(matched1[m], matched2[m]) ins = [] for j in range(N): trans = padded2[j] @ H trans = np.array([trans[0] / trans[2], trans[1] / trans[2]]) di = np.linalg.norm(trans - matched1[j]) if di < threshold: ins.append(j) if len(ins) > n_inliers: n_inliers = len(ins) max_inliers = ins H = fit_affine_matrix(matched1[max_inliers], matched2[max_inliers]) ### END YOUR CODE return H, orig_matches[max_inliers]
def __init__(self, dataframe, test=False): """ dataframe: pandas.DataFrame object containing the dataset test: boolean values describing whether or not this is the test set """ self.samples = [] # Will store the final data self.test = test # Whether or not the dataset is a test set # Convert the strings to token indices input1, input2 = get_tokenized(dataframe) # Pad data according to the longest sentence x1, x1_mask = pad(input1) x2, x2_mask = pad(input2) # Build the tuple structure to be used in training and testing for idx, _ in enumerate(tqdm(range(len(x1)), desc="Loading Data", leave=False)): sample = { "x1": x1[idx], "x1_mask": x1_mask[idx], "x2": x2[idx], "x2_mask": x2_mask[idx], } # The score feature is only available for test and dev sets if not self.test: sample["score"] = dataframe.iloc[idx].scores self.samples.append(sample)
def backward(self, previousGrad): X = self.input batch_size = np.shape(self.input)[-1] dX = np.zeros(np.shape(self.input)) self.grads = np.zeros(np.shape(self.parameters)) X_pad = utils.pad(X, self.pad_w, self.pad_h) dX_pad = utils.pad(dX, self.pad_w, self.pad_h) for h in range(self.n_H): for w in range(self.n_W): y_start = self.stride * h y_end = y_start + self.kernel_shape[1] x_start = self.stride * w x_end = x_start + self.kernel_shape[0] x_slice = X_pad[x_start:x_end, y_start:y_end, :, :] dX_pad[x_start:x_end, y_start:y_end, :, :] += np.dot( self.parameters[:, :, :, :], previousGrad[w, h, :, :]) self.gradient[:, :, :, :] += np.dot(x_slice, previousGrad[w, h, :, :].T) self.gradient = self.gradient / batch_size for parent in self.parents: parent.sonsVisited += 1 parent.backward(dX)
def make(cls, example, tokenizer, max_len): sys_utt_tokens, usr_utt_tokens = example.utt joint_sys_utt_label = np.array( [x for x, _, _ in example.label_dict.values()]).any(axis=0) joint_usr_utt_label = np.array( [x for _, x, _ in example.label_dict.values()]).any(axis=0) class_label_id_dict, start_pos_dict, end_pos_dict = {}, {}, {} for slot, (sys_utt_label, usr_utt_label, class_type) in example.label_dict.items(): sys_tokens, sys_token_labels = \ sub_tokenize(sys_utt_tokens, sys_utt_label, joint_sys_utt_label, tokenizer) usr_tokens, usr_token_labels = \ sub_tokenize(usr_utt_tokens, usr_utt_label, joint_usr_utt_label, tokenizer) # TO DO : truncate token_labels = utils.pad(max_len, 0, [0] + sys_token_labels + [0] + usr_token_labels) class_label_id_dict[slot] = class_types.index(class_type) start_pos_dict[slot], end_pos_dict[slot] = \ get_start_end_pos(class_type, token_labels, max_len) tokens = ['[CLS]'] + sys_tokens + ['[SEP]'] + usr_tokens + ['[SEP]'] input_ids = tokenizer.convert_tokens_to_ids(tokens) input_mask = [1] * len(input_ids) segment_ids = [0] * (2 + len(sys_tokens)) + [1] * (1 + len(usr_tokens)) utils.pad(max_len, 0, input_ids, input_mask, segment_ids) return cls(example.guid, tokens, input_ids, input_mask, segment_ids, start_pos_dict, end_pos_dict, class_label_id_dict)
def __init__(self, in_channel, hidden_size, dim): ''' :param in_channel: 表示输入的特征通道数 :param hidden_size: 表示隐藏层的神经元个数 :param dim: 表示光谱维的长度 ''' super().__init__() self.dim = dim m = list() padding_0 = pad(dim, 5, 3) m.append( nn.Conv3d(in_channel, hidden_size, (5, 3, 3), (3, 1, 1), padding=(padding_0, 0, 0))) m.append(nn.ReLU()) m.append(nn.BatchNorm3d(hidden_size)) dim_1 = (dim - 5 + 2 * padding_0) // 3 + 1 padding_1 = pad(dim_1, 5, 3) m.append( nn.Conv3d(hidden_size, hidden_size, (5, 3, 3), (3, 1, 1), padding=(padding_1, 0, 0))) m.append(nn.ReLU()) m.append(nn.BatchNorm3d(hidden_size)) # dim_2 = (dim_1 - 5 + 2 * padding_1) // 3 + 1 # padding_2 = pad(dim_2, 5, 3) # m.append(nn.Conv3d(hidden_size, hidden_size, (5, 3, 3), (3, 1, 1), padding=(padding_2, 0, 0))) # m.append(nn.ReLU()) # m.append(nn.BatchNorm3d(hidden_size)) self.out_dim = (dim_1 - 5 + 2 * padding_1) // 3 + 1 self.encoder = nn.Sequential(*m)
def __init__(self, out_channel, hidden_size, dim): super().__init__() padding_0 = pad(dim, 5, 3) dim_1 = (dim - 5 + 2 * padding_0) // 3 + 1 padding_1 = pad(dim_1, 5, 3) dim_2 = (dim_1 - 5 + 2 * padding_1) // 3 + 1 padding_2 = pad(dim_2, 5, 3) m = list() m.append( nn.ConvTranspose3d(hidden_size, hidden_size, (5, 3, 3), (3, 1, 1), padding=(padding_2, 0, 0))) m.append(nn.ReLU()) m.append(nn.BatchNorm3d(hidden_size)) m.append( nn.ConvTranspose3d(hidden_size, hidden_size, (5, 3, 3), (3, 1, 1), padding=(padding_1, 0, 0))) m.append(nn.ReLU()) m.append(nn.BatchNorm3d(hidden_size)) m.append( nn.ConvTranspose3d(hidden_size, out_channel, (5, 3, 3), (3, 1, 1), padding=(padding_0, 0, 0))) m.append(nn.ReLU()) m.append(nn.BatchNorm3d(out_channel)) self.decoder = nn.Sequential(*m)
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:, 0]]) matched2 = pad(keypoints2[matches[:, 1]]) max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for o in np.arange(n_iters): # step1: random_select_matches = matches[ np.random.choice(np.arange(N), n_samples, replace=False), :] # step2: p1 = keypoints1[random_select_matches[:, 0]] p2 = keypoints2[random_select_matches[:, 1]] H = fit_affine_matrix(p1, p2) # step3: diff = matched2.dot(H) - matched1 temp_inliers = np.linalg.norm(diff, axis=1)**2 < threshold temp_n_inliers = temp_inliers.sum() # step4: if temp_n_inliers > n_inliers: n_inliers = temp_n_inliers max_inliers = temp_inliers max_matches = matches[max_inliers, :] p1 = keypoints1[max_matches[:, 0]] p2 = keypoints2[max_matches[:, 1]] H = fit_affine_matrix(p1, p2) ### END YOUR CODE return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:,0]]) matched2 = pad(keypoints2[matches[:,1]]) max_inliers = [] n_inliers = 0 # RANSAC iteration start H = None for n in range(n_iters): matches_indices = np.random.randint(N, size = n_samples) p1 = keypoints1[matches[matches_indices,0]] p2 = keypoints2[matches[matches_indices,1]] affine = fit_affine_matrix(p1, p2) dot_temp = np.dot(matched2, affine) temp_inliers = [] for i in range(N): point_trans = dot_temp[i] point_orig = matched1[i] distance = np.linalg.norm(point_trans - point_orig) if distance < threshold: temp_inliers.append(i) if len(temp_inliers) > len(max_inliers): max_inliers = temp_inliers H = affine return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:, 0]]) matched2 = pad(keypoints2[matches[:, 1]]) max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE t = 0 while t < n_iters: nn_inliers = 0 index_sample = [random.randint(0, N - 1) for _ in range(n_samples)] p1 = matched1[index_sample, :] p2 = matched2[index_sample, :] H = np.linalg.lstsq(p2, p1)[0] H[:, 2] = np.array([0, 0, 1]) test1 = np.dot(matched2, H) temp = (test1 - matched1)**2 distance = np.sqrt(temp[:, 0] + temp[:, 1]) print(distance) for i in range(distance.shape[0]): if distance[i] <= threshold: nn_inliers += 1 if nn_inliers > n_inliers: n_inliers = nn_inliers max_inliers = np.where(distance <= threshold)[0] t += 1 p1 = matched1[max_inliers] p2 = matched2[max_inliers] H = np.linalg.lstsq(p2, p1)[0] H[:, 2] = np.array([0, 0, 1]) pass ### END YOUR CODE return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ # Copy matches array, to avoid overwriting it orig_matches = matches.copy() matches = matches.copy() N = matches.shape[0] print(N) n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:, 0]]) matched2 = pad(keypoints2[matches[:, 1]]) max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for i in range(n_iters): temp_max = np.zeros(N, dtype=np.int32) temp_n = 0 idx = np.random.choice(N, n_samples, replace=False) p1 = matched1[idx, :] p2 = matched2[idx, :] H = np.linalg.lstsq(p2, p1, rcond=None)[0] H[:, 2] = np.array([0, 0, 1]) temp_max = np.linalg.norm(matched2.dot(H) - matched1, axis=1)**2 < threshold temp_n = np.sum(temp_max) if temp_n > n_inliers: max_inliers = temp_max.copy() n_inliers = temp_n H = np.linalg.lstsq(matched2[max_inliers], matched1[max_inliers], rcond=None)[0] H[:, 2] = np.array([0, 0, 1]) ### END YOUR CODE print(H) return H, orig_matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:, 0]]) matched2 = pad(keypoints2[matches[:, 1]]) max_inliers = np.zeros(N) n_inliers = 0 H = None # RANSAC iteration start ### YOUR CODE HERE pass for i in range(0, n_iters): #1. Select random set of matches random_indices = random.sample(list(range(N)), n_samples) chosen_kp1 = matched1[random_indices] chosen_kp2 = matched2[random_indices] #2. Compute affine transformation matrix affine_transformation_matrix = np.linalg.lstsq(chosen_kp2, chosen_kp1)[0] affine_transformation_matrix[:, 2] = [0, 0, 1] #3. Compute number of inlier real_matched1 = np.dot(matched2, affine_transformation_matrix) diff = real_matched1 - matched1 dists = np.linalg.norm(diff, axis=1) curr_inliers = len(np.argwhere(dists < threshold)) curr_max_inliers = np.where(dists < threshold) #4. Keep the largest set of inliers if curr_inliers > n_inliers: n_inliers = curr_inliers H = affine_transformation_matrix max_inliers = curr_max_inliers #5. Re-compute least-squares estimate on all of the inliers H = np.linalg.lstsq(matched2[max_inliers], matched1[max_inliers])[0] H[:, 2] = np.array([0, 0, 1]) ### END YOUR CODE return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] choices = list(range(N)) n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:,0]]) matched2 = pad(keypoints2[matches[:,1]]) max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for i in range(n_iters): # 1 select random set of matches sample = np.random.choice(choices, n_samples, replace=False) # 2 fitting the the set of p1 to p2 p1 = matched1[sample,:] p2 = matched2[sample,:] #fitt the matrix mat= np.linalg.lstsq(p2, p1)[0] #compute the image of rest of the point image_matched1= matched2.dot(mat) # compute the number of inlinier dists = np.linalg.norm(image_matched1-matched1,axis=1) inliers= (dists < threshold) if(inliers.sum()> n_inliers): n_inliers = inliers.sum() H= mat max_inliers=inliers ### END YOUR CODE return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation: 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers via Euclidean distance 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Update max_inliers as a boolean array where True represents the keypoint at this index is an inlier, while False represents that it is not an inlier. Hint: You can use np.linalg.lstsq function to solve the problem. Explicitly specify np.linalg.lstsq's new default parameter rcond=None to suppress deprecation warnings, and match the autograder. You can compute elementwise boolean operations between two numpy arrays, and use boolean arrays to select array elements by index: https://numpy.org/doc/stable/reference/arrays.indexing.html#boolean-array-indexing Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ # Copy matches array, to avoid overwriting it orig_matches = matches.copy() matches = matches.copy() N = matches.shape[0] print(N) n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:,0]]) matched2 = pad(keypoints2[matches[:,1]]) max_inliers = np.zeros(N, dtype=bool) n_inliers = 0 # RANSAC iteration start # Note: while there're many ways to do random sampling, please use `np.random.shuffle()` # followed by slicing out the first `n_samples` matches here in order to align with the auto-grader. ### YOUR CODE HERE pass ### END YOUR CODE print(H) return H, orig_matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:,0]]) matched2 = pad(keypoints2[matches[:,1]]) max_inliers = np.zeros(N, dtype=bool) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE allIdxs = np.arange(N) for iterTime in range(n_iters): np.random.shuffle(allIdxs) sampleIdxs = allIdxs[0:n_samples] outIdxs = allIdxs[n_samples:] sample_p1s = matched1[sampleIdxs] sample_p2s = matched2[sampleIdxs] out_p1s = matched1[outIdxs] out_p2s = matched2[outIdxs] tempH = np.linalg.lstsq(sample_p2s,sample_p1s)[0] tempH[:,2] = np.array([0, 0, 1]) errors = np.sum( (np.dot(out_p2s,tempH) - out_p1s)**2 ,axis = 1) otherInliers = outIdxs[errors < threshold] totalInliers = len(otherInliers) + n_samples if totalInliers > n_inliers: max_inliers[:] = False n_inliers = totalInliers recomputeIdxs = np.concatenate( (sampleIdxs,otherInliers) ) max_inliers[recomputeIdxs] = True recompute_p1s = matched1[recomputeIdxs] recompute_p2s = matched2[recomputeIdxs] H = np.linalg.lstsq(recompute_p2s,recompute_p1s)[0] H[:,2] = np.array([0, 0, 1]) ### END YOUR CODE return H, matches[max_inliers]
def ransac(keypoints1, keypoints2, matches, n_iters=200, threshold=20): """ Use RANSAC to find a robust affine transformation 1. Select random set of matches 2. Compute affine transformation matrix 3. Compute inliers 4. Keep the largest set of inliers 5. Re-compute least-squares estimate on all of the inliers Args: keypoints1: M1 x 2 matrix, each row is a point keypoints2: M2 x 2 matrix, each row is a point matches: N x 2 matrix, each row represents a match [index of keypoint1, index of keypoint 2] n_iters: the number of iterations RANSAC will run threshold: the number of threshold to find inliers Returns: H: a robust estimation of affine transformation from keypoints2 to keypoints 1 """ N = matches.shape[0] n_samples = int(N * 0.2) matched1 = pad(keypoints1[matches[:, 0]]) matched2 = pad(keypoints2[matches[:, 1]]) max_inliers = np.zeros(N) n_inliers = 0 # RANSAC iteration start ### YOUR CODE HERE for i in range(n_iters): inliersArr = np.zeros(N) idx = np.random.choice(N, n_samples, replace=False) p1 = matched1[idx, :] p2 = matched2[idx, :] H, residuals, rank, s = np.linalg.lstsq(p2, p1, rcond=None) H[:, 2] = np.array([0, 0, 1]) output = np.dot(matched2, H) inliersArr = np.linalg.norm(output - matched1, axis=1)**2 < threshold inliersCount = np.sum(inliersArr) if inliersCount > n_inliers: max_inliers = inliersArr.copy() #还是注意深拷贝和浅拷贝啊 n_inliers = inliersCount # 迭代完成,拿最大数目的匹配点对进行估计变换矩阵 H, residuals, rank, s = np.linalg.lstsq(matched2[max_inliers], matched1[max_inliers], rcond=None) H[:, 2] = np.array([0, 0, 1]) ### END YOUR CODE return H, matches[max_inliers]
def __getitem__(self, idx): img_path = self.img_paths[idx] mask_path = self.mask_paths[idx] image = imread(img_path) mask = imread(mask_path) image = image.astype('float32') / 255 if 'train' in img_path: mask = (mask>65535/2).astype('float32') else: if self.args.loss == 'LovaszHingeLoss': mask = (mask>127).astype('float32') else: mask = mask.astype('float32') / 255 if not self.args.pad: image = cv2.resize(image, (self.args.img_size, self.args.img_size)) mask = cv2.resize(mask, (self.args.img_size, self.args.img_size)) else: image = pad(image, self.args.img_size) mask = pad(mask, self.args.img_size) if self.aug: if self.args.aug == 1: image, mask = random_fliplr(image, mask) elif self.args.aug == 2: image, mask = random_fliplr(image, mask) image = random_erase(image) elif self.args.aug == 3: image, mask = random_fliplr(image, mask) image, mask = random_shift(image, mask, wrg=0.1, hrg=0.1, fill_mode='nearest') image = random_erase(image) if 'Res' in self.args.arch: means = [0.485, 0.456, 0.406] stds = [0.229, 0.224, 0.225] for i in range(3): image[:,:,i] = (image[:,:,i] - means[i]) / stds[i] if 'Inception' in self.args.arch: means = [0.5, 0.5, 0.5] stds = [0.5, 0.5, 0.5] for i in range(3): image[:,:,i] = (image[:,:,i] - means[i]) / stds[i] if self.args.depth: image = depth_encode(image) if self.args.coord_conv: image = coord_conv(image) image = image.transpose((2, 0, 1)) mask = mask[:,:,np.newaxis] mask = mask.transpose((2, 0, 1)) return image, mask
def _do_pad(match): """ Substitutes padded for unpadded frames. """ result = list(match.groups()) result[1] = pad(result[1], zfill) if result[4]: result[4] = pad(result[4], zfill) return ''.join((i for i in result if i))
def framesToFrameRange(frames, sort=True, zfill=0, compress=False): """ Converts an iterator of frames into a :class:`fileseq.framerange.FrameRange`. :type frames: iterable :param frames: sequence of frames to process :type sort: bool :param sort: sort the sequence before processing :type zfill: int :param zfill: width for zero padding :type compress: bool :param compress: remove any duplicates before processing :rtype: str """ if compress: frames = unique(set(), frames) frames = list(frames) if not frames: return '' if len(frames) == 1: return pad(frames[0], zfill) if sort: frames.sort() return ','.join(FrameSet.framesToFrameRanges(frames, zfill))
def guess(self, words): text = ' '.join(words) text = utils.pad(text, self.maxlen) if self.invert: text = text[::-1] input_vec = self.input_codec.encode(text) preds = self.model.predict_classes(np.array([input_vec]), verbose=0) return self.codec.decode(preds[0], calc_argmax=False).strip('\x00')
def print_raid(used_devices): output = [[' Raid '], []] raids = get_raids() for raid_name, raid in raids.iteritems(): used_devices.update({'/dev/' + n: 'R' for n in raid['disks']}) output.append([raid_name, '[' + raid['personality'] + ']', str(raid['status']['non_degraded_disks']) + '/' + str(raid['status']['raid_disks'])]) for d in raid['disks']: output.append([' ' + '/dev/' + d]) return pad(output)
def print_mounts(mounts): output = [' Mounts ', ''] mnts = [] for fs_name, mount in sorted(mounts.iteritems(), key=lambda k: k[0]): mnts.append([fs_name, mount['fs_type'], mount['total'], mount['used']]) mnts.append([' ' + mount['mount_point']]) output += pad(mnts) return output
def infer(data_dir, model_name, sentence=None): """ """ # Load components with open(os.path.join(basedir, data_dir, 'char2index.json'), 'r') as f: char2index = json.load(f) with open(os.path.join(basedir, data_dir, 'index2class.json'), 'r') as f: index2class = json.load(f) # Enter the sentence print ("Classes:", index2class.values()) if not sentence: sentence = input("Please enter the sentence: ") # Normalize the sentece sentence = normalize_string(sentence) # Convert sentence(s) to indexes input_ = convert_sentence( sentence=sentence, char2index=char2index, ) # Convert to model input input_, _ = pad( inputs=[input_], char2index=char2index, max_length=len(input_), ) # Convert to Variable X = Variable(torch.FloatTensor(input_), requires_grad=False) # Load the model model = torch.load(os.path.join( basedir, "data", data_dir.split("/")[-1], model_name)) # Feed through model model.eval() scores = model(X) probabilities = F.softmax(scores) # Sorted probabilities sorted_, indices = torch.sort(probabilities, descending=True) for index in indices[0]: print ("%s - %i%%" % ( index2class[str(index.data[0])], 100.0*probabilities.data[0][index.data[0]]))
def print_devices(used_devices): output = [' Devices ', ''] for dev in sorted(parted.getAllDevices(), key=lambda x: x.path): output.append(dev.path + ' ' + dev.model + ' ' + human_number(dev.getSize(), 'MiB')) try: partitions = [] disk = parted.Disk(dev) for partition in disk.partitions: used = ' ' + used_devices.get(partition.path, '') + ' ' partitions.append([used, partition.path, human_number(partition.getLength() * partition.geometry.device.sectorSize)]) if partitions: output += pad(partitions) except parted.DiskLabelException: pass return output
def print_lvm(mounts, used_devices): output = [' LVM ', ''] lvm = LVM() for vg in sorted(lvm.vgscan(), key=lambda x: x.name): output.append(' '.join([vg.name, human_number(vg.size('KiB'), 'KiB'), '(' + str(int((1 - vg.free_size() / vg.size()) * 100)) + '%)'])) vs = [] for pv in sorted(vg.pvscan(), key=lambda x: x.name): name = pv.name used_devices[name] = 'L' vs.append([' P ', name, human_number(pv.size('KiB'), 'KiB'), '(' + str(int((1 - pv.free() / pv.size()) * 100)) + '%)']) for lv in sorted(vg.lvscan(), key=lambda x: x.name): used = mounts.get('/dev/mapper/' + vg.name + '-' + lv.name, {'used': ''})['used'] vs.append([' L ', lv.name, human_number(lv.size('KiB'), 'KiB'), used]) output += pad(vs) return output