def __init__(self, image_shape, numClasses, palette, pretrained_unet=None, pretrained_fcn=None, pretrained_segnet=None, unet=False, fcn=False, segnet=False, channels=None): if channels is None: channels = [3, 3] # model selection self.unetchoice = unet self.fcnchoice = fcn self.segnetchoice = segnet # model parameters self.input_shape = (image_shape[0], image_shape[1], channels[0]) self.num_of_classes = numClasses self.palette = palette if unet: if not pretrained_unet: raise ValueError("Need Weights to Evaluate Model") else: self.unet = UNET.build(self.input_shape, self.num_of_classes, pretrained_weights=pretrained_unet) if fcn: if not pretrained_fcn: raise ValueError("Need Weights to Evaluate Model") else: self.fcn = FCN.build(self.input_shape, self.num_of_classes, pretrained_weights=pretrained_fcn) if segnet: if not pretrained_segnet: raise ValueError("Need Weights to Evaluate Model") else: self.segnet = SegNet.build(self.input_shape, self.num_of_classes, pretrained_weights=pretrained_segnet)
def __init__(self, image_shape, numClasses, pretrained_weights, palette, channels=None): """ :param image_shape: input image shape :param numClasses: number of classes in segmentation network :param pretrained_weights: pretrained weights for generator model (Not Optional) :param palette: colour palette for interpreting probability maps :param channels: number of channels in image and ground truth label maps, default=[3, 3] """ if channels is None: channels = [3, 3] if pretrained_weights is None: raise ValueError('The generator model must be pre-trained!!') self.numOfClasses = numClasses self.pretrained_weights = pretrained_weights self.palette = palette # Training Parameters for adversarial (From Journal) self.INIT_LR = 0.0000001 self.weight = 4 # Learning parameters for segmentor self.INIT_LR_Seg = 0.0001 # model parameters self.seg_shape = (image_shape[0], image_shape[1], numClasses) self.input_shape = (image_shape[0], image_shape[1], channels[0]) # optimizers opt_dis = Adam(lr=self.INIT_LR) opt_gen = Adam(lr=self.INIT_LR_Seg) # Build Generator self.generator = FCN.build(self.input_shape, self.numOfClasses, pretrained_weights=pretrained_weights) self.generator.summary() # Build and Compile Discriminator self.discriminator = Stanford_Adversarial.build(self.input_shape, self.seg_shape) self.discriminator.trainable = True self.discriminator.compile(optimizer=opt_dis, loss='binary_crossentropy', metrics=['acc']) self.discriminator.summary() # Define Composite (Segmentor -> Adversarial) Model self.composite = self._define_composite(self.INIT_LR_Seg) # Compile Segmentor self.generator.trainable = True self.generator.compile(optimizer=opt_gen, loss='categorical_crossentropy', metrics=['acc'])
from evalution_segmentaion import eval_semantic_segmentation from Models import FCN import cfg device = t.device('cuda') if t.cuda.is_available() else t.device('cpu') num_class = cfg.DATASET[1] Load_train = LoadDataset([cfg.TRAIN_ROOT, cfg.TRAIN_LABEL], cfg.crop_size) Load_val = LoadDataset([cfg.VAL_ROOT, cfg.VAL_LABEL], cfg.crop_size) train_data = DataLoader(Load_train, batch_size=cfg.BATCH_SIZE, shuffle=True, num_workers=1) val_data = DataLoader(Load_val, batch_size=cfg.BATCH_SIZE, shuffle=True, num_workers=1) fcn = FCN.FCN(num_class) fcn = fcn.to(device) criterion = nn.NLLLoss().to(device) optimizer = optim.Adam(fcn.parameters(), lr=1e-4) def train(model): best = [0] net = model.train() # 训练轮次 for epoch in range(cfg.EPOCH_NUMBER): print('Epoch is [{}/{}]'.format(epoch + 1, cfg.EPOCH_NUMBER)) if epoch % 50 == 0 and epoch != 0: for group in optimizer.param_groups: group['lr'] *= 0.5
output = image.copy() # import colour palette df = pd.read_csv('classes.csv', ",", header=None) palette = np.array(df.values, dtype=np.float32) image = cv2.resize(image, (512, 512)) image = image.astype("float32") / 255 # add batch dimension image = image.reshape((1, image.shape[0], image.shape[1], 3)) # load model print("[INFO] Loading Model............") model = FCN.build((512, 512, 3), 29, pretrained_weights='FCN_final/weights_CS_FCN.h5') # model = load_model(args["model"], custom_objects={'dice_coef': dice_coef, 'mean_iou': mean_iou, # 'mean_iou_2': mean_iou_2},) # make a prediction on the image preds = model.predict(image) # collapse class probabilities to label map preds = preds.reshape((preds.shape[1], preds.shape[2], preds.shape[3])) preds = preds.argmax(axis=-1) label_map = np.zeros((preds.shape[0], preds.shape[1], 3)).astype('float32') for ind in range(0, len(palette)): submat = np.where(preds == ind)
from Models import FCN import cfg device = t.device('cuda') if t.cuda.is_available() else t.device('cpu') num_class = cfg.DATASET[1] BATCH_SIZE = 4 miou_list = [0] Load_test = LoadDataset([cfg.TEST_ROOT, cfg.TEST_LABEL], cfg.crop_size) test_data = DataLoader(Load_test, batch_size=BATCH_SIZE, shuffle=False, num_workers=4) net = FCN.FCN(num_class) net.eval() net.to(device) net.load_state_dict(t.load("./Results/weights/xxx.pth")) train_acc = 0 train_miou = 0 train_class_acc = 0 train_mpa = 0 error = 0 for i, sample in enumerate(test_data): data = Variable(sample['img']).to(device) label = Variable(sample['label']).to(device) out = net(data) out = F.log_softmax(out, dim=1)
import numpy as np import torch as t import torch.nn.functional as F from torch.utils.data import DataLoader from PIL import Image from dataset import LoadDataset from Models import FCN import cfg device = t.device('cuda') if t.cuda.is_available() else t.device('cpu') num_class = cfg.DATASET[1] Load_test = LoadDataset([cfg.TEST_ROOT, cfg.TEST_LABEL], cfg.crop_size) test_data = DataLoader(Load_test, batch_size=1, shuffle=True, num_workers=4) net = FCN.FCN(num_class).to(device) net.load_state_dict(t.load("./Results/weights/xxx.pth")) net.eval() pd_label_color = pd.read_csv(cfg.class_dict_path, sep=',') name_value = pd_label_color['name'].values num_class = len(name_value) colormap = [] for i in range(num_class): tmp = pd_label_color.iloc[i] color = [tmp['r'], tmp['g'], tmp['b']] colormap.append(color) cm = np.array(colormap).astype('uint8') dir = "./Results/result_pics/"
image_size=input_size, numclasses=num_of_Classes, channels=[3, 3], palette=palette, seed=47) val_set = Dataloader(image_paths=val_frame_path, mask_paths=val_mask_path, image_size=input_size, numclasses=num_of_Classes, channels=[3, 3], palette=palette, seed=47) # Build Model model_FCN = FCN.build((352, 352, 3), num_of_Classes, pretrained_weights="FCN_weights.h5") model_FCN.summary() # learning parameters INIT_LR = 0.001 EPOCHS = 100 BS = 2 # initialize data generators traingen = threadsafe_iter(train_set.data_gen(should_augment=True, batch_size=BS)) valgen = threadsafe_iter(val_set.data_gen(should_augment=False, batch_size=BS)) # Print INFO No_of_train_images = len(os.listdir(dataset_path + '/train_frames/train')) No_of_val_images = len(os.listdir(dataset_path + '/val_frames/val')) print("Number of Training Images = {}".format(No_of_train_images))
numclasses=num_of_Classes, channels=[3, 3], palette=palette, seed=47) val_set = Dataloader(image_paths=val_frame_path, mask_paths=val_mask_path, image_size=input_size, numclasses=num_of_Classes, channels=[3, 3], palette=palette, seed=47) # build model model = FCN.build((352, 352, 3), num_of_Classes, pretrained_weights='output/weights_CS_FCN.h5') model.summary() # learning parameters BS = 2 INIT_LR = 0.00001 EPOCHS = 10 # initialize data generators traingen = train_set.data_gen(should_augment=True, batch_size=BS) valgen = val_set.data_gen(should_augment=False, batch_size=BS) # initialise variables No_of_train_images = len(os.listdir(dataset_path + '/train_frames/train')) No_of_val_images = len(os.listdir(dataset_path + '/val_frames/val'))