import os import sys import tensorflow as tf from tensorflow.keras.callbacks import ModelCheckpoint root_path = os.path.abspath(os.path.join('..')) if root_path not in sys.path: sys.path.append(root_path) import config from birds_loader import load_birds, build_dataset from ssd_utils.ssd_loss import SSDLoss from utils import import_by_name, train_test_split_tensors, MeanAveragePrecisionCallback # Load train and validation data train_image_paths, train_bnd_boxes = load_birds(split='train') valid_image_paths, valid_bnd_boxes = load_birds(split='valid') valid_data = build_dataset(valid_image_paths, valid_bnd_boxes, image_size=config.IMAGE_SIZE, batch_size=config.BATCH_SIZE) for run in range(1, config.NUM_RUNS+1): weights_dir = 'weights_{}'.format(run) history_dir = 'history_{}'.format(run) os.makedirs(weights_dir, exist_ok=True) os.makedirs(history_dir, exist_ok=True) for architecture in config.ARCHITECTURES: model_class = import_by_name('ssd_utils.networks.' + architecture)
import pandas as pd import os import sys import tensorflow as tf root_path = os.path.abspath('..') if root_path not in sys.path: sys.path.append(root_path) import config_naive_pasting_ablation as config from birds_loader import load_birds, build_dataset from ssd_utils import output_encoder from ssd_utils.metrics import MeanAveragePrecision from utils import import_by_name, MeanAveragePrecisionCallback test_image_paths, test_bnd_boxes = load_birds(split='test') test_data = build_dataset(test_image_paths, test_bnd_boxes, image_size=config.IMAGE_SIZE, batch_size=config.BATCH_SIZE) meanAP_metric = MeanAveragePrecision() results = {'architecture': []} for run in range(1, config.NUM_RUNS + 1): results['run_{}'.format(run)] = [] for architecture in config.ARCHITECTURES: model_class = import_by_name('ssd_utils.networks.' + architecture) model = model_class(num_classes=len(config.CLASSES))
states = torch.load('cub_nets_state.pth') opt = states['options'] MASK_SIZE = opt.sizex if "netEncM" in states: netEncM = models._netEncM(sizex=opt.sizex, nIn=opt.nx, nMasks=opt.nMasks, nRes=opt.nResM, nf=opt.nfM, temperature=opt.temperature).to(device) netEncM.load_state_dict(states["netEncM"]) netEncM.eval() real_image_paths, bbox_crops = load_birds(root=REAL_OBJECTS_DIR, split='train') def sample_object(): obj_idx = np.random.choice(len(real_image_paths)) image_path, bbox = real_image_paths[obj_idx], bbox_crops[obj_idx][0] image = Image.open(image_path.numpy()) image = image.crop(bbox.numpy()[:-1]) image = image.resize((MASK_SIZE, MASK_SIZE)) image = np.array(image) if image.ndim != 3: return sample_object() return image def calculate_mask(obj): obj_t = obj.transpose(2, 0, 1)