-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transfer learning y convolucional #2
Changes from 4 commits
49e57b6
06b9761
9a286fb
0824345
c30b306
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,31 +3,38 @@ | |
|
||
from PIL import Image | ||
from torch.utils.data import Dataset | ||
from torchvision import transforms | ||
|
||
from definitions import IMG_PATH | ||
|
||
|
||
|
||
class CatDogDataset(Dataset): | ||
def __init__(self, cat_dog_df, transforms=None, img_output_size=(500,500)): | ||
def __init__(self, cat_dog_df, transformations=None, feature_scaling=255, img_output_size=(500, 500), img_path="../data/images/"): | ||
self.files = (IMG_PATH + cat_dog_df["file"]).values | ||
self.width = cat_dog_df["width"].values | ||
self.height = cat_dog_df["height"].values | ||
self.target = np.where(cat_dog_df["class"].values == "cat", 1, 0).astype(np.float32) | ||
self.bbox = cat_dog_df[["xmin", "ymin", "xmax", "ymax"]].values.astype(np.float32) | ||
self.resizer = fn.Resize(img_output_size) | ||
self.resizer = transforms.Resize(img_output_size) | ||
|
||
self.to_tensor = transforms.ToTensor() | ||
self.normalizer = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. esto no es hardcode?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahi vi que se usa eso como estandar de pytorch. lo queremos usar para todos nuestros modelos, aun los que no fueron pre entrenados?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactamente por eso lo puse. Osea sí, mucho no me lo cuestioné. Podemos calcular esos valores con nuestro dataset y reemplazarlos, pero no me calienta mucho diría. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. de acuerdo |
||
self.scaling = feature_scaling | ||
|
||
|
||
self.transformations = transformations | ||
|
||
self.transforms = transforms | ||
|
||
def __len__(self): | ||
return len(self.files) | ||
|
||
def __getitem__(self, idx): | ||
resized_img = self.resizer(Image.open(self.files[idx]).convert("RGB")) | ||
np_img = np.asarray(resized_img) | ||
resized_img = self.resizer(self.to_tensor(Image.open(self.files[idx]).convert("RGB"))) | ||
img = self.normalizer(resized_img) | ||
|
||
bbox = self.bbox[idx] | ||
if self.transforms is not None: | ||
np_img, bbox = self.transforms(np_img, bbox) | ||
if self.transformations is not None: | ||
np_img, bbox = self.transformations(img, bbox) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aca tal vez queremos renombrar np_img?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De hecho sí, es un bug. Corrijo en próximo commit. |
||
|
||
torch_img = fn.functional.to_tensor(np_img) | ||
return torch_img, self.target[idx], bbox | ||
return img, self.target[idx], bbox |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import torch.nn | ||
from .base import CatDogClassifier, CatDogOutput | ||
from catdog.utils.image import appropiate_padding | ||
|
||
|
||
class ConvolutionalClassifier(CatDogClassifier): | ||
def __init__(self, input_shape, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
in_channels, current_height, current_width = input_shape | ||
conv1 = torch.nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=(3, 3), | ||
# padding=appropiate_padding((current_height, current_width), (3,3)) | ||
) | ||
conv2 = torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), | ||
# padding=appropiate_padding((current_height, current_width), (3,3)) | ||
) | ||
|
||
conv3 = torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), | ||
# padding=appropiate_padding((current_height, current_width), (3,3)) | ||
) | ||
|
||
self.model = torch.nn.Sequential(conv1, torch.nn.ReLU(), torch.nn.MaxPool2d((2, 2)), | ||
conv2, torch.nn.ReLU(), torch.nn.MaxPool2d((2, 2)), | ||
conv3, torch.nn.ReLU(), torch.nn.MaxPool2d((2, 2)), | ||
torch.nn.Flatten(start_dim=1), | ||
CatDogOutput(36992) | ||
) | ||
|
||
def configure_optimizers(self): | ||
params = self.hparams["optimizer_params"] | ||
params = self.get_default_optimizer_params() if not params else params | ||
return torch.optim.Adam(self.parameters(), **params) | ||
|
||
def forward_pass(self, img): | ||
return self.model(img) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Any | ||
|
||
from catdog.models.base import CatDogClassifier, CatDogOutput | ||
from torch.optim import Adam | ||
import torchvision | ||
|
||
class TransferLearningClassifier(CatDogClassifier): | ||
def __init__(self, *args: Any, **kwargs: Any): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.resnet = torchvision.models.resnet18(pretrained=True) | ||
for param in self.resnet.parameters(): | ||
param.requires_grad = False | ||
self.resnet.fc = CatDogOutput(512) | ||
|
||
def configure_optimizers(self): | ||
params = self.hparams.get("optimizer_params", None) | ||
params = self.get_default_optimizer_params() if not params else params | ||
# Only optimize the parameters for the last layer | ||
return Adam(self.resnet.fc.parameters(), **params) | ||
|
||
def forward_pass(self, img): | ||
return self.resnet(img) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
img_path hardcodeado lo queres ahi?? yo lo habia reemplazado por definitions.IMG_PATH
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, debí mirar con poco amor al hacer el merge del archivo con main. Corrijo en próximo commit.