def train_composition(dataset, transformation_list): """ Train a model on dataset on which a sequence of transformations applied :param dataset: the original dataset :param transformation_list: the sequence of transformations :return: """ # Apply a sequence of transformations (X_train, Y_train), (X_test, Y_test) = load_data(dataset) X_train = transform(X_train, transformation_list) nb_examples, img_rows, img_cols, nb_channels = X_train.shape nb_classes = Y_train.shape[1] input_shape = (img_rows, img_cols, nb_channels) # Train a model and save model_name = 'model-{}-cnn-{}'.format(dataset, 'composition') require_preprocess = (dataset == DATA.cifar_10) model = models.create_model(dataset, input_shape, nb_classes) models.train(model, X_train, Y_train, model_name, require_preprocess) # save to disk models.save_model(model, model_name) # evaluate the new model loaded_model = models.load_model(model_name) X_test = transform(X_test, transformation_list) if require_preprocess: X_test = normalize(X_test) scores = loaded_model.evaluate(X_test, Y_test, verbose=2) print('*** Evaluating the new model: {}'.format(scores)) del loaded_model
def train_model(dataset, transform_type): """ Train specific model on given dataset. :param dataset: :param transform_type: """ print('Training model ({}) on {}...'.format(transform_type, dataset)) (X_train, Y_train), (X_test, Y_test) = load_data(dataset) nb_examples, img_rows, img_cols, nb_channels = X_train.shape nb_classes = Y_train.shape[1] input_shape = (img_rows, img_cols, nb_channels) X_train = transform(X_train, transform_type) model_name = 'model-{}-cnn-{}'.format(dataset, transform_type) require_preprocess = False if (dataset == DATA.cifar_10): require_preprocess = True # train model = models.create_model(dataset, input_shape, nb_classes) models.train(model, X_train, Y_train, model_name, require_preprocess) # save to disk models.save_model(model, model_name) # evaluate the new model X_test = transform(X_test, transform_type) loaded_model = models.load_model(model_name) scores = loaded_model.evaluate(X_test, Y_test, verbose=2) print('*** Evaluating the new model: {}'.format(scores)) del loaded_model
def reset(X, trans_type): if trans_type == TRANSFORMATION.rotate90: X = transform(X, TRANSFORMATION.rotate270) elif trans_type == TRANSFORMATION.rotate270: X = transform(X, TRANSFORMATION.rotate90) elif trans_type == TRANSFORMATION.rotate180: X = transform(X, TRANSFORMATION.rotate180) return X
def deconversion5(obs, az, h): try: RA = [] DEC = [] for i in range(len(az)): a = transform((az[i], h[i]), 'horizon', 'equatorial', obs) RA.append(a[0]) DEC.append(a[1]) return np.array([RA, DEC]) except: a = transform((az, h), 'horizon', 'equatorial', obs) return np.array([a[0], a[1]])
def prediction(data, models, nClasses, transformationList): ''' input: data: nSamples X <Sample Dimension> models: a list of classification models output: prediction matrix M - nWeakModels X nSamples X nClasses. ''' nSamples, nWeakModels = data.shape[0], len(models) rawPred = np.zeros((nWeakModels, nSamples, nClasses)) transTCs = [] predTCs = [] for mIdx in range(nWeakModels): testData = data.copy() # some transformation will change the data. startTime = time.time() transformationType = transformationList[mIdx] testData = transform(testData, transformationType) transTCs.append(time.time()-startTime) startTime = time.time() rawPred[mIdx] = models[mIdx].predict(testData) predTCs.append(time.time() - startTime) return rawPred, transTCs, predTCs
def train_models_with_newLabels( dataset_name, AE_type_tag, defense_tag, transform_type, num_of_samples, X, Y, validation_rate=0.2, need_argument=False): print('Training model ({}) on {} {} new labels collected from ensemble ({}) built upon {}...'.format(transform_type, num_of_samples, dataset_name, defense_tag, AE_type_tag)) if transform_type != TRANSFORMATION.clean: # transform images on demand. X = transform(X, transform_type) model_name = 'model-{}-cnn-{}-{}-{}-{}'.format( dataset_name, transform_type, AE_type_tag, defense_tag, num_of_samples) models.train_and_save( model_name, X, Y, validation_rate, need_argument)
def training(args, model, train_dl): nb_datapoints = train_dl.shape[0] index = np.arange(nb_datapoints) image_index = open('{}/index.txt'.format(args.result_dir), 'a+') for epoch in range(1, args.epoch + 1): #shuffle the training set before generating batches print('Shuffling training set...') np.random.shuffle(index) #devide training set into batches print('Training epoch{}...'.format(epoch)) logging.info('Training epoch{}...'.format(epoch)) nb_batch = int(math.ceil(len(train_dl) / args.batchsize)) for batch in range(nb_batch): image_index.write('E{} B{}:'.format(epoch, batch + 1)) index_batch = index[batch * args.batchsize:(batch + 1) * args.batchsize] for i in index_batch: image_index.write(' {}'.format(i)) train_batch = [train_dl[i] for i in index_batch] images_batch, joints_batch = transform(args, train_batch) for i in range(args.times_per_batch): loss, acc = model.train_on_batch(images_batch, joints_batch) print('E{} B{} Iter{}: loss:{}'.format(epoch, batch + 1, i + 1, loss)) logging.info('E{} B{} Iter{}: loss:{}'.format( epoch, batch + 1, i + 1, loss)) image_index.write('\n') image_index.write('\n') image_index.close()
def isItalic(im): im_s = transform(im, alpha*np.pi/180) p, p_s = project(im), project(im_s) width = 1 fig = plt.figure() a=fig.add_subplot(1,4,1) plt.bar(range(len(p)), p, width, color="blue") a.set_title('Original') a=fig.add_subplot(1,4,2) plt.imshow(im) a=fig.add_subplot(1,4,3) plt.bar(range(len(p)), p_s, width, color="blue") a.set_title('Slanted') a=fig.add_subplot(1,4,4) plt.imshow(im_s) plt.show() b,l,t,r = fit_contours(im) b_s, l_s, t_s, r_s = fit_contours(im_s) width = -l + r + 1 width_s = -l_s + r_s +1 print width, width_s if width < width_s: return False elif width > width_s: return True else: # print "Width unchanged" p, p_s = project(im), project(im_s) if max(p_s) > max(p): return False return True
def conversion5(obs, dec, ra): """ AZALT=np.array(map(lambda x,y : transform((x,y),'equatorial','horizon',obs),np.array(ra),np.array(dec))) AZ = AZALT[:,0] ALT = AZALT[:,1] return np.array([AZ,ALT]) """ try: AZ = [] ALT = [] for i in range(len(dec)): a = transform((ra[i], dec[i]), 'equatorial', 'horizon', obs) AZ.append(a[0]) ALT.append(a[1]) return np.array([AZ, ALT]) except: a = transform((ra, dec), 'equatorial', 'horizon', obs) return np.array([a[0], a[1]])
def test_model(model, test_data, transformation_type=TRANSFORMATION.clean): X, Y = test_data print('Transforming test data set...') X = transform(X, transformation_type) print('Testing model [{}]...'.format(transformation_type)) models.evaluate_model(model, X, Y) del X, Y
def plotLine(x0, y0, x1, y1, window, viewport, win, color): # Take care of vertical line separately and # rest other cases using reflection if (x1 - x0 == 0): slope = None (x0, y0) = (y0, x0) (x1, y1) = (y1, x1) else: slope = (y1 - y0) / (x1 - x0) if (slope >= 0) and (abs(slope) > 1): (x0, y0) = (y0, x0) (x1, y1) = (y1, x1) elif (slope < 0) and (abs(slope) <= 1): (x0, y0) = (-x0, y0) (x1, y1) = (-x1, y1) elif (slope < 0) and (abs(slope) > 1): (x0, y0) = (y0, -x0) (x1, y1) = (y1, -x1) if (x0 > x1): (xt, yt) = (x0, y0) (x0, y0) = (x1, y1) (x1, y1) = (xt, yt) # Now the actual algorithm with those assumptions dy = y1 - y0 dx = x1 - x0 a = dy b = -dx delta_E = a delta_NE = a + b # Initialize d (decision parameter) d = a + (b / 2) x = x0 y = y0 print("Slope =", slope) while (x <= x1): if (slope is None): (xp, yp) = (y, x) elif (slope >= 0) and (abs(slope) > 1): (xp, yp) = (y, x) elif (slope < 0) and (abs(slope) <= 1): (xp, yp) = (-x, y) elif (slope < 0) and (abs(slope) > 1): (xp, yp) = (-y, x) else: (xp, yp) = (x, y) pt = transform(xp, yp, window, viewport) win.plotPixel(pt.getX(), pt.getY(), color) if (d < 0): # Choose East d = d + delta_E else: d = d + delta_NE y = y + 1 x = x + 1
def run(fname_coordinates, fname_forces, fname_zmatrix): coordinates = [] with open(fname_coordinates, 'r') as f: line = f.readline() while line: num_atoms = int(line) line = f.readline() line = f.readline() step = [] for _ in range(num_atoms): _, *xs = line.split() xs = [float(x) for x in xs] step.append(xs) line = f.readline() coordinates.append(step) coordinates = np.array(coordinates) forces = [] with open(fname_forces, 'r') as f: line = f.readline() while line: while line and "# Atom Kind Element" not in f.readline(): True line = f.readline() step = [] while line and "SUM OF ATOMIC FORCES" not in line: _, _, _, *xs = line.split() xs = [float(x) for x in xs] step.append(xs) line = f.readline() line = f.readline() forces.append(step) forces = np.array(forces) zmatrix = [] with open(fname_zmatrix, 'r') as f: for line in f.readlines(): type, ids = line.split() ids = [int(x) - 1 for x in ids.split(',')] zmatrix.append((type, ids)) out_file = Path(fname_forces.stem + "-out" + fname_forces.suffix) with open(out_file, 'w') as f: for i, (x, dEdx) in enumerate(zip(coordinates, forces)): new_forces = transform(x, dEdx, zmatrix) line = f"i = {i}\n" for ic, force in zip(zmatrix, new_forces): line += f"{ic[0]:<8}{force}\n" line += "\n" f.write(line) return out_file
def polyFill(edges,win): for i in range(len(edges)): tmp = list(edges[i]) if(tmp[1] > tmp[3]): (tmp[1],tmp[3]) = (tmp[3],tmp[1]) (tmp[0],tmp[2]) = (tmp[2],tmp[0]) edges[i] = tuple(tmp) y_max = edges[0][3] ; y_min = edges[0][1] for i in range(len(edges)): if(edges[i][3] > y_max): y_max = edges[i][3] if(edges[i][1] < y_min): y_min = edges[i][1] no_of_scan_lines = y_max-y_min+1 edge_table = list() for i in range(no_of_scan_lines): edge_table.append([]) for i in range(len(edges)): tmp = edges[i] if(tmp[1] != tmp[3]): one_by_m = (tmp[2]-tmp[0])/(tmp[3]-tmp[1]) # y_max, x_of_ymin, 1/m tt1 = tuple([tmp[3],tmp[0],one_by_m]) edge_table[tmp[1]-y_min].append(tt1) # Yaha pe sorting na bhi kare toh chalega shayad AET = list() ; y = y_min while(y <= y_max): tmp = edge_table[y-y_min] for i in tmp: AET.append(i) tmp = len(AET) ; i = 0 while(i<tmp): if(AET[i][0] == y): del AET[i] tmp=len(AET) else: i=i+1 AET.sort(key=lambda x:x[1],reverse=False) i = 0 while(i <= len(AET)-2): x1 = int(math.ceil(AET[i][1])) # Round Up x2 = int(math.floor(AET[i+1][1])) # Round Down for x in range(x1,x2+1): pt = transform(xw=x,yw=y,window=window,viewport=viewport) win.plotPixel(pt.getX(),pt.getY(),color_rgb(5,56,107)) i=i+2 y=y+1 for i in range(len(AET)): tmp = list(AET[i]) tmp[1] = tmp[1] + tmp[2] AET[i] = tuple(tmp) return 0
def lambda_handler(event, context): dfNYT = pd.read_csv(nytURL) dfJH = pd.read_csv(johnHopkinsURL, usecols=['Date', 'Country/Region', 'Recovered']) try: dfFinal = transformation.transform(dfNYT, dfJH) except Exception as e: notify("Transform function raised exception because {}", format(e)) exit(1) conn = database_connection() cur = conn.cursor() data = [] cur.execute("""SELECT to_regclass('etl')""") query_results = cur.fetchall() if query_results[0][0] == None: try: query = """CREATE TABLE etl (reportdate date PRIMARY KEY, cases integer, deaths integer, recovered integer)""" cur.execute(query) except Exception as e: notify( "Exception raised while creation of table because {}".format( e)) exit(1) try: query, data = first_insert(dfFinal, data) cur.execute(query, data) except Exception as e: notify( "Couldn't complete first time data insertion in the table because {}" .format(e)) exit(1) notify("Table is created and data insertion is done") else: cur.execute("""SELECT max(reportdate) from etl""") query_results = cur.fetchall() diff = max(dfFinal['date']).date() - query_results[0][0] if diff.days > 0: try: query, data = everyday_insert(dfFinal, data, diff.days) cur.execute(query, data) except Exception as e: notify("Daily insertion of data is unsuccessful because {}". format(e)) exit(1) notify("Today " + str(diff.days) + " rows updated") else: notify("Data is not updated yet") conn.commit()
def ransac(n, dist, kp1, kp2, pixel_room=2, inlier_coverage_threshold=0.8): """ :param n: Type of transformation, 1 for translation, 2 for Translation and Rotation, 3 for Affine and 4 for Projective :param dist: Ratio of euclidean distance, key points of first and second image, and descriptors of first and second image :param kp1, kp2: key points of image1 and image2 :param pixel_room: integer value for pixel room error :param inlier_coverage_threshold: value between 0 to 1 giving the coverage of inliers :return: final_inliers and projection matrix and flag 1 if projection matrix is computed else 0 """ final_inliers = [] final_proj = [] count = 0 for x in range(1000): kp_sub_x = [] kp_sub_y = [] for i in range(n): key = list(dist.keys())[random.randrange(0, len(dist))] kp_sub_x.append(int(kp1[key[0]].pt[0])) kp_sub_y.append(int(kp1[key[0]].pt[1])) kp_sub_x.append(int(kp2[key[1]].pt[0])) kp_sub_y.append(int(kp2[key[1]].pt[1])) proj, flag = transform(n, kp_sub_x, kp_sub_y) if flag == 1: count += 1 inliers = [] for i in list(dist.keys()): error = distance(kp1[i[0]].pt[0], kp1[i[0]].pt[1], kp2[i[1]].pt[0], kp2[i[1]].pt[1], proj) if error < pixel_room: inliers.append( (int(kp1[i[0]].pt[0]), int(kp1[i[0]].pt[1]), int(kp2[i[1]].pt[0]), int(kp2[i[1]].pt[1]))) if len(inliers) > (len(final_inliers)): final_inliers = copy.deepcopy(inliers) final_proj = copy.deepcopy(proj) if len(final_inliers) > len(dist) * inlier_coverage_threshold: break if count > 0: return final_inliers, final_proj, 1 else: return final_inliers, final_proj, 0
def train_model(data, transformation_type=TRANSFORMATION.clean): X, Y = data print('Transforming training data set [{}]...'.format(transformation_type)) X = transform(X, transformation_type) model_name = 'model-{}-cnn-{}'.format(DATA.CUR_DATASET_NAME, transformation_type) model = models.create_model(DATA.CUR_DATASET_NAME) print('Training model [{}]...'.format(model_name)) model = models.train(model, X, Y, model_name) print('Saving model...') models.save_model(model, model_name) print('Done.') return model
def test_model(args, test_dl): json_structure = '%s/model_structure.json' % args.modeldir weights = '%s/model_weights.h5' % args.modeldir print('Loading model...') model = load_model(args, json_structure, weights) print('Model loaded!') log_fn = '%s/test_log.txt' % args.modeldir logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', filename=log_fn, level=logging.DEBUG) logging.info(args) nb_batch = int(math.ceil(len(test_dl) / args.batchsize)) sum_loss = 0 prediction_file = open('%s/predict_joints.csv' % args.modeldir, 'w') scale_groundtruth_file = open( '%s/scale_groundtruth_joints.csv' % args.modeldir, 'w') for batch in range(nb_batch): test_batch = test_dl[batch * args.batchsize:(batch + 1) * args.batchsize] test_images_batch, test_joints_batch = transform(args, test_batch) loss, acc = model.test_on_batch(test_images_batch, test_joints_batch) print('B{}: loss:{}'.format(batch + 1, loss)) logging.info('B{}: loss:{}'.format(batch + 1, loss)) sum_loss += loss predict_joints = model.predict_on_batch(test_images_batch) for i, test_datapoint in enumerate(test_batch): filename = test_datapoint.split(',')[0] msg = '{},{}\n'.format( filename, ','.join([str(j) for j in predict_joints[i].tolist()])) gt_msg = '{},{}\n'.format( filename, ','.join([str(j) for j in test_joints_batch[i].tolist()])) prediction_file.write(msg) scale_groundtruth_file.write(gt_msg) prediction_file.close() scale_groundtruth_file.close() avg_loss = sum_loss / nb_batch print('Average loss:{}'.format(avg_loss)) logging.info('Average loss:{}'.format(avg_loss))
def lambda_handler(event, context): cases = pd.read_csv(casesURL, header=None) deaths = pd.read_csv(deathsURL, header=None) recovered = pd.read_csv(recoveredURL, header=None) try: finalDF = transformation.transform(cases,deaths,recovered) except Exception as e: notify("Transform function raised exception because {}",format(e)) exit(1) conn = database_connection() cur = conn.cursor() data = [] cur.execute("""SELECT to_regclass('etl')""") query_results = cur.fetchall() if query_results[0][0]==None: try: query = """CREATE TABLE etl (reportdate date PRIMARY KEY, cases integer, deaths integer, recovered integer)""" cur.execute(query) except Exception as e: notify("Exception raised while creation of table because {}".format(e)) exit(1) try: query,data = first_insert(finalDF,data) cur.execute(query,data) except Exception as e: notify("Couldn't complete first time data insertion in the table because {}".format(e)) exit(1) notify("Table is created and data insertion is done") else: cur.execute("""SELECT max(reportdate) from etl""") query_results = cur.fetchall() diff = max(finalDF['date']).date()-query_results[0][0] if diff.days>0: try: query,data = everyday_insert(finalDF,data,diff.days) cur.execute(query,data) except Exception as e: notify("Daily insertion of data is unsuccessful because {}".format(e)) exit(1) notify("Today "+str(diff.days)+" rows updated") else: notify("Data is not updated yet") conn.commit() print("end lambda function")
def eval_single_model(model_name, testset_name, labels_name): """ Evaluate model on test set :param model_name: :param testset_name: :return: """ prefix, dataset, architect, trans_type = model_name.split('-') X_test = np.load('{}/{}.npy'.format(PATH.ADVERSARIAL_FILE, testset_name)) labels = np.load('{}/{}.npy'.format(PATH.ADVERSARIAL_FILE, labels_name)) if 'composition' in trans_type: trans_type = TRANSFORMATION.get_transformation_compositions() print(type(trans_type), trans_type) # apply transformation(s) X_test = transform(X_test, trans_type) # evaluate each of the composition if 'composition' in trans_type: for trans in trans_type: print(type(trans), trans) m_name = '{}-{}-{}-{}'.format(prefix, dataset, architect, trans) model = models.load_model(m_name) print('*** Evaluating ({}) on ({})...'.format( m_name, testset_name)) scores = model.evaluate(X_test, labels, verbose=2) print(scores) del model # evaluate the model model = models.load_model(model_name) if (dataset == DATA.cifar_10): X_test = normalize(X_test) print('*** Evaluating ({}) on ({})...'.format(model_name, testset_name)) scores = model.evaluate(X_test, labels, verbose=2) print(scores) return scores
# ako approx_curve da 4 vrha, mozemo biti sigurni da je to nas trazeni objekt if len(approx_curve) == 4: best_contour = approx_curve break # print len(best_contour) # nacrtaj objekt na temelju nadjene konture, zelenom bojom cv2.drawContours(image, [best_contour], -1, (0, 255, 0), 2) # cv2.imshow("Outline", image) # cv2.imwrite("outline.jpg", image) # cv2.waitKey(0) # cv2.destroyAllWindows() # sada je potrebno podignuti sliku, koja je pod nekim kutem != 90 stupnjeva u top-down perspektivu warped_image = transform(orig, best_contour.reshape(4, 2) * ratio) # print "SHAPE: ", warped_image.shape # grey_warped_image = cv2.cvtColor(warped_image, cv2.COLOR_BGR2GRAY) # black_and_white = threshold_adaptive(grey_warped_image, 250, offset=10) # napravi binarnu sliku, crno-bijelu # black_and_white = black_and_white.astype("uint8") * 255 # cv2.imshow("Original", imutils.resize(orig, height=350)) warped_image = imutils.resize(warped_image, width=250, height=350) # cv2.imwrite("blobcount.jpg", warped_image) # print "SHAPE 2: ", warped_image.shape # cv2.waitKey(0) # cv2.destroyAllWindows() # warped_image = warped_image.astype("uint8") * 255 # cv2.imshow("Warped perspective", imutils.resize(warped_image, height=350)) # cv2.waitKey(0) # cv2.destroyAllWindows() type = detect_type(warped_image) # 1 -> numericka, 2 -> royal
#!/usr/bin/env python # -*- coding: utf-8 -*- # # model.py import pickle import pandas as pd import numpy as np from Cluster import clust from transformation import transform X=transform() #numpy array containing input vectors for each ride y=clust() #Labels for each ride(Cluster under which it exists n_examples = len(X) nninput_dim =2 #26 nnoutput_dim =2 #2072 learn_rate = 0.01 r_lam= 0.01 def calculate_loss(model): W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2'] zzz1 = X.dot(W1) + b1 aaa1 = np.tanh(zzz1) zzz2 = aa1.dot(W2) + b2 expo_scor = np.exp(zzz2) probabs = expo_scor / np.sum(expo_scor, axis=1, keepdims=True) correct_logprobabs = -np.log(probabs[range(n_examples), y]) dataloss = np.sum(correct_logprobabs) dataloss += r_lam/2 * (np.sum(np.square(W1)) + np.sum(np.square(W2))) return 1./n_examples * dataloss def predict(model, x): W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2'] zzz1 = x.dot(W1) + b1 aaa1 = np.tanh(zzz1)
while picker.connection.printing: time.sleep(0.1) picker.lightOn('white') image = snapImage(5) picker.lightOff('white') pl.figure() pl.imshow(image) pl.show() #objects = analyzer.process_image(image) positions = numpy.array([[916,80],[586,702],[685,969]]) #positions = numpy.array(analyzer.get_positions(objects)) pos = transformation.transform(positions) #pl.figure() #pl.imshow(objects) #pl.show() picker.home() for i in range(len(pos)): picker.pickNextBead() while picker.connection.printing: time.sleep(0.1) picker.pickColony(pos[i]) while picker.connection.printing: time.sleep(0.1) picker.innoculateNextWell()
def main(): # Open the video capture = cv2.VideoCapture('big_files/final.mp4') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') # if the output mp4 already exists, delete it try: os.remove('outputs/output.mp4') except: pass # create a new output mp4 video = cv2.VideoWriter('outputs/output.mp4', fourcc, 30.0, size) fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=100, detectShadows=False) detector = cv2.SimpleBlobDetector_create() # background image we're doing right background = cv2.imread('big_files/background.png', 0) # background = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY) # open transformation calibration checkerboard image checkerboard_image = cv2.imread('betterCheckb.png') # calculate transformation matrix transformation_matrix, _ = transform(checkerboard_image) transformed_background = cv2.warpPerspective(background, transformation_matrix, (2000, 2000)) # draw lane lines on background for l in LANE_LINES: cv2.line(transformed_background, (l, 0), (l, 2000), (0, 0, 0), 3) # keep a cache of the previous frame centers transformed_previous_frame_centers = [] raw_previous_frame_centers = [] frame_count = 0 # preview settings bird_eye_preview = True blob_preview = False # loop through frames of video while True: # capture current frame in video ret, img = capture.read() if ret == True: if frame_count % FRAMES_FOR_SPEED == 0: # birds-eye if bird_eye_preview: transformed_output = transformed_background.copy() imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # transformed_image = transform(imgray) # use the background subtractor fgbg.apply(background) fgmask = fgbg.apply(imgray) # Pre processing, which includes blurring the image and thresholding threshold = 10 fgmask = cv2.GaussianBlur(fgmask, (29, 29), 0) ret, thresh = cv2.threshold(fgmask, threshold, 255, cv2.THRESH_BINARY) if blob_preview: cv2.imshow('blobs', thresh) # Get the contours for the thresholded image im2, cnts, hierarchy = cv2.findContours( thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) blob_area_threshold = 700 # minimum size of blob in order to be considered a vehicle raw_current_frame_centers = [ ] # will contain a list of the centroids of moving vehicles on raw footage transformed_current_frame_centers = [] # loop over the contours for c in cnts: area = cv2.contourArea(c) # getting blob area to threshold # compute the center of the contour if area > blob_area_threshold: # import ipdb; ipdb.set_trace() M = cv2.moments(c) # prevent divide by zer0 if M["m00"] != 0.0: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) centers_xy_coordinates = (cX, cY) transformed_xy_coordinates = transformToBirdsEye( [centers_xy_coordinates], transformation_matrix) # import ipdb; ipdb.set_trace() tX, tY = transformed_xy_coordinates[0][0] if tX >= LANE_LINES[0] and tX <= LANE_LINES[ -1] and tY > 100 and tY < 1900: raw_current_frame_centers.append( centers_xy_coordinates) transformed_current_frame_centers.append( [tX, tY]) # draw the contour and center of the shape on the image cv2.drawContours(img, [c], -1, (0, 0, 204), 2) cv2.circle(img, (cX, cY), 7, (0, 0, 204), -1) transformed_current_frame_centers = np.array( [transformed_current_frame_centers]) # birds-eye if bird_eye_preview and len( transformed_current_frame_centers) > 0: for x, y in transformed_current_frame_centers[0]: cv2.circle(transformed_output, (int(x), int(y)), 10, (0, 0, 0), -1) car_map = match_centers_across_frames( [raw_current_frame_centers], [raw_previous_frame_centers], transformed_current_frame_centers, transformed_previous_frame_centers ) # need to return velocities of vehicles (speed + direction) # put velocities on the original image for key in car_map: if car_map[key] == None: continue raw_center, transformed_center, speed, raw_parametrized_direction, transformed_parametrized_direction = car_map[ key] r_cX, r_cY = raw_center r_Dx, r_Dy = raw_parametrized_direction t_cX, t_cY = transformed_center t_Dx, t_Dy = transformed_parametrized_direction if speed != float('inf'): cv2.putText(img, "{0} mph".format(round(speed)), (r_cX - 20, r_cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 100), 2) cv2.arrowedLine(img, (r_cX, r_cY), (int(r_cX + r_Dx), int(r_cY + r_Dy)), (0, 0, 100), 2) # birds-eye if bird_eye_preview: cv2.arrowedLine( transformed_output, (int(t_cX), int(t_cY)), (int(t_cX + t_Dx), int(t_cY + t_Dy)), (0, 0, 0), 2) transformed_previous_frame_centers = transformed_current_frame_centers raw_previous_frame_centers = raw_current_frame_centers cv2.imshow("original footage with blob/centroid", img) # birds-eye if bird_eye_preview: cv2.imshow('birds-eye', transformed_output) frame_count += 1 if (cv2.waitKey(27) != -1): break capture.release() video.release() cv2.destroyAllWindows()
def generate_single(sess, x, y, attacker=ATTACK.FGSM, candidates=None, attack_count=None, max_perturb=get_perturb_upperbound(), strategy=ATTACK_STRATEGY.RANDOM.value): # candidate_names = candidates.copy() candidate_names = copy.deepcopy(list(candidates.keys())) fooled = [] attack_params = get_attack_params(attacker) x_adv = x perturbed_norm = measure.frobenius_norm(x_adv, x) max_iteration = len(candidate_names) iter = 0 while ((len(fooled) < attack_count) and (iter < max_iteration)): # generate adversarial example for target model print('ITERATION {}: candidates/fooled ::: {}/{}'.format( iter, len(candidate_names), len(fooled))) iter += 1 target_name = pick_target_model(candidate_names, strategy) transformation = target_name.split('.')[0].split('-')[-1] x_trans = transform(x_adv, transformation) if len(x_trans.shape) < 4: print('x_trans shape:', x_trans.shape) x_trans = np.expand_dims(x_trans, axis=0) x_tmp = attack_single(sess, candidates[target_name], attacker, x_trans, y, **attack_params) perturbed_norm = measure.frobenius_norm(x_tmp, transform(x, transformation)) if perturbed_norm >= max_perturb: # keep the last x_adv if current one is out of the boundary print('out of perturbed boundary, stop.') break x_adv = reset(x_tmp, transformation) if MODE.DEBUG: plot_image(x_adv[0], transformation) del x_trans # filter out candidates that are fooled by x_adv true_label = np.argmax(y) for cand_name in candidate_names: transformation = cand_name.split('.')[0].split('-')[-1] # apply transformation x_trans = transform(x_adv, transformation) pred_label = np.argmax(candidates[cand_name].predict(x_trans)) if MODE.DEBUG: print('prediction: [{}/{}/{}]'.format(transformation, true_label, pred_label)) if (true_label != pred_label): # remove candidate being fooled by x_adv candidate_names.remove(cand_name) # record only the name of the weak defense print('+++ fooled [{}]'.format(cand_name)) fooled.append(cand_name) # release del x_trans # use current adversarial example as the input of next iteration print('') del target_name return x_adv[0]
picker.lightOn('white') image = snapImage(5) picker.lightOff('white') pl.figure() pl.imshow(image) pl.show() objects = analyzer.process_image(image) positions = numpy.array(analyzer.get_positions(objects)) params = (2.113487776997974, 0.073996640890684487, numpy.array([684.0, 487.71428571]), numpy.array([-49.02857143, 32.65714286])) pos = transformation.transform(positions, params) pl.figure() pl.imshow(objects) pl.show() picker.home() for i in range(len(objects)): picker.pickNextBead() while picker.connection.printing: time.sleep(0.1) picker.pickColony(pos[i]) while picker.connection.printing: time.sleep(0.1) picker.innoculateNextWell()
from transformation import transform trvb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/binary/training_vectors_"#training vector base trlb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/binary/training_labels_"#training label base ttb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/transactions/training_transactions_"#training trainsactions base tevb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/binary/test_vectors_" telb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/binary/test_labels_" tetb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm2/14/transactions/test_transactions_" for i in range(0,14): transform(tevb+str(i)+".csv",telb+str(i)+".csv", tetb+str(i))
from transformation import transform trvb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/training_vectors.csv"#training vector base trlb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/training_labels.csv"#training label base ttb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/training_transactions"#training trainsactions base tevb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/test_vectors.csv" telb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/test_labels.csv" tetb="/Users/mengqizhou/Desktop/datamining/assignment5/algorithm1/test_transactions" transform(tevb,telb, tetb) transform(trvb,trlb, ttb)
output_file.close() elif sys.argv[1] == 'part2': coord_len = int(sys.argv[2]) target_img = cv2.imread(sys.argv[3]) source_img = cv2.imread(sys.argv[4]) output_img = sys.argv[5] coord_list = sys.argv[6:] x_coord = [] y_coord = [] for coord in coord_list: x, y = coord.split(',') x_coord.append(int(x)) y_coord.append(int(y)) t_mat, flag = transform(coord_len, x_coord, y_coord) if flag != 0: new_img = bilinear_interpolation(source_img, np.linalg.inv(t_mat), target_img)[0] cv2.imwrite(output_img, new_img) else: print('error in finding the transformation matrix!') elif sys.argv[1] == 'part3': target_img = cv2.imread(sys.argv[2], cv2.IMREAD_GRAYSCALE) source_img = cv2.imread(sys.argv[3], cv2.IMREAD_GRAYSCALE) output_img = sys.argv[4] threshold = 0.8 orb = cv2.ORB_create(nfeatures=500)
textcoords='offset points', ha='right', va='bottom', arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) c = plt.scatter(ys, xs, color='r') c.set_alpha(0.25) plt.axis("off") plt.title('Colony Selection from Plate: ' + plate_name + ', at ' + datestr) plt.show() # Offer exit dialog imageOK = tkMessageBox.askyesno( 'Option', 'Pick Colonies?') # Dialog box: Do you want to continue? print "here" print imageOK if not (imageOK): sys.exit() for i in range(5): picker.pickNextBead() xpos = (scipy.ndimage.center_of_mass(picture, colonies[0], sel[-1 - i])[0]) ypos = (scipy.ndimage.center_of_mass(picture, colonies[0], sel[-1 - i])[1]) print xpos, ypos pos = transformation.transform([[xpos, ypos]])[0] print xpos, ypos, pos picker.pickColony(pos) picker.innoculateNextWell() picker.home()
def craft(dataset, gen_test=True, method=ATTACK.FGSM, trans_type=TRANSFORMATION.clean): print('loading original images...') if gen_test: # generate for test set _, (X, Y) = load_data(dataset) prefix = 'test' else: # generate for train set (the last 20% of the original train set) (X, Y), _ = load_data(dataset) nb_trainings = int(X.shape[0] * 0.8) X = X[nb_trainings:] Y = Y[nb_trainings:] prefix = 'val' """ In debugging mode, crafting for 50 samples. """ if MODE.DEBUG: X = X[:30] Y = Y[:30] X = transform(X, trans_type) model_name = 'model-{}-cnn-{}'.format(dataset, trans_type) if method == ATTACK.FGSM: for eps in ATTACK.get_fgsm_eps(): print('{}: (eps={})'.format(method.upper(), eps)) X_adv, _ = get_adversarial_examples(model_name, method, X, Y, eps=eps) attack_params = 'eps{}'.format(int(1000 * eps)) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.BIM: for ord in ATTACK.get_bim_norm(): for nb_iter in ATTACK.get_bim_nbIter(): for eps in ATTACK.get_bim_eps(ord): print('{}: (ord={}, nb_iter={}, eps={})'.format( method.upper(), ord, nb_iter, eps)) X_adv, _ = get_adversarial_examples(model_name, method, X, Y, ord=ord, nb_iter=nb_iter, eps=eps) if ord == np.inf: norm = 'inf' else: norm = ord attack_params = 'ord{}_nbIter{}_eps{}'.format( norm, nb_iter, int(1000 * eps)) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.DEEPFOOL: for order in [2]: for overshoot in ATTACK.get_df_overshoots(order): print('attack {} -- order: {}; overshoot: {}'.format( method.upper(), order, overshoot)) X_adv, _ = get_adversarial_examples(model_name, method, X, Y, ord=order, overshoot=overshoot) attack_params = 'l{}_overshoot{}'.format(order, int(overshoot)) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.CW_L2: binary_search_steps = 16 #9 cw_batch_size = 2 #1 initial_const = 1 #10 for learning_rate in ATTACK.get_cwl2_lr(): for max_iter in ATTACK.get_cwl2_maxIter(): print('{}: (ord={}, max_iterations={})'.format( method.upper(), 2, max_iter)) X_adv, _ = get_adversarial_examples( model_name, method, X, Y, ord=2, max_iterations=max_iter, binary_search_steps=binary_search_steps, cw_batch_size=cw_batch_size, initial_const=initial_const, learning_rate=learning_rate) attack_params = 'lr{}_maxIter{}'.format( int(learning_rate * 1000), max_iter) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.CW_Linf: initial_const = 1e-5 # X *= 255. for learning_rate in ATTACK.get_cwl2_lr(): for max_iter in ATTACK.get_cwl2_maxIter(): print('{}: (ord={}, max_iterations={})'.format( method.upper(), np.inf, max_iter)) X_adv, _ = get_adversarial_examples( model_name, method, X, Y, max_iterations=max_iter, initial_const=initial_const, learning_rate=learning_rate) attack_params = 'lr{}_maxIter{}'.format( int(learning_rate * 10), max_iter) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.CW_L0: initial_const = 1e-5 for learning_rate in ATTACK.get_cwl2_lr(): for max_iter in ATTACK.get_cwl2_maxIter(): print('{}: (ord={}, max_iterations={})'.format( method.upper(), np.inf, max_iter)) X_adv, _ = get_adversarial_examples( model_name, method, X, Y, max_iterations=max_iter, initial_const=initial_const, learning_rate=learning_rate) attack_params = 'lr{}_maxIter{}'.format( int(learning_rate * 10), max_iter) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.JSMA: for theta in ATTACK.get_jsma_theta(): for gamma in ATTACK.get_jsma_gamma(): print('{}: (theta={}, gamma={})'.format( method.upper(), theta, gamma)) X_adv, _ = get_adversarial_examples(model_name, method, X, Y, theta=theta, gamma=gamma) attack_params = 'theta{}_gamma{}'.format( int(100 * theta), int(100 * gamma)) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.PGD: nb_iter = 1000 eps_iter = 0.05 #0.01 for eps in ATTACK.get_pgd_eps(): if eps < 0.05: eps_iter = 0.01 elif eps <= 0.01: eps_iter = 0.005 X_adv, _ = get_adversarial_examples(model_name, method, X, Y, eps=eps, nb_iter=nb_iter, eps_iter=eps_iter) attack_params = 'eps{}_nbIter{}_epsIter{}'.format( int(1000 * eps), nb_iter, int(1000 * eps_iter)) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.ONE_PIXEL: for pixel_counts in ATTACK.get_op_pxCnt(): for max_iter in ATTACK.get_op_maxIter(): for pop_size in ATTACK.get_op_popsize(): attack_params = { 'pixel_counts': pixel_counts, 'max_iter': max_iter, 'pop_size': pop_size } X_adv, _ = get_adversarial_examples( model_name, method, X, Y, **attack_params) X_adv = np.asarray(X_adv) attack_params = 'pxCount{}_maxIter{}_popsize{}'.format( pixel_counts, max_iter, pop_size) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) elif method == ATTACK.MIM: for eps in ATTACK.get_mim_eps(): for nb_iter in ATTACK.get_mim_nbIter(): attack_params = {'eps': eps, 'nb_iter': nb_iter} X_adv, _ = get_adversarial_examples(model_name, method, X, Y, **attack_params) attack_params = 'eps{}_nbIter{}'.format( int(eps * 100), nb_iter) reset(X, trans_type) reset(X_adv, trans_type) save_adv_examples(X_adv, prefix=prefix, bs_samples=X, dataset=dataset, transformation=trans_type, attack_method=method, attack_params=attack_params) del X del Y