def test_real_dataset_for_measuring_X_matrix_conformance(self): arff_data = self.generate_arff("""1,2,3,Arial,Bonito,1 7,8,9,Deustrech,Arial,1 10,11,12,Bonito,Arial,1 4,5,6,Bonito,Deustrech,1""") X = np.ones(np.array(arff_data['data']).shape) y = np.ones(X.shape[0]) arguments = { 'X': X, 'y': y, 'attributes': arff_data['attributes'], 'data': np.array(arff_data['data']) } result = self.extractor.execute(arguments) self.assertEqual(y.shape[0], result['X'].shape[0])
def train(self, X, y, learning_rate = 0.01, update_size = None): assert len(np.shape(X)) == 2, "Invalid input shape. The input must be 2d (batch, input_dim)" batch_size, input_dim = np.shape(X) assert self.input_dim == input_dim, "Unmatch input_dim." X = np.column_stack((X, np.ones((batch_size, 1)))) y = y.reshape((-1, self.output_dim)) if self.updateMethod == "closed-form": # check whether the matrix can be inversed assert np.linalg.det(X) != 0, "This matrix cann't be inversed." # update the parameters self.w = np.linalg.inv(np.dot(X.T, X)) self.w = np.dot(self.w, X.T) self.w = np.dot(self.w, y) # predict under the train set Y_predict = np.dot(X, self.w) # calculate the absolute differences loss_train = np.average(np.abs(Y_predict - y)) return Y_predict, loss_train
def apply_pytesseract(image): """Convert the image to black and white and apply pytesseract to get the text""" # load the image #image = cv2.imread(input_image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) kernel = np.ones((1, 1), np.uint8) gray = cv2.dilate(gray, kernel, iterations=1) gray = cv2.erode(gray, kernel, iterations=1) gray_thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] gray_blur = cv2.medianBlur(gray, 3) # store grayscale image as a temp file to apply OCR filename = "Screens/{}blur.png".format(os.getpid()) cv2.imwrite(filename, gray_blur) # load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file text = pytesseract.image_to_string(Image.open(filename)) os.remove(filename) if not text: filename = "Screens/{}thresh.png".format(os.getpid()) cv2.imwrite(filename, gray_thresh) # load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file text = pytesseract.image_to_string(Image.open(filename)) os.remove(filename) return text
def main(argv): #for i in range(4): imageName = 'image0.png' src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR) # Load an image # Check if image is loaded fine if src is None: print('Error opening image') return -1 print("ne") laplacian(src, imageName) gaussian(src, imageName) sobel(src,imageName) img = cv.imread('input.jpg') # cv.imshow('Original', img) size = 15 # generating the kernel kernel_motion_blur = np.zeros((size, size)) kernel_motion_blur[int((size - 1) / 2), :] = np.ones(size) kernel_motion_blur = kernel_motion_blur / size # applying the kernel to the input image output = cv.filter2D(img, -1, kernel_motion_blur) cv.imshow('Motion Blur', output) cv.waitKey(0) return 0
def predict(self, X): assert len(np.shape(X)) == 2, "Invalid input shape. The input must be 2d (batch, input_dim)" batch_size, input_dim = np.shape(X) assert self.input_dim == input_dim, "Unmatch input_dim." X = np.column_stack((X, np.ones((batch_size, 1)))) y_predict = np.dot(X, self.w) return y_predict
def gradAscent(dataMatIn, classLabels): dataMatrix = np.mat(dataMatIn) labelMat = np.mat(classLabels).transpose() m, n = np.shape(dataMatrix) alpha = 0.001 maxCycles = 500 weights = np.ones((n, 1)) for k in range(maxCycles): h = sigmoid(dataMatrix * weights) error = labelMat - h weights = weights + alpha * dataMatrix.transpose() * error return weights.getA()
def transform(pt, center, scale, rot, res, invert): # For managing coordinate transformations between the original image space # and the heatmap pt_ = np.ones(3) pt_[1] = pt[1] pt_[2] = pt[2] t = getTransform(center, scale, rot, res) if invert: t = np.inverse(t) new_point = (t*pt_)[0:2].astype(int) return new_point
def execute(self, arff_data): attributes = [attribute[0] for attribute in arff_data['attributes']] dataset = arff_data['data'] X = (arff_data['X'].T.tolist() if 'X' in arff_data else []) base_height = np.array(dataset[:, attributes.index('baseHeight')], dtype='float64') target_height = np.array(dataset[:, attributes.index('targetHeight')], dtype='float64') base_width = np.array(dataset[:, attributes.index('baseWidth')], dtype='float64') target_width = np.array(dataset[:, attributes.index('targetWidth')], dtype='float64') X.append( np.minimum(base_height * base_width, target_height * target_width)) base_x = np.array(dataset[:, attributes.index('baseX')], dtype='float64') target_x = np.array(dataset[:, attributes.index('targetX')], dtype='float64') base_y = np.array(dataset[:, attributes.index('baseY')], dtype='float64') target_y = np.array(dataset[:, attributes.index('targetY')], dtype='float64') X.append( np.sqrt( np.power(np.abs(base_x - target_x), 2) + np.power(np.abs(base_y - target_y), 2))) X.append( np.abs((base_height * base_width) - (target_height * target_width)) / np.maximum( np.minimum(base_height * base_width, target_height * target_width), np.ones(len(base_height)))) X.append(dataset[:, attributes.index('chiSquared')]) arff_data['X'] = np.array(X, dtype='float64').T prev_features = (arff_data['features'] if 'features' in arff_data else []) arff_data['features'] = prev_features + [ 'area', 'displacement', 'sdr', 'chisquared' ] arff_data['y'] = np.array( arff_data['data'][:, attributes.index(self._class_attr)], dtype='int16') return arff_data
def execute(self, arff_dataset): prev_features = (arff_dataset['features'] if 'features' in arff_dataset else []) arff_dataset['features'] = prev_features + [ 'diff_height', 'diff_width', 'diff_x', 'diff_y', 'missmatch', 'correlation', 'base_bin1', 'base_bin2', 'base_bin3', 'base_bin4', 'base_bin5', 'base_bin6', 'base_bin7', 'base_bin8', 'base_bin9', 'base_bin10' ] X_t = (arff_dataset['X'].T.tolist() if 'X' in arff_dataset else []) attributes = [attr[0] for attr in arff_dataset['attributes']] data = arff_dataset['data'] X_t.append( np.array(data[:, attributes.index('baseHeight')], dtype='float64')) X_t.append( np.array(data[:, attributes.index('baseWidth')], dtype='float64')) X_t.append( np.array(data[:, attributes.index('baseX')], dtype='float64')) X_t.append( np.array(data[:, attributes.index('baseY')], dtype='float64')) X_t.append(np.ones(len(data))) X_t.append(np.array(data[:, attributes.index('ncc')], dtype='float64')) X_t.append(np.array(data[:, attributes.index('base_bin1')])) X_t.append(np.array(data[:, attributes.index('base_bin2')])) X_t.append(np.array(data[:, attributes.index('base_bin3')])) X_t.append(np.array(data[:, attributes.index('base_bin4')])) X_t.append(np.array(data[:, attributes.index('base_bin5')])) X_t.append(np.array(data[:, attributes.index('base_bin6')])) X_t.append(np.array(data[:, attributes.index('base_bin7')])) X_t.append(np.array(data[:, attributes.index('base_bin8')])) X_t.append(np.array(data[:, attributes.index('base_bin9')])) X_t.append(np.array(data[:, attributes.index('base_bin10')])) (PlatformExtractor()).execute(arff_dataset, attributes, X_t) arff_dataset['X'] = np.array(X_t, dtype='float64').T arff_dataset['y'] = np.array(data[:, attributes.index(self._class_attr)], dtype='float64') return arff_dataset
#img = cv2.equalizeHist(img) #img = cv2.Laplacian(img, cv2.CV_8UC1, ksize=5) #img = cv2.Sobel(img, cv2.CV_8U, 1, 1, ksize=5) #bk, img = cv2.threshold(img, 20, 80, cv2.THRESH_BINARY) #konwertujemy fotke na odcienie szarosci img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #cv2.imwrite("toGrey.png",img) #sprawdzamy wspolczynnik bieli i dostosowujemy thresha plus dla bieli negatyw if (wspolczynnikBieli > 75): ret, thresh = cv2.threshold(img, 110, 255, 0) thresh = 255 - thresh else: ret, thresh = cv2.threshold(img, 55, 255, 0) kernel = np.ones((5, 5), np.uint8) thresh = cv2.erode(thresh, kernel, iterations=1) im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contourList = [] tempCont = [] tempRadius = [] tempX = [] tempY = [] tempMaxRadius = [] sredniDist = [] #sprawdzamy czy namierzone obiekty to kola #liczymy momenty aby uzyskac srodek ciezkosci for i in range(len(contours)):
for i in range(len(data)): premise = data[i]['sentence1'].split(' ') premise.extend(["<PAD>" for i in range(15 - len(premise))]) hypothesis = data[i]['sentence2'].split(' ') hypothesis.extend(["<PAD>" for i in range(15 - len(hypothesis))]) labels = data[i]['gold_label'] premise_embedding = np.zeros((15, emb_dim)) hypothesis_embedding = np.zeros((15, emb_dim)) for j, word in enumerate(premise): try: premise_embedding[j] = glove[word] except KeyError: premise_embedding[j] = np.ones(emb_dim) * np.random.sample() premise_embedding_array.append(torch.from_numpy(premise_embedding)) for j, word in enumerate(hypothesis): try: hypothesis_embedding[j] = glove[word] except KeyError: hypothesis_embedding[j] = np.ones(emb_dim) * np.random.sample() hypothesis_embedding_array.append(torch.from_numpy(hypothesis_embedding)) for i in range(len(test_data)): premise = data[i]['sentence1'].split(' ') premise.extend(["<PAD>" for i in range(15 - len(premise))])
import np import matplotlib.pyplot as plt from matplotlib.pyplot import * N = 15 # make an empty data set data = np.ones((N, N)) * np.nan # fill in some fake data for j in range(3)[::-1]: data[N//2 - j : N//2 + j +1, N//2 - j : N//2 + j +1] = j # make a figure + axes fig, ax = plt.subplots(1, 1, tight_layout=True) # make color map my_cmap = matplotlib.colors.ListedColormap(['r', 'g', 'b']) # set the 'bad' values (nan) to be white and transparent my_cmap.set_bad(color='w', alpha=0) # draw the grid for x in range(N + 1): ax.axhline(x, lw=2, color='k', zorder=5) ax.axvline(x, lw=2, color='k', zorder=5) # draw the boxes ax.imshow(data, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N], zorder=0) # turn off the axis labels ax.axis('off')
def contrast_demo(self, img): # 亮度就是每个像素所有通道都加上b rows, cols, chunnel = img.shape blank = np.ones([rows, cols, chunnel], img.dtype) # np.zeros(img1.shape, dtype=uint8) return_img = cv.addWeighted(img, 1.3, blank, 0.5, 3) return return_img
import np as np import numpy as np from torch.autograd import Variable from matplotlib import pyplot as plt size = 150 x: np = np.random.randint(0, 70, size) yd: np = np.array(3 * x - 5 + 5 * np.random.randn(size)) x = np.concatenate((x[:, np.newaxis], np.ones((x.shape[0], 1))), axis=1) yd = yd[:, np.newaxis] learning_rate = 0.001 def sigmoid(x): # 應用sigmoid啟用函式 return 1 / (1 + np.exp(-x)) def sigmoid_derivative(x): # 計算Sigmoid函式的偏導數 return x * (1 - x) def train(training_inputs, yd, training_iterations): w = np.random.randn(2, 1) # w = np.array([[3.0],[1.0]]) for iteration in range(training_iterations): # 得到輸出 y = sigmoid(np.dot(training_inputs, w)) # 計算誤差 error = yd - y
f = open(path, 'r') bugs = list(filter(lambda x: len(x) > 0, f.read().split('\n'))) f.close() bugs = [json.loads(bug)['DESCRIPTION'].lower() for bug in bugs] return bugs severity0_path = './%s/class0.txt' % (sys.argv[1]) severity1_path = './%s/class1.txt' % (sys.argv[1]) bugs0 = load_json(severity0_path) bugs1 = load_json(severity1_path) f = TextFilter(language='english') X = f.transform(bugs0 + bugs1) y = np.zeros(len(bugs0)).tolist() + np.ones(len(bugs1)).tolist() feature_selection = None if sys.argv[2] == 'nothing': feature_selection = TfidfVectorizer(stop_words='english') if sys.argv[2] == 'uses': feature_selection = FeatureSearch(strategy=USES(), random_state=42) if sys.argv[2] == 'usesplus': feature_selection = FeatureSearch(strategy=USESPlus(), random_state=42) pipe = Pipeline([('feature extraction/selection', feature_selection), ('classifier', DecisionTreeClassifier())]) skf = StratifiedKFold(n_splits=10, random_state=42, shuffle=True) results = cross_validate(pipe, X,