def plot_prediction(stage='test',name_file='_VHR_60_fake',out_file='VHR',name_model='UNet11',fold_out=0,fold_in=0,epochs=40, count=30): # #HR •dist loss_file = open(("predictions_{}/pred_loss_{}{}_{}_foldout{}_foldin{}_{}epochs.txt").format(out_file,stage,name_file,name_model,fold_out,fold_in,epochs)) filedata = loss_file.read() filedata = filedata.replace("bce",",bce") filedata = filedata.split(",") val_file = (("predictions_{}/inputs_{}{}_{}_foldout{}_foldin{}_{}epochs_{}.npy").format(out_file, stage, name_file,name_model,fold_out,fold_in,epochs, count)) pred_file =(("predictions_{}/pred_{}{}_{}_foldout{}_foldin{}_{}epochs_{}.npy").format(out_file, stage, name_file,name_model,fold_out,fold_in,epochs, count)) label_file = (("predictions_{}/labels_{}{}_{}_foldout{}_foldin{}_{}epochs_{}.npy").format(out_file, stage, name_file,name_model,fold_out,fold_in,epochs, count)) val_images = np.load(val_file) pred_images = np.load(pred_file) val_label = np.load(label_file) print(val_images.shape,val_label.shape,pred_images.shape) input_images_rgb = [helper.reverse_transform(x,out_file) for x in val_images[:95,0,:3,:,:]] #new metrics # Map each channel (i.e. class) to each color target_masks_rgb = [helper.masks_to_colorimg(x) for x in val_label[:95,0,:3,:,:]] pred_rgb = [helper.masks_to_colorimg(x) for x in pred_images[:95,0,:,:,:]] name_output=("{}{}_{}_foldout{}_foldin{}_{}epochs").format(stage, name_file,name_model,fold_out,fold_in,epochs) # stage + name_file + name_model+'_foldin' +str(fold_in) helper.plot_side_by_side([input_images_rgb, target_masks_rgb, pred_rgb],filedata, out_file, name_output, save=1)
import helper import simulation # Generate some random images input_images, target_masks = simulation.generate_random_data(192, 192, count=3) print(input_images.shape, target_masks.shape) # Change channel-order and make 3 channels for matplot input_images_rgb = [ (x.swapaxes(0, 2).swapaxes(0, 1) * -255 + 255).astype(np.uint8) for x in input_images ] # Map each channel (i.e. class) to each color target_masks_rgb = [helper.masks_to_colorimg(x) for x in target_masks] # Left: Input image, Right: Target mask helper.plot_side_by_side([input_images_rgb, target_masks_rgb]) #%% from torchvision import models base_model = models.resnet18(pretrained=True) def find_last_layer(layer): children = list(layer.children()) if len(children) == 0: return layer else:
import copy import math # Generate some random images input_images, target_masks = simulation.generate_random_data(192, 192, count=3) for x in [input_images, target_masks]: print(x.shape) print(x.min(), x.max()) # Change channel-order and make 3 channels for matplot input_images_rgb = [x.astype(np.uint8) for x in input_images] # Map each channel (i.e. class) to each color target_masks_rgb = [helper.masks_to_colorimg(x) for x in target_masks] # Left: Input image, Right: Target mask (Ground-truth) helper.plot_side_by_side([input_images_rgb, target_masks_rgb]) #plt.show() plt.clf() class SimDataset(Dataset): def __init__(self, count, transform=None): self.input_images, self.target_masks = simulation.generate_random_data( 192, 192, count=count) self.transform = transform def __len__(self): return len(self.input_images)
inputs, labels = next(data_iter) inputs = inputs.to(device) labels = labels.to(device) predictions = model(inputs) metrics = defaultdict(float) epoch_samples = inputs.size(0) loss = calc_loss(predictions, labels, metrics) print_metrics(metrics, epoch_samples, 'val') predictions = F.sigmoid(predictions) import helper input_images = [tensor_to_np(x) for x in inputs] mask_images = [helper.masks_to_colorimg(x) for x in labels] prediction_images = [helper.masks_to_colorimg(x) for x in predictions] chicken.display_all(input_images + mask_images + prediction_images) def pred_to_channels(single_pred): channel_images = [] # z = np.zeros(pix.shape) for pix in single_pred: z = np.zeros(pix.shape) channel = np.stack([pix, z, z], axis=2) channel_images.append(channel) return np.array(channel_images)