Sophie98 commited on
Commit
7ea2648
1 Parent(s): 14d0912

projection only works with queue

Browse files
Segmentation/segmentation.py CHANGED
@@ -1,39 +1,33 @@
1
- # Import libraries
2
-
3
  import cv2
4
  from tensorflow import keras
5
  import numpy as np
6
- import matplotlib.pyplot as plt
7
  from PIL import Image
8
  import segmentation_models as sm
9
  sm.set_framework('tf.keras')
10
 
 
11
  model_path = "Segmentation/model_checkpoint.h5"
12
  CLASSES = ['sofa']
13
  BACKBONE = 'resnet50'
14
 
15
- # define network parameters
16
- n_classes = 1 if len(CLASSES) == 1 else (len(CLASSES) + 1) # case for binary and multiclass segmentation
17
  activation = 'sigmoid' if n_classes == 1 else 'softmax'
18
  preprocess_input = sm.get_preprocessing(BACKBONE)
19
  LR=0.0001
20
-
21
  #create model architecture
22
  model = sm.Unet(BACKBONE, classes=n_classes, activation=activation)
23
  # define optomizer
24
  optim = keras.optimizers.Adam(LR)
25
- # Segmentation models losses can be combined together by '+' and scaled by integer or float factor
26
  dice_loss = sm.losses.DiceLoss()
27
  focal_loss = sm.losses.BinaryFocalLoss() if n_classes == 1 else sm.losses.CategoricalFocalLoss()
28
  total_loss = dice_loss + (1 * focal_loss)
29
- # actulally total_loss can be imported directly from library, above example just show you how to manipulate with losses
30
- # total_loss = sm.losses.binary_focal_dice_loss # or sm.losses.categorical_focal_dice_loss
31
  metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]
32
  # compile keras model with defined optimozer, loss and metrics
33
  model.compile(optim, total_loss, metrics)
34
  model.load_weights(model_path)
35
 
36
- def get_mask(image:Image) -> Image:
37
  """
38
  This function generates a mask of the image that highlights all the sofas in the image.
39
  This uses a pre-trained Unet model with a resnet50 backbone.
@@ -57,7 +51,7 @@ def get_mask(image:Image) -> Image:
57
  mask = Image.fromarray(prediction[...,0].squeeze()*255).convert("L")
58
  return mask
59
 
60
- def replace_sofa(image:Image, mask:Image, styled_sofa:Image) -> Image:
61
  """
62
  This function replaces the original sofa in the image by the new styled sofa according
63
  to the mask.
@@ -77,12 +71,3 @@ def replace_sofa(image:Image, mask:Image, styled_sofa:Image) -> Image:
77
  sofa_fg = cv2.bitwise_and(styled_sofa,styled_sofa,mask = mask)
78
  new_image = cv2.add(image_bg,sofa_fg)
79
  return Image.fromarray(new_image)
80
-
81
- # image = cv2.imread('input/sofa.jpg')
82
- # mask = cv2.imread('masks/sofa.jpg')
83
- # styled_sofa = cv2.imread('output/sofa_stylized_style.jpg')
84
-
85
- # #get_mask(image)
86
-
87
- # plt.imshow(replace_sofa(image,mask,styled_sofa))
88
- # plt.show()
 
 
 
1
  import cv2
2
  from tensorflow import keras
3
  import numpy as np
 
4
  from PIL import Image
5
  import segmentation_models as sm
6
  sm.set_framework('tf.keras')
7
 
8
+ # Load model at build time
9
  model_path = "Segmentation/model_checkpoint.h5"
10
  CLASSES = ['sofa']
11
  BACKBONE = 'resnet50'
12
 
13
+ # define network parameters (only neede to load the weights)
14
+ n_classes = 1 if len(CLASSES) == 1 else (len(CLASSES) + 1)
15
  activation = 'sigmoid' if n_classes == 1 else 'softmax'
16
  preprocess_input = sm.get_preprocessing(BACKBONE)
17
  LR=0.0001
 
18
  #create model architecture
19
  model = sm.Unet(BACKBONE, classes=n_classes, activation=activation)
20
  # define optomizer
21
  optim = keras.optimizers.Adam(LR)
 
22
  dice_loss = sm.losses.DiceLoss()
23
  focal_loss = sm.losses.BinaryFocalLoss() if n_classes == 1 else sm.losses.CategoricalFocalLoss()
24
  total_loss = dice_loss + (1 * focal_loss)
 
 
25
  metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]
26
  # compile keras model with defined optimozer, loss and metrics
27
  model.compile(optim, total_loss, metrics)
28
  model.load_weights(model_path)
29
 
30
+ def get_mask(image:Image.Image) -> Image.Image:
31
  """
32
  This function generates a mask of the image that highlights all the sofas in the image.
33
  This uses a pre-trained Unet model with a resnet50 backbone.
 
51
  mask = Image.fromarray(prediction[...,0].squeeze()*255).convert("L")
52
  return mask
53
 
54
+ def replace_sofa(image:Image.Image, mask:Image.Image, styled_sofa:Image.Image) -> Image.Image:
55
  """
56
  This function replaces the original sofa in the image by the new styled sofa according
57
  to the mask.
 
71
  sofa_fg = cv2.bitwise_and(styled_sofa,styled_sofa,mask = mask)
72
  new_image = cv2.add(image_bg,sofa_fg)
73
  return Image.fromarray(new_image)
 
 
 
 
 
 
 
 
 
StyleTransfer/styleTransfer.py CHANGED
@@ -1,6 +1,7 @@
1
  from PIL import Image
2
  import numpy as np
3
  import torch
 
4
  import torch.nn as nn
5
  from torchvision import transforms
6
  import StyleTransfer.transformer as transformer
 
1
  from PIL import Image
2
  import numpy as np
3
  import torch
4
+ torch.cuda.is_available()
5
  import torch.nn as nn
6
  from torchvision import transforms
7
  import StyleTransfer.transformer as transformer