def get_googlenet_model(csv_path, img_path, img_ext, dtype): from googlenet import GoogLeNet import torchvision.transforms as T model = GoogLeNet() model.type(dtype) IMAGENET_MEAN = [0.485, 0.456, 0.406] IMAGENET_STD = [0.229, 0.224, 0.225] dataset = AmazonDataset(csv_path, img_path, img_ext, dtype, three_band=True, channel_means=IMAGENET_MEAN, channel_stds=IMAGENET_STD) return model, dataset
def get_resnet_model(csv_path, img_path, img_ext, dtype): from torchvision.models import resnet18 import torchvision.transforms as T model = resnet18() model.fc = nn.Linear(model.fc.in_features, 17) model.type(dtype) transform_list = [T.Scale(224)] IMAGENET_MEAN = [0.485, 0.456, 0.406] IMAGENET_STD = [0.229, 0.224, 0.225] dataset = AmazonDataset(csv_path, img_path, img_ext, dtype, transform_list=transform_list, three_band=True, channel_means=IMAGENET_MEAN, channel_stds=IMAGENET_STD) return model, dataset
def get_conv6_model(csv_path, img_path, img_ext, dtype): model = nn.Sequential( ## 256x256 nn.Conv2d(4, 16, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(16), nn.Conv2d(16, 16, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(16), nn.AdaptiveMaxPool2d(128), ## 128x128 nn.Conv2d(16, 32, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(32), nn.Conv2d(32, 32, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(32), nn.AdaptiveMaxPool2d(64), ## 64x64 nn.Conv2d(32, 64, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(64), nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(64), nn.AdaptiveMaxPool2d(32), ## 32x32 Flatten(), nn.Linear(64 * 32 * 32, 1024), nn.ReLU(inplace=True), nn.Linear(1024, 17)) model.type(dtype) dataset = AmazonDataset(csv_path, img_path, img_ext, dtype) return model, dataset
if __name__ == '__main__': try: from_pickle = int(sys.argv[1]) except IndexError: from_pickle = 1 ## cpu dtype dtype = torch.FloatTensor save_model_path = "model_state_dict.pkl" csv_path = '../../data/train_v2.csv' img_path = '../../data/train-jpg' img_ext = '.jpg' transform_list = [transforms.Scale(224)] training_dataset = AmazonDataset(csv_path, img_path, img_ext, dtype, transform_list=transform_list, three_band=True) ## loader train_loader = DataLoader( training_dataset, batch_size=128, shuffle=True, num_workers=4, # 0 for CUDA ) model = torchvision.models.vgg11(pretrained=True) model.type(dtype) loss_fn = nn.MultiLabelSoftMarginLoss().type(dtype)
from pytorch_addons.pytorch_lr_scheduler.lr_scheduler import ReduceLROnPlateau if __name__ == '__main__': try: from_pickle = int(sys.argv[1]) except IndexError: from_pickle = 1 ## cpu dtype dtype = torch.cuda.FloatTensor save_model_path = "conv_6-layer_state_dict.pkl" save_mat_path = "conv_6-layer_loss_and_acc.mat" csv_path = '../../data/train_v2.csv' img_path = '../../data/train-jpg' img_ext = '.jpg' dataset = AmazonDataset(csv_path, img_path, img_ext, dtype) train_loader, val_loader = generate_train_val_dataloader( dataset, batch_size=128, num_workers=0, use_fraction_of_data=0.2, ) ## 6 conv layers model = nn.Sequential( ## 256x256 nn.Conv2d(4, 16, kernel_size=3, stride=1), nn.ReLU(inplace=True), nn.BatchNorm2d(16), nn.Conv2d(16, 16, kernel_size=3, stride=1),
if use_cuda: dtype = torch.cuda.FloatTensor num_workers = 0 else: dtype = torch.FloatTensor num_workers = 4 transform_list = [T.Scale(224)] IMAGENET_MEAN = [0.485, 0.456, 0.406] IMAGENET_STD = [0.229, 0.224, 0.225] dataset = AmazonDataset(csv_path, img_path, img_ext, dtype, transform_list=transform_list, three_band=True, channel_means=IMAGENET_MEAN, channel_stds=IMAGENET_STD) train_loader, val_loader = generate_train_val_dataloader( dataset, batch_size=batch_size, num_workers=num_workers, use_fraction_of_data=use_fraction_of_data, ) ## pretrained resnet model = resnet101(pretrained=True) ## resize last fully connected layer to match our problem model.fc = nn.Linear(model.fc.in_features, 17)
## cpu/gpu setup if use_cuda: dtype = torch.cuda.FloatTensor num_workers = 0 else: dtype = torch.FloatTensor num_workers = 4 transform_list = [] # [T.Scale(224)] IMAGENET_MEAN = [0.485, 0.456, 0.406] + [1.5 * .456] IMAGENET_STD = [0.229, 0.224, 0.225] + [2 * .224] dataset = AmazonDataset(csv_path, img_path, img_ext, dtype, transform_list=transform_list, use_flips=False, channel_means=IMAGENET_MEAN, channel_stds=IMAGENET_STD) train_loader, val_loader = generate_train_val_dataloader( dataset, batch_size=batch_size, num_workers=num_workers, use_fraction_of_data=use_fraction_of_data, ) ## pretrained resnet model = resnet18(pretrained=True) ## resize last fully connected layer to match our problem model.fc = nn.Linear(model.fc.in_features, 17) model.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False) model.type(dtype)
""" import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader from training_utils import train from layers import Flatten from read_in_data import AmazonDataset if __name__ == '__main__': ## cpu dtype dtype = torch.FloatTensor csv_path = '../../data/train.csv' img_path = '../../data/train-jpg' training_dataset = AmazonDataset(csv_path, img_path, dtype) ## loader train_loader = DataLoader( training_dataset, batch_size=256, shuffle=True, num_workers=4 # 1 for CUDA # pin_memory=True # CUDA only ) ## simple linear model model = nn.Sequential(Flatten(), nn.Linear(4 * 256 * 256, 17)) model.type(dtype) loss_fn = nn.BCELoss().type(dtype) optimizer = optim.Adam(model.parameters(), lr=5e-2)