Help with modelling a multi-input trainer. #936
-
Help, I am attempting to create a Cutom task using the templates provided; sentinel2 rasters of size Here is my class: class CustomModelTask(ClassificationTask):
def config_task(self) -> None:
self.config_model()
if self.hyperparams["loss"] == "bce":
self.loss = nn.BCEWithLogitsLoss()
else:
raise ValueError(f"loss type '{self.hyperparams['loss']}' is not valid.")
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.input_features = 100
self.output_features = 100
self.classification_head = {"LAYER_COUNT": 3}
self.save_hyperparameters() # type: ignore[operator]
self.hyperparams = cast(Dict[str, Any], self.hparams)
self.config_task()
self.backbone = resnet50(pretrained=False)
def forward(self,x):
self.backbone.conv1 = torch.nn.Conv2d(in_channels=x.shape[1], out_channels = 64,
kernel_size=(7,7),
stride=(2,2),
bias=False)
self.backbone.fc = torch.nn.Linear(self.backbone.fc.in_features,100)
self.fc = torch.nn.Sequential(
*([
torch.nn.Sequential(
torch.nn.Linear(in_features=self.input_features,out_features=self.output_features),
torch.nn.ReLU()
)
for _ in range(self.classification_head.get("LAYER_COUNT", 5) - 1)
])
)
output = self.backbone(x)
self.label_prediction = torch.nn.Linear(400, 100)
self.label_count = torch.nn.Linear(400, 100)
return output
def training_step(self, *args: Any, **kwargs: Any)-> Tensor:
batch = args[0]
x1 = batch["s2_image"]
x2 = batch["lc_image"]
y = batch["label"]
output1 = self.forward(x1)
# output2 = self.forward(x2)
output1 = output1.flatten()
x2_flat = x2.flatten()
combined = torch.cat((output1, x2_flat), dim=0).to(torch.float32)
outputs = self.fc(combined)
y_hat = self.label_prediction(outputs)
label_count = self.label_count(outputs)
# TODO: Add loss function and everything else:
y_hat = y_hat.unsqueeze(0)
loss = self.loss(y_hat, y)
print(loss)
self.log("train_loss", loss, on_step = True, on_epoch=False)
self.train_metrics(y_hat, y)
return cast(Tensor, loss) I however run into mismatch errors of the size of the target vector vs the predicted vector; Question:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Personally, I would concatenate the (2, 13, 600, 600) Sentinel image with the (2, 1, 600, 600) esri land cover mask to get a single (2, 14, 600, 600) input image. Then I would pass this through a model like normal. So first create a dataset that does the concatenation, then use the normal builtin ClassificationTask trainer. |
Beta Was this translation helpful? Give feedback.
Personally, I would concatenate the (2, 13, 600, 600) Sentinel image with the (2, 1, 600, 600) esri land cover mask to get a single (2, 14, 600, 600) input image. Then I would pass this through a model like normal. So first create a dataset that does the concatenation, then use the normal builtin ClassificationTask trainer.