def extract_items(self, indices, deepcopy=True, verbose=False): """ Given a list of indices, it returns a new DataObj that is a subset of this one, that includes only items at those indices. By default it creates a deep copy of the data object. Args: indices: (list) the indices you want to keep deepcopy: (bool) creates a deep copy of the data verbose: (bool) Returns: DataObj """ verbose_print("Extracting a slice of items", verbose, end="") # CREATE NEW DATA OBJ WITH EXTRACTED DATA new_data = DataObj() new_data.X = self.X[indices] new_data.Y = self.Y[indices] new_data.N = self.N[indices] new_data.BBOX = self.BBOX[indices] verbose_print_done(verbose) if deepcopy: return copy.deepcopy(new_data) else: return new_data
def steal_n_samples(self, n=1024, random=False, verbose=False): """ Removes n samples from the data and returns those removed samples as a new data object. You can optionaly select to steal the first n items (default) or a random sample of n items (random=True). """ verbose_print("Stealing {} samples. ".format(n), verbose, end="") # SELECT WHICH INDICES TO STEAL if random: indices = np.random.choice(np.arange(self.n_samples), size=n, replace=False) else: indices = np.arange(n) # CREATE NEW DATA OBJ WITH EXTRACTED DATA new_data = DataObj() new_data.X = self.X[indices] new_data.Y = self.Y[indices] new_data.N = self.N[indices] new_data.BBOX = self.BBOX[indices] # REMOVE DATA USED self.X = np.delete(self.X, indices, axis=0) self.Y = np.delete(self.Y, indices, axis=0) self.N = np.delete(self.N, indices, axis=0) self.BBOX = np.delete(self.BBOX, indices, axis=0) verbose_print_done(verbose) return new_data
def create_batch(self, batch_n, augment=False, verbose=False): """ Given the n'th batch number, it creates a new data object with that batch of data. Optionally allows you to do data augmentation on that batch. """ verbose_print("Creating a Batch of Data", verbose, end="") # CREATE NEW DATA OBJ WITH EXTRACTED DATA i, j = self.batch_indices(batch_n) batch_data = DataObj(batchsize=self.batchsize) batch_data.X = self.X[i:j] batch_data.Y = self.Y[i:j] batch_data.N = self.N[i:j] batch_data.BBOX = self.BBOX[i:j] if augment: batch_data.do_random_transforms() # batch_data.X, batch_data.BBOX = batch_augmentation( # X=batch_data.X, # bboxes=batch_data.BBOX, # N=batch_data.N, # crop=54, # random_crop=True, # brightness=0.3, # contrast=0.3, # blur=1, # noise=10, # seed=None) verbose_print_done(verbose) return batch_data
def get_n_samples(self, n=1024, random=False, indices=None, verbose=False): """ Gets n number of samples and returns those samples as a new data object. You can optionaly select to get the first n items (default) or a random sample of n items (random=True). """ verbose_print("Creating a sample of {} items".format(n), verbose, end="") # SELECT WHICH INDICES TO USE if random: indices = np.random.choice(np.arange(self.n_samples), size=n, replace=False) else: indices = np.arange(n) new_data = self.extract_items(indices=indices, deepcopy=True, verbose=False) verbose_print_done(verbose) # # CREATE NEW DATA OBJ WITH EXTRACTED DATA # new_data = DataObj() # new_data.X = self.X[indices] # new_data.Y = self.Y[indices] # new_data.N = self.N[indices] # new_data.BBOX = self.BBOX[indices] # # verbose_print_done(verbose) # return copy.deepcopy(new_data) return new_data
def shuffle(self, verbose=False): verbose_print("Shuffling the Data", verbose, end="") i_shuffled = np.random.permutation(self.n_samples) self.X = self.X[i_shuffled] self.Y = self.Y[i_shuffled] self.N = self.N[i_shuffled] self.BBOX = self.BBOX[i_shuffled] verbose_print_done(verbose)
def set_valid_data(self, n=1024, random=True, batchsize=128, verbose=False): verbose_print("Creating Validation Data", verbose, end="") self.valid = self.train.steal_n_samples(n=n, random=random) self.valid.do_center_crops() self.valid.set_batchsize(n=batchsize) verbose_print_done(verbose)
def set_train_eval_data(self, n=1024, random=False, random_transforms=True, batchsize=128, verbose=False): verbose_print("Creating Train Evaluation Data", verbose, end="") self.train_eval = self.train.get_n_samples(n=n, random=random) if random_transforms: self.train_eval.do_random_transforms() self.train_eval.set_batchsize(n=batchsize) verbose_print_done(verbose)
def do_random_transforms(self, verbose=False): """ Modifies the X data by applying random transforms on the image data, such as cropping, shifting, brightness, contrast, blur and noise. Also updates the bounding box labels to account for these transformations. NOTE: the images are rescaled to 54x54 """ verbose_print("Randomly transform the images", verbose, end="") self.X, self.BBOX = batch_augmentation(X=self.X, bboxes=self.BBOX, N=self.N, crop=54, random_crop=True, brightness=0.3, contrast=0.3, blur=1, noise=10, seed=None) verbose_print_done(verbose)
def limit_samples(self, n, verbose=False): """ Limits the data to a maximum of the first `n` samples. Deletes everything else. WARNING: This modifies the data object in-place. Args: n: (int or None) Limit data to this number of samples. If None is passed, then the data remains intact, no trimming occurs. verbose: (bool) """ if n is not None: verbose_print("Limiting data to {} samples".format(n), verbose, end="") self.X = self.X[:n] self.Y = self.Y[:n] self.N = self.N[:n] self.BBOX = self.BBOX[:n] verbose_print(" -- [DONE]", verbose) else: verbose_print( "No limit was imposed, all data will be used. ".format(n), verbose, end="\n")
def __init__(self, d=None, pickle=None, verbose=False): """ Creates an Evals object to store evaluation metrics for each epoch. Args: d: (dict or None)(optional) - initialize Evals object from a dictionary pickle: (str or None) (optional) path to a pickle file of a dictionary to initialize the Evals object. verbose: (bool) """ self.stuff = dict() self._initialized = True # INITIAL BLANK VALUES self.pda_train = [] self.pda_valid = [] self.wna_train = [] self.wna_valid = [] self.iou_train = [] self.iou_valid = [] self.time_pred = [] self.time_train = [] self.loss = [] self.alpha = [] # LOAD EVALS FROM DICTIONARY if d is not None: verbose_print("Loading evals from a dictionary", verbose=verbose, end="") self.stuff.update(copy.deepcopy(d)) # LOAD EVALS FROM PICKLE FILE (of a dictionary) elif pickle is not None: short_path = limit_string(pickle, tail=PRINT_WIDTH - 32) verbose_print("Loading evals from " + short_path, verbose, end="") if os.path.exists(pickle): d = pickle2obj(pickle) self.stuff.update(copy.deepcopy(d)) else: verbose_print("\n-- file does not exist. Creating blank Evals", verbose, end="") else: verbose_print("Creating blank Evals", verbose, end="") verbose_print_done(verbose)
# ESTABLISH MODEL SETTINGS settings = Settings() settings.conv_dropout = 0.1 settings.fc_dropout = 0.1 settings.image_chanels = 1 settings.image_size = (54, 54) # ============================================================================== # DATA # ============================================================================== data_dir = "data" X_file = os.path.join(data_dir, "X_test_cropped64.pickle") Y_file = os.path.join(data_dir, "Y_test.pickle") verbose_print("Loading data", verbose=verbose, end="") data = DataObj(X=pickle2obj(X_file), Y=pickle2obj(Y_file), batchsize=128) verbose_print_done(verbose) data.limit_samples(n=limit, verbose=verbose) verbose_print("Performing center crops", verbose=verbose, end="") data.do_center_crops() verbose_print_done(verbose) # ============================================================================== # GRAPH AND SESSION # ============================================================================== model = model_a checkpoint_dir = "results/A_02/checkpoints/" checkpoint_file = os.path.join(checkpoint_dir, "checkpoint_max.chk")
if evals.newest_is_max(): saver.save(sess, paths.checkpoint_max) ################################################################################ # MAIN ################################################################################ if __name__ == "__main__": # SETTINGS verbose = True opts = parse_settings() print_headers(opts.output_name, border="#", align="center", width=PRINT_WIDTH) verbose_print("Establishing paths", verbose, end="") paths = establish_paths(output_name=opts.output_name, input=opts.input_dir) verbose_print_done(verbose) # TRAIN DATA data = DataObjects() data.set_train_data(X=pickle2obj(paths.X_train, verbose=True), Y=pickle2obj(paths.Y_train, verbose=True), batchsize=opts.batch_size) # VALID DATA data.set_valid_data(n=opts.valid_size, random=False, batchsize=128, verbose=verbose)
from file_support import pickle2obj ################################################################################ # SETTINGS ################################################################################ verbose = True data_dir = "data" ################################################################################ # DATA ################################################################################ print_headers("EXTRACTING LABELS - NOTE: this may take a LONG time", verbose=verbose) verbose_print("Extracting labels from TRAIN mat file", verbose, end="") matfile = os.path.join(data_dir, "train", "digitStruct.mat") Y_train = mat_data_to_dict_of_arrays(matfile, limit=None) verbose_print_done(verbose) verbose_print("Extracting labels from EXTRA mat file", verbose, end="") matfile = os.path.join(data_dir, "extra", "digitStruct.mat") Y_extra = mat_data_to_dict_of_arrays(matfile, limit=None) verbose_print_done(verbose) verbose_print("Extracting labels from TEST mat file", verbose, end="") matfile = os.path.join(data_dir, "test", "digitStruct.mat") Y_test = mat_data_to_dict_of_arrays(matfile, limit=None) verbose_print_done(verbose) Y = {"train": Y_train, "extra": Y_extra, "test": Y_test}
with tf.Session(graph=graph) as sess: # GET IMPORTANT OPERATIONS AND TENSORS FROM GRAPH g = GraphOps(graph, "X", "Y", "BBOX", "is_training", "digit_logits", "bbox_logits") # INITIALIZE VARIABLES verbose = True saver = tf.train.Saver(name="saver") tf_initialize_vars_from_file(f=checkpoint_file, s=sess, saver=saver, verbose=verbose) # INITIALIZE THE VIDEO CAPTURE DEVICE verbose_print("Capturing webcam video", verbose, end="") cap = cv2.VideoCapture(0) verbose_print_done(verbose) if settings.save_stream is not None: print("Saving video as:", limit_string(settings.save_stream, tail=50)) # CONTINUOUSLY CAPTURE A FRAME, MAKE A PREDICTION, AND DISPLAY/SAVE while (1): # Take each frame from camera _, frame = cap.read() # PREPARE IMAGE im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert to grayscale im = cv2.resize(im, settings.image_size) # Scale image # PREDICT
def save_dict_pickle(self, f, verbose=False): short_path = limit_string(f, front=10, tail=31) verbose_print("Saving Evals to " + short_path, verbose, end="") obj2pickle(self.stuff, file=f) verbose_print_done(verbose)