def eval(checkpoint, data_path, params): # 数据 files, label, boxes = load_annotation(data_path, 'test') eval_set = YoloDataset(paths=files, bboxes=boxes, labels=label, params=params, train=False) eval_loader = DataLoader(eval_set, batch_size=params.batch_size, num_workers=params.num_gpus * 8, shuffle=False) # 模型 state_dict = torch.load(checkpoint) model = Backbone() model.load_state_dict(state_dict) model = model.cuda() # 损失 criterion = SumSquareError() model.eval() total_loss = 0 with torch.no_grad(): for iter, (img, annotation) in enumerate(eval_loader): img = img.cuda() annotation = annotation.cuda() output = model(img) loss = criterion(output, annotation).item() total_loss += loss * len(img) print(f'evaluate loss: {total_loss / len(eval_set)}')
def normalize(image_data, training_dir, train=True): '''Normalizes the data set by removing specified mean and diving by specified standard deviation. Inputs: image_data : np array of shape (#images,side,side,channels) containing the images. training_dir : the training set directory's path. train : whether we are normalizing the training set or another set. Returns: image_data : the normalized data as a np array of the same shape. ''' n = image_data.shape[0] if (train): # Normalizaing training set mean = np.mean(image_data) std = np.std(image_data) stat = {'mean': mean, 'std': std} save_annotation(stat, os.path.join(training_dir, 'mean.p')) elif (os.path.exists(os.path.join(training_dir, 'mean.p'))): # Normalizing another set with respect to the training set stat = load_annotation(os.path.join(training_dir, 'mean.p')) mean = stat['mean'] std = stat['std'] else: raise ValueError( 'Missing file for training mean and standard deviation.') for i in range(n): image_data[i] = (image_data[i] - mean) / std return image_data
def process(self, img_path): img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) img = transform(img, img_w=img_w, img_h=img_h) img = img.astype(np.float32) img = (img / 255.0) * 2.0 - 1.0 text = load_annotation(img_path) labels = text_to_labels(text) return img, labels, len(labels)
def eval_image(img_fn, crnn): img = cv2.imread(img_fn) true_text = load_annotation(img_fn) ts_text = ts.image_to_string(img, config=("-l eng --oem 1 --psm 8")) crnn_text = process_image(crnn.model, [img])[0] return { 'true_text': true_text, 'tesseract': ts_text, 'crnn': crnn_text, }
def run_test(wav_dir, csv_dir, buffer_size, log_file): ''' Run test function: input: - wav_dir: Location of the audio - csv_dir: Location of the csv annotation - buffer_size: Default is 512 but it can be modified to test the system on different buffer sizes - log_file: Location of the file where all results are logged. ''' # Load audio and its annotation print(wav_dir) audio = Waveform(path=wav_dir) groundtruth_annotation = load_annotation(csv_dir) # Init system simulation init_pre_processing() init_activity_detection(func_type=1) init_feature_extraction(by_pass=True) init_classificator(by_pass=True) # run simulation result = main(audio, buffer_size) # Init groundtruth activity array groundtruth_activity = np.zeros(len(result['ONSET_LOCATIONS'])) sample_rate = audio.sample_rate # Transform annotation in the desired format (1 activity, 0 non-activity) for i in range(0, len(groundtruth_annotation), 2): sample_instant_1 = int( float(groundtruth_annotation[i][0]) * sample_rate) sample_instant_2 = int( float(groundtruth_annotation[i + 1][0]) * sample_rate) groundtruth_activity[sample_instant_1:sample_instant_2] = 1 # evaluate activity detection precision, recall, f1_score, accuracy = evaluate_activity_detection( groundtruth_activity, result['ONSET_LOCATIONS']) row = [wav_dir, precision, recall, f1_score, accuracy] with open(log_file, 'a+', newline='') as file: w = csv.writer(file) w.writerow(row) file.close()
def _main(args): # Raw arguments from parser data_path = args.data_path # dictionary of the organs and their index in the classes.txt file label_dict = {'bladder' : 0, 'rectum' : 1, 'prostate' : 2} # Environment variables sets = [x for x in ['train','val','test'] if x in os.listdir(data_path)] if len(sets) == 0: raise ValueError('No set to extract the data from.') for set in sets: dataset_dir = os.path.join(data_path,set) annotation = 'annotations_'+set+'.p' filename = 'pelvis_data_'+set+'.npz' dict = load_annotation(os.path.join(dataset_dir,annotation)) yolo_dataset(dict,dataset_dir,filename,label_dict)
def k_mean_clustering(k, data_path, S, input_size, display=False): dict = load_annotation(data_path) h = [] w = [] for file in dict.keys(): for organ in dict[file]['bb'].keys(): box = dict[file]['bb'][organ] if box is not None and file.startswith('charleroi'): assert len(box) == 4 h.append(box[2] - box[0]) w.append(box[3] - box[1]) # data df = pd.DataFrame({'x': w, 'y': h}) # fit kmeans = KMeans(n_clusters=k) kmeans.fit(df) # learn the labels labels = kmeans.predict(df) centroids = kmeans.cluster_centers_ # display if (display): colmap = {1: 'r', 2: 'g', 3: 'b', 4: 'y', 5: 'm'} fig = plt.figure(figsize=(5, 5)) colors = list(map(lambda x: colmap[x + 1], labels)) plt.scatter(df['x'], df['y'], color=colors, alpha=0.5, edgecolor='k') for idx, centroid in enumerate(centroids): plt.scatter(*centroid, color=colmap[idx + 1], label='cluster ' + str(idx)) plt.xlim(0, 130) plt.ylim(0, 120) plt.legend() plt.ylabel('height (pixels)') plt.xlabel('width (pixels)') plt.show() return (centroids / input_size) * S
continue image = cv2.resize(image, image_size, image) image_channels = cv2.split(image) for channel_idx, channel in enumerate(image_channels): np.copyto(net.blobs["data"].data[0, channel_idx, :, :], channel) # image = np.dstack(cv2.split(image)) # np.copyto(net.blobs["data"].data, image) # net.blobs["data"].data = image output_blobs = net.forward(end="conv1", blobs=["conv1", ]) channels_num = output_blobs["conv1"].shape[1] channels = [output_blobs["conv1"][0, i, :, :] for i in range(channels_num)] features = cv2.merge(channels) output_dir = join(args.features_dir, "positives" if label else "negatives") if not isdir(output_dir): mkdir(output_dir) feature_map_path = join(output_dir, splitext(basename(image_path))[0] + ".pkl") pkl.dump(features, file(feature_map_path, "w")) stop_progress_bar() image_size = (64, 64) print("reading annotation...") annotation = load_annotation(args.annotation) caffe.set_mode_gpu() net = caffe.Net(args.net_proto, args.net_caffemodel, caffe.TEST) net.blobs["data"].reshape(1, 3, image_size[0], image_size[1]) net.reshape() extract_features(annotation, image_size=image_size)
def train(params, _run=None): params = Params(params) set_random_seeds(params.seed) time_now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") params.save_root = params.save_root + f'/{params.project_name}_{time_now}_{params.version}' os.makedirs(params.save_root, exist_ok=True) logging.basicConfig(filename=f'{params.save_root}/{params.project_name}_{time_now}_{params.version}.log', filemode='a', format='%{asctime}s - %(levalname)s: %(message)s') if params.num_gpus == 0: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' logging.info(f'Available GPUs: {torch.cuda.device_count()}') train2007, train_label_2007, train_bb_2007 = load_annotation(os.path.join(params.data_root, 'VOC2007'), 'trainval') test2007, test_label_2007, test_bb_2007 = load_annotation(os.path.join(params.data_root, 'VOC2007'), 'test') train2012, train_label_2012, train_bb_2012 = load_annotation(os.path.join(params.data_root, 'VOC2012'), 'trainval') test2012, test_label_2012, test_bb_2012 = load_annotation(os.path.join(params.data_root, 'VOC2012'), 'test') train_data = train2007+test2007+train2012 train_label = train_label_2007+test_label_2007+train_label_2012 train_bb = train_bb_2007 + test_bb_2007 + train_bb_2012 test_data = test2012 test_label = test_label_2012 test_bb = test_bb_2012 train_dataset = YoloDataset(train_data, train_bb, train_label, params, train=True) eval_dataset = YoloDataset(test_data, test_bb, test_label, params, train=False) train_loader = DataLoader(dataset=train_dataset, num_workers=params.num_gpus*8, batch_size=params.batch_size, shuffle=True, drop_last=True, pin_memory=True) eval_loader = DataLoader(dataset=eval_dataset, num_workers=1, batch_size=1, shuffle=False, pin_memory=True) model = Backbone() last_step = 0 last_epoch = 0 if params.num_gpus > 0: model = model.cuda() if params.num_gpus > 1: model = nn.DataParallel(model) if params.optim == 'Adam': optimizer = torch.optim.Adam(model.parameters(), lr=params.learning_rate) else: optimizer = torch.optim.SGD(model.parameters(), lr=params.learning_rate, momentum=0.9, nesterov=True, weight_decay=0.0005) criterion = SumSquareError() schedule = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, factor=0.5, verbose=True, patience=10) epoch = 0 begin_epoch = max(0, last_epoch) step = max(0, last_step) best_loss = 1e6 logging.info('Begin to train...') model.train() import cv2 as cv try: for epoch in range(begin_epoch, params.epoch): for iter, (img, annotation) in enumerate(train_loader): output = model(img.cuda()) loss = criterion(output, annotation.cuda()) optimizer.zero_grad() loss.backward() optimizer.step() if iter % params.save_interval == 0: logging.info(f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ' f'Train Epoch: {epoch} iter: {iter} loss: {loss.item()}') step += 1 if epoch % params.eval_interval == 0: model.eval() epoch_loss = 0 with torch.no_grad(): for iter, (img, annotation) in enumerate(eval_loader): output = model(img.cuda()) loss = criterion(output, annotation.cuda()).item() epoch_loss += loss * len(img) loss = epoch_loss / len(eval_dataset) logging.info(f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ' f'Eval Epoch: {epoch} loss: {loss}') schedule.step(loss) if loss < best_loss: best_loss = loss save_checkpoint(model, f'{params.save_root}/{epoch}_{step}.pth') model.train() except KeyboardInterrupt: save_checkpoint(model, f'{params.save_root}/Interrupt_{epoch}_{step}.pth')
parser.add_argument("--levels", default=2, type=int, help="") parser.add_argument("--out", default="/tmp", help="") parser.add_argument("--fe", default="rf", help="") parser.add_argument("--classifier", default="gbt", help="") parser.add_argument("--onehot", action="store_true", help="") parser.add_argument("--njobs", default=8, type=int, help="") parser.add_argument("--init", default="patches", help="patches or hogs or load") parser.add_argument("--image_size", default="32,32", help="initial patch size") args = parser.parse_args() print(" ".join(sys.argv)) print("reading train annotation...") annotation_train = load_annotation(args.annotation_train) print("reading test annotation...") annotation_test = load_annotation(args.annotation_test) def extract_patches_2d(image, patch_size, patch_stride=1, max_patches=None, random_state=None, reshape=True): from sklearn.utils.validation import check_array, check_random_state from sklearn.feature_extraction.image import extract_patches, _compute_n_patches i_h, i_w = image.shape[:2] p_h, p_w = patch_size
def evaluate(self, model, imgs, obj_threshold=0.3, nms_threshold=0.3, iou_threshold=0.5): """ # Arguments model : The model to evaluate. imgs : list of parsed test_img dictionaries. obj_threshold : The score confidence threshold to use for detections. nms_threshold : The threshold used to consider when a detection is positive or negative. # Returns A dict mapping class names to mAP scores. """ # gather all detections and annotations test_size = len(imgs) all_detections = [[None for i in range(self.n_class)] for j in range(test_size)] all_annotations = [[None for i in range(self.n_class)] for j in range(test_size)] ious = [] for i in range(test_size): image_name = imgs[i]['filename'] if '.jpg' not in image_name and '.png' not in image_name: image_name += '.jpg' raw_image = cv2.imread(image_name) raw_height, raw_width, raw_channels = raw_image.shape # make the boxes and the labels pred_boxes = self.predict(model, raw_image, obj_threshold, nms_threshold) score = np.array([box.score for box in pred_boxes]) pred_labels = np.array([box.label for box in pred_boxes]) if len(pred_boxes) > 0: pred_boxes = np.array([[ box.xmin * raw_width, box.ymin * raw_height, box.xmax * raw_width, box.ymax * raw_height, box.score ] for box in pred_boxes]) else: pred_boxes = np.array([[]]) # sort the boxes and the labels according to scores score_sort = np.argsort(-score) pred_labels = pred_labels[score_sort] pred_boxes = pred_boxes[score_sort] # copy detections to all_detections for label in range(self.n_class): all_detections[i][label] = pred_boxes[pred_labels == label, :] annotations = load_annotation(imgs, i, self.labels) # copy detections to all_annotations for label in range(self.n_class): all_annotations[i][label] = annotations[annotations[:, 4] == label, :4].copy() # compute mAP by comparing all detections and all annotations average_precisions = {} for label in range(self.n_class): false_positives = np.zeros((0, )) true_positives = np.zeros((0, )) scores = np.zeros((0, )) num_annotations = 0.0 for i in range(test_size): detections = all_detections[i][label] annotations = all_annotations[i][label] num_annotations += annotations.shape[0] detected_annotations = [] for d in detections: scores = np.append(scores, d[4]) if annotations.shape[0] == 0: false_positives = np.append(false_positives, 1) true_positives = np.append(true_positives, 0) continue overlaps = compute_overlap(np.expand_dims(d, axis=0), annotations) assigned_annotation = np.argmax(overlaps, axis=1) max_overlap = overlaps[0, assigned_annotation] ious.append(max_overlap) if max_overlap >= iou_threshold and assigned_annotation not in detected_annotations: false_positives = np.append(false_positives, 0) true_positives = np.append(true_positives, 1) detected_annotations.append(assigned_annotation) else: false_positives = np.append(false_positives, 1) true_positives = np.append(true_positives, 0) # no annotations -> AP for this class is 0 (is this correct?) if num_annotations == 0: average_precisions[label] = 0 continue # sort by score indices = np.argsort(-scores) false_positives = false_positives[indices] true_positives = true_positives[indices] # compute false positives and true positives false_positives = np.cumsum(false_positives) true_positives = np.cumsum(true_positives) # compute recall and precision recall = true_positives / num_annotations precision = true_positives / np.maximum( true_positives + false_positives, np.finfo(np.float64).eps) # compute average precision average_precision = compute_ap(recall, precision) average_precisions[label] = average_precision map_dict = {} # print evaluation for label, average_precision in average_precisions.items(): map_dict[self.labels[label]] = average_precision print(self.labels[label], '{:.4f}'.format(average_precision)) print('mAP: {:.4f}'.format( sum(average_precisions.values()) / len(average_precisions))) print('average IOU: {:.4f}'.format(np.mean(ious))) average_map = sum( average_precisions.values()) / len(average_precisions) return [average_map, map_dict, np.mean(ious)]
# image = np.dstack(cv2.split(image)) # np.copyto(net.blobs["data"].data, image) # net.blobs["data"].data = image output_blobs = net.forward(end="conv1", blobs=[ "conv1", ]) channels_num = output_blobs["conv1"].shape[1] channels = [ output_blobs["conv1"][0, i, :, :] for i in range(channels_num) ] features = cv2.merge(channels) output_dir = join(args.features_dir, "positives" if label else "negatives") if not isdir(output_dir): mkdir(output_dir) feature_map_path = join(output_dir, splitext(basename(image_path))[0] + ".pkl") pkl.dump(features, file(feature_map_path, "w")) stop_progress_bar() image_size = (64, 64) print("reading annotation...") annotation = load_annotation(args.annotation) caffe.set_mode_gpu() net = caffe.Net(args.net_proto, args.net_caffemodel, caffe.TEST) net.blobs["data"].reshape(1, 3, image_size[0], image_size[1]) net.reshape() extract_features(annotation, image_size=image_size)
def train_generator(data_path, model, model_body, training_generator, validation_generator, train_size, data_shape, class_names, anchors, config, weights_path=None, results_dir='results', gpu='3'): ''' Retrains/fine-tune the model with custom data generator. Saves the weights every 10 epochs for 1000 epochs. Inputs: model: the model as returned by the create_model function. training_generator: the generator object for training data. validation_generator: the generator object for validation data. train_size: the size of the training set. data_shape: the shape of the input data. class_names: the list contatining the class names. anchors: a np array containing the anchor boxes dimensions. config: a config object that has all the configuration parameters for the training. weights_path: the path to the weights that the models needs to load. None if we want to start from scratch. results_dir: the path to the directory where the results are going to be saved. ''' # Creating missing directories if not os.path.exists(os.path.join(results_dir, 'models')): os.makedirs(os.path.join(results_dir, 'models')) if not os.path.exists(os.path.join(results_dir, 'history')): os.makedirs(os.path.join(results_dir, 'history')) # Loading configuration parameters freeze_body = config.FREEZE learning_rate = config.LEARNING_RATE batch_size = config.BATCH_SIZE epochs = config.EPOCHS ###################################### ############# TRAINING ############### ###################################### # Defining metrics met = metric([ model.get_layer('conv2d_24').output, model.inputs[1], model.inputs[2], model.inputs[3] ], data_shape, anchors, len(class_names), score_threshold=0.07, iou_threshold=0.0) # Compile model and load weights_name optimizer = optimizers.Adam(lr=learning_rate) model.compile( optimizer=optimizer, loss={ 'yolo_loss': lambda y_true, y_pred: y_pred }, metrics=[ met.IoU, met.classification_loss, met.coord_loss, met.conf_loss ]) # This is a hack to use the custom loss function in the last layer if weights_path is not None: model.load_weights(weights_path) basename = os.path.basename(weights_path) load_stage = int(re.findall('\d+', basename)[0]) else: load_stage = -1 # Callbacks logging = TensorBoard() #checkpoint = ModelCheckpoint("trained_best.h5", monitor='val_loss',save_weights_only=True, save_best_only=True) #early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=15, verbose=1, mode='auto') # Initializing mean IoU if (load_stage == 0): mean_IoU = [0] else: mean_IoU = [] # Training for loop for stage in range(load_stage + 1, 100): # SLaunch training history = model.fit_generator(generator=training_generator, validation_data=validation_generator, steps_per_epoch=int( np.ceil(train_size / batch_size)), epochs=epochs, callbacks=[logging]) # Saving history and weights with open( os.path.join(results_dir, 'history', 'history' + str(stage) + '.p'), 'wb') as fp: pickle.dump(history.history, fp) model.save_weights( os.path.join(results_dir, 'models', 'trained_stage_' + str(stage) + '.h5')) # Call to predict function w = 'trained_stage_' + str(stage) + '.h5' dir_list = [ x for x in sorted(os.listdir(os.path.join(data_path, 'val'))) if x.endswith('.jpg') ] pred = predict(model_body, class_names, anchors, data_val[0], w, dir_list, non_best_sup=config.NON_BEST_SUP, results_dir=results_dir, save=False, training=True) # Call to evaluate function gt = load_annotation( os.path.join(data_path, 'val', 'annotations_val.p')) ratio = (config.INPUT_DIM[0] / config.OUTPUT_DIM[0]) iou_stats = IoU(gt, pred, ratio) # Appending new mean IoU to list mean_IoU.append( np.mean([item for key, item in iou_stats['mean'].items()])) print('average IoU update : {}'.format(mean_IoU)) # Safety load model.load_weights( os.path.join(results_dir, 'models', 'trained_stage_' + str(stage) + '.h5'))
parser.add_argument("--ntrees", default=10, type=int, help="") parser.add_argument("--depth", default=5, type=int, help="") parser.add_argument("--levels", default=2, type=int, help="") parser.add_argument("--out", default="/tmp", help="") parser.add_argument("--fe", default="rf", help="") parser.add_argument("--classifier", default="gbt", help="") parser.add_argument("--onehot", action="store_true", help="") parser.add_argument("--njobs", default=8, type=int, help="") parser.add_argument("--init", default="patches", help="patches or hogs or load") parser.add_argument("--image_size", default="32,32", help="initial patch size") args = parser.parse_args() print(" ".join(sys.argv)) print("reading train annotation...") annotation_train = load_annotation(args.annotation_train) print("reading test annotation...") annotation_test = load_annotation(args.annotation_test) def extract_patches_2d(image, patch_size, patch_stride=1, max_patches=None, random_state=None, reshape=True): from sklearn.utils.validation import check_array, check_random_state from sklearn.feature_extraction.image import extract_patches, _compute_n_patches i_h, i_w = image.shape[:2] p_h, p_w = patch_size if p_h > i_h: raise ValueError("Height of the patch should be less than the height" " of the image.") if p_w > i_w: