import torch.nn as nn
import torchvision.models as models
import flexflow.torch.fx as fx

# alexnet = models.alexnet()
# fx.torch_to_flexflow(alexnet, "alexnet.ff")
#
# vgg16 = models.vgg16()
# fx.torch_to_flexflow(vgg16, "vgg16.ff")
#
# squeezenet = models.squeezenet1_0()
# fx.torch_to_flexflow(squeezenet, "squeezenet.ff")

# densenet = models.densenet161()
# fx.torch_to_flexflow(densenet, "densenet.ff")

# inception = models.inception_v3()
# fx.torch_to_flexflow(inception, "inception.ff")

googlenet = models.googlenet()
fx.torch_to_flexflow(googlenet, "googlenet.ff")

# shufflenet = models.shufflenet_v2_x1_0()
# fx.torch_to_flexflow(shufflenet, "shufflenet.ff")

# mobilenet = models.mobilenet_v2()
# fx.torch_to_flexflow(mobilenet, "mobilenet.ff")
def classify_images():
    net = models.googlenet(pretrained=True)
    net.eval()
    transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    paths = {
        '../data/ILSVRC2012_img_val/raw/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify.csv',
        '../data/ILSVRC2012_img_val/originalImgModification/denoised/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_denoised.csv',
        '../data/ILSVRC2012_img_val/originalImgModification/sharpen/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_sharpened.csv',
        '../data/ILSVRC2012_img_val/originalImgModification/bilateralfilter/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_bilateralfilter.csv',
        '../data/ILSVRC2012_img_val/originalImgModification/gaussianblur/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_gaussianblur.csv',
        '../data/ILSVRC2012_img_val/originalImgModification/medianblur/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_medianblur.csv',
        '../data/ILSVRC2012_img_val/perturbed/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed.csv',  #you may have to change this filepath
        '../data/ILSVRC2012_img_val/perturbedModification/denoised/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed_denoised.csv',
        '../data/ILSVRC2012_img_val/perturbedModification/sharpen/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed_sharpened.csv',
        '../data/ILSVRC2012_img_val/perturbedModification/bilateralfilter/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed_bilateralfilter.csv',
        '../data/ILSVRC2012_img_val/perturbedModification/gaussianblur/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed_gaussianblur.csv',
        '../data/ILSVRC2012_img_val/perturbedModification/medianblur/':
        '../data/ILSVRC2012_img_val/classification/imagenet_classify_perturbed_medianblur.csv'
    }

    for input, output in paths.items():
        files = [f for f in glob.glob(input + "**/*.JPEG", recursive=True)]
        bar = progressbar.ProgressBar(maxval=len(files)).start()
        classify = {}
        i = 0
        for path in files:
            img = Image.open(path)
            if (img.mode != "RGB"):
                img = img.convert(mode="RGB")

            image = transform(img)

            f_image = net.forward(
                Variable(image[None, :, :, :],
                         requires_grad=True)).data.cpu().numpy().flatten()
            I = (np.array(f_image)).flatten().argsort()[::-1]

            label = I[0]
            classify[os.path.basename(path)] = label
            i = i + 1
            bar.update(i)

        with open(output, 'w+') as f:
            writer = csv.writer(f, lineterminator='\n')
            for row in classify.items():
                writer.writerow(row)
Example #3
0
def get_model(name, device):
    if name == "normal_cnn":
        model = Net().to(device)
        return model
    elif name == "alexnet":
        model = models.alexnet().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg11":
        model = models.vgg11().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg13":
        model = models.vgg13().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg16":
        model = models.vgg16().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg19":
        model = models.vgg19().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg11_bn":
        model = models.vgg11_bn().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg13_bn":
        model = models.vgg13_bn().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg16_bn":
        model = models.vgg16_bn().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "vgg19_bn":
        model = models.vgg19_bn().to(device)
        num_features = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "densenet121":
        model = models.densenet121().to(device)
        num_features = model.classifier.in_features
        model.classifier = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "densenet161":
        model = models.densenet161().to(device)
        num_features = model.classifier.in_features
        model.classifier = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "densenet169":
        model = models.densenet169().to(device)
        num_features = model.classifier.in_features
        model.classifier = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "densenet201":
        model = models.densenet201().to(device)
        num_features = model.classifier.in_features
        model.classifier = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnet18":
        model = models.resnet18().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnet34":
        model = models.resnet34().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnet50":
        model = models.resnet50().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnet101":
        model = models.resnet101().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnet152":
        model = models.resnet152().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "googlenet":
        model = models.googlenet(aux_logits=False).to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "inception_v3":
        model = models.inception_v3(aux_logits=False).to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "shufflenet_v2_x0_5":
        model = models.shufflenet_v2_x0_5().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "shufflenet_v2_x1_0":
        model = models.shufflenet_v2_x1_0().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "shufflenet_v2_x1_5":
        model = models.shufflenet_v2_x1_5().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "shufflenet_v2_x2_0":
        model = models.shufflenet_v2_x2_0().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "mobilenet_v2":
        model = models.mobilenet_v2().to(device)
        num_features = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnext50_32x4d":
        model = models.resnext50_32x4d().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "resnext101_32x8d":
        model = models.resnext101_32x8d().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "wide_resnet50_2":
        model = models.wide_resnet50_2().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "wide_resnet101_2":
        model = models.wide_resnet101_2().to(device)
        num_features = model.fc.in_features
        model.fc = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "mnasnet0_5":
        model = models.mnasnet0_5().to(device)
        num_features = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "mnasnet0_75":
        model = models.mnasnet0_75().to(device)
        num_features = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "mnasnet1_0":
        model = models.mnasnet1_0().to(device)
        num_features = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_features, 1).to(device)
        return model
    elif name == "mnasnet1_3":
        model = models.mnasnet1_3().to(device)
        num_features = model.classifier[1].in_features
        model.classifier[1] = nn.Linear(num_features, 1).to(device)
        return model
 def test_googlenet_eval(self):
     m = torch.jit.script(models.googlenet(pretrained=True).eval())
     self.checkModule(m, "googlenet", torch.rand(1, 3, 224, 224))
Example #5
0
def create_model(model_type, num_classes, feature_extract, pretrained):
    """
    Creates a model.
    :param model_type: Model type.
    :param num_classes: Number of classes.
    :param feature_extract: A boolean indicating if we are extracting features.
    :param pretrained: A boolean indicating if pretrained weights should be used.
    :return: Model.
    """
    device = get_device()
    if 'resnet18' == model_type:
        model = models.resnet18(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'resnet34' == model_type:
        model = models.resnet34(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'resnet50' == model_type:
        model = models.resnet50(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'resnet101' == model_type:
        model = models.resnet101(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'resnet152' == model_type:
        model = models.resnet152(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'alexnet' == model_type:
        model = models.alexnet(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(4096, num_classes)
    elif 'vgg11' == model_type:
        model = models.vgg11(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg11_bn' == model_type:
        model = models.vgg11_bn(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg13' == model_type:
        model = models.vgg13(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg13_bn' == model_type:
        model = models.vgg13_bn(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg16' == model_type:
        model = models.vgg16(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg16_bn' == model_type:
        model = models.vgg16_bn(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg19' == model_type:
        model = models.vgg19(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'vgg19_bn' == model_type:
        model = models.vgg19_bn(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[6] = nn.Linear(model.classifier[6].in_features,
                                        num_classes)
    elif 'squeezenet1_0' == model_type:
        model = models.squeezenet1_0(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Conv2d(512,
                                        num_classes,
                                        kernel_size=(1, 1),
                                        stride=(1, 1))
        model.num_classes = num_classes
    elif 'squeezenet1_1' == model_type:
        model = models.squeezenet1_1(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Conv2d(512,
                                        num_classes,
                                        kernel_size=(1, 1),
                                        stride=(1, 1))
        model.num_classes = num_classes
    elif 'densenet121' == model_type:
        model = models.densenet121(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
    elif 'densenet161' == model_type:
        model = models.densenet161(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
    elif 'densenet169' == model_type:
        model = models.densenet169(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
    elif 'densenet201' == model_type:
        model = models.densenet201(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier = nn.Linear(model.classifier.in_features, num_classes)
    elif 'googlenet' == model_type:
        model = models.googlenet(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'shufflenet_v2_x0_5' == model_type:
        model = models.shufflenet_v2_x0_5(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'shufflenet_v2_x1_0' == model_type:
        model = models.shufflenet_v2_x1_0(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'mobilenet_v2' == model_type:
        model = models.mobilenet_v2(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Linear(model.classifier[1].in_features,
                                        num_classes)
    elif 'resnext50_32x4d' == model_type:
        model = models.resnext50_32x4d(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Linear(model.classifier[1].in_features,
                                        num_classes)
    elif 'resnext101_32x8d' == model_type:
        model = models.resnext101_32x8d(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'wide_resnet50_2' == model_type:
        model = models.wide_resnet50_2(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'wide_resnet101_2' == model_type:
        model = models.wide_resnet101_2(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    elif 'mnasnet0_5' == model_type:
        model = models.mnasnet0_5(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Linear(model.classifier[1].in_features,
                                        num_classes)
    elif 'mnasnet1_0' == model_type:
        model = models.mnasnet1_0(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.classifier[1] = nn.Linear(model.classifier[1].in_features,
                                        num_classes)
    else:
        model = models.inception_v3(pretrained=pretrained)
        set_parameter_requires_grad(model, feature_extract)
        model.AuxLogits.fc = nn.Linear(model.AuxLogits.fc.in_features,
                                       num_classes)
        model.fc = nn.Linear(model.fc.in_features, num_classes)
    return model.to(device)
Example #6
0
def googlenet():
    return models.googlenet(pretrained=True)
def initialize_model(model_name,
                     embedding_dim,
                     feature_extracting,
                     use_pretrained=True):
    if model_name == "densenet161":
        model_ft = models.densenet161(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
        if model_name == "densenet169":
            model_ft = models.densenet161(pretrained=use_pretrained,
                                          memory_efficient=True)
            set_parameter_requires_grad(model_ft, feature_extracting)

            #print(model_ft)
            num_features = model_ft.classifier.in_features
            print(num_features)
            #print(embedding_dim)
            model_ft.classifier = nn.Linear(num_features, embedding_dim)
            #print(model_ft)
    if model_name == "densenet121":
        model_ft = models.densenet121(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    if model_name == "densenet201":
        model_ft = models.densenet201(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    elif model_name == "resnet101":
        model_ft = models.resnet101(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "resnet34":
        model_ft = models.resnet34(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_resnet50":
        model_ft = adl_resnet50(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "inceptionv3":
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext":
        model_ft = se_resnext101_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext50":
        model_ft = se_resnext50_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "googlenet":
        model_ft = models.googlenet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        #print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    elif model_name == "mobilenet2":
        model_ft = models.MobileNetV2(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)

        num_features = model_ft.classifier[1].in_features
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "mnasnet":
        model_ft = mnasnet1_0(pretrained=use_pretrained)
        #model_ft = MnasNet()
        set_parameter_requires_grad(model_ft, feature_extracting)

        print(model_ft.classifier[0])
        print(model_ft.classifier[1])

        num_features = model_ft.classifier[1].in_features
        print(num_features)
        print(embedding_dim)
        #model_ft.classifier[0] = nn.Dropout(p=0.2, inplace=False)
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_googlenet":
        model_ft = GoogLeNet()
        set_parameter_requires_grad(model_ft, feature_extracting)
        print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    else:
        raise ValueError

    return model_ft
Example #8
0
def main():

    #=======================================
    #           1. Load dataset
    #=======================================
    # CIFAR data
    data_tf = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    train_set = datasets.CIFAR10('./data',
                                 train=True,
                                 transform=data_tf,
                                 download=True)
    train_loader = torch.utils.data.DataLoader(train_set,
                                               batch_size=64,
                                               shuffle=True)
    test_set = datasets.CIFAR10('./data',
                                train=False,
                                transform=data_tf,
                                download=True)
    valid_loader = torch.utils.data.DataLoader(test_set,
                                               batch_size=128,
                                               shuffle=False)
    # custom datasets
    # train_folders, valid_folders = get_folders(
    #     config.train_data, config.valid_data)
    # train_datasets = MyDataSet(train_folders, transforms=None)
    # valid_datasets = MyDataSet(valid_folders, transforms=None, train=False)
    # train_loader = DataLoader(dataset=train_datasets,
    #                           batch_size=config.batch_size, shuffle=True)
    # valid_loader = DataLoader(dataset=valid_datasets,
    #                           batch_size=config.batch_size, shuffle=True)
    print("Train numbers:{:d}".format(len(train_loader)))
    print("Test numbers:{:d}".format(len(valid_loader)))

    #=======================================
    #   2. Define network and Load model
    #=======================================
    if config.pretrained:
        model = models.googlenet(num_classes=config.num_classes)
        checkpoint = torch.load(config.model_path)
        model.load_state_dict(checkpoint['model_state_dict'])
        print("load model success")
    else:
        model = models.googlenet(pretrained=True)
        # adjust last fc layer to class number
        channel_in = model.fc.in_features
        model.fc = nn.Linear(channel_in, config.num_classes)
    model.to(device)

    #=======================================
    # 3. Define Loss function and optimizer
    #=======================================
    criterion = nn.CrossEntropyLoss().to(device)
    optimizer = optim.Adam(model.parameters(),
                           lr=config.lr,
                           amsgrad=True,
                           weight_decay=config.weight_decay)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)

    #=======================================
    #     4. Train and Test the network
    #=======================================
    best_accuracy = 0.
    epoch = 0
    resume = False

    # ====4.1 restart the training process====
    if resume:
        checkpoint = torch.load(config.model_path)
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        epoch = checkpoint["epoch"]
        loss = checkpoint["loss"]
        best_accuracy = checkpoint["best_accuracy"]
        # print checkpoint
        print('epoch:', epoch)
        print('best_accuracy:', best_accuracy)
        print("model's state_dict:")
        for param_tensor in model.state_dict():
            print(param_tensor, '\t', model.state_dict()[param_tensor].size())

    for epoch in range(1, config.epochs + 1):

        # ====4.2 start training====
        print("======start training=====")
        model.train()

        torch.cuda.empty_cache()
        start = time.time()
        index = 1
        sum_loss = 0.
        correct = 0.
        total = 0.

        for images, labels in train_loader:
            # clear the cuda cache
            torch.cuda.empty_cache()
            images = images.to(device)
            labels = labels.to(device, dtype=torch.long)

            # Forward pass
            outputs = model(images)
            loss = criterion(outputs, labels)
            # Backward and Optimize
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            # print training loss and accuracy
            _, predicted = torch.max(outputs.data, 1)
            sum_loss += loss.item()
            total += labels.size(0)
            correct += (predicted == labels).sum()
            if index % 10 == 0:
                print("Iter: %d ===> Loss: %.8f | Acc: %.3f%%" %
                      (index, sum_loss / index, 100. * correct / total))
            index += 1

        end = time.time()
        print("Epoch [%d/%d], Loss: %.8f, Time: %.1fsec!" %
              (epoch, config.epochs, loss.item(), (end - start)))
        vis.line(X=[epoch],
                 Y=[loss.item()],
                 win='loss',
                 opts=dict(title='train loss'),
                 update='append')

        # ====4.3 start evalidating====
        model.eval()

        correct_prediction = 0.
        total = 0

        with torch.no_grad():
            for images, labels in valid_loader:
                # clear the cuda cache
                torch.cuda.empty_cache()
                images = images.to(device)
                labels = labels.to(device, dtype=torch.long)

                # print prediction
                outputs = model(images)
                _, predicted = torch.max(outputs.data, 1)
                # total labels number
                total += labels.size(0)
                # add correct
                correct_prediction += (predicted == labels).sum().item()
                # print("total correct number: ", correct_prediction)

        accuracy = 100. * correct_prediction / total
        print("Accuracy: %.4f%%" % accuracy)
        scheduler.step()

        if accuracy > best_accuracy:
            if not os.path.exists(config.checkpoint):
                os.mkdir(config.checkpoint)
            # save networks
            torch.save(
                {
                    'epoch': epoch,
                    'model_state_dict': model.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict(),
                    'loss': loss,
                    'best_accuracy': accuracy,
                },
                os.path.join("%s-%.3f.pth") % (config.best_models, accuracy))
            print("save networks for epoch:", epoch)
            best_accuracy = accuracy
Example #9
0
    def forward(self, x):
        batch_size = x.size(0)

        feat = self.FNet(x)
        # incept4e and incept5b are middle layer of GNet
        # then used to calculate OAM
        incept4e = self.incept4e(feat)
        incept5b = self.incept5b(incept4e)
        oam = (self.OAM(incept4e) + self.OAM(incept5b)) / 2
        # N x 480 x 14 x 14
        caps = [
            feat * self.CAMs[i](feat).view(batch_size, 1, 1, -1)
            for i in range(self.cams)
        ]
        # #cams of N x 480 x 14 x 14
        cams = [
            self.GNet(caps[i]).view(batch_size, -1) for i in range(self.cams)
        ]  # each of size N x 1024
        embeddings = [self.CA_learners[i](cams[i]) for i in range(self.cams)]

        adv_reverse = [
            self.CA_sub_adv(embeddings[i]) for i in range(self.cams)
        ]
        adv = [self.CA_sub(embeddings[i]) for i in range(self.cams)]

        return cams, embeddings, adv, adv_reverse


if __name__ == '__main__':
    googlenet = models.googlenet(pretrained=False)
    summary(googlenet, (3, 512, 512))
Example #10
0
learning_rate = 10
n_iterations = 100
layers = [11]  # For resnet50, layer can range from 0 to 9
# n_octaves = 7
# octave_scale = 1.4
# channel = 0

# ~~~~~~~~~~~~~~~~~~~

# set mean and std_deviation for imagenet
mean = [.485, .456, .406]
std = [.229, .224, .225]

# initialize net
net = models.googlenet(pretrained=True).to(device)
net.eval()

# preprocess image
img = Image.open("dog1.jpg")
img.show()
img = transform(img).unsqueeze(0).to(device)

children = list(net.children())
for i in range(len(layers)):
    layers[i] = nn.Sequential(*children)[:layers[i] - len(children)]

for _ in range(n_iterations):
    for layer in layers:
        # apply jitter
        y_jitter, x_jitter = np.random.randint(-5, 5, size=2)
Example #11
0
def initialize_model(model_name='vgg16',
                     num_classes=1,
                     feature_extract=True,
                     use_pretrained=True):
    # Initialize these variables which will be set in this if statement. Each of these
    #   variables is model specific.
    model_ft = None
    input_size = 0

    if model_name == "googlenet":
        """ googlenet
        """
        model_ft = models.googlenet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "resnet152":
        """ Resnet152
        """
        model_ft = models.resnet152(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "vgg16":
        """ VGG16
        """
        model_ft = models.vgg16(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier[6].in_features
        model_ft.classifier[6] = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "resnet18":
        """ Resnet18
        """
        model_ft = models.resnet18(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "alexnet":
        """ Alexnet
        """
        model_ft = models.alexnet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier[6].in_features
        model_ft.classifier[6] = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "vgg11_bn":
        """ VGG11_bn
        """
        model_ft = models.vgg11_bn(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier[6].in_features
        model_ft.classifier[6] = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "squeezenet":
        """ Squeezenet
        """
        model_ft = models.squeezenet1_0(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        model_ft.classifier[1] = nn.Conv2d(512,
                                           num_classes,
                                           kernel_size=(1, 1),
                                           stride=(1, 1))
        model_ft.num_classes = num_classes
        input_size = 224

    elif model_name == "densenet":
        """ Densenet
        """
        model_ft = models.densenet121(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.classifier.in_features
        model_ft.classifier = nn.Linear(num_ftrs, num_classes)
        input_size = 224

    elif model_name == "inception":
        """ Inception v3
        Be careful, expects (299,299) sized images and has auxiliary output
        """
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extract)
        # Handle the auxilary net
        num_ftrs = model_ft.AuxLogits.fc.in_features
        model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
        # Handle the primary net
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 299

    else:
        print("Invalid model name, exiting...")
        exit()

    return model_ft, input_size
Example #12
0
    def get_model(model_id, use_pretrained):
        model_ft = None
        if model_id == PyTorchModelsEnum.ALEXNET:
            model_ft = models.alexnet(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.DENSENET121:
            model_ft = models.densenet121(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.DENSENET161:
            model_ft = models.densenet161(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.DENSENET169:
            model_ft = models.densenet169(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.DENSENET201:
            model_ft = models.densenet201(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.GOOGLENET:
            model_ft = models.googlenet(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.INCEPTION_V3:
            model_ft = models.inception_v3(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.MOBILENET_V2:
            model_ft = models.mobilenet_v2(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.MNASNET_0_5:
            model_ft = models.mnasnet0_5(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.MNASNET_0_75:  # no pretrained
            model_ft = models.mnasnet0_75(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.MNASNET_1_0:
            model_ft = models.mnasnet1_0(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.MNASNET_1_3:
            model_ft = models.mnasnet1_3(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNET18:
            model_ft = models.resnet18(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNET34:
            model_ft = models.resnet34(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNET50:
            model_ft = models.resnet50(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNET101:
            model_ft = models.resnet101(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNET152:
            model_ft = models.resnet152(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNEXT50:
            model_ft = models.resnext50_32x4d(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.RESNEXT101:
            model_ft = models.resnext101_32x8d(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SHUFFLENET_V2_0_5:
            model_ft = models.shufflenet_v2_x0_5(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SHUFFLENET_V2_1_0:
            model_ft = models.shufflenet_v2_x1_0(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SHUFFLENET_V2_1_5:
            model_ft = models.shufflenet_v2_x1_5(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SHUFFLENET_V2_2_0:
            model_ft = models.shufflenet_v2_x2_0(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SQUEEZENET1_0:
            model_ft = models.squeezenet1_0(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.SQUEEZENET1_1:
            model_ft = models.squeezenet1_1(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG11:
            model_ft = models.vgg11(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG11_BN:
            model_ft = models.vgg11_bn(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG13:
            model_ft = models.vgg13(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG13_BN:
            model_ft = models.vgg13_bn(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG16:
            model_ft = models.vgg16(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG16_BN:
            model_ft = models.vgg16_bn(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG19:
            model_ft = models.vgg19(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.VGG19_BN:
            model_ft = models.vgg19_bn(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.WIDE_RESNET50:
            model_ft = models.wide_resnet50_2(pretrained=use_pretrained)
        elif model_id == PyTorchModelsEnum.WIDE_RESNET101:
            model_ft = models.wide_resnet101_2(pretrained=use_pretrained)

        return model_ft
import cv2

# from model_utils import model, generate_mask, segmentation_model, layers

import runway
from runway.data_types import image, number, boolean
"""
works on torch_dreams v1.1.0
for tests use: $ pip install git+https://github.com/Mayukhdeb/torch-dreams
to run server on localhost: $ python runway_model.py
"""
"""
set up segmentation utils
"""
segmentation_model = models.segmentation.fcn_resnet101(pretrained=True).eval()
model = models.googlenet(pretrained=True).eval()

seg_transforms = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])


def generate_mask(model,
                  image,
                  transforms=seg_transforms,
                  invert=False,
                  factor=1.0):
    inp = seg_transforms(image).unsqueeze(0)
Example #14
0
def main():
    print(torch.__version__)
    # برای تقسیم داده ها به آموزش و تست به صورت خودکار
    # این تابع درصدی از داده های آموزش را ضمن حفظ اسامی پوشه ها، به پوشه تست منتقل می کند 
    if(not os.path.exists(vars.val_dir)):
        utils.create_validation_data(vars.train_dir, vars.val_dir, vars.val_split_ratio, 'jpg')


    def handler(signum, frame):
        print('Signal handler called with signal', signum)
        print('Training will finish after this epoch')
        vars.stop_training = True
        #raise OSError("Couldn't open vars.device!")

    signal.signal(signal.SIGINT, handler) # only in python version >= 3.2

    print("Start Time: ", strftime("%Y-%m-%d %H:%M:%S", localtime()))
    print("Active Mode: " + vars.mode)
    plt.ion()   # interactive mode
    ######################################################################
    # Load Data
    # Data augmentation and normalization for training
    # Just normalization for validation

    data_transforms = {
        'train': transforms.Compose([
            transforms.Resize([vars.input_size, vars.input_size]),
            #transforms.ColorJitter(0.1, 0.1, 0.1, 0.01),
            #transforms.RandomRotation(5),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val': transforms.Compose([
            transforms.Resize([vars.input_size, vars.input_size]),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'test': transforms.Compose([
            transforms.Resize([vars.input_size, vars.input_size]),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }


    # image_dataset_train = {'train': datasets.ImageFolder(os.path.join(vars.data_dir, 'train'), data_transforms['train'])}
    # image_dataset_test = {'val': datasets.ImageFolder(os.path.join(vars.data_dir, 'val'), data_transforms['val'])}
    # image_dataset_train.update(image_dataset_test)
    # خط پایین با سه خط بالا برابری می کند!
    image_datasets = {x: datasets.ImageFolder(os.path.join(vars.data_dir, x), data_transforms[x])
                    for x in ['train', 'val']}

    vars.dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=vars.batch_size, shuffle=True, num_workers=0)
                for x in ['train', 'val']}

    vars.dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
    vars.class_names = image_datasets['train'].classes

    # Get a batch of training data
    inputs, classes = next(iter(vars.dataloaders['train']))

    # Make a grid from batch
    out = torchvision.utils.make_grid(inputs)

    #utils.imshow(out, title=[vars.class_names[x] for x in classes])


    ######################################################################
    # Finetuning the convnet
    # Load a pretrained model and reset final fully connected layer.

    ##\\//\\//model, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)
    if(vars.model_name.find('vgg') != -1):
        model = models.vgg11_bn(pretrained=vars.pre_trained)
        num_ftrs = model.classifier[6].in_features
        model.classifier[6] = nn.Linear(num_ftrs, len(vars.class_names))
    elif(vars.model_name == 'resnet152'):
        model = models.resnet152(pretrained=vars.pre_trained)	
    elif(vars.model_name == 'resnet50'):
        model = models.resnet50(pretrained=vars.pre_trained)
    elif(vars.model_name == 'resnet18'):
        model = models.resnet18(pretrained=vars.pre_trained)
    elif(vars.model_name == 'googlenet'):
        model = models.googlenet(pretrained=vars.pre_trained)
    elif(vars.model_name == 'darknet53'):
        model = darknet.darknet53(1000)
        optimizer = optim.SGD(model.parameters(), lr = vars.learning_rate, momentum=0.9)
        if(vars.pre_trained):
            #model.load_state_dict( torch.load('D:\\Projects\\_Python\\car Detection\\model_best.pth.tar') )	
            checkpoint = torch.load('D:\\Projects\\_Python\\car Detection\\model_best.pth.tar')
            start_epoch = checkpoint['epoch']
            best_acc1 = checkpoint['best_acc1']
            if vars.device.type == 'cuda':
                # best_acc1 may be from a checkpoint from a different GPU
                best_acc1 = best_acc1.to(vars.device)
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
    elif(vars.model_name == 'car3conv'):
        model = mymodels.car3conv()
    elif(vars.model_name == 'car5conv'):
        model = mymodels.car5conv()	
    elif(vars.model_name == 'car2conv'):
        model = mymodels.car2conv()		
    elif(vars.model_name == 'mymodel'):
        model = mymodels.MyModel()
    elif(vars.model_name == 'mymodel2'):
        model = mymodels.MyModel2()
    else:
        return hogmain.main(None)
        

    if(vars.model_name != 'vgg11'):
        num_ftrs = model.fc.in_features
        model.fc = nn.Linear(num_ftrs, len(vars.class_names))

    model.to(vars.device)

    if(vars.mode == 'test'):#test
        #model.load_state_dict(torch.load("D:\\Projects\\Python\\Zeitoon Detection\"))
        model.load_state_dict(torch.load(vars.test_model))

        # log_dir = '{}-{}-{}-batch-{}'.format(vars.model_name, 'SGD', 'cuda', vars.batch_size)

        # if(vars.pre_trained):
        #	 log_dir = log_dir + '-pretrained'
        # if not os.path.exists(log_dir):
        #	 os.mkdir(log_dir)

        log_file = open(".\\Time-{}-{}.log".format(vars.model_name, vars.batch_size),"w")	
        for dev in ['cuda', 'cpu']:
            vars.device = torch.device(dev)
            model = model.to(vars.device)
            #run model on one batch to allocate required memory on device (and have more exact results)
            inputs = inputs.to(vars.device)
            outputs = model(inputs)

            s = test_model(model, vars.criterion, 'val', 100)
            log_file.write(s)
            #log_file.write('\n' + '-'*80)
        
        log_file.write(summary(model, input_size=(3, vars.input_size, vars.input_size), batch_size=-1, device=vars.device.type))
        log_file.close() 


        print(summary(model, input_size=(3, vars.input_size, vars.input_size), batch_size=-1, device=vars.device.type))
    else:
        print(summary(model, input_size=(3, vars.input_size, vars.input_size), batch_size=-1, device=vars.device.type))

        #model.load_state_dict(torch.load("C:\\Projects\\Python\\Car Detection\\darknet53-SGD-cuda-batch-32\\ep7-acc97.83-loss0.0667.pth"))

        optimizer = optim.SGD(model.parameters(), lr = vars.learning_rate, momentum=0.9)
        #optimizer = optim.Adam(model.parameters(), lr=0.05)
        # Decay LR by a factor of 0.6 every 6 epochs
        exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size = vars.scheduler_step_size, gamma = vars.scheduler_gamma)
        model = model.to(vars.device)
        model = train_model(model, vars.criterion, optimizer, exp_lr_scheduler, vars.num_epochs)
        visualize_model(model)
        plt.ioff()
        plt.show()
Example #15
0
def adam(model: nn.Module, lr=0.001):
    return torch.optim.Adam(model.parameters(), lr=lr)


def sgd(model: nn.Module, lr=0.001):
    return torch.optim.SGD(model.parameters(), lr=lr)


OPTIMIZERS = {
    'adam': adam,
    'sgd': sgd,
}

FEATURE_EXTRACTORS = {
    'resnet18': lambda: models.resnet18(pretrained=False),
    'googlenet': lambda: models.googlenet(pretrained=False),
    'resnet18-np': lambda: ResNet18NoFlattening(pretrained=False),
    'resnet12-np': lambda: ResNet12NoFlattening(),
    'resnet12-np-o': lambda: ResNet12NoFlatteningOriginal(),
    'conv256-np-o': lambda: ConvNet256Original(),
    'conv64-np-o': lambda: ConvNet64Original(),
    'conv64-p-o': lambda: ConvNet64PoolingOriginal(),
}


class FSLEpisodeSampler(Dataset):
    def __init__(self, subdataset: LabeledSubdataset, n_way: int, n_shot: int, batch_size: int, balanced: bool,
                 device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
        self.subdataset = subdataset
        self.n_way = n_way
        self.n_shot = n_shot
Example #16
0
 def init_from_googlenet(self):
     googlenet = models.googlenet(pretrained=True)
     self.load_state_dict(googlenet.state_dict(), strict=False)
     self.transform_input = True
Example #17
0
def main():
    opts = get_train_args()
    print("load data ...")
    train_data = datasets.ImageFolder(
        root="data/train",
        transform=transforms.Compose([
            transforms.Resize((256, 256)),  # 한 축을 128로 조절하고
            #transforms.CenterCrop(256),  # square를 한 후,
            transforms.ToTensor(),  # Tensor로 바꾸고 (0~1로 자동으로 normalize)
            transforms.Normalize(
                (0.5, 0.5, 0.5),  # -1 ~ 1 사이로 normalize
                (0.5, 0.5, 0.5)),  # (c - m)/s 니까...
        ]))

    test_data = datasets.ImageFolder(
        root="data/test",
        transform=transforms.Compose([
            transforms.Resize((256, 256)),  # 한 축을 128로 조절하고
            #transforms.CenterCrop(256),  # square를 한 후,
            transforms.ToTensor(),  # Tensor로 바꾸고 (0~1로 자동으로 normalize)
            transforms.Normalize(
                (0.5, 0.5, 0.5),  # -1 ~ 1 사이로 normalize
                (0.5, 0.5, 0.5)),  # (c - m)/s 니까...
        ]))
    test_loader = DataLoader(test_data,
                             batch_size=opts.batch_size,
                             shuffle=False,
                             num_workers=opts.num_processes)

    classes = train_data.classes
    print(classes)

    print("load model ...")
    if opts.model == 'resnet':
        model = models.resnet50(progress=True)
        model.load_state_dict(torch.load('resnet_model.pt'))
    elif opts.model == 'vggnet':
        model = models.vgg13_bn(progress=True)
        model.load_state_dict(torch.load('vggnet_model.pt'))
    elif opts.model == 'googlenet':
        model = models.googlenet(progress=True)
        model.load_state_dict(torch.load('googlenet_model.pt'))
    elif opts.model == 'densenet':
        model = models.densenet121(progress=True)
        model.load_state_dict(torch.load('densenet_model.pt'))
    else:
        model = models.resnext50_32x4d(progress=True)
        model.load_state_dict(torch.load('resnext_model.pt'))
    print(opts.model)
    model.cuda()

    print("start inference")

    idx = 0
    with torch.no_grad():
        with open(opts.model + '_result.txt', 'a') as f:
            for i, (inputs, labels) in enumerate(test_loader):
                inputs = inputs.cuda()
                outputs = model(inputs)
                _, predicted = outputs.max(1)
                for j, meta in enumerate(predicted):
                    predicted_class = classes[meta]
                    plant_class = predicted_class.split('_')[0]
                    disease_class = predicted_class.split('_')[1]
                    f.write(
                        str(test_loader.dataset.samples[idx][0].split('/')
                            [-1].split('.')[0]) + '\t' + plant_class + '\t' +
                        disease_class + '\n')
                    idx += 1
def main():
    # Number of images to perturb, 0 means "all of them"
    N = 0
    # List to hold L2 norms of r for all perturbed images so rho can be caluclated at the end
    r_arr = []
    # List to hold original labels
    orig_labels = []
    # List to hold perturbed labels
    pert_labels = []
    # List to hold L2 norms
    L2_norms = []
    # Cumulative sum for rho
    rho_sum = 0

    j = 0

    # Network you're using (can change to whatever)
    net = models.googlenet(pretrained=True)

    # Switch to evaluation mode
    net.eval()

    def clip_tensor(A, minv, maxv):
        A = torch.max(A, minv * torch.ones(A.shape))
        A = torch.min(A, maxv * torch.ones(A.shape))
        return A

    base = "../data/ILSVRC2012_img_val/"

    # Get list of files in ImageNet directory (MAKE SURE `base` ABOVE IS CORRECT)

    # First get the list of already perturbed files
    pert_walk_gen = os.walk(base + "perturbed/", topdown=True)
    _, _, pert_files_raw = next(pert_walk_gen)
    pert_files = [p for p in pert_files_raw
                  if p != ".gitignore"]  # Remove .gitignore

    # Now process the files
    raw_walk_gen = os.walk(base + "raw/", topdown=True)
    _, _, files_raw = next(raw_walk_gen)
    files_tmp = [f for f in files_raw
                 if f != ".gitignore"]  # Remove .gitignore
    files = [f for f in files_tmp if not (f in pert_files)
             ]  # Don't process pictures that have already been perturbed

    sorted_files = sorted(files, key=lambda item: int(item[18:23]))

    end = N if N != 0 else len(sorted_files)

    try:
        # Now for the correct number of images
        for name in sorted_files[0:end]:
            orig_img = Image.open(base + "raw/" + name)

            # Preprocessing only works for colour images
            if (orig_img.mode != "RBG"):
                orig_img = orig_img.convert(mode="RGB")

            if (orig_img.mode == "RGB"):  # Belt-and-suspenders check
                mean = [0.485, 0.456, 0.406]
                std = [0.229, 0.224, 0.225]

                # Get vector form of image so L2 norm of image x can be calculated (See denominator of eqn 15 in DeepFool paper)
                img_arr = np.array(orig_img)
                img_vect = img_arr.ravel()
                L2_norms.append(np.linalg.norm(img_vect))

                # Remove the mean
                im = transforms.Compose([
                    transforms.Resize(256),
                    #transforms.CenterCrop(224),
                    transforms.ToTensor(),
                    transforms.Normalize(mean=mean, std=std)
                ])(orig_img)

                r, loop_i, label_orig, label_pert, pert_image = deepfool(
                    im, net)

                # Add L2 norm of perturbation to array (See numerator of eqn 15 in DeepFool paper)
                r_norm = np.linalg.norm(r)
                r_arr.append(r_norm)

                # Add original labels and perturbed labels to array (just in case you need them later, not rlly using rn)
                orig_labels.append(label_orig)
                pert_labels.append(label_pert)

                labels = open(os.path.join('synset_words.txt'),
                              'r').read().split('\n')

                str_label_orig = labels[np.int(label_orig)].split(',')[0]
                str_label_pert = labels[np.int(label_pert)].split(',')[0]

                print("Original label = ", str_label_orig)
                print("Perturbed label = ", str_label_pert)
                print("Number of Iterations = ", loop_i)

                clip = lambda x: clip_tensor(x, 0, 255)

                tf = transforms.Compose([
                    transforms.Normalize(mean=[0, 0, 0],
                                         std=[(1 / 0.229), (1 / 0.244),
                                              (1 / 0.255)]),
                    transforms.Normalize(mean=[-0.485, -0.456, -0.406],
                                         std=[1, 1, 1]),
                    transforms.Lambda(clip),
                    transforms.ToPILImage(),
                    #transforms.Resize(256),
                    #transforms.CenterCrop(224)
                ])

                # Add to cumulative sum term to get rho (See eqn 15 in DeepFool paper)
                rho_sum = rho_sum + r_norm / np.linalg.norm(img_vect)

                # Write image file to directory to hold perturbed images
                if (os.path.exists(base + 'perturbed/') != 1):
                    os.mkdir(base + 'perturbed/')
                tf(pert_image.cpu()[0]).save(base + 'perturbed/' + name)

                # Create .csv with original saved image name and predicted label
                with open(base + 'orig_names_and_labels.csv', 'a') as orig_f:
                    writer = csv.writer(orig_f, lineterminator='\n')
                    writer.writerow([name, str(label_orig)])

                # Create .csv with perturbed saved image name and predicted label
                with open(base + 'pert_names_and_labels.csv', 'a') as pert_f:
                    writer = csv.writer(pert_f, lineterminator='\n')
                    writer.writerow([name, str(label_pert)])

                j += 1
            else:
                raise TypeError(
                    f"expected Image Mode RBG, got {orig_img.mode}")
    except KeyboardInterrupt:
        pass

    # Compute average robustness (rho) for the simulation (See eqn 15 in DeepFool paper)
    rho = (1 / j) * rho_sum
    print(f"Number of samples: {j}, Average robustness: {rho}")
    with open(base + "rho.txt", 'a') as rho_f:
        rho_f.write(f"Number of samples: {j}, Average robustness: {rho}\n")
Example #19
0
    def training(self):
        self.tr_loss_track, self.val_loss_track, self.tr_acc_track, self.val_acc_track, self.val_acc_history = [], [], [], [], []

        print('training', self.model_type, self.optim_type, self.learning_rate)
        # Defining pretrained model
        if self.model_type == "vgg19":
            model = models.vgg19(pretrained=True)
            model.classifier[6] = nn.Linear(4096, self.class_labels)
        elif self.model_type == "resnet50":
            model = models.resnet50(pretrained=True)
            num_features = model.fc.in_features
            model.fc = nn.Linear(num_features, self.class_labels)
        elif self.model_type == "resnet101":
            model = models.resnet101(pretrained=True)
            num_features = model.fc.in_features
            model.fc = nn.Linear(num_features, self.class_labels)
        elif self.model_type == "resnet151":
            model = models.resnet101(pretrained=True)
            num_features = model.fc.in_features
            model.fc = nn.Linear(num_features, self.class_labels)
        elif self.model_type == "googlenet":
            model = models.googlenet(pretrained=True)
            model.classifier = nn.Linear(1000, self.class_labels)
        elif self.model_type == "densenet121":
            model = models.densenet121(pretrained=True)
            num_features = model.classifier.in_features
            model.classifier = nn.Linear(num_features, self.class_labels)
        # #Without fine tuning
        elif self.model_type == "un_vgg19":
            # print("unpretrained1")
            model = models.vgg19(pretrained=False)
            model.classifier[6] = nn.Linear(4096, self.class_labels)
        # set pretrained =False for models
        elif self.model_type == "un_resnet50":
            # print("unpretrained2")
            model = models.resnet50(pretrained=False)
            num_features = model.fc.in_features
            model.fc = nn.Linear(num_features, self.class_labels)

        model = model.to(self.device)
        criterian = nn.CrossEntropyLoss()

        if self.optim_type == "adam":
            optimizer = torch.optim.Adam(model.parameters(),
                                         lr=self.learning_rate)
        elif self.optim_type == "sgd":
            optimizer = torch.optim.SGD(model.parameters(),
                                        lr=self.learning_rate,
                                        momentum=0.9)

        # training
        start_time = time.time()
        best_loss = float('inf')
        best_model = None
        best_acc = 0.0
        # best_model_wts = copy.deepcopy(model.state_dict())

        for epoch in range(1, self.num_epochs + 1):
            print('epoch {}/{}'.format(epoch, self.num_epochs))
            for t in ['train', 'val']:
                num_correct = 0.0
                num_samples = 0
                r_loss = 0.0
                running_corrects = 0

                if t == 'train':
                    # training mode
                    model.train()
                else:
                    # evaluate model
                    model.eval()

                count = 0
                for data in self.dataloaders[t]:
                    count += 1
                    # data has three types files, labels and filename
                    files, labels, filename = data

                    files = Variable(files.to(self.device))  # to gpu or cpu
                    labels = Variable(labels.to(self.device))

                    optimizer.zero_grad(
                    )  # clearning old gradient from last step

                    with torch.set_grad_enabled(t == 'train'):
                        pred = model(files)
                        # loss computation
                        loss = criterian(pred, labels)

                        _, prediction = torch.max(pred, 1)

                        # backprop gradients at training time
                        if t == 'train':
                            loss.backward()
                            optimizer.step()

                        # print(t +' iteration {}:loss {}  '.format(count,r_loss))
                    # statistics
                    r_loss += loss.item() * files.size(0)
                    print(t + ' iteration {}:loss {}  '.format(count, r_loss))
                    running_corrects += torch.sum(prediction == labels.data)
                epoch_loss = r_loss / len(self.dataloaders[t].dataset)
                epoch_acc = running_corrects.double() / len(
                    self.dataloaders[t].dataset)

                print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                    t, epoch_loss, epoch_acc))

                # print(t +' epoch {}:loss {}  '.format(epoch,r_loss))

                # deep copy the model
                # print('epoch_acc',epoch_acc,'best_acc',best_acc)
                if t == 'val' and epoch_acc > best_acc:
                    print('inside check point if')
                    best_acc = epoch_acc
                    best_model_wts = copy.deepcopy(model.state_dict())
                    self.checkpoint(best_model_wts, best_loss, epoch,
                                    self.learning_rate)
                if t == 'val':
                    self.val_acc_history.append(epoch_acc.item())
                    self.val_loss_track.append(epoch_loss)

                if t == 'train':
                    self.tr_loss_track.append(epoch_loss)
                    self.tr_acc_track.append(epoch_acc.item())

        time_elapsed = time.time() - start_time
        print('Training complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))
        print('Best val Acc: {:4f}'.format(best_acc))

        # load best model weights
        # model.load_state_dict(best_model_wts)
        # updating best model in checkpoint

        self.plot_losses_both(self.tr_loss_track, self.val_loss_track)
        self.plot_loss_Val(self.val_loss_track)
        self.plot_loss_Accu(self.val_acc_history)
 def test_googlenet(self):
     process_model(models.googlenet(), self.image,
                   _C_tests.forward_googlenet, "GoogLeNet")
def main():

    data_transforms = {
        "train":
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ]),
        "val":
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ]),
        "test":
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ]),
    }

    model_ft = models.googlenet(pretrained=True)
    num_ftrs = model_ft.fc.in_features
    # Here the size of each output sample is set to 2.
    # Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
    model_ft.aux_logits = False
    model_ft.fc = nn.Linear(num_ftrs, 2)
    device = "cpu"
    model_ft = model_ft.to(device)

    # Load model
    model_ft.load_state_dict(torch.load("weightsOld"))
    print("loaded weights")

    image = Image.open("data/train/notPotH/negative982.JPG")
    target_class = 1

    prep_image = data_transforms["val"](image)

    print(prep_image.shape)

    prep_image.requires_grad_()
    scores = model_ft(prep_image.reshape(1, *prep_image.shape))

    score_max_index = scores.argmax()
    print("index: ", score_max_index)
    score_max = scores[0, 0]
    score_max.backward()
    print(prep_image.grad.shape)
    saliency = prep_image.grad.numpy()
    saliency = np.abs(saliency)
    saliency = saliency.reshape(224, 224, 3)
    saliency = np.max(saliency, axis=2)
    print(
        f"max: {np.max(saliency):.3f} min: {np.min(saliency):.3f} mean: {np.mean(saliency):.3f}"
    )
    # saliency, _ = torch.max(prep_image.grad.data.abs(), dim=1)

    # print(saliency.shape)
    # saliency = exposure.rescale_intensity(saliency, (0.0, 1.0))
    saliency = exposure.equalize_hist(saliency)

    print(
        f"max: {np.max(saliency):.3f} min: {np.min(saliency):.3f} mean: {np.mean(saliency):.3f}"
    )
    plt.imshow(saliency, cmap=plt.cm.hot)
    plt.axis("off")
    plt.show()
Example #22
0
    def __init__(self, model_type=3, cnn_type=19, cnn_layer_test=0):
        super(Classic_feat, self).__init__()
        self.model_type = model_type
        self.cnn_type = cnn_type
        self.bn = True  # BenchNorm
        self.use_layer = cnn_layer_test

        # select cnn_model from AlexNet, GoogLeNet, VggNet
        if self.model_type == ClassicCNN.AlexNet.value:
            print('use AlexNet')
            self.model = models.alexnet(pretrained=True)
            self.sel_feat_layer = {0, 3, 6, 12}  # conv1, conv2, conv3, conv5

        elif self.model_type == ClassicCNN.GoogLeNet.value:
            if self.cnn_type == 1:  # GoogLeNet v1
                print('use GoogLeNet v1')
                self.model = models.googlenet(pretrained=True)
            else:
                print('use Inception v3')
                self.model = models.inception_v3(pretrained=True)

        elif self.model_type == ClassicCNN.VggNet.value:
            if cnn_type == 16:
                print('use Vgg16')
                self.model = models.vgg16(pretrained=True)  # 31
                self.sel_feat_layer = {
                    2, 7, 12, 19, 30
                }  # conv1_1, conv2_2, conv3_2, conv4_2, conv5_4
            else:  # use Vgg19 with bn
                self.cnn_type = 19
                if self.bn:
                    print('use Vgg19_bn')
                    self.model = models.vgg19_bn(pretrained=True)
                    self.sel_feat_layer = {
                        3, 10, 17, 30, 52
                    }  # conv1, conv2, conv3, conv4, conv5
                else:
                    print('use Vgg19')
                    self.model = models.vgg19(pretrained=True)  # 37
                    self.sel_feat_layer = {
                        2, 7, 12, 21, 36
                    }  # conv1, conv2, conv3, conv4, conv5

        # use grad-CAM
        params = list(self.model.parameters())
        self.weight_softmax = np.squeeze(params[-2].data.numpy())
        self.CAMs = None

        self.model.eval()
        if use_gpu:
            print('use cuda model')
            self.model = self.model.cuda()
        if self.model_type != ClassicCNN.GoogLeNet.value:
            self.features = nn.ModuleList(list(self.model.features)).eval()
            self.classify = nn.ModuleList(list(self.model.classifier)).eval()

        # test feature
        if self.use_layer == 0:  # run combine cnn_features
            self.use_layer = -4
            print('test linear combined cnn_features')
        elif self.use_layer > 4:  # run combine features with grad-CAM
            print('test combined cnn_features with grad-CAM')
        else:  # test each conv layers
            print('test single conv layer', self.use_layer)
def Netowork_test(test):
    transform = transforms.Compose([  # [1]
        transforms.Resize(256),  # [2]
        transforms.CenterCrop(224),  # [3]
        transforms.ToTensor(),  # [4]
        transforms.Normalize(  # [5]
            mean=[0.485, 0.456, 0.406],  # [6]
            std=[0.229, 0.224, 0.225]  # [7]
        )
    ])
    img = Image.open(test)
    img_t = transform(img)
    batch_t = torch.unsqueeze(img_t, 0)
    Alexnet = models.alexnet(True)
    Alexnet.eval()
    out_alexnet = Alexnet(batch_t)
    VGG_16 = models.vgg16_bn(True)
    VGG_16.eval()
    out_Vgg_16 = VGG_16(batch_t)
    Googlenet = models.googlenet(True)
    Googlenet.eval()
    out_googlenet = Googlenet(batch_t)
    # Inception = models.inception_v3(True)
    # Inception.eval()
    # out_inception = Inception(batch_t)

    test_app = "\n" + test + "\n"

    with open('imagenet_classes.txt') as f:
        classes = [line.strip() for line in f.readlines()]
    print("Alex-Net")
    _, indices = torch.sort(out_alexnet, descending=True)
    percentage = torch.nn.functional.softmax(out_alexnet, dim=1)[0] * 100
    # [print (classes[idx], percentage[idx].item()) for idx in indices[0][:5]]
    file1 = open("Alexnet_without_prob.txt", "a+")
    file2 = open("Alexnet.txt", "a+")
    file1.write(test_app)
    file2.write(test_app)

    for idx in indices[0][:5]:
        x, y = classes[idx], percentage[idx].item()
        file1.writelines("__" + str(x))
        file2.writelines(str(x) + str(y) + "\n")
    file1.close()
    file2.close()

    print("VGG-16")
    _, indices = torch.sort(out_Vgg_16, descending=True)
    percentage = torch.nn.functional.softmax(out_Vgg_16, dim=1)[0] * 100
    # [print(classes[idx], percentage[idx].item()) for idx in indices[0][:5]]
    file1 = open("VGG_16_without_prob.txt", "a+")
    file2 = open("VGG_16.txt", "a+")
    file1.write(test_app)
    file2.write(test_app)
    for idx in indices[0][:5]:
        x, y = classes[idx], percentage[idx].item()
        file1.writelines("__" + str(x))
        file2.writelines(str(x) + str(y) + "\n")
    file1.close()
    file2.close()

    print("Googlenet")
    _, indices = torch.sort(out_googlenet, descending=True)
    percentage = torch.nn.functional.softmax(out_googlenet, dim=1)[0] * 100
    # [print(classes[idx], percentage[idx].item()) for idx in indices[0][:5]]

    file1 = open("GoogleNet_without_prob.txt", "a+")
    file2 = open("GoogleNet.txt", "a+")
    file1.write(test_app)
    file2.write(test_app)
    for idx in indices[0][:5]:
        x, y = classes[idx], percentage[idx].item()
        file1.writelines("__" + str(x))
        file2.writelines(str(x) + str(y) + "\n")
    file1.close()
    file2.close()
            transforms.CenterCrop(227),
            transforms.ToTensor()
        ]),
    }

    image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
                                              data_transforms[x])
                      for x in ['train', 'val']}
    dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=20,
                                                  shuffle=True, num_workers=4)
                   for x in ['train', 'val']}
    dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
    class_names = image_datasets['train'].classes
    print("class_names", class_names)

    model_ft = models.googlenet(pretrained=False,num_classes=2)

    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
        model_ft = nn.DataParallel(model_ft)

    model_ft = model_ft.to(device)

    criterion = nn.CrossEntropyLoss()

    # Observe that all parameters are being optimized
    # optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
    optimizer_ft = optim.Adam(model_ft.parameters())

    # Decay LR by a factor of 0.1 every 7 epochs
Example #25
0
BATCH_SIZE = 10
EPOCH = 5
LR = 1e-3

full_dataset = dataset("traindata.h5")
train_size = int(len(full_dataset)*0.8)
valid_size = len(full_dataset) - train_size
train_dataset, valid_dataset = torch.utils.data.random_split(full_dataset, [train_size, valid_size])
print("Train Data Size:", len(train_dataset))
print("Valid Data Size:", len(valid_dataset))
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, num_workers=0, shuffle=True)
valid_loader = DataLoader(valid_dataset, batch_size=1, num_workers=0, shuffle=False)

vgg16 = models.vgg16(pretrained=True)
vgg16.eval()
googlenet = models.googlenet(pretrained=True)
googlenet.eval()

model = "vgg"

if model == "vgg":
    net = MLFFNN([25088, 4096, 1024])
elif model == "googlenet":
    net = MLFFNN([1024, 512, 128])

criterion = nn.CrossEntropyLoss()
mse = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=LR)

net.to(DEVICE)
googlenet.to(DEVICE)
Example #26
0
 def __init__(self, sample_nums=16):
     super(SingleObjRegressor, self).__init__()
     self.backbone = models.googlenet(pretrained=True)
     self.sample_nums = sample_nums
def main():

    m_Wts_preload = None  #"recentPreWeights"        # None/ filename of saved weights
    b_size = 192  # batch size
    tb_comment = "_1GN_E40_128_Ad_WM_IN_lr-15_b1-0.92_LWts(samSize)_60_epochs"  # comment for tensorboard to save file
    data_dir = "data"  # data directory
    num_epoch = 60  # num epochs
    l_rate = 0.001  #learning rate
    lr_step = 15  #decay of l_rate after these steps
    w_decay = 0.001  #Adam
    betas = (0.92, 0.999)  #Adam
    momentum = 0.9  # SGD momentum
    weightz = [1558 / 9247,
               7689 / 9247]  #weights for loss function as per sample size
    statistics_file = "meanAndSD.npy"

    #data transformations and augmentation
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'test':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    image_datasets = {
        x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
        for x in ["train", "val", "test"]
    }
    dataloaders = {
        x: torch.utils.data.DataLoader(
            image_datasets[x],
            batch_size=b_size,
            shuffle=True,
            num_workers=4,
            pin_memory=True,
        )
        for x in ["train", "val", "test"]
    }
    dataset_sizes = {
        x: len(image_datasets[x])
        for x in ["train", "val", "test"]
    }
    class_names = image_datasets["train"].classes

    #choose GPU if available
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    def imshow(inp, title=None):
        """Imshow for Tensor."""
        inp = inp.numpy().transpose((1, 2, 0))
        mean = np.array([0.485, 0.456, 0.406])
        std = np.array([0.229, 0.224, 0.225])
        inp = std * inp + mean
        inp = np.clip(inp, 0, 1)
        plt.imshow(inp)
        if title is not None:
            plt.title(title)
        plt.show()
        # plt.pause(0.001)  # pause a bit so that plots are updated

    # Get a batch of training data
    inputs, classes = next(iter(dataloaders["train"]))

    # Make a grid from batch
    out = torchvision.utils.make_grid(inputs)

    imshow(out, title=[class_names[x] for x in classes])

    # writer object for Tensorboard
    writer = SummaryWriter(comment=tb_comment)

    #function to plot confusion matrix from a confusion matrix
    def plot_confusion_matrix(cm,
                              target_names,
                              title="Confusion matrix",
                              cmap=None,
                              normalize=True):

        accuracy = np.trace(cm) / float(np.sum(cm))
        misclass = 1 - accuracy

        if cmap is None:
            cmap = plt.get_cmap("Blues")

        fig = plt.figure(figsize=(8, 6))
        plt.imshow(cm, interpolation="nearest", cmap=cmap)
        plt.title(title)
        plt.colorbar()

        if target_names is not None:
            tick_marks = np.arange(len(target_names))
            plt.xticks(tick_marks, target_names, rotation=45)
            plt.yticks(tick_marks, target_names)

        if normalize:
            cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]

        thresh = cm.max() / 1.5 if normalize else cm.max() / 2
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            if normalize:
                plt.text(
                    j,
                    i,
                    "{:0.4f}".format(cm[i, j]),
                    horizontalalignment="center",
                    color="white" if cm[i, j] > thresh else "black",
                )
            else:
                plt.text(
                    j,
                    i,
                    "{:,}".format(cm[i, j]),
                    horizontalalignment="center",
                    color="white" if cm[i, j] > thresh else "black",
                )

        plt.tight_layout()
        plt.ylabel("True label")
        plt.xlabel(
            "Predicted label\naccuracy={:0.4f}; misclass={:0.4f}".format(
                accuracy, misclass))
        writer.add_figure("Confusion_Matrix",
                          fig,
                          global_step=None,
                          close=True,
                          walltime=None)
        plt.show()

    def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
        since = time.time()

        #to load pre-trained weights
        if m_Wts_preload is not None:
            print("Loading locally pre-trained weights...")
            model.load_state_dict(torch.load(m_Wts_preload))

        bestPre_model_wts = copy.deepcopy(model.state_dict())
        bestRec_model_wts = copy.deepcopy(model.state_dict())

        best_precision = 0.0
        best_recall = 0.0

        for epoch in range(num_epochs):
            print("Epoch {}/{}".format(epoch, num_epochs - 1))
            print("-" * 10)
            # Each epoch has a training and validation phase
            for phase in ["train", "val", "test"]:
                epoch_preds = None
                epoch_labels = None
                if phase == "train":
                    model.train()  # Set model to training mode
                else:
                    model.eval()  # Set model to evaluate mode
                running_loss = 0.0
                running_corrects = 0

                # Iterate over data.
                for inputs, labels in dataloaders[phase]:
                    inputs = inputs.to(device)
                    labels = labels.to(device)

                    # zero the parameter gradients
                    optimizer.zero_grad()

                    # forward
                    # track history if only in train
                    with torch.set_grad_enabled(phase == "train"):
                        outputs = model(inputs)
                        # print(outputs.shape)s
                        _, preds = torch.max(outputs, 1)
                        loss = criterion(outputs, labels)

                        # backward + optimize only if in training phase
                        if phase == "train":
                            loss.backward()
                            optimizer.step()

                    # statistics
                    running_loss += loss.item() * inputs.size(0)
                    running_corrects += torch.sum(preds == labels.data)

                    if epoch_labels is None:
                        epoch_labels = labels.cpu().numpy()
                    else:
                        epoch_labels = np.concatenate(
                            (epoch_labels, labels.cpu().numpy()))

                    # epoch_labels.append(label)
                    if epoch_preds is None:
                        epoch_preds = preds.cpu().numpy()
                    else:
                        epoch_preds = np.concatenate(
                            (epoch_preds, preds.cpu().numpy()))
                    # epoch_preds.append(preds)

                if phase == "train":
                    scheduler.step()

                # calculation of evaluation metrics
                epoch_loss = running_loss / dataset_sizes[phase]
                epoch_acc = running_corrects.double() / dataset_sizes[phase]
                epoch_prec = precision_score(epoch_labels,
                                             epoch_preds,
                                             average="weighted")
                epoch_rec = recall_score(epoch_labels,
                                         epoch_preds,
                                         average="weighted")
                epoch_f1 = f1_score(epoch_labels,
                                    epoch_preds,
                                    average="weighted")

                #plot confusion matrix for validation and test sets only
                if phase in ["val", "test"] and epoch == num_epochs - 1:
                    cm = confusion_matrix(epoch_labels,
                                          epoch_preds,
                                          labels=None,
                                          normalize=None)

                    plot_confusion_matrix(
                        cm,
                        target_names=["notPotH", "potH"],
                        title="Confusion Matrix " + phase + " set",
                        cmap=None,
                        normalize=False,
                    )
                    plot_confusion_matrix(
                        cm,
                        target_names=["notPotH", "potH"],
                        title="Normalized Confusion Matrix " + phase + " set",
                        cmap=None,
                        normalize=True,
                    )

                #write to tensorboard
                writer.add_scalars("loss", {phase: epoch_loss}, epoch)
                writer.add_scalars("precision", {phase: epoch_prec}, epoch)
                writer.add_scalars("recall", {phase: epoch_rec}, epoch)
                writer.add_scalars("f1_score", {phase: epoch_f1}, epoch)

                writer.flush()
                print(
                    "{} Loss: {:.4f} Acc: {:.4f} Ep_Prec: {:.4f} Ep_Rec: {:.4f} Ep_F1Score: {:.4f}"
                    .format(phase, epoch_loss, epoch_acc, epoch_prec,
                            epoch_rec, epoch_f1))

                # deep copy the model
                if phase == "val" and epoch_prec > best_precision:
                    best_precision = epoch_prec
                    bestPre_model_wts = copy.deepcopy(model.state_dict())
                if phase == "val" and epoch_rec > best_recall:
                    best_recall = epoch_rec
                    bestRec_model_wts = copy.deepcopy(model.state_dict())
            if (epoch % 3 == 0 or epoch
                    == num_epoch):  # save model weights every 3rd epoch
                torch.save(model.state_dict(), "recentWtz")

            print("Time Elapsed:", (time.time() - since) / 60, " mins")
            print()

        time_elapsed = time.time() - since
        print("Training complete in {:.0f}m {:.0f}s".format(
            time_elapsed // 60, time_elapsed % 60))
        print("Best val Prec: {:4f} Rec: {:4f}".format(best_precision,
                                                       best_recall))

        torch.save(bestPre_model_wts, "recentPreWeights")
        torch.save(bestRec_model_wts, "recentRecWeights")
        return model

    def visualize_model(model, num_images=6):
        was_training = model.training
        model.eval()
        images_so_far = 0
        fig = plt.figure()

        with torch.no_grad():
            for i, (inputs, labels) in enumerate(dataloaders["val"]):
                inputs = inputs.to(device)
                labels = labels.to(device)

                outputs = model(inputs)
                _, preds = torch.max(outputs, 1)

                for j in range(inputs.size()[0]):
                    images_so_far += 1
                    ax = plt.subplot(num_images // 2, 2, images_so_far)
                    ax.axis("off")
                    ax.set_title("predicted: {}".format(class_names[preds[j]]))
                    imshow(inputs.cpu().data[j])

                    if images_so_far == num_images:
                        model.train(mode=was_training)
                        return
            model.train(mode=was_training)

    model_ft = models.googlenet(pretrained=True)
    #model_ft = models.resnet18(pretrained=True)

    num_ftrs = model_ft.fc.in_features
    # Here the size of each output sample is set to 2.
    # Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
    model_ft.aux_logits = False
    model_ft.fc = nn.Linear(num_ftrs, 2)

    model_ft = model_ft.to(device)
    # print(torch.from_numpy(np.float32(weightz)))
    criterion = nn.CrossEntropyLoss(
        weight=torch.from_numpy(np.float32(weightz)).to(device))

    # Observe that all parameters are being optimized
    # optimizer_ft = optim.SGD(model_ft.parameters(), lr=l_rate, momentum=momentum)
    optimizer_ft = optim.Adam(model_ft.parameters(),
                              lr=l_rate,
                              betas=betas,
                              weight_decay=w_decay)

    # Decay LR by a factor of 0.1 every 7 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft,
                                           step_size=lr_step,
                                           gamma=0.1)

    model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
                           num_epoch)

    torch.save(model_ft.state_dict(), "recentWtzz")

    visualize_model(model_ft)
    writer.close()
Example #28
0
    def __init__(self, *args, **kwargs):
        super().__init__()
        self.num_classes = kwargs['num_classes']
        self.batch_size = kwargs['batch_size']  # 64
        # Pretrained image model
        self.inception = models.googlenet(pretrained=True, aux_logits=True)
        # set_parameter_requires_grad(model=self.inception, feature_extracting=True)
        # Handle the auxilary net
        self.num_ftrs = self.inception.aux1.fc2.in_features
        self.inception.aux1.fc2 = nn.Linear(self.num_ftrs, self.num_classes)
        self.num_ftrs = self.inception.aux2.fc2.in_features
        self.inception.aux2.fc2 = nn.Linear(self.num_ftrs, self.num_classes)
        # Handle the primary net
        self.num_ftrs = self.inception.fc.in_features
        print('self.num_ftrs: {}'.format(self.num_ftrs))
        self.inception.fc = nn.Linear(self.num_ftrs, self.num_classes)
        # Return features before fc layer.
        self.inception.fc = nn.Identity()
        # print('self.inception:\n{}'.format(self.inception))
        self.image_size = 224

        # Text model
        self.vocab_size = kwargs['vocab_size']
        self.embedding_dim = kwargs['embedding_dim']  # 50
        # self.glove_embedding = kwargs['glove_embedding']
        # # self.word_embedding = nn.Embedding(num_embeddings=self.vocab_size,
        # #   embedding_dim=self.embedding_dim)
        # # self.word_embedding.load_state_dict({'weight': self.glove_embedding})
        # self.word_embedding = nn.Embedding.from_pretrained(
        #   embeddings=self.glove_embedding)

        params_model = {
            'bsize': self.batch_size,
            'word_emb_dim': self.embedding_dim,
            'enc_lstm_dim': 2048,
            'pool_type': 'max',
            'dpout_model': 0.0,
            'version': 1
        }
        self.infersent = InferSent(params_model)
        self.infersent.load_state_dict(
            torch.load(
                os.path.join(os.getcwd(), '../data/encoder/infersent1.pkl')))
        self.infersent.set_w2v_path(w2v_path=os.path.join(
            os.getcwd(), '../data/glove/glove.840B.300d.txt'))
        self.infersent.build_vocab_k_words(K=self.vocab_size)
        print('self.infersent:\n{}'.format(self.infersent))

        # LSTM
        self.hidden_size = 1024
        # self.lstm = nn.LSTM(input_size=4096,
        #   hidden_size=self.hidden_size, num_layers=1)
        # print('self.lstm:\n{}'.format(self.lstm))

        # Fully connected layers
        self.fc_size = 512
        self.encode_dim = 4096
        self.fc1 = nn.Linear(in_features=self.num_ftrs + self.encode_dim,
                             out_features=self.fc_size)
        self.fc1_bn = nn.BatchNorm1d(num_features=self.fc_size)
        self.fc2 = nn.Linear(in_features=self.fc_size,
                             out_features=self.num_classes)
        print('self.fc1:\n{}'.format(self.fc1))
        print('self.fc2:\n{}'.format(self.fc2))
Example #29
0
def main(train_loader, train_datasets, valid_loader):
    '''args: model_arch, train_loader, train_datasets, valid_loader, epochs, learning_rate, hidden_units, device, save_dir

    saves checkpoint under save_dir

    returns: checkpoint with {'epoch_tot',
                              'model',
                              'criterion',
                              'optimizer',
                              'optimizer.state_dict',
                              'model.state_dict',
                              'model.class_to_idx'}'''

    arg = input_args()
    model_arch = arg.arch
    hidden_units = arg.hidden_units
    learning_rate = arg.learning_rate
    device = arg.device
    epochs = arg.epochs
    save_dir = arg.save_dir

    if model_arch == 'alexnet':
        gs_vgg = models.alexnet(pretrained=True)
    elif model_arch == 'vgg11':
        gs_vgg = models.vgg11(pretrained=True)
    elif model_arch == 'vgg11_bn':
        gs_vgg = models.vgg11_bn(pretrained=True)
    elif model_arch == 'vgg13':
        gs_vgg = models.vgg13(pretrained=True)
    elif model_arch == 'vgg13_bn':
        gs_vgg = models.vgg13_bn(pretrained=True)
    elif model_arch == 'vgg16':
        gs_vgg = models.vgg16(pretrained=True)
    elif model_arch == 'vgg16_bn':
        gs_vgg = models.vgg16_bn(pretrained=True)
    elif model_arch == 'vgg19':
        gs_vgg = models.vgg19(pretrained=True)
    elif model_arch == 'vgg19_bn':
        gs_vgg = models.vgg19_bn(pretrained=True)
    elif model_arch == 'resnet18':
        gs_vgg = models.resnet18(pretrained=True)
    elif model_arch == 'resnet34':
        gs_vgg = models.resnet34(pretrained=True)
    elif model_arch == 'resnet50':
        gs_vgg = models.resnet50(pretrained=True)
    elif model_arch == 'resnet101':
        gs_vgg = models.resnet101(pretrained=True)
    elif model_arch == 'resnet152':
        gs_vgg = models.resnet152(pretrained=True)
    elif model_arch == 'squeezenet1_0':
        gs_vgg = models.squeezenet1_0(pretrained=True)
    elif model_arch == 'squeezenet1_1':
        gs_vgg = models.squeezenet1_1(pretrained=True)
    elif model_arch == 'densenet121':
        gs_vgg = models.densenet121(pretrained=True)
    elif model_arch == 'densenet169':
        gs_vgg = models.densenet169(pretrained=True)
    elif model_arch == 'densenet161':
        gs_vgg = models.densenet161(pretrained=True)
    elif model_arch == 'densenet201':
        gs_vgg = models.densenet201(pretrained=True)
    elif model_arch == 'inception_v3':
        gs_vgg = models.inception_v3(pretrained=True)
    elif model_arch == 'googlenet':
        gs_vgg = models.googlenet(pretrained=True)
    elif model_arch == 'shufflenet_v2_x0_5':
        gs_vgg = models.shufflenet_v2_x0_5(pretrained=True)
    elif model_arch == 'shufflenet_v2_x1_0':
        gs_vgg = models.shufflenet_v2_x1_0(pretrained=True)
    elif model_arch == 'shufflenet_v2_x1_5':
        gs_vgg = models.shufflenet_v2_x1_5(pretrained=True)
    elif model_arch == 'shufflenet_v2_x2_0':
        gs_vgg = models.shufflenet_v2_x2_0(pretrained=True)
    elif model_arch == 'mobilenet_v2':
        gs_vgg = models.mobilenet_v2(pretrained=True)
    elif model_arch == 'resnext50_32x4d':
        gs_vgg = models.resnext50_32x4d(pretrained=True)
    elif model_arch == 'resnext101_32x8d':
        gs_vgg = models.resnext101_32x8d(pretrained=True)
    elif model_arch == 'wide_resnet50_2':
        gs_vgg = models.wide_resnet50_2(pretrained=True)
    elif model_arch == 'wide_resnet101_2':
        gs_vgg = models.wide_resnet101_2(pretrained=True)
    elif model_arch == 'mnasnet0_5':
        gs_vgg = models.mnasnet0_5(pretrained=True)
    elif model_arch == 'mnasnet0_75':
        gs_vgg = models.mnasnet0_75(pretrained=True)
    elif model_arch == 'mnasnet1_0':
        gs_vgg = models.mnasnet1_0(pretrained=True)
    elif model_arch == 'mnasnet1_3':
        gs_vgg = models.mnasnet1_3(pretrained=True)

    epoch_tot = 0

    for parameters in gs_vgg.parameters():
        parameters.requires_grad = False

    try:
        input_layer = gs_vgg.classifier[0].in_features
    except:
        input_layer = gs_vgg.classifier.in_features
    hidden_layers = [(int(hidden_units * 0.68)), (int(hidden_units * 0.32))]
    output_layer = len(train_loader)

    gs_vgg.classifier = nn.Sequential(
        nn.Linear(input_layer, hidden_layers[0]),
        nn.ReLU(),
        nn.Dropout(p=0.3),
        nn.Linear(hidden_layers[0], hidden_layers[1]),
        nn.ReLU(),
        #nn.Linear(hidden_layers[1], output_layer),
        nn.Linear(hidden_layers[1], 102),
        nn.LogSoftmax(dim=1))

    criterion = nn.NLLLoss()
    optimizer = optim.Adam(gs_vgg.classifier.parameters(), lr=learning_rate)

    gs_vgg.to(device)
    step_num = 0
    epoch = epochs
    running_loss = 0
    print_every = 10
    for e in range(epoch):
        epoch_tot += 1
        for images, labels in train_loader:

            gs_vgg.train()

            images, labels = images.to(device), labels.to(device)

            optimizer.zero_grad()

            output = gs_vgg.forward(images)
            loss = criterion(output, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            step_num += 1

            if step_num % print_every == 0:
                test_loss = 0
                accuracy = 0
                gs_vgg.eval()
                for images, labels in valid_loader:
                    images, labels = images.to(device), labels.to(device)
                    output = gs_vgg.forward(images)
                    loss = criterion(output, labels)

                    test_loss += loss.item()

                    prob = torch.exp(output)
                    top_p, top_class = prob.topk(1, dim=1)
                    equals = top_class == labels.view(*top_class.shape)
                    accuracy += torch.mean(equals.type(
                        torch.FloatTensor)).item()

                print(
                    f"Total Epochs: {epoch_tot}.. "
                    f"Train loss: {running_loss/print_every:.3f}.. "
                    f"Test loss: {test_loss/len(valid_loader):.3f}.. "
                    f"Test accuracy: {(accuracy/len(valid_loader))*100:.1f}%")
                running_loss = 0
                gs_vgg.train()
    gs_vgg.class_to_idx = train_datasets.class_to_idx
    gs_checkpoint = {
        'epoch_tot': epoch_tot,
        'model': gs_vgg,
        'criterion': criterion,
        'optimizer': optimizer,
        'optimizer.state_dict': optimizer.state_dict(),
        'model.state_dict': gs_vgg.state_dict(),
        'model.class_to_idx': gs_vgg.class_to_idx
    }
    torch.save(gs_checkpoint, save_dir)
    return gs_checkpoint
Example #30
0
    def __init__(self,
                 data_dir='./data/',
                 model='resnet50',
                 save_features='False',
                 save_file='features',
                 max_pooling='False',
                 generate_vat='False',
                 resize=224):

        self.data_dir = data_dir
        self.model = model
        self.save_features = save_features
        self.save_file = save_file
        self.max_pooling = max_pooling
        self.generate_vat = generate_vat
        self.resize = resize
        self.num_images = len(os.listdir(data_dir))

        # Let's get our class labels.
        LABELS_URL = 'https://s3.amazonaws.com/outcome-blog/imagenet/labels.json'
        response = requests.get(
            LABELS_URL)  # Make an HTTP GET request and store the response.
        labels = {int(key): value for key, value in response.json().items()}

        self.trans = transforms.Compose([
            transforms.Resize((resize, resize)),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])

        if (self.model.lower() == 'alexnet'):

            self.model1 = models.alexnet(pretrained=True)

        elif (self.model.lower() == 'google'):

            self.model1 = models.googlenet(pretrained=True)

        elif (self.model.lower() == 'resnet18'):

            self.model1 = models.resnet18(pretrained=True)

        elif (self.model.lower() == 'resnet50'):

            self.model1 = models.resnet50(pretrained=True)

        elif (self.model.lower() == 'resnet101'):

            self.model1 = models.resnet101(pretrained=True)

        elif (self.model.lower() == 'vgg'):

            self.model1 = models.vgg16(pretrained=True)

        else:

            print('\nInvalid model.... Exiting!\n')
            exit()

        self.new_model = torch.nn.Sequential(
            *list(self.model1.children())[:-2])