def test_initializers_1(): wide = Wide(100, 1) deepdense = DeepDense(hidden_layers=[32,16], dropout=[0.5, 0.5], deep_column_idx=deep_column_idx, embed_input=embed_input, continuous_cols=colnames[-5:]) deeptext = DeepText( vocab_size=vocab_size, embed_dim=32, padding_idx=0) deepimage=DeepImage(pretrained=True) model = WideDeep(wide=wide, deepdense=deepdense, deeptext=deeptext, deepimage=deepimage, output_dim=1) cmodel = c(model) org_weights = [] for n,p in cmodel.named_parameters(): if n in test_layers_1: org_weights.append(p) model.compile(method='binary', verbose=0, initializers=initializers_1) init_weights = [] for n,p in model.named_parameters(): if n in test_layers_1: init_weights.append(p) res = all([torch.all((1-(a==b).int()).bool()) for a,b in zip(org_weights, init_weights)]) assert res
def test_initializers_1(initializers, test_layers): wide = Wide(np.unique(X_wide).shape[0], 1) deeptabular = TabMlp( mlp_hidden_dims=[32, 16], mlp_dropout=[0.5, 0.5], column_idx=column_idx, embed_input=embed_input, continuous_cols=colnames[-5:], ) deeptext = DeepText(vocab_size=vocab_size, embed_dim=32, padding_idx=0) deepimage = DeepImage(pretrained=True) model = WideDeep( wide=wide, deeptabular=deeptabular, deeptext=deeptext, deepimage=deepimage, pred_dim=1, ) cmodel = c(model) org_weights = [] for n, p in cmodel.named_parameters(): if n in test_layers: org_weights.append(p) trainer = Trainer(model, objective="binary", verbose=0, initializers=initializers) init_weights = [] for n, p in trainer.model.named_parameters(): if n in test_layers: init_weights.append(p) res = all([ torch.all((1 - (a == b).int()).bool()) for a, b in zip(org_weights, init_weights) ]) assert res
# deepdense = TabResnet( # blocks_dims=[64, 32], # dropout=0.2, # column_idx=tab_preprocessor.column_idx, # embed_input=tab_preprocessor.embeddings_input, # continuous_cols=continuous_cols, # ) deeptext = DeepText( vocab_size=len(text_processor.vocab.itos), hidden_dim=64, n_layers=3, rnn_dropout=0.5, padding_idx=1, embed_matrix=text_processor.embedding_matrix, ) deepimage = DeepImage(pretrained=True, head_hidden_dims=None) model = WideDeep(wide=wide, deeptabular=deepdense, deeptext=deeptext, deepimage=deepimage) wide_opt = torch.optim.Adam(model.wide.parameters(), lr=0.01) deep_opt = torch.optim.Adam(model.deeptabular.parameters()) text_opt = RAdam(model.deeptext.parameters()) img_opt = RAdam(model.deepimage.parameters()) wide_sch = torch.optim.lr_scheduler.StepLR(wide_opt, step_size=5) deep_sch = torch.optim.lr_scheduler.StepLR(deep_opt, step_size=3) text_sch = torch.optim.lr_scheduler.StepLR(text_opt, step_size=5) img_sch = torch.optim.lr_scheduler.StepLR(img_opt, step_size=3)
deepdense = DeepDense( hidden_layers=[64, 32], dropout=[0.2, 0.2], deep_column_idx=prepare_deep.deep_column_idx, embed_input=prepare_deep.embeddings_input, continuous_cols=continuous_cols, ) deeptext = DeepText( vocab_size=len(text_processor.vocab.itos), hidden_dim=64, n_layers=3, rnn_dropout=0.5, padding_idx=1, embedding_matrix=text_processor.embedding_matrix, ) deepimage = DeepImage(pretrained=True, head_layers=None) model = WideDeep(wide=wide, deepdense=deepdense, deeptext=deeptext, deepimage=deepimage) wide_opt = torch.optim.Adam(model.wide.parameters(), lr=0.01) deep_opt = torch.optim.Adam(model.deepdense.parameters()) text_opt = RAdam(model.deeptext.parameters()) img_opt = RAdam(model.deepimage.parameters()) wide_sch = torch.optim.lr_scheduler.StepLR(wide_opt, step_size=5) deep_sch = torch.optim.lr_scheduler.StepLR(deep_opt, step_size=3) text_sch = torch.optim.lr_scheduler.StepLR(text_opt, step_size=5) img_sch = torch.optim.lr_scheduler.StepLR(img_opt, step_size=3)
y_train, y_val, ) = train_test_split(X_wide, X_deep, X_text, X_img, target) # build model components wide = Wide(np.unique(X_wide).shape[0], 1) deepdense = DeepDense( hidden_layers=[32, 16], dropout=[0.5, 0.5], deep_column_idx={k: v for v, k in enumerate(colnames)}, embed_input=embed_input, continuous_cols=colnames[-5:], ) deeptext = DeepText(vocab_size=vocab_size, embed_dim=32, padding_idx=0) deepimage = DeepImage(pretrained=True) # transforms mean = [0.406, 0.456, 0.485] # BGR std = [0.225, 0.224, 0.229] # BGR transforms1 = [ToTensor, Normalize(mean=mean, std=std)] transforms2 = [Normalize(mean=mean, std=std)] ############################################################################## # Test many possible scenarios of data inputs I can think off. Surely users # will input something unexpected ############################################################################## @pytest.mark.parametrize( "X_wide, X_deep, X_text, X_img, X_train, X_val, target, val_split, transforms, nepoch, null", [
import numpy as np import torch import pytest from pytorch_widedeep.models import DeepImage X_images = (torch.from_numpy(np.random.rand(10, 3, 224, 224))).float() ############################################################################### # Simply testing that it runs with the defaults ############################################################################### model1 = DeepImage() def test_deep_image(): out = model1(X_images) assert out.size(0) == 10 and out.size(1) == 512 ############################################################################### # Testing with custome backbone ############################################################################### model2 = DeepImage(pretrained=False) def test_deep_image_custom_backbone(): out = model2(X_images) assert out.size(0) == 10 and out.size(1) == 512 ###############################################################################
Wide, DeepText, WideDeep, DeepDense, DeepImage, ) embed_input = [(u, i, j) for u, i, j in zip(["a", "b", "c"][:4], [4] * 3, [8] * 3)] deep_column_idx = {k: v for v, k in enumerate(["a", "b", "c"])} wide = Wide(10, 1) deepdense = DeepDense(hidden_layers=[16, 8], deep_column_idx=deep_column_idx, embed_input=embed_input) deeptext = DeepText(vocab_size=100, embed_dim=8) deepimage = DeepImage(pretrained=False) ############################################################################### # test raising 'output dim errors' ############################################################################### @pytest.mark.parametrize( "deepcomponent, component_name", [ (None, "dense"), (deeptext, "text"), (deepimage, "image"), ], ) def test_history_callback(deepcomponent, component_name):
def test_too_cold(): with pytest.raises(ValueError): mod = DeepImage(freeze_n=10) # noqa: F841
import numpy as np import torch import pytest from pytorch_widedeep.models import DeepImage X_images = (torch.from_numpy(np.random.rand(10, 3, 224, 224))).float() ############################################################################### # Simply testing that it runs with the defaults ############################################################################### model1 = DeepImage() def test_deep_image_1(): out = model1(X_images) assert out.size(0) == 10 and out.size(1) == 512 ############################################################################### # Testing with 'custom' backbone ############################################################################### model2 = DeepImage(pretrained=False) def test_deep_image_custom_backbone(): out = model2(X_images) assert out.size(0) == 10 and out.size(1) == 512 ###############################################################################