mykeysid10 commited on
Commit
e757fca
·
1 Parent(s): fd5ef50

Update cloud_coverage_pipeline.py

Browse files
Files changed (1) hide show
  1. cloud_coverage_pipeline.py +28 -34
cloud_coverage_pipeline.py CHANGED
@@ -1,16 +1,16 @@
1
  # Importing Libraries
2
- import torch, os
3
  import numpy as np
4
  import cv2
 
5
  from torch import nn
6
  import timm
 
7
  from transformers import DistilBertModel, DistilBertConfig
8
  from torch.utils.data import Dataset, DataLoader
9
  from tqdm.autonotebook import tqdm
10
- import pickle
11
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
12
 
13
-
14
  # Trained Model Configurations
15
  CFG = {
16
  "debug": False,
@@ -40,44 +40,40 @@ CFG = {
40
  "dropout": 0.1
41
  }
42
 
43
-
44
  # Loading Finetuned Clip Model to the below class format
45
  class CLIPModel(nn.Module):
46
  def __init__(
47
  self,
48
- temperature=CFG["temperature"],
49
- image_embedding=CFG["image_embedding"],
50
- text_embedding=CFG["text_embedding"],
51
  ):
52
  super().__init__()
53
  self.image_encoder = ImageEncoder()
54
  self.text_encoder = TextEncoder()
55
- self.image_projection = ProjectionHead(embedding_dim=image_embedding)
56
- self.text_projection = ProjectionHead(embedding_dim=text_embedding)
57
  self.temperature = temperature
58
 
59
-
60
  # Image Encoder Class to extract features using finetuned clip's Resnet Image Encoder
61
  class ImageEncoder(nn.Module):
62
- def __init__(self, model_name=CFG["model_name"], pretrained=CFG["pretrained"], trainable=CFG["trainable"]):
63
  super().__init__()
64
- self.model = timm.create_model(model_name, pretrained, num_classes=0, global_pool="avg")
65
  for p in self.model.parameters():
66
  p.requires_grad = trainable
67
 
68
  def forward(self, x):
69
  return self.model(x)
70
 
71
-
72
  # Text Encoder - Optional in inference
73
  class TextEncoder(nn.Module):
74
- def __init__(self, model_name=CFG["text_encoder_model"], pretrained=CFG["pretrained"],
75
- trainable=CFG["trainable"]):
76
  super().__init__()
77
  if pretrained:
78
  self.model = DistilBertModel.from_pretrained(model_name)
79
  else:
80
- self.model = DistilBertModel(config=DistilBertConfig())
81
 
82
  for p in self.model.parameters():
83
  p.requires_grad = trainable
@@ -85,18 +81,17 @@ class TextEncoder(nn.Module):
85
  self.target_token_idx = 0
86
 
87
  def forward(self, input_ids, attention_mask):
88
- output = self.model(input_ids=input_ids, attention_mask=attention_mask)
89
  last_hidden_state = output.last_hidden_state
90
  return last_hidden_state[:, self.target_token_idx, :]
91
 
92
-
93
  # Projection Class - Optional in inference
94
  class ProjectionHead(nn.Module):
95
  def __init__(
96
  self,
97
  embedding_dim,
98
- projection_dim=CFG["projection_dim"],
99
- dropout=CFG["dropout"]
100
  ):
101
  super().__init__()
102
  self.projection = nn.Linear(embedding_dim, projection_dim)
@@ -114,7 +109,6 @@ class ProjectionHead(nn.Module):
114
  x = self.layer_norm(x)
115
  return x
116
 
117
-
118
  # Class to transform image to custom data format
119
  class SkyImage(Dataset):
120
  def __init__(self, img, label):
@@ -126,34 +120,34 @@ class SkyImage(Dataset):
126
 
127
  def __getitem__(self, idx):
128
  image = cv2.resize(self.img[idx], (244, 244))
129
- # image = cv2.cvtColor(self.img[idx], cv2.COLOR_BGR2RGB)
130
- # image = cv2.resize(image, (244, 244))
131
  image = np.moveaxis(image, -1, 0)
132
  label = self.img_label[idx]
133
  return image, label
134
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  # Method to extract features from finetuned clip model
137
  def get_features(clip_model, dataset):
138
  features, label, embeddings = [], [], []
139
  with torch.no_grad():
140
- for images, labels in tqdm(DataLoader(dataset, batch_size=64)):
141
  image_input = torch.tensor(np.stack(images)).cpu().float()
142
  image_features = clip_model.image_encoder(image_input)
143
  features.append(image_features)
144
  label.append(labels)
145
  return torch.cat(features), torch.cat(label).cpu()
146
 
147
-
148
- # Loading Clip and Catboost models
149
- CTBR_model = pickle.load(open("catboost_model.sav", 'rb'))
150
- clip_model = CLIPModel().to(CFG["device"])
151
- clip_model.load_state_dict(torch.load("clip_model.pt", map_location=CFG["device"]))
152
- clip_model.eval()
153
-
154
-
155
  # Method to calculate cloud coverage
156
- def predict_cloud_coverage(image):
157
  img, lbl = [image], [0]
158
  # Transforming Data into custom format
159
  test_image = SkyImage(img, lbl)
@@ -161,4 +155,4 @@ def predict_cloud_coverage(image):
161
  features, label = get_features(clip_model, test_image)
162
  # Predicting Cloud Coverage based on extracted features
163
  pred_cloud_coverage = CTBR_model.predict(features.cpu().numpy())
164
- return(round(max(0.0, min(100.0, pred_cloud_coverage[0])), 1))
 
1
  # Importing Libraries
2
+ import os
3
  import numpy as np
4
  import cv2
5
+ import torch
6
  from torch import nn
7
  import timm
8
+ import pickle
9
  from transformers import DistilBertModel, DistilBertConfig
10
  from torch.utils.data import Dataset, DataLoader
11
  from tqdm.autonotebook import tqdm
 
12
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
13
 
 
14
  # Trained Model Configurations
15
  CFG = {
16
  "debug": False,
 
40
  "dropout": 0.1
41
  }
42
 
 
43
  # Loading Finetuned Clip Model to the below class format
44
  class CLIPModel(nn.Module):
45
  def __init__(
46
  self,
47
+ temperature = CFG["temperature"],
48
+ image_embedding = CFG["image_embedding"],
49
+ text_embedding = CFG["text_embedding"],
50
  ):
51
  super().__init__()
52
  self.image_encoder = ImageEncoder()
53
  self.text_encoder = TextEncoder()
54
+ self.image_projection = ProjectionHead(embedding_dim = image_embedding)
55
+ self.text_projection = ProjectionHead(embedding_dim = text_embedding)
56
  self.temperature = temperature
57
 
 
58
  # Image Encoder Class to extract features using finetuned clip's Resnet Image Encoder
59
  class ImageEncoder(nn.Module):
60
+ def __init__(self, model_name = CFG["model_name"], pretrained = CFG["pretrained"], trainable = CFG["trainable"]):
61
  super().__init__()
62
+ self.model = timm.create_model(model_name, pretrained, num_classes = 0, global_pool = "avg")
63
  for p in self.model.parameters():
64
  p.requires_grad = trainable
65
 
66
  def forward(self, x):
67
  return self.model(x)
68
 
 
69
  # Text Encoder - Optional in inference
70
  class TextEncoder(nn.Module):
71
+ def __init__(self, model_name = CFG["text_encoder_model"], pretrained = CFG["pretrained"], trainable = CFG["trainable"]):
 
72
  super().__init__()
73
  if pretrained:
74
  self.model = DistilBertModel.from_pretrained(model_name)
75
  else:
76
+ self.model = DistilBertModel(config = DistilBertConfig())
77
 
78
  for p in self.model.parameters():
79
  p.requires_grad = trainable
 
81
  self.target_token_idx = 0
82
 
83
  def forward(self, input_ids, attention_mask):
84
+ output = self.model(input_ids = input_ids, attention_mask = attention_mask)
85
  last_hidden_state = output.last_hidden_state
86
  return last_hidden_state[:, self.target_token_idx, :]
87
 
 
88
  # Projection Class - Optional in inference
89
  class ProjectionHead(nn.Module):
90
  def __init__(
91
  self,
92
  embedding_dim,
93
+ projection_dim = CFG["projection_dim"],
94
+ dropout = CFG["dropout"]
95
  ):
96
  super().__init__()
97
  self.projection = nn.Linear(embedding_dim, projection_dim)
 
109
  x = self.layer_norm(x)
110
  return x
111
 
 
112
  # Class to transform image to custom data format
113
  class SkyImage(Dataset):
114
  def __init__(self, img, label):
 
120
 
121
  def __getitem__(self, idx):
122
  image = cv2.resize(self.img[idx], (244, 244))
 
 
123
  image = np.moveaxis(image, -1, 0)
124
  label = self.img_label[idx]
125
  return image, label
126
 
127
+ # Method to initialize CatBoost model
128
+ def initialize_catboost_model():
129
+ return pickle.load(open("catboost_model.sav", 'rb'))
130
+
131
+ # Method to initialize CLIP model
132
+ def initialize_clip_model():
133
+ clip_model = CLIPModel().to(CFG["device"])
134
+ clip_model.load_state_dict(torch.load("clip_model.pt", map_location = CFG["device"]))
135
+ clip_model.eval()
136
+ return clip_model
137
 
138
  # Method to extract features from finetuned clip model
139
  def get_features(clip_model, dataset):
140
  features, label, embeddings = [], [], []
141
  with torch.no_grad():
142
+ for images, labels in tqdm(DataLoader(dataset, batch_size = 64)):
143
  image_input = torch.tensor(np.stack(images)).cpu().float()
144
  image_features = clip_model.image_encoder(image_input)
145
  features.append(image_features)
146
  label.append(labels)
147
  return torch.cat(features), torch.cat(label).cpu()
148
 
 
 
 
 
 
 
 
 
149
  # Method to calculate cloud coverage
150
+ def predict_cloud_coverage(image, clip_model, CTBR_model):
151
  img, lbl = [image], [0]
152
  # Transforming Data into custom format
153
  test_image = SkyImage(img, lbl)
 
155
  features, label = get_features(clip_model, test_image)
156
  # Predicting Cloud Coverage based on extracted features
157
  pred_cloud_coverage = CTBR_model.predict(features.cpu().numpy())
158
+ return round(max(0.0, min(100.0, pred_cloud_coverage[0])), 1)