def main(): # Fixes numpy's random seed np.random.seed(0) print(f"Loading train and validate datasets...") train_data, train_tags = load_dataset(TRAIN_CSV_PATH) validate_data, validate_tags = load_dataset(VALIDATE_CSV_PATH) print(f"Loaded datasets successfully") ann = ANN() ann.add_layer(number_of_neurons=256, activation_function=Relu, input_dim=3072) ann.add_layer(number_of_neurons=128, activation_function=Relu) ann.add_layer(number_of_neurons=10, activation_function=Softmax) create_output_dir(MODELS_DIR) print(f"Starting the ANN train process...") for i in range(START_EPOCH, EPOCHS + START_EPOCH): ann.train(train_data, train_tags, alpha=0.0005, epochs=1, noise_factor=0.8) acc_train = ann.evaluate(train_data, train_tags) acc_validate = ann.evaluate(validate_data, validate_tags) model_file_name = f"{i}_{acc_train * 100:.3f}_{acc_validate * 100:.3f}" + ANN.EXTENSION print( f"Epoch: {i}, Train accuracy: {acc_train * 100:.3f}, Validate accuracy: {acc_validate * 100:.3f}" ) ann.save(os.path.join(MODELS_DIR, model_file_name))
def __init__(self, args): self.args = args self.args.n_datasets = len(args.data) self.modelPath = Path('checkpoints') / args.expName self.logger = create_output_dir(args, self.modelPath) self.data = [DatasetSet(d, args.seq_len, args) for d in args.data] self.losses_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.loss_total = LossMeter('total') self.evals_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.eval_total = LossMeter('eval total') self.start_epoch = 0 #torch.manual_seed(args.seed) #torch.cuda.manual_seed(args.seed) #get the pretrained model checkpoints checkpoint = args.checkpoint.parent.glob(args.checkpoint.name + '_*.pth') checkpoint = [c for c in checkpoint if extract_id(c) in args.decoder][0] model_args = torch.load(args.checkpoint.parent / 'args.pth')[0] self.encoder = Encoder(model_args) self.decoder = WaveNet(model_args) self.encoder = Encoder(model_args) self.encoder.load_state_dict(torch.load(checkpoint)['encoder_state']) #encoder freeze for param in self.encoder.parameters(): param.requires_grad = False #self.logger.debug(f'encoder at start: {param}') self.decoder = WaveNet(model_args) self.decoder.load_state_dict(torch.load(checkpoint)['decoder_state']) #decoder freeze for param in self.decoder.layers[:-args.decoder_update].parameters(): param.requires_grad = False #self.logger.debug(f'decoder at start: {param}') self.encoder = torch.nn.DataParallel(self.encoder).cuda() self.decoder = torch.nn.DataParallel(self.decoder).cuda() self.model_optimizer = optim.Adam(chain(self.encoder.parameters(), self.decoder.parameters()), lr=args.lr) self.lr_manager = torch.optim.lr_scheduler.ExponentialLR( self.model_optimizer, args.lr_decay) self.lr_manager.step()
def __init__(self, args): self.args = args self.args.n_datasets = len(self.args.data) self.expPath = Path('checkpoints') / args.expName torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) self.logger = create_output_dir(args, self.expPath) self.data = [DatasetSet(d, args.seq_len, args) for d in args.data] self.losses_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.loss_total = LossMeter('total') self.evals_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.eval_total = LossMeter('eval total') self.encoder = Encoder(args) self.decoder = WaveNet(args) assert args.checkpoint, 'you MUST pass a checkpoint for the encoder' if args.continue_training: checkpoint_args_path = os.path.dirname( args.checkpoint) + '/args.pth' checkpoint_args = torch.load(checkpoint_args_path) self.start_epoch = checkpoint_args[-1] + 1 else: self.start_epoch = 0 states = torch.load(args.checkpoint) self.encoder.load_state_dict(states['encoder_state']) if args.continue_training: self.decoder.load_state_dict(states['decoder_state']) self.logger.info('Loaded checkpoint parameters') self.encoder = torch.nn.DataParallel(self.encoder).cuda() self.decoder = torch.nn.DataParallel(self.decoder).cuda() self.model_optimizer = optim.Adam(self.decoder.parameters(), lr=args.lr) if args.continue_training: self.model_optimizer.load_state_dict( states['model_optimizer_state']) self.lr_manager = torch.optim.lr_scheduler.ExponentialLR( self.model_optimizer, args.lr_decay) self.lr_manager.last_epoch = self.start_epoch self.lr_manager.step()
def initialize_step(self): """ Sets up the logger and creates the output directory :return: """ function_name = sys._getframe(1).f_code.co_name log = logging.getLogger(name=function_name) if self.debug: log.setLevel(logging.DEBUG) else: log.setLevel(logging.WARNING) output_dir = create_output_dir(output_dir_name=function_name, parent_dir=self.out_dir, debug=self.debug) return log, output_dir
def main(): parser = argparse.ArgumentParser(description='PyTorch Loop') # Env options: parser.add_argument('--epochs', type=int, default=92, metavar='N', help='number of epochs to train (default: 92)') parser.add_argument('--seed', type=int, default=10, metavar='S', help='random seed (default: 3)') parser.add_argument('--expName', type=str, default='vctk', metavar='E', help='Experiment name') parser.add_argument('--data', default='data/vctk', metavar='D', type=str, help='Data path') parser.add_argument('--checkpoint', default='', metavar='C', type=str, help='Checkpoint path') parser.add_argument('--gpu', default=0, metavar='G', type=int, help='GPU device ID') # Data options parser.add_argument('--max-seq-len', type=int, default=1000, help='Max sequence length for tbptt') parser.add_argument('--batch-size', type=int, default=64, help='Batch size') # Model options parser.add_argument('--nspk', type=int, default=22, help='Number of speakers') # init args = parser.parse_args() args.expName = os.path.join('checkpoints', args.expName) torch.cuda.set_device(args.gpu) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) logging = create_output_dir(args) # data valid_dataset = NpzFolder(args.data + '/numpy_features_valid', args.nspk == 1) valid_loader = NpzLoader(valid_dataset, max_seq_len=args.max_seq_len, batch_size=args.batch_size, num_workers=4, pin_memory=True) # load model model, norm = model_def(args.checkpoint, gpu=args.gpu, valid_loader=valid_loader) # Begin! eval_loss = evaluate(model, norm, valid_loader, logging)
def find_fit(vi, signaturedirectory, image, outputdir, outputfoldername, startdoy, doyinterval, temporalshift, threshold, ndvalue, subset, meantype, numberofprocesses, cliptopixelextent, cliptoshapeextent, timebounds, xbounds, ybounds): """ Fit the fit of reference temporal signatures to pixels in a multidate image. """ #TODO Docstring #TODO Add Parameter Validation Callbacks as necessary # validate clip options if cliptopixelextent and cliptoshapeextent: click.BadParameter("Cannot clip the image to both a shapefile and pixel extent. Choose one or the other.") # import required modules import os from signatureFunctions import get_sigs_in_dir from utils import create_output_dir from imageFunctions import clip_raster_to_extent, clip_and_mask_raster_with_shapefile from fitting import fit_refs_to_image signatures = get_sigs_in_dir(signaturedirectory, viname=vi) if outputdir is None: outputdir = os.path.dirname(image) outdir = create_output_dir(outputdir, outputfoldername) if cliptoshapeextent: imagename, ext = os.path.splitext(os.path.basename(image)) outimage = os.path.join(outdir, imagename + "_clip" + ext) imagetoprocess = clip_and_mask_raster_with_shapefile(image, cliptoshapeextent, outimage) elif cliptopixelextent: imagename, ext = os.path.splitext(os.path.basename(image)) outimage = os.path.join(outdir, imagename + "_clip" + ext) imagetoprocess = clip_raster_to_extent(image, outimage, cliptopixelextent[0], cliptopixelextent[1], cliptopixelextent[2], cliptopixelextent[3]) else: imagetoprocess = image fit_refs_to_image(imagetoprocess, outdir, signatures, startdoy, doyinterval, temporalshift, threshold=threshold, ndvalue=ndvalue, subset=subset, meantype=meantype, workers=numberofprocesses, timebounds=timebounds, xbounds=xbounds, ybounds=ybounds)
def extract_signatures(image, shapefiledirectory, startdoy, doyinterval, outputdir, filelabel, plotsigs): """ Extracts temporal signatures for a set of point geometry shapefiles in a specified directory and outputs them to a set of .ref files in an output directory. """ import os from plotting import SignaturePlot from utils import find_files, create_output_dir, unique_name from signatureFunctions import get_sigs_in_dir, get_reference_curves if outputdir is None: outputdir = create_output_dir(os.path.dirname(image), "signatures", usetime=True) shapefiles = find_files(shapefiledirectory, ".shp", recursive=False) #TODO: Need a method to find only valid shapefiles in the directory get_reference_curves(image, shapefiles, startdoy, doyinterval, outdir=outputdir, filepostfix=filelabel) if plotsigs: path = unique_name(outputdir, "signaturePlot", ext=".pdf") sigs = get_sigs_in_dir(outputdir) plot = SignaturePlot(outputdir, os.path.basename(path)) plot.plot_collection(sigs)
def classify(fitimagedirectory, cropimage, outputdirectory, ndvalue, outputimagename, valueofcropinimage, tstart, tstep, tstepcount, nocombo, thresholds, numberofprocesses, chunksize): """ Classify a multidate image and assess the accuracy of said classification. """ # import required functions import os from utils import create_output_dir from classify import classify_and_assess_accuracy, generate_thresholds, get_fit_rasters, chunks import multiprocessing # get the fit rasters to use filevallist = get_fit_rasters(fitimagedirectory, valueofcropinimage) # validate threshold parameters if (tstart or tstep or tstepcount or nocombo) and thresholds: raise click.BadParameter("Cannot use both a threshold list and stepping threshold options.") elif thresholds: thresholds = eval(thresholds) for thresh in thresholds: if len(thresh) != len(filevallist): raise click.BadParameter("Length of threshold in threshold value list is not the same as the number of fit rasters. Counts must be equal.") else: pass thresholds = (thresholds, len(thresholds)) elif tstart and tstepcount and tstep: # create threshold generator if nocombo: thresholds = [] for val in range(tstart, (tstepcount * tstep + tstart), tstep): threshtemp = [val for item in filevallist] thresholds.append(threshtemp) thresholds = (thresholds, len(thresholds)) else: thresholds = (generate_thresholds(tstart, tstep, tstepcount, len(filevallist)), tstepcount**len(filevallist)) else: raise click.BadParameter("Threshold options incomplete or otherwise incorrectly used.") if outputdirectory is None: outputdirectory = create_output_dir(os.path.dirname(fitimagedirectory), "classification", usetime=True) if numberofprocesses == 1: classify_and_assess_accuracy(outputdirectory, cropimage, valueofcropinimage, filevallist, ndvalue, thresholds, classifiedimagename=outputimagename) elif numberofprocesses > 1: processes = [] threshlength = thresholds[1] i = 0 for chunk in chunks(thresholds[0], size=chunksize): if threshlength - chunksize >= 0: threshlength -= chunksize chunk = (chunk, chunksize) else: chunk = (chunk, threshlength) processoutput = create_output_dir(outputdirectory, "process_" + str(i)) i += 1 p = multiprocessing.Process(target=classify_and_assess_accuracy, args=(processoutput, cropimage, valueofcropinimage, filevallist, ndvalue, chunk), kwargs={"classifiedimagename": outputimagename}) p.start() processes.append(p) if len(processes) == numberofprocesses: for p in processes: p.join() processes.remove(p) for p in processes: p.join() processes.remove(p) else: click.BadParameter("Number of worker processes must be greater than zero.")
print('-' * 30) print('Parameters') print('-' * 30) for key, value in vars(args).items(): print('{:<20} := {}'.format(key, value)) print('-' * 30) if "chauffeur" in args.model: model = load_model(args.model, custom_objects={"rmse": rmse}) else: model = load_model(args.model) assert model is not None MIN_SPEED = 10 MAX_SPEED = args.max_speed speed_limit = MAX_SPEED if args.data_dir != '': utils.create_output_dir(args, utils.csv_fieldnames_original_simulator) print("RECORDING THIS RUN ...") else: print("NOT RECORDING THIS RUN ...") # wrap Flask application with engineio's middleware app = socketio.Middleware(sio, app) # deploy as an eventlet WSGI server eventlet.wsgi.server(eventlet.listen(('', 4567)), app)
help='Noise level to use') parser.add_argument('--attention-alignment', type=float, default=0.05, help='# of features per letter/phoneme') parser.add_argument('--nspk', type=int, default=22, help='Number of speakers') parser.add_argument('--mem-size', type=int, default=20, help='Memory number of segments') # init args = parser.parse_args() args.expName = os.path.join('checkpoints', args.expName) torch.cuda.set_device(args.gpu) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) logging = create_output_dir(args) vis = visdom.Visdom(env=args.expName) # data logging.info("Building dataset.") train_dataset = NpzFolder(args.data + '/numpy_features', args.nspk == 1) train_loader = NpzLoader(train_dataset, max_seq_len=args.max_seq_len, batch_size=args.batch_size, num_workers=4, pin_memory=True, shuffle=True) valid_dataset = NpzFolder(args.data + '/numpy_features_valid', args.nspk == 1) valid_loader = NpzLoader(valid_dataset,
def main(data_dir, plane, epochs, lr, weight_decay, device=None): diagnoses = ['abnormal', 'acl', 'meniscus'] exp = f'{datetime.now():%Y-%m-%d_%H-%M}' out_dir, losses_path = create_output_dir(exp, plane) if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' print('Creating data loaders...') train_loader = make_data_loader(data_dir, 'train', plane, device, shuffle=True) valid_loader = make_data_loader(data_dir, 'valid', plane, device) print(f'Creating models...') # Create a model for each diagnosis models = [MRNet().to(device), MRNet().to(device), MRNet().to(device)] # Calculate loss weights based on the prevalences in train set pos_weights = calculate_weights(data_dir, 'train', device) criterions = [nn.BCEWithLogitsLoss(pos_weight=weight) \ for weight in pos_weights] optimizers = [make_adam_optimizer(model, lr, weight_decay) \ for model in models] lr_schedulers = [make_lr_scheduler(optimizer) for optimizer in optimizers] min_valid_losses = [np.inf, np.inf, np.inf] print(f'Training a model using {plane} series...') print(f'Checkpoints and losses will be save to {out_dir}') for epoch, _ in enumerate(range(epochs), 1): print(f'=== Epoch {epoch}/{epochs} ===') batch_train_losses = np.array([0.0, 0.0, 0.0]) batch_valid_losses = np.array([0.0, 0.0, 0.0]) for inputs, labels in train_loader: inputs, labels = inputs.to(device), labels.to(device) batch_loss = batch_forward_backprop(models, inputs, labels, criterions, optimizers) batch_train_losses += batch_loss valid_preds = [] valid_labels = [] for inputs, labels in valid_loader: inputs, labels = inputs.to(device), labels.to(device) batch_preds, batch_loss = \ batch_forward(models, inputs, labels, criterions) batch_valid_losses += batch_loss valid_labels.append(labels.detach().cpu().numpy().squeeze()) valid_preds.append(batch_preds) batch_train_losses /= len(train_loader) batch_valid_losses /= len(valid_loader) print_stats(batch_train_losses, batch_valid_losses, valid_labels, valid_preds) save_losses(batch_train_losses, batch_valid_losses, losses_path) update_lr_schedulers(lr_schedulers, batch_valid_losses) for i, (batch_v_loss, min_v_loss) in \ enumerate(zip(batch_valid_losses, min_valid_losses)): if batch_v_loss < min_v_loss: save_checkpoint(epoch, plane, diagnoses[i], models[i], optimizers[i], out_dir) min_valid_losses[i] = batch_v_loss
print('-' * 30) MAX_SPEED = args.speed MIN_SPEED = 10 speed_limit = MAX_SPEED if "chauffeur" in args.model: model = load_model(args.model, custom_objects={"rmse": rmse}) MAX_SPEED += 5 speed_limit = MAX_SPEED else: model = load_model(args.model) MAX_SPEED += 5 speed_limit = MAX_SPEED autoenconder_model = VariationalAutoencoder(args.anomaly_detector) anomaly_detection = utils.load_autoencoder(autoenconder_model) anomaly_detection.compile(optimizer='adam', loss='mean_squared_error') if args.data_dir != '': utils.create_output_dir(args, utils.csv_fieldnames_improved_simulator) print("RECORDING THIS RUN ...") else: print("NOT RECORDING THIS RUN ...") # wrap Flask application with engineio's middleware app = socketio.Middleware(sio, app) # deploy as an eventlet WSGI server eventlet.wsgi.server(eventlet.listen(('', 4567)), app)
def track_faces( self, clip_dir: str, out_base_dir: str, draw_on_dir: str = None, detect_only: bool = False, ): # Setup # load image paths frames: List[os.DirEntry] = load_and_sort_dir(clip_dir) draw_on_frames: List[os.DirEntry] = load_and_sort_dir(draw_on_dir) assert len(draw_on_frames) in (0, len(frames)) # create output directory out_dir: str = create_output_dir(out_base_dir) # initialize variables required for object tracking new_face_id: Iterator[int] = count(start=1) tracked_faces: Dict[int, TrackedFace] = {} # Iterate Through Video Frames for frame, draw_on_frame in zip_longest(frames, draw_on_frames): # load new frame img = cv.imread(frame.path) # load out_img out_img: np.ndarray = (img.copy() if draw_on_frame is None else cv.imread(draw_on_frame.path)) # ensure out_img is at least as large as img assert len(img.shape) == len(out_img.shape) and all( out_dim >= in_dim for in_dim, out_dim in zip(img.shape, out_img.shape)) detected_face_boxes: List[Box] = self.detect_face_boxes(img) # If tracking is disabled, draw the boxes and move to next frame if detect_only: write_boxes( out_path=os.path.join(out_dir, frame.name), out_img=out_img, boxes=detected_face_boxes, ) continue detected_faces: List[GenderedFace] = gender_faces( img=img, faces=[ self.recognize_face(img, detected_face_box) for detected_face_box in detected_face_boxes ], ) current_face_ids: Set[int] = set() lost_face_ids: Set[int] = set() # Iterate over the known (tracked) faces for tracked_face in tracked_faces.values(): matched_detected_faces: List[GenderedFace] = [ detected_face for detected_face in detected_faces if self.faces_match(tracked_face, detected_face) ] if not matched_detected_faces: # Tracked face was not matched to and detected face # Increment staleness since we didn't detect this face tracked_face.staleness += 1 # Update tracker with img and get confidence tracked_confidence: float = tracked_face.tracker.update( img) if (tracked_face.staleness < self.tracking_expiry and tracked_confidence >= self.tracking_threshold): # Assume face is still in frame but we failed to detect # Update box with predicted location box predicted_box: Box = Box.from_dlib_rect( tracked_face.tracker.get_position()) tracked_face.box = predicted_box current_face_ids.add(tracked_face.id_) else: # Assume face has left frame because either it is too stale or confidence is too low if self.remember_identities: # Set effectively infinite staleness to force tracker reset if face is found again later tracked_face.staleness = sys.maxsize else: lost_face_ids.add(tracked_face.id_) continue # Tracked face was matched to one or more detected faces # Multiple matches should rarely happen if faces in frame are distinct. We take closest to prev location # TODO: Handle same person multiple times in frame matched_detected_face = min( matched_detected_faces, key=lambda face: tracked_face.box.distance_to(face.box), ) # Update tracked_face tracked_face.descriptor = matched_detected_face.descriptor tracked_face.shape = matched_detected_face.descriptor tracked_face.box = matched_detected_face.box if tracked_face.staleness >= self.tracking_expiry: # Face was not present in last frame so reset tracker tracked_face.tracker = dlib.correlation_tracker() tracked_face.tracker.start_track( image=img, bounding_box=tracked_face.box.to_dlib_rect()) else: # Face was present in last frame so just update guess tracked_face.tracker.update( image=img, guess=tracked_face.box.to_dlib_rect()) tracked_face.staleness = 0 tracked_face.gender = matched_detected_face.gender tracked_face.gender_confidence = matched_detected_face.gender_confidence # Add tracked_face to current_ids to reflect that it is in the frame current_face_ids.add(tracked_face.id_) # remove matched_detected_face from detected_faces detected_faces.remove(matched_detected_face) # Delete all faces that were being tracked but are now lost # lost_face_ids will always be empty if self.remember_identities is True for id_ in lost_face_ids: del tracked_faces[id_] for new_face in detected_faces: # This is a new face (previously unseen) id_ = next(new_face_id) tracker: dlib.correlation_tracker = dlib.correlation_tracker() tracker.start_track(image=img, bounding_box=new_face.box.to_dlib_rect()) tracked_faces[id_] = TrackedFace( box=new_face.box, descriptor=new_face.descriptor, shape=new_face.shape, id_=id_, tracker=tracker, gender=new_face.gender, gender_confidence=new_face.gender_confidence, ) current_face_ids.add(id_) write_boxes( out_path=os.path.join(out_dir, frame.name), out_img=out_img, boxes=[tracked_faces[id_].box for id_ in current_face_ids], labelss=[[ ( f'Person {id_}', Point(3, 14), ), ( f'{tracked_faces[id_].gender.name[0].upper()}: {round(100 * tracked_faces[id_].gender_confidence, 1)}%', Point(3, 30), ), ] for id_ in current_face_ids], color=Color.yellow(), ) print( f"Processed {frame.name}. Currently tracking {len(tracked_faces)} faces" ) return out_dir
def __init__(self, args): self.args = args self.args.n_datasets = len(self.args.data) self.expPath = Path('checkpoints') / args.expName torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) self.logger = create_output_dir(args, self.expPath) self.data = [DatasetSet(d, args.seq_len, args) for d in args.data] assert not args.distributed or len(self.data) == int( os.environ['WORLD_SIZE'] ), "Number of datasets must match number of nodes" self.losses_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.loss_d_right = LossMeter('d') self.loss_total = LossMeter('total') self.evals_recon = [ LossMeter(f'recon {i}') for i in range(self.args.n_datasets) ] self.eval_d_right = LossMeter('eval d') self.eval_total = LossMeter('eval total') self.encoder = Encoder(args) self.decoder = WaveNet(args) self.discriminator = ZDiscriminator(args) if args.checkpoint: checkpoint_args_path = os.path.dirname( args.checkpoint) + '/args.pth' checkpoint_args = torch.load(checkpoint_args_path) self.start_epoch = checkpoint_args[-1] + 1 states = torch.load(args.checkpoint) self.encoder.load_state_dict(states['encoder_state']) self.decoder.load_state_dict(states['decoder_state']) self.discriminator.load_state_dict(states['discriminator_state']) self.logger.info('Loaded checkpoint parameters') else: self.start_epoch = 0 if args.distributed: self.encoder.cuda() self.encoder = torch.nn.parallel.DistributedDataParallel( self.encoder) self.discriminator.cuda() self.discriminator = torch.nn.parallel.DistributedDataParallel( self.discriminator) self.logger.info('Created DistributedDataParallel') else: self.encoder = torch.nn.DataParallel(self.encoder).cuda() self.discriminator = torch.nn.DataParallel( self.discriminator).cuda() self.decoder = torch.nn.DataParallel(self.decoder).cuda() self.model_optimizer = optim.Adam(chain(self.encoder.parameters(), self.decoder.parameters()), lr=args.lr) self.d_optimizer = optim.Adam(self.discriminator.parameters(), lr=args.lr) if args.checkpoint and args.load_optimizer: self.model_optimizer.load_state_dict( states['model_optimizer_state']) self.d_optimizer.load_state_dict(states['d_optimizer_state']) self.lr_manager = torch.optim.lr_scheduler.ExponentialLR( self.model_optimizer, args.lr_decay) self.lr_manager.last_epoch = self.start_epoch self.lr_manager.step()
plevels.append(level) return plevels if sys.version_info[0] == 2: rinput = raw_input( "Do you want to download the modular polynomial database? [Y/N]: ") else: rinput = input( "Do you want to download the modular polynomial database? [Y/N]: ") if rinput.lower() != "y": sys.exit() dirname = os.path.join(os.path.abspath(".."), MODPOLYS_DIR) create_output_dir(dirname) print("\n====================================================================") print("Downloading the modular polynomial database...\n") for level in prime_levels(): url = get_url(level) filename = url.rsplit("/", 1)[1] filepath = os.path.join(dirname, filename) print("Downloading %s..." % filename, end=" ") r = requests.get(url, allow_redirects=True) with open(filepath, "wb") as f: f.write(r.content) print("Done.", end="\n") print("\nFinished downloading the modular polynomial database.") print("====================================================================")
def build_multiband_image(rootDIR, outName, newfoldername, find, drivercode, ndvalue, outputdir=None): """ ##Set Args## rootdirectory = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/MODIS_KANSAS_2012/" outputfilename = "test" newfoldername = "kansas" VItofind = "EVI" drivercode = "ENVI" nodatavalue = -3000 #projection = "PROJCS[\"Sinusoidal\",GEOGCS[\"GCS_Undefined\",DATUM[\"D_Undefined\", SPHEROID[\"User_Defined_Spheroid\",6371007.181,0.0]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\", 0.017453292519943295]],PROJECTION[\"Sinusoidal\"],PARAMETER[\"False_Easting\",0.0], PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",0.0],UNIT[\"Meter\",1.0]]" sys.exit(build_multiband_image(rootdirectory, outputfilename, newfoldername, VItofind, drivercode, nodatavalue) """ #TODO docstrings if outputdir is None: outputdir = rootDIR outdir = create_output_dir(outputdir, newfoldername) print "\nOutputting files to : {0}".format(outdir) print "\nFinding HDF files in directory/subfolders: {0}".format(rootDIR) hdfs = find_files(rootDIR, ".hdf") print "\tFound {0} files.".format(len(hdfs)) print "\nGetting images to process of type {0}...".format(find) toprocess = [] for hdf in hdfs: sds = get_hdf_subdatasets(hdf) for ds in sds: if find.upper() in ds[1].upper(): toprocess.append(ds[0]) print "\t\t{0}".format(ds[0]) bands = len(toprocess) print "\tFound {0} images of type {1}.".format(bands, find) #print "\nGetting output parameters..." #rows, cols, datatype, geotransform, projection = open_image(toprocess[0]) #print "\tParameters: rows: {0}, cols: {1}, datatype: {2}, projection: {3}.".format(rows, cols, datatype, projection) outfile = os.path.join(outdir, outName) print "\nOutput file is: {0}".format(outfile) ## Create output file from first file to process ## template = openImage(toprocess[0]) templateproperties = gdalProperties(template) outds = copySchemaToNewImage(templateproperties, outfile, numberofbands=bands, drivername=drivercode) template = "" del template print "\tCreated output file." print"\nAdding bands to output file..." for i in range(0, bands): print "\tProcessing band {0} of {1}...".format(i + 1, bands) print toprocess[i] image = openImage(toprocess[i]) band = image.GetRasterBand(1) outband = outds.GetRasterBand(i + 1) print "\t\tReading band data to array..." data = band.ReadAsArray(0, 0, templateproperties.cols, templateproperties.rows) print "\t\tWriting band data to output band..." outband.WriteArray(data, 0, 0) outband.SetNoDataValue(ndvalue) outband.FlushCache() outband = "" del data, outband band = "" image = "" print "\tFinished adding bands to output file." outds = "" del outds print "\nProcess completed."
def track_faces( self, clip_dir: str, out_base_dir: str, draw_on_dir: str = None, detect_only: bool = False, ): """ This is NOT recognition Tracking should be based on smooth object motion, not face recognition Steps Every frame: detect faces for old-face in tracked-faces: for new-face in detected-faces: if new-face in old-face-region and old-face in new-face-region: match new-face and old-face break if old-face not in matches and tracker.update(img) > thresh: match to tracked location for new-face not in matches: create new tracked-face """ # Setup # load image paths frames: List[os.DirEntry] = load_and_sort_dir(clip_dir) draw_on_frames: List[os.DirEntry] = load_and_sort_dir(draw_on_dir) assert len(draw_on_frames) in (0, len(frames)) # create output directory out_dir: str = create_output_dir(out_base_dir) # initialize variables required for object tracking new_face_id: Iterator[int] = count(start=1) tracked_faces: Dict[int, TrackedFace] = {} prev_img = None # Iterate Through Video Frames for frame, draw_on_frame in zip_longest(frames, draw_on_frames): # Read Images # read image to process img: np.ndarray = cv.imread(frame.path) # read image to draw on (if different) out_img = img.copy() if draw_on_frame is None else cv.imread( draw_on_frame.path) # ensure out_img is at least as large as img assert len(img.shape) == len(out_img.shape) and all( out_dim >= in_dim for in_dim, out_dim in zip(img.shape, out_img.shape)) detected_face_boxes: List[Box] = self.detect_faces(img) # If tracking is disabled, draw the boxes and move to next frame if detect_only: write_boxes( out_path=os.path.join(out_dir, frame.name), out_img=out_img, boxes=detected_face_boxes, ) continue current_ids_to_detection_idx: Dict[int, Optional[int]] = {} lost_tracked_face_ids: List[int] = [] # Iterate over the known (tracked) faces for tracked_face in tracked_faces.values(): # Update the tracker with the new image # Tracker generates new predicted_rect from previous predicted_rect # Tracker returns its confidence that the face is inside new predicted_rect predicted_rect_confidence: float = tracked_face.tracker.update(img) if predicted_rect_confidence < self.tracking_threshold: # We've lost te object. Maybe due to a cut. Can't simply look for closest faces. # We assume the face is no longer present in img and stop tracking it print( f"Too low: id={tracked_face.id_}, conf={predicted_rect_confidence}, frame={frame.name}" ) lost_tracked_face_ids.append(tracked_face.id_) # TODO: In this case, maybe matchTemplate with found faces to see if one is above thresh continue predicted_rect: dlib.rectangle = tracked_face.tracker.get_position( ) tracked_last_rect: dlib.rectangle = tracked_face.box.to_dlib_rect() # Iterate over newly detected faces for detected_i, detected_face_box in enumerate( detected_face_boxes): # TODO Maybe just do distance based # add confidence here? # I think track motion and distance detected_rect = detected_face_box.to_dlib_rect() if ( # TODO: verify these are good checks. Maybe check that the l2 dist is minimal instead # need to make sure not modifying tracked faces as we go if we start computing minimums # THEY ARENT # sanity check: face hasn't moved too much tracked_last_rect.contains(detected_rect.center()) and detected_rect.contains(tracked_last_rect.center()) # sanity check: tracker prediction isn't too far from detection and detected_rect.contains(predicted_rect.center()) and predicted_rect.contains(detected_rect.center())): # detected_face_box and tracked_face are the same face # tracker was already update to this location if tracked_face.id_ in current_ids_to_detection_idx: print( f'[ERROR] {tracked_face.id_} found multiple times. Keeping first match' ) else: tracked_face.box = detected_face_box current_ids_to_detection_idx[ tracked_face.id_] = detected_i new_tracker = dlib.correlation_tracker() new_tracker.start_track(image=img, bounding_box=detected_rect) tracked_face.tracker = new_tracker if tracked_face.id_ not in current_ids_to_detection_idx: assert predicted_rect_confidence >= self.tracking_threshold # Didn't detect this face, but tracker is confident it is at the predicted location. # We assume detector gave false negative tracked_face.box = Box.from_dlib_rect(predicted_rect) # tracker was updated to predicted_rect in update() call in condition current_ids_to_detection_idx[tracked_face.id_] = None # Remove lost face ids for lost_tracked_face_id in lost_tracked_face_ids: del tracked_faces[lost_tracked_face_id] tracked_detection_idxs = current_ids_to_detection_idx.values() # Track new faces for detected_i, detected_face_box in enumerate(detected_face_boxes): if detected_i not in tracked_detection_idxs: # Assume new face has entered frame and start tracking it id_ = next(new_face_id) tracker: dlib.correlation_tracker = dlib.correlation_tracker() tracker.start_track( image=img, bounding_box=detected_face_box.to_dlib_rect()) tracked_faces[id_] = TrackedFace(id_=id_, box=detected_face_box, tracker=tracker) current_ids_to_detection_idx[id_] = detected_i tracked_detection_idxs = current_ids_to_detection_idx.values() assert all(i in tracked_detection_idxs for i in range(len(detected_face_boxes))) assert len(current_ids_to_detection_idx) == len(tracked_faces) write_boxes( out_path=os.path.join(out_dir, frame.name), out_img=out_img, boxes=[face.box for face in tracked_faces.values()], labelss=[[(f'Person {face.id_}', Point(1, -9))] for face in tracked_faces.values()], )
if not os.path.isdir(opt.exp_path): os.makedirs(opt.exp_path) opt.log_dir = os.path.join(opt.exp_path, opt.model_name) if not os.path.exists(opt.log_dir): os.makedirs(opt.log_dir) opt.model_dir = os.path.join(opt.exp_path, opt.model_name) if not os.path.exists(opt.model_dir): os.makedirs(opt.model_dir) if opt.cmvn_file is not None: opt.cmvn_file = os.path.join(opt.model_dir, opt.cmvn_file) ## Create a logger for loging the training phase ## logging = create_output_dir(opt, opt.log_dir) ## Data Prepare ## logging.info("Building dataset") if opt.seq_training == 'true': opt.data_type = 'test' val_dataset = DeepSpeakerSeqDataset(opt, os.path.join(opt.dataroot, 'train')) val_loader = DeepSpeakerSeqDataLoader(val_dataset, batch_size=1, num_workers=opt.num_workers, shuffle=False, pin_memory=True) else: opt.data_type = 'test' val_dataset = DeepSpeakerDataset(opt, os.path.join(opt.dataroot, 'train'))
import torch.optim as optim import torch.nn.functional as F from torch.autograd import Variable from model import DeepSpeakerModel, DeepSpeakerSeqModel, DeepSpeakerCnnModel, DeepSpeakerCnnSeqModel from model import similarity, loss_cal, normalize, penalty_loss_cal, similarity_segment, loss_cal_segment, penalty_seq_loss_cal from config import TrainOptions from data_loader import DeepSpeakerDataset, DeepSpeakerDataLoader, DeepSpeakerSeqDataset, DeepSpeakerSeqDataLoader import utils opt = TrainOptions().parse() manualSeed = random.randint(1, 10000) random.seed(manualSeed) torch.manual_seed(manualSeed) torch.cuda.manual_seed(manualSeed) logging = utils.create_output_dir(opt) print(opt.gpu_ids) device = torch.device("cuda:{}".format(opt.gpu_ids[0]) if len(opt.gpu_ids) > 0 and torch.cuda.is_available() else "cpu") # data logging.info("Building dataset.") if opt.seq_training == 'true': opt.data_type = 'train' train_dataset = DeepSpeakerSeqDataset(opt, os.path.join(opt.dataroot, 'train')) train_loader = DeepSpeakerSeqDataLoader(train_dataset, batch_size=1, num_workers=opt.num_workers, shuffle=True,