def train_model_1(): base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) pipeline(base_model, model_name='resnet', fout_name='resnet', img_size=224, n_ft_layers=-28)
def train_model_4(): base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(256, 256, 3)) pipeline(base_model, model_name='resnet_256_2', fout_name='resnet_256_2', img_size=256, batch_size=32, n_ft_layers=-90)
def process_image(image): result = pipeline(image) # plt.xticks([]) # plt.yticks([]) # plt.imshow(result) # plt.show() return result
def get_detections_from_frame(idx, ts, im, thread_context=None, **kwargs): nonlocal pipeline if pipeline is not None: pipeline_results = pipeline([im]) else: if "pipeline" not in thread_context: thread_context["pipeline"] = pipeline_factory() pipeline_results = thread_context["pipeline"]([im]) #confident_ids = [r for c, r in zip(confidences, decoded_ids) if c >= threshold] #decimal_ids = set([ids.BeesbookID.from_bb_binary(i).as_ferwar() for i in confident_ids]) if verbose: crowns = pipeline_results[CrownOverlay] frame = ResultCrownVisualizer.add_overlay( im.astype(np.float64) / 255, crowns) fig, ax = plt.subplots(figsize=(20, 10)) plt.imshow(frame) plt.axis("off") plt.show() frame_id = bb_pipeline.io.unique_id() required_results = pipeline_results[PipelineResult] n_detections = required_results.orientations.shape[0] detection_ids = [ 'f{}d{}c{}'.format(frame_id, detection_idx, cam_id) for detection_idx in range(n_detections) ] decoded_ids = [list(r) for r in list(required_results.ids)] if n_detections > 0: frame_data = { "id": detection_ids, "localizerSaliency": required_results.saliencies.flatten(), "beeID": decoded_ids, "xpos": required_results.positions[:, 1] / scale, "ypos": required_results.positions[:, 0] / scale, "camID": [cam_id] * n_detections, "zrotation": required_results.orientations[:, 0], "timestamp": [ts] * n_detections, "frameIdx": [idx] * n_detections } if calculate_confidences: confidences = np.array([ np.product(np.abs(0.5 - np.array(r)) * 2) for r in decoded_ids ]) frame_data["confidence"] = confidences if confidence_filter is not None: frame_data = frame_data[ frame_data.confidence >= confidence_filter] else: frame_data = None return idx, frame_id, ts, frame_data
def main(): # K and d of the left and right camera used for the 3 data sequences are known, if one of these is selected as the master_path_to_dataset load the appropriate K. K_right = np.array([1.2432403472640376e+003, 0., 6.6606587228414776e+002, 0., 1.2331555645540684e+003, 4.5437658085060713e+002, 0., 0., 1.]).reshape(3, 3) d_right = np.array([-4.6261205649774895e-001, 2.0732672793966445e-001, 6.2375723417721863e-003, 2.6299185722505536e-003, -3.6689019973370124e-002]).reshape(1, 5) K_left = np.array([1.1930484416274905e+003, 0., 6.1463232877569749e+002, 0., 1.1864252309660656e+003, 4.3318083737428361e+002, 0., 0., 1.]).reshape(3, 3) d_left = np.array([-4.8441890104482732e-001, 3.1770182182461387e-001, 4.8167296939537890e-003, 5.9334794668205733e-004, -1.4902486951308128e-001]).reshape(1, 5) # Check if the dataset is one of the 3 sequences, and if so if it's the right or left camera if '2015-03-27_10-47-08_' in master_path_to_dataset: if 'monocular_left_calibrated' in master_path_to_dataset: print("Using K for left sequences") K = np.copy(K_left) d = np.copy(d_left) elif 'monocular_right_calibrated' in master_path_to_dataset: print("Using K for right sequences") K = np.copy(K_right) d = np.copy(d_right) else: print('Error: Original data sequence identified, but neither right nor left camera chosen: {}'.format(master_path_to_dataset)) K = np.zeros((3, 3)) d = np.zeroes((1, 5)) else: # Unexpected dataset: make the user confirm the images are in chronological order, and that they've inserted the values for K and d here #HERE K = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]).reshape(3, 3) d = np.array([0,1,2,3,4]).reshape(1, 5) cont = input("A different dataset than the one given with the assignment was detected.\nPlease ensure that the images in the directory are in chronological order, alphabetically (i.e. first image alphabetically is also first image chronologically)\nAlso ensure that you have inserted the correct camera intrinsics values into K and d in the code, after the comment '#HERE'.\nContinue? (y/n): ") if('y' != cont.lower() and 'yes' != cont.lower()): quit() # Execute the SfM pipeline on the image sequence pipeline(master_path_to_dataset, K, verbose=verbose, verbose_img=verbose_img)
def bulk_processing(): start_url = config('S3_LOCATION') # creates a list of all S3 URLs of raw images in the S3 bucket. all_files = get_matching_s3_keys(S3_BUCKET, 'raw_images/', '.png') clean_urls = [] urls = list(all_files) for a_url in urls: a_url = str(a_url) new_url = a_url.replace(" ", "+") new_url = start_url + new_url clean_urls.append(new_url) x = 0 # loops through each image organized above and processed using the # pipeline function, then subdividies it into 81 sudoku cells and # uploads each cell. for url in clean_urls: img_url = url request = urllib.request.Request(img_url) img = urllib.request.urlopen(request) imghash = hashlib.md5(img.read()).hexdigest() processed, imgarray = pipeline(img_url) processed_image = Image.fromarray(processed) with BytesIO() as in_mem_file_cropped: processed_image.save(in_mem_file_cropped, format='PNG') in_mem_file_cropped.seek(0) upload_file_to_s3( in_mem_file_cropped, config('S3_BUCKET'), "processed_puzzles/" + imghash + '_bulk_processed.png') processed_cells = [] i = 0 for array in imgarray: proc_img = Image.fromarray(array) with BytesIO() as in_mem_file: proc_img.convert('RGB').save(in_mem_file, format='PNG') in_mem_file.seek(0) upload_file_to_s3( in_mem_file, config('S3_BUCKET'), "processed_cells/" + imghash + "_" + str(i) + '_bulk_cell.png') i = i + 1 print("This is file " + str(x) + " out of " + str(len(clean_urls))) x = x + 1 print(str(img_url) + " has been uploaded!") return render_template('bulk_results.html', uploaded_files=clean_urls)
def run_async(self): startProcessTime = time.time() if not self.params['skipArchive']: # retrieve images from lt-archive self.logger.info("(process.run_async) retrieving images from archive") ltarchive = archive(self.params['path_pw_list'], self.params['archive_credentials_id'], self.params['skycam_lup_db_credentials_id'], self.err, self.logger) ## search for images matching criteria MySQLLogFile = self.params['resPath'] + "skycamfiles" ltarchive.getMySQLLog(self.params['resPath'], "skycam", "skycam", self.params['dateFrom'], self.params['dateTo'], self.params['instrument'], MySQLLogFile) ## get the data ltarchive.getData(MySQLLogFile, self.params['resPath']) else: for f in os.listdir(self.params['tmpMockPath']): shutil.copyfile(self.params['tmpMockPath'] + str(f), self.params['resPath'] + str(f)) # decompress and sort images by ascending datetime decompress_files(self.params['resPath'], self.err, self.logger) images = sort_image_directory_UTC(self.params['resPath'], self.err, self.logger) if not images: self.err.setError(-12) self.err.handleError() # START THE PIPELINE pipe = pipeline(self.params, self.err, self.logger) # spawn pipeline instance pipe.run(images) # and run for these images # log error code with open(self.params['resPath'] + 'res.exitcode', 'w') as f: f.write(str(self.err.getError())) # zip files in directory and purge res dir archive_name = self.params['dateFrom'].replace(" ", "T").replace("-", "").replace(":", "") + ".tar" zip_output_files_in_directory(self.params['resPath'], archive_name, self.err, self.logger) self._purge_output_dir(skipTarFiles=True) elapsed = (time.time() - startProcessTime) self.logger.info("(process.run_async) child process finished in " + str(round(elapsed)) + "s")
def run_sync(self): self.logger.info("(process.run_sync) starting synchronous process... ") pipe = pipeline(self.params, self.err, self.logger) UT_time_end_iter = time.time() - (60*3600) current_lag = 0 while True: UT_time_end = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(UT_time_end_iter)) UT_time_st_iter = UT_time_end_iter - self.params['t_sync_check'] UT_time_st = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(UT_time_st_iter)) self.logger.info("(process.run_sync) searching for files between " + UT_time_st + " and " + UT_time_end) startProcessTime = time.time() try: # retrieve latest images from lt-archive self.logger.info("(process.run) retrieving latest images from archive") ltarchive = archive(self.params['path_pw_list'], self.params['archive_credentials_id'], self.params['skycam_lup_db_credentials_id'], self.err, self.logger) ## search for images matching criteria MySQLLogFile = self.params['resPath'] + "res.skycamfiles" num_files = ltarchive.getMySQLLog(self.params['resPath'], "skycam", "skycam", UT_time_st, UT_time_end, self.params['instrument'], MySQLLogFile) if num_files > 0: ## get the data ltarchive.getData(MySQLLogFile, self.params['resPath']) # decompress and sort images by ascending datetime decompress_files(self.params['resPath'], self.err, self.logger) images = sort_image_directory_UTC(self.params['resPath'], self.err, self.logger) if not images: self.err.setError(-12) self.err.handleError() # run pipeline pipe.run(images) # zip files in directory and purge res dir archive_name = UT_time_st.replace(" ", "").replace("-", "").replace(":", "") + ".tar" zip_output_files_in_directory(self.params['resPath'], archive_name, self.err, self.logger) self._purge_output_dir(skipTarFiles=True) else: self.logger.info("(process.run_sync) no files found for this time range, continuing") except RuntimeError: self.err.setError(2) self.err.handleError() elapsed = (time.time() - startProcessTime) if elapsed > self.params['t_sync_check']: # we're lagging this iteration self.err.setError(13) self.err.handleError() current_lag = current_lag + (elapsed - self.params['t_sync_check']) if current_lag > 0: self.logger.info("(process.run_sync) processing is lagging by " + str(sf(current_lag, 2)) + "s") else: current_lag = 0 # wait some time before we query archive again, unless we're lagging time_to_wait = self.params['t_sync_check'] - elapsed - current_lag if time_to_wait > 0: time.sleep(time_to_wait) UT_time_end_iter = UT_time_end_iter + self.params['t_sync_check']
gfrom pipeline import pipeline nlp = pipeline("e2e-qg", model="valhalla/t5-base-e2e-qg") qg = pipeline("e2e-qg") qg2 = pipeline("multitask-qa-qg") print("preload finished.")
def process_image(image): global prev_left_fit, prev_right_fit out_img, prev_left_fit, prev_right_fit = pipeline(image, prev_left_fit, prev_right_fit) return out_img
def demo_file(): # Pulls the image file in the post request as a object in memory image_file = request.files['file'] # Takes the hash of the image - it will be used as a unique identifer # across the DS API. imghash = hashlib.md5(image_file.read()).hexdigest() image_file.seek(0) # uploads image to raw_puzzles folder imgurl = upload_file_to_s3(image_file, config('S3_BUCKET'), "raw_puzzles/" + imghash + '.png') # processes the raw image, and returns the raw image to processed # variable and returns 81 numpy arrays (of 28x28 dimensions) # representing each individual sudoku cell that makes up the whole # puzzle processed, imgarray = pipeline(imgurl) processed_image = Image.fromarray(processed) # loads the processed image in memory so it can be uploaded to S3 in # the process_puzzles folder. with BytesIO() as in_mem_file_cropped: processed_image.save(in_mem_file_cropped, format='PNG') in_mem_file_cropped.seek(0) processed_url = upload_file_to_s3( in_mem_file_cropped, config('S3_BUCKET'), "processed_puzzles/" + imghash + '_processed.png') # processes each individual suodku cell to be placed into S3. # This will add sevearl seconds to response time, so consider # commenting out when going to production once this is automated into a # daily batch job across newly uploaded images. processed_cells = [] i = 0 j = 0 for array in imgarray: proc_img = Image.fromarray(array) with BytesIO() as in_mem_file: proc_img.convert('RGB').save(in_mem_file, format='PNG') in_mem_file.seek(0) processed_cell_url = upload_file_to_s3( in_mem_file, config('S3_BUCKET'), "processed_cells/" + imghash + "_" + str(i) + '_cell.png') i = i + 1 processed_cells.append(processed_cell_url) # loads all of the numpy arrays from the image processing pipeline allArrays = np.empty((0, )) for array in imgarray: an_array = array.flatten().reshape(784, ) allArrays = np.concatenate([allArrays, an_array]) # reshapes the numpy array into 81 rows (one for each cell) and 784 columns (one for each value in the 784 pixels that make up a 28x28 pixel image) # loads it into a csv_buffer object and loaded to S3 for sagemaker # processing. allArrays = allArrays.reshape(81, 784) allArrays = np.rint(allArrays) csv_array = pd.DataFrame(allArrays) csv_buffer = StringIO() csv_array.to_csv(csv_buffer, header=False, index=False) csv_path = 'predict_payloads/' + str(imghash) + '.csv' csv_url = config('S3_LOCATION') + \ str(csv_path) csv_buffer.seek(0) s3.put_object(Body=csv_buffer.read(), Bucket=config('S3_BUCKET'), Key=csv_path) # Sends an POST request to the sagemaker API URL, appending the created # above to the request for the sagemaker endpoint to read in. SAGEMAKER_API_URL = config('SAGEMAKER_API_URL') data = {'data': csv_url} sagermaker_response = requests.post(SAGEMAKER_API_URL, json=data) # Below line is to use the AWS Sagemaker returned predictions. Comment it out, if you're testing with local models. #pred = sagermaker_response.content.decode('utf-8').replace("\n","").replace("0",".") # Below line is used for local KNN model. Comment out to use Sagemaker endpoint. # The web team needs to endpoint for reliability of testing, so leaving # it uncommented. A future release should use the sagemaker endpoint in # production. pred = predict_knn(config('MODEL_FILEPATH'), imgarray) # runs the predicted digits against the solver function # The definitions of these paramaters is documented in solver.py and # ai.py. grid_status = solve(str(pred))[0] solution = solve(str(pred))[1] difficulty = solve(str(pred))[3] # In the event a puzzle has invalid values, remapping from backend to # frontend is required for the front end to highlight invalid cells # (web team did not get this functionality in final launch, but leaving # it in for a future release as DS team functionality is ready) if len(list(solve(str(pred))[1])) != 81 & grid_status == 2: errors = list(solve(str(pred))[1]) for e in errors: error_pairs = [] if e == '': pass else: guess_pair = [] guess = e[0] cell = find_replace_multi(e[1], translation_dictionary) guess_pair.append(guess) guess_pair.append(cell) error_pairs.append(guess_pair) solution = error_pairs else: pass # db_csv = pd.DataFrame() # for i in range(len(pred)): # db_csv['pred'][i] = pred[i] # for i in range(len(processed_cell_url)): # db_csv['cell_url'][i] = processed_cell_url[i] # If the puzzle is solveable, adding the numpy arrays for the sudoku # cells into the ModelTrainer Database for future image training. if grid_status == 1: for i in range(len(imgarray)): #outfile = TemporaryFile() #np.save(outfile, imgarray[i]) #import pdb; pdb.set_trace() # stitch predicted class with S3 URL and Numpy Array entry = ModelTrainer( sudoku_hash=imghash, procesed_puzzle_url=processed_url, cell_url=processed_cells[i], numpy_array=imgarray[i].flatten().tolist(), predicted_value=pred[i]) DB.session.add(entry) DB.session.commit() # This template is used if you want to use the DS view (referenced in step 8b) # return render_template( # 'results.html', # imghash=imghash, # imgurl=imgurl, # pred=pred, # processed_url=processed_url, # processed_cells=processed_cells, # original_grid=original_grid, # solved=solution, # grid_status=grid_status) # This is the production API response to use for web team # functionality. return jsonify(values=pred, puzzle_status=grid_status, solution=solution, difficulty=difficulty)
import matplotlib.pyplot as plt import matplotlib.image as mpimg import pickle import glob import os from moviepy.editor import VideoFileClip from line import Line from utils import * from pipeline import * images = glob.glob('test_images/*.jpg') # images = glob.glob('test_images/*.png') # images = glob.glob('test_images/test5.jpg') for idx, fname in enumerate(images): image = mpimg.imread(fname) result = pipeline(image) left_line.detected = False left_line.recent_fits = [] left_line.best_fit = None right_line.detected = False right_line.recent_fits = [] right_line.best_fit = None # Plot the result f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9)) f.tight_layout() ax1.imshow(image) ax1.set_title('Original Image', fontsize=40)
def main(): # if __name__=="__main__": pipeline()
import os import sys import numpy as np from pipeline import * fasta_proteome_path = sys.argv[1] outpath = sys.argv[2] data = pipeline(fasta_proteome_path) # outfile = open(outpath, 'w') # data.to_csv(outfile) # outfile.close() data.to_csv(outpath)
def _getData(self, filename): """Run through the pipeline and load the data.""" endog, exog, self.end_blocks = pipeline(filename) self.endog = endog self.exog = exog
conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) flat = Flatten()(pool2) hidden1 = Dense(10, activation='relu')(flat) output = Dense(1, activation='sigmoid')(hidden1) model = Model(inputs=visible, outputs=output) # summarize layers print(model.summary()) # training num_epochs = 5 model.compile( optimizer=RMSprop(), loss=tf.keras.losses.BinaryCrossentropy(), metrics=["accuracy"], ) if not (path.isfile('model.h5')): print("Model doesn't currently exist, creating new one.") history = model.fit(pipeline(train_generator), batch_size=32, steps_per_epoch=100, epochs=num_epochs) model.save_weights("model.h5") else: model.load_weights("model.h5") history = model.fit(pipeline(train_generator), batch_size=32, steps_per_epoch=100, epochs=num_epochs) model.save_weights("model.h5")
]) traveltime_result.to_csv(os.path.join( cfg.data.prediction_dir, '{}_travelTime.csv'.format(exp_name)), sep=',', header=True, index=False) logging.info('Prediction Finished!') sess.close() if __name__ == '__main__': # parse args parser = argparse.ArgumentParser() parser.add_argument( '--update_feature', type=int, choices=[0, 1], default=0, help='Indicate whether to create or update hand-craft features') parser.add_argument('--lr', type=float, help='Learning rate') parser.add_argument('--resume_epoch', type=int, help='Resume training') args = parser.parse_args() # logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s') # Start Pipeline logging.info("Check Arguments: {}".format(args)) args.update_feature = bool(args.update_feature) pipeline(args)
def recognize(pipeline, img): package = Package(img) pipeline(package) return package